@itwin/presentation-components
Version:
React components based on iTwin.js Presentation library
60 lines • 2.81 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Viewport
*/
import "../common/DisposePolyfill.js";
import { createContext, memo, useContext, useEffect, useState } from "react";
import { getDisplayName } from "../common/Utils.js";
import { ViewportSelectionHandler } from "./ViewportSelectionHandler.js";
const ViewportSelectionHandlerContext = createContext(undefined);
/** @internal */
export function ViewportSelectionHandlerContextProvider({ selectionHandler, children }) {
return _jsx(ViewportSelectionHandlerContext.Provider, { value: selectionHandler, children: children });
}
/** @internal */
export function useViewportSelectionHandlerContext() {
return useContext(ViewportSelectionHandlerContext);
}
/**
* A HOC component that adds unified selection functionality to the supplied
* viewport component.
*
* @public
* @deprecated in 5.7. Use `enableUnifiedSelectionSyncWithIModel` from `@itwin/unified-selection` package instead.
*/
export function viewWithUnifiedSelection(ViewportComponent) {
const WithUnifiedSelection = memo((props) => {
const { imodel } = props;
const [viewportSelectionHandler, setViewportSelectionHandler] = useState();
const selectionHandler = useViewportSelectionHandlerContext();
// apply currentSelection when 'viewportSelectionHandler' is initialized (set to handler from props or new is created)
useEffect(() => {
if (selectionHandler) {
selectionHandler.applyCurrentSelection();
setViewportSelectionHandler(selectionHandler);
return;
}
const handler = new ViewportSelectionHandler({ imodel });
handler.applyCurrentSelection();
setViewportSelectionHandler(handler);
return () => {
handler[Symbol.dispose]();
};
}, [selectionHandler, imodel]);
// set new imodel on 'viewportSelectionHandler' when it changes
useEffect(() => {
if (!viewportSelectionHandler) {
return;
}
viewportSelectionHandler.imodel = imodel;
}, [viewportSelectionHandler, imodel]);
return _jsx(ViewportComponent, { ...props });
});
WithUnifiedSelection.displayName = `WithUnifiedSelection(${getDisplayName(ViewportComponent)})`;
return WithUnifiedSelection;
}
//# sourceMappingURL=WithUnifiedSelection.js.map