@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,250 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { Canvas, EditorContext } from "@land/canvas-react";
|
|
3
|
+
import { createCanvasPluginHostRuntime } from "@land/canvas-react/host";
|
|
4
|
+
import type { AssetId, ContextMenuOpenSnapshot, Editor } from "@land/canvas-core";
|
|
5
|
+
import type { CanvasPluginHostRuntime } from "@land/canvas-react/host";
|
|
6
|
+
import type {
|
|
7
|
+
ProductApiRegistry,
|
|
8
|
+
ProductApiRegistryRuntime,
|
|
9
|
+
} from "../api/apiRegistry";
|
|
10
|
+
import { createProductApiRegistry } from "../api/apiRegistry";
|
|
11
|
+
import type { CanvasProductDefinition } from "./productDefinition";
|
|
12
|
+
import type {
|
|
13
|
+
ProductAssetResolverService,
|
|
14
|
+
ProductServiceRegistry,
|
|
15
|
+
ProductServiceRegistryRuntime,
|
|
16
|
+
} from "./productServices";
|
|
17
|
+
import { createProductServiceRegistry } from "./productServices";
|
|
18
|
+
|
|
19
|
+
export function CanvasProductRoot({
|
|
20
|
+
product,
|
|
21
|
+
}: {
|
|
22
|
+
product: CanvasProductDefinition;
|
|
23
|
+
}) {
|
|
24
|
+
const [runtime, setRuntime] = useState<CanvasProductRuntime | null>(null);
|
|
25
|
+
const [contextMenuOpenSnapshot, setContextMenuOpenSnapshot] =
|
|
26
|
+
useState<ContextMenuOpenSnapshot | null>(null);
|
|
27
|
+
const activeRuntime = runtime?.product === product ? runtime : null;
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const nextRuntime = createCanvasProductRuntime(product);
|
|
31
|
+
setRuntime(nextRuntime);
|
|
32
|
+
setContextMenuOpenSnapshot(null);
|
|
33
|
+
|
|
34
|
+
return () => {
|
|
35
|
+
nextRuntime.services.dispose();
|
|
36
|
+
};
|
|
37
|
+
}, [product]);
|
|
38
|
+
|
|
39
|
+
const shouldRestoreStoredSnapshots =
|
|
40
|
+
activeRuntime &&
|
|
41
|
+
!!product.persistence?.restore &&
|
|
42
|
+
!activeRuntime.pluginHostRuntime.disablePersistence &&
|
|
43
|
+
!activeRuntime.pluginHostRuntime.ignoreStoredSnapshots &&
|
|
44
|
+
!(product.shouldSkipStoredSnapshots?.() ?? false);
|
|
45
|
+
const [hasHandledStoredSnapshots, setHasHandledStoredSnapshots] =
|
|
46
|
+
useState(false);
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (!activeRuntime) {
|
|
50
|
+
setHasHandledStoredSnapshots(false);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const { editor, pluginHostRuntime, services } = activeRuntime;
|
|
55
|
+
|
|
56
|
+
if (pluginHostRuntime.disablePersistence) {
|
|
57
|
+
setHasHandledStoredSnapshots(true);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const persistence = product.persistence;
|
|
62
|
+
let cancelled = false;
|
|
63
|
+
let stopPersistence: (() => void) | null = null;
|
|
64
|
+
const startPersistence = () => {
|
|
65
|
+
if (!cancelled && persistence?.start) {
|
|
66
|
+
stopPersistence = persistence.start(editor);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
if (!shouldRestoreStoredSnapshots) {
|
|
71
|
+
setHasHandledStoredSnapshots(true);
|
|
72
|
+
startPersistence();
|
|
73
|
+
return () => {
|
|
74
|
+
cancelled = true;
|
|
75
|
+
stopPersistence?.();
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
setHasHandledStoredSnapshots(false);
|
|
80
|
+
void persistence?.restore?.(editor)
|
|
81
|
+
.then(() => preloadProductAssets({ editor, product, services }))
|
|
82
|
+
.then(() => cleanupProductAssetsAfterRestore({ editor, product, services }))
|
|
83
|
+
.then(() => {
|
|
84
|
+
if (cancelled) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
setHasHandledStoredSnapshots(true);
|
|
88
|
+
startPersistence();
|
|
89
|
+
})
|
|
90
|
+
.catch((error: unknown) => {
|
|
91
|
+
console.warn("Could not restore persisted canvas snapshot.", error);
|
|
92
|
+
setHasHandledStoredSnapshots(true);
|
|
93
|
+
startPersistence();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return () => {
|
|
97
|
+
cancelled = true;
|
|
98
|
+
stopPersistence?.();
|
|
99
|
+
};
|
|
100
|
+
}, [activeRuntime, product, shouldRestoreStoredSnapshots]);
|
|
101
|
+
|
|
102
|
+
if (!activeRuntime) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const { apiRegistry, editor, pluginHostRuntime, services } = activeRuntime;
|
|
107
|
+
const ProductShell = product.Shell;
|
|
108
|
+
const canvas = hasHandledStoredSnapshots ? (
|
|
109
|
+
<Canvas
|
|
110
|
+
canvasComponents={pluginHostRuntime.canvasComponents}
|
|
111
|
+
canvasProviders={pluginHostRuntime.canvasProviders}
|
|
112
|
+
domEventGuardRegistry={pluginHostRuntime.domEventGuardRegistry}
|
|
113
|
+
onContextMenuRequest={setContextMenuOpenSnapshot}
|
|
114
|
+
rendererRegistry={pluginHostRuntime.rendererRegistry}
|
|
115
|
+
/>
|
|
116
|
+
) : null;
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<EditorContext.Provider value={editor}>
|
|
120
|
+
<ProductShell
|
|
121
|
+
product={product}
|
|
122
|
+
editor={editor}
|
|
123
|
+
pluginHostRuntime={pluginHostRuntime}
|
|
124
|
+
apiRegistry={apiRegistry}
|
|
125
|
+
services={services}
|
|
126
|
+
canvas={canvas}
|
|
127
|
+
contextMenuOpenSnapshot={contextMenuOpenSnapshot}
|
|
128
|
+
onContextMenuOpenChange={setContextMenuOpenSnapshot}
|
|
129
|
+
isReady={hasHandledStoredSnapshots}
|
|
130
|
+
/>
|
|
131
|
+
</EditorContext.Provider>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
interface CanvasProductRuntime {
|
|
136
|
+
product: CanvasProductDefinition;
|
|
137
|
+
editor: Editor;
|
|
138
|
+
apiRegistry: ProductApiRegistryRuntime;
|
|
139
|
+
pluginHostRuntime: CanvasPluginHostRuntime;
|
|
140
|
+
services: ProductServiceRegistryRuntime;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function createCanvasProductRuntime(
|
|
144
|
+
product: CanvasProductDefinition,
|
|
145
|
+
): CanvasProductRuntime {
|
|
146
|
+
const services = createProductServiceRegistry({
|
|
147
|
+
factories: product.services ?? [],
|
|
148
|
+
productId: product.id,
|
|
149
|
+
});
|
|
150
|
+
const apiRegistry = createProductApiRegistry();
|
|
151
|
+
const pluginHostRuntime = createCanvasPluginHostRuntime(product.extensions);
|
|
152
|
+
|
|
153
|
+
let currentEditor: Editor | null = null;
|
|
154
|
+
const editor = product.createEditor({
|
|
155
|
+
corePlugins: pluginHostRuntime.corePlugins,
|
|
156
|
+
resolveAssetUrl(assetId) {
|
|
157
|
+
return currentEditor
|
|
158
|
+
? resolveProductAssetUrl({
|
|
159
|
+
assetId,
|
|
160
|
+
editor: currentEditor,
|
|
161
|
+
product,
|
|
162
|
+
services,
|
|
163
|
+
})
|
|
164
|
+
: null;
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
currentEditor = editor;
|
|
168
|
+
|
|
169
|
+
for (const setupRuntime of product.runtimeSetups ?? []) {
|
|
170
|
+
setupRuntime({
|
|
171
|
+
productId: product.id,
|
|
172
|
+
apiRegistry,
|
|
173
|
+
editor,
|
|
174
|
+
pluginHostRuntime,
|
|
175
|
+
services,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return {
|
|
180
|
+
product,
|
|
181
|
+
apiRegistry,
|
|
182
|
+
editor,
|
|
183
|
+
pluginHostRuntime,
|
|
184
|
+
services,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function getAssetResolverServices({
|
|
189
|
+
product,
|
|
190
|
+
services,
|
|
191
|
+
}: {
|
|
192
|
+
product: CanvasProductDefinition;
|
|
193
|
+
services: ProductServiceRegistry;
|
|
194
|
+
}): ProductAssetResolverService[] {
|
|
195
|
+
return (product.assetResolverTokens ?? [])
|
|
196
|
+
.map((token) => services.get(token))
|
|
197
|
+
.filter((service): service is ProductAssetResolverService => !!service);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function resolveProductAssetUrl({
|
|
201
|
+
assetId,
|
|
202
|
+
editor,
|
|
203
|
+
product,
|
|
204
|
+
services,
|
|
205
|
+
}: {
|
|
206
|
+
assetId: AssetId;
|
|
207
|
+
editor: Editor;
|
|
208
|
+
product: CanvasProductDefinition;
|
|
209
|
+
services: ProductServiceRegistry;
|
|
210
|
+
}): string | null {
|
|
211
|
+
for (const resolver of getAssetResolverServices({ product, services })) {
|
|
212
|
+
const url = resolver.resolveAssetUrl(editor, assetId);
|
|
213
|
+
if (url) {
|
|
214
|
+
return url;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function preloadProductAssets({
|
|
221
|
+
editor,
|
|
222
|
+
product,
|
|
223
|
+
services,
|
|
224
|
+
}: {
|
|
225
|
+
editor: Editor;
|
|
226
|
+
product: CanvasProductDefinition;
|
|
227
|
+
services: ProductServiceRegistry;
|
|
228
|
+
}): Promise<void> {
|
|
229
|
+
await Promise.all(
|
|
230
|
+
getAssetResolverServices({ product, services }).map((resolver) =>
|
|
231
|
+
resolver.preloadForEditor?.(editor),
|
|
232
|
+
),
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async function cleanupProductAssetsAfterRestore({
|
|
237
|
+
editor,
|
|
238
|
+
product,
|
|
239
|
+
services,
|
|
240
|
+
}: {
|
|
241
|
+
editor: Editor;
|
|
242
|
+
product: CanvasProductDefinition;
|
|
243
|
+
services: ProductServiceRegistry;
|
|
244
|
+
}): Promise<void> {
|
|
245
|
+
await Promise.all(
|
|
246
|
+
getAssetResolverServices({ product, services }).map((resolver) =>
|
|
247
|
+
resolver.cleanupAfterRestore?.(editor),
|
|
248
|
+
),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { ComponentType, ReactNode } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
AssetId,
|
|
4
|
+
ContextMenuOpenSnapshot,
|
|
5
|
+
Editor,
|
|
6
|
+
LandCorePlugin,
|
|
7
|
+
} from "@land/canvas-core";
|
|
8
|
+
import type { LandExtension } from "@land/canvas-plugin-api";
|
|
9
|
+
import type { CanvasPluginHostRuntime } from "@land/canvas-react/host";
|
|
10
|
+
import type {
|
|
11
|
+
ProductApiRegistry,
|
|
12
|
+
ProductApiRegistryRuntime,
|
|
13
|
+
} from "../api/apiRegistry";
|
|
14
|
+
import type {
|
|
15
|
+
ProductAssetResolverService,
|
|
16
|
+
ProductServiceFactory,
|
|
17
|
+
ProductServiceRegistry,
|
|
18
|
+
ProductServiceRegistryRuntime,
|
|
19
|
+
ProductServiceToken,
|
|
20
|
+
} from "./productServices";
|
|
21
|
+
|
|
22
|
+
export interface ProductPersistenceAdapter {
|
|
23
|
+
restore?(editor: Editor): Promise<void>;
|
|
24
|
+
start?(editor: Editor): () => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface CreateCanvasProductEditorOptions {
|
|
28
|
+
corePlugins: readonly LandCorePlugin[];
|
|
29
|
+
resolveAssetUrl?: (assetId: AssetId) => string | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface CanvasProductRuntimeSetupContext {
|
|
33
|
+
productId: string;
|
|
34
|
+
apiRegistry: ProductApiRegistryRuntime;
|
|
35
|
+
editor: Editor;
|
|
36
|
+
pluginHostRuntime: CanvasPluginHostRuntime;
|
|
37
|
+
services: ProductServiceRegistryRuntime;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type CanvasProductRuntimeSetup = (
|
|
41
|
+
context: CanvasProductRuntimeSetupContext,
|
|
42
|
+
) => void;
|
|
43
|
+
|
|
44
|
+
export interface CanvasProductShellProps {
|
|
45
|
+
product: CanvasProductDefinition;
|
|
46
|
+
editor: Editor;
|
|
47
|
+
pluginHostRuntime: CanvasPluginHostRuntime;
|
|
48
|
+
apiRegistry: ProductApiRegistry;
|
|
49
|
+
services: ProductServiceRegistry;
|
|
50
|
+
canvas: ReactNode | null;
|
|
51
|
+
contextMenuOpenSnapshot: ContextMenuOpenSnapshot | null;
|
|
52
|
+
onContextMenuOpenChange: (snapshot: ContextMenuOpenSnapshot | null) => void;
|
|
53
|
+
isReady: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface CanvasProductDefinition {
|
|
57
|
+
id: string;
|
|
58
|
+
extensions: readonly LandExtension[];
|
|
59
|
+
createEditor(options: CreateCanvasProductEditorOptions): Editor;
|
|
60
|
+
Shell: ComponentType<CanvasProductShellProps>;
|
|
61
|
+
runtimeSetups?: readonly CanvasProductRuntimeSetup[];
|
|
62
|
+
services?: readonly ProductServiceFactory<unknown>[];
|
|
63
|
+
assetResolverTokens?: readonly ProductServiceToken<ProductAssetResolverService>[];
|
|
64
|
+
persistence?: ProductPersistenceAdapter | null;
|
|
65
|
+
shouldSkipStoredSnapshots?: () => boolean;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function defineCanvasProduct(
|
|
69
|
+
definition: CanvasProductDefinition,
|
|
70
|
+
): CanvasProductDefinition {
|
|
71
|
+
return definition;
|
|
72
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { AssetId, Editor } from "@land/canvas-core";
|
|
2
|
+
|
|
3
|
+
export interface ProductServiceToken<TService> {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly __service?: TService;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ProductServiceFactoryContext {
|
|
9
|
+
productId: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ProductServiceFactory<TService> {
|
|
13
|
+
token: ProductServiceToken<TService>;
|
|
14
|
+
create(context: ProductServiceFactoryContext): TService;
|
|
15
|
+
dispose?(service: TService): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ProductServiceRegistry {
|
|
19
|
+
get<TService>(token: ProductServiceToken<TService>): TService | null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ProductServiceRegistryRuntime extends ProductServiceRegistry {
|
|
23
|
+
register<TService>(
|
|
24
|
+
factory: ProductServiceFactory<TService>,
|
|
25
|
+
context: ProductServiceFactoryContext,
|
|
26
|
+
): TService;
|
|
27
|
+
dispose(): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ProductAssetResolverService {
|
|
31
|
+
resolveAssetUrl(editor: Editor, assetId: AssetId): string | null;
|
|
32
|
+
preloadForEditor?(editor: Editor): Promise<void>;
|
|
33
|
+
cleanupAfterRestore?(editor: Editor): Promise<void> | void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createProductServiceToken<TService>(
|
|
37
|
+
id: string,
|
|
38
|
+
): ProductServiceToken<TService> {
|
|
39
|
+
return { id };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createProductServiceRegistry({
|
|
43
|
+
factories,
|
|
44
|
+
productId,
|
|
45
|
+
}: {
|
|
46
|
+
factories: readonly ProductServiceFactory<unknown>[];
|
|
47
|
+
productId: string;
|
|
48
|
+
}): ProductServiceRegistryRuntime {
|
|
49
|
+
const services = new Map<string, unknown>();
|
|
50
|
+
const disposers: Array<() => void> = [];
|
|
51
|
+
|
|
52
|
+
const register = <TService>(
|
|
53
|
+
factory: ProductServiceFactory<TService>,
|
|
54
|
+
context: ProductServiceFactoryContext,
|
|
55
|
+
): TService => {
|
|
56
|
+
const existing = services.get(factory.token.id) as TService | undefined;
|
|
57
|
+
if (existing) {
|
|
58
|
+
return existing;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const service = factory.create(context);
|
|
62
|
+
services.set(factory.token.id, service);
|
|
63
|
+
if (factory.dispose) {
|
|
64
|
+
disposers.push(() => factory.dispose?.(service));
|
|
65
|
+
}
|
|
66
|
+
return service;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
for (const factory of factories) {
|
|
70
|
+
register(factory, { productId });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
register,
|
|
75
|
+
get<TService>(token: ProductServiceToken<TService>): TService | null {
|
|
76
|
+
return (services.get(token.id) as TService | undefined) ?? null;
|
|
77
|
+
},
|
|
78
|
+
dispose() {
|
|
79
|
+
for (const dispose of [...disposers].reverse()) {
|
|
80
|
+
dispose();
|
|
81
|
+
}
|
|
82
|
+
services.clear();
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function requireProductService<TService>(
|
|
88
|
+
services: ProductServiceRegistry,
|
|
89
|
+
token: ProductServiceToken<TService>,
|
|
90
|
+
): TService {
|
|
91
|
+
const service = services.get(token);
|
|
92
|
+
if (!service) {
|
|
93
|
+
throw new Error(`Missing product service: ${token.id}`);
|
|
94
|
+
}
|
|
95
|
+
return service;
|
|
96
|
+
}
|