@itwin/presentation-components
Version:
React components based on iTwin.js Presentation library
68 lines • 3.58 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.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable @typescript-eslint/no-deprecated */
/**
* @packageDocumentation
* @module UnifiedSelection
*/
import { createContext, useContext, useEffect, useRef, useState } from "react";
import { assert } from "@itwin/core-bentley";
import { Presentation } from "@itwin/presentation-frontend";
import { memoize } from "../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.
*/
export function UnifiedSelectionContextProvider(props) {
const selectionLevel = props.selectionLevel ?? 0;
const contextRef = useRef();
if (contextRef.current?.imodel !== props.imodel || contextRef.current.selectionLevel !== selectionLevel) {
contextRef.current = createSelectionContext(props.imodel, selectionLevel);
}
const [_, setState] = useState({});
useEffect(() => {
const currentContext = contextRef.current;
assert(currentContext !== undefined);
return Presentation.selection.selectionChange.addListener((args) => {
if (args.level > currentContext.selectionLevel) {
return;
}
contextRef.current = {
...currentContext,
getSelection: createGetSelection(props.imodel, selectionLevel),
};
setState({});
});
}, [props.imodel, selectionLevel]);
return _jsx(unifiedSelectionContext.Provider, { value: contextRef.current, children: props.children });
}
function createSelectionContext(imodel, selectionLevel) {
return {
imodel,
selectionLevel,
getSelection: createGetSelection(imodel, selectionLevel),
replaceSelection: (keys, level) => Presentation.selection.replaceSelection("UnifiedSelectionContext", imodel, keys, level ?? selectionLevel),
addToSelection: (keys, level) => Presentation.selection.addToSelection("UnifiedSelectionContext", imodel, keys, level ?? selectionLevel),
clearSelection: (level) => Presentation.selection.clearSelection("UnifiedSelectionContext", imodel, level ?? selectionLevel),
removeFromSelection: (keys, level) => Presentation.selection.removeFromSelection("UnifiedSelectionContext", imodel, keys, level ?? selectionLevel),
};
}
function createGetSelection(imodel, selectionLevel) {
return memoize((level) => new Proxy(Presentation.selection.getSelection(imodel, level ?? selectionLevel), {}), { maxSize: Number.MAX_SAFE_INTEGER });
}
const unifiedSelectionContext = createContext(undefined);
/**
* Returns Unified Selection context provided by [[UnifiedSelectionContextProvider]].
* @public
* @deprecated in 5.9.0. Use `useUnifiedSelectionContext` from `@itwin/unified-selection-react` package instead.
*/
export function useUnifiedSelectionContext() {
return useContext(unifiedSelectionContext);
}
//# sourceMappingURL=UnifiedSelectionContext.js.map