@yuzhouu/canvas-kit 0.1.2
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/README.md +145 -0
- package/_vendor/canvas-core/bindings/BindingUtil.ts +92 -0
- package/_vendor/canvas-core/bindings/BindingUtilRegistry.ts +22 -0
- package/_vendor/canvas-core/bindings/arrow/arrowBindingHelpers.ts +52 -0
- package/_vendor/canvas-core/contextMenu.ts +85 -0
- package/_vendor/canvas-core/editor/Editor.ts +2614 -0
- package/_vendor/canvas-core/editor/HistoryManager.ts +83 -0
- package/_vendor/canvas-core/editor/StateNode.ts +162 -0
- package/_vendor/canvas-core/editor/camera.ts +6 -0
- package/_vendor/canvas-core/editor/clipboard.ts +478 -0
- package/_vendor/canvas-core/editor/commands.ts +833 -0
- package/_vendor/canvas-core/editor/edgeScroll.ts +37 -0
- package/_vendor/canvas-core/editor/editorComputedHelpers.ts +257 -0
- package/_vendor/canvas-core/editor/editorSelectionTransforms.ts +116 -0
- package/_vendor/canvas-core/editor/events.ts +93 -0
- package/_vendor/canvas-core/editor/index.ts +12 -0
- package/_vendor/canvas-core/editor/interactions.ts +101 -0
- package/_vendor/canvas-core/editor/internal.ts +22 -0
- package/_vendor/canvas-core/editor/managers/BindingManager.ts +262 -0
- package/_vendor/canvas-core/editor/managers/CameraManager.ts +147 -0
- package/_vendor/canvas-core/editor/managers/ClipboardManager.ts +32 -0
- package/_vendor/canvas-core/editor/managers/DocumentSnapshotManager.ts +256 -0
- package/_vendor/canvas-core/editor/managers/InputsManager.ts +319 -0
- package/_vendor/canvas-core/editor/managers/InteractionTransientManager.ts +218 -0
- package/_vendor/canvas-core/editor/managers/QueryManager.ts +837 -0
- package/_vendor/canvas-core/editor/managers/RenderWindowManager.ts +347 -0
- package/_vendor/canvas-core/editor/managers/SelectionLayoutManager.ts +196 -0
- package/_vendor/canvas-core/editor/managers/SelectionManager.ts +98 -0
- package/_vendor/canvas-core/editor/managers/SelectionOverlayManager.ts +308 -0
- package/_vendor/canvas-core/editor/managers/ShapeEditingManager.ts +226 -0
- package/_vendor/canvas-core/editor/managers/ShapeGeometryManager.ts +162 -0
- package/_vendor/canvas-core/editor/managers/ShapeHierarchyManager.ts +797 -0
- package/_vendor/canvas-core/editor/managers/SideEffectManager.ts +83 -0
- package/_vendor/canvas-core/editor/managers/SnapManager.ts +433 -0
- package/_vendor/canvas-core/editor/managers/SnapSnapshotBuilder.ts +430 -0
- package/_vendor/canvas-core/editor/managers/SpatialIndexManager.ts +354 -0
- package/_vendor/canvas-core/editor/managers/ToolManager.ts +81 -0
- package/_vendor/canvas-core/editor/managers/queryState.ts +583 -0
- package/_vendor/canvas-core/editor/managers/selectionGeometry.ts +287 -0
- package/_vendor/canvas-core/editor/managers/shapeOrder.ts +350 -0
- package/_vendor/canvas-core/editor/managers/shapeReparenting.ts +160 -0
- package/_vendor/canvas-core/editor/managers/snapCalculations.ts +412 -0
- package/_vendor/canvas-core/editor/managers/snapGeometry.ts +202 -0
- package/_vendor/canvas-core/editor/managers/snapIndicators.ts +403 -0
- package/_vendor/canvas-core/editor/managers/snapTypes.ts +122 -0
- package/_vendor/canvas-core/editor/queries/shapeTransforms.ts +171 -0
- package/_vendor/canvas-core/editor/renderWindow.ts +439 -0
- package/_vendor/canvas-core/editor/resizeGeometry.ts +654 -0
- package/_vendor/canvas-core/editor/selection.ts +489 -0
- package/_vendor/canvas-core/editor/selectionHandleInteractions.ts +43 -0
- package/_vendor/canvas-core/editor/selectionOverlay.ts +567 -0
- package/_vendor/canvas-core/exports/integrity.ts +174 -0
- package/_vendor/canvas-core/exports/svg.ts +494 -0
- package/_vendor/canvas-core/geometry/Box.ts +80 -0
- package/_vendor/canvas-core/geometry/Ellipse2d.ts +67 -0
- package/_vendor/canvas-core/geometry/Geometry2d.ts +52 -0
- package/_vendor/canvas-core/geometry/Mat.ts +92 -0
- package/_vendor/canvas-core/geometry/Polyline2d.ts +63 -0
- package/_vendor/canvas-core/geometry/Rectangle2d.ts +64 -0
- package/_vendor/canvas-core/geometry/Vec.ts +123 -0
- package/_vendor/canvas-core/geometry/index.ts +13 -0
- package/_vendor/canvas-core/geometry/segments.ts +65 -0
- package/_vendor/canvas-core/index.ts +391 -0
- package/_vendor/canvas-core/plugins/index.ts +5 -0
- package/_vendor/canvas-core/plugins/types.ts +53 -0
- package/_vendor/canvas-core/reactivity.ts +16 -0
- package/_vendor/canvas-core/schema/records.ts +221 -0
- package/_vendor/canvas-core/schema/snapshots.ts +527 -0
- package/_vendor/canvas-core/shapes/ShapeUtil.ts +516 -0
- package/_vendor/canvas-core/shapes/ShapeUtilRegistry.ts +26 -0
- package/_vendor/canvas-core/shapes/capabilities.ts +67 -0
- package/_vendor/canvas-core/shapes/types.ts +20 -0
- package/_vendor/canvas-core/toolbar.ts +148 -0
- package/_vendor/canvas-core/tools/hand/HandTool.ts +75 -0
- package/_vendor/canvas-core/tools/index.ts +2 -0
- package/_vendor/canvas-core/tools/select/SelectTool.ts +33 -0
- package/_vendor/canvas-core/tools/select/selectContainment.ts +104 -0
- package/_vendor/canvas-core/tools/select/selectToolHelpers.ts +801 -0
- package/_vendor/canvas-core/tools/select/states/BrushingState.ts +145 -0
- package/_vendor/canvas-core/tools/select/states/DraggingSelectionHandleInteractionState.ts +116 -0
- package/_vendor/canvas-core/tools/select/states/IdleState.ts +159 -0
- package/_vendor/canvas-core/tools/select/states/PointingCanvasState.ts +41 -0
- package/_vendor/canvas-core/tools/select/states/PointingResizeHandleState.ts +78 -0
- package/_vendor/canvas-core/tools/select/states/PointingRotateHandleState.ts +64 -0
- package/_vendor/canvas-core/tools/select/states/PointingSelectionHandleInteractionState.ts +34 -0
- package/_vendor/canvas-core/tools/select/states/PointingShapeState.ts +140 -0
- package/_vendor/canvas-core/tools/select/states/ResizingState.ts +171 -0
- package/_vendor/canvas-core/tools/select/states/RotatingState.ts +124 -0
- package/_vendor/canvas-core/tools/select/states/SelectStateNode.ts +128 -0
- package/_vendor/canvas-core/tools/select/states/TranslatingState.ts +139 -0
- package/_vendor/canvas-core/tools/select/states/index.ts +11 -0
- package/_vendor/canvas-plugin-api/feature.ts +204 -0
- package/_vendor/canvas-plugin-api/index.ts +142 -0
- package/_vendor/canvas-product-react/api/apiRegistry.ts +35 -0
- package/_vendor/canvas-product-react/api/canvasProductApi.ts +422 -0
- package/_vendor/canvas-product-react/api/index.ts +31 -0
- package/_vendor/canvas-product-react/capabilities/clipboard/clipboardService.ts +36 -0
- package/_vendor/canvas-product-react/capabilities/clipboard/systemClipboard.ts +245 -0
- package/_vendor/canvas-product-react/capabilities/context-menu/systemContextMenu.ts +396 -0
- package/_vendor/canvas-product-react/capabilities/context-menu/useContextMenuSurface.ts +59 -0
- package/_vendor/canvas-product-react/capabilities/export/useSvgExport.ts +156 -0
- package/_vendor/canvas-product-react/capabilities/history/useHistoryControls.ts +23 -0
- package/_vendor/canvas-product-react/capabilities/pages/usePageControls.ts +41 -0
- package/_vendor/canvas-product-react/capabilities/toolbar/systemToolbar.ts +490 -0
- package/_vendor/canvas-product-react/capabilities/toolbar/useToolbarSurfaces.ts +92 -0
- package/_vendor/canvas-product-react/capabilities/zoom/useZoomControls.ts +27 -0
- package/_vendor/canvas-product-react/index.ts +84 -0
- package/_vendor/canvas-product-react/persistence/index.ts +18 -0
- package/_vendor/canvas-product-react/persistence/localSnapshotPersistence.ts +294 -0
- package/_vendor/canvas-product-react/runtime/CanvasProductRoot.tsx +250 -0
- package/_vendor/canvas-product-react/runtime/productDefinition.ts +72 -0
- package/_vendor/canvas-product-react/runtime/productServices.ts +96 -0
- package/_vendor/canvas-react/components/CanvasView.tsx +796 -0
- package/_vendor/canvas-react/components/OverlayLayer.tsx +879 -0
- package/_vendor/canvas-react/components/PageSpace.tsx +34 -0
- package/_vendor/canvas-react/components/ShapeLayer.tsx +428 -0
- package/_vendor/canvas-react/components/index.ts +4 -0
- package/_vendor/canvas-react/hooks/useEditor.ts +11 -0
- package/_vendor/canvas-react/hooks/useValue.ts +14 -0
- package/_vendor/canvas-react/host/CanvasRendererRegistryContext.tsx +14 -0
- package/_vendor/canvas-react/host/contextMenuRegistry.ts +61 -0
- package/_vendor/canvas-react/host/domEventGuards.ts +61 -0
- package/_vendor/canvas-react/host/index.ts +38 -0
- package/_vendor/canvas-react/host/pluginHost.ts +635 -0
- package/_vendor/canvas-react/host/rendererRegistry.tsx +58 -0
- package/_vendor/canvas-react/host/toolbarRegistry.ts +100 -0
- package/_vendor/canvas-react/index.ts +3 -0
- package/_vendor/plugin-arrow/ArrowBindingUtil.ts +239 -0
- package/_vendor/plugin-arrow/ArrowShapeUtil.ts +311 -0
- package/_vendor/plugin-arrow/ArrowTool.ts +208 -0
- package/_vendor/plugin-arrow/arrowGeometry.ts +441 -0
- package/_vendor/plugin-arrow/arrowTerminalBindingResolver.ts +253 -0
- package/_vendor/plugin-arrow/arrowTerminalCommands.ts +230 -0
- package/_vendor/plugin-arrow/canvas.tsx +92 -0
- package/_vendor/plugin-arrow/core.ts +409 -0
- package/_vendor/plugin-arrow/feature.ts +20 -0
- package/_vendor/plugin-arrow/index.ts +28 -0
- package/_vendor/plugin-arrow/selectionHandleInteractions.ts +154 -0
- package/_vendor/plugin-arrow/types.ts +63 -0
- package/_vendor/plugin-draw/DrawShapeRenderer.tsx +60 -0
- package/_vendor/plugin-draw/DrawShapeUtil.ts +372 -0
- package/_vendor/plugin-draw/DrawTool.ts +444 -0
- package/_vendor/plugin-draw/canvas.tsx +24 -0
- package/_vendor/plugin-draw/core.ts +515 -0
- package/_vendor/plugin-draw/drawGeometry.ts +491 -0
- package/_vendor/plugin-draw/drawHitTest.ts +271 -0
- package/_vendor/plugin-draw/drawInterpolation.ts +250 -0
- package/_vendor/plugin-draw/drawPath.ts +550 -0
- package/_vendor/plugin-draw/drawSegments.ts +106 -0
- package/_vendor/plugin-draw/drawSelectionTransform.ts +67 -0
- package/_vendor/plugin-draw/drawShapeGeometry.ts +307 -0
- package/_vendor/plugin-draw/drawStrokeGeometry.ts +454 -0
- package/_vendor/plugin-draw/feature.ts +20 -0
- package/_vendor/plugin-draw/index.ts +31 -0
- package/_vendor/plugin-draw/types.ts +36 -0
- package/_vendor/plugin-eraser/EraserTool.ts +297 -0
- package/_vendor/plugin-eraser/canvas.tsx +616 -0
- package/_vendor/plugin-eraser/core.ts +49 -0
- package/_vendor/plugin-eraser/feature.ts +20 -0
- package/_vendor/plugin-eraser/index.ts +4 -0
- package/_vendor/plugin-frame/FrameShapeUtil.ts +173 -0
- package/_vendor/plugin-frame/FrameTool.ts +225 -0
- package/_vendor/plugin-frame/canvas.tsx +75 -0
- package/_vendor/plugin-frame/core.ts +534 -0
- package/_vendor/plugin-frame/feature.ts +20 -0
- package/_vendor/plugin-frame/index.ts +6 -0
- package/_vendor/plugin-geo/GeoShapeUtil.ts +463 -0
- package/_vendor/plugin-geo/GeoTools.ts +247 -0
- package/_vendor/plugin-geo/canvas.tsx +90 -0
- package/_vendor/plugin-geo/core.ts +495 -0
- package/_vendor/plugin-geo/feature.ts +20 -0
- package/_vendor/plugin-geo/index.ts +7 -0
- package/_vendor/plugin-geo/types.ts +27 -0
- package/_vendor/plugin-group/GroupShapeUtil.ts +59 -0
- package/_vendor/plugin-group/canvas.tsx +59 -0
- package/_vendor/plugin-group/core.ts +40 -0
- package/_vendor/plugin-group/feature.ts +20 -0
- package/_vendor/plugin-group/index.ts +5 -0
- package/_vendor/plugin-image/ImageShapeUtil.ts +95 -0
- package/_vendor/plugin-image/canvas.tsx +212 -0
- package/_vendor/plugin-image/core.ts +542 -0
- package/_vendor/plugin-image/crop.ts +770 -0
- package/_vendor/plugin-image/cropInteractions.ts +350 -0
- package/_vendor/plugin-image/cropSession.ts +38 -0
- package/_vendor/plugin-image/feature.ts +20 -0
- package/_vendor/plugin-image/index.ts +29 -0
- package/_vendor/plugin-image/types.ts +55 -0
- package/_vendor/plugin-image-product-react/ImageImportController.tsx +143 -0
- package/_vendor/plugin-image-product-react/imageAssetService.ts +52 -0
- package/_vendor/plugin-image-product-react/imageBlobStore.ts +315 -0
- package/_vendor/plugin-image-product-react/imageHostActions.ts +438 -0
- package/_vendor/plugin-image-product-react/imageProductApi.ts +88 -0
- package/_vendor/plugin-image-product-react/imageToolbarHostBridge.ts +48 -0
- package/_vendor/plugin-image-product-react/imageToolbarRuntime.ts +21 -0
- package/_vendor/plugin-image-product-react/index.ts +60 -0
- package/_vendor/plugin-image-product-react/snapshotGuards.ts +33 -0
- package/_vendor/plugin-image-product-react/svgImageAssetInliner.ts +54 -0
- package/_vendor/plugin-image-product-react/useImageImport.ts +64 -0
- package/_vendor/plugin-laser/LaserTool.ts +154 -0
- package/_vendor/plugin-laser/canvas.tsx +645 -0
- package/_vendor/plugin-laser/core.ts +49 -0
- package/_vendor/plugin-laser/feature.ts +20 -0
- package/_vendor/plugin-laser/index.ts +4 -0
- package/_vendor/plugin-text/ArrowLabelBindingUtil.ts +336 -0
- package/_vendor/plugin-text/GeoLabelBindingUtil.ts +391 -0
- package/_vendor/plugin-text/TextShapeSvgExporter.ts +101 -0
- package/_vendor/plugin-text/TextShapeUtil.ts +284 -0
- package/_vendor/plugin-text/TextTool.ts +228 -0
- package/_vendor/plugin-text/canvas.tsx +115 -0
- package/_vendor/plugin-text/core.ts +422 -0
- package/_vendor/plugin-text/feature.ts +20 -0
- package/_vendor/plugin-text/host/RichTextEditingBox.tsx +144 -0
- package/_vendor/plugin-text/host/RichTextToolbar.tsx +266 -0
- package/_vendor/plugin-text/host/TiptapTextEditor.tsx +191 -0
- package/_vendor/plugin-text/host/richTextRendering.tsx +49 -0
- package/_vendor/plugin-text/host/textDomSelection.ts +93 -0
- package/_vendor/plugin-text/host/textEditingBoxStyle.ts +36 -0
- package/_vendor/plugin-text/host/textEntryFocus.ts +307 -0
- package/_vendor/plugin-text/host/textFontLoader.ts +149 -0
- package/_vendor/plugin-text/host/textMeasurement.ts +258 -0
- package/_vendor/plugin-text/host/textMeasurementSync.ts +77 -0
- package/_vendor/plugin-text/host/textRangeSelection.ts +134 -0
- package/_vendor/plugin-text/host/textShapeRendererRuntime.tsx +89 -0
- package/_vendor/plugin-text/host/textShapeSync.ts +173 -0
- package/_vendor/plugin-text/host/tiptapTextEditorController.ts +148 -0
- package/_vendor/plugin-text/index.ts +18 -0
- package/_vendor/plugin-text/richTextUtils.ts +259 -0
- package/_vendor/plugin-text/textInteractions.ts +366 -0
- package/_vendor/plugin-text/textLabelHelpers.ts +235 -0
- package/_vendor/plugin-text/textShapeHelpers.ts +123 -0
- package/_vendor/plugin-text/textStyle.ts +96 -0
- package/_vendor/plugin-text/textTypes.ts +48 -0
- package/_vendor/signals/atom.ts +74 -0
- package/_vendor/signals/computed.ts +132 -0
- package/_vendor/signals/core.ts +130 -0
- package/_vendor/signals/effect.ts +117 -0
- package/_vendor/signals/index.ts +9 -0
- package/_vendor/signals/types.ts +17 -0
- package/_vendor/store/Store.ts +171 -0
- package/_vendor/store/diff.ts +229 -0
- package/_vendor/store/history.ts +177 -0
- package/_vendor/store/index.ts +32 -0
- package/_vendor/store/types.ts +37 -0
- package/api/canvasKitApi.d.ts +29 -0
- package/index.css +3 -0
- package/index.d.ts +16 -0
- package/index.mjs +1 -0
- package/internal-modules.d.ts +187 -0
- package/package.json +48 -0
- package/product/Shell.d.ts +19 -0
- package/product/chromeContext.d.ts +70 -0
- package/product/createCanvasKitEditor.d.ts +6 -0
- package/product/index.d.ts +7 -0
- package/product/persistence.d.ts +3 -0
- package/product/product.d.ts +22 -0
- package/product/useCanvasClipboardShortcuts.d.ts +6 -0
- package/product-iyBbEPIs.mjs +4 -0
- package/product.mjs +1 -0
- package/standard/extensions.d.ts +1 -0
- package/standard/index.d.ts +3 -0
- package/standard/standardApi.d.ts +91 -0
- package/standard-BmrxwqpJ.mjs +44 -0
- package/standard.mjs +1 -0
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
import type { RecordUpdate, StoreChange } from "@land/store";
|
|
2
|
+
import {
|
|
3
|
+
type BindingId,
|
|
4
|
+
type BindingRecord,
|
|
5
|
+
type EditorRecord,
|
|
6
|
+
isBindingRecord,
|
|
7
|
+
isPageRecord,
|
|
8
|
+
isShapeRecord,
|
|
9
|
+
type PageId,
|
|
10
|
+
type ShapeId,
|
|
11
|
+
type ShapeParentId,
|
|
12
|
+
type ShapeRecord,
|
|
13
|
+
} from "../../schema/records";
|
|
14
|
+
|
|
15
|
+
export interface QueryState {
|
|
16
|
+
pageShapeIdsByPage: Map<PageId, ShapeId[]>;
|
|
17
|
+
childShapeIdsByParent: Map<ShapeParentId, ShapeId[]>;
|
|
18
|
+
pageIdByShape: Map<ShapeId, PageId>;
|
|
19
|
+
bindingById: Map<BindingId, BindingRecord>;
|
|
20
|
+
bindingIdsByFromShape: Map<ShapeId, BindingId[]>;
|
|
21
|
+
bindingIdsByToShape: Map<ShapeId, BindingId[]>;
|
|
22
|
+
relatedBindingIdsByShape: Map<ShapeId, BindingId[]>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type ReadEditorRecord = (id: string) => EditorRecord | undefined;
|
|
26
|
+
|
|
27
|
+
function pushShapeId<Key extends string>(
|
|
28
|
+
map: Map<Key, ShapeId[]>,
|
|
29
|
+
key: Key,
|
|
30
|
+
shapeId: ShapeId,
|
|
31
|
+
): void {
|
|
32
|
+
const ids = map.get(key);
|
|
33
|
+
if (ids) {
|
|
34
|
+
ids.push(shapeId);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
map.set(key, [shapeId]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function pushBindingId<Key extends string>(
|
|
41
|
+
map: Map<Key, BindingId[]>,
|
|
42
|
+
key: Key,
|
|
43
|
+
bindingId: BindingId,
|
|
44
|
+
): void {
|
|
45
|
+
const ids = map.get(key);
|
|
46
|
+
if (ids) {
|
|
47
|
+
ids.push(bindingId);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
map.set(key, [bindingId]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function removeIdFromMap<Key extends string, TId extends string>(
|
|
54
|
+
map: Map<Key, TId[]>,
|
|
55
|
+
key: Key,
|
|
56
|
+
id: TId,
|
|
57
|
+
): boolean {
|
|
58
|
+
const currentIds = map.get(key);
|
|
59
|
+
if (!currentIds) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const index = currentIds.indexOf(id);
|
|
64
|
+
if (index < 0) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (currentIds.length === 1) {
|
|
69
|
+
map.delete(key);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const nextIds = currentIds.slice();
|
|
74
|
+
nextIds.splice(index, 1);
|
|
75
|
+
map.set(key, nextIds);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function areShapeIdArraysEqual(a: ShapeId[], b: ShapeId[]): boolean {
|
|
80
|
+
if (a === b) return true;
|
|
81
|
+
if (a.length !== b.length) return false;
|
|
82
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
83
|
+
if (a[i] !== b[i]) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function compareShapeIndex(left: ShapeRecord, right: ShapeRecord): number {
|
|
91
|
+
return left.index.localeCompare(right.index);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function compareBindings(
|
|
95
|
+
left: BindingRecord,
|
|
96
|
+
right: BindingRecord,
|
|
97
|
+
): number {
|
|
98
|
+
if (left.fromId === right.fromId) {
|
|
99
|
+
return left.id.localeCompare(right.id);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return left.fromId.localeCompare(right.fromId);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function compareBindingForRelation(
|
|
106
|
+
relation: "from" | "to" | "related",
|
|
107
|
+
left: BindingRecord,
|
|
108
|
+
right: BindingRecord,
|
|
109
|
+
): number {
|
|
110
|
+
if (relation === "to") {
|
|
111
|
+
if (left.toId === right.toId) {
|
|
112
|
+
if (left.fromId === right.fromId) {
|
|
113
|
+
return left.id.localeCompare(right.id);
|
|
114
|
+
}
|
|
115
|
+
return left.fromId.localeCompare(right.fromId);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return left.toId.localeCompare(right.toId);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return compareBindings(left, right);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function findBindingInsertIndex(
|
|
125
|
+
bindingIds: BindingId[],
|
|
126
|
+
binding: BindingRecord,
|
|
127
|
+
relation: "from" | "to" | "related",
|
|
128
|
+
getBinding: (id: BindingId) => BindingRecord | undefined,
|
|
129
|
+
): number {
|
|
130
|
+
for (let index = 0; index < bindingIds.length; index += 1) {
|
|
131
|
+
const currentBinding = getBinding(bindingIds[index]!);
|
|
132
|
+
if (
|
|
133
|
+
currentBinding &&
|
|
134
|
+
compareBindingForRelation(relation, binding, currentBinding) < 0
|
|
135
|
+
) {
|
|
136
|
+
return index;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return bindingIds.length;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function insertBindingId<Key extends string>(
|
|
144
|
+
map: Map<Key, BindingId[]>,
|
|
145
|
+
key: Key,
|
|
146
|
+
binding: BindingRecord,
|
|
147
|
+
relation: "from" | "to" | "related",
|
|
148
|
+
getBinding: (id: BindingId) => BindingRecord | undefined,
|
|
149
|
+
): boolean {
|
|
150
|
+
const currentIds = map.get(key);
|
|
151
|
+
if (!currentIds) {
|
|
152
|
+
map.set(key, [binding.id]);
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (currentIds.includes(binding.id)) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const nextIds = currentIds.slice();
|
|
161
|
+
nextIds.splice(
|
|
162
|
+
findBindingInsertIndex(currentIds, binding, relation, getBinding),
|
|
163
|
+
0,
|
|
164
|
+
binding.id,
|
|
165
|
+
);
|
|
166
|
+
map.set(key, nextIds);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function insertShapeIdSorted(
|
|
171
|
+
map: Map<ShapeParentId, ShapeId[]>,
|
|
172
|
+
parentId: ShapeParentId,
|
|
173
|
+
shape: ShapeRecord,
|
|
174
|
+
resolveIndex: (id: ShapeId) => string | undefined,
|
|
175
|
+
): void {
|
|
176
|
+
const ids = map.get(parentId);
|
|
177
|
+
if (!ids) {
|
|
178
|
+
map.set(parentId, [shape.id]);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
if (ids.includes(shape.id)) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
let insertAt = ids.length;
|
|
185
|
+
for (let i = 0; i < ids.length; i += 1) {
|
|
186
|
+
const siblingIndex = resolveIndex(ids[i]!);
|
|
187
|
+
if (
|
|
188
|
+
siblingIndex !== undefined &&
|
|
189
|
+
siblingIndex.localeCompare(shape.index) > 0
|
|
190
|
+
) {
|
|
191
|
+
insertAt = i;
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const next = ids.slice();
|
|
196
|
+
next.splice(insertAt, 0, shape.id);
|
|
197
|
+
map.set(parentId, next);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function removeShapeIdFromParent(
|
|
201
|
+
map: Map<ShapeParentId, ShapeId[]>,
|
|
202
|
+
parentId: ShapeParentId,
|
|
203
|
+
shapeId: ShapeId,
|
|
204
|
+
): void {
|
|
205
|
+
removeIdFromMap(map, parentId, shapeId);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function isShapeUpdate(
|
|
209
|
+
update: RecordUpdate<EditorRecord>,
|
|
210
|
+
): update is RecordUpdate<ShapeRecord> {
|
|
211
|
+
return isShapeRecord(update.before) && isShapeRecord(update.after);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function isBindingUpdate(
|
|
215
|
+
update: RecordUpdate<EditorRecord>,
|
|
216
|
+
): update is RecordUpdate<BindingRecord> {
|
|
217
|
+
return isBindingRecord(update.before) && isBindingRecord(update.after);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function buildQueryState(records: EditorRecord[]): QueryState {
|
|
221
|
+
const pages = records.filter(isPageRecord).sort((left, right) =>
|
|
222
|
+
left.index.localeCompare(right.index),
|
|
223
|
+
);
|
|
224
|
+
const shapes = records.filter(isShapeRecord).sort((left, right) => {
|
|
225
|
+
if (left.parentId === right.parentId) {
|
|
226
|
+
return compareShapeIndex(left, right);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return left.parentId.localeCompare(right.parentId);
|
|
230
|
+
});
|
|
231
|
+
const bindings = records.filter(isBindingRecord).sort(compareBindings);
|
|
232
|
+
|
|
233
|
+
const pageShapeIdsByPage = new Map<PageId, ShapeId[]>();
|
|
234
|
+
const childShapeIdsByParent = new Map<ShapeParentId, ShapeId[]>();
|
|
235
|
+
const pageIdByShape = new Map<ShapeId, PageId>();
|
|
236
|
+
const shapeById = new Map(shapes.map((shape) => [shape.id, shape]));
|
|
237
|
+
const bindingById = new Map<BindingId, BindingRecord>();
|
|
238
|
+
const bindingIdsByFromShape = new Map<ShapeId, BindingId[]>();
|
|
239
|
+
const bindingIdsByToShape = new Map<ShapeId, BindingId[]>();
|
|
240
|
+
const relatedBindingIdsByShape = new Map<ShapeId, BindingId[]>();
|
|
241
|
+
|
|
242
|
+
for (const shape of shapes) {
|
|
243
|
+
pushShapeId(childShapeIdsByParent, shape.parentId, shape.id);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const collectPageShapeIds = (pageId: PageId, parentId: ShapeParentId) => {
|
|
247
|
+
for (const childId of childShapeIdsByParent.get(parentId) ?? []) {
|
|
248
|
+
const child = shapeById.get(childId);
|
|
249
|
+
if (!child) {
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
pushShapeId(pageShapeIdsByPage, pageId, child.id);
|
|
254
|
+
pageIdByShape.set(child.id, pageId);
|
|
255
|
+
collectPageShapeIds(pageId, child.id);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
for (const page of pages) {
|
|
260
|
+
collectPageShapeIds(page.id, page.id);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
for (const binding of bindings) {
|
|
264
|
+
bindingById.set(binding.id, binding);
|
|
265
|
+
pushBindingId(bindingIdsByFromShape, binding.fromId, binding.id);
|
|
266
|
+
pushBindingId(bindingIdsByToShape, binding.toId, binding.id);
|
|
267
|
+
pushBindingId(relatedBindingIdsByShape, binding.fromId, binding.id);
|
|
268
|
+
pushBindingId(relatedBindingIdsByShape, binding.toId, binding.id);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
pageShapeIdsByPage,
|
|
273
|
+
childShapeIdsByParent,
|
|
274
|
+
pageIdByShape,
|
|
275
|
+
bindingById,
|
|
276
|
+
bindingIdsByFromShape,
|
|
277
|
+
bindingIdsByToShape,
|
|
278
|
+
relatedBindingIdsByShape,
|
|
279
|
+
} satisfies QueryState;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function getRemovedShapeIdsFromQueryStateChange(
|
|
283
|
+
change: StoreChange<EditorRecord>,
|
|
284
|
+
): ShapeId[] {
|
|
285
|
+
return change.diff.removed.filter(isShapeRecord).map((shape) => shape.id);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function updateQueryStateFromStoreChange(
|
|
289
|
+
previous: QueryState,
|
|
290
|
+
change: StoreChange<EditorRecord>,
|
|
291
|
+
getRecord: ReadEditorRecord,
|
|
292
|
+
): QueryState {
|
|
293
|
+
const addedShapes = change.diff.added.filter(isShapeRecord);
|
|
294
|
+
const updatedShapes = change.diff.updated.filter(isShapeUpdate);
|
|
295
|
+
const removedShapes = change.diff.removed.filter(isShapeRecord);
|
|
296
|
+
|
|
297
|
+
const addedBindings = change.diff.added.filter(isBindingRecord);
|
|
298
|
+
const updatedBindings = change.diff.updated.filter(isBindingUpdate);
|
|
299
|
+
const removedBindings = change.diff.removed.filter(isBindingRecord);
|
|
300
|
+
|
|
301
|
+
const hasStructuralShapeChange =
|
|
302
|
+
addedShapes.length > 0 ||
|
|
303
|
+
removedShapes.length > 0 ||
|
|
304
|
+
updatedShapes.some(
|
|
305
|
+
({ before, after }) =>
|
|
306
|
+
before.parentId !== after.parentId || before.index !== after.index,
|
|
307
|
+
);
|
|
308
|
+
const hasBindingIndexChange =
|
|
309
|
+
addedBindings.length > 0 ||
|
|
310
|
+
removedBindings.length > 0 ||
|
|
311
|
+
updatedBindings.some(
|
|
312
|
+
({ before, after }) =>
|
|
313
|
+
before.fromId !== after.fromId || before.toId !== after.toId,
|
|
314
|
+
);
|
|
315
|
+
const hasBindingRecordChange =
|
|
316
|
+
addedBindings.length > 0 ||
|
|
317
|
+
removedBindings.length > 0 ||
|
|
318
|
+
updatedBindings.length > 0;
|
|
319
|
+
|
|
320
|
+
if (!hasStructuralShapeChange && !hasBindingRecordChange) {
|
|
321
|
+
return previous;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const next: QueryState = {
|
|
325
|
+
pageShapeIdsByPage: new Map(previous.pageShapeIdsByPage),
|
|
326
|
+
childShapeIdsByParent: new Map(previous.childShapeIdsByParent),
|
|
327
|
+
pageIdByShape: new Map(previous.pageIdByShape),
|
|
328
|
+
bindingById: new Map(previous.bindingById),
|
|
329
|
+
bindingIdsByFromShape: new Map(previous.bindingIdsByFromShape),
|
|
330
|
+
bindingIdsByToShape: new Map(previous.bindingIdsByToShape),
|
|
331
|
+
relatedBindingIdsByShape: new Map(previous.relatedBindingIdsByShape),
|
|
332
|
+
};
|
|
333
|
+
let didChange = false;
|
|
334
|
+
|
|
335
|
+
if (hasStructuralShapeChange) {
|
|
336
|
+
const resolveIndex = (id: ShapeId): string | undefined => {
|
|
337
|
+
const record = getRecord(id);
|
|
338
|
+
return isShapeRecord(record) ? record.index : undefined;
|
|
339
|
+
};
|
|
340
|
+
const affectedPages = new Set<PageId>();
|
|
341
|
+
|
|
342
|
+
const resolvePageFromStore = (parentId: ShapeParentId): PageId | null => {
|
|
343
|
+
let current: ShapeParentId | undefined = parentId;
|
|
344
|
+
const guard = new Set<ShapeParentId>();
|
|
345
|
+
while (current && !guard.has(current)) {
|
|
346
|
+
guard.add(current);
|
|
347
|
+
const record = getRecord(current);
|
|
348
|
+
if (!record) return null;
|
|
349
|
+
if (isPageRecord(record)) return record.id;
|
|
350
|
+
if (isShapeRecord(record)) {
|
|
351
|
+
current = record.parentId;
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
return null;
|
|
355
|
+
}
|
|
356
|
+
return null;
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
for (const shape of removedShapes) {
|
|
360
|
+
removeShapeIdFromParent(
|
|
361
|
+
next.childShapeIdsByParent,
|
|
362
|
+
shape.parentId,
|
|
363
|
+
shape.id,
|
|
364
|
+
);
|
|
365
|
+
const page = previous.pageIdByShape.get(shape.id);
|
|
366
|
+
if (page) affectedPages.add(page);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
for (const { before, after } of updatedShapes) {
|
|
370
|
+
if (before.parentId === after.parentId && before.index === after.index) {
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
removeShapeIdFromParent(
|
|
374
|
+
next.childShapeIdsByParent,
|
|
375
|
+
before.parentId,
|
|
376
|
+
before.id,
|
|
377
|
+
);
|
|
378
|
+
insertShapeIdSorted(
|
|
379
|
+
next.childShapeIdsByParent,
|
|
380
|
+
after.parentId,
|
|
381
|
+
after,
|
|
382
|
+
resolveIndex,
|
|
383
|
+
);
|
|
384
|
+
const oldPage = previous.pageIdByShape.get(before.id);
|
|
385
|
+
if (oldPage) affectedPages.add(oldPage);
|
|
386
|
+
const newPage = resolvePageFromStore(after.parentId);
|
|
387
|
+
if (newPage) affectedPages.add(newPage);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
for (const shape of addedShapes) {
|
|
391
|
+
insertShapeIdSorted(
|
|
392
|
+
next.childShapeIdsByParent,
|
|
393
|
+
shape.parentId,
|
|
394
|
+
shape,
|
|
395
|
+
resolveIndex,
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
for (const shape of addedShapes) {
|
|
399
|
+
const page = resolvePageFromStore(shape.parentId);
|
|
400
|
+
if (page) affectedPages.add(page);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (affectedPages.size > 0) {
|
|
404
|
+
const pageShapeIdsByPage = new Map(previous.pageShapeIdsByPage);
|
|
405
|
+
const pageIdByShape = new Map(previous.pageIdByShape);
|
|
406
|
+
|
|
407
|
+
for (const [shapeId, pageId] of previous.pageIdByShape) {
|
|
408
|
+
if (affectedPages.has(pageId)) {
|
|
409
|
+
pageIdByShape.delete(shapeId);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
const walk = (pageId: PageId, parentId: ShapeParentId): ShapeId[] => {
|
|
414
|
+
const collected: ShapeId[] = [];
|
|
415
|
+
for (const childId of next.childShapeIdsByParent.get(parentId) ?? []) {
|
|
416
|
+
if (!getRecord(childId)) continue;
|
|
417
|
+
collected.push(childId);
|
|
418
|
+
pageIdByShape.set(childId, pageId);
|
|
419
|
+
collected.push(...walk(pageId, childId));
|
|
420
|
+
}
|
|
421
|
+
return collected;
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
for (const pageId of affectedPages) {
|
|
425
|
+
const ids = walk(pageId, pageId);
|
|
426
|
+
if (ids.length > 0) {
|
|
427
|
+
pageShapeIdsByPage.set(pageId, ids);
|
|
428
|
+
} else {
|
|
429
|
+
pageShapeIdsByPage.delete(pageId);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
next.pageShapeIdsByPage = pageShapeIdsByPage;
|
|
434
|
+
next.pageIdByShape = pageIdByShape;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
didChange = true;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (hasBindingRecordChange) {
|
|
441
|
+
for (const binding of removedBindings) {
|
|
442
|
+
didChange = next.bindingById.delete(binding.id) || didChange;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (hasBindingIndexChange) {
|
|
446
|
+
for (const binding of removedBindings) {
|
|
447
|
+
didChange =
|
|
448
|
+
removeIdFromMap(
|
|
449
|
+
next.bindingIdsByFromShape,
|
|
450
|
+
binding.fromId,
|
|
451
|
+
binding.id,
|
|
452
|
+
) || didChange;
|
|
453
|
+
didChange =
|
|
454
|
+
removeIdFromMap(
|
|
455
|
+
next.bindingIdsByToShape,
|
|
456
|
+
binding.toId,
|
|
457
|
+
binding.id,
|
|
458
|
+
) || didChange;
|
|
459
|
+
didChange =
|
|
460
|
+
removeIdFromMap(
|
|
461
|
+
next.relatedBindingIdsByShape,
|
|
462
|
+
binding.fromId,
|
|
463
|
+
binding.id,
|
|
464
|
+
) || didChange;
|
|
465
|
+
didChange =
|
|
466
|
+
removeIdFromMap(
|
|
467
|
+
next.relatedBindingIdsByShape,
|
|
468
|
+
binding.toId,
|
|
469
|
+
binding.id,
|
|
470
|
+
) || didChange;
|
|
471
|
+
}
|
|
472
|
+
for (const { before, after } of updatedBindings) {
|
|
473
|
+
if (before.fromId !== after.fromId || before.toId !== after.toId) {
|
|
474
|
+
didChange =
|
|
475
|
+
removeIdFromMap(
|
|
476
|
+
next.bindingIdsByFromShape,
|
|
477
|
+
before.fromId,
|
|
478
|
+
before.id,
|
|
479
|
+
) || didChange;
|
|
480
|
+
didChange =
|
|
481
|
+
removeIdFromMap(next.bindingIdsByToShape, before.toId, before.id) ||
|
|
482
|
+
didChange;
|
|
483
|
+
didChange =
|
|
484
|
+
removeIdFromMap(
|
|
485
|
+
next.relatedBindingIdsByShape,
|
|
486
|
+
before.fromId,
|
|
487
|
+
before.id,
|
|
488
|
+
) || didChange;
|
|
489
|
+
didChange =
|
|
490
|
+
removeIdFromMap(
|
|
491
|
+
next.relatedBindingIdsByShape,
|
|
492
|
+
before.toId,
|
|
493
|
+
before.id,
|
|
494
|
+
) || didChange;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
for (const binding of addedBindings) {
|
|
500
|
+
next.bindingById.set(binding.id, binding);
|
|
501
|
+
didChange = true;
|
|
502
|
+
}
|
|
503
|
+
for (const { after } of updatedBindings) {
|
|
504
|
+
next.bindingById.set(after.id, after);
|
|
505
|
+
didChange = true;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
if (hasBindingIndexChange) {
|
|
509
|
+
for (const binding of addedBindings) {
|
|
510
|
+
didChange =
|
|
511
|
+
insertBindingId(
|
|
512
|
+
next.bindingIdsByFromShape,
|
|
513
|
+
binding.fromId,
|
|
514
|
+
binding,
|
|
515
|
+
"from",
|
|
516
|
+
(bindingId) => next.bindingById.get(bindingId),
|
|
517
|
+
) || didChange;
|
|
518
|
+
didChange =
|
|
519
|
+
insertBindingId(
|
|
520
|
+
next.bindingIdsByToShape,
|
|
521
|
+
binding.toId,
|
|
522
|
+
binding,
|
|
523
|
+
"to",
|
|
524
|
+
(bindingId) => next.bindingById.get(bindingId),
|
|
525
|
+
) || didChange;
|
|
526
|
+
didChange =
|
|
527
|
+
insertBindingId(
|
|
528
|
+
next.relatedBindingIdsByShape,
|
|
529
|
+
binding.fromId,
|
|
530
|
+
binding,
|
|
531
|
+
"related",
|
|
532
|
+
(bindingId) => next.bindingById.get(bindingId),
|
|
533
|
+
) || didChange;
|
|
534
|
+
didChange =
|
|
535
|
+
insertBindingId(
|
|
536
|
+
next.relatedBindingIdsByShape,
|
|
537
|
+
binding.toId,
|
|
538
|
+
binding,
|
|
539
|
+
"related",
|
|
540
|
+
(bindingId) => next.bindingById.get(bindingId),
|
|
541
|
+
) || didChange;
|
|
542
|
+
}
|
|
543
|
+
for (const { before, after } of updatedBindings) {
|
|
544
|
+
if (before.fromId !== after.fromId || before.toId !== after.toId) {
|
|
545
|
+
didChange =
|
|
546
|
+
insertBindingId(
|
|
547
|
+
next.bindingIdsByFromShape,
|
|
548
|
+
after.fromId,
|
|
549
|
+
after,
|
|
550
|
+
"from",
|
|
551
|
+
(bindingId) => next.bindingById.get(bindingId),
|
|
552
|
+
) || didChange;
|
|
553
|
+
didChange =
|
|
554
|
+
insertBindingId(
|
|
555
|
+
next.bindingIdsByToShape,
|
|
556
|
+
after.toId,
|
|
557
|
+
after,
|
|
558
|
+
"to",
|
|
559
|
+
(bindingId) => next.bindingById.get(bindingId),
|
|
560
|
+
) || didChange;
|
|
561
|
+
didChange =
|
|
562
|
+
insertBindingId(
|
|
563
|
+
next.relatedBindingIdsByShape,
|
|
564
|
+
after.fromId,
|
|
565
|
+
after,
|
|
566
|
+
"related",
|
|
567
|
+
(bindingId) => next.bindingById.get(bindingId),
|
|
568
|
+
) || didChange;
|
|
569
|
+
didChange =
|
|
570
|
+
insertBindingId(
|
|
571
|
+
next.relatedBindingIdsByShape,
|
|
572
|
+
after.toId,
|
|
573
|
+
after,
|
|
574
|
+
"related",
|
|
575
|
+
(bindingId) => next.bindingById.get(bindingId),
|
|
576
|
+
) || didChange;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
return didChange ? next : previous;
|
|
583
|
+
}
|