@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,515 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SET_ACTIVE_TOOL_TOOLBAR_ACTION_ID,
|
|
3
|
+
type ContextMenuContribution,
|
|
4
|
+
type LandCorePlugin,
|
|
5
|
+
type ShapeSvgExporter,
|
|
6
|
+
type SvgNode,
|
|
7
|
+
type ToolbarAction,
|
|
8
|
+
type ToolbarActionValue,
|
|
9
|
+
type ToolbarContribution,
|
|
10
|
+
} from "@land/canvas-core";
|
|
11
|
+
import { DrawShapeUtil } from "./DrawShapeUtil";
|
|
12
|
+
import { DrawTool } from "./DrawTool";
|
|
13
|
+
import { getDrawShapeLocalGeometry } from "./drawGeometry";
|
|
14
|
+
import {
|
|
15
|
+
isDrawShapeRecord,
|
|
16
|
+
type DrawShapeProps,
|
|
17
|
+
type DrawShapeRecord,
|
|
18
|
+
} from "./types";
|
|
19
|
+
|
|
20
|
+
export class DrawShapeSvgExporter implements ShapeSvgExporter<DrawShapeRecord> {
|
|
21
|
+
readonly type = "draw" as const;
|
|
22
|
+
|
|
23
|
+
toSvg(shape: DrawShapeRecord): SvgNode | null {
|
|
24
|
+
const geometry = getDrawShapeLocalGeometry(shape);
|
|
25
|
+
if (!geometry.path && !geometry.selectionPath) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const children: SvgNode[] = [];
|
|
30
|
+
if (geometry.fillPath && shape.props.fill !== "transparent") {
|
|
31
|
+
children.push({
|
|
32
|
+
tag: "path",
|
|
33
|
+
attrs: {
|
|
34
|
+
d: geometry.fillPath,
|
|
35
|
+
fill: shape.props.fill,
|
|
36
|
+
stroke: "none",
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (shape.props.isClosed && geometry.selectionPath) {
|
|
41
|
+
children.push({
|
|
42
|
+
tag: "path",
|
|
43
|
+
attrs: {
|
|
44
|
+
d: geometry.selectionPath,
|
|
45
|
+
fill: "none",
|
|
46
|
+
stroke: shape.props.stroke,
|
|
47
|
+
"stroke-width": shape.props.strokeWidth,
|
|
48
|
+
"stroke-linecap": "round",
|
|
49
|
+
"stroke-linejoin": "round",
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
} else if (geometry.path) {
|
|
53
|
+
children.push({
|
|
54
|
+
tag: "path",
|
|
55
|
+
attrs: {
|
|
56
|
+
d: geometry.path,
|
|
57
|
+
fill: shape.props.stroke,
|
|
58
|
+
stroke: "none",
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
tag: "g",
|
|
65
|
+
children,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function createDrawCorePlugin(): LandCorePlugin {
|
|
71
|
+
return {
|
|
72
|
+
id: "land.draw",
|
|
73
|
+
shapeCapabilities: [
|
|
74
|
+
{
|
|
75
|
+
type: "draw",
|
|
76
|
+
schemaProps: [
|
|
77
|
+
"segments",
|
|
78
|
+
"stroke",
|
|
79
|
+
"strokeWidth",
|
|
80
|
+
"fill",
|
|
81
|
+
"isClosed",
|
|
82
|
+
"isPen",
|
|
83
|
+
],
|
|
84
|
+
reactRenderer: "supported",
|
|
85
|
+
toolbarEntry: "supported",
|
|
86
|
+
notes: [
|
|
87
|
+
"Draw stores raw segments as document truth and derives a shared freehand stroke geometry for render, hit test, brush wrap, and SVG export.",
|
|
88
|
+
],
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
shapeUtils: [new DrawShapeUtil()],
|
|
92
|
+
shapeSvgExporters: [new DrawShapeSvgExporter()],
|
|
93
|
+
toolbarActions: [
|
|
94
|
+
createSetDrawClosedToolbarAction(),
|
|
95
|
+
createSetDrawStyleToolbarAction("fill"),
|
|
96
|
+
createSetDrawStyleToolbarAction("stroke"),
|
|
97
|
+
createSetDrawStyleToolbarAction("strokeWidth"),
|
|
98
|
+
createSetDrawDefaultClosedToolbarAction(),
|
|
99
|
+
createSetDrawDefaultStyleToolbarAction("fill"),
|
|
100
|
+
createSetDrawDefaultStyleToolbarAction("stroke"),
|
|
101
|
+
createSetDrawDefaultStyleToolbarAction("strokeWidth"),
|
|
102
|
+
],
|
|
103
|
+
toolbarContributions: [
|
|
104
|
+
createDrawToolToolbarContribution(),
|
|
105
|
+
createDrawDefaultStyleToolbarContribution(),
|
|
106
|
+
createDrawSelectionToolbarContribution(),
|
|
107
|
+
],
|
|
108
|
+
contextMenuContributions: [createDrawContextMenuContribution()],
|
|
109
|
+
setup(editor) {
|
|
110
|
+
editor.tools.register(
|
|
111
|
+
"draw",
|
|
112
|
+
(currentEditor) => new DrawTool(currentEditor),
|
|
113
|
+
{ shortcut: "p" },
|
|
114
|
+
);
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function createDrawContextMenuContribution(): ContextMenuContribution {
|
|
120
|
+
return {
|
|
121
|
+
id: "draw.shape-context-menu",
|
|
122
|
+
target: "selection",
|
|
123
|
+
order: 35,
|
|
124
|
+
actionIds: ["draw.set-closed"],
|
|
125
|
+
when(context) {
|
|
126
|
+
return context.selection.commonShapeType === "draw";
|
|
127
|
+
},
|
|
128
|
+
getSections(context) {
|
|
129
|
+
const currentValue = getCommonDrawClosedValue(
|
|
130
|
+
context.selection.selectedShapes,
|
|
131
|
+
);
|
|
132
|
+
const nextValue = currentValue === "closed" ? "open" : "closed";
|
|
133
|
+
return [
|
|
134
|
+
{
|
|
135
|
+
id: "draw.context-menu",
|
|
136
|
+
title: "Draw",
|
|
137
|
+
items: [
|
|
138
|
+
{
|
|
139
|
+
kind: "command",
|
|
140
|
+
id: "draw.toggle-closed",
|
|
141
|
+
actionId: "draw.set-closed",
|
|
142
|
+
value: nextValue,
|
|
143
|
+
label:
|
|
144
|
+
nextValue === "closed" ? "Close freehand" : "Open freehand",
|
|
145
|
+
icon: "pencil",
|
|
146
|
+
dataUi: "context-menu-toggle-draw-closed-button",
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
];
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const DRAW_STROKE_SWATCHES = [
|
|
156
|
+
{ value: "#1d4ed8", label: "Blue", color: "#1d4ed8" },
|
|
157
|
+
{ value: "#15803d", label: "Green", color: "#15803d" },
|
|
158
|
+
{ value: "#dc2626", label: "Red", color: "#dc2626" },
|
|
159
|
+
{ value: "#334155", label: "Slate", color: "#334155" },
|
|
160
|
+
] as const;
|
|
161
|
+
|
|
162
|
+
const DRAW_FILL_SWATCHES = [
|
|
163
|
+
{ value: "transparent", label: "None", color: "transparent" },
|
|
164
|
+
{ value: "#dbeafe", label: "Blue", color: "#dbeafe" },
|
|
165
|
+
{ value: "#dcfce7", label: "Green", color: "#dcfce7" },
|
|
166
|
+
{ value: "#fee2e2", label: "Red", color: "#fee2e2" },
|
|
167
|
+
{ value: "#f8fafc", label: "White", color: "#f8fafc" },
|
|
168
|
+
] as const;
|
|
169
|
+
|
|
170
|
+
const DRAW_STROKE_WIDTH_OPTIONS = [
|
|
171
|
+
{ value: 4, label: "4" },
|
|
172
|
+
{ value: 6, label: "6" },
|
|
173
|
+
{ value: 8, label: "8" },
|
|
174
|
+
{ value: 10, label: "10" },
|
|
175
|
+
] as const;
|
|
176
|
+
|
|
177
|
+
const DRAW_CLOSED_OPTIONS = [
|
|
178
|
+
{ value: "open", label: "Open" },
|
|
179
|
+
{ value: "closed", label: "Closed" },
|
|
180
|
+
] as const;
|
|
181
|
+
|
|
182
|
+
function createDrawToolToolbarContribution(): ToolbarContribution {
|
|
183
|
+
return {
|
|
184
|
+
id: "draw.tool-toolbar",
|
|
185
|
+
surface: "tool",
|
|
186
|
+
placement: "chrome",
|
|
187
|
+
order: 45,
|
|
188
|
+
actionIds: [SET_ACTIVE_TOOL_TOOLBAR_ACTION_ID],
|
|
189
|
+
getGroups() {
|
|
190
|
+
return [
|
|
191
|
+
{
|
|
192
|
+
id: "draw.tools",
|
|
193
|
+
items: [
|
|
194
|
+
{
|
|
195
|
+
kind: "button",
|
|
196
|
+
id: "draw.tool.draw",
|
|
197
|
+
actionId: SET_ACTIVE_TOOL_TOOLBAR_ACTION_ID,
|
|
198
|
+
value: "draw",
|
|
199
|
+
label: "Draw",
|
|
200
|
+
shortcut: "P",
|
|
201
|
+
icon: "pencil",
|
|
202
|
+
dataUi: "draw-tool-button",
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
},
|
|
206
|
+
];
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function createDrawSelectionToolbarContribution(): ToolbarContribution {
|
|
212
|
+
return {
|
|
213
|
+
id: "draw.selection-toolbar",
|
|
214
|
+
surface: "selection",
|
|
215
|
+
order: 20,
|
|
216
|
+
actionIds: [
|
|
217
|
+
"draw.set-closed",
|
|
218
|
+
"draw.set-fill",
|
|
219
|
+
"draw.set-stroke",
|
|
220
|
+
"draw.set-strokeWidth",
|
|
221
|
+
],
|
|
222
|
+
when(context) {
|
|
223
|
+
return context.selection.commonShapeType === "draw";
|
|
224
|
+
},
|
|
225
|
+
getGroups() {
|
|
226
|
+
return getDrawStyleToolbarGroups({
|
|
227
|
+
closedActionId: "draw.set-closed",
|
|
228
|
+
fillActionId: "draw.set-fill",
|
|
229
|
+
strokeActionId: "draw.set-stroke",
|
|
230
|
+
strokeWidthActionId: "draw.set-strokeWidth",
|
|
231
|
+
dataUiPrefix: "draw",
|
|
232
|
+
});
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function createDrawDefaultStyleToolbarContribution(): ToolbarContribution {
|
|
238
|
+
return {
|
|
239
|
+
id: "draw.default-style.tool-toolbar",
|
|
240
|
+
surface: "tool",
|
|
241
|
+
order: 46,
|
|
242
|
+
actionIds: [
|
|
243
|
+
"draw.default-closed",
|
|
244
|
+
"draw.default-fill",
|
|
245
|
+
"draw.default-stroke",
|
|
246
|
+
"draw.default-strokeWidth",
|
|
247
|
+
],
|
|
248
|
+
when(context) {
|
|
249
|
+
return context.tool.activeToolId === "draw";
|
|
250
|
+
},
|
|
251
|
+
getGroups() {
|
|
252
|
+
return getDrawStyleToolbarGroups({
|
|
253
|
+
closedActionId: "draw.default-closed",
|
|
254
|
+
fillActionId: "draw.default-fill",
|
|
255
|
+
strokeActionId: "draw.default-stroke",
|
|
256
|
+
strokeWidthActionId: "draw.default-strokeWidth",
|
|
257
|
+
dataUiPrefix: "draw-default",
|
|
258
|
+
});
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function getDrawStyleToolbarGroups({
|
|
264
|
+
closedActionId,
|
|
265
|
+
fillActionId,
|
|
266
|
+
strokeActionId,
|
|
267
|
+
strokeWidthActionId,
|
|
268
|
+
dataUiPrefix,
|
|
269
|
+
}: {
|
|
270
|
+
closedActionId: string;
|
|
271
|
+
fillActionId: string;
|
|
272
|
+
strokeActionId: string;
|
|
273
|
+
strokeWidthActionId: string;
|
|
274
|
+
dataUiPrefix: string;
|
|
275
|
+
}) {
|
|
276
|
+
return [
|
|
277
|
+
{
|
|
278
|
+
id: `${dataUiPrefix}.style-closed`,
|
|
279
|
+
items: [
|
|
280
|
+
{
|
|
281
|
+
kind: "segmented" as const,
|
|
282
|
+
id: `${dataUiPrefix}.closed`,
|
|
283
|
+
actionId: closedActionId,
|
|
284
|
+
options: DRAW_CLOSED_OPTIONS.map((option) => ({
|
|
285
|
+
...option,
|
|
286
|
+
dataUi: `${dataUiPrefix}-${option.value}-button`,
|
|
287
|
+
})),
|
|
288
|
+
},
|
|
289
|
+
],
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
id: `${dataUiPrefix}.style-fill`,
|
|
293
|
+
items: [
|
|
294
|
+
{
|
|
295
|
+
kind: "swatches" as const,
|
|
296
|
+
id: `${dataUiPrefix}.fill`,
|
|
297
|
+
actionId: fillActionId,
|
|
298
|
+
label: "Fill",
|
|
299
|
+
options: DRAW_FILL_SWATCHES.map((option) => ({
|
|
300
|
+
...option,
|
|
301
|
+
dataUi: `${dataUiPrefix}-fill-${option.label.toLowerCase()}-button`,
|
|
302
|
+
})),
|
|
303
|
+
},
|
|
304
|
+
],
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
id: `${dataUiPrefix}.style-stroke`,
|
|
308
|
+
items: [
|
|
309
|
+
{
|
|
310
|
+
kind: "swatches" as const,
|
|
311
|
+
id: `${dataUiPrefix}.stroke`,
|
|
312
|
+
actionId: strokeActionId,
|
|
313
|
+
label: "Stroke",
|
|
314
|
+
options: DRAW_STROKE_SWATCHES.map((option) => ({
|
|
315
|
+
...option,
|
|
316
|
+
dataUi: `${dataUiPrefix}-stroke-${option.label.toLowerCase()}-button`,
|
|
317
|
+
})),
|
|
318
|
+
},
|
|
319
|
+
],
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
id: `${dataUiPrefix}.style-stroke-width`,
|
|
323
|
+
items: [
|
|
324
|
+
{
|
|
325
|
+
kind: "stepper" as const,
|
|
326
|
+
id: `${dataUiPrefix}.stroke-width`,
|
|
327
|
+
actionId: strokeWidthActionId,
|
|
328
|
+
label: "Stroke width",
|
|
329
|
+
dataUi: `${dataUiPrefix}-stroke-width-control`,
|
|
330
|
+
options: DRAW_STROKE_WIDTH_OPTIONS.map((option) => ({
|
|
331
|
+
...option,
|
|
332
|
+
dataUi: `${dataUiPrefix}-stroke-width-${option.value}-button`,
|
|
333
|
+
})),
|
|
334
|
+
},
|
|
335
|
+
],
|
|
336
|
+
},
|
|
337
|
+
];
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function createSetDrawStyleToolbarAction(
|
|
341
|
+
prop: "fill" | "stroke" | "strokeWidth",
|
|
342
|
+
): ToolbarAction<ToolbarActionValue> {
|
|
343
|
+
return {
|
|
344
|
+
id: `draw.set-${prop}`,
|
|
345
|
+
isVisible(context) {
|
|
346
|
+
return context.selection.commonShapeType === "draw";
|
|
347
|
+
},
|
|
348
|
+
isEnabled(context) {
|
|
349
|
+
return context.selection.count > 0;
|
|
350
|
+
},
|
|
351
|
+
getValue(context) {
|
|
352
|
+
return getCommonDrawPropValue(context.selection.selectedShapes, prop);
|
|
353
|
+
},
|
|
354
|
+
run({ context, value }) {
|
|
355
|
+
if (!isValidDrawStyleValue(prop, value)) {
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
const updates = context.selection.selectedShapes
|
|
359
|
+
.filter(isDrawShapeRecord)
|
|
360
|
+
.filter((shape) => !shape.isLocked)
|
|
361
|
+
.map((shape) => ({
|
|
362
|
+
id: shape.id,
|
|
363
|
+
props: {
|
|
364
|
+
[prop]: value,
|
|
365
|
+
},
|
|
366
|
+
}));
|
|
367
|
+
if (updates.length === 0) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
context.editor.commands.updateShapes(updates, { source: "user" });
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function createSetDrawDefaultStyleToolbarAction(
|
|
376
|
+
prop: "fill" | "stroke" | "strokeWidth",
|
|
377
|
+
): ToolbarAction<ToolbarActionValue> {
|
|
378
|
+
return {
|
|
379
|
+
id: `draw.default-${prop}`,
|
|
380
|
+
isVisible(context) {
|
|
381
|
+
return context.tool.activeToolId === "draw";
|
|
382
|
+
},
|
|
383
|
+
getValue(context) {
|
|
384
|
+
return context.editor.getDefaultShapeProps<DrawShapeProps>("draw")[prop];
|
|
385
|
+
},
|
|
386
|
+
run({ context, value }) {
|
|
387
|
+
if (!isValidDrawStyleValue(prop, value)) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
context.editor.updateDefaultShapeProps("draw", { [prop]: value });
|
|
391
|
+
},
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function createSetDrawClosedToolbarAction(): ToolbarAction<string | null> {
|
|
396
|
+
return {
|
|
397
|
+
id: "draw.set-closed",
|
|
398
|
+
isVisible(context) {
|
|
399
|
+
return context.selection.commonShapeType === "draw";
|
|
400
|
+
},
|
|
401
|
+
isEnabled(context) {
|
|
402
|
+
return context.selection.count > 0;
|
|
403
|
+
},
|
|
404
|
+
getValue(context) {
|
|
405
|
+
return getCommonDrawClosedValue(context.selection.selectedShapes);
|
|
406
|
+
},
|
|
407
|
+
run({ context, value }) {
|
|
408
|
+
const isClosed = parseDrawClosedToolbarValue(value);
|
|
409
|
+
if (isClosed === null) {
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
const updates = context.selection.selectedShapes
|
|
413
|
+
.filter(isDrawShapeRecord)
|
|
414
|
+
.filter((shape) => !shape.isLocked)
|
|
415
|
+
.map((shape) => ({
|
|
416
|
+
id: shape.id,
|
|
417
|
+
props: {
|
|
418
|
+
isClosed,
|
|
419
|
+
},
|
|
420
|
+
}));
|
|
421
|
+
if (updates.length === 0) {
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
context.editor.commands.updateShapes(updates, { source: "user" });
|
|
425
|
+
},
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function createSetDrawDefaultClosedToolbarAction(): ToolbarAction<string | null> {
|
|
430
|
+
return {
|
|
431
|
+
id: "draw.default-closed",
|
|
432
|
+
isVisible(context) {
|
|
433
|
+
return context.tool.activeToolId === "draw";
|
|
434
|
+
},
|
|
435
|
+
getValue(context) {
|
|
436
|
+
return context.editor.getDefaultShapeProps<DrawShapeProps>("draw").isClosed
|
|
437
|
+
? "closed"
|
|
438
|
+
: "open";
|
|
439
|
+
},
|
|
440
|
+
run({ context, value }) {
|
|
441
|
+
const isClosed = parseDrawClosedToolbarValue(value);
|
|
442
|
+
if (isClosed === null) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
context.editor.updateDefaultShapeProps("draw", { isClosed });
|
|
446
|
+
},
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function getCommonDrawPropValue(
|
|
451
|
+
selectedShapes: readonly { type: string; props: object }[],
|
|
452
|
+
prop: "fill" | "stroke" | "strokeWidth",
|
|
453
|
+
): ToolbarActionValue {
|
|
454
|
+
let value: ToolbarActionValue | undefined;
|
|
455
|
+
for (const shape of selectedShapes) {
|
|
456
|
+
if (shape.type !== "draw") {
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
const nextValue = (shape.props as DrawShapeProps)[prop];
|
|
460
|
+
if (value === undefined) {
|
|
461
|
+
value = nextValue;
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
if (value !== nextValue) {
|
|
465
|
+
return null;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return value ?? null;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function getCommonDrawClosedValue(
|
|
472
|
+
selectedShapes: readonly { type: string; props: object }[],
|
|
473
|
+
): string | null {
|
|
474
|
+
let isClosed: boolean | undefined;
|
|
475
|
+
for (const shape of selectedShapes) {
|
|
476
|
+
if (shape.type !== "draw") {
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
const nextValue = (shape.props as DrawShapeProps).isClosed;
|
|
480
|
+
if (isClosed === undefined) {
|
|
481
|
+
isClosed = nextValue;
|
|
482
|
+
continue;
|
|
483
|
+
}
|
|
484
|
+
if (isClosed !== nextValue) {
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if (isClosed === undefined) {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
return isClosed ? "closed" : "open";
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function parseDrawClosedToolbarValue(value: ToolbarActionValue): boolean | null {
|
|
495
|
+
if (value === "closed") {
|
|
496
|
+
return true;
|
|
497
|
+
}
|
|
498
|
+
if (value === "open") {
|
|
499
|
+
return false;
|
|
500
|
+
}
|
|
501
|
+
return null;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function isValidDrawStyleValue(
|
|
505
|
+
prop: "fill" | "stroke" | "strokeWidth",
|
|
506
|
+
value: ToolbarActionValue,
|
|
507
|
+
): boolean {
|
|
508
|
+
if (prop === "strokeWidth") {
|
|
509
|
+
return DRAW_STROKE_WIDTH_OPTIONS.some((option) => option.value === value);
|
|
510
|
+
}
|
|
511
|
+
if (prop === "fill") {
|
|
512
|
+
return DRAW_FILL_SWATCHES.some((option) => option.value === value);
|
|
513
|
+
}
|
|
514
|
+
return DRAW_STROKE_SWATCHES.some((option) => option.value === value);
|
|
515
|
+
}
|