UNPKG

@itwin/presentation-components

Version:

React components based on iTwin.js Presentation library

72 lines 4.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnifiedSelectionContextProvider = UnifiedSelectionContextProvider; exports.useUnifiedSelectionContext = useUnifiedSelectionContext; 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. *--------------------------------------------------------------------------------------------*/ /* eslint-disable @typescript-eslint/no-deprecated */ /** * @packageDocumentation * @module UnifiedSelection */ const react_1 = require("react"); const core_bentley_1 = require("@itwin/core-bentley"); const presentation_frontend_1 = require("@itwin/presentation-frontend"); const Utils_js_1 = require("../common/Utils.js"); /** * Unified Selection context provider. It adapts framework-agnostic * [Unified Selection]($docs/presentation/unified-selection/index.md) API to be better suited for React * applications. The provided context is accessible via [[useUnifiedSelectionContext]] hook. * @public * @deprecated in 5.9.0. Use `UnifiedSelectionContextProvider` from `@itwin/unified-selection-react` package instead. */ function UnifiedSelectionContextProvider(props) { const selectionLevel = props.selectionLevel ?? 0; const contextRef = (0, react_1.useRef)(); if (contextRef.current?.imodel !== props.imodel || contextRef.current.selectionLevel !== selectionLevel) { contextRef.current = createSelectionContext(props.imodel, selectionLevel); } const [_, setState] = (0, react_1.useState)({}); (0, react_1.useEffect)(() => { const currentContext = contextRef.current; (0, core_bentley_1.assert)(currentContext !== undefined); return presentation_frontend_1.Presentation.selection.selectionChange.addListener((args) => { if (args.level > currentContext.selectionLevel) { return; } contextRef.current = { ...currentContext, getSelection: createGetSelection(props.imodel, selectionLevel), }; setState({}); }); }, [props.imodel, selectionLevel]); return (0, jsx_runtime_1.jsx)(unifiedSelectionContext.Provider, { value: contextRef.current, children: props.children }); } function createSelectionContext(imodel, selectionLevel) { return { imodel, selectionLevel, getSelection: createGetSelection(imodel, selectionLevel), replaceSelection: (keys, level) => presentation_frontend_1.Presentation.selection.replaceSelection("UnifiedSelectionContext", imodel, keys, level ?? selectionLevel), addToSelection: (keys, level) => presentation_frontend_1.Presentation.selection.addToSelection("UnifiedSelectionContext", imodel, keys, level ?? selectionLevel), clearSelection: (level) => presentation_frontend_1.Presentation.selection.clearSelection("UnifiedSelectionContext", imodel, level ?? selectionLevel), removeFromSelection: (keys, level) => presentation_frontend_1.Presentation.selection.removeFromSelection("UnifiedSelectionContext", imodel, keys, level ?? selectionLevel), }; } function createGetSelection(imodel, selectionLevel) { return (0, Utils_js_1.memoize)((level) => new Proxy(presentation_frontend_1.Presentation.selection.getSelection(imodel, level ?? selectionLevel), {}), { maxSize: Number.MAX_SAFE_INTEGER }); } const unifiedSelectionContext = (0, react_1.createContext)(undefined); /** * Returns Unified Selection context provided by [[UnifiedSelectionContextProvider]]. * @public * @deprecated in 5.9.0. Use `useUnifiedSelectionContext` from `@itwin/unified-selection-react` package instead. */ function useUnifiedSelectionContext() { return (0, react_1.useContext)(unifiedSelectionContext); } //# sourceMappingURL=UnifiedSelectionContext.js.map