@asyra/preset 0.5.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.
- package/LICENSE +21 -0
- package/README.md +14 -0
- package/dist/catalog.d.ts +3 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +61 -0
- package/dist/catalog.js.map +1 -0
- package/dist/cleanup-reporter.d.ts +6 -0
- package/dist/cleanup-reporter.d.ts.map +1 -0
- package/dist/cleanup-reporter.js +14 -0
- package/dist/cleanup-reporter.js.map +1 -0
- package/dist/components/fills.d.ts +28 -0
- package/dist/components/fills.d.ts.map +1 -0
- package/dist/components/fills.js +180 -0
- package/dist/components/fills.js.map +1 -0
- package/dist/components/frame.d.ts +4 -0
- package/dist/components/frame.d.ts.map +1 -0
- package/dist/components/frame.js +44 -0
- package/dist/components/frame.js.map +1 -0
- package/dist/components/group.d.ts +72 -0
- package/dist/components/group.d.ts.map +1 -0
- package/dist/components/group.js +491 -0
- package/dist/components/group.js.map +1 -0
- package/dist/components/index.d.ts +24 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +65 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/oval.d.ts +4 -0
- package/dist/components/oval.d.ts.map +1 -0
- package/dist/components/oval.js +56 -0
- package/dist/components/oval.js.map +1 -0
- package/dist/components/rectangle.d.ts +4 -0
- package/dist/components/rectangle.d.ts.map +1 -0
- package/dist/components/rectangle.js +54 -0
- package/dist/components/rectangle.js.map +1 -0
- package/dist/components/shape-hit-area.d.ts +8 -0
- package/dist/components/shape-hit-area.d.ts.map +1 -0
- package/dist/components/shape-hit-area.js +30 -0
- package/dist/components/shape-hit-area.js.map +1 -0
- package/dist/components/stroke-defaults.d.ts +5 -0
- package/dist/components/stroke-defaults.d.ts.map +1 -0
- package/dist/components/stroke-defaults.js +4 -0
- package/dist/components/stroke-defaults.js.map +1 -0
- package/dist/components/vector.d.ts +7 -0
- package/dist/components/vector.d.ts.map +1 -0
- package/dist/components/vector.js +961 -0
- package/dist/components/vector.js.map +1 -0
- package/dist/composition/error.d.ts +16 -0
- package/dist/composition/error.d.ts.map +1 -0
- package/dist/composition/error.js +12 -0
- package/dist/composition/error.js.map +1 -0
- package/dist/composition/profile-provider.d.ts +7 -0
- package/dist/composition/profile-provider.d.ts.map +1 -0
- package/dist/composition/profile-provider.js +24 -0
- package/dist/composition/profile-provider.js.map +1 -0
- package/dist/composition/resolve.d.ts +11 -0
- package/dist/composition/resolve.d.ts.map +1 -0
- package/dist/composition/resolve.js +96 -0
- package/dist/composition/resolve.js.map +1 -0
- package/dist/composition/result.d.ts +3 -0
- package/dist/composition/result.d.ts.map +1 -0
- package/dist/composition/result.js +7 -0
- package/dist/composition/result.js.map +1 -0
- package/dist/constants.d.ts +30 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +30 -0
- package/dist/constants.js.map +1 -0
- package/dist/defaults/install.d.ts +16 -0
- package/dist/defaults/install.d.ts.map +1 -0
- package/dist/defaults/install.js +51 -0
- package/dist/defaults/install.js.map +1 -0
- package/dist/defaults/installation.d.ts +14 -0
- package/dist/defaults/installation.d.ts.map +1 -0
- package/dist/defaults/installation.js +85 -0
- package/dist/defaults/installation.js.map +1 -0
- package/dist/defaults/modules/basic-shapes.d.ts +3 -0
- package/dist/defaults/modules/basic-shapes.d.ts.map +1 -0
- package/dist/defaults/modules/basic-shapes.js +9 -0
- package/dist/defaults/modules/basic-shapes.js.map +1 -0
- package/dist/defaults/modules/containers.d.ts +3 -0
- package/dist/defaults/modules/containers.d.ts.map +1 -0
- package/dist/defaults/modules/containers.js +9 -0
- package/dist/defaults/modules/containers.js.map +1 -0
- package/dist/defaults/modules/input.d.ts +3 -0
- package/dist/defaults/modules/input.d.ts.map +1 -0
- package/dist/defaults/modules/input.js +7 -0
- package/dist/defaults/modules/input.js.map +1 -0
- package/dist/defaults/modules/selection.d.ts +3 -0
- package/dist/defaults/modules/selection.d.ts.map +1 -0
- package/dist/defaults/modules/selection.js +20 -0
- package/dist/defaults/modules/selection.js.map +1 -0
- package/dist/defaults/modules/ui-context.d.ts +3 -0
- package/dist/defaults/modules/ui-context.d.ts.map +1 -0
- package/dist/defaults/modules/ui-context.js +9 -0
- package/dist/defaults/modules/ui-context.js.map +1 -0
- package/dist/defaults/modules/vector-editing.d.ts +3 -0
- package/dist/defaults/modules/vector-editing.d.ts.map +1 -0
- package/dist/defaults/modules/vector-editing.js +22 -0
- package/dist/defaults/modules/vector-editing.js.map +1 -0
- package/dist/defaults/modules/vector.d.ts +3 -0
- package/dist/defaults/modules/vector.d.ts.map +1 -0
- package/dist/defaults/modules/vector.js +9 -0
- package/dist/defaults/modules/vector.js.map +1 -0
- package/dist/defaults/modules/viewport.d.ts +3 -0
- package/dist/defaults/modules/viewport.d.ts.map +1 -0
- package/dist/defaults/modules/viewport.js +7 -0
- package/dist/defaults/modules/viewport.js.map +1 -0
- package/dist/defaults/owned-state.d.ts +3 -0
- package/dist/defaults/owned-state.d.ts.map +1 -0
- package/dist/defaults/owned-state.js +72 -0
- package/dist/defaults/owned-state.js.map +1 -0
- package/dist/defaults/private-manager.d.ts +3 -0
- package/dist/defaults/private-manager.d.ts.map +1 -0
- package/dist/defaults/private-manager.js +15 -0
- package/dist/defaults/private-manager.js.map +1 -0
- package/dist/defaults/types.d.ts +12 -0
- package/dist/defaults/types.d.ts.map +1 -0
- package/dist/defaults/types.js +2 -0
- package/dist/defaults/types.js.map +1 -0
- package/dist/events/index.d.ts +3 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +3 -0
- package/dist/events/index.js.map +1 -0
- package/dist/events/input-events.d.ts +19 -0
- package/dist/events/input-events.d.ts.map +1 -0
- package/dist/events/input-events.js +18 -0
- package/dist/events/input-events.js.map +1 -0
- package/dist/events/preset-event-names.d.ts +136 -0
- package/dist/events/preset-event-names.d.ts.map +1 -0
- package/dist/events/preset-event-names.js +18 -0
- package/dist/events/preset-event-names.js.map +1 -0
- package/dist/events/register-events.d.ts +4 -0
- package/dist/events/register-events.d.ts.map +1 -0
- package/dist/events/register-events.js +30 -0
- package/dist/events/register-events.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/preset.d.ts +3 -0
- package/dist/preset.d.ts.map +1 -0
- package/dist/preset.js +86 -0
- package/dist/preset.js.map +1 -0
- package/dist/props/components/anchor-point-component.d.ts +3 -0
- package/dist/props/components/anchor-point-component.d.ts.map +1 -0
- package/dist/props/components/anchor-point-component.js +6 -0
- package/dist/props/components/anchor-point-component.js.map +1 -0
- package/dist/props/components/anchor-points-component.d.ts +3 -0
- package/dist/props/components/anchor-points-component.d.ts.map +1 -0
- package/dist/props/components/anchor-points-component.js +43 -0
- package/dist/props/components/anchor-points-component.js.map +1 -0
- package/dist/props/components/children-map-property-component.d.ts +15 -0
- package/dist/props/components/children-map-property-component.d.ts.map +1 -0
- package/dist/props/components/children-map-property-component.js +205 -0
- package/dist/props/components/children-map-property-component.js.map +1 -0
- package/dist/props/components/custom-component.d.ts +3 -0
- package/dist/props/components/custom-component.d.ts.map +1 -0
- package/dist/props/components/custom-component.js +6 -0
- package/dist/props/components/custom-component.js.map +1 -0
- package/dist/props/components/dimension-component.d.ts +3 -0
- package/dist/props/components/dimension-component.d.ts.map +1 -0
- package/dist/props/components/dimension-component.js +6 -0
- package/dist/props/components/dimension-component.js.map +1 -0
- package/dist/props/components/fill-component.d.ts +3 -0
- package/dist/props/components/fill-component.d.ts.map +1 -0
- package/dist/props/components/fill-component.js +25 -0
- package/dist/props/components/fill-component.js.map +1 -0
- package/dist/props/components/fills-component.d.ts +3 -0
- package/dist/props/components/fills-component.d.ts.map +1 -0
- package/dist/props/components/fills-component.js +33 -0
- package/dist/props/components/fills-component.js.map +1 -0
- package/dist/props/components/index.d.ts +7 -0
- package/dist/props/components/index.d.ts.map +1 -0
- package/dist/props/components/index.js +51 -0
- package/dist/props/components/index.js.map +1 -0
- package/dist/props/components/number-value.d.ts +2 -0
- package/dist/props/components/number-value.d.ts.map +1 -0
- package/dist/props/components/number-value.js +2 -0
- package/dist/props/components/number-value.js.map +1 -0
- package/dist/props/components/position-component.d.ts +3 -0
- package/dist/props/components/position-component.d.ts.map +1 -0
- package/dist/props/components/position-component.js +6 -0
- package/dist/props/components/position-component.js.map +1 -0
- package/dist/props/components/stroke-component.d.ts +3 -0
- package/dist/props/components/stroke-component.d.ts.map +1 -0
- package/dist/props/components/stroke-component.js +29 -0
- package/dist/props/components/stroke-component.js.map +1 -0
- package/dist/props/components/strokes-component.d.ts +3 -0
- package/dist/props/components/strokes-component.d.ts.map +1 -0
- package/dist/props/components/strokes-component.js +44 -0
- package/dist/props/components/strokes-component.js.map +1 -0
- package/dist/props/components/vector-network-component.d.ts +3 -0
- package/dist/props/components/vector-network-component.d.ts.map +1 -0
- package/dist/props/components/vector-network-component.js +10 -0
- package/dist/props/components/vector-network-component.js.map +1 -0
- package/dist/props/components/vector-networks-component.d.ts +2 -0
- package/dist/props/components/vector-networks-component.d.ts.map +1 -0
- package/dist/props/components/vector-networks-component.js +28 -0
- package/dist/props/components/vector-networks-component.js.map +1 -0
- package/dist/props/components/vector-point-component.d.ts +3 -0
- package/dist/props/components/vector-point-component.d.ts.map +1 -0
- package/dist/props/components/vector-point-component.js +15 -0
- package/dist/props/components/vector-point-component.js.map +1 -0
- package/dist/props/components/vector-points-component.d.ts +2 -0
- package/dist/props/components/vector-points-component.d.ts.map +1 -0
- package/dist/props/components/vector-points-component.js +66 -0
- package/dist/props/components/vector-points-component.js.map +1 -0
- package/dist/props/components/vector-segment-component.d.ts +3 -0
- package/dist/props/components/vector-segment-component.d.ts.map +1 -0
- package/dist/props/components/vector-segment-component.js +11 -0
- package/dist/props/components/vector-segment-component.js.map +1 -0
- package/dist/props/components/vector-segments-component.d.ts +2 -0
- package/dist/props/components/vector-segments-component.d.ts.map +1 -0
- package/dist/props/components/vector-segments-component.js +25 -0
- package/dist/props/components/vector-segments-component.js.map +1 -0
- package/dist/props/register-property-schemas.d.ts +7 -0
- package/dist/props/register-property-schemas.d.ts.map +1 -0
- package/dist/props/register-property-schemas.js +475 -0
- package/dist/props/register-property-schemas.js.map +1 -0
- package/dist/registration.d.ts +6 -0
- package/dist/registration.d.ts.map +1 -0
- package/dist/registration.js +15 -0
- package/dist/registration.js.map +1 -0
- package/dist/render-layers/index.d.ts +3 -0
- package/dist/render-layers/index.d.ts.map +1 -0
- package/dist/render-layers/index.js +3 -0
- package/dist/render-layers/index.js.map +1 -0
- package/dist/render-layers/selection-overlay-render-layer.d.ts +15 -0
- package/dist/render-layers/selection-overlay-render-layer.d.ts.map +1 -0
- package/dist/render-layers/selection-overlay-render-layer.js +393 -0
- package/dist/render-layers/selection-overlay-render-layer.js.map +1 -0
- package/dist/render-layers/vector-path-editing-render-layer.d.ts +31 -0
- package/dist/render-layers/vector-path-editing-render-layer.d.ts.map +1 -0
- package/dist/render-layers/vector-path-editing-render-layer.js +815 -0
- package/dist/render-layers/vector-path-editing-render-layer.js.map +1 -0
- package/dist/selection/channels.d.ts +23 -0
- package/dist/selection/channels.d.ts.map +1 -0
- package/dist/selection/channels.js +25 -0
- package/dist/selection/channels.js.map +1 -0
- package/dist/selection/ids.d.ts +8 -0
- package/dist/selection/ids.d.ts.map +1 -0
- package/dist/selection/ids.js +49 -0
- package/dist/selection/ids.js.map +1 -0
- package/dist/selection/register-default-selections.d.ts +6 -0
- package/dist/selection/register-default-selections.d.ts.map +1 -0
- package/dist/selection/register-default-selections.js +52 -0
- package/dist/selection/register-default-selections.js.map +1 -0
- package/dist/subscriptions/data-channel.d.ts +12 -0
- package/dist/subscriptions/data-channel.d.ts.map +1 -0
- package/dist/subscriptions/data-channel.js +1366 -0
- package/dist/subscriptions/data-channel.js.map +1 -0
- package/dist/subscriptions/index.d.ts +4 -0
- package/dist/subscriptions/index.d.ts.map +1 -0
- package/dist/subscriptions/index.js +4 -0
- package/dist/subscriptions/index.js.map +1 -0
- package/dist/subscriptions/render-system.d.ts +3 -0
- package/dist/subscriptions/render-system.d.ts.map +1 -0
- package/dist/subscriptions/render-system.js +41 -0
- package/dist/subscriptions/render-system.js.map +1 -0
- package/dist/subscriptions/shared-channels.d.ts +7 -0
- package/dist/subscriptions/shared-channels.d.ts.map +1 -0
- package/dist/subscriptions/shared-channels.js +67 -0
- package/dist/subscriptions/shared-channels.js.map +1 -0
- package/dist/system-property-keys.d.ts +69 -0
- package/dist/system-property-keys.d.ts.map +1 -0
- package/dist/system-property-keys.js +44 -0
- package/dist/system-property-keys.js.map +1 -0
- package/dist/types.d.ts +32 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui/register-properties.d.ts +12 -0
- package/dist/ui/register-properties.d.ts.map +1 -0
- package/dist/ui/register-properties.js +318 -0
- package/dist/ui/register-properties.js.map +1 -0
- package/dist/vector/synthetic-handle.d.ts +3 -0
- package/dist/vector/synthetic-handle.d.ts.map +1 -0
- package/dist/vector/synthetic-handle.js +38 -0
- package/dist/vector/synthetic-handle.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,1366 @@
|
|
|
1
|
+
import { defineDataChannelObserver, EventTypes, propertyRegistry, renderSceneTreeStore, renderSelectionStore, subscribeToSynchronousEvent, uiContext, subscribeToFileLoadComplete } from '@asyra/core';
|
|
2
|
+
import { EntityTypes, SCENE_TREE_ACTIONS, SharedDataChannelNames, emitDiagnosticCounter, measureBrowserDragPhase } from '@asyra/utils';
|
|
3
|
+
import { subscribeToEventBatches } from '@asyra/reactive-events';
|
|
4
|
+
import { createCleanupReporter } from '../cleanup-reporter.js';
|
|
5
|
+
import { deriveGroupBounds } from '../components/group.js';
|
|
6
|
+
import { SelectionActions, SelectionChannels, SelectionEventNames } from '../selection/channels.js';
|
|
7
|
+
const ELEMENT_DATA_MAP_COMPUTED_KEYS = new Set([
|
|
8
|
+
'name',
|
|
9
|
+
'type',
|
|
10
|
+
'visible',
|
|
11
|
+
'lock'
|
|
12
|
+
]);
|
|
13
|
+
const shouldUpdateElementDataMapForComputedKey = (key) => ELEMENT_DATA_MAP_COMPUTED_KEYS.has(key);
|
|
14
|
+
const RENDER_PROJECTION_OUTCOMES = new Set([
|
|
15
|
+
'applied',
|
|
16
|
+
'resynced',
|
|
17
|
+
'removed',
|
|
18
|
+
'failed'
|
|
19
|
+
]);
|
|
20
|
+
const recordRenderProjectionOutcome = (outcome) => {
|
|
21
|
+
const status = outcome && typeof outcome === 'object' && 'status' in outcome
|
|
22
|
+
? outcome.status
|
|
23
|
+
: undefined;
|
|
24
|
+
if (typeof status === 'string' && RENDER_PROJECTION_OUTCOMES.has(status)) {
|
|
25
|
+
emitDiagnosticCounter(`render-projection-outcome-${status}`);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
emitDiagnosticCounter('render-projection-outcome-missing');
|
|
29
|
+
}
|
|
30
|
+
return outcome;
|
|
31
|
+
};
|
|
32
|
+
const getMatchingPropertiesForSceneTreeChange = (change) => {
|
|
33
|
+
if (change.action === SCENE_TREE_ACTIONS.UPDATE_ELEMENT_DATA) {
|
|
34
|
+
const rawChange = change;
|
|
35
|
+
return Array.from(new Set(rawChange.changes.flatMap(({ key, before, after }) => propertyRegistry.getMatchingProperties({
|
|
36
|
+
action: SCENE_TREE_ACTIONS.UPDATE_ELEMENT_DATA,
|
|
37
|
+
eventName: rawChange.eventName,
|
|
38
|
+
id: rawChange.id,
|
|
39
|
+
key,
|
|
40
|
+
before,
|
|
41
|
+
after,
|
|
42
|
+
options: rawChange.options
|
|
43
|
+
}))));
|
|
44
|
+
}
|
|
45
|
+
if (change.action !== SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_BATCH &&
|
|
46
|
+
change.action !== SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_PATCH) {
|
|
47
|
+
return propertyRegistry.getMatchingProperties(change);
|
|
48
|
+
}
|
|
49
|
+
if (change.action === SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_PATCH) {
|
|
50
|
+
const patchChange = change;
|
|
51
|
+
const changedKeys = [
|
|
52
|
+
...Object.keys(patchChange.patch.values ?? {}),
|
|
53
|
+
...Object.keys(patchChange.patch.records ?? {})
|
|
54
|
+
];
|
|
55
|
+
return Array.from(new Set(changedKeys.flatMap((key) => propertyRegistry.getMatchingProperties({
|
|
56
|
+
action: SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA,
|
|
57
|
+
eventName: patchChange.eventName,
|
|
58
|
+
id: patchChange.id,
|
|
59
|
+
owner: 'computed',
|
|
60
|
+
key,
|
|
61
|
+
before: null,
|
|
62
|
+
after: null,
|
|
63
|
+
options: patchChange.options
|
|
64
|
+
}))));
|
|
65
|
+
}
|
|
66
|
+
const batchChange = change;
|
|
67
|
+
return Array.from(new Set(batchChange.changes.flatMap(({ owner, key, before, after }) => propertyRegistry.getMatchingProperties({
|
|
68
|
+
action: SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA,
|
|
69
|
+
eventName: batchChange.eventName,
|
|
70
|
+
id: batchChange.id,
|
|
71
|
+
owner,
|
|
72
|
+
key,
|
|
73
|
+
before,
|
|
74
|
+
after,
|
|
75
|
+
options: batchChange.options
|
|
76
|
+
}))));
|
|
77
|
+
};
|
|
78
|
+
// Render observers keep render-internal stores in sync with shared channel changes.
|
|
79
|
+
const updateRenderSceneTree = (change) => {
|
|
80
|
+
switch (change.action) {
|
|
81
|
+
case SCENE_TREE_ACTIONS.ADD_ELEMENT: {
|
|
82
|
+
const { data, parentId, index } = change;
|
|
83
|
+
if (typeof parentId === 'string' &&
|
|
84
|
+
Number.isInteger(index) &&
|
|
85
|
+
index >= 0) {
|
|
86
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.addElements([
|
|
87
|
+
{ data, parentId, index: index }
|
|
88
|
+
])[0]);
|
|
89
|
+
}
|
|
90
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.addElementById(data.id, parentId, index));
|
|
91
|
+
}
|
|
92
|
+
case SCENE_TREE_ACTIONS.REMOVE_ELEMENT: {
|
|
93
|
+
const { parentId, index, data } = change;
|
|
94
|
+
if (typeof parentId === 'string' &&
|
|
95
|
+
Number.isInteger(index) &&
|
|
96
|
+
index >= 0) {
|
|
97
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.removeElements([
|
|
98
|
+
{ data, parentId, index: index }
|
|
99
|
+
])[0]);
|
|
100
|
+
}
|
|
101
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.removeElement(data, parentId, index));
|
|
102
|
+
}
|
|
103
|
+
case SCENE_TREE_ACTIONS.ADD_ELEMENTS: {
|
|
104
|
+
const { entries } = change;
|
|
105
|
+
return renderSceneTreeStore
|
|
106
|
+
.addElements(entries)
|
|
107
|
+
.map(recordRenderProjectionOutcome);
|
|
108
|
+
}
|
|
109
|
+
case SCENE_TREE_ACTIONS.REMOVE_ELEMENTS: {
|
|
110
|
+
const { entries } = change;
|
|
111
|
+
return renderSceneTreeStore
|
|
112
|
+
.removeElements(entries)
|
|
113
|
+
.map(recordRenderProjectionOutcome);
|
|
114
|
+
}
|
|
115
|
+
case SCENE_TREE_ACTIONS.MOVE_ELEMENTS: {
|
|
116
|
+
const { moves } = change;
|
|
117
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.moveElements(moves));
|
|
118
|
+
}
|
|
119
|
+
case SCENE_TREE_ACTIONS.REMOVE_SUBTREE:
|
|
120
|
+
case SCENE_TREE_ACTIONS.RESTORE_SUBTREE:
|
|
121
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.applySubtreeChange(change));
|
|
122
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_DATA: {
|
|
123
|
+
const { id, changes, options } = change;
|
|
124
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.updateElementBatch(id, changes.map(({ key, before, after }) => ({
|
|
125
|
+
owner: 'raw',
|
|
126
|
+
key,
|
|
127
|
+
before,
|
|
128
|
+
after
|
|
129
|
+
})), options));
|
|
130
|
+
}
|
|
131
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA: {
|
|
132
|
+
const { id, owner, key, before, after, options } = change;
|
|
133
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.updateElement(id, owner, key, before, after, options));
|
|
134
|
+
}
|
|
135
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_BATCH: {
|
|
136
|
+
const { id, changes, options } = change;
|
|
137
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.updateElementBatch(id, changes, options));
|
|
138
|
+
}
|
|
139
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_PATCH: {
|
|
140
|
+
const { id, patch, options } = change;
|
|
141
|
+
return recordRenderProjectionOutcome(renderSceneTreeStore.updateElementPatch(id, patch, options));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const toRenderElementAddition = (change) => {
|
|
146
|
+
if (change.action !== SCENE_TREE_ACTIONS.ADD_ELEMENT) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const { data, parentId, index } = change;
|
|
150
|
+
if (typeof data.id !== 'string' ||
|
|
151
|
+
data.id.length === 0 ||
|
|
152
|
+
typeof parentId !== 'string' ||
|
|
153
|
+
parentId.length === 0 ||
|
|
154
|
+
!Number.isInteger(index) ||
|
|
155
|
+
index < 0) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
data,
|
|
160
|
+
parentId,
|
|
161
|
+
index: index
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
const toLocalComputedSceneTreeChange = (event) => {
|
|
165
|
+
const { payload } = event;
|
|
166
|
+
if ('patch' in payload) {
|
|
167
|
+
return {
|
|
168
|
+
action: SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_PATCH,
|
|
169
|
+
eventName: EventTypes.UPDATE_COMPUTED_DATA_PATCH,
|
|
170
|
+
id: payload.id,
|
|
171
|
+
patch: payload.patch
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if ('changes' in payload) {
|
|
175
|
+
if (payload.changes.some(({ owner }) => owner !== 'computed')) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
action: SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_BATCH,
|
|
180
|
+
eventName: EventTypes.UPDATE_COMPUTED_DATA,
|
|
181
|
+
id: payload.id,
|
|
182
|
+
changes: payload.changes
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (payload.owner !== 'computed') {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
action: SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA,
|
|
190
|
+
eventName: EventTypes.UPDATE_COMPUTED_DATA,
|
|
191
|
+
id: payload.id,
|
|
192
|
+
owner: payload.owner,
|
|
193
|
+
key: payload.key,
|
|
194
|
+
before: payload.before,
|
|
195
|
+
after: payload.after
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
export const projectLocalComputedEventToRender = (event) => {
|
|
199
|
+
const change = toLocalComputedSceneTreeChange(event);
|
|
200
|
+
if (change) {
|
|
201
|
+
return updateRenderSceneTree(change);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
const isLocalComputedSceneTreeChange = (change) => change.action === SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA ||
|
|
205
|
+
change.action === SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_BATCH ||
|
|
206
|
+
change.action === SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_PATCH;
|
|
207
|
+
const toLocalComputedProjectionEvent = (event) => {
|
|
208
|
+
if (event.type !== EventTypes.UPDATE_COMPUTED_DATA &&
|
|
209
|
+
event.type !== EventTypes.UPDATE_COMPUTED_DATA_PATCH) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
return event;
|
|
213
|
+
};
|
|
214
|
+
const updateRenderSceneTreeBatch = (changes) => {
|
|
215
|
+
let changeIndex = 0;
|
|
216
|
+
while (changeIndex < changes.length) {
|
|
217
|
+
const change = changes[changeIndex];
|
|
218
|
+
const firstAddition = toRenderElementAddition(change);
|
|
219
|
+
if (!firstAddition) {
|
|
220
|
+
updateRenderSceneTree(change);
|
|
221
|
+
changeIndex += 1;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const additions = [firstAddition];
|
|
225
|
+
let nextIndex = changeIndex + 1;
|
|
226
|
+
while (nextIndex < changes.length) {
|
|
227
|
+
const nextAddition = toRenderElementAddition(changes[nextIndex]);
|
|
228
|
+
if (!nextAddition || nextAddition.parentId !== firstAddition.parentId) {
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
additions.push(nextAddition);
|
|
232
|
+
nextIndex += 1;
|
|
233
|
+
}
|
|
234
|
+
renderSceneTreeStore
|
|
235
|
+
.addElements(additions)
|
|
236
|
+
.forEach(recordRenderProjectionOutcome);
|
|
237
|
+
changeIndex = nextIndex;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
// Render selection mirror used by overlay/render behavior.
|
|
241
|
+
const updateRenderSelection = (change) => {
|
|
242
|
+
switch (change.action) {
|
|
243
|
+
case SelectionActions.SELECT_ELEMENTS:
|
|
244
|
+
case SelectionActions.DESELECT_ELEMENTS:
|
|
245
|
+
renderSelectionStore.updateSelection(SelectionChannels.ELEMENT);
|
|
246
|
+
break;
|
|
247
|
+
case SelectionActions.SELECT_VECTOR_POINTS:
|
|
248
|
+
case SelectionActions.DESELECT_VECTOR_POINTS:
|
|
249
|
+
renderSelectionStore.updateSelection(SelectionChannels.VECTOR_POINT);
|
|
250
|
+
break;
|
|
251
|
+
case SelectionActions.SELECT_VECTOR_SEGMENTS:
|
|
252
|
+
case SelectionActions.DESELECT_VECTOR_SEGMENTS:
|
|
253
|
+
renderSelectionStore.updateSelection(SelectionChannels.VECTOR_SEGMENT);
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
// Data-channel observer definitions for render defaults.
|
|
258
|
+
const renderSelectionDataChannelObserver = defineDataChannelObserver({
|
|
259
|
+
name: 'preset.render.selection',
|
|
260
|
+
channel: SharedDataChannelNames.SELECTION,
|
|
261
|
+
onChange: updateRenderSelection
|
|
262
|
+
});
|
|
263
|
+
// Read current selected IDs from selection runtime (single source of truth).
|
|
264
|
+
const getSelectedIds = (core, type) => {
|
|
265
|
+
const selection = core.getSelection(type);
|
|
266
|
+
return selection ? selection.getSelectedIds() : new Set();
|
|
267
|
+
};
|
|
268
|
+
const readElementLayoutBounds = (deps, elementId, visitedIds) => {
|
|
269
|
+
if (visitedIds.has(elementId)) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
const element = deps.sceneTree.getElementById(elementId);
|
|
273
|
+
const computed = element
|
|
274
|
+
?.computed;
|
|
275
|
+
if (!element || !computed) {
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
const x = computed.get('x');
|
|
279
|
+
const y = computed.get('y');
|
|
280
|
+
if (typeof x !== 'number' ||
|
|
281
|
+
!Number.isFinite(x) ||
|
|
282
|
+
typeof y !== 'number' ||
|
|
283
|
+
!Number.isFinite(y)) {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
if (element.get('type') !== EntityTypes.GROUP) {
|
|
287
|
+
const width = computed.get('width');
|
|
288
|
+
const height = computed.get('height');
|
|
289
|
+
if (typeof width !== 'number' ||
|
|
290
|
+
!Number.isFinite(width) ||
|
|
291
|
+
typeof height !== 'number' ||
|
|
292
|
+
!Number.isFinite(height)) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
return { x, y, width, height };
|
|
296
|
+
}
|
|
297
|
+
const children = element.save().children;
|
|
298
|
+
if (!Array.isArray(children) ||
|
|
299
|
+
children.some((childId) => typeof childId !== 'string')) {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
if (children.length === 0) {
|
|
303
|
+
return { x, y, width: 0, height: 0 };
|
|
304
|
+
}
|
|
305
|
+
const nextVisitedIds = new Set(visitedIds);
|
|
306
|
+
nextVisitedIds.add(elementId);
|
|
307
|
+
const childBounds = [];
|
|
308
|
+
for (const childId of children) {
|
|
309
|
+
const bounds = readElementLayoutBounds(deps, childId, nextVisitedIds);
|
|
310
|
+
if (!bounds) {
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
childBounds.push(bounds);
|
|
314
|
+
}
|
|
315
|
+
const contentBounds = deriveGroupBounds(childBounds);
|
|
316
|
+
return {
|
|
317
|
+
x: x + contentBounds.x,
|
|
318
|
+
y: y + contentBounds.y,
|
|
319
|
+
width: contentBounds.width,
|
|
320
|
+
height: contentBounds.height
|
|
321
|
+
};
|
|
322
|
+
};
|
|
323
|
+
// Build ui-context aggregate compute input from current selection + scene-tree data.
|
|
324
|
+
const buildSelectionContext = (deps, selectedIds) => {
|
|
325
|
+
const elements = [...selectedIds].reduce((acc, elementId) => {
|
|
326
|
+
const element = deps.sceneTree.getElementById(elementId);
|
|
327
|
+
if (!element) {
|
|
328
|
+
return acc;
|
|
329
|
+
}
|
|
330
|
+
const elementData = element.getAllComputedData();
|
|
331
|
+
if (element.get('type') !== EntityTypes.GROUP) {
|
|
332
|
+
acc.push(elementData);
|
|
333
|
+
return acc;
|
|
334
|
+
}
|
|
335
|
+
const currentBounds = readElementLayoutBounds(deps, elementId, new Set());
|
|
336
|
+
if (currentBounds) {
|
|
337
|
+
acc.push({
|
|
338
|
+
...elementData,
|
|
339
|
+
...currentBounds
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
return acc;
|
|
343
|
+
}, []);
|
|
344
|
+
return {
|
|
345
|
+
selectedIds,
|
|
346
|
+
elements
|
|
347
|
+
};
|
|
348
|
+
};
|
|
349
|
+
// Sync selection-related ui-context properties and run aggregate recompute (x/y/width/...).
|
|
350
|
+
const syncElementSelectionAndDerived = (core, deps) => {
|
|
351
|
+
const selectedIds = getSelectedIds(core, SelectionChannels.ELEMENT);
|
|
352
|
+
uiContext.set('elementSelection', selectedIds);
|
|
353
|
+
uiContext.recomputeSelectionProperties(buildSelectionContext(deps, selectedIds));
|
|
354
|
+
};
|
|
355
|
+
const syncSelectionOnElementRemovals = (core, removedIds) => {
|
|
356
|
+
const selection = core.getSelection(SelectionChannels.ELEMENT);
|
|
357
|
+
if (!selection) {
|
|
358
|
+
return false;
|
|
359
|
+
}
|
|
360
|
+
const removedIdSet = new Set(removedIds.filter((removedId) => typeof removedId === 'string' && removedId.length > 0));
|
|
361
|
+
if (removedIdSet.size === 0) {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
const current = Array.from(selection.getSelectedIds());
|
|
365
|
+
const next = current.filter((id) => !removedIdSet.has(id));
|
|
366
|
+
if (next.length === current.length) {
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
selection.select(next);
|
|
370
|
+
selection.cleanChanges();
|
|
371
|
+
renderSelectionStore.updateSelection(SelectionChannels.ELEMENT);
|
|
372
|
+
return true;
|
|
373
|
+
};
|
|
374
|
+
// Sync vector point/segment selection mirrors for UI consumers.
|
|
375
|
+
const syncVectorSelections = (core) => {
|
|
376
|
+
uiContext.set('vectorPointSelection', getSelectedIds(core, SelectionChannels.VECTOR_POINT));
|
|
377
|
+
uiContext.set('vectorSegmentSelection', getSelectedIds(core, SelectionChannels.VECTOR_SEGMENT));
|
|
378
|
+
};
|
|
379
|
+
// Build a depth-first flattened element id index from the scene-tree.
|
|
380
|
+
const collectChildrenIds = (deps, elementId, ids) => {
|
|
381
|
+
const element = deps.sceneTree.getElementById(elementId);
|
|
382
|
+
if (!element) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
ids.push(elementId);
|
|
386
|
+
const elementData = element.save();
|
|
387
|
+
if (elementData.type === EntityTypes.GROUP) {
|
|
388
|
+
const children = elementData.children ?? [];
|
|
389
|
+
children.forEach((childId) => {
|
|
390
|
+
collectChildrenIds(deps, childId, ids);
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
const getFlattenedElementIds = (deps) => {
|
|
395
|
+
const ids = [];
|
|
396
|
+
const workspace = deps.sceneTree.currentWorkspace;
|
|
397
|
+
if (!workspace) {
|
|
398
|
+
return ids;
|
|
399
|
+
}
|
|
400
|
+
const workspaceData = workspace.save();
|
|
401
|
+
const workspaceChildren = workspaceData.children ?? [];
|
|
402
|
+
workspaceChildren.forEach((childId) => {
|
|
403
|
+
collectChildrenIds(deps, childId, ids);
|
|
404
|
+
});
|
|
405
|
+
return ids;
|
|
406
|
+
};
|
|
407
|
+
// Publish flattened ids so UI can do fast filtering/lookup without tree traversal.
|
|
408
|
+
const syncFlattenedElementIds = (deps) => {
|
|
409
|
+
uiContext.set('flattenedElementIds', getFlattenedElementIds(deps));
|
|
410
|
+
};
|
|
411
|
+
// Published UI lookup map keyed by element id (used by element row rendering).
|
|
412
|
+
const getElementDataMap = (deps) => {
|
|
413
|
+
const dataMap = {};
|
|
414
|
+
deps.sceneTree.getAllElements().forEach((element, elementId) => {
|
|
415
|
+
if (element.get('type') === EntityTypes.WORKSPACE) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
dataMap[elementId] = element.save();
|
|
419
|
+
});
|
|
420
|
+
return dataMap;
|
|
421
|
+
};
|
|
422
|
+
const syncElementDataMap = (deps) => {
|
|
423
|
+
uiContext.set('elementDataMap', getElementDataMap(deps));
|
|
424
|
+
};
|
|
425
|
+
const PROJECTED_ELEMENT_MAP_BUCKET_COUNT = 256;
|
|
426
|
+
const PROJECTED_ELEMENT_DELETED = Symbol('projected-element-deleted');
|
|
427
|
+
const projectedElementDataMapBackingByView = new WeakMap();
|
|
428
|
+
const getProjectedElementBucketIndex = (elementId) => {
|
|
429
|
+
let hash = 0;
|
|
430
|
+
for (let index = 0; index < elementId.length; index += 1) {
|
|
431
|
+
hash = (hash * 31 + elementId.charCodeAt(index)) >>> 0;
|
|
432
|
+
}
|
|
433
|
+
return hash % PROJECTED_ELEMENT_MAP_BUCKET_COUNT;
|
|
434
|
+
};
|
|
435
|
+
const getProjectedElementFromBacking = (backing, elementId) => {
|
|
436
|
+
const bucket = backing.buckets[getProjectedElementBucketIndex(elementId)];
|
|
437
|
+
if (bucket?.has(elementId)) {
|
|
438
|
+
const value = bucket.get(elementId);
|
|
439
|
+
return value === PROJECTED_ELEMENT_DELETED ? undefined : value;
|
|
440
|
+
}
|
|
441
|
+
return backing.base[elementId];
|
|
442
|
+
};
|
|
443
|
+
const listProjectedElementBackingKeys = (backing, staged) => {
|
|
444
|
+
const keys = new Set();
|
|
445
|
+
backing.orderedKeys.forEach((elementId) => {
|
|
446
|
+
if (getProjectedElementFromBacking(backing, elementId) !== undefined) {
|
|
447
|
+
keys.add(elementId);
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
Object.keys(backing.base).forEach((elementId) => {
|
|
451
|
+
if (getProjectedElementFromBacking(backing, elementId) !== undefined) {
|
|
452
|
+
keys.add(elementId);
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
backing.buckets.forEach((bucket) => {
|
|
456
|
+
bucket?.forEach((value, elementId) => {
|
|
457
|
+
if (value === PROJECTED_ELEMENT_DELETED) {
|
|
458
|
+
keys.delete(elementId);
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
keys.add(elementId);
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
staged?.forEach((value, elementId) => {
|
|
466
|
+
if (value === PROJECTED_ELEMENT_DELETED) {
|
|
467
|
+
keys.delete(elementId);
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
keys.add(elementId);
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
return [...keys];
|
|
474
|
+
};
|
|
475
|
+
const createProjectedElementDataMapView = (backing) => {
|
|
476
|
+
const target = {};
|
|
477
|
+
const view = new Proxy(target, {
|
|
478
|
+
get: (_target, property, receiver) => {
|
|
479
|
+
if (typeof property === 'string') {
|
|
480
|
+
const value = getProjectedElementFromBacking(backing, property);
|
|
481
|
+
if (value !== undefined) {
|
|
482
|
+
return value;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return Reflect.get(target, property, receiver);
|
|
486
|
+
},
|
|
487
|
+
has: (_target, property) => typeof property === 'string'
|
|
488
|
+
? getProjectedElementFromBacking(backing, property) !== undefined
|
|
489
|
+
: Reflect.has(target, property),
|
|
490
|
+
ownKeys: () => listProjectedElementBackingKeys(backing),
|
|
491
|
+
getOwnPropertyDescriptor: (_target, property) => {
|
|
492
|
+
if (typeof property !== 'string') {
|
|
493
|
+
return Reflect.getOwnPropertyDescriptor(target, property);
|
|
494
|
+
}
|
|
495
|
+
const value = getProjectedElementFromBacking(backing, property);
|
|
496
|
+
return value === undefined
|
|
497
|
+
? undefined
|
|
498
|
+
: {
|
|
499
|
+
configurable: true,
|
|
500
|
+
enumerable: true,
|
|
501
|
+
value,
|
|
502
|
+
writable: false
|
|
503
|
+
};
|
|
504
|
+
},
|
|
505
|
+
set: () => false,
|
|
506
|
+
deleteProperty: () => false
|
|
507
|
+
});
|
|
508
|
+
projectedElementDataMapBackingByView.set(view, backing);
|
|
509
|
+
return view;
|
|
510
|
+
};
|
|
511
|
+
const createProjectedElementDataMapMutation = (current, currentOrderedKeys) => {
|
|
512
|
+
const backing = projectedElementDataMapBackingByView.get(current) ?? {
|
|
513
|
+
base: current,
|
|
514
|
+
buckets: new Array(PROJECTED_ELEMENT_MAP_BUCKET_COUNT),
|
|
515
|
+
orderedKeys: currentOrderedKeys
|
|
516
|
+
};
|
|
517
|
+
const staged = new Map();
|
|
518
|
+
const read = (elementId) => {
|
|
519
|
+
if (staged.has(elementId)) {
|
|
520
|
+
const value = staged.get(elementId);
|
|
521
|
+
return value === PROJECTED_ELEMENT_DELETED ? undefined : value;
|
|
522
|
+
}
|
|
523
|
+
return getProjectedElementFromBacking(backing, elementId);
|
|
524
|
+
};
|
|
525
|
+
const target = {};
|
|
526
|
+
const value = new Proxy(target, {
|
|
527
|
+
get: (_target, property, receiver) => {
|
|
528
|
+
if (typeof property === 'string') {
|
|
529
|
+
const projected = read(property);
|
|
530
|
+
if (projected !== undefined) {
|
|
531
|
+
return projected;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return Reflect.get(target, property, receiver);
|
|
535
|
+
},
|
|
536
|
+
set: (_target, property, nextValue) => {
|
|
537
|
+
if (typeof property !== 'string') {
|
|
538
|
+
return false;
|
|
539
|
+
}
|
|
540
|
+
staged.set(property, nextValue);
|
|
541
|
+
return true;
|
|
542
|
+
},
|
|
543
|
+
deleteProperty: (_target, property) => {
|
|
544
|
+
if (typeof property !== 'string') {
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
staged.set(property, PROJECTED_ELEMENT_DELETED);
|
|
548
|
+
return true;
|
|
549
|
+
},
|
|
550
|
+
has: (_target, property) => typeof property === 'string'
|
|
551
|
+
? read(property) !== undefined
|
|
552
|
+
: Reflect.has(target, property),
|
|
553
|
+
ownKeys: () => listProjectedElementBackingKeys(backing, staged),
|
|
554
|
+
getOwnPropertyDescriptor: (_target, property) => {
|
|
555
|
+
if (typeof property !== 'string') {
|
|
556
|
+
return Reflect.getOwnPropertyDescriptor(target, property);
|
|
557
|
+
}
|
|
558
|
+
const projected = read(property);
|
|
559
|
+
return projected === undefined
|
|
560
|
+
? undefined
|
|
561
|
+
: {
|
|
562
|
+
configurable: true,
|
|
563
|
+
enumerable: true,
|
|
564
|
+
value: projected,
|
|
565
|
+
writable: true
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
return {
|
|
570
|
+
value,
|
|
571
|
+
commit: (orderedKeys) => {
|
|
572
|
+
const buckets = [...backing.buckets];
|
|
573
|
+
const mutableBuckets = new Map();
|
|
574
|
+
staged.forEach((stagedValue, elementId) => {
|
|
575
|
+
const bucketIndex = getProjectedElementBucketIndex(elementId);
|
|
576
|
+
let bucket = mutableBuckets.get(bucketIndex);
|
|
577
|
+
if (!bucket) {
|
|
578
|
+
bucket = new Map(buckets[bucketIndex]);
|
|
579
|
+
mutableBuckets.set(bucketIndex, bucket);
|
|
580
|
+
buckets[bucketIndex] = bucket;
|
|
581
|
+
}
|
|
582
|
+
bucket.set(elementId, stagedValue);
|
|
583
|
+
});
|
|
584
|
+
return createProjectedElementDataMapView({
|
|
585
|
+
base: backing.base,
|
|
586
|
+
buckets,
|
|
587
|
+
orderedKeys
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
};
|
|
592
|
+
const cloneProjectedElementData = (value) => {
|
|
593
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
596
|
+
const data = { ...value };
|
|
597
|
+
if (Array.isArray(data.children)) {
|
|
598
|
+
data.children = [...data.children];
|
|
599
|
+
}
|
|
600
|
+
return data;
|
|
601
|
+
};
|
|
602
|
+
const getProjectedParentId = (data) => typeof data?.parentId === 'string' && data.parentId.length > 0
|
|
603
|
+
? data.parentId
|
|
604
|
+
: undefined;
|
|
605
|
+
const isProjectedHierarchyKey = (key) => key === 'parentId' || key === 'children';
|
|
606
|
+
const sceneTreeChangeUpdatesProjectedHierarchy = (change) => {
|
|
607
|
+
switch (change.action) {
|
|
608
|
+
case SCENE_TREE_ACTIONS.ADD_ELEMENT:
|
|
609
|
+
case SCENE_TREE_ACTIONS.ADD_ELEMENTS:
|
|
610
|
+
case SCENE_TREE_ACTIONS.REMOVE_ELEMENT:
|
|
611
|
+
case SCENE_TREE_ACTIONS.REMOVE_ELEMENTS:
|
|
612
|
+
case SCENE_TREE_ACTIONS.MOVE_ELEMENTS:
|
|
613
|
+
case SCENE_TREE_ACTIONS.REMOVE_SUBTREE:
|
|
614
|
+
case SCENE_TREE_ACTIONS.RESTORE_SUBTREE:
|
|
615
|
+
return true;
|
|
616
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA:
|
|
617
|
+
return isProjectedHierarchyKey(change.key);
|
|
618
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_BATCH:
|
|
619
|
+
return change.changes.some(({ key }) => isProjectedHierarchyKey(key));
|
|
620
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_PATCH:
|
|
621
|
+
return Object.keys(change.patch.values ?? {}).some(isProjectedHierarchyKey);
|
|
622
|
+
default:
|
|
623
|
+
return false;
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
const projectUIContextSceneTreeBatch = (changes) => {
|
|
627
|
+
if (changes.length === 0) {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
measureBrowserDragPhase('ui-context:project-scene-tree-batch', () => {
|
|
631
|
+
const updatesHierarchy = changes.some(sceneTreeChangeUpdatesProjectedHierarchy);
|
|
632
|
+
const current = uiContext.get('elementDataMap') ?? {};
|
|
633
|
+
const currentFlattenedIds = uiContext.get('flattenedElementIds') ?? [];
|
|
634
|
+
const mutation = createProjectedElementDataMapMutation(current, currentFlattenedIds);
|
|
635
|
+
const next = mutation.value;
|
|
636
|
+
let mapChanged = false;
|
|
637
|
+
const childrenByParent = new Map();
|
|
638
|
+
const dirtyParentIds = new Set();
|
|
639
|
+
const changedElementIds = new Set();
|
|
640
|
+
const ensureMutableMap = () => {
|
|
641
|
+
mapChanged = true;
|
|
642
|
+
return next;
|
|
643
|
+
};
|
|
644
|
+
if (updatesHierarchy) {
|
|
645
|
+
currentFlattenedIds.forEach((elementId) => {
|
|
646
|
+
const parentId = getProjectedParentId(current[elementId]);
|
|
647
|
+
if (!parentId)
|
|
648
|
+
return;
|
|
649
|
+
const children = childrenByParent.get(parentId) ?? [];
|
|
650
|
+
children.push(elementId);
|
|
651
|
+
childrenByParent.set(parentId, children);
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
const removeMembership = (parentId, elementId) => {
|
|
655
|
+
if (!parentId)
|
|
656
|
+
return;
|
|
657
|
+
const children = childrenByParent.get(parentId);
|
|
658
|
+
if (!children)
|
|
659
|
+
return;
|
|
660
|
+
const index = children.indexOf(elementId);
|
|
661
|
+
if (index >= 0) {
|
|
662
|
+
children.splice(index, 1);
|
|
663
|
+
dirtyParentIds.add(parentId);
|
|
664
|
+
}
|
|
665
|
+
};
|
|
666
|
+
const insertMembership = (parentId, elementId, requestedIndex) => {
|
|
667
|
+
if (!parentId)
|
|
668
|
+
return;
|
|
669
|
+
emitDiagnosticCounter('ui-context-membership-add-scalar');
|
|
670
|
+
const children = childrenByParent.get(parentId) ?? [];
|
|
671
|
+
const existingIndex = children.indexOf(elementId);
|
|
672
|
+
if (existingIndex >= 0) {
|
|
673
|
+
children.splice(existingIndex, 1);
|
|
674
|
+
}
|
|
675
|
+
const index = Number.isInteger(requestedIndex) && requestedIndex >= 0
|
|
676
|
+
? Math.min(requestedIndex, children.length)
|
|
677
|
+
: children.length;
|
|
678
|
+
children.splice(index, 0, elementId);
|
|
679
|
+
childrenByParent.set(parentId, children);
|
|
680
|
+
dirtyParentIds.add(parentId);
|
|
681
|
+
};
|
|
682
|
+
const insertMembershipBatch = (entries) => {
|
|
683
|
+
if (entries.length === 0) {
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
emitDiagnosticCounter('ui-context-membership-add-batch');
|
|
687
|
+
emitDiagnosticCounter('ui-context-membership-add-batch-entry', entries.length);
|
|
688
|
+
const entriesByParent = new Map();
|
|
689
|
+
entries.forEach(({ parentId, elementId, index }) => {
|
|
690
|
+
const parentEntries = entriesByParent.get(parentId) ?? [];
|
|
691
|
+
parentEntries.push({ elementId, index });
|
|
692
|
+
entriesByParent.set(parentId, parentEntries);
|
|
693
|
+
});
|
|
694
|
+
entriesByParent.forEach((parentEntries, parentId) => {
|
|
695
|
+
const insertedIds = new Set(parentEntries.map(({ elementId }) => elementId));
|
|
696
|
+
const retainedChildren = (childrenByParent.get(parentId) ?? []).filter((childId) => !insertedIds.has(childId));
|
|
697
|
+
const insertionByIndex = new Map(parentEntries.map(({ elementId, index }) => [index, elementId]));
|
|
698
|
+
const children = [];
|
|
699
|
+
let retainedIndex = 0;
|
|
700
|
+
const childCount = retainedChildren.length + insertionByIndex.size;
|
|
701
|
+
for (let index = 0; index < childCount; index += 1) {
|
|
702
|
+
const insertedId = insertionByIndex.get(index);
|
|
703
|
+
if (insertedId) {
|
|
704
|
+
children.push(insertedId);
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
const retainedId = retainedChildren[retainedIndex];
|
|
708
|
+
if (retainedId) {
|
|
709
|
+
children.push(retainedId);
|
|
710
|
+
retainedIndex += 1;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
childrenByParent.set(parentId, children);
|
|
715
|
+
dirtyParentIds.add(parentId);
|
|
716
|
+
});
|
|
717
|
+
};
|
|
718
|
+
const removeMembershipBatch = (entries) => {
|
|
719
|
+
const removedIdsByParent = new Map();
|
|
720
|
+
entries.forEach(({ parentId, elementId }) => {
|
|
721
|
+
const removedIds = removedIdsByParent.get(parentId) ?? new Set();
|
|
722
|
+
removedIds.add(elementId);
|
|
723
|
+
removedIdsByParent.set(parentId, removedIds);
|
|
724
|
+
});
|
|
725
|
+
removedIdsByParent.forEach((removedIds, parentId) => {
|
|
726
|
+
const children = childrenByParent.get(parentId);
|
|
727
|
+
if (!children)
|
|
728
|
+
return;
|
|
729
|
+
const retainedChildren = children.filter((childId) => !removedIds.has(childId));
|
|
730
|
+
if (retainedChildren.length !== children.length) {
|
|
731
|
+
childrenByParent.set(parentId, retainedChildren);
|
|
732
|
+
dirtyParentIds.add(parentId);
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
};
|
|
736
|
+
const setProjectedValue = (elementId, key, value) => {
|
|
737
|
+
const currentData = next[elementId];
|
|
738
|
+
if (!currentData)
|
|
739
|
+
return;
|
|
740
|
+
if (!isProjectedHierarchyKey(key) &&
|
|
741
|
+
!shouldUpdateElementDataMapForComputedKey(key)) {
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
if (key === 'parentId') {
|
|
745
|
+
const beforeParentId = getProjectedParentId(currentData);
|
|
746
|
+
const afterParentId = typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
747
|
+
removeMembership(beforeParentId, elementId);
|
|
748
|
+
insertMembership(afterParentId, elementId);
|
|
749
|
+
}
|
|
750
|
+
else if (key === 'children' && Array.isArray(value)) {
|
|
751
|
+
childrenByParent.set(elementId, value.filter((childId) => typeof childId === 'string' && childId.length > 0));
|
|
752
|
+
dirtyParentIds.add(elementId);
|
|
753
|
+
}
|
|
754
|
+
ensureMutableMap()[elementId] = {
|
|
755
|
+
...currentData,
|
|
756
|
+
[key]: Array.isArray(value) ? [...value] : value
|
|
757
|
+
};
|
|
758
|
+
changedElementIds.add(elementId);
|
|
759
|
+
};
|
|
760
|
+
const pendingScalarAdditionMemberships = [];
|
|
761
|
+
const flushScalarAdditionMemberships = () => {
|
|
762
|
+
if (pendingScalarAdditionMemberships.length === 0) {
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
insertMembershipBatch(pendingScalarAdditionMemberships);
|
|
766
|
+
pendingScalarAdditionMemberships.length = 0;
|
|
767
|
+
};
|
|
768
|
+
changes.forEach((change) => {
|
|
769
|
+
if (change.action !== SCENE_TREE_ACTIONS.ADD_ELEMENT) {
|
|
770
|
+
flushScalarAdditionMemberships();
|
|
771
|
+
}
|
|
772
|
+
switch (change.action) {
|
|
773
|
+
case SCENE_TREE_ACTIONS.ADD_ELEMENT: {
|
|
774
|
+
const { data, parentId, index } = change;
|
|
775
|
+
const elementData = cloneProjectedElementData(data);
|
|
776
|
+
const elementId = typeof elementData?.id === 'string' ? elementData.id : undefined;
|
|
777
|
+
if (!elementData || !elementId)
|
|
778
|
+
return;
|
|
779
|
+
const projectedParentId = typeof parentId === 'string'
|
|
780
|
+
? parentId
|
|
781
|
+
: getProjectedParentId(elementData);
|
|
782
|
+
if (projectedParentId) {
|
|
783
|
+
elementData.parentId = projectedParentId;
|
|
784
|
+
}
|
|
785
|
+
ensureMutableMap()[elementId] = elementData;
|
|
786
|
+
changedElementIds.add(elementId);
|
|
787
|
+
if (Array.isArray(elementData.children)) {
|
|
788
|
+
childrenByParent.set(elementId, elementData.children.filter((childId) => typeof childId === 'string' && childId.length > 0));
|
|
789
|
+
dirtyParentIds.add(elementId);
|
|
790
|
+
}
|
|
791
|
+
if (projectedParentId &&
|
|
792
|
+
Number.isInteger(index) &&
|
|
793
|
+
index >= 0) {
|
|
794
|
+
pendingScalarAdditionMemberships.push({
|
|
795
|
+
parentId: projectedParentId,
|
|
796
|
+
elementId,
|
|
797
|
+
index: index
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
else {
|
|
801
|
+
insertMembership(projectedParentId, elementId, index);
|
|
802
|
+
}
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
case SCENE_TREE_ACTIONS.ADD_ELEMENTS: {
|
|
806
|
+
const { entries } = change;
|
|
807
|
+
const membershipEntries = [];
|
|
808
|
+
entries.forEach(({ data, parentId, index }) => {
|
|
809
|
+
const elementData = cloneProjectedElementData(data);
|
|
810
|
+
const elementId = typeof elementData?.id === 'string' ? elementData.id : undefined;
|
|
811
|
+
if (!elementData || !elementId)
|
|
812
|
+
return;
|
|
813
|
+
elementData.parentId = parentId;
|
|
814
|
+
ensureMutableMap()[elementId] = elementData;
|
|
815
|
+
changedElementIds.add(elementId);
|
|
816
|
+
if (Array.isArray(elementData.children)) {
|
|
817
|
+
childrenByParent.set(elementId, elementData.children.filter((childId) => typeof childId === 'string' && childId.length > 0));
|
|
818
|
+
dirtyParentIds.add(elementId);
|
|
819
|
+
}
|
|
820
|
+
membershipEntries.push({ parentId, elementId, index });
|
|
821
|
+
});
|
|
822
|
+
insertMembershipBatch(membershipEntries);
|
|
823
|
+
break;
|
|
824
|
+
}
|
|
825
|
+
case SCENE_TREE_ACTIONS.REMOVE_ELEMENT: {
|
|
826
|
+
const { data, parentId } = change;
|
|
827
|
+
const elementId = data.id;
|
|
828
|
+
removeMembership(typeof parentId === 'string'
|
|
829
|
+
? parentId
|
|
830
|
+
: getProjectedParentId(next[elementId]), elementId);
|
|
831
|
+
Reflect.deleteProperty(ensureMutableMap(), elementId);
|
|
832
|
+
changedElementIds.add(elementId);
|
|
833
|
+
childrenByParent.delete(elementId);
|
|
834
|
+
break;
|
|
835
|
+
}
|
|
836
|
+
case SCENE_TREE_ACTIONS.REMOVE_ELEMENTS: {
|
|
837
|
+
const { entries } = change;
|
|
838
|
+
removeMembershipBatch(entries.map(({ data, parentId }) => ({
|
|
839
|
+
parentId,
|
|
840
|
+
elementId: data.id
|
|
841
|
+
})));
|
|
842
|
+
entries.forEach(({ data }) => {
|
|
843
|
+
Reflect.deleteProperty(ensureMutableMap(), data.id);
|
|
844
|
+
changedElementIds.add(data.id);
|
|
845
|
+
childrenByParent.delete(data.id);
|
|
846
|
+
});
|
|
847
|
+
break;
|
|
848
|
+
}
|
|
849
|
+
case SCENE_TREE_ACTIONS.MOVE_ELEMENTS: {
|
|
850
|
+
const { moves } = change;
|
|
851
|
+
const movedIds = new Set(moves.map(({ elementId }) => elementId));
|
|
852
|
+
const affectedParentIds = new Set();
|
|
853
|
+
moves.forEach(({ before, after }) => {
|
|
854
|
+
affectedParentIds.add(before.parentId);
|
|
855
|
+
affectedParentIds.add(after.parentId);
|
|
856
|
+
});
|
|
857
|
+
affectedParentIds.forEach((parentId) => {
|
|
858
|
+
const children = childrenByParent.get(parentId);
|
|
859
|
+
if (!children)
|
|
860
|
+
return;
|
|
861
|
+
const retainedChildren = children.filter((childId) => !movedIds.has(childId));
|
|
862
|
+
if (retainedChildren.length !== children.length) {
|
|
863
|
+
childrenByParent.set(parentId, retainedChildren);
|
|
864
|
+
dirtyParentIds.add(parentId);
|
|
865
|
+
}
|
|
866
|
+
});
|
|
867
|
+
moves
|
|
868
|
+
.slice()
|
|
869
|
+
.sort((left, right) => left.after.parentId.localeCompare(right.after.parentId) ||
|
|
870
|
+
left.after.index - right.after.index)
|
|
871
|
+
.forEach(({ elementId, after }) => {
|
|
872
|
+
insertMembership(after.parentId, elementId, after.index);
|
|
873
|
+
});
|
|
874
|
+
moves.forEach(({ elementId, after }) => {
|
|
875
|
+
const elementData = next[elementId];
|
|
876
|
+
if (elementData) {
|
|
877
|
+
ensureMutableMap()[elementId] = {
|
|
878
|
+
...elementData,
|
|
879
|
+
parentId: after.parentId
|
|
880
|
+
};
|
|
881
|
+
changedElementIds.add(elementId);
|
|
882
|
+
}
|
|
883
|
+
});
|
|
884
|
+
break;
|
|
885
|
+
}
|
|
886
|
+
case SCENE_TREE_ACTIONS.REMOVE_SUBTREE:
|
|
887
|
+
case SCENE_TREE_ACTIONS.RESTORE_SUBTREE: {
|
|
888
|
+
const subtree = change;
|
|
889
|
+
if (change.action === SCENE_TREE_ACTIONS.REMOVE_SUBTREE) {
|
|
890
|
+
subtree.removed.forEach(({ elementId, parentId }) => {
|
|
891
|
+
removeMembership(parentId, elementId);
|
|
892
|
+
Reflect.deleteProperty(ensureMutableMap(), elementId);
|
|
893
|
+
changedElementIds.add(elementId);
|
|
894
|
+
childrenByParent.delete(elementId);
|
|
895
|
+
});
|
|
896
|
+
const root = subtree.removed.find(({ elementId }) => elementId === subtree.elementId);
|
|
897
|
+
if (root) {
|
|
898
|
+
childrenByParent.set(root.parentId, [
|
|
899
|
+
...subtree.rootParentChildrenAfter
|
|
900
|
+
]);
|
|
901
|
+
dirtyParentIds.add(root.parentId);
|
|
902
|
+
}
|
|
903
|
+
break;
|
|
904
|
+
}
|
|
905
|
+
subtree.removed.forEach(({ data, elementId }) => {
|
|
906
|
+
const elementData = cloneProjectedElementData(data);
|
|
907
|
+
if (!elementData)
|
|
908
|
+
return;
|
|
909
|
+
ensureMutableMap()[elementId] = elementData;
|
|
910
|
+
changedElementIds.add(elementId);
|
|
911
|
+
if (Array.isArray(elementData.children)) {
|
|
912
|
+
childrenByParent.set(elementId, [...elementData.children]);
|
|
913
|
+
dirtyParentIds.add(elementId);
|
|
914
|
+
}
|
|
915
|
+
});
|
|
916
|
+
subtree.removed.forEach(({ elementId, parentId, index }) => {
|
|
917
|
+
insertMembership(parentId, elementId, index);
|
|
918
|
+
const elementData = next[elementId];
|
|
919
|
+
if (elementData) {
|
|
920
|
+
ensureMutableMap()[elementId] = {
|
|
921
|
+
...elementData,
|
|
922
|
+
parentId
|
|
923
|
+
};
|
|
924
|
+
changedElementIds.add(elementId);
|
|
925
|
+
}
|
|
926
|
+
});
|
|
927
|
+
break;
|
|
928
|
+
}
|
|
929
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_DATA: {
|
|
930
|
+
const { id, changes: updates } = change;
|
|
931
|
+
updates.forEach(({ key, after }) => {
|
|
932
|
+
setProjectedValue(id, key, after);
|
|
933
|
+
});
|
|
934
|
+
break;
|
|
935
|
+
}
|
|
936
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA: {
|
|
937
|
+
const { id, key, after } = change;
|
|
938
|
+
setProjectedValue(id, key, after);
|
|
939
|
+
break;
|
|
940
|
+
}
|
|
941
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_BATCH: {
|
|
942
|
+
const { id, changes: updates } = change;
|
|
943
|
+
updates.forEach(({ key, after }) => {
|
|
944
|
+
setProjectedValue(id, key, after);
|
|
945
|
+
});
|
|
946
|
+
break;
|
|
947
|
+
}
|
|
948
|
+
case SCENE_TREE_ACTIONS.UPDATE_ELEMENT_COMPUTED_DATA_PATCH: {
|
|
949
|
+
const { id, patch } = change;
|
|
950
|
+
Object.entries(patch.values ?? {}).forEach(([key, value]) => {
|
|
951
|
+
setProjectedValue(id, key, value.after);
|
|
952
|
+
});
|
|
953
|
+
break;
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
});
|
|
957
|
+
flushScalarAdditionMemberships();
|
|
958
|
+
dirtyParentIds.forEach((parentId) => {
|
|
959
|
+
const parent = next[parentId];
|
|
960
|
+
if (!parent)
|
|
961
|
+
return;
|
|
962
|
+
ensureMutableMap()[parentId] = {
|
|
963
|
+
...parent,
|
|
964
|
+
children: [...(childrenByParent.get(parentId) ?? [])]
|
|
965
|
+
};
|
|
966
|
+
changedElementIds.add(parentId);
|
|
967
|
+
});
|
|
968
|
+
let nextFlattenedIds = currentFlattenedIds;
|
|
969
|
+
if (updatesHierarchy) {
|
|
970
|
+
const currentFlattenedIdSet = new Set(currentFlattenedIds);
|
|
971
|
+
const orderedCandidates = [
|
|
972
|
+
...currentFlattenedIds,
|
|
973
|
+
...[...changedElementIds].filter((elementId) => !currentFlattenedIdSet.has(elementId))
|
|
974
|
+
];
|
|
975
|
+
const rootParentIds = [];
|
|
976
|
+
const rootParentIdSet = new Set();
|
|
977
|
+
orderedCandidates.forEach((elementId) => {
|
|
978
|
+
const parentId = getProjectedParentId(next[elementId]);
|
|
979
|
+
if (parentId && !next[parentId] && !rootParentIdSet.has(parentId)) {
|
|
980
|
+
rootParentIdSet.add(parentId);
|
|
981
|
+
rootParentIds.push(parentId);
|
|
982
|
+
}
|
|
983
|
+
});
|
|
984
|
+
const flattenedElementIds = [];
|
|
985
|
+
const visited = new Set();
|
|
986
|
+
const collectProjectedChildren = (elementId) => {
|
|
987
|
+
if (visited.has(elementId) || !next[elementId])
|
|
988
|
+
return;
|
|
989
|
+
visited.add(elementId);
|
|
990
|
+
flattenedElementIds.push(elementId);
|
|
991
|
+
(childrenByParent.get(elementId) ?? []).forEach(collectProjectedChildren);
|
|
992
|
+
};
|
|
993
|
+
rootParentIds.forEach((parentId) => {
|
|
994
|
+
;
|
|
995
|
+
(childrenByParent.get(parentId) ?? []).forEach(collectProjectedChildren);
|
|
996
|
+
});
|
|
997
|
+
emitDiagnosticCounter('ui-context-sync-flattened-ids');
|
|
998
|
+
nextFlattenedIds = flattenedElementIds;
|
|
999
|
+
}
|
|
1000
|
+
if (mapChanged) {
|
|
1001
|
+
emitDiagnosticCounter('ui-context-sync-element-data-map-entry', changedElementIds.size);
|
|
1002
|
+
const subject = uiContext.getSubject('elementDataMap');
|
|
1003
|
+
const projected = mutation.commit(nextFlattenedIds);
|
|
1004
|
+
if (subject) {
|
|
1005
|
+
subject.next(projected);
|
|
1006
|
+
}
|
|
1007
|
+
else {
|
|
1008
|
+
uiContext.set('elementDataMap', projected);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
if (updatesHierarchy) {
|
|
1012
|
+
uiContext.set('flattenedElementIds', nextFlattenedIds);
|
|
1013
|
+
}
|
|
1014
|
+
});
|
|
1015
|
+
};
|
|
1016
|
+
const createPendingUIContextSync = () => ({
|
|
1017
|
+
elementSelectionAndDerived: false,
|
|
1018
|
+
dirtyPropertyKeys: new Set(),
|
|
1019
|
+
removedElementIds: new Set(),
|
|
1020
|
+
sceneTreeChanges: []
|
|
1021
|
+
});
|
|
1022
|
+
const createUIContextSyncLifetime = () => ({
|
|
1023
|
+
disposed: false,
|
|
1024
|
+
pending: createPendingUIContextSync()
|
|
1025
|
+
});
|
|
1026
|
+
const resetPendingUIContextSync = (lifetime) => {
|
|
1027
|
+
lifetime.pending = createPendingUIContextSync();
|
|
1028
|
+
};
|
|
1029
|
+
const hasPendingUIContextSync = (lifetime) => lifetime.pending.elementSelectionAndDerived ||
|
|
1030
|
+
lifetime.pending.dirtyPropertyKeys.size > 0 ||
|
|
1031
|
+
lifetime.pending.removedElementIds.size > 0 ||
|
|
1032
|
+
lifetime.pending.sceneTreeChanges.length > 0;
|
|
1033
|
+
const flushPendingUIContextSync = (lifetime, core, deps) => {
|
|
1034
|
+
if (!hasPendingUIContextSync(lifetime)) {
|
|
1035
|
+
emitDiagnosticCounter('ui-context-transaction-flush-skip');
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
const pending = lifetime.pending;
|
|
1039
|
+
resetPendingUIContextSync(lifetime);
|
|
1040
|
+
measureBrowserDragPhase('ui-context:flush', () => {
|
|
1041
|
+
emitDiagnosticCounter('ui-context-transaction-flush');
|
|
1042
|
+
projectUIContextSceneTreeBatch(pending.sceneTreeChanges);
|
|
1043
|
+
if (pending.removedElementIds.size > 0 &&
|
|
1044
|
+
syncSelectionOnElementRemovals(core, [...pending.removedElementIds])) {
|
|
1045
|
+
pending.elementSelectionAndDerived = true;
|
|
1046
|
+
}
|
|
1047
|
+
if (pending.elementSelectionAndDerived) {
|
|
1048
|
+
emitDiagnosticCounter('ui-context-sync-element-selection-derived');
|
|
1049
|
+
syncElementSelectionAndDerived(core, deps);
|
|
1050
|
+
}
|
|
1051
|
+
if (pending.dirtyPropertyKeys.size > 0) {
|
|
1052
|
+
emitDiagnosticCounter('ui-context-recompute-property-key-count', pending.dirtyPropertyKeys.size);
|
|
1053
|
+
const selectedIds = getSelectedIds(core, SelectionChannels.ELEMENT);
|
|
1054
|
+
uiContext.recomputeProperties([...pending.dirtyPropertyKeys], buildSelectionContext(deps, selectedIds));
|
|
1055
|
+
}
|
|
1056
|
+
});
|
|
1057
|
+
};
|
|
1058
|
+
// Selection channel updates only affect selection-derived UI properties.
|
|
1059
|
+
const updateUIContextElementSelection = (change, core, deps) => {
|
|
1060
|
+
switch (change.action) {
|
|
1061
|
+
case SelectionActions.SELECT_ELEMENTS:
|
|
1062
|
+
case SelectionActions.DESELECT_ELEMENTS:
|
|
1063
|
+
syncElementSelectionAndDerived(core, deps);
|
|
1064
|
+
break;
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
const updateVectorEditingSelection = (change, core) => {
|
|
1068
|
+
switch (change.action) {
|
|
1069
|
+
case SelectionActions.SELECT_VECTOR_POINTS:
|
|
1070
|
+
case SelectionActions.DESELECT_VECTOR_POINTS:
|
|
1071
|
+
uiContext.set('vectorPointSelection', getSelectedIds(core, SelectionChannels.VECTOR_POINT));
|
|
1072
|
+
break;
|
|
1073
|
+
case SelectionActions.SELECT_VECTOR_SEGMENTS:
|
|
1074
|
+
case SelectionActions.DESELECT_VECTOR_SEGMENTS:
|
|
1075
|
+
uiContext.set('vectorSegmentSelection', getSelectedIds(core, SelectionChannels.VECTOR_SEGMENT));
|
|
1076
|
+
break;
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
1079
|
+
// Scene-tree channel updates affect list/map mirrors, and may trigger aggregate recompute.
|
|
1080
|
+
const handleUIContextSceneTreeChange = (change, core, _deps, lifetime) => {
|
|
1081
|
+
lifetime.pending.sceneTreeChanges.push(change);
|
|
1082
|
+
const updatedPropertyKeys = getMatchingPropertiesForSceneTreeChange(change);
|
|
1083
|
+
updatedPropertyKeys.forEach((key) => {
|
|
1084
|
+
lifetime.pending.dirtyPropertyKeys.add(key);
|
|
1085
|
+
});
|
|
1086
|
+
let removedIds = [];
|
|
1087
|
+
if (change.action === SCENE_TREE_ACTIONS.REMOVE_ELEMENT) {
|
|
1088
|
+
removedIds = [change.data.id];
|
|
1089
|
+
}
|
|
1090
|
+
else if (change.action === SCENE_TREE_ACTIONS.REMOVE_ELEMENTS) {
|
|
1091
|
+
removedIds = change.entries.map(({ data }) => data.id);
|
|
1092
|
+
}
|
|
1093
|
+
removedIds.forEach((removedId) => {
|
|
1094
|
+
if (typeof removedId === 'string' && removedId.length > 0) {
|
|
1095
|
+
lifetime.pending.removedElementIds.add(removedId);
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
};
|
|
1099
|
+
const applySelectionChangeToRuntime = (core, change) => {
|
|
1100
|
+
const selection = core.getSelection(change.selectionType);
|
|
1101
|
+
if (!selection) {
|
|
1102
|
+
return;
|
|
1103
|
+
}
|
|
1104
|
+
selection.select(change.after, change.options);
|
|
1105
|
+
selection.cleanChanges();
|
|
1106
|
+
};
|
|
1107
|
+
const applySelectionIdsToRuntime = (core, selectionType, after, options) => {
|
|
1108
|
+
const selection = core.getSelection(selectionType);
|
|
1109
|
+
if (!selection) {
|
|
1110
|
+
return;
|
|
1111
|
+
}
|
|
1112
|
+
selection.select(after, options);
|
|
1113
|
+
selection.cleanChanges();
|
|
1114
|
+
};
|
|
1115
|
+
const createSelectionChangeFromDirectEvent = (selectionType, action, eventName, payload, options) => {
|
|
1116
|
+
const raw = (payload ?? {});
|
|
1117
|
+
return {
|
|
1118
|
+
selectionType: raw.selectionType ?? selectionType,
|
|
1119
|
+
action: raw.action ?? action,
|
|
1120
|
+
eventName: raw.eventName ?? eventName,
|
|
1121
|
+
before: Array.isArray(raw.before) ? raw.before : [],
|
|
1122
|
+
after: Array.isArray(raw.after) ? raw.after : [],
|
|
1123
|
+
options: raw.options ?? options
|
|
1124
|
+
};
|
|
1125
|
+
};
|
|
1126
|
+
// Register preset default shared-channel observers for one preset lifetime.
|
|
1127
|
+
export const registerDefaultDataChannelObservers = (core, deps, onCleanupReady, options = {
|
|
1128
|
+
propertyProjection: true,
|
|
1129
|
+
renderScene: true,
|
|
1130
|
+
selection: true,
|
|
1131
|
+
vectorEditing: true,
|
|
1132
|
+
uiContext: true
|
|
1133
|
+
}) => {
|
|
1134
|
+
const propertyProjectionEnabled = options.propertyProjection === true;
|
|
1135
|
+
const renderSceneEnabled = options.renderScene === true;
|
|
1136
|
+
const selectionEnabled = options.selection === true;
|
|
1137
|
+
const vectorEditingEnabled = options.vectorEditing === true;
|
|
1138
|
+
const uiContextEnabled = options.uiContext === true;
|
|
1139
|
+
const eventSubscriptions = [];
|
|
1140
|
+
const registeredObserverNames = [];
|
|
1141
|
+
const uiContextSyncLifetime = createUIContextSyncLifetime();
|
|
1142
|
+
let disposed = false;
|
|
1143
|
+
const dispose = () => {
|
|
1144
|
+
if (disposed)
|
|
1145
|
+
return;
|
|
1146
|
+
disposed = true;
|
|
1147
|
+
const failures = [];
|
|
1148
|
+
for (let index = registeredObserverNames.length - 1; index >= 0; index--) {
|
|
1149
|
+
const name = registeredObserverNames[index];
|
|
1150
|
+
try {
|
|
1151
|
+
core.unregisterDataChannelObserver(name);
|
|
1152
|
+
registeredObserverNames.splice(index, 1);
|
|
1153
|
+
}
|
|
1154
|
+
catch (error) {
|
|
1155
|
+
failures.push(error);
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
for (let index = eventSubscriptions.length - 1; index >= 0; index--) {
|
|
1159
|
+
try {
|
|
1160
|
+
eventSubscriptions[index].unsubscribe();
|
|
1161
|
+
eventSubscriptions.splice(index, 1);
|
|
1162
|
+
}
|
|
1163
|
+
catch (error) {
|
|
1164
|
+
failures.push(error);
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
if (renderSceneEnabled) {
|
|
1168
|
+
try {
|
|
1169
|
+
renderSceneTreeStore.clearProjection();
|
|
1170
|
+
}
|
|
1171
|
+
catch (error) {
|
|
1172
|
+
failures.push(error);
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
uiContextSyncLifetime.disposed = true;
|
|
1176
|
+
resetPendingUIContextSync(uiContextSyncLifetime);
|
|
1177
|
+
if (failures.length > 0) {
|
|
1178
|
+
throw failures[0];
|
|
1179
|
+
}
|
|
1180
|
+
};
|
|
1181
|
+
const cleanupReporter = createCleanupReporter(onCleanupReady, dispose);
|
|
1182
|
+
const registerObserver = (registration) => {
|
|
1183
|
+
core.registerDataChannelObserver(registration);
|
|
1184
|
+
registeredObserverNames.push(registration.name);
|
|
1185
|
+
cleanupReporter.report();
|
|
1186
|
+
};
|
|
1187
|
+
const canonicalPropertyDataChannelObserver = defineDataChannelObserver({
|
|
1188
|
+
name: 'preset.sceneTree.props',
|
|
1189
|
+
channel: SharedDataChannelNames.PROPS,
|
|
1190
|
+
onBatch: (changes) => {
|
|
1191
|
+
if (disposed) {
|
|
1192
|
+
return;
|
|
1193
|
+
}
|
|
1194
|
+
const propertyIds = [];
|
|
1195
|
+
const seenPropertyIds = new Set();
|
|
1196
|
+
const appendPropertyId = (propertyId) => {
|
|
1197
|
+
if (seenPropertyIds.has(propertyId)) {
|
|
1198
|
+
return;
|
|
1199
|
+
}
|
|
1200
|
+
seenPropertyIds.add(propertyId);
|
|
1201
|
+
propertyIds.push(propertyId);
|
|
1202
|
+
};
|
|
1203
|
+
changes.forEach((change) => {
|
|
1204
|
+
if ('id' in change) {
|
|
1205
|
+
appendPropertyId(change.id);
|
|
1206
|
+
return;
|
|
1207
|
+
}
|
|
1208
|
+
change.data.forEach(({ id }) => appendPropertyId(id));
|
|
1209
|
+
});
|
|
1210
|
+
if (propertyIds.length > 0) {
|
|
1211
|
+
deps.sceneTree.projectLocalComputedDataFromPropertyIds(propertyIds);
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
});
|
|
1215
|
+
const uiContextSceneTreeDataChannelObserver = defineDataChannelObserver({
|
|
1216
|
+
name: 'preset.uiContext.sceneTree',
|
|
1217
|
+
channel: SharedDataChannelNames.SCENE_TREE,
|
|
1218
|
+
onBatch: (changes) => {
|
|
1219
|
+
if (uiContextSyncLifetime.disposed) {
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
changes
|
|
1223
|
+
.filter((change) => !isLocalComputedSceneTreeChange(change))
|
|
1224
|
+
.forEach((change) => {
|
|
1225
|
+
handleUIContextSceneTreeChange(change, core, deps, uiContextSyncLifetime);
|
|
1226
|
+
});
|
|
1227
|
+
flushPendingUIContextSync(uiContextSyncLifetime, core, deps);
|
|
1228
|
+
}
|
|
1229
|
+
});
|
|
1230
|
+
const renderSceneTreeDataChannelObserver = defineDataChannelObserver({
|
|
1231
|
+
name: 'preset.render.sceneTree',
|
|
1232
|
+
channel: SharedDataChannelNames.SCENE_TREE,
|
|
1233
|
+
onBatch: (changes) => {
|
|
1234
|
+
if (!disposed) {
|
|
1235
|
+
updateRenderSceneTreeBatch(changes.filter((change) => !isLocalComputedSceneTreeChange(change)));
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
});
|
|
1239
|
+
const uiContextSelectionDataChannelObserver = defineDataChannelObserver({
|
|
1240
|
+
name: 'preset.uiContext.selection',
|
|
1241
|
+
channel: SharedDataChannelNames.SELECTION,
|
|
1242
|
+
onChange: (change) => updateUIContextElementSelection(change, core, deps)
|
|
1243
|
+
});
|
|
1244
|
+
const vectorEditingSelectionDataChannelObserver = defineDataChannelObserver({
|
|
1245
|
+
name: 'preset.vectorEditing.selection',
|
|
1246
|
+
channel: SharedDataChannelNames.SELECTION,
|
|
1247
|
+
onChange: (change) => updateVectorEditingSelection(change, core)
|
|
1248
|
+
});
|
|
1249
|
+
const selectionRuntimeDataChannelObserver = defineDataChannelObserver({
|
|
1250
|
+
name: 'preset.selection.runtime',
|
|
1251
|
+
channel: SharedDataChannelNames.SELECTION,
|
|
1252
|
+
onChange: (change) => applySelectionChangeToRuntime(core, change)
|
|
1253
|
+
});
|
|
1254
|
+
try {
|
|
1255
|
+
if (propertyProjectionEnabled) {
|
|
1256
|
+
registerObserver(canonicalPropertyDataChannelObserver);
|
|
1257
|
+
}
|
|
1258
|
+
if (renderSceneEnabled || uiContextEnabled) {
|
|
1259
|
+
eventSubscriptions.push(subscribeToEventBatches((events) => {
|
|
1260
|
+
if (disposed) {
|
|
1261
|
+
return;
|
|
1262
|
+
}
|
|
1263
|
+
const localComputedChanges = events.flatMap((event) => {
|
|
1264
|
+
const computedEvent = toLocalComputedProjectionEvent(event);
|
|
1265
|
+
if (!computedEvent) {
|
|
1266
|
+
return [];
|
|
1267
|
+
}
|
|
1268
|
+
const change = toLocalComputedSceneTreeChange(computedEvent);
|
|
1269
|
+
return change ? [change] : [];
|
|
1270
|
+
});
|
|
1271
|
+
if (renderSceneEnabled) {
|
|
1272
|
+
updateRenderSceneTreeBatch(localComputedChanges);
|
|
1273
|
+
}
|
|
1274
|
+
if (uiContextEnabled && localComputedChanges.length > 0) {
|
|
1275
|
+
localComputedChanges.forEach((change) => {
|
|
1276
|
+
handleUIContextSceneTreeChange(change, core, deps, uiContextSyncLifetime);
|
|
1277
|
+
});
|
|
1278
|
+
flushPendingUIContextSync(uiContextSyncLifetime, core, deps);
|
|
1279
|
+
}
|
|
1280
|
+
}));
|
|
1281
|
+
cleanupReporter.report();
|
|
1282
|
+
}
|
|
1283
|
+
if (renderSceneEnabled) {
|
|
1284
|
+
eventSubscriptions.push(subscribeToSynchronousEvent(EventTypes.FILE_LOAD_COMPLETE, () => {
|
|
1285
|
+
renderSceneTreeStore.reload();
|
|
1286
|
+
}));
|
|
1287
|
+
cleanupReporter.report();
|
|
1288
|
+
}
|
|
1289
|
+
if (uiContextEnabled || vectorEditingEnabled) {
|
|
1290
|
+
eventSubscriptions.push(subscribeToFileLoadComplete(() => {
|
|
1291
|
+
if (uiContextEnabled) {
|
|
1292
|
+
resetPendingUIContextSync(uiContextSyncLifetime);
|
|
1293
|
+
syncFlattenedElementIds(deps);
|
|
1294
|
+
syncElementDataMap(deps);
|
|
1295
|
+
syncElementSelectionAndDerived(core, deps);
|
|
1296
|
+
}
|
|
1297
|
+
if (vectorEditingEnabled) {
|
|
1298
|
+
syncVectorSelections(core);
|
|
1299
|
+
}
|
|
1300
|
+
}));
|
|
1301
|
+
cleanupReporter.report();
|
|
1302
|
+
}
|
|
1303
|
+
// Undo/redo publishes selection events directly from transaction history.
|
|
1304
|
+
// Apply those payloads to runtime so selection state is restored correctly.
|
|
1305
|
+
if (selectionEnabled || uiContextEnabled) {
|
|
1306
|
+
eventSubscriptions.push(subscribeToSynchronousEvent(EventTypes.SELECT_ELEMENTS, (event) => {
|
|
1307
|
+
const change = createSelectionChangeFromDirectEvent(SelectionChannels.ELEMENT, SelectionActions.SELECT_ELEMENTS, SelectionEventNames.SELECT_ELEMENTS, event.payload, event.options);
|
|
1308
|
+
if (selectionEnabled) {
|
|
1309
|
+
applySelectionIdsToRuntime(core, SelectionChannels.ELEMENT, change.after, change.options);
|
|
1310
|
+
// Replay events bypass shared channel observers. Mirror them to render.
|
|
1311
|
+
updateRenderSelection(change);
|
|
1312
|
+
}
|
|
1313
|
+
if (uiContextEnabled) {
|
|
1314
|
+
updateUIContextElementSelection(change, core, deps);
|
|
1315
|
+
}
|
|
1316
|
+
}));
|
|
1317
|
+
cleanupReporter.report();
|
|
1318
|
+
}
|
|
1319
|
+
if (selectionEnabled || vectorEditingEnabled) {
|
|
1320
|
+
eventSubscriptions.push(subscribeToSynchronousEvent(EventTypes.SELECT_VECTOR_POINTS, (event) => {
|
|
1321
|
+
const change = createSelectionChangeFromDirectEvent(SelectionChannels.VECTOR_POINT, SelectionActions.SELECT_VECTOR_POINTS, SelectionEventNames.SELECT_VECTOR_POINTS, event.payload, event.options);
|
|
1322
|
+
if (selectionEnabled) {
|
|
1323
|
+
applySelectionIdsToRuntime(core, SelectionChannels.VECTOR_POINT, change.after, change.options);
|
|
1324
|
+
updateRenderSelection(change);
|
|
1325
|
+
}
|
|
1326
|
+
if (vectorEditingEnabled) {
|
|
1327
|
+
updateVectorEditingSelection(change, core);
|
|
1328
|
+
}
|
|
1329
|
+
}));
|
|
1330
|
+
cleanupReporter.report();
|
|
1331
|
+
eventSubscriptions.push(subscribeToSynchronousEvent(EventTypes.SELECT_VECTOR_SEGMENTS, (event) => {
|
|
1332
|
+
const change = createSelectionChangeFromDirectEvent(SelectionChannels.VECTOR_SEGMENT, SelectionActions.SELECT_VECTOR_SEGMENTS, SelectionEventNames.SELECT_VECTOR_SEGMENTS, event.payload, event.options);
|
|
1333
|
+
if (selectionEnabled) {
|
|
1334
|
+
applySelectionIdsToRuntime(core, SelectionChannels.VECTOR_SEGMENT, change.after, change.options);
|
|
1335
|
+
updateRenderSelection(change);
|
|
1336
|
+
}
|
|
1337
|
+
if (vectorEditingEnabled) {
|
|
1338
|
+
updateVectorEditingSelection(change, core);
|
|
1339
|
+
}
|
|
1340
|
+
}));
|
|
1341
|
+
cleanupReporter.report();
|
|
1342
|
+
}
|
|
1343
|
+
if (renderSceneEnabled) {
|
|
1344
|
+
registerObserver(renderSceneTreeDataChannelObserver);
|
|
1345
|
+
renderSceneTreeStore.reload();
|
|
1346
|
+
}
|
|
1347
|
+
if (selectionEnabled) {
|
|
1348
|
+
registerObserver(selectionRuntimeDataChannelObserver);
|
|
1349
|
+
registerObserver(renderSelectionDataChannelObserver);
|
|
1350
|
+
}
|
|
1351
|
+
if (uiContextEnabled) {
|
|
1352
|
+
registerObserver(uiContextSceneTreeDataChannelObserver);
|
|
1353
|
+
registerObserver(uiContextSelectionDataChannelObserver);
|
|
1354
|
+
}
|
|
1355
|
+
if (vectorEditingEnabled) {
|
|
1356
|
+
registerObserver(vectorEditingSelectionDataChannelObserver);
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
catch (error) {
|
|
1360
|
+
if (!cleanupReporter.hasReported())
|
|
1361
|
+
dispose();
|
|
1362
|
+
throw error;
|
|
1363
|
+
}
|
|
1364
|
+
return dispose;
|
|
1365
|
+
};
|
|
1366
|
+
//# sourceMappingURL=data-channel.js.map
|