@itwin/unified-selection 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/CHANGELOG.md +129 -0
  2. package/README.md +104 -6
  3. package/lib/cjs/unified-selection/CachingHiliteSetProvider.d.ts +33 -0
  4. package/lib/cjs/unified-selection/CachingHiliteSetProvider.d.ts.map +1 -0
  5. package/lib/cjs/unified-selection/CachingHiliteSetProvider.js +62 -0
  6. package/lib/cjs/unified-selection/CachingHiliteSetProvider.js.map +1 -0
  7. package/lib/cjs/unified-selection/EnableUnifiedSelectionSyncWithIModel.d.ts +90 -0
  8. package/lib/cjs/unified-selection/EnableUnifiedSelectionSyncWithIModel.d.ts.map +1 -0
  9. package/lib/cjs/unified-selection/EnableUnifiedSelectionSyncWithIModel.js +205 -0
  10. package/lib/cjs/unified-selection/EnableUnifiedSelectionSyncWithIModel.js.map +1 -0
  11. package/lib/cjs/unified-selection/HiliteSetProvider.d.ts +37 -0
  12. package/lib/cjs/unified-selection/HiliteSetProvider.d.ts.map +1 -0
  13. package/lib/cjs/unified-selection/HiliteSetProvider.js +344 -0
  14. package/lib/cjs/unified-selection/HiliteSetProvider.js.map +1 -0
  15. package/lib/cjs/unified-selection/Selectable.d.ts +0 -3
  16. package/lib/cjs/unified-selection/Selectable.d.ts.map +1 -1
  17. package/lib/cjs/unified-selection/Selectable.js +2 -2
  18. package/lib/cjs/unified-selection/Selectable.js.map +1 -1
  19. package/lib/cjs/unified-selection/SelectionChangeEvent.d.ts +11 -7
  20. package/lib/cjs/unified-selection/SelectionChangeEvent.d.ts.map +1 -1
  21. package/lib/cjs/unified-selection/SelectionChangeEvent.js +1 -1
  22. package/lib/cjs/unified-selection/SelectionChangeEvent.js.map +1 -1
  23. package/lib/cjs/unified-selection/SelectionScope.d.ts +44 -0
  24. package/lib/cjs/unified-selection/SelectionScope.d.ts.map +1 -0
  25. package/lib/cjs/unified-selection/SelectionScope.js +193 -0
  26. package/lib/cjs/unified-selection/SelectionScope.js.map +1 -0
  27. package/lib/cjs/unified-selection/SelectionStorage.d.ts +34 -29
  28. package/lib/cjs/unified-selection/SelectionStorage.d.ts.map +1 -1
  29. package/lib/cjs/unified-selection/SelectionStorage.js +32 -18
  30. package/lib/cjs/unified-selection/SelectionStorage.js.map +1 -1
  31. package/lib/cjs/unified-selection/Utils.d.ts +7 -0
  32. package/lib/cjs/unified-selection/Utils.d.ts.map +1 -0
  33. package/lib/cjs/unified-selection/Utils.js +24 -0
  34. package/lib/cjs/unified-selection/Utils.js.map +1 -0
  35. package/lib/cjs/unified-selection/types/IModel.d.ts +84 -0
  36. package/lib/cjs/unified-selection/types/IModel.d.ts.map +1 -0
  37. package/lib/cjs/unified-selection/types/IModel.js +24 -0
  38. package/lib/cjs/unified-selection/types/IModel.js.map +1 -0
  39. package/lib/cjs/unified-selection.d.ts +5 -7
  40. package/lib/cjs/unified-selection.d.ts.map +1 -1
  41. package/lib/cjs/unified-selection.js +11 -7
  42. package/lib/cjs/unified-selection.js.map +1 -1
  43. package/lib/esm/unified-selection/CachingHiliteSetProvider.d.ts +33 -0
  44. package/lib/esm/unified-selection/CachingHiliteSetProvider.d.ts.map +1 -0
  45. package/lib/esm/unified-selection/CachingHiliteSetProvider.js +58 -0
  46. package/lib/esm/unified-selection/CachingHiliteSetProvider.js.map +1 -0
  47. package/lib/esm/unified-selection/EnableUnifiedSelectionSyncWithIModel.d.ts +90 -0
  48. package/lib/esm/unified-selection/EnableUnifiedSelectionSyncWithIModel.d.ts.map +1 -0
  49. package/lib/esm/unified-selection/EnableUnifiedSelectionSyncWithIModel.js +200 -0
  50. package/lib/esm/unified-selection/EnableUnifiedSelectionSyncWithIModel.js.map +1 -0
  51. package/lib/esm/unified-selection/HiliteSetProvider.d.ts +37 -0
  52. package/lib/esm/unified-selection/HiliteSetProvider.d.ts.map +1 -0
  53. package/lib/esm/unified-selection/HiliteSetProvider.js +340 -0
  54. package/lib/esm/unified-selection/HiliteSetProvider.js.map +1 -0
  55. package/lib/esm/unified-selection/Selectable.d.ts +0 -3
  56. package/lib/esm/unified-selection/Selectable.d.ts.map +1 -1
  57. package/lib/esm/unified-selection/Selectable.js.map +1 -1
  58. package/lib/esm/unified-selection/SelectionChangeEvent.d.ts +11 -7
  59. package/lib/esm/unified-selection/SelectionChangeEvent.d.ts.map +1 -1
  60. package/lib/esm/unified-selection/SelectionChangeEvent.js +1 -1
  61. package/lib/esm/unified-selection/SelectionChangeEvent.js.map +1 -1
  62. package/lib/esm/unified-selection/SelectionScope.d.ts +44 -0
  63. package/lib/esm/unified-selection/SelectionScope.d.ts.map +1 -0
  64. package/lib/esm/unified-selection/SelectionScope.js +189 -0
  65. package/lib/esm/unified-selection/SelectionScope.js.map +1 -0
  66. package/lib/esm/unified-selection/SelectionStorage.d.ts +34 -29
  67. package/lib/esm/unified-selection/SelectionStorage.d.ts.map +1 -1
  68. package/lib/esm/unified-selection/SelectionStorage.js +31 -17
  69. package/lib/esm/unified-selection/SelectionStorage.js.map +1 -1
  70. package/lib/esm/unified-selection/Utils.d.ts +7 -0
  71. package/lib/esm/unified-selection/Utils.d.ts.map +1 -0
  72. package/lib/esm/unified-selection/Utils.js +20 -0
  73. package/lib/esm/unified-selection/Utils.js.map +1 -0
  74. package/lib/esm/unified-selection/types/IModel.d.ts +84 -0
  75. package/lib/esm/unified-selection/types/IModel.d.ts.map +1 -0
  76. package/lib/esm/unified-selection/types/IModel.js +21 -0
  77. package/lib/esm/unified-selection/types/IModel.js.map +1 -0
  78. package/lib/esm/unified-selection.d.ts +5 -7
  79. package/lib/esm/unified-selection.d.ts.map +1 -1
  80. package/lib/esm/unified-selection.js +5 -7
  81. package/lib/esm/unified-selection.js.map +1 -1
  82. package/package.json +28 -15
