UNPKG

@itwin/presentation-components

Version:

React components based on iTwin.js Presentation library

65 lines 3.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ViewportSelectionHandlerContextProvider = ViewportSelectionHandlerContextProvider; exports.useViewportSelectionHandlerContext = useViewportSelectionHandlerContext; exports.viewWithUnifiedSelection = viewWithUnifiedSelection; const jsx_runtime_1 = require("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 */ require("../common/DisposePolyfill.js"); const react_1 = require("react"); const Utils_js_1 = require("../common/Utils.js"); const ViewportSelectionHandler_js_1 = require("./ViewportSelectionHandler.js"); const ViewportSelectionHandlerContext = (0, react_1.createContext)(undefined); /** @internal */ function ViewportSelectionHandlerContextProvider({ selectionHandler, children }) { return (0, jsx_runtime_1.jsx)(ViewportSelectionHandlerContext.Provider, { value: selectionHandler, children: children }); } /** @internal */ function useViewportSelectionHandlerContext() { return (0, react_1.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. */ function viewWithUnifiedSelection(ViewportComponent) { const WithUnifiedSelection = (0, react_1.memo)((props) => { const { imodel } = props; const [viewportSelectionHandler, setViewportSelectionHandler] = (0, react_1.useState)(); const selectionHandler = useViewportSelectionHandlerContext(); // apply currentSelection when 'viewportSelectionHandler' is initialized (set to handler from props or new is created) (0, react_1.useEffect)(() => { if (selectionHandler) { selectionHandler.applyCurrentSelection(); setViewportSelectionHandler(selectionHandler); return; } const handler = new ViewportSelectionHandler_js_1.ViewportSelectionHandler({ imodel }); handler.applyCurrentSelection(); setViewportSelectionHandler(handler); return () => { handler[Symbol.dispose](); }; }, [selectionHandler, imodel]); // set new imodel on 'viewportSelectionHandler' when it changes (0, react_1.useEffect)(() => { if (!viewportSelectionHandler) { return; } viewportSelectionHandler.imodel = imodel; }, [viewportSelectionHandler, imodel]); return (0, jsx_runtime_1.jsx)(ViewportComponent, { ...props }); }); WithUnifiedSelection.displayName = `WithUnifiedSelection(${(0, Utils_js_1.getDisplayName)(ViewportComponent)})`; return WithUnifiedSelection; } //# sourceMappingURL=WithUnifiedSelection.js.map