@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,350 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Vec,
|
|
3
|
+
type Editor,
|
|
4
|
+
type KeyboardEditorEvent,
|
|
5
|
+
type LandInteractionHandler,
|
|
6
|
+
type PointerEditorEvent,
|
|
7
|
+
type SelectionResizeHandleId,
|
|
8
|
+
type SelectionHandleInteractionHandler,
|
|
9
|
+
type SelectionHandleInteractionSession,
|
|
10
|
+
type ShapeId,
|
|
11
|
+
type ShapeUpdate,
|
|
12
|
+
} from "@land/canvas-core";
|
|
13
|
+
import {
|
|
14
|
+
getImageCropUpdateFromHandleDrag,
|
|
15
|
+
getImageCropUpdateFromContentDrag,
|
|
16
|
+
getImageCropUpdateFromContentNudge,
|
|
17
|
+
IMAGE_CROP_HANDLE_HISTORY_MARK,
|
|
18
|
+
IMAGE_CROP_HANDLE_TYPE,
|
|
19
|
+
IMAGE_CROP_TRANSLATE_HISTORY_MARK,
|
|
20
|
+
isImageCropModeForShape,
|
|
21
|
+
isImageCropHandleId,
|
|
22
|
+
} from "./crop";
|
|
23
|
+
import { clearImageCropSessionBaseline } from "./cropSession";
|
|
24
|
+
import {
|
|
25
|
+
isImageShapeRecord,
|
|
26
|
+
type ImageShapeCrop,
|
|
27
|
+
type ImageShapeRecord,
|
|
28
|
+
} from "./types";
|
|
29
|
+
|
|
30
|
+
const IMAGE_CROP_DRAG_START_DISTANCE_SQUARED = 9;
|
|
31
|
+
const IMAGE_CROP_NUDGE_DISTANCE = 1;
|
|
32
|
+
const IMAGE_CROP_FAST_NUDGE_DISTANCE = 10;
|
|
33
|
+
|
|
34
|
+
interface ImageCropContentDragSession {
|
|
35
|
+
pointerId: number;
|
|
36
|
+
shapeId: ShapeId;
|
|
37
|
+
startCrop: ImageShapeCrop | null;
|
|
38
|
+
startPagePoint: Vec;
|
|
39
|
+
startPageToLocal: { applyToPoint(point: Vec): Vec };
|
|
40
|
+
startShape: ImageShapeRecord;
|
|
41
|
+
hasDragged: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function createImageCropSession({
|
|
45
|
+
handleId,
|
|
46
|
+
shapeId,
|
|
47
|
+
startPagePoint,
|
|
48
|
+
startPageToLocal,
|
|
49
|
+
startShape,
|
|
50
|
+
startCrop,
|
|
51
|
+
}: {
|
|
52
|
+
handleId: SelectionResizeHandleId;
|
|
53
|
+
shapeId: ShapeId;
|
|
54
|
+
startPagePoint: Vec;
|
|
55
|
+
startPageToLocal: { applyToPoint(point: Vec): Vec };
|
|
56
|
+
startShape: ImageShapeRecord;
|
|
57
|
+
startCrop: ImageShapeCrop | null;
|
|
58
|
+
}): SelectionHandleInteractionSession {
|
|
59
|
+
return {
|
|
60
|
+
shapeId,
|
|
61
|
+
historyMark: IMAGE_CROP_HANDLE_HISTORY_MARK,
|
|
62
|
+
update({ editor, pagePoint, source }) {
|
|
63
|
+
const shape = editor.getShape(shapeId);
|
|
64
|
+
if (!isImageShapeRecord(shape)) {
|
|
65
|
+
return "stop";
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const update = getImageCropUpdateFromHandleDrag({
|
|
69
|
+
handleId,
|
|
70
|
+
currentPagePoint: pagePoint,
|
|
71
|
+
pageToLocal: startPageToLocal,
|
|
72
|
+
shape: startShape,
|
|
73
|
+
startCrop,
|
|
74
|
+
startPagePoint,
|
|
75
|
+
});
|
|
76
|
+
if (!update) {
|
|
77
|
+
return "continue";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
editor.commands.updateShapes(
|
|
81
|
+
[update],
|
|
82
|
+
{ source },
|
|
83
|
+
);
|
|
84
|
+
return "continue";
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function createImageCropSelectionHandleInteractionHandler(): SelectionHandleInteractionHandler {
|
|
90
|
+
return {
|
|
91
|
+
createSession(editor, { handle, shape, originPagePoint }) {
|
|
92
|
+
if (
|
|
93
|
+
!isImageShapeRecord(shape) ||
|
|
94
|
+
handle.kind !== "custom" ||
|
|
95
|
+
handle.type !== IMAGE_CROP_HANDLE_TYPE ||
|
|
96
|
+
!isImageCropHandleId(handle.id)
|
|
97
|
+
) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return createImageCropSession({
|
|
102
|
+
handleId: handle.id,
|
|
103
|
+
shapeId: shape.id,
|
|
104
|
+
startPagePoint: originPagePoint,
|
|
105
|
+
startPageToLocal: editor.computeShapeLocalToPageTransform(shape).invert(),
|
|
106
|
+
startShape: shape,
|
|
107
|
+
startCrop: shape.props.crop,
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function createImageCropInteractionHandler(): LandInteractionHandler {
|
|
114
|
+
let contentDragSession: ImageCropContentDragSession | null = null;
|
|
115
|
+
|
|
116
|
+
const clearContentDragSession = (): void => {
|
|
117
|
+
contentDragSession = null;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
id: "land.image.crop.interactions",
|
|
122
|
+
editorEvent: {
|
|
123
|
+
handleEvent(editor, event) {
|
|
124
|
+
if (event.type === "keyboard") {
|
|
125
|
+
return handleCropKeyboardEvent(editor, event);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (event.type !== "pointer") {
|
|
129
|
+
return "continue";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (event.name === "pointer_cancel") {
|
|
133
|
+
if (contentDragSession?.hasDragged) {
|
|
134
|
+
editor.history.bailToMark(IMAGE_CROP_TRANSLATE_HISTORY_MARK);
|
|
135
|
+
}
|
|
136
|
+
clearContentDragSession();
|
|
137
|
+
return "continue";
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (event.name === "pointer_down") {
|
|
141
|
+
clearContentDragSession();
|
|
142
|
+
|
|
143
|
+
if (
|
|
144
|
+
event.button !== 0 ||
|
|
145
|
+
editor.getActiveToolId() !== "select" ||
|
|
146
|
+
!editor.isActiveToolInInitialState()
|
|
147
|
+
) {
|
|
148
|
+
return "continue";
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const handle = editor.hitTestSelectionHandle(event.pagePoint);
|
|
152
|
+
if (handle) {
|
|
153
|
+
return "continue";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const shape = getActiveImageCropShape(editor);
|
|
157
|
+
if (!shape) {
|
|
158
|
+
return "continue";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!isPointInsideImageShape(editor, shape, event)) {
|
|
162
|
+
if (editor.completeEditing("user")) {
|
|
163
|
+
clearImageCropSessionBaseline(shape.id);
|
|
164
|
+
editor.dispatch(event);
|
|
165
|
+
return "handled";
|
|
166
|
+
}
|
|
167
|
+
return "continue";
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
contentDragSession = {
|
|
171
|
+
pointerId: event.pointerId,
|
|
172
|
+
shapeId: shape.id,
|
|
173
|
+
startCrop: shape.props.crop,
|
|
174
|
+
startPagePoint: event.pagePoint.clone(),
|
|
175
|
+
startPageToLocal: editor.computeShapeLocalToPageTransform(shape).invert(),
|
|
176
|
+
startShape: shape,
|
|
177
|
+
hasDragged: false,
|
|
178
|
+
};
|
|
179
|
+
return "handled";
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (!contentDragSession || event.pointerId !== contentDragSession.pointerId) {
|
|
183
|
+
return "continue";
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (event.name === "pointer_move") {
|
|
187
|
+
updateContentDrag(editor, event, contentDragSession);
|
|
188
|
+
return "handled";
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (event.name === "pointer_up") {
|
|
192
|
+
updateContentDrag(editor, event, contentDragSession);
|
|
193
|
+
if (contentDragSession.hasDragged) {
|
|
194
|
+
editor.history.squashToMark(
|
|
195
|
+
IMAGE_CROP_TRANSLATE_HISTORY_MARK,
|
|
196
|
+
"shape.edit",
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
clearContentDragSession();
|
|
200
|
+
return "handled";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return "continue";
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
selectionHandle: createImageCropSelectionHandleInteractionHandler(),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function handleCropKeyboardEvent(
|
|
211
|
+
editor: Editor,
|
|
212
|
+
event: KeyboardEditorEvent,
|
|
213
|
+
): "continue" | "handled" {
|
|
214
|
+
if (event.name !== "key_down") {
|
|
215
|
+
return "continue";
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const shape = getActiveImageCropShape(editor);
|
|
219
|
+
if (!shape) {
|
|
220
|
+
return "continue";
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (
|
|
224
|
+
event.key === "Escape" &&
|
|
225
|
+
!event.altKey &&
|
|
226
|
+
!event.accelKey
|
|
227
|
+
) {
|
|
228
|
+
if (editor.cancelEditing("user")) {
|
|
229
|
+
clearImageCropSessionBaseline(shape.id);
|
|
230
|
+
}
|
|
231
|
+
return "handled";
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (
|
|
235
|
+
event.key === "Enter" &&
|
|
236
|
+
!event.altKey &&
|
|
237
|
+
!event.accelKey
|
|
238
|
+
) {
|
|
239
|
+
if (editor.completeEditing("user")) {
|
|
240
|
+
clearImageCropSessionBaseline(shape.id);
|
|
241
|
+
}
|
|
242
|
+
return "handled";
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const nudgeDelta = getCropNudgeDelta(event);
|
|
246
|
+
if (nudgeDelta) {
|
|
247
|
+
const update = getImageCropUpdateFromContentNudge({
|
|
248
|
+
pageDelta: nudgeDelta,
|
|
249
|
+
shape,
|
|
250
|
+
startCrop: shape.props.crop,
|
|
251
|
+
});
|
|
252
|
+
if (update) {
|
|
253
|
+
editor.commands.updateShapes([update], { source: "user" });
|
|
254
|
+
}
|
|
255
|
+
return "handled";
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return "continue";
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function getCropNudgeDelta(event: KeyboardEditorEvent): Vec | null {
|
|
262
|
+
if (event.altKey || event.accelKey) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const distance = event.shiftKey
|
|
267
|
+
? IMAGE_CROP_FAST_NUDGE_DISTANCE
|
|
268
|
+
: IMAGE_CROP_NUDGE_DISTANCE;
|
|
269
|
+
switch (event.key) {
|
|
270
|
+
case "ArrowLeft":
|
|
271
|
+
return new Vec(distance, 0);
|
|
272
|
+
case "ArrowRight":
|
|
273
|
+
return new Vec(-distance, 0);
|
|
274
|
+
case "ArrowUp":
|
|
275
|
+
return new Vec(0, distance);
|
|
276
|
+
case "ArrowDown":
|
|
277
|
+
return new Vec(0, -distance);
|
|
278
|
+
default:
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function getActiveImageCropShape(editor: Editor): ImageShapeRecord | null {
|
|
284
|
+
const [shape] = editor.getSelectedShapes();
|
|
285
|
+
if (!isImageShapeRecord(shape)) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const session = editor.getShapeEditingSession();
|
|
290
|
+
if (
|
|
291
|
+
!session ||
|
|
292
|
+
session.shapeId !== shape.id ||
|
|
293
|
+
!isImageCropModeForShape(shape, session.markId)
|
|
294
|
+
) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return shape;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function isPointInsideImageShape(
|
|
302
|
+
editor: Editor,
|
|
303
|
+
shape: ImageShapeRecord,
|
|
304
|
+
event: PointerEditorEvent,
|
|
305
|
+
): boolean {
|
|
306
|
+
const pageToLocal = editor.computeShapeLocalToPageTransform(shape).invert();
|
|
307
|
+
const localPoint = pageToLocal.applyToPoint(event.pagePoint);
|
|
308
|
+
return (
|
|
309
|
+
localPoint.x >= 0 &&
|
|
310
|
+
localPoint.y >= 0 &&
|
|
311
|
+
localPoint.x <= shape.props.w &&
|
|
312
|
+
localPoint.y <= shape.props.h
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function updateContentDrag(
|
|
317
|
+
editor: Editor,
|
|
318
|
+
event: PointerEditorEvent,
|
|
319
|
+
session: ImageCropContentDragSession,
|
|
320
|
+
): void {
|
|
321
|
+
if (!session.hasDragged) {
|
|
322
|
+
if (
|
|
323
|
+
session.startPagePoint.dist2(event.pagePoint) <
|
|
324
|
+
IMAGE_CROP_DRAG_START_DISTANCE_SQUARED
|
|
325
|
+
) {
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
editor.history.mark(IMAGE_CROP_TRANSLATE_HISTORY_MARK);
|
|
329
|
+
session.hasDragged = true;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const currentShape = editor.getShape(session.shapeId);
|
|
333
|
+
if (!isImageShapeRecord(currentShape)) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const update: ShapeUpdate | null = getImageCropUpdateFromContentDrag({
|
|
338
|
+
currentPagePoint: event.pagePoint,
|
|
339
|
+
pageToLocal: session.startPageToLocal,
|
|
340
|
+
shape: session.startShape,
|
|
341
|
+
shiftKey: event.shiftKey,
|
|
342
|
+
startCrop: session.startCrop,
|
|
343
|
+
startPagePoint: session.startPagePoint,
|
|
344
|
+
});
|
|
345
|
+
if (!update) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
editor.commands.updateShapes([update], { source: "user" });
|
|
350
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ImageShapeRecord } from "./types";
|
|
2
|
+
|
|
3
|
+
const imageCropSessionBaselineByShapeId = new Map<
|
|
4
|
+
ImageShapeRecord["id"],
|
|
5
|
+
ImageShapeRecord
|
|
6
|
+
>();
|
|
7
|
+
|
|
8
|
+
export function rememberImageCropSessionBaseline(
|
|
9
|
+
shape: ImageShapeRecord,
|
|
10
|
+
): void {
|
|
11
|
+
imageCropSessionBaselineByShapeId.set(shape.id, {
|
|
12
|
+
...shape,
|
|
13
|
+
props: {
|
|
14
|
+
...shape.props,
|
|
15
|
+
crop: shape.props.crop
|
|
16
|
+
? {
|
|
17
|
+
topLeft: { ...shape.props.crop.topLeft },
|
|
18
|
+
bottomRight: { ...shape.props.crop.bottomRight },
|
|
19
|
+
}
|
|
20
|
+
: null,
|
|
21
|
+
},
|
|
22
|
+
meta: {
|
|
23
|
+
...shape.meta,
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getImageCropSessionBaseline(
|
|
29
|
+
shapeId: ImageShapeRecord["id"],
|
|
30
|
+
): ImageShapeRecord | null {
|
|
31
|
+
return imageCropSessionBaselineByShapeId.get(shapeId) ?? null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function clearImageCropSessionBaseline(
|
|
35
|
+
shapeId: ImageShapeRecord["id"],
|
|
36
|
+
): void {
|
|
37
|
+
imageCropSessionBaselineByShapeId.delete(shapeId);
|
|
38
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineLandExtension,
|
|
3
|
+
type LandFeatureBundle,
|
|
4
|
+
} from "@land/canvas-plugin-api";
|
|
5
|
+
import { createImageCanvasPlugin } from "./canvas";
|
|
6
|
+
import { createImageCorePlugin } from "./core";
|
|
7
|
+
|
|
8
|
+
export function createImageFeatureBundle(): LandFeatureBundle {
|
|
9
|
+
return {
|
|
10
|
+
id: "land.image.bundle",
|
|
11
|
+
corePlugins: [createImageCorePlugin()],
|
|
12
|
+
canvasPlugins: [createImageCanvasPlugin()],
|
|
13
|
+
shapeTypes: ["image"],
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const Image = defineLandExtension({
|
|
18
|
+
name: "image",
|
|
19
|
+
createFeature: createImageFeatureBundle,
|
|
20
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export {
|
|
2
|
+
IMAGE_CROP_ASPECT_RATIO_TOOLBAR_ACTION_ID,
|
|
3
|
+
IMAGE_CROP_DONE_TOOLBAR_ACTION_ID,
|
|
4
|
+
IMAGE_CROP_RESET_TOOLBAR_ACTION_ID,
|
|
5
|
+
IMAGE_CROP_TOOLBAR_ACTION_ID,
|
|
6
|
+
IMAGE_CROP_ZOOM_TOOLBAR_ACTION_ID,
|
|
7
|
+
IMAGE_DOWNLOAD_TOOLBAR_ACTION_ID,
|
|
8
|
+
IMAGE_INSERT_TOOLBAR_ACTION_ID,
|
|
9
|
+
IMAGE_REPLACE_TOOLBAR_ACTION_ID,
|
|
10
|
+
ImageShapeSvgExporter,
|
|
11
|
+
createImageCorePlugin,
|
|
12
|
+
isImageCropToolbarSession,
|
|
13
|
+
} from "./core";
|
|
14
|
+
export { createImageCanvasPlugin } from "./canvas";
|
|
15
|
+
export {
|
|
16
|
+
createImageCropInteractionHandler,
|
|
17
|
+
createImageCropSelectionHandleInteractionHandler,
|
|
18
|
+
} from "./cropInteractions";
|
|
19
|
+
export { getImageCropModeMarkId } from "./crop";
|
|
20
|
+
export { Image, createImageFeatureBundle } from "./feature";
|
|
21
|
+
export { ImageShapeUtil } from "./ImageShapeUtil";
|
|
22
|
+
export type {
|
|
23
|
+
ImageAssetProps,
|
|
24
|
+
ImageAssetRecord,
|
|
25
|
+
ImageShapeCrop,
|
|
26
|
+
ImageShapeProps,
|
|
27
|
+
ImageShapeRecord,
|
|
28
|
+
} from "./types";
|
|
29
|
+
export { isImageAssetRecord, isImageShapeRecord } from "./types";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isAssetRecord,
|
|
3
|
+
isShapeRecord,
|
|
4
|
+
type AssetId,
|
|
5
|
+
type AssetRecordBase,
|
|
6
|
+
type BaseRecord,
|
|
7
|
+
type ShapeRecordBase,
|
|
8
|
+
} from "@land/canvas-core";
|
|
9
|
+
|
|
10
|
+
export interface ImageAssetProps {
|
|
11
|
+
w: number;
|
|
12
|
+
h: number;
|
|
13
|
+
name: string;
|
|
14
|
+
storageKey: string;
|
|
15
|
+
mimeType: string | null;
|
|
16
|
+
fileSize?: number;
|
|
17
|
+
isAnimated: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ImageAssetRecord
|
|
21
|
+
extends AssetRecordBase<"image", ImageAssetProps> {
|
|
22
|
+
type: "image";
|
|
23
|
+
props: ImageAssetProps;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ImageShapeCrop {
|
|
27
|
+
topLeft: { x: number; y: number };
|
|
28
|
+
bottomRight: { x: number; y: number };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ImageShapeProps {
|
|
32
|
+
w: number;
|
|
33
|
+
h: number;
|
|
34
|
+
assetId: AssetId | null;
|
|
35
|
+
crop: ImageShapeCrop | null;
|
|
36
|
+
altText: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ImageShapeRecord
|
|
40
|
+
extends ShapeRecordBase<"image", ImageShapeProps> {
|
|
41
|
+
type: "image";
|
|
42
|
+
props: ImageShapeProps;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function isImageAssetRecord(
|
|
46
|
+
record: BaseRecord | null | undefined,
|
|
47
|
+
): record is ImageAssetRecord {
|
|
48
|
+
return isAssetRecord(record) && record.type === "image";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function isImageShapeRecord(
|
|
52
|
+
record: BaseRecord | null | undefined,
|
|
53
|
+
): record is ImageShapeRecord {
|
|
54
|
+
return isShapeRecord(record) && record.type === "image";
|
|
55
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { memo, useEffect, useRef } from "react";
|
|
2
|
+
import type { Editor } from "@land/canvas-core";
|
|
3
|
+
import {
|
|
4
|
+
getDropPagePoint,
|
|
5
|
+
getFirstImageFileFromDataTransfer,
|
|
6
|
+
hasImageFileItem,
|
|
7
|
+
isEditableEventTarget,
|
|
8
|
+
} from "./imageHostActions";
|
|
9
|
+
import type { ImageAssetService } from "./imageAssetService";
|
|
10
|
+
import { useImageImport } from "./useImageImport";
|
|
11
|
+
import type { ImageToolbarHostBridge } from "./imageToolbarHostBridge";
|
|
12
|
+
|
|
13
|
+
export const ImageImportController = memo(function ImageImportController({
|
|
14
|
+
editor,
|
|
15
|
+
hostBridge,
|
|
16
|
+
imageAssetService,
|
|
17
|
+
persistEditorSnapshot,
|
|
18
|
+
}: {
|
|
19
|
+
editor: Editor;
|
|
20
|
+
hostBridge?: ImageToolbarHostBridge;
|
|
21
|
+
imageAssetService: ImageAssetService;
|
|
22
|
+
persistEditorSnapshot?: (editor: Editor) => Promise<void>;
|
|
23
|
+
}) {
|
|
24
|
+
const insertImageInputRef = useRef<HTMLInputElement | null>(null);
|
|
25
|
+
const replaceImageInputRef = useRef<HTMLInputElement | null>(null);
|
|
26
|
+
const { insertImageFromFile, replaceSelectedImage } = useImageImport({
|
|
27
|
+
editor,
|
|
28
|
+
imageAssetService,
|
|
29
|
+
persistEditorSnapshot,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (!hostBridge) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
return hostBridge.register({
|
|
37
|
+
openInsertFilePicker() {
|
|
38
|
+
insertImageInputRef.current?.click();
|
|
39
|
+
},
|
|
40
|
+
openReplaceFilePicker() {
|
|
41
|
+
replaceImageInputRef.current?.click();
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}, [hostBridge]);
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
const handlePaste = (event: ClipboardEvent) => {
|
|
48
|
+
if (editor.shapeEditingStateSignal.get().session) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (isEditableEventTarget(event.target)) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const file = getFirstImageFileFromDataTransfer(event.clipboardData);
|
|
55
|
+
if (!file) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
event.preventDefault();
|
|
59
|
+
void insertImageFromFile({ file, source: "paste" });
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const handleDragOver = (event: DragEvent) => {
|
|
63
|
+
if (editor.shapeEditingStateSignal.get().session) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (isEditableEventTarget(event.target)) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (!hasImageFileItem(event.dataTransfer)) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
if (event.dataTransfer) {
|
|
74
|
+
event.dataTransfer.dropEffect = "copy";
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const handleDrop = (event: DragEvent) => {
|
|
79
|
+
if (editor.shapeEditingStateSignal.get().session) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (isEditableEventTarget(event.target)) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const file = getFirstImageFileFromDataTransfer(event.dataTransfer);
|
|
86
|
+
if (!file) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
event.preventDefault();
|
|
90
|
+
const pagePoint = getDropPagePoint(editor, event);
|
|
91
|
+
void insertImageFromFile({
|
|
92
|
+
file,
|
|
93
|
+
pagePoint,
|
|
94
|
+
source: "drop",
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
window.addEventListener("paste", handlePaste);
|
|
99
|
+
window.addEventListener("dragover", handleDragOver);
|
|
100
|
+
window.addEventListener("drop", handleDrop);
|
|
101
|
+
return () => {
|
|
102
|
+
window.removeEventListener("paste", handlePaste);
|
|
103
|
+
window.removeEventListener("dragover", handleDragOver);
|
|
104
|
+
window.removeEventListener("drop", handleDrop);
|
|
105
|
+
};
|
|
106
|
+
}, [editor, insertImageFromFile]);
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<>
|
|
110
|
+
<input
|
|
111
|
+
ref={insertImageInputRef}
|
|
112
|
+
type="file"
|
|
113
|
+
accept="image/*"
|
|
114
|
+
hidden
|
|
115
|
+
data-ui="insert-image-file-input"
|
|
116
|
+
onChange={(event) => {
|
|
117
|
+
const file = event.currentTarget.files?.[0] ?? null;
|
|
118
|
+
event.currentTarget.value = "";
|
|
119
|
+
if (file) {
|
|
120
|
+
void insertImageFromFile({
|
|
121
|
+
file,
|
|
122
|
+
source: "file-picker",
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}}
|
|
126
|
+
/>
|
|
127
|
+
<input
|
|
128
|
+
ref={replaceImageInputRef}
|
|
129
|
+
type="file"
|
|
130
|
+
accept="image/*"
|
|
131
|
+
hidden
|
|
132
|
+
data-ui="replace-image-file-input"
|
|
133
|
+
onChange={(event) => {
|
|
134
|
+
const file = event.currentTarget.files?.[0] ?? null;
|
|
135
|
+
event.currentTarget.value = "";
|
|
136
|
+
if (file) {
|
|
137
|
+
void replaceSelectedImage(file);
|
|
138
|
+
}
|
|
139
|
+
}}
|
|
140
|
+
/>
|
|
141
|
+
</>
|
|
142
|
+
);
|
|
143
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Editor } from "@land/canvas-core";
|
|
2
|
+
import {
|
|
3
|
+
createProductServiceToken,
|
|
4
|
+
type ProductAssetResolverService,
|
|
5
|
+
type ProductServiceFactory,
|
|
6
|
+
} from "@land/canvas-product-react";
|
|
7
|
+
import {
|
|
8
|
+
cleanupUnreferencedImageBlobs,
|
|
9
|
+
createImageObjectUrlStore,
|
|
10
|
+
type ImageObjectUrlStore,
|
|
11
|
+
} from "./imageBlobStore";
|
|
12
|
+
|
|
13
|
+
export interface ImageAssetService extends ProductAssetResolverService {
|
|
14
|
+
objectUrls: ImageObjectUrlStore;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const imageAssetServiceToken =
|
|
18
|
+
createProductServiceToken<ImageAssetService>("plugin-image.asset-service");
|
|
19
|
+
|
|
20
|
+
export function localImageAssetService(): ProductServiceFactory<ImageAssetService> {
|
|
21
|
+
return {
|
|
22
|
+
token: imageAssetServiceToken,
|
|
23
|
+
create() {
|
|
24
|
+
const objectUrls = createImageObjectUrlStore();
|
|
25
|
+
return {
|
|
26
|
+
objectUrls,
|
|
27
|
+
cleanupAfterRestore(editor: Editor) {
|
|
28
|
+
return cleanupUnreferencedImageBlobs({
|
|
29
|
+
editor,
|
|
30
|
+
objectUrls,
|
|
31
|
+
})
|
|
32
|
+
.then(() => undefined)
|
|
33
|
+
.catch((error: unknown) => {
|
|
34
|
+
console.warn(
|
|
35
|
+
"Could not clean up unreferenced image blobs.",
|
|
36
|
+
error,
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
preloadForEditor(editor: Editor) {
|
|
41
|
+
return objectUrls.preloadForEditor(editor);
|
|
42
|
+
},
|
|
43
|
+
resolveAssetUrl(editor, assetId) {
|
|
44
|
+
return objectUrls.resolveAssetUrl(editor, assetId);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
dispose(service) {
|
|
49
|
+
service.objectUrls.releaseAll();
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|