@@ -0,0 +1,90 @@
1
+ import { ECSchemaProvider, ECSqlQueryExecutor } from "@itwin/presentation-shared";
2
+ import { CachingHiliteSetProvider } from "./CachingHiliteSetProvider";
3
+ import { ComputeSelectionProps } from "./SelectionScope";
4
+ import { SelectionStorage } from "./SelectionStorage";
5
+ import { CoreIModelHiliteSet, CoreIModelSelectionSet } from "./types/IModel";
6
+ /**
7
+ * Props for `enableUnifiedSelectionSyncWithIModel`.
8
+ * @internal Not exported through barrel, but used in public API as an argument. May be supplemented with optional attributes any time.
9
+ */
10
+ export interface EnableUnifiedSelectionSyncWithIModelProps {
11
+ /**
12
+ * Provides access to different iModel's features: query executing, metadata access, selection and hilite sets.
13
+ * It's recommended to use `@itwin/presentation-core-interop` to create `ECSqlQueryExecutor` and `ECSchemaProvider` from
14
+ * [IModelConnection](https://www.itwinjs.org/reference/core-frontend/imodelconnection/imodelconnection/) and map its `key`,
15
+ * `hilited` and `selectionSet` attributes like this:
16
+ *
17
+ * ```ts
18
+ * import { createECSqlQueryExecutor, createECSchemaProvider } from "@itwin/presentation-core-interop";
19
+ * import { IModelConnection } from "@itwin/core-frontend";
20
+ *
21
+ * const imodel: IModelConnection = ...
22
+ * const imodelAccess = {
23
+ * ...createECSqlQueryExecutor(imodel),
24
+ * ...createECSchemaProvider(MyAppFrontend.getSchemaContext(imodel)),
25
+ * key: imodel.key,
26
+ * hiliteSet: imodel.hilited,
27
+ * selectionSet: imodel.selectionSet,
28
+ * };
29
+ * ```.
30
+ */
31
+ imodelAccess: ECSqlQueryExecutor & ECSchemaProvider & {
32
+ /** Key of the iModel. Generally taken from `IModelConnection.key`. */
33
+ readonly key: string;
34
+ /** The set of currently hilited elements taken from `IModelConnection.hilited`. */
35
+ readonly hiliteSet: CoreIModelHiliteSet;
36
+ /** The set of currently selected elements taken from `IModelConnection.selectionSet`. */
37
+ readonly selectionSet: CoreIModelSelectionSet;
38
+ };
39
+ /** Selection storage to synchronize IModel's tool selection with. */
40
+ selectionStorage: SelectionStorage;
41
+ /** Active scope provider. */
42
+ activeScopeProvider: () => ComputeSelectionProps["scope"];
43
+ /**
44
+ * A caching hilite set provider used to retrieve hilite sets for an iModel. If not provided, a new `CachingHiliteSetProvider`
45
+ * will be created for the given iModel using the provided `imodelAccess`.
46
+ * If the consuming application already has a `CachingHiliteSetProvider` defined, it should be provided instead
47
+ * to reuse the cache and avoid creating new providers for each iModel.
48
+ */
49
+ cachingHiliteSetProvider?: CachingHiliteSetProvider;
50
+ }
51
+ /**
52
+ * Enables synchronization between iModel selection and unified selection.
53
+ * @returns function for disposing the synchronization.
54
+ * @beta
55
+ */
56
+ export declare function enableUnifiedSelectionSyncWithIModel(props: EnableUnifiedSelectionSyncWithIModelProps): () => void;
57
+ /**
58
+ * A handler that syncs selection between unified selection storage
59
+ * (`SelectionStorage`) and an iModel (`iModel.selectionSet`, `iModel.hilited`).
60
+ * @internal
61
+ */
62
+ export declare class IModelSelectionHandler {
63
+ private _selectionSourceName;
64
+ private _imodelAccess;
65
+ private _selectionStorage;
66
+ private _hiliteSetProvider;
67
+ private _cachingHiliteSetProvider;
68
+ private _activeScopeProvider;
69
+ private _isSuspended;
70
+ private _cancelOngoingChanges;
71
+ private _unifiedSelectionListenerDisposeFunc;
72
+ private _imodelListenerDisposeFunc;
73
+ private _hasCustomCachingHiliteSetProvider;
74
+ constructor(props: EnableUnifiedSelectionSyncWithIModelProps);
75
+ dispose(): void;
76
+ /** Temporarily suspends tool selection synchronization until the returned `IDisposable` is disposed. */
77
+ suspendIModelToolSelectionSync(): {
78
+ dispose: () => boolean;
79
+ };
80
+ private handleUnifiedSelectionChange;
81
+ private onUnifiedSelectionChanged;
82
+ private applyCurrentHiliteSet;
83
+ private addHiliteSet;
84
+ private removeHiliteSet;
85
+ private onIModelSelectionChanged;
86
+ private handleIModelSelectionChange;
87
+ private getSelectionSetChangeIds;
88
+ private idArgToIds;
89
+ }
90
+ //# sourceMappingURL=EnableUnifiedSelectionSyncWithIModel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EnableUnifiedSelectionSyncWithIModel.d.ts","sourceRoot":"","sources":["../../../src/unified-selection/EnableUnifiedSelectionSyncWithIModel.ts"],"names":[],"mappings":"AAO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O,CAAC,0BAA0B,CAAa;IAC/C,OAAO,CAAC,kCAAkC,CAAU;gBAEjC,KAAK,EAAE,yCAAyC;IAuB5D,OAAO;IASd,wGAAwG;IACjG,8BAA8B;;;IAQrC,OAAO,CAAC,4BAA4B;IA6BpC,OAAO,CAAC,yBAAyB,CAU/B;IAEF,OAAO,CAAC,qBAAqB;IAmB7B,OAAO,CAAC,YAAY;IAepB,OAAO,CAAC,eAAe;IAevB,OAAO,CAAC,wBAAwB,CAa9B;YAEY,2BAA2B;IAiBzC,OAAO,CAAC,wBAAwB;IAYhC,OAAO,CAAC,UAAU;CASnB"}
@@ -0,0 +1,200 @@
1
+ /*---------------------------------------------------------------------------------------------
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
+ import { from, Subject, takeUntil } from "rxjs";
6
+ import { assert, using } from "@itwin/core-bentley";
7
+ import { createCachingHiliteSetProvider } from "./CachingHiliteSetProvider";
8
+ import { createHiliteSetProvider } from "./HiliteSetProvider";
9
+ import { computeSelection } from "./SelectionScope";
10
+ import { CoreSelectionSetEventType } from "./types/IModel";
11
+ /**
12
+ * Enables synchronization between iModel selection and unified selection.
13
+ * @returns function for disposing the synchronization.
14
+ * @beta
15
+ */
16
+ export function enableUnifiedSelectionSyncWithIModel(props) {
17
+ const selectionHandler = new IModelSelectionHandler(props);
18
+ return () => selectionHandler.dispose();
19
+ }
20
+ /**
21
+ * A handler that syncs selection between unified selection storage
22
+ * (`SelectionStorage`) and an iModel (`iModel.selectionSet`, `iModel.hilited`).
23
+ * @internal
24
+ */
25
+ export class IModelSelectionHandler {
26
+ _selectionSourceName = "Tool";
27
+ _imodelAccess;
28
+ _selectionStorage;
29
+ _hiliteSetProvider;
30
+ _cachingHiliteSetProvider;
31
+ _activeScopeProvider;
32
+ _isSuspended;
33
+ _cancelOngoingChanges = new Subject();
34
+ _unifiedSelectionListenerDisposeFunc;
35
+ _imodelListenerDisposeFunc;
36
+ _hasCustomCachingHiliteSetProvider;
37
+ constructor(props) {
38
+ this._imodelAccess = props.imodelAccess;
39
+ this._selectionStorage = props.selectionStorage;
40
+ this._activeScopeProvider = props.activeScopeProvider;
41
+ this._isSuspended = false;
42
+ this._hasCustomCachingHiliteSetProvider = !!props.cachingHiliteSetProvider;
43
+ this._cachingHiliteSetProvider =
44
+ props.cachingHiliteSetProvider ??
45
+ createCachingHiliteSetProvider({
46
+ selectionStorage: this._selectionStorage,
47
+ imodelProvider: () => this._imodelAccess,
48
+ });
49
+ this._hiliteSetProvider = createHiliteSetProvider({ imodelAccess: this._imodelAccess });
50
+ this._imodelListenerDisposeFunc = this._imodelAccess.selectionSet.onChanged.addListener(this.onIModelSelectionChanged);
51
+ this._unifiedSelectionListenerDisposeFunc = this._selectionStorage.selectionChangeEvent.addListener(this.onUnifiedSelectionChanged);
52
+ // stop imodel from syncing tool selection with hilited list - we want to override that behavior
53
+ this._imodelAccess.hiliteSet.wantSyncWithSelectionSet = false;
54
+ this.applyCurrentHiliteSet({ activeSelectionAction: "clearAll" });
55
+ }
56
+ dispose() {
57
+ this._cancelOngoingChanges.next();
58
+ this._imodelListenerDisposeFunc();
59
+ this._unifiedSelectionListenerDisposeFunc();
60
+ if (!this._hasCustomCachingHiliteSetProvider) {
61
+ this._cachingHiliteSetProvider.dispose();
62
+ }
63
+ }
64
+ /** Temporarily suspends tool selection synchronization until the returned `IDisposable` is disposed. */
65
+ suspendIModelToolSelectionSync() {
66
+ const wasSuspended = this._isSuspended;
67
+ this._isSuspended = true;
68
+ return {
69
+ dispose: () => (this._isSuspended = wasSuspended),
70
+ };
71
+ }
72
+ handleUnifiedSelectionChange(changeType, selectables, source) {
73
+ if (changeType === "clear" || changeType === "replace") {
74
+ this.applyCurrentHiliteSet({ activeSelectionAction: changeType === "replace" && source === "Tool" ? "clearHilited" : "clearAll" });
75
+ return;
76
+ }
77
+ if (changeType === "add") {
78
+ from(this._hiliteSetProvider.getHiliteSet({ selectables }))
79
+ .pipe(takeUntil(this._cancelOngoingChanges))
80
+ .subscribe({
81
+ next: (set) => {
82
+ this.addHiliteSet(set);
83
+ },
84
+ });
85
+ return;
86
+ }
87
+ from(this._hiliteSetProvider.getHiliteSet({ selectables }))
88
+ .pipe(takeUntil(this._cancelOngoingChanges))
89
+ .subscribe({
90
+ next: (set) => {
91
+ this.removeHiliteSet(set);
92
+ },
93
+ complete: () => {
94
+ this.applyCurrentHiliteSet({ activeSelectionAction: "keep" });
95
+ },
96
+ });
97
+ }
98
+ onUnifiedSelectionChanged = (args) => {
99
+ // iModels are only interested in top-level selection changes
100
+ if (args.imodelKey !== this._imodelAccess.key || args.level !== 0) {
101
+ return;
102
+ }
103
+ if (args.changeType === "replace" || args.changeType === "clear") {
104
+ this._cancelOngoingChanges.next();
105
+ }
106
+ this.handleUnifiedSelectionChange(args.changeType, args.selectables, args.source);
107
+ };
108
+ applyCurrentHiliteSet({ activeSelectionAction }) {
109
+ if (activeSelectionAction !== "keep") {
110
+ using(this.suspendIModelToolSelectionSync(), (_) => {
111
+ this._imodelAccess.hiliteSet.clear();
112
+ if (activeSelectionAction === "clearAll") {
113
+ this._imodelAccess.selectionSet.emptyAll();
114
+ }
115
+ });
116
+ }
117
+ from(this._cachingHiliteSetProvider.getHiliteSet({ imodelKey: this._imodelAccess.key }))
118
+ .pipe(takeUntil(this._cancelOngoingChanges))
119
+ .subscribe({
120
+ next: (ids) => {
121
+ this.addHiliteSet(ids);
122
+ },
123
+ });
124
+ }
125
+ addHiliteSet(set) {
126
+ using(this.suspendIModelToolSelectionSync(), (_) => {
127
+ if (set.models && set.models.length) {
128
+ this._imodelAccess.hiliteSet.models.addIds(set.models);
129
+ }
130
+ if (set.subCategories && set.subCategories.length) {
131
+ this._imodelAccess.hiliteSet.subcategories.addIds(set.subCategories);
132
+ }
133
+ if (set.elements && set.elements.length) {
134
+ this._imodelAccess.hiliteSet.elements.addIds(set.elements);
135
+ this._imodelAccess.selectionSet.add(set.elements);
136
+ }
137
+ });
138
+ }
139
+ removeHiliteSet(set) {
140
+ using(this.suspendIModelToolSelectionSync(), (_) => {
141
+ if (set.models.length) {
142
+ this._imodelAccess.hiliteSet.models.deleteIds(set.models);
143
+ }
144
+ if (set.subCategories.length) {
145
+ this._imodelAccess.hiliteSet.subcategories.deleteIds(set.subCategories);
146
+ }
147
+ if (set.elements.length) {
148
+ this._imodelAccess.hiliteSet.elements.deleteIds(set.elements);
149
+ this._imodelAccess.selectionSet.remove(set.elements);
150
+ }
151
+ });
152
+ }
153
+ onIModelSelectionChanged = async (event) => {
154
+ if (this._isSuspended) {
155
+ return;
156
+ }
157
+ if (CoreSelectionSetEventType.Clear === event.type) {
158
+ this._selectionStorage.clearSelection({ imodelKey: this._imodelAccess.key, source: this._selectionSourceName });
159
+ return;
160
+ }
161
+ const elementIds = this.getSelectionSetChangeIds(event);
162
+ const scopedSelection = computeSelection({ queryExecutor: this._imodelAccess, elementIds, scope: this._activeScopeProvider() });
163
+ await this.handleIModelSelectionChange(event.type, scopedSelection);
164
+ };
165
+ async handleIModelSelectionChange(type, iterator) {
166
+ if (type === CoreSelectionSetEventType.Remove) {
167
+ for await (const selectable of iterator) {
168
+ this._selectionStorage.removeFromSelection({ imodelKey: this._imodelAccess.key, source: this._selectionSourceName, selectables: [selectable] });
169
+ }
170
+ return;
171
+ }
172
+ if (type === CoreSelectionSetEventType.Replace) {
173
+ this._selectionStorage.clearSelection({ imodelKey: this._imodelAccess.key, source: this._selectionSourceName });
174
+ }
175
+ for await (const selectable of iterator) {
176
+ this._selectionStorage.addToSelection({ imodelKey: this._imodelAccess.key, source: this._selectionSourceName, selectables: [selectable] });
177
+ }
178
+ }
179
+ getSelectionSetChangeIds(event) {
180
+ switch (event.type) {
181
+ case CoreSelectionSetEventType.Add:
182
+ case CoreSelectionSetEventType.Replace:
183
+ assert(!!event.added);
184
+ return this.idArgToIds(event.added);
185
+ default:
186
+ assert(!!event.removed);
187
+ return this.idArgToIds(event.removed);
188
+ }
189
+ }
190
+ idArgToIds(ids) {
191
+ if (typeof ids === "string") {
192
+ return [ids];
193
+ }
194
+ if (ids instanceof Set) {
195
+ return [...ids];
196
+ }
197
+ return ids;
198
+ }
199
+ }
200
+ //# sourceMappingURL=EnableUnifiedSelectionSyncWithIModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EnableUnifiedSelectionSyncWithIModel.js","sourceRoot":"","sources":["../../../src/unified-selection/EnableUnifiedSelectionSyncWithIModel.ts"],"names":[],"mappings":"AAAA;;;gGAGgG;AAEhG,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAC;AAChD,OAAO,EAAE,MAAM,EAAW,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAE7D,OAAO,EAA4B,8BAA8B,EAAE,MAAM,4BAA4B,CAAC;AACtG,OAAO,EAAE,uBAAuB,EAAgC,MAAM,qBAAqB,CAAC;AAG5F,OAAO,EAAE,gBAAgB,EAAyB,MAAM,kBAAkB,CAAC;AAE3E,OAAO,EAA+C,yBAAyB,EAA+B,MAAM,gBAAgB,CAAC;AAoDrI;;;;GAIG;AACH,MAAM,UAAU,oCAAoC,CAAC,KAAgD;IACnG,MAAM,gBAAgB,GAAG,IAAI,sBAAsB,CAAC,KAAK,CAAC,CAAC;IAC3D,OAAO,GAAG,EAAE,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC;AAC1C,CAAC;AAED;;;;GAIG;AACH,MAAM,OAAO,sBAAsB;IACzB,oBAAoB,GAAG,MAAM,CAAC;IAE9B,aAAa,CAA4D;IACzE,iBAAiB,CAAmB;IACpC,kBAAkB,CAAoB;IACtC,yBAAyB,CAA2B;IACpD,oBAAoB,CAAuC;IAE3D,YAAY,CAAU;IACtB,qBAAqB,GAAG,IAAI,OAAO,EAAQ,CAAC;IAC5C,oCAAoC,CAAa;IACjD,0BAA0B,CAAa;IACvC,kCAAkC,CAAU;IAEpD,YAAmB,KAAgD;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,YAAY,GAAG,EAAE,CAAC;YACvB,OAAO,CAAC,GAAG,GAAG,CAAC,CAAC;QAClB,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\n * Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n * See LICENSE.md in the project root for license terms and full copyright notice.\n *--------------------------------------------------------------------------------------------*/\n\nimport { from, Subject, takeUntil } from \"rxjs\";\nimport { assert, Id64Arg, using } from \"@itwin/core-bentley\";\nimport { ECSchemaProvider, ECSqlQueryExecutor } from \"@itwin/presentation-shared\";\nimport { CachingHiliteSetProvider, createCachingHiliteSetProvider } from \"./CachingHiliteSetProvider\";\nimport { createHiliteSetProvider, HiliteSet, HiliteSetProvider } from \"./HiliteSetProvider\";\nimport { SelectableInstanceKey, Selectables } from \"./Selectable\";\nimport { StorageSelectionChangeEventArgs, StorageSelectionChangeType } from \"./SelectionChangeEvent\";\nimport { computeSelection, ComputeSelectionProps } from \"./SelectionScope\";\nimport { SelectionStorage } from \"./SelectionStorage\";\nimport { CoreIModelHiliteSet, CoreIModelSelectionSet, CoreSelectionSetEventType, CoreSelectionSetEventUnsafe } from \"./types/IModel\";\n\n/**\n * Props for `enableUnifiedSelectionSyncWithIModel`.\n * @internal Not exported through barrel, but used in public API as an argument. May be supplemented with optional attributes any time.\n */\nexport interface EnableUnifiedSelectionSyncWithIModelProps {\n /**\n * Provides access to different iModel's features: query executing, metadata access, selection and hilite sets.\n * It's recommended to use `@itwin/presentation-core-interop` to create `ECSqlQueryExecutor` and `ECSchemaProvider` from\n * [IModelConnection](https://www.itwinjs.org/reference/core-frontend/imodelconnection/imodelconnection/) and map its `key`,\n * `hilited` and `selectionSet` attributes like this:\n *\n * ```ts\n * import { createECSqlQueryExecutor, createECSchemaProvider } from \"@itwin/presentation-core-interop\";\n * import { IModelConnection } from \"@itwin/core-frontend\";\n *\n * const imodel: IModelConnection = ...\n * const imodelAccess = {\n * ...createECSqlQueryExecutor(imodel),\n * ...createECSchemaProvider(MyAppFrontend.getSchemaContext(imodel)),\n * key: imodel.key,\n * hiliteSet: imodel.hilited,\n * selectionSet: imodel.selectionSet,\n * };\n * ```.\n */\n imodelAccess: ECSqlQueryExecutor &\n ECSchemaProvider & {\n /** Key of the iModel. Generally taken from `IModelConnection.key`. */\n readonly key: string;\n /** The set of currently hilited elements taken from `IModelConnection.hilited`. */\n readonly hiliteSet: CoreIModelHiliteSet;\n /** The set of currently selected elements taken from `IModelConnection.selectionSet`. */\n readonly selectionSet: CoreIModelSelectionSet;\n };\n\n /** Selection storage to synchronize IModel's tool selection with. */\n selectionStorage: SelectionStorage;\n\n /** Active scope provider. */\n activeScopeProvider: () => ComputeSelectionProps[\"scope\"];\n\n /**\n * A caching hilite set provider used to retrieve hilite sets for an iModel. If not provided, a new `CachingHiliteSetProvider`\n * will be created for the given iModel using the provided `imodelAccess`.\n * If the consuming application already has a `CachingHiliteSetProvider` defined, it should be provided instead\n * to reuse the cache and avoid creating new providers for each iModel.\n */\n cachingHiliteSetProvider?: CachingHiliteSetProvider;\n}\n\n/**\n * Enables synchronization between iModel selection and unified selection.\n * @returns function for disposing the synchronization.\n * @beta\n */\nexport function enableUnifiedSelectionSyncWithIModel(props: EnableUnifiedSelectionSyncWithIModelProps): () => void {\n const selectionHandler = new IModelSelectionHandler(props);\n return () => selectionHandler.dispose();\n}\n\n/**\n * A handler that syncs selection between unified selection storage\n * (`SelectionStorage`) and an iModel (`iModel.selectionSet`, `iModel.hilited`).\n * @internal\n */\nexport class IModelSelectionHandler {\n private _selectionSourceName = \"Tool\";\n\n private _imodelAccess: EnableUnifiedSelectionSyncWithIModelProps[\"imodelAccess\"];\n private _selectionStorage: SelectionStorage;\n private _hiliteSetProvider: HiliteSetProvider;\n private _cachingHiliteSetProvider: CachingHiliteSetProvider;\n private _activeScopeProvider: () => ComputeSelectionProps[\"scope\"];\n\n private _isSuspended: boolean;\n private _cancelOngoingChanges = new Subject<void>();\n private _unifiedSelectionListenerDisposeFunc: () => void;\n private _imodelListenerDisposeFunc: () => void;\n private _hasCustomCachingHiliteSetProvider: boolean;\n\n public constructor(props: EnableUnifiedSelectionSyncWithIModelProps) {\n this._imodelAccess = props.imodelAccess;\n this._selectionStorage = props.selectionStorage;\n this._activeScopeProvider = props.activeScopeProvider;\n this._isSuspended = false;\n this._hasCustomCachingHiliteSetProvider = !!props.cachingHiliteSetProvider;\n\n this._cachingHiliteSetProvider =\n props.cachingHiliteSetProvider ??\n createCachingHiliteSetProvider({\n selectionStorage: this._selectionStorage,\n imodelProvider: () => this._imodelAccess,\n });\n\n this._hiliteSetProvider = createHiliteSetProvider({ imodelAccess: this._imodelAccess });\n this._imodelListenerDisposeFunc = this._imodelAccess.selectionSet.onChanged.addListener(this.onIModelSelectionChanged);\n this._unifiedSelectionListenerDisposeFunc = this._selectionStorage.selectionChangeEvent.addListener(this.onUnifiedSelectionChanged);\n\n // stop imodel from syncing tool selection with hilited list - we want to override that behavior\n this._imodelAccess.hiliteSet.wantSyncWithSelectionSet = false;\n this.applyCurrentHiliteSet({ activeSelectionAction: \"clearAll\" });\n }\n\n public dispose() {\n this._cancelOngoingChanges.next();\n this._imodelListenerDisposeFunc();\n this._unifiedSelectionListenerDisposeFunc();\n if (!this._hasCustomCachingHiliteSetProvider) {\n this._cachingHiliteSetProvider.dispose();\n }\n }\n\n /** Temporarily suspends tool selection synchronization until the returned `IDisposable` is disposed. */\n public suspendIModelToolSelectionSync() {\n const wasSuspended = this._isSuspended;\n this._isSuspended = true;\n return {\n dispose: () => (this._isSuspended = wasSuspended),\n };\n }\n\n private handleUnifiedSelectionChange(changeType: StorageSelectionChangeType, selectables: Selectables, source: string) {\n if (changeType === \"clear\" || changeType === \"replace\") {\n this.applyCurrentHiliteSet({ activeSelectionAction: changeType === \"replace\" && source === \"Tool\" ? \"clearHilited\" : \"clearAll\" });\n return;\n }\n\n if (changeType === \"add\") {\n from(this._hiliteSetProvider.getHiliteSet({ selectables }))\n .pipe(takeUntil(this._cancelOngoingChanges))\n .subscribe({\n next: (set) => {\n this.addHiliteSet(set);\n },\n });\n return;\n }\n\n from(this._hiliteSetProvider.getHiliteSet({ selectables }))\n .pipe(takeUntil(this._cancelOngoingChanges))\n .subscribe({\n next: (set) => {\n this.removeHiliteSet(set);\n },\n complete: () => {\n this.applyCurrentHiliteSet({ activeSelectionAction: \"keep\" });\n },\n });\n }\n\n private onUnifiedSelectionChanged = (args: StorageSelectionChangeEventArgs) => {\n // iModels are only interested in top-level selection changes\n if (args.imodelKey !== this._imodelAccess.key || args.level !== 0) {\n return;\n }\n\n if (args.changeType === \"replace\" || args.changeType === \"clear\") {\n this._cancelOngoingChanges.next();\n }\n this.handleUnifiedSelectionChange(args.changeType, args.selectables, args.source);\n };\n\n private applyCurrentHiliteSet({ activeSelectionAction }: { activeSelectionAction: \"clearAll\" | \"clearHilited\" | \"keep\" }) {\n if (activeSelectionAction !== \"keep\") {\n using(this.suspendIModelToolSelectionSync(), (_) => {\n this._imodelAccess.hiliteSet.clear();\n if (activeSelectionAction === \"clearAll\") {\n this._imodelAccess.selectionSet.emptyAll();\n }\n });\n }\n\n from(this._cachingHiliteSetProvider.getHiliteSet({ imodelKey: this._imodelAccess.key }))\n .pipe(takeUntil(this._cancelOngoingChanges))\n .subscribe({\n next: (ids) => {\n this.addHiliteSet(ids);\n },\n });\n }\n\n private addHiliteSet(set: HiliteSet) {\n using(this.suspendIModelToolSelectionSync(), (_) => {\n if (set.models && set.models.length) {\n this._imodelAccess.hiliteSet.models.addIds(set.models);\n }\n if (set.subCategories && set.subCategories.length) {\n this._imodelAccess.hiliteSet.subcategories.addIds(set.subCategories);\n }\n if (set.elements && set.elements.length) {\n this._imodelAccess.hiliteSet.elements.addIds(set.elements);\n this._imodelAccess.selectionSet.add(set.elements);\n }\n });\n }\n\n private removeHiliteSet(set: HiliteSet) {\n using(this.suspendIModelToolSelectionSync(), (_) => {\n if (set.models.length) {\n this._imodelAccess.hiliteSet.models.deleteIds(set.models);\n }\n if (set.subCategories.length) {\n this._imodelAccess.hiliteSet.subcategories.deleteIds(set.subCategories);\n }\n if (set.elements.length) {\n this._imodelAccess.hiliteSet.elements.deleteIds(set.elements);\n this._imodelAccess.selectionSet.remove(set.elements);\n }\n });\n }\n\n private onIModelSelectionChanged = async (event?: CoreSelectionSetEventUnsafe): Promise<void> => {\n if (this._isSuspended) {\n return;\n }\n\n if (CoreSelectionSetEventType.Clear === event!.type) {\n this._selectionStorage.clearSelection({ imodelKey: this._imodelAccess.key, source: this._selectionSourceName });\n return;\n }\n\n const elementIds = this.getSelectionSetChangeIds(event!);\n const scopedSelection = computeSelection({ queryExecutor: this._imodelAccess, elementIds, scope: this._activeScopeProvider() });\n await this.handleIModelSelectionChange(event!.type, scopedSelection);\n };\n\n private async handleIModelSelectionChange(type: CoreSelectionSetEventType, iterator: AsyncIterableIterator<SelectableInstanceKey>) {\n if (type === CoreSelectionSetEventType.Remove) {\n for await (const selectable of iterator) {\n this._selectionStorage.removeFromSelection({ imodelKey: this._imodelAccess.key, source: this._selectionSourceName, selectables: [selectable] });\n }\n return;\n }\n\n if (type === CoreSelectionSetEventType.Replace) {\n this._selectionStorage.clearSelection({ imodelKey: this._imodelAccess.key, source: this._selectionSourceName });\n }\n\n for await (const selectable of iterator) {\n this._selectionStorage.addToSelection({ imodelKey: this._imodelAccess.key, source: this._selectionSourceName, selectables: [selectable] });\n }\n }\n\n private getSelectionSetChangeIds(event: CoreSelectionSetEventUnsafe): string[] {\n switch (event.type) {\n case CoreSelectionSetEventType.Add:\n case CoreSelectionSetEventType.Replace:\n assert(!!event.added);\n return this.idArgToIds(event.added);\n default:\n assert(!!event.removed);\n return this.idArgToIds(event.removed);\n }\n }\n\n private idArgToIds(ids: Id64Arg): string[] {\n if (typeof ids === \"string\") {\n return [ids];\n }\n if (ids instanceof Set) {\n return [...ids];\n }\n return ids;\n }\n}\n"]}
@@ -0,0 +1,37 @@
1
+ import { ECSchemaProvider, ECSqlQueryExecutor } from "@itwin/presentation-shared";
2
+ import { Selectables } from "./Selectable";
3
+ /**
4
+ * A set of model, subcategory and element ids that can be used for specifying hilite.
5
+ * @see https://www.itwinjs.org/reference/core-frontend/selectionset/hiliteset/
6
+ * @beta
7
+ */
8
+ export interface HiliteSet {
9
+ models: string[];
10
+ subCategories: string[];
11
+ elements: string[];
12
+ }
13
+ /**
14
+ * Props for creating a `HiliteSetProvider` instance.
15
+ * @internal Not exported through barrel, but used in public API as an argument. May be supplemented with optional attributes any time.
16
+ */
17
+ export interface HiliteSetProviderProps {
18
+ imodelAccess: ECSchemaProvider & ECSqlQueryExecutor;
19
+ }
20
+ /**
21
+ * Defines return value of `createHiliteSetProvider`.
22
+ *
23
+ * @beta Used in public API as a return value. Not expected to be created / extended by package
24
+ * consumers, may be supplemented with required attributes any time.
25
+ */
26
+ export interface HiliteSetProvider {
27
+ /** Get the current hilite set iterator for the specified imodel */
28
+ getHiliteSet(props: {
29
+ selectables: Selectables;
30
+ }): AsyncIterableIterator<HiliteSet>;
31
+ }
32
+ /**
33
+ * Creates a hilite set provider that returns a `HiliteSet` for given selectables.
34
+ * @beta
35
+ */
36
+ export declare function createHiliteSetProvider(props: HiliteSetProviderProps): HiliteSetProvider;
37
+ //# sourceMappingURL=HiliteSetProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"HiliteSetProvider.d.ts","sourceRoot":"","sources":["../../../src/unified-selection/HiliteSetProvider.ts"],"names":[],"mappings":"AAOA,OAAO,EAAM,gBAAgB,EAAgB,kBAAkB,EAAsB,MAAM,4BAA4B,CAAC;AACxH,OAAO,EAAyB,WAAW,EAAE,MAAM,cAAc,CAAC;AAKlE;;;;GAIG;AACH,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAED;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACrC,YAAY,EAAE,gBAAgB,GAAG,kBAAkB,CAAC;CACrD;AAED;;;;;GAKG;AACH,MAAM,WAAW,iBAAiB;IAChC,mEAAmE;IACnE,YAAY,CAAC,KAAK,EAAE;QAAE,WAAW,EAAE,WAAW,CAAA;KAAE,GAAG,qBAAqB,CAAC,SAAS,CAAC,CAAC;CACrF;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,sBAAsB,GAAG,iBAAiB,CAExF"}