@widgetic/canvas 0.5.34 → 0.5.36
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 +105 -36
- package/dist/canvas/DomCanvas.svelte +454 -0
- package/dist/canvas/DomCanvas.svelte.d.ts +222 -0
- package/dist/canvas/autosaveScheduler.d.ts +58 -0
- package/dist/canvas/autosaveScheduler.js +157 -0
- package/dist/canvas/boardFrame.d.ts +13 -0
- package/dist/canvas/boardFrame.js +13 -0
- package/dist/canvas/boardSurface.d.ts +9 -0
- package/dist/canvas/boardSurface.js +9 -0
- package/dist/canvas/chromeLayout.d.ts +76 -0
- package/dist/canvas/chromeLayout.js +121 -0
- package/dist/canvas/emptyBoardHint.d.ts +17 -0
- package/dist/canvas/emptyBoardHint.js +12 -0
- package/dist/canvas/eventScope.d.ts +1 -1
- package/dist/canvas/eventScope.js +1 -1
- package/dist/canvas/host/CanvasHost.d.ts +351 -0
- package/dist/canvas/host/CanvasHost.js +1740 -0
- package/dist/canvas/host/CanvasHostApi.d.ts +30 -0
- package/dist/canvas/host/CanvasHostApi.js +95 -0
- package/dist/canvas/host/browserServices.d.ts +46 -0
- package/dist/canvas/host/browserServices.js +167 -0
- package/dist/canvas/host/conversion.d.ts +51 -0
- package/dist/canvas/host/conversion.js +123 -0
- package/dist/canvas/host/hostObject.d.ts +40 -0
- package/dist/canvas/host/hostObject.js +93 -0
- package/dist/canvas/host/pageEvents.d.ts +24 -0
- package/dist/canvas/host/pageEvents.js +35 -0
- package/dist/canvas/host/screenRects.d.ts +66 -0
- package/dist/canvas/host/screenRects.js +102 -0
- package/dist/canvas/host/strokeSnap.d.ts +67 -0
- package/dist/canvas/host/strokeSnap.js +165 -0
- package/dist/canvas/index.d.ts +9 -7
- package/dist/canvas/index.js +9 -7
- package/dist/canvas/openMarks.d.ts +17 -0
- package/dist/canvas/openMarks.js +26 -0
- package/dist/canvas/panZoomPosition.d.ts +15 -0
- package/dist/canvas/panZoomPosition.js +16 -0
- package/dist/canvas/preferredTextFont.d.ts +12 -0
- package/dist/canvas/preferredTextFont.js +38 -0
- package/dist/canvas/shapes/ShapeLibrary.d.ts +1 -1
- package/dist/canvas/shapes/ShapeLibrary.js +13 -1
- package/dist/canvas/shapes/strokeShapeSnap.d.ts +14 -12
- package/dist/canvas/shapes/strokeShapeSnap.js +10 -8
- package/dist/canvas/toolbarPosition.d.ts +24 -0
- package/dist/canvas/toolbarPosition.js +24 -0
- package/dist/canvas/types.d.ts +14 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte +173 -0
- package/dist/canvas/ui/CanvasContextMenu.svelte.d.ts +12 -0
- package/dist/canvas/ui/CanvasHint.svelte +20 -0
- package/dist/canvas/ui/CanvasHint.svelte.d.ts +18 -0
- package/dist/canvas/ui/CanvasToolbar.svelte +95 -0
- package/dist/canvas/ui/CanvasToolbar.svelte.d.ts +14 -0
- package/dist/canvas/ui/ColorField.svelte +155 -0
- package/dist/canvas/ui/ColorField.svelte.d.ts +14 -0
- package/dist/canvas/ui/LoadNotice.svelte +39 -0
- package/dist/canvas/ui/LoadNotice.svelte.d.ts +8 -0
- package/dist/canvas/ui/SelectionPanel.svelte +300 -0
- package/dist/canvas/ui/SelectionPanel.svelte.d.ts +20 -0
- package/dist/canvas/ui/SelectorBar.svelte +82 -0
- package/dist/canvas/ui/SelectorBar.svelte.d.ts +12 -0
- package/dist/canvas/ui/ShapesMenu.svelte +185 -0
- package/dist/canvas/ui/ShapesMenu.svelte.d.ts +15 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte +124 -0
- package/dist/canvas/ui/ShortcutsPanel.svelte.d.ts +7 -0
- package/dist/canvas/ui/ToolButton.svelte +78 -0
- package/dist/canvas/ui/ToolButton.svelte.d.ts +20 -0
- package/dist/canvas/ui/WidgetActions.svelte +152 -0
- package/dist/canvas/ui/WidgetActions.svelte.d.ts +19 -0
- package/dist/canvas/ui/ZoomControl.svelte +89 -0
- package/dist/canvas/ui/ZoomControl.svelte.d.ts +18 -0
- package/dist/canvas/ui/colorModel.d.ts +18 -0
- package/dist/canvas/ui/colorModel.js +85 -0
- package/dist/canvas/ui/colorPickerMessages.d.ts +5 -0
- package/dist/canvas/ui/colorPickerMessages.js +15 -0
- package/dist/canvas/ui/floatingCard.d.ts +107 -0
- package/dist/canvas/ui/floatingCard.js +107 -0
- package/dist/canvas/ui/floatingLayer.d.ts +21 -0
- package/dist/canvas/ui/floatingLayer.js +52 -0
- package/dist/canvas/ui/icons.d.ts +12 -0
- package/dist/canvas/ui/icons.js +7 -0
- package/dist/canvas/ui/keyHints.d.ts +27 -0
- package/dist/canvas/ui/keyHints.js +43 -0
- package/dist/canvas/ui/popoverSide.d.ts +5 -0
- package/dist/canvas/ui/popoverSide.js +20 -0
- package/dist/canvas/ui/selectionTraits.d.ts +61 -0
- package/dist/canvas/ui/selectionTraits.js +99 -0
- package/dist/canvas/view/CanvasStage.svelte +132 -0
- package/dist/canvas/view/CanvasStage.svelte.d.ts +21 -0
- package/dist/canvas/view/EraserTrail.svelte +50 -0
- package/dist/canvas/view/EraserTrail.svelte.d.ts +9 -0
- package/dist/canvas/view/ObjectView.svelte +245 -0
- package/dist/canvas/view/ObjectView.svelte.d.ts +21 -0
- package/dist/canvas/view/SelectionOverlay.svelte +487 -0
- package/dist/canvas/view/SelectionOverlay.svelte.d.ts +17 -0
- package/dist/canvas/view/TextEditor.svelte +76 -0
- package/dist/canvas/view/TextEditor.svelte.d.ts +9 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte +83 -0
- package/dist/canvas/view/WidgetChromeLayer.svelte.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.d.ts +13 -0
- package/dist/canvas/view/erasingMarks.js +34 -0
- package/dist/canvas/view/geometry.d.ts +169 -0
- package/dist/canvas/view/geometry.js +272 -0
- package/dist/components/CanvasBoardPlaceholder.svelte +5 -3
- package/dist/components/CanvasBoardPlaceholder.svelte.d.ts +3 -3
- package/dist/components/index.js +1 -1
- package/dist/components/input-controls/ColorIC.svelte +2 -2
- package/dist/engine/EngineCore.d.ts +273 -0
- package/dist/engine/EngineCore.js +1051 -0
- package/dist/engine/codec/canvasDocument.d.ts +57 -0
- package/dist/engine/codec/canvasDocument.js +471 -0
- package/dist/engine/codec/hostReadableKeys.d.ts +25 -0
- package/dist/engine/codec/hostReadableKeys.js +25 -0
- package/dist/engine/core/arrange.d.ts +20 -0
- package/dist/engine/core/arrange.js +81 -0
- package/dist/engine/core/arrow.d.ts +91 -0
- package/dist/engine/core/arrow.js +182 -0
- package/dist/engine/core/brush.d.ts +68 -0
- package/dist/engine/core/brush.js +173 -0
- package/dist/engine/core/defaults.d.ts +70 -0
- package/dist/engine/core/defaults.js +158 -0
- package/dist/engine/core/freehand.d.ts +130 -0
- package/dist/engine/core/freehand.js +430 -0
- package/dist/engine/core/geometry.d.ts +44 -0
- package/dist/engine/core/geometry.js +216 -0
- package/dist/engine/core/history.d.ts +39 -0
- package/dist/engine/core/history.js +69 -0
- package/dist/engine/core/hitTest.d.ts +54 -0
- package/dist/engine/core/hitTest.js +298 -0
- package/dist/engine/core/matrix.d.ts +47 -0
- package/dist/engine/core/matrix.js +101 -0
- package/dist/engine/core/pathData.d.ts +50 -0
- package/dist/engine/core/pathData.js +550 -0
- package/dist/engine/core/rotationReset.d.ts +10 -0
- package/dist/engine/core/rotationReset.js +17 -0
- package/dist/engine/core/scene.d.ts +99 -0
- package/dist/engine/core/scene.js +450 -0
- package/dist/engine/core/scribble.d.ts +64 -0
- package/dist/engine/core/scribble.js +121 -0
- package/dist/engine/core/shapePaths.d.ts +34 -0
- package/dist/engine/core/shapePaths.js +69 -0
- package/dist/engine/core/shapeTools.d.ts +80 -0
- package/dist/engine/core/shapeTools.js +161 -0
- package/dist/engine/core/snapping.d.ts +64 -0
- package/dist/engine/core/snapping.js +164 -0
- package/dist/engine/core/style.d.ts +40 -0
- package/dist/engine/core/style.js +151 -0
- package/dist/engine/core/transform.d.ts +79 -0
- package/dist/engine/core/transform.js +258 -0
- package/dist/engine/core/types.d.ts +238 -0
- package/dist/engine/core/types.js +31 -0
- package/dist/engine/core/viewport.d.ts +32 -0
- package/dist/engine/core/viewport.js +84 -0
- package/dist/engine/input/PointerController.d.ts +38 -0
- package/dist/engine/input/PointerController.js +545 -0
- package/dist/engine/input/arrowHandles.d.ts +38 -0
- package/dist/engine/input/arrowHandles.js +60 -0
- package/dist/engine/input/gestures.d.ts +79 -0
- package/dist/engine/input/gestures.js +769 -0
- package/dist/engine/input/hoverCursor.d.ts +29 -0
- package/dist/engine/input/hoverCursor.js +23 -0
- package/dist/engine/input/keyboard.d.ts +75 -0
- package/dist/engine/input/keyboard.js +264 -0
- package/dist/engine/input/selectionHandles.d.ts +89 -0
- package/dist/engine/input/selectionHandles.js +169 -0
- package/dist/engine/input/types.d.ts +82 -0
- package/dist/engine/input/types.js +5 -0
- package/dist/engine/render/rasterize.d.ts +74 -0
- package/dist/engine/render/rasterize.js +273 -0
- package/dist/engine/state.d.ts +124 -0
- package/dist/engine/state.js +47 -0
- package/dist/engine/text/layoutText.d.ts +53 -0
- package/dist/engine/text/layoutText.js +170 -0
- package/dist/engine/widgets.d.ts +124 -0
- package/dist/engine/widgets.js +345 -0
- package/dist/i18n/labels.d.ts +4 -15
- package/dist/i18n/labels.js +33 -53
- package/dist/i18n/language.d.ts +3 -3
- package/dist/i18n/messages.d.ts +51 -122
- package/dist/i18n/messages.js +58 -131
- package/dist/i18n/shortcuts.d.ts +0 -1
- package/dist/i18n/shortcuts.js +2 -11
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/package.json +30 -5
- package/dist/canvas/Canvas.svelte +0 -13299
- package/dist/canvas/Canvas.svelte.d.ts +0 -281
- package/dist/canvas/CanvasToolbar.svelte +0 -1498
- package/dist/canvas/CanvasToolbar.svelte.d.ts +0 -69
- package/dist/canvas/ContextMenu.svelte +0 -305
- package/dist/canvas/ContextMenu.svelte.d.ts +0 -42
- package/dist/canvas/PanZoomPanel.svelte +0 -478
- package/dist/canvas/PanZoomPanel.svelte.d.ts +0 -38
- package/dist/canvas/WidgetSelectionLayer.svelte +0 -156
- package/dist/canvas/WidgetSelectionLayer.svelte.d.ts +0 -11
- package/dist/canvas/props-panel/PropsPanel.svelte +0 -2075
- package/dist/canvas/props-panel/PropsPanel.svelte.d.ts +0 -84
- package/dist/canvas/props-panel/TextPropsICSection.svelte +0 -242
- package/dist/canvas/props-panel/TextPropsICSection.svelte.d.ts +0 -40
- package/dist/canvas/shadowRootText.d.ts +0 -5
- package/dist/canvas/shadowRootText.js +0 -43
- package/dist/canvas/shapes/FrameShape.d.ts +0 -97
- package/dist/canvas/shapes/FrameShape.js +0 -953
- package/dist/canvas/shapes/ImageShape.d.ts +0 -47
- package/dist/canvas/shapes/ImageShape.js +0 -339
- package/dist/canvas/shapes/WidgetShape.d.ts +0 -30
- package/dist/canvas/shapes/WidgetShape.js +0 -200
- package/dist/canvas/widgetPreviewGeometry.d.ts +0 -19
- package/dist/canvas/widgetPreviewGeometry.js +0 -66
- package/dist/i18n/shapeWords.d.ts +0 -30
- package/dist/i18n/shapeWords.js +0 -36
- package/dist/i18n/slots.d.ts +0 -20
- package/dist/i18n/slots.js +0 -40
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The functions of the canvas that the page calls (the widget creator, the site's canvas screen, the Shopify module).
|
|
3
|
+
*
|
|
4
|
+
* `DomCanvas.svelte` is the one public `Canvas` component. The functions below are what `bind:this` hands to a page, and
|
|
5
|
+
* every name in the list is a name a page already calls (frozen in `tests/fixtures/hostSurface.json`), so no call site
|
|
6
|
+
* changes. The shell exports each one by name and forwards it to the host; the type of each is the host method's own
|
|
7
|
+
* type, so a signature cannot drift between the two.
|
|
8
|
+
*
|
|
9
|
+
* Kept free of Svelte so that Node can test it: `tests/host/hostSurface.test.mjs` holds this list, the fixture and the
|
|
10
|
+
* shell to each other.
|
|
11
|
+
*/
|
|
12
|
+
import type { CanvasHost } from './CanvasHost.js';
|
|
13
|
+
/** Every function a page already calls, in the order the fixture lists them. */
|
|
14
|
+
export declare const CANVAS_CONTRACT_FUNCTION_NAMES: readonly ["shapeFillColor", "captureCanvasSketch", "absorbCanvasSketchIntoWidget", "deselectAll", "duplicateSelectedObjects", "deleteSelectedObjects", "markSelectedAsDuplicating", "linkObjectToWidget", "linkFrameToWidget", "replaceObjectWithWidgetImage", "updateWidgetImage", "selectWidgetShape", "listWidgetShapeScreenRects", "getWidgetShapeScreenRect", "getObjectScreenRect", "getWidgetShapeDataUrl", "setWidgetShapeLivePreviewHiddenImage", "addWidgetShape", "getLinkedWidgetIds", "keepOnlyWidgetShape", "hydrateWidgetShapeMetadata", "markOrphanedWidgetShapes", "onSelectSelectTool", "onSelectLibraryShapeTool", "onSelectEraseTool", "onSelectDrawTool", "onSelectTextTool", "onSelectArrowTool", "onSelectShapeTool", "onSelectFrameTool", "setPrimaryWidgetShape", "getPrimaryWidgetId", "setWidgetShapeBodyDragAllowed", "translateWidgetShapeByScreen", "commitWidgetShapeTranslate", "setWidgetShapeOverlayDragActive", "selectWidgetShapeById", "getSelectedWidgetShapeId", "getSelectedObject", "getHoveredWidgetShapeId", "onSelectImageTool", "onSelectVideoTool", "onSelectPanTool", "zoomIn", "zoomOut", "zoomTo", "zoomReset", "zoomFit", "onGroupSelection", "onUngroupSelection", "onSelectAllObjects", "onDeselectAllObjects", "flushAutosave", "saveNow", "undo", "redo", "saveCanvas", "loadCanvas", "onClearAllObjects", "addText", "addTriangle", "addRect", "addCircle", "addFrame", "addImage"];
|
|
15
|
+
/**
|
|
16
|
+
* Functions added to the contract. The widget creator's in-flight branches call it, and the simple-board branch
|
|
17
|
+
* (feat/shopify-feedback-canvas) adds the same one, so it keeps the name and the answer (how many objects went): removes
|
|
18
|
+
* the sketch objects around one widget card and keeps the card.
|
|
19
|
+
*/
|
|
20
|
+
export declare const CANVAS_ADDED_FUNCTION_NAMES: readonly ["removeSketchObjectsKeepingWidget"];
|
|
21
|
+
/** Every function the shell exports. */
|
|
22
|
+
export declare const CANVAS_EXPORTED_FUNCTION_NAMES: readonly ["shapeFillColor", "captureCanvasSketch", "absorbCanvasSketchIntoWidget", "deselectAll", "duplicateSelectedObjects", "deleteSelectedObjects", "markSelectedAsDuplicating", "linkObjectToWidget", "linkFrameToWidget", "replaceObjectWithWidgetImage", "updateWidgetImage", "selectWidgetShape", "listWidgetShapeScreenRects", "getWidgetShapeScreenRect", "getObjectScreenRect", "getWidgetShapeDataUrl", "setWidgetShapeLivePreviewHiddenImage", "addWidgetShape", "getLinkedWidgetIds", "keepOnlyWidgetShape", "hydrateWidgetShapeMetadata", "markOrphanedWidgetShapes", "onSelectSelectTool", "onSelectLibraryShapeTool", "onSelectEraseTool", "onSelectDrawTool", "onSelectTextTool", "onSelectArrowTool", "onSelectShapeTool", "onSelectFrameTool", "setPrimaryWidgetShape", "getPrimaryWidgetId", "setWidgetShapeBodyDragAllowed", "translateWidgetShapeByScreen", "commitWidgetShapeTranslate", "setWidgetShapeOverlayDragActive", "selectWidgetShapeById", "getSelectedWidgetShapeId", "getSelectedObject", "getHoveredWidgetShapeId", "onSelectImageTool", "onSelectVideoTool", "onSelectPanTool", "zoomIn", "zoomOut", "zoomTo", "zoomReset", "zoomFit", "onGroupSelection", "onUngroupSelection", "onSelectAllObjects", "onDeselectAllObjects", "flushAutosave", "saveNow", "undo", "redo", "saveCanvas", "loadCanvas", "onClearAllObjects", "addText", "addTriangle", "addRect", "addCircle", "addFrame", "addImage", "removeSketchObjectsKeepingWidget"];
|
|
23
|
+
export type CanvasExportedFunctionName = (typeof CANVAS_EXPORTED_FUNCTION_NAMES)[number];
|
|
24
|
+
/** What a page gets from `bind:this` besides the component's props: the host's own type of each function. */
|
|
25
|
+
export type CanvasHostApi = Pick<CanvasHost, CanvasExportedFunctionName>;
|
|
26
|
+
/**
|
|
27
|
+
* One exported function of the shell: it calls the host's method of that name, with the host as `this`, and has the
|
|
28
|
+
* method's own type. A page that holds the shell's reference calls it exactly as it calls the host's method.
|
|
29
|
+
*/
|
|
30
|
+
export declare function forwardToHost<Name extends CanvasExportedFunctionName>(host: CanvasHost, name: Name): CanvasHost[Name];
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The functions of the canvas that the page calls (the widget creator, the site's canvas screen, the Shopify module).
|
|
3
|
+
*
|
|
4
|
+
* `DomCanvas.svelte` is the one public `Canvas` component. The functions below are what `bind:this` hands to a page, and
|
|
5
|
+
* every name in the list is a name a page already calls (frozen in `tests/fixtures/hostSurface.json`), so no call site
|
|
6
|
+
* changes. The shell exports each one by name and forwards it to the host; the type of each is the host method's own
|
|
7
|
+
* type, so a signature cannot drift between the two.
|
|
8
|
+
*
|
|
9
|
+
* Kept free of Svelte so that Node can test it: `tests/host/hostSurface.test.mjs` holds this list, the fixture and the
|
|
10
|
+
* shell to each other.
|
|
11
|
+
*/
|
|
12
|
+
/** Every function a page already calls, in the order the fixture lists them. */
|
|
13
|
+
export const CANVAS_CONTRACT_FUNCTION_NAMES = [
|
|
14
|
+
'shapeFillColor',
|
|
15
|
+
'captureCanvasSketch',
|
|
16
|
+
'absorbCanvasSketchIntoWidget',
|
|
17
|
+
'deselectAll',
|
|
18
|
+
'duplicateSelectedObjects',
|
|
19
|
+
'deleteSelectedObjects',
|
|
20
|
+
'markSelectedAsDuplicating',
|
|
21
|
+
'linkObjectToWidget',
|
|
22
|
+
'linkFrameToWidget',
|
|
23
|
+
'replaceObjectWithWidgetImage',
|
|
24
|
+
'updateWidgetImage',
|
|
25
|
+
'selectWidgetShape',
|
|
26
|
+
'listWidgetShapeScreenRects',
|
|
27
|
+
'getWidgetShapeScreenRect',
|
|
28
|
+
'getObjectScreenRect',
|
|
29
|
+
'getWidgetShapeDataUrl',
|
|
30
|
+
'setWidgetShapeLivePreviewHiddenImage',
|
|
31
|
+
'addWidgetShape',
|
|
32
|
+
'getLinkedWidgetIds',
|
|
33
|
+
'keepOnlyWidgetShape',
|
|
34
|
+
'hydrateWidgetShapeMetadata',
|
|
35
|
+
'markOrphanedWidgetShapes',
|
|
36
|
+
'onSelectSelectTool',
|
|
37
|
+
'onSelectLibraryShapeTool',
|
|
38
|
+
'onSelectEraseTool',
|
|
39
|
+
'onSelectDrawTool',
|
|
40
|
+
'onSelectTextTool',
|
|
41
|
+
'onSelectArrowTool',
|
|
42
|
+
'onSelectShapeTool',
|
|
43
|
+
'onSelectFrameTool',
|
|
44
|
+
'setPrimaryWidgetShape',
|
|
45
|
+
'getPrimaryWidgetId',
|
|
46
|
+
'setWidgetShapeBodyDragAllowed',
|
|
47
|
+
'translateWidgetShapeByScreen',
|
|
48
|
+
'commitWidgetShapeTranslate',
|
|
49
|
+
'setWidgetShapeOverlayDragActive',
|
|
50
|
+
'selectWidgetShapeById',
|
|
51
|
+
'getSelectedWidgetShapeId',
|
|
52
|
+
'getSelectedObject',
|
|
53
|
+
'getHoveredWidgetShapeId',
|
|
54
|
+
'onSelectImageTool',
|
|
55
|
+
'onSelectVideoTool',
|
|
56
|
+
'onSelectPanTool',
|
|
57
|
+
'zoomIn',
|
|
58
|
+
'zoomOut',
|
|
59
|
+
'zoomTo',
|
|
60
|
+
'zoomReset',
|
|
61
|
+
'zoomFit',
|
|
62
|
+
'onGroupSelection',
|
|
63
|
+
'onUngroupSelection',
|
|
64
|
+
'onSelectAllObjects',
|
|
65
|
+
'onDeselectAllObjects',
|
|
66
|
+
'flushAutosave',
|
|
67
|
+
'saveNow',
|
|
68
|
+
'undo',
|
|
69
|
+
'redo',
|
|
70
|
+
'saveCanvas',
|
|
71
|
+
'loadCanvas',
|
|
72
|
+
'onClearAllObjects',
|
|
73
|
+
'addText',
|
|
74
|
+
'addTriangle',
|
|
75
|
+
'addRect',
|
|
76
|
+
'addCircle',
|
|
77
|
+
'addFrame',
|
|
78
|
+
'addImage'
|
|
79
|
+
];
|
|
80
|
+
/**
|
|
81
|
+
* Functions added to the contract. The widget creator's in-flight branches call it, and the simple-board branch
|
|
82
|
+
* (feat/shopify-feedback-canvas) adds the same one, so it keeps the name and the answer (how many objects went): removes
|
|
83
|
+
* the sketch objects around one widget card and keeps the card.
|
|
84
|
+
*/
|
|
85
|
+
export const CANVAS_ADDED_FUNCTION_NAMES = ['removeSketchObjectsKeepingWidget'];
|
|
86
|
+
/** Every function the shell exports. */
|
|
87
|
+
export const CANVAS_EXPORTED_FUNCTION_NAMES = [...CANVAS_CONTRACT_FUNCTION_NAMES, ...CANVAS_ADDED_FUNCTION_NAMES];
|
|
88
|
+
/**
|
|
89
|
+
* One exported function of the shell: it calls the host's method of that name, with the host as `this`, and has the
|
|
90
|
+
* method's own type. A page that holds the shell's reference calls it exactly as it calls the host's method.
|
|
91
|
+
*/
|
|
92
|
+
export function forwardToHost(host, name) {
|
|
93
|
+
const forwarded = (...values) => host[name].apply(host, values);
|
|
94
|
+
return forwarded;
|
|
95
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the canvas borrows from the browser: measuring a piece of text, making an offscreen canvas, loading a
|
|
3
|
+
* picture, choosing files. Everything that touches `document` or `window` and is not part of the engine lives here,
|
|
4
|
+
* so the engine and the host logic run in Node with plain stand-ins.
|
|
5
|
+
*
|
|
6
|
+
* Every function checks for the browser itself and answers with the safest fallback when it is not there (the page
|
|
7
|
+
* is rendered on the server first, and nothing here may throw then).
|
|
8
|
+
*/
|
|
9
|
+
import type { MeasureText } from '../../engine/text/layoutText.js';
|
|
10
|
+
import type { RasterServices } from '../../engine/render/rasterize.js';
|
|
11
|
+
/** A text measurer that uses the browser's canvas, or a fixed advance where there is none (the server, a test). */
|
|
12
|
+
export declare function createBrowserMeasureText(): MeasureText;
|
|
13
|
+
/** The picture behind an address, decoded. Null when it cannot be loaded. */
|
|
14
|
+
export declare function loadImageElement(source: string): Promise<HTMLImageElement | null>;
|
|
15
|
+
/** The services the rasterizer needs, made of the browser. */
|
|
16
|
+
export declare function createBrowserRasterServices(measureText: MeasureText): RasterServices;
|
|
17
|
+
/** Whether the person's keyboard says Cmd rather than Ctrl. */
|
|
18
|
+
export declare function isApplePlatform(): boolean;
|
|
19
|
+
/** A picture the person chose, ready to be put on the board. */
|
|
20
|
+
export interface LoadedImageFile {
|
|
21
|
+
src: string;
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
fileName: string;
|
|
25
|
+
}
|
|
26
|
+
/** The longest side a picture keeps when it is large; a bigger one is scaled down before it is stored in the board. */
|
|
27
|
+
export declare const IMAGE_MAX_SIDE = 2048;
|
|
28
|
+
/** Pictures smaller than this are stored as they came. */
|
|
29
|
+
export declare const IMAGE_RESIZE_THRESHOLD_BYTES: number;
|
|
30
|
+
/** What a picture without a size of its own (an SVG with no width and height) is given. */
|
|
31
|
+
export declare const IMAGE_FALLBACK_SIDE = 300;
|
|
32
|
+
/**
|
|
33
|
+
* Reads a picture file (or a pasted picture) into a data address and measures it. A large photo is scaled down to
|
|
34
|
+
* `IMAGE_MAX_SIDE` so the saved board does not grow by megabytes; small files, and every SVG, are kept as they are.
|
|
35
|
+
* Rejects when the file is not a picture the browser can show.
|
|
36
|
+
*/
|
|
37
|
+
export declare function readImageFile(file: File | Blob, fileName?: string): Promise<LoadedImageFile>;
|
|
38
|
+
/**
|
|
39
|
+
* Opens the browser's file chooser for pictures and answers with what was chosen (an empty list when the person
|
|
40
|
+
* cancels). Must be called while a click or a key press is being handled, or the browser refuses to open the chooser.
|
|
41
|
+
* The hidden input goes into the shadow root the canvas is in, so a partner's own page never sees it.
|
|
42
|
+
*/
|
|
43
|
+
export declare function chooseImageFiles(scope: Node | null | undefined, options?: {
|
|
44
|
+
multiple?: boolean;
|
|
45
|
+
accept?: string;
|
|
46
|
+
}): Promise<File[]>;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the canvas borrows from the browser: measuring a piece of text, making an offscreen canvas, loading a
|
|
3
|
+
* picture, choosing files. Everything that touches `document` or `window` and is not part of the engine lives here,
|
|
4
|
+
* so the engine and the host logic run in Node with plain stand-ins.
|
|
5
|
+
*
|
|
6
|
+
* Every function checks for the browser itself and answers with the safest fallback when it is not there (the page
|
|
7
|
+
* is rendered on the server first, and nothing here may throw then).
|
|
8
|
+
*/
|
|
9
|
+
import { createFixedAdvanceMeasure } from '../../engine/text/layoutText.js';
|
|
10
|
+
import { getTransientHostFor } from '../eventScope.js';
|
|
11
|
+
const hasDocument = () => typeof document !== 'undefined' && typeof document.createElement === 'function';
|
|
12
|
+
/** A text measurer that uses the browser's canvas, or a fixed advance where there is none (the server, a test). */
|
|
13
|
+
export function createBrowserMeasureText() {
|
|
14
|
+
if (!hasDocument())
|
|
15
|
+
return createFixedAdvanceMeasure();
|
|
16
|
+
let context;
|
|
17
|
+
const fallback = createFixedAdvanceMeasure();
|
|
18
|
+
return (text, font) => {
|
|
19
|
+
if (context === undefined) {
|
|
20
|
+
try {
|
|
21
|
+
context = document.createElement('canvas').getContext('2d');
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
context = null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (!context)
|
|
28
|
+
return fallback(text, font);
|
|
29
|
+
if (context.font !== font)
|
|
30
|
+
context.font = font;
|
|
31
|
+
return context.measureText(text).width;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** The picture behind an address, decoded. Null when it cannot be loaded. */
|
|
35
|
+
export function loadImageElement(source) {
|
|
36
|
+
if (typeof Image === 'undefined')
|
|
37
|
+
return Promise.resolve(null);
|
|
38
|
+
return new Promise((resolve) => {
|
|
39
|
+
const image = new Image();
|
|
40
|
+
// A picture from another site is only usable in a canvas when that site allows it; a data address needs nothing.
|
|
41
|
+
if (/^https?:/i.test(source))
|
|
42
|
+
image.crossOrigin = 'anonymous';
|
|
43
|
+
image.onload = () => {
|
|
44
|
+
if (typeof image.decode === 'function') {
|
|
45
|
+
image.decode().then(() => resolve(image), () => resolve(image.naturalWidth > 0 ? image : null));
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
resolve(image);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
image.onerror = () => resolve(null);
|
|
52
|
+
image.src = source;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/** The services the rasterizer needs, made of the browser. */
|
|
56
|
+
export function createBrowserRasterServices(measureText) {
|
|
57
|
+
return {
|
|
58
|
+
createCanvas(width, height) {
|
|
59
|
+
if (!hasDocument())
|
|
60
|
+
return null;
|
|
61
|
+
try {
|
|
62
|
+
const canvas = document.createElement('canvas');
|
|
63
|
+
canvas.width = width;
|
|
64
|
+
canvas.height = height;
|
|
65
|
+
return canvas;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
loadImage: loadImageElement,
|
|
72
|
+
createPath: (data) => new Path2D(data),
|
|
73
|
+
measureText
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** Whether the person's keyboard says Cmd rather than Ctrl. */
|
|
77
|
+
export function isApplePlatform() {
|
|
78
|
+
if (typeof navigator === 'undefined')
|
|
79
|
+
return false;
|
|
80
|
+
const platform = navigator.userAgentData?.platform || navigator.platform || navigator.userAgent || '';
|
|
81
|
+
return /Mac|iPhone|iPad|iPod/i.test(platform);
|
|
82
|
+
}
|
|
83
|
+
/** The longest side a picture keeps when it is large; a bigger one is scaled down before it is stored in the board. */
|
|
84
|
+
export const IMAGE_MAX_SIDE = 2048;
|
|
85
|
+
/** Pictures smaller than this are stored as they came. */
|
|
86
|
+
export const IMAGE_RESIZE_THRESHOLD_BYTES = 2.5 * 1024 * 1024;
|
|
87
|
+
/** What a picture without a size of its own (an SVG with no width and height) is given. */
|
|
88
|
+
export const IMAGE_FALLBACK_SIDE = 300;
|
|
89
|
+
function readAsDataUrl(file) {
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
const reader = new FileReader();
|
|
92
|
+
reader.onload = () => (typeof reader.result === 'string' ? resolve(reader.result) : reject(new Error('The file could not be read')));
|
|
93
|
+
reader.onerror = () => reject(reader.error ?? new Error('The file could not be read'));
|
|
94
|
+
reader.readAsDataURL(file);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Reads a picture file (or a pasted picture) into a data address and measures it. A large photo is scaled down to
|
|
99
|
+
* `IMAGE_MAX_SIDE` so the saved board does not grow by megabytes; small files, and every SVG, are kept as they are.
|
|
100
|
+
* Rejects when the file is not a picture the browser can show.
|
|
101
|
+
*/
|
|
102
|
+
export async function readImageFile(file, fileName = '') {
|
|
103
|
+
const name = fileName || file.name || '';
|
|
104
|
+
const dataUrl = await readAsDataUrl(file);
|
|
105
|
+
const image = await loadImageElement(dataUrl);
|
|
106
|
+
if (!image)
|
|
107
|
+
throw new Error('The file is not a picture the browser can show');
|
|
108
|
+
let width = image.naturalWidth || image.width || IMAGE_FALLBACK_SIDE;
|
|
109
|
+
let height = image.naturalHeight || image.height || IMAGE_FALLBACK_SIDE;
|
|
110
|
+
let src = dataUrl;
|
|
111
|
+
const isSvg = file.type === 'image/svg+xml' || /\.svg$/i.test(name);
|
|
112
|
+
const tooBig = Math.max(width, height) > IMAGE_MAX_SIDE;
|
|
113
|
+
if (!isSvg && tooBig && file.size > IMAGE_RESIZE_THRESHOLD_BYTES && hasDocument()) {
|
|
114
|
+
try {
|
|
115
|
+
const scale = IMAGE_MAX_SIDE / Math.max(width, height);
|
|
116
|
+
const scaledWidth = Math.max(1, Math.round(width * scale));
|
|
117
|
+
const scaledHeight = Math.max(1, Math.round(height * scale));
|
|
118
|
+
const canvas = document.createElement('canvas');
|
|
119
|
+
canvas.width = scaledWidth;
|
|
120
|
+
canvas.height = scaledHeight;
|
|
121
|
+
const context = canvas.getContext('2d');
|
|
122
|
+
if (context) {
|
|
123
|
+
context.drawImage(image, 0, 0, scaledWidth, scaledHeight);
|
|
124
|
+
// Pictures that can have see-through parts stay PNG; photos become JPEG.
|
|
125
|
+
const keepsAlpha = /png|gif|webp/i.test(file.type);
|
|
126
|
+
src = canvas.toDataURL(keepsAlpha ? 'image/png' : 'image/jpeg', 0.88);
|
|
127
|
+
width = scaledWidth;
|
|
128
|
+
height = scaledHeight;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
console.warn('Canvas: the picture could not be scaled down, it is stored as it came', error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { src, width, height, fileName: name };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Opens the browser's file chooser for pictures and answers with what was chosen (an empty list when the person
|
|
139
|
+
* cancels). Must be called while a click or a key press is being handled, or the browser refuses to open the chooser.
|
|
140
|
+
* The hidden input goes into the shadow root the canvas is in, so a partner's own page never sees it.
|
|
141
|
+
*/
|
|
142
|
+
export function chooseImageFiles(scope, options = {}) {
|
|
143
|
+
if (!hasDocument())
|
|
144
|
+
return Promise.resolve([]);
|
|
145
|
+
return new Promise((resolve) => {
|
|
146
|
+
const input = document.createElement('input');
|
|
147
|
+
input.type = 'file';
|
|
148
|
+
// Every kind of picture by default; the SVG entry of the shapes menu asks for vector files only.
|
|
149
|
+
input.accept = options.accept ?? 'image/*,.svg';
|
|
150
|
+
input.multiple = !!options.multiple;
|
|
151
|
+
input.style.display = 'none';
|
|
152
|
+
const host = getTransientHostFor(scope);
|
|
153
|
+
let settled = false;
|
|
154
|
+
const finish = (files) => {
|
|
155
|
+
if (settled)
|
|
156
|
+
return;
|
|
157
|
+
settled = true;
|
|
158
|
+
input.remove();
|
|
159
|
+
resolve(files);
|
|
160
|
+
};
|
|
161
|
+
input.addEventListener('change', () => finish(Array.from(input.files ?? [])));
|
|
162
|
+
// The browsers that say so tell us when the chooser was closed without a file.
|
|
163
|
+
input.addEventListener('cancel', () => finish([]));
|
|
164
|
+
host.appendChild(input);
|
|
165
|
+
input.click();
|
|
166
|
+
});
|
|
167
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the board tells the host when a person converts a selection into a widget, or captures it.
|
|
3
|
+
*
|
|
4
|
+
* Both start from the same thing: the objects that are selected, a screenshot of just them, and the rectangle
|
|
5
|
+
* they cover. The host answers a conversion later (it makes a widget first), by the id this payload carried, so
|
|
6
|
+
* a selection of several objects, which has no id of its own, is remembered under a new one (the registry): by
|
|
7
|
+
* the time the answer comes the person may have clicked elsewhere, and the answer must still replace exactly
|
|
8
|
+
* those objects.
|
|
9
|
+
*
|
|
10
|
+
* Plain TypeScript over the engine's records and the rasterizer: runs in Node.
|
|
11
|
+
*/
|
|
12
|
+
import type { Rect, SceneObject, ShapeObject } from '../../engine/core/types.js';
|
|
13
|
+
import type { EngineCore } from '../../engine/EngineCore.js';
|
|
14
|
+
import type { RasterServices } from '../../engine/render/rasterize.js';
|
|
15
|
+
import { ConversionRegistry } from '../../engine/widgets.js';
|
|
16
|
+
import type { ObjectClickPayload, WidgetConversionPayload } from '../types.js';
|
|
17
|
+
/** The background of a screenshot: opaque, so a model that reads a transparent PNG as black still sees the drawing. */
|
|
18
|
+
export declare const SCREENSHOT_BACKGROUND = "#ffffff";
|
|
19
|
+
/** A screenshot is twice the size of the board (as the canvas it replaces made them), so small text stays legible. */
|
|
20
|
+
export declare const SCREENSHOT_SCALE = 2;
|
|
21
|
+
export interface SelectionDescription {
|
|
22
|
+
/** The selected units: top-level objects, or members of the group that was entered. */
|
|
23
|
+
units: SceneObject[];
|
|
24
|
+
/** Every shape the units draw (the members of a group, not the group). */
|
|
25
|
+
shapes: ShapeObject[];
|
|
26
|
+
/** The rectangle the shapes cover, strokes included. */
|
|
27
|
+
bounds: Rect;
|
|
28
|
+
/** The name the host is given: the kind of the one object, or `activeSelection` for several. */
|
|
29
|
+
name: string;
|
|
30
|
+
/** The kinds of the shapes, each once. */
|
|
31
|
+
shapeTypes: string[];
|
|
32
|
+
hasImages: boolean;
|
|
33
|
+
hasText: boolean;
|
|
34
|
+
}
|
|
35
|
+
/** What is selected, as the conversion and the capture see it. Null when nothing is. */
|
|
36
|
+
export declare function describeSelection(engine: EngineCore): SelectionDescription | null;
|
|
37
|
+
export interface ScreenshotDeps {
|
|
38
|
+
engine: EngineCore;
|
|
39
|
+
raster: RasterServices;
|
|
40
|
+
/** The label of a card whose widget has no name yet (the words come from the host's language). */
|
|
41
|
+
widgetFallbackName: string;
|
|
42
|
+
}
|
|
43
|
+
/** A PNG of some shapes on a plain background, cropped to what they cover. Empty when there is nothing to draw. */
|
|
44
|
+
export declare function screenshotOfShapes(shapes: readonly ShapeObject[], deps: ScreenshotDeps): Promise<string>;
|
|
45
|
+
/** What the host needs to turn a selection into a widget, or null when nothing is selected. */
|
|
46
|
+
export declare function buildConversionPayload(deps: ScreenshotDeps & {
|
|
47
|
+
conversions: ConversionRegistry;
|
|
48
|
+
untitledShapeName: string;
|
|
49
|
+
}): Promise<WidgetConversionPayload | null>;
|
|
50
|
+
/** What the host is given when a selection is captured (the "capture" button of the selection panel). */
|
|
51
|
+
export declare function buildCapturePayload(deps: ScreenshotDeps): Promise<ObjectClickPayload | null>;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the board tells the host when a person converts a selection into a widget, or captures it.
|
|
3
|
+
*
|
|
4
|
+
* Both start from the same thing: the objects that are selected, a screenshot of just them, and the rectangle
|
|
5
|
+
* they cover. The host answers a conversion later (it makes a widget first), by the id this payload carried, so
|
|
6
|
+
* a selection of several objects, which has no id of its own, is remembered under a new one (the registry): by
|
|
7
|
+
* the time the answer comes the person may have clicked elsewhere, and the answer must still replace exactly
|
|
8
|
+
* those objects.
|
|
9
|
+
*
|
|
10
|
+
* Plain TypeScript over the engine's records and the rasterizer: runs in Node.
|
|
11
|
+
*/
|
|
12
|
+
import { leafShapesOf } from '../../engine/core/scene.js';
|
|
13
|
+
import { rasterBoundsOf, renderShapesToDataUrl } from '../../engine/render/rasterize.js';
|
|
14
|
+
import { ConversionRegistry, selectionConversionBounds } from '../../engine/widgets.js';
|
|
15
|
+
import { hostCustomTypeOf, hostObjectById, hostTypeOf, selectedHostObject } from './hostObject.js';
|
|
16
|
+
/** The background of a screenshot: opaque, so a model that reads a transparent PNG as black still sees the drawing. */
|
|
17
|
+
export const SCREENSHOT_BACKGROUND = '#ffffff';
|
|
18
|
+
/** A screenshot is twice the size of the board (as the canvas it replaces made them), so small text stays legible. */
|
|
19
|
+
export const SCREENSHOT_SCALE = 2;
|
|
20
|
+
/** What is selected, as the conversion and the capture see it. Null when nothing is. */
|
|
21
|
+
export function describeSelection(engine) {
|
|
22
|
+
const { selectedIds } = engine.state;
|
|
23
|
+
if (selectedIds.length === 0)
|
|
24
|
+
return null;
|
|
25
|
+
const units = engine.selectedObjects();
|
|
26
|
+
const shapes = leafShapesOf(engine.state.scene, selectedIds);
|
|
27
|
+
const bounds = selectionConversionBounds(engine);
|
|
28
|
+
if (units.length === 0 || shapes.length === 0 || !bounds)
|
|
29
|
+
return null;
|
|
30
|
+
const host = selectedHostObject(engine);
|
|
31
|
+
const name = host ? (host.id === '' ? host.type : (host._customType ?? host.type)) : 'object';
|
|
32
|
+
const kinds = new Set(shapes.map((shape) => hostCustomTypeOf(shape) ?? hostTypeOf(shape)));
|
|
33
|
+
return {
|
|
34
|
+
units,
|
|
35
|
+
shapes,
|
|
36
|
+
bounds,
|
|
37
|
+
name,
|
|
38
|
+
shapeTypes: [...kinds],
|
|
39
|
+
hasImages: shapes.some((shape) => shape.type === 'image' && Boolean(shape.src || shape.imageRef)),
|
|
40
|
+
hasText: shapes.some((shape) => shape.type === 'text')
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** A PNG of some shapes on a plain background, cropped to what they cover. Empty when there is nothing to draw. */
|
|
44
|
+
export async function screenshotOfShapes(shapes, deps) {
|
|
45
|
+
if (shapes.length === 0 || !rasterBoundsOf(shapes))
|
|
46
|
+
return '';
|
|
47
|
+
try {
|
|
48
|
+
return await renderShapesToDataUrl(shapes, deps.raster, {
|
|
49
|
+
scale: SCREENSHOT_SCALE,
|
|
50
|
+
background: SCREENSHOT_BACKGROUND,
|
|
51
|
+
widgetFallbackName: deps.widgetFallbackName
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
console.warn('Canvas: could not draw the screenshot', error);
|
|
56
|
+
return '';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** What the host needs to turn a selection into a widget, or null when nothing is selected. */
|
|
60
|
+
export async function buildConversionPayload(deps) {
|
|
61
|
+
const { engine, conversions } = deps;
|
|
62
|
+
const selection = describeSelection(engine);
|
|
63
|
+
if (!selection)
|
|
64
|
+
return null;
|
|
65
|
+
// One object is converted by its own id. Several have none: the selection gets one and is remembered under it,
|
|
66
|
+
// before anything (the screenshot takes a moment) can clear the selection.
|
|
67
|
+
const single = selection.units.length === 1 ? selection.units[0] : null;
|
|
68
|
+
const frameId = single ? single.id : engine.services.createId('selection');
|
|
69
|
+
if (!single) {
|
|
70
|
+
conversions.remember(frameId, { unitIds: selection.units.map((unit) => unit.id), bounds: selection.bounds });
|
|
71
|
+
}
|
|
72
|
+
const screenshot = await screenshotOfShapes(selection.shapes, deps);
|
|
73
|
+
const frame = single ? hostObjectById(engine, single.id) : selectedHostObject(engine);
|
|
74
|
+
return {
|
|
75
|
+
frameName: selection.name || deps.untitledShapeName,
|
|
76
|
+
frameId,
|
|
77
|
+
screenshot,
|
|
78
|
+
canvasData: {
|
|
79
|
+
frame: frame ? { ...frame } : {},
|
|
80
|
+
children: [],
|
|
81
|
+
bounds: { left: selection.bounds.x, top: selection.bounds.y, width: selection.bounds.width, height: selection.bounds.height }
|
|
82
|
+
},
|
|
83
|
+
metadata: {
|
|
84
|
+
childCount: selection.shapes.length,
|
|
85
|
+
shapeTypes: selection.shapeTypes,
|
|
86
|
+
hasImages: selection.hasImages,
|
|
87
|
+
hasText: selection.hasText
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** The extra facts the host is given about what was captured. */
|
|
92
|
+
function metadataOf(selection) {
|
|
93
|
+
const metadata = {};
|
|
94
|
+
const only = selection.shapes.length === 1 && selection.units.length === 1 ? selection.shapes[0] : null;
|
|
95
|
+
if (only && only.type === 'image') {
|
|
96
|
+
metadata.hasImage = Boolean(only.src || only.imageRef);
|
|
97
|
+
metadata.imageUrl = only.src ?? null;
|
|
98
|
+
metadata.originalFilename = only.fileName ?? null;
|
|
99
|
+
}
|
|
100
|
+
if (only && only.type === 'text')
|
|
101
|
+
metadata.text = only.text;
|
|
102
|
+
if (selection.units.length > 1 || selection.units[0]?.type === 'group')
|
|
103
|
+
metadata.objectCount = selection.shapes.length;
|
|
104
|
+
return metadata;
|
|
105
|
+
}
|
|
106
|
+
/** What the host is given when a selection is captured (the "capture" button of the selection panel). */
|
|
107
|
+
export async function buildCapturePayload(deps) {
|
|
108
|
+
const { engine } = deps;
|
|
109
|
+
const selection = describeSelection(engine);
|
|
110
|
+
if (!selection)
|
|
111
|
+
return null;
|
|
112
|
+
const single = selection.units.length === 1 ? selection.units[0] : null;
|
|
113
|
+
const screenshot = await screenshotOfShapes(selection.shapes, deps);
|
|
114
|
+
const host = selectedHostObject(engine);
|
|
115
|
+
return {
|
|
116
|
+
screenshot,
|
|
117
|
+
objectType: host?.type ?? 'unknown',
|
|
118
|
+
customType: host?._customType ?? undefined,
|
|
119
|
+
bounds: { left: selection.bounds.x, top: selection.bounds.y, width: selection.bounds.width, height: selection.bounds.height },
|
|
120
|
+
objectId: single ? single.id : engine.services.createId('selection'),
|
|
121
|
+
metadata: metadataOf(selection)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the host is shown for an object on the board.
|
|
3
|
+
*
|
|
4
|
+
* The widget creator and the other modules that mount the canvas never see the records of the engine. They were
|
|
5
|
+
* written against the objects of the canvas this one replaced and read a handful of underscore fields from them
|
|
6
|
+
* (`_widgetId`, `_isWidgetImage`, `_objectId`, `_customType`), so the host is handed a small read-only view that keeps
|
|
7
|
+
* exactly those names. A view is made when it is asked for; changing it changes nothing on the board.
|
|
8
|
+
*/
|
|
9
|
+
import type { EngineCore } from '../../engine/EngineCore.js';
|
|
10
|
+
import type { ObjectId, SceneObject, ShapeObject } from '../../engine/core/types.js';
|
|
11
|
+
/** The object, as a module outside the canvas sees it. */
|
|
12
|
+
export interface HostObject {
|
|
13
|
+
id: ObjectId;
|
|
14
|
+
/** The same id, under the name the old canvas kept it in. */
|
|
15
|
+
_objectId: ObjectId;
|
|
16
|
+
/** `rect`, `ellipse`, `circle`, `path`, `textbox`, `ImageShape`, `WidgetShape`, `group` or `activeSelection`. */
|
|
17
|
+
type: string;
|
|
18
|
+
/** `image`, `triangle`, `arrow`, `libraryShape`, or null for the rest. */
|
|
19
|
+
_customType: string | null;
|
|
20
|
+
/** True for a card that stands for a widget. */
|
|
21
|
+
_isWidgetImage?: boolean;
|
|
22
|
+
_widgetId?: string;
|
|
23
|
+
_widgetName?: string | null;
|
|
24
|
+
_isPrimaryWidget?: boolean;
|
|
25
|
+
/** What a group or a selection holds. */
|
|
26
|
+
objects?: HostObject[];
|
|
27
|
+
}
|
|
28
|
+
/** The name the old canvas gave the kind of an object. */
|
|
29
|
+
export declare function hostTypeOf(object: SceneObject): string;
|
|
30
|
+
/** What kind of custom shape an object is, in the words the old canvas saved it with. */
|
|
31
|
+
export declare function hostCustomTypeOf(object: SceneObject): string | null;
|
|
32
|
+
/** The view of one object by id, or null when the board does not have it. */
|
|
33
|
+
export declare function hostObjectById(engine: EngineCore, id: ObjectId | null | undefined): HostObject | null;
|
|
34
|
+
/**
|
|
35
|
+
* The view of what is selected: the one object, or an `activeSelection` that lists them when there are several.
|
|
36
|
+
* Null when nothing is selected.
|
|
37
|
+
*/
|
|
38
|
+
export declare function selectedHostObject(engine: EngineCore): HostObject | null;
|
|
39
|
+
/** The shapes behind a list of units (a group counts through its members), for the conversion payload. */
|
|
40
|
+
export declare function shapesOfUnits(engine: EngineCore, unitIds: readonly ObjectId[]): ShapeObject[];
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the host is shown for an object on the board.
|
|
3
|
+
*
|
|
4
|
+
* The widget creator and the other modules that mount the canvas never see the records of the engine. They were
|
|
5
|
+
* written against the objects of the canvas this one replaced and read a handful of underscore fields from them
|
|
6
|
+
* (`_widgetId`, `_isWidgetImage`, `_objectId`, `_customType`), so the host is handed a small read-only view that keeps
|
|
7
|
+
* exactly those names. A view is made when it is asked for; changing it changes nothing on the board.
|
|
8
|
+
*/
|
|
9
|
+
import { leafShapesOf } from '../../engine/core/scene.js';
|
|
10
|
+
/** The name the old canvas gave the kind of an object. */
|
|
11
|
+
export function hostTypeOf(object) {
|
|
12
|
+
switch (object.type) {
|
|
13
|
+
case 'rect':
|
|
14
|
+
return 'rect';
|
|
15
|
+
case 'ellipse':
|
|
16
|
+
return object.aspectLocked ? 'circle' : 'ellipse';
|
|
17
|
+
case 'path':
|
|
18
|
+
return 'path';
|
|
19
|
+
case 'text':
|
|
20
|
+
return 'textbox';
|
|
21
|
+
case 'image':
|
|
22
|
+
return 'ImageShape';
|
|
23
|
+
case 'widget':
|
|
24
|
+
return 'WidgetShape';
|
|
25
|
+
case 'group':
|
|
26
|
+
return 'group';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** What kind of custom shape an object is, in the words the old canvas saved it with. */
|
|
30
|
+
export function hostCustomTypeOf(object) {
|
|
31
|
+
if (object.type === 'image' || object.type === 'widget')
|
|
32
|
+
return 'image';
|
|
33
|
+
if (object.type === 'path') {
|
|
34
|
+
if (object.shape === 'triangle')
|
|
35
|
+
return 'triangle';
|
|
36
|
+
if (object.shape === 'arrow')
|
|
37
|
+
return 'arrow';
|
|
38
|
+
if (object.shape)
|
|
39
|
+
return 'libraryShape';
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
function viewOf(object, index, scene) {
|
|
44
|
+
const view = {
|
|
45
|
+
id: object.id,
|
|
46
|
+
_objectId: object.id,
|
|
47
|
+
type: hostTypeOf(object),
|
|
48
|
+
_customType: hostCustomTypeOf(object)
|
|
49
|
+
};
|
|
50
|
+
if (object.type === 'widget') {
|
|
51
|
+
view._isWidgetImage = true;
|
|
52
|
+
view._widgetId = object.widgetId;
|
|
53
|
+
view._widgetName = object.widgetName;
|
|
54
|
+
view._isPrimaryWidget = object.isPrimary;
|
|
55
|
+
}
|
|
56
|
+
if (object.type === 'group') {
|
|
57
|
+
view.objects = scene.filter((member) => member.parentId === object.id).map((member) => viewOf(member, index, scene));
|
|
58
|
+
}
|
|
59
|
+
return view;
|
|
60
|
+
}
|
|
61
|
+
/** The view of one object by id, or null when the board does not have it. */
|
|
62
|
+
export function hostObjectById(engine, id) {
|
|
63
|
+
if (!id)
|
|
64
|
+
return null;
|
|
65
|
+
const object = engine.getObject(id);
|
|
66
|
+
if (!object)
|
|
67
|
+
return null;
|
|
68
|
+
return viewOf(object, engine.sceneIndex(), engine.state.scene);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The view of what is selected: the one object, or an `activeSelection` that lists them when there are several.
|
|
72
|
+
* Null when nothing is selected.
|
|
73
|
+
*/
|
|
74
|
+
export function selectedHostObject(engine) {
|
|
75
|
+
const selected = engine.selectedObjects();
|
|
76
|
+
if (selected.length === 0)
|
|
77
|
+
return null;
|
|
78
|
+
const index = engine.sceneIndex();
|
|
79
|
+
const scene = engine.state.scene;
|
|
80
|
+
if (selected.length === 1)
|
|
81
|
+
return viewOf(selected[0], index, scene);
|
|
82
|
+
return {
|
|
83
|
+
id: '',
|
|
84
|
+
_objectId: '',
|
|
85
|
+
type: 'activeSelection',
|
|
86
|
+
_customType: null,
|
|
87
|
+
objects: selected.map((object) => viewOf(object, index, scene))
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** The shapes behind a list of units (a group counts through its members), for the conversion payload. */
|
|
91
|
+
export function shapesOfUnits(engine, unitIds) {
|
|
92
|
+
return leafShapesOf(engine.state.scene, unitIds);
|
|
93
|
+
}
|