@bendyline/squisq-editor-react 2.0.1 → 2.2.0
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/LICENSE +21 -0
- package/NOTICE.md +40 -0
- package/README.md +10 -2
- package/dist/index.d.ts +683 -60
- package/dist/index.js +9648 -3750
- package/dist/monaco.js +0 -1
- package/dist/styles/index.css +1160 -241
- package/package.json +10 -6
- package/dist/index.js.map +0 -1
- package/dist/monaco.js.map +0 -1
- package/src/BlockCardView.tsx +0 -121
- package/src/BlockPreviewPanel.tsx +0 -69
- package/src/BlockPropertiesPopover.tsx +0 -222
- package/src/DocumentSettingsDialog.tsx +0 -272
- package/src/DropZoneOverlay.tsx +0 -137
- package/src/EditorContext.tsx +0 -1247
- package/src/EditorShell.tsx +0 -1334
- package/src/EmojiPicker.tsx +0 -332
- package/src/FolderView.tsx +0 -131
- package/src/Icon.tsx +0 -26
- package/src/ImageEditor.tsx +0 -395
- package/src/ImageNodeView.tsx +0 -284
- package/src/ImageViewer.tsx +0 -221
- package/src/InlineIcon.ts +0 -84
- package/src/InlinePreviewGutter.tsx +0 -590
- package/src/LinkDialog.tsx +0 -276
- package/src/MediaBin.tsx +0 -500
- package/src/MentionExtension.tsx +0 -261
- package/src/OutlinePanel.tsx +0 -547
- package/src/PlainHtmlPreview.tsx +0 -277
- package/src/PreviewControls.tsx +0 -1250
- package/src/PreviewPanel.tsx +0 -422
- package/src/RawEditor.tsx +0 -731
- package/src/RecorderEntry.tsx +0 -159
- package/src/StatusBar.tsx +0 -51
- package/src/TemplateAnnotation.ts +0 -167
- package/src/TemplateContentPreview.tsx +0 -56
- package/src/TemplatePicker.tsx +0 -1314
- package/src/ThemeCustomizerPanel.tsx +0 -304
- package/src/ThemePicker.tsx +0 -433
- package/src/TimelineBlockPreview.tsx +0 -37
- package/src/TimelineTrack.tsx +0 -671
- package/src/Toolbar.tsx +0 -2587
- package/src/Tooltip.tsx +0 -142
- package/src/TransitionPicker.tsx +0 -390
- package/src/VersionHistoryPanel.tsx +0 -359
- package/src/ViewMenuPanel.tsx +0 -189
- package/src/ViewSwitcher.tsx +0 -56
- package/src/WysiwygEditor.tsx +0 -792
- package/src/__tests__/blockProperties.test.ts +0 -92
- package/src/__tests__/blockPropertiesPopoverTheme.test.tsx +0 -33
- package/src/__tests__/blockRange.test.ts +0 -105
- package/src/__tests__/blockTagActivity.test.ts +0 -183
- package/src/__tests__/buildDocumentPreviewMarkdown.test.ts +0 -75
- package/src/__tests__/buildPreviewDocContent.test.ts +0 -48
- package/src/__tests__/buildPreviewDocTransition.test.ts +0 -92
- package/src/__tests__/canvasSurfaceTextEditing.test.tsx +0 -60
- package/src/__tests__/codeContextSectionView.test.tsx +0 -97
- package/src/__tests__/codeContextZoneManager.test.ts +0 -127
- package/src/__tests__/createShapeLayer.test.ts +0 -46
- package/src/__tests__/customLayoutManagerTheme.test.tsx +0 -34
- package/src/__tests__/detectMarkdown.test.ts +0 -73
- package/src/__tests__/diffContextSections.test.ts +0 -39
- package/src/__tests__/documentSettingsDialog.test.tsx +0 -197
- package/src/__tests__/drawingShapeRoundTrip.test.ts +0 -49
- package/src/__tests__/editorScrollHandoff.test.tsx +0 -102
- package/src/__tests__/editorScrollSync.test.ts +0 -65
- package/src/__tests__/editorShellCodeContext.test.tsx +0 -86
- package/src/__tests__/editorShellProps.test.tsx +0 -485
- package/src/__tests__/embeddedMedia.test.ts +0 -48
- package/src/__tests__/emojiPicker.test.tsx +0 -93
- package/src/__tests__/fileKind.test.ts +0 -112
- package/src/__tests__/headingTransition.test.ts +0 -176
- package/src/__tests__/imageEditAffordance.test.tsx +0 -272
- package/src/__tests__/imageEditModalContract.test.tsx +0 -119
- package/src/__tests__/imageEditorShell.test.tsx +0 -94
- package/src/__tests__/imageEditorState.test.ts +0 -171
- package/src/__tests__/inlinePreviewGutter.test.tsx +0 -62
- package/src/__tests__/inlinePreviewGutterAllBlocks.test.tsx +0 -103
- package/src/__tests__/jsonEditor.test.tsx +0 -223
- package/src/__tests__/layersPanel.test.tsx +0 -167
- package/src/__tests__/layoutChildRoundTrip.test.ts +0 -71
- package/src/__tests__/linkDialogDocPicker.test.tsx +0 -137
- package/src/__tests__/majorApiTypes.test.ts +0 -26
- package/src/__tests__/mediaAttachmentFlow.test.ts +0 -110
- package/src/__tests__/mediaBinDrop.test.tsx +0 -90
- package/src/__tests__/mediaReferences.test.ts +0 -82
- package/src/__tests__/narrationSave.test.ts +0 -148
- package/src/__tests__/outlinePanel.test.tsx +0 -169
- package/src/__tests__/outlineSource.test.ts +0 -217
- package/src/__tests__/plainHtmlPreview.test.tsx +0 -125
- package/src/__tests__/presentationMode.test.tsx +0 -290
- package/src/__tests__/previewControls.test.tsx +0 -540
- package/src/__tests__/previewPanelAudioFailure.test.tsx +0 -46
- package/src/__tests__/previewPanelPresentation.test.tsx +0 -126
- package/src/__tests__/previewPanelTransforms.test.tsx +0 -117
- package/src/__tests__/propertiesPanel.test.tsx +0 -69
- package/src/__tests__/rawEditorModelIsolation.test.ts +0 -19
- package/src/__tests__/recorderFormats.test.ts +0 -146
- package/src/__tests__/recorderMediaInsert.test.ts +0 -86
- package/src/__tests__/recorderTheme.test.tsx +0 -42
- package/src/__tests__/recorderTimingJson.test.ts +0 -41
- package/src/__tests__/resolveBlockVisual.test.ts +0 -43
- package/src/__tests__/selectionConversions.test.ts +0 -80
- package/src/__tests__/statusBar.test.tsx +0 -27
- package/src/__tests__/teleprompter.test.tsx +0 -190
- package/src/__tests__/templateAnnotationRoundTrip.test.ts +0 -99
- package/src/__tests__/templateContentPreview.test.ts +0 -101
- package/src/__tests__/templatePickerMetadata.test.ts +0 -32
- package/src/__tests__/templatePickerPortal.test.tsx +0 -62
- package/src/__tests__/timelineSource.test.ts +0 -134
- package/src/__tests__/tiptapBridge.test.ts +0 -566
- package/src/__tests__/tiptapBridgeConformance.test.ts +0 -47
- package/src/__tests__/tiptapCodeBlockRoundTrip.test.ts +0 -109
- package/src/__tests__/tiptapImageRoundTrip.test.ts +0 -73
- package/src/__tests__/toolbarSelectionConversion.test.tsx +0 -164
- package/src/__tests__/tooltip.test.tsx +0 -72
- package/src/__tests__/transformStyleId.test.ts +0 -13
- package/src/__tests__/transitionCatalog.test.ts +0 -64
- package/src/__tests__/useBlockNavigator.test.tsx +0 -67
- package/src/__tests__/useImageEditor.test.tsx +0 -226
- package/src/__tests__/useJsonEditorTokens.test.ts +0 -59
- package/src/__tests__/useMediaRecorder.test.ts +0 -302
- package/src/__tests__/useTimelineClock.test.ts +0 -21
- package/src/__tests__/versionHistory.test.tsx +0 -197
- package/src/__tests__/viewMenuPanel.test.tsx +0 -130
- package/src/__tests__/wysiwygImageUpload.test.ts +0 -69
- package/src/__tests__/wysiwygTemplateBadgeFocus.test.tsx +0 -56
- package/src/asciiDiagram/AsciiDiagramExtension.ts +0 -343
- package/src/asciiDiagram/AsciiDiagramWidget.tsx +0 -254
- package/src/asciiDiagram/RepairableDiagramExtension.ts +0 -200
- package/src/asciiDiagram/__tests__/AsciiDiagramExtension.test.ts +0 -145
- package/src/asciiDiagram/__tests__/RepairableDiagramExtension.test.ts +0 -159
- package/src/asciiDiagram/__tests__/asciiDiagramCommands.test.ts +0 -284
- package/src/asciiDiagram/__tests__/asciiDiagramOps.test.ts +0 -114
- package/src/asciiDiagram/__tests__/asciiPaste.test.ts +0 -43
- package/src/asciiDiagram/asciiDiagramCommands.ts +0 -174
- package/src/asciiDiagram/asciiDiagramData.ts +0 -149
- package/src/asciiDiagram/asciiDiagramOps.ts +0 -238
- package/src/asciiDiagram/asciiPaste.ts +0 -19
- package/src/blockProperties.ts +0 -88
- package/src/blockRange.ts +0 -132
- package/src/blockSlice.ts +0 -75
- package/src/blockTagActivity.ts +0 -233
- package/src/buildDocumentPreviewMarkdown.ts +0 -168
- package/src/buildPreviewDoc.ts +0 -405
- package/src/codeContext/CodeContextSectionView.tsx +0 -124
- package/src/codeContext/CodeContextZoneManager.ts +0 -149
- package/src/codeContext/CodeContextZones.tsx +0 -121
- package/src/codeContext/diffContextSections.ts +0 -38
- package/src/codeContext/types.ts +0 -75
- package/src/customTemplates/AddBin.tsx +0 -126
- package/src/customTemplates/CustomLayoutManager.tsx +0 -234
- package/src/customTemplates/CustomTemplateContext.tsx +0 -188
- package/src/customTemplates/LayerToolbar.tsx +0 -580
- package/src/customTemplates/ShapeGlyph.tsx +0 -47
- package/src/customTemplates/TemplateDesigner.tsx +0 -433
- package/src/customTemplates/__tests__/library.test.ts +0 -96
- package/src/customTemplates/__tests__/normalizePositions.test.ts +0 -109
- package/src/customTemplates/__tests__/shapeDefs.test.ts +0 -49
- package/src/customTemplates/__tests__/useMemoryLayerAdapter.test.ts +0 -108
- package/src/customTemplates/designer.css +0 -732
- package/src/customTemplates/index.ts +0 -31
- package/src/customTemplates/library.ts +0 -110
- package/src/customTemplates/normalizePositions.ts +0 -75
- package/src/customTemplates/shapeDefs.ts +0 -131
- package/src/customTemplates/thumbnail.tsx +0 -63
- package/src/customTemplates/tokenDefs.ts +0 -60
- package/src/customTemplates/useDocCustomTemplates.ts +0 -52
- package/src/customTemplates/useMemoryLayerAdapter.ts +0 -129
- package/src/customThemes/CustomThemeContext.tsx +0 -185
- package/src/customThemes/CustomThemeDialog.tsx +0 -316
- package/src/customThemes/ImportThemeSection.tsx +0 -178
- package/src/customThemes/__tests__/CustomThemeContext.test.tsx +0 -64
- package/src/customThemes/__tests__/CustomThemeDialog.test.tsx +0 -47
- package/src/customThemes/__tests__/customThemeLibrary.test.ts +0 -60
- package/src/customThemes/__tests__/importThemeSection.test.tsx +0 -192
- package/src/customThemes/customThemeLibrary.ts +0 -100
- package/src/customThemes/index.ts +0 -37
- package/src/customThemes/themeControls.tsx +0 -229
- package/src/customThemes/themeDraft.ts +0 -288
- package/src/customThemes/useDocCustomThemes.ts +0 -49
- package/src/detectMarkdown.ts +0 -62
- package/src/diagram/DiagramCanvas.tsx +0 -259
- package/src/diagram/DiagramMaximizedOverlay.tsx +0 -46
- package/src/diagram/diagramCommands.ts +0 -604
- package/src/diagram/diagramConstants.ts +0 -17
- package/src/diagram/types.ts +0 -35
- package/src/editorScrollSync.ts +0 -73
- package/src/embeddedMedia.ts +0 -78
- package/src/emojiData.ts +0 -1317
- package/src/fileKind.ts +0 -158
- package/src/frontmatter.ts +0 -29
- package/src/frontmatterSettings.ts +0 -23
- package/src/headingTransition.ts +0 -291
- package/src/hooks/useFileDrop.ts +0 -266
- package/src/imageEditor/CanvasSurface.tsx +0 -703
- package/src/imageEditor/ImageVersionHistoryDropdown.tsx +0 -396
- package/src/imageEditor/LayersPanel.tsx +0 -218
- package/src/imageEditor/PropertiesPanel.tsx +0 -474
- package/src/imageEditor/Toolbar.tsx +0 -459
- package/src/imageEditor/createShapeLayer.ts +0 -280
- package/src/imageEditor/icons.tsx +0 -68
- package/src/imageEditor/image-editor.css +0 -574
- package/src/imageEditor/layers/EditorImageLayer.tsx +0 -45
- package/src/imageEditor/layers/EditorShapeLayer.tsx +0 -62
- package/src/imageEditor/layers/EditorTextLayer.tsx +0 -45
- package/src/imageEditor/layers/SelectionHandles.tsx +0 -86
- package/src/imageEditor/state.ts +0 -173
- package/src/imageEditor/useImageEditor.ts +0 -385
- package/src/imageEditor/useImageEditorTokens.ts +0 -70
- package/src/index.ts +0 -470
- package/src/jsonEditor/EmbeddedRichTextField.tsx +0 -81
- package/src/jsonEditor/JsonEditor.tsx +0 -81
- package/src/jsonEditor/JsonEditorContext.tsx +0 -81
- package/src/jsonEditor/RenderNode.tsx +0 -87
- package/src/jsonEditor/editors.tsx +0 -745
- package/src/jsonEditor/index.ts +0 -2
- package/src/jsonEditor/json-editor.css +0 -463
- package/src/jsonEditor/useJsonEditorTokens.ts +0 -33
- package/src/mediaDragMime.ts +0 -32
- package/src/mediaEntries.ts +0 -12
- package/src/mediaReferences.ts +0 -299
- package/src/monaco.ts +0 -35
- package/src/monacoWorkers.ts +0 -89
- package/src/outlineSource.ts +0 -171
- package/src/presentation/PresentationMode.tsx +0 -596
- package/src/rawEditorIsolation.ts +0 -18
- package/src/recorder/RecorderButton.tsx +0 -80
- package/src/recorder/RecorderModal.tsx +0 -706
- package/src/recorder/RecorderPanel.tsx +0 -77
- package/src/recorder/formats.ts +0 -159
- package/src/recorder/hooks/useMediaRecorder.ts +0 -436
- package/src/recorder/hooks/useStreamPreview.ts +0 -47
- package/src/recorder/insertMediaBlock.ts +0 -30
- package/src/recorder/sources/cameraStream.ts +0 -32
- package/src/recorder/sources/micStream.ts +0 -25
- package/src/recorder/sources/screenStream.ts +0 -162
- package/src/recorder/timingJson.ts +0 -66
- package/src/resolveBlockVisual.ts +0 -39
- package/src/scene/Scene.tsx +0 -616
- package/src/scene/SceneBlockExtension.ts +0 -205
- package/src/scene/SceneBlockToolbar.tsx +0 -201
- package/src/scene/SceneBlockWidget.tsx +0 -441
- package/src/scene/ScenePropsBar.tsx +0 -85
- package/src/scene/SceneSelection.tsx +0 -111
- package/src/scene/SceneSideToolbar.tsx +0 -89
- package/src/scene/SceneViewport.tsx +0 -106
- package/src/scene/ShapePalette.tsx +0 -196
- package/src/scene/__tests__/DiagramAdapter.test.ts +0 -142
- package/src/scene/__tests__/SceneBlockExtension.test.ts +0 -33
- package/src/scene/__tests__/bezierEdit.test.ts +0 -85
- package/src/scene/__tests__/blockLayers.test.ts +0 -57
- package/src/scene/__tests__/sceneIsolation.test.tsx +0 -119
- package/src/scene/__tests__/shapeLayers.test.ts +0 -106
- package/src/scene/__tests__/useSceneHitTest.test.ts +0 -90
- package/src/scene/__tests__/useScenePanZoom.test.ts +0 -103
- package/src/scene/adapters/DiagramAdapter.ts +0 -79
- package/src/scene/adapters/DrawingAdapter.ts +0 -420
- package/src/scene/adapters/LayoutAdapter.ts +0 -313
- package/src/scene/adapters/blockLayers.ts +0 -159
- package/src/scene/commands/SceneCommand.ts +0 -84
- package/src/scene/commands/drawingCommands.ts +0 -318
- package/src/scene/commands/layoutCommands.ts +0 -301
- package/src/scene/hooks/useSceneHitTest.ts +0 -105
- package/src/scene/hooks/useScenePanZoom.ts +0 -147
- package/src/scene/hooks/useSceneSelection.ts +0 -62
- package/src/scene/index.ts +0 -90
- package/src/scene/layers/DiagramEdges.tsx +0 -142
- package/src/scene/layers/edgeGeometry.ts +0 -104
- package/src/scene/layers/nodeCard.tsx +0 -164
- package/src/scene/layers/renderLayer.tsx +0 -70
- package/src/scene/layers/shapeLayers.ts +0 -201
- package/src/scene/paths/bezierEdit.ts +0 -208
- package/src/scene/scene.css +0 -792
- package/src/scene/text/SceneTextOverlay.tsx +0 -161
- package/src/scene/text/sceneTextChannel.ts +0 -46
- package/src/scene/text/sceneTextConfig.ts +0 -31
- package/src/scene/text/sceneTiptap.ts +0 -36
- package/src/scene/text/useSceneTextEditing.ts +0 -39
- package/src/scene/tools/ConnectTool.ts +0 -209
- package/src/scene/tools/DrawingConnectTool.ts +0 -231
- package/src/scene/tools/PathTool.ts +0 -158
- package/src/scene/tools/PlaceTool.ts +0 -47
- package/src/scene/tools/SceneTool.ts +0 -85
- package/src/scene/tools/SelectTool.ts +0 -300
- package/src/scene/tools/ShapeTool.ts +0 -143
- package/src/scene/tools/TextTool.ts +0 -72
- package/src/scene/tools/TokenTool.ts +0 -95
- package/src/scene/tools/createDrawShapeTool.ts +0 -82
- package/src/selectionConversions.ts +0 -155
- package/src/styles/ascii-diagram.css +0 -79
- package/src/styles/ascii-timeline.css +0 -596
- package/src/styles/code-context.css +0 -155
- package/src/styles/diagram.css +0 -183
- package/src/styles/editor.css +0 -6811
- package/src/styles/folder-view.css +0 -210
- package/src/styles/image-edit-affordance.css +0 -201
- package/src/styles/index.css +0 -18
- package/src/styles/tree-view.css +0 -189
- package/src/teleprompter/TeleprompterControls.tsx +0 -218
- package/src/teleprompter/TeleprompterSelfView.tsx +0 -22
- package/src/teleprompter/TeleprompterSurface.tsx +0 -267
- package/src/teleprompter/TeleprompterView.tsx +0 -338
- package/src/teleprompter/canvasRenderer.ts +0 -161
- package/src/teleprompter/floatingWindow.ts +0 -352
- package/src/teleprompter/index.ts +0 -61
- package/src/teleprompter/pcmWorklet.ts +0 -62
- package/src/teleprompter/recording/insertPreamble.ts +0 -80
- package/src/teleprompter/recording/narrationSave.ts +0 -134
- package/src/teleprompter/recording/useNarrationRecorder.ts +0 -367
- package/src/teleprompter/scrollModel.ts +0 -85
- package/src/teleprompter/teleprompterTheme.ts +0 -303
- package/src/teleprompter/types.ts +0 -38
- package/src/teleprompter/useFloatingWindow.ts +0 -74
- package/src/teleprompter/useMicAnalysis.ts +0 -211
- package/src/teleprompter/useTeleprompter.ts +0 -421
- package/src/templateContentPreviewResolver.ts +0 -360
- package/src/timeline/TimelineEditorWidget.tsx +0 -861
- package/src/timeline/TimelineViewExtension.ts +0 -252
- package/src/timeline/__tests__/TimelineEditorWidget.test.tsx +0 -414
- package/src/timeline/__tests__/TimelineViewExtension.test.ts +0 -163
- package/src/timeline/__tests__/timelineCommands.test.ts +0 -359
- package/src/timeline/__tests__/timelineOps.test.ts +0 -277
- package/src/timeline/__tests__/timelinePaste.test.ts +0 -34
- package/src/timeline/timelineCommands.ts +0 -421
- package/src/timeline/timelineData.ts +0 -54
- package/src/timeline/timelineOps.ts +0 -381
- package/src/timeline/timelinePaste.ts +0 -8
- package/src/timelineSource.ts +0 -244
- package/src/tiptap/TiptapAudio.tsx +0 -86
- package/src/tiptap/TiptapVideo.tsx +0 -119
- package/src/tiptap/useResolvedMediaSrc.ts +0 -47
- package/src/tiptapBridge.ts +0 -986
- package/src/tooltipPlacement.ts +0 -13
- package/src/transformStyleId.ts +0 -17
- package/src/transitionCatalog.ts +0 -159
- package/src/treeview/TreeOutlineWidget.tsx +0 -390
- package/src/treeview/TreeViewExtension.ts +0 -250
- package/src/treeview/__tests__/TreeOutlineWidget.test.tsx +0 -156
- package/src/treeview/__tests__/TreeViewExtension.test.ts +0 -122
- package/src/treeview/__tests__/treeOps.test.ts +0 -177
- package/src/treeview/__tests__/treePaste.test.ts +0 -40
- package/src/treeview/__tests__/treeViewCommands.test.ts +0 -179
- package/src/treeview/treeOps.ts +0 -231
- package/src/treeview/treePaste.ts +0 -13
- package/src/treeview/treeViewCommands.ts +0 -99
- package/src/treeview/treeViewData.ts +0 -41
- package/src/types/monaco-shims.d.ts +0 -10
- package/src/useBlockNavigator.ts +0 -153
- package/src/useHeadingLayout.ts +0 -294
- package/src/useMonacoLoader.ts +0 -82
- package/src/useTimelineClock.ts +0 -76
- package/src/utils/collectInlineFontAwesomeCss.ts +0 -69
- package/src/utils/dropUtils.ts +0 -136
- package/src/utils/normalizeMalformedAssetUrl.ts +0 -22
- package/src/wysiwygImageUpload.ts +0 -113
|
@@ -1,703 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CanvasSurface — the SVG editing surface for `<ImageEditor>`.
|
|
3
|
-
*
|
|
4
|
-
* Renders the doc's canvas (with background and per-layer rendering) in
|
|
5
|
-
* an `<svg>` whose `viewBox` matches the canvas dimensions. Pointer
|
|
6
|
-
* events are normalized to canvas coordinates and dispatched as either
|
|
7
|
-
* selection / drag / resize gestures (select tool) or as a crop-rect
|
|
8
|
-
* gesture (crop tool).
|
|
9
|
-
*
|
|
10
|
-
* Layer rendering uses small purpose-built renderers in `./layers/` for
|
|
11
|
-
* image / text / shape, which don't need media-context lookup, animation,
|
|
12
|
-
* or blockTime-driven reflow. `path` layers (the full drawing-shape
|
|
13
|
-
* vocabulary) reuse the shared `@bendyline/squisq-react` `PathLayer` so the
|
|
14
|
-
* image editor draws named shapes with exactly the same code as drawings —
|
|
15
|
-
* the canvas is the viewport and `blockTime` is 0 (no animation).
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
|
|
19
|
-
import { PathLayer } from '@bendyline/squisq-react';
|
|
20
|
-
import type { ImageEditDoc, ImageEditLayer } from '@bendyline/squisq/schemas';
|
|
21
|
-
import type { CanvasRect, ImageEditorAction, ImageEditorTool } from './state.js';
|
|
22
|
-
import { EditorImageLayer } from './layers/EditorImageLayer.js';
|
|
23
|
-
import { EditorTextLayer } from './layers/EditorTextLayer.js';
|
|
24
|
-
import { EditorShapeLayer } from './layers/EditorShapeLayer.js';
|
|
25
|
-
import { SelectionHandles, type Handle } from './layers/SelectionHandles.js';
|
|
26
|
-
|
|
27
|
-
export interface CanvasSurfaceProps {
|
|
28
|
-
doc: ImageEditDoc;
|
|
29
|
-
selectedLayerId: string | null;
|
|
30
|
-
tool: ImageEditorTool;
|
|
31
|
-
resolveAssetUrl: (path: string) => Promise<string>;
|
|
32
|
-
dispatch: (action: ImageEditorAction) => void;
|
|
33
|
-
/** When the text tool drops a new layer at a given canvas point. */
|
|
34
|
-
onCreateTextAt?: (x: number, y: number) => void;
|
|
35
|
-
/** When the shape tool drops a new layer at a given canvas point (click placement). */
|
|
36
|
-
onCreateShapeAt?: (x: number, y: number) => void;
|
|
37
|
-
/**
|
|
38
|
-
* When the shape tool finishes a drag gesture (drag-to-draw placement).
|
|
39
|
-
* Only called when `shapeDragDraw` is true and the drag exceeds the minimum distance.
|
|
40
|
-
*/
|
|
41
|
-
onCreateShapeFromPoints?: (x1: number, y1: number, x2: number, y2: number) => void;
|
|
42
|
-
/**
|
|
43
|
-
* When true the shape tool uses drag-to-draw instead of click-to-place.
|
|
44
|
-
* Set by the host when the active shape kind is line-based (line, arrow, etc.).
|
|
45
|
-
*/
|
|
46
|
-
shapeDragDraw?: boolean;
|
|
47
|
-
/** Background fill behind the canvas (the editor "paper"). */
|
|
48
|
-
workspaceBackground?: string;
|
|
49
|
-
/** Current zoom level (1 = 100% = 1:1 pixels). */
|
|
50
|
-
zoom?: number;
|
|
51
|
-
/** Called when a zoom-rect drag or other gesture wants to change zoom. */
|
|
52
|
-
onSetZoom?: (zoom: number) => void;
|
|
53
|
-
/** Ref forwarded to the outer scrollable surface div so the host can read its size and scroll position. */
|
|
54
|
-
surfaceRef?: React.RefObject<HTMLDivElement>;
|
|
55
|
-
/**
|
|
56
|
-
* When set, immediately enters inline text editing for this layer ID.
|
|
57
|
-
* Used by the host to trigger editing after creating a new text layer.
|
|
58
|
-
*/
|
|
59
|
-
requestEditLayerId?: string | null;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
interface DragState {
|
|
63
|
-
layerId: string;
|
|
64
|
-
startCanvasX: number;
|
|
65
|
-
startCanvasY: number;
|
|
66
|
-
startBox: CanvasRect;
|
|
67
|
-
handle: Handle | 'move';
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
interface CropDragState {
|
|
71
|
-
startCanvasX: number;
|
|
72
|
-
startCanvasY: number;
|
|
73
|
-
currentX: number;
|
|
74
|
-
currentY: number;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
interface ShapeLineDragState {
|
|
78
|
-
startX: number;
|
|
79
|
-
startY: number;
|
|
80
|
-
currentX: number;
|
|
81
|
-
currentY: number;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function CanvasSurface({
|
|
85
|
-
doc,
|
|
86
|
-
selectedLayerId,
|
|
87
|
-
tool,
|
|
88
|
-
resolveAssetUrl,
|
|
89
|
-
dispatch,
|
|
90
|
-
onCreateTextAt,
|
|
91
|
-
onCreateShapeAt,
|
|
92
|
-
onCreateShapeFromPoints,
|
|
93
|
-
shapeDragDraw,
|
|
94
|
-
workspaceBackground,
|
|
95
|
-
zoom = 1,
|
|
96
|
-
onSetZoom,
|
|
97
|
-
surfaceRef,
|
|
98
|
-
requestEditLayerId,
|
|
99
|
-
}: CanvasSurfaceProps) {
|
|
100
|
-
const checkerId = `squisq-image-editor-checker-${useId().replace(/:/g, '')}`;
|
|
101
|
-
const svgRef = useRef<SVGSVGElement | null>(null);
|
|
102
|
-
const dragRef = useRef<DragState | null>(null);
|
|
103
|
-
const [, forceRender] = useState(0);
|
|
104
|
-
const [cropDrag, setCropDrag] = useState<CropDragState | null>(null);
|
|
105
|
-
const [shapeLineDrag, setShapeLineDrag] = useState<ShapeLineDragState | null>(null);
|
|
106
|
-
const internalWrapRef = useRef<HTMLDivElement | null>(null);
|
|
107
|
-
const pendingScrollRef = useRef<{ left: number; top: number } | null>(null);
|
|
108
|
-
const [zoomDrag, setZoomDrag] = useState<CropDragState | null>(null); // reuse CropDragState shape
|
|
109
|
-
const [editingLayerId, setEditingLayerId] = useState<string | null>(null);
|
|
110
|
-
const editTextareaRef = useRef<HTMLTextAreaElement>(null);
|
|
111
|
-
|
|
112
|
-
/** Convert client-space coordinates to canvas coordinates. */
|
|
113
|
-
const toCanvas = useCallback(
|
|
114
|
-
(clientX: number, clientY: number): { x: number; y: number } => {
|
|
115
|
-
const svg = svgRef.current;
|
|
116
|
-
if (!svg) return { x: 0, y: 0 };
|
|
117
|
-
const rect = svg.getBoundingClientRect();
|
|
118
|
-
const x = ((clientX - rect.left) / rect.width) * doc.canvas.width;
|
|
119
|
-
const y = ((clientY - rect.top) / rect.height) * doc.canvas.height;
|
|
120
|
-
return { x, y };
|
|
121
|
-
},
|
|
122
|
-
[doc.canvas.width, doc.canvas.height],
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
// After zoom changes, apply any pending scroll (set by zoom-rect commit).
|
|
126
|
-
useLayoutEffect(() => {
|
|
127
|
-
const pending = pendingScrollRef.current;
|
|
128
|
-
if (pending && internalWrapRef.current) {
|
|
129
|
-
pendingScrollRef.current = null;
|
|
130
|
-
internalWrapRef.current.scrollLeft = pending.left;
|
|
131
|
-
internalWrapRef.current.scrollTop = pending.top;
|
|
132
|
-
}
|
|
133
|
-
}, [zoom]);
|
|
134
|
-
|
|
135
|
-
// Helper: assign both internal and external ref
|
|
136
|
-
const setWrapRefCallback = useCallback(
|
|
137
|
-
(el: HTMLDivElement | null) => {
|
|
138
|
-
internalWrapRef.current = el;
|
|
139
|
-
if (surfaceRef) {
|
|
140
|
-
(surfaceRef as React.MutableRefObject<HTMLDivElement | null>).current = el;
|
|
141
|
-
}
|
|
142
|
-
},
|
|
143
|
-
[surfaceRef],
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
// Enter editing when the host requests it (e.g. after creating a new text layer).
|
|
147
|
-
useEffect(() => {
|
|
148
|
-
if (requestEditLayerId) setEditingLayerId(requestEditLayerId);
|
|
149
|
-
}, [requestEditLayerId]);
|
|
150
|
-
|
|
151
|
-
// Exit editing when a different layer is selected externally.
|
|
152
|
-
useEffect(() => {
|
|
153
|
-
if (editingLayerId && selectedLayerId !== editingLayerId) setEditingLayerId(null);
|
|
154
|
-
}, [selectedLayerId, editingLayerId]);
|
|
155
|
-
|
|
156
|
-
// Auto-focus the inline textarea whenever we enter editing mode.
|
|
157
|
-
useLayoutEffect(() => {
|
|
158
|
-
if (editingLayerId && editTextareaRef.current) {
|
|
159
|
-
editTextareaRef.current.focus();
|
|
160
|
-
editTextareaRef.current.select();
|
|
161
|
-
}
|
|
162
|
-
}, [editingLayerId]);
|
|
163
|
-
|
|
164
|
-
// ── Pointer handlers ───────────────────────────────────────────────────
|
|
165
|
-
const onPointerDownLayer = useCallback(
|
|
166
|
-
(e: React.PointerEvent<SVGGElement>, layer: ImageEditLayer) => {
|
|
167
|
-
if (tool !== 'select') return;
|
|
168
|
-
if (layer.locked) return;
|
|
169
|
-
e.stopPropagation();
|
|
170
|
-
const pt = toCanvas(e.clientX, e.clientY);
|
|
171
|
-
dispatch({ type: 'select', layerId: layer.id });
|
|
172
|
-
const box = layerBox(layer, doc);
|
|
173
|
-
dragRef.current = {
|
|
174
|
-
layerId: layer.id,
|
|
175
|
-
startCanvasX: pt.x,
|
|
176
|
-
startCanvasY: pt.y,
|
|
177
|
-
startBox: box,
|
|
178
|
-
handle: 'move',
|
|
179
|
-
};
|
|
180
|
-
(e.target as Element).setPointerCapture?.(e.pointerId);
|
|
181
|
-
},
|
|
182
|
-
[tool, dispatch, toCanvas, doc],
|
|
183
|
-
);
|
|
184
|
-
|
|
185
|
-
const onPointerDownHandle = useCallback(
|
|
186
|
-
(e: React.PointerEvent<SVGRectElement>, handle: Handle) => {
|
|
187
|
-
if (!selectedLayerId) return;
|
|
188
|
-
const layer = doc.layers.find((l) => l.id === selectedLayerId);
|
|
189
|
-
if (!layer || layer.locked) return;
|
|
190
|
-
e.stopPropagation();
|
|
191
|
-
const pt = toCanvas(e.clientX, e.clientY);
|
|
192
|
-
const box = layerBox(layer, doc);
|
|
193
|
-
dragRef.current = {
|
|
194
|
-
layerId: layer.id,
|
|
195
|
-
startCanvasX: pt.x,
|
|
196
|
-
startCanvasY: pt.y,
|
|
197
|
-
startBox: box,
|
|
198
|
-
handle,
|
|
199
|
-
};
|
|
200
|
-
(e.target as Element).setPointerCapture?.(e.pointerId);
|
|
201
|
-
},
|
|
202
|
-
[selectedLayerId, doc, toCanvas],
|
|
203
|
-
);
|
|
204
|
-
|
|
205
|
-
const onPointerDownEmpty = useCallback(
|
|
206
|
-
(e: React.PointerEvent<SVGSVGElement>) => {
|
|
207
|
-
const pt = toCanvas(e.clientX, e.clientY);
|
|
208
|
-
if (tool === 'select') {
|
|
209
|
-
dispatch({ type: 'select', layerId: null });
|
|
210
|
-
setEditingLayerId(null);
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
if (tool === 'zoom-rect') {
|
|
214
|
-
e.preventDefault();
|
|
215
|
-
setZoomDrag({ startCanvasX: pt.x, startCanvasY: pt.y, currentX: pt.x, currentY: pt.y });
|
|
216
|
-
(e.target as Element).setPointerCapture?.(e.pointerId);
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
if (tool === 'crop') {
|
|
220
|
-
e.preventDefault();
|
|
221
|
-
setCropDrag({ startCanvasX: pt.x, startCanvasY: pt.y, currentX: pt.x, currentY: pt.y });
|
|
222
|
-
(e.target as Element).setPointerCapture?.(e.pointerId);
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
if (tool === 'text') {
|
|
226
|
-
onCreateTextAt?.(pt.x, pt.y);
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
if (tool === 'shape') {
|
|
230
|
-
if (shapeDragDraw) {
|
|
231
|
-
e.preventDefault();
|
|
232
|
-
setShapeLineDrag({ startX: pt.x, startY: pt.y, currentX: pt.x, currentY: pt.y });
|
|
233
|
-
(e.target as Element).setPointerCapture?.(e.pointerId);
|
|
234
|
-
} else {
|
|
235
|
-
onCreateShapeAt?.(pt.x, pt.y);
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
},
|
|
239
|
-
[tool, dispatch, toCanvas, onCreateTextAt, onCreateShapeAt, shapeDragDraw],
|
|
240
|
-
);
|
|
241
|
-
|
|
242
|
-
useEffect(() => {
|
|
243
|
-
function onMove(e: PointerEvent) {
|
|
244
|
-
const drag = dragRef.current;
|
|
245
|
-
if (drag) {
|
|
246
|
-
const pt = toCanvas(e.clientX, e.clientY);
|
|
247
|
-
const dx = pt.x - drag.startCanvasX;
|
|
248
|
-
const dy = pt.y - drag.startCanvasY;
|
|
249
|
-
const next = applyHandle(drag.startBox, drag.handle, dx, dy);
|
|
250
|
-
dispatch({
|
|
251
|
-
type: 'update-layer',
|
|
252
|
-
layerId: drag.layerId,
|
|
253
|
-
patch: {
|
|
254
|
-
position: {
|
|
255
|
-
x: Math.round(next.x),
|
|
256
|
-
y: Math.round(next.y),
|
|
257
|
-
width: Math.round(next.width),
|
|
258
|
-
height: Math.round(next.height),
|
|
259
|
-
},
|
|
260
|
-
},
|
|
261
|
-
});
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
if (shapeLineDrag) {
|
|
265
|
-
const pt = toCanvas(e.clientX, e.clientY);
|
|
266
|
-
setShapeLineDrag((prev) => (prev ? { ...prev, currentX: pt.x, currentY: pt.y } : prev));
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
if (zoomDrag) {
|
|
270
|
-
const pt = toCanvas(e.clientX, e.clientY);
|
|
271
|
-
setZoomDrag((prev) => (prev ? { ...prev, currentX: pt.x, currentY: pt.y } : prev));
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
if (cropDrag) {
|
|
275
|
-
const pt = toCanvas(e.clientX, e.clientY);
|
|
276
|
-
setCropDrag((prev) => (prev ? { ...prev, currentX: pt.x, currentY: pt.y } : prev));
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
function onUp() {
|
|
280
|
-
if (dragRef.current) {
|
|
281
|
-
dragRef.current = null;
|
|
282
|
-
forceRender((n) => n + 1);
|
|
283
|
-
}
|
|
284
|
-
if (shapeLineDrag) {
|
|
285
|
-
const dist = Math.hypot(
|
|
286
|
-
shapeLineDrag.currentX - shapeLineDrag.startX,
|
|
287
|
-
shapeLineDrag.currentY - shapeLineDrag.startY,
|
|
288
|
-
);
|
|
289
|
-
if (dist >= 4 && onCreateShapeFromPoints) {
|
|
290
|
-
onCreateShapeFromPoints(
|
|
291
|
-
shapeLineDrag.startX,
|
|
292
|
-
shapeLineDrag.startY,
|
|
293
|
-
shapeLineDrag.currentX,
|
|
294
|
-
shapeLineDrag.currentY,
|
|
295
|
-
);
|
|
296
|
-
} else {
|
|
297
|
-
// Too short to be a drag — fall back to fixed-size placement.
|
|
298
|
-
onCreateShapeAt?.(shapeLineDrag.startX, shapeLineDrag.startY);
|
|
299
|
-
}
|
|
300
|
-
setShapeLineDrag(null);
|
|
301
|
-
}
|
|
302
|
-
if (zoomDrag) {
|
|
303
|
-
const rectW = Math.abs(zoomDrag.currentX - zoomDrag.startCanvasX);
|
|
304
|
-
const rectH = Math.abs(zoomDrag.currentY - zoomDrag.startCanvasY);
|
|
305
|
-
if (rectW >= 10 && rectH >= 10 && onSetZoom && internalWrapRef.current) {
|
|
306
|
-
const wrap = internalWrapRef.current;
|
|
307
|
-
const viewportW = wrap.clientWidth - 32;
|
|
308
|
-
const viewportH = wrap.clientHeight - 32;
|
|
309
|
-
const newZoom = Math.max(
|
|
310
|
-
0.0625,
|
|
311
|
-
Math.min(16, Math.min(viewportW / rectW, viewportH / rectH)),
|
|
312
|
-
);
|
|
313
|
-
const cx = Math.min(zoomDrag.startCanvasX, zoomDrag.currentX) + rectW / 2;
|
|
314
|
-
const cy = Math.min(zoomDrag.startCanvasY, zoomDrag.currentY) + rectH / 2;
|
|
315
|
-
pendingScrollRef.current = {
|
|
316
|
-
left: cx * newZoom - wrap.clientWidth / 2 + 16,
|
|
317
|
-
top: cy * newZoom - wrap.clientHeight / 2 + 16,
|
|
318
|
-
};
|
|
319
|
-
onSetZoom(newZoom);
|
|
320
|
-
dispatch({ type: 'set-tool', tool: 'select' });
|
|
321
|
-
}
|
|
322
|
-
setZoomDrag(null);
|
|
323
|
-
}
|
|
324
|
-
if (cropDrag) {
|
|
325
|
-
const rect = normalizeCropRect(cropDrag);
|
|
326
|
-
if (rect.width >= 8 && rect.height >= 8) {
|
|
327
|
-
dispatch({ type: 'crop', rect });
|
|
328
|
-
dispatch({ type: 'set-tool', tool: 'select' });
|
|
329
|
-
}
|
|
330
|
-
setCropDrag(null);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
window.addEventListener('pointermove', onMove);
|
|
334
|
-
window.addEventListener('pointerup', onUp);
|
|
335
|
-
return () => {
|
|
336
|
-
window.removeEventListener('pointermove', onMove);
|
|
337
|
-
window.removeEventListener('pointerup', onUp);
|
|
338
|
-
};
|
|
339
|
-
}, [
|
|
340
|
-
toCanvas,
|
|
341
|
-
dispatch,
|
|
342
|
-
cropDrag,
|
|
343
|
-
shapeLineDrag,
|
|
344
|
-
zoomDrag,
|
|
345
|
-
onSetZoom,
|
|
346
|
-
onCreateShapeFromPoints,
|
|
347
|
-
onCreateShapeAt,
|
|
348
|
-
]);
|
|
349
|
-
|
|
350
|
-
// ── Render ─────────────────────────────────────────────────────────────
|
|
351
|
-
const selectedLayer = selectedLayerId
|
|
352
|
-
? (doc.layers.find((l) => l.id === selectedLayerId) ?? null)
|
|
353
|
-
: null;
|
|
354
|
-
const selectedBox = selectedLayer ? layerBox(selectedLayer, doc) : null;
|
|
355
|
-
const selectionBox =
|
|
356
|
-
selectedLayer && selectedLayer.type === 'text'
|
|
357
|
-
? measureTextLayerBox(selectedLayer, selectedBox!)
|
|
358
|
-
: selectedBox;
|
|
359
|
-
|
|
360
|
-
// Expand the selection box outward so the dashed outline clears the layer's own stroke.
|
|
361
|
-
// Padding = half-stroke (so the outline sits just past the stroke edge) + a proportional
|
|
362
|
-
// visual buffer that stays consistent in screen-space across different canvas sizes.
|
|
363
|
-
const paddedSelectionBox = (() => {
|
|
364
|
-
if (!selectionBox || !selectedLayer) return null;
|
|
365
|
-
const sw =
|
|
366
|
-
selectedLayer.type === 'shape' || selectedLayer.type === 'path'
|
|
367
|
-
? (((selectedLayer.content as Record<string, unknown>)['strokeWidth'] as
|
|
368
|
-
| number
|
|
369
|
-
| undefined) ?? 0)
|
|
370
|
-
: 0;
|
|
371
|
-
const strokeHalf = sw / 2;
|
|
372
|
-
const proportional = Math.max(doc.canvas.width, doc.canvas.height) * 0.007;
|
|
373
|
-
const p = Math.ceil(strokeHalf + proportional);
|
|
374
|
-
return {
|
|
375
|
-
x: selectionBox.x - p,
|
|
376
|
-
y: selectionBox.y - p,
|
|
377
|
-
width: selectionBox.width + p * 2,
|
|
378
|
-
height: selectionBox.height + p * 2,
|
|
379
|
-
};
|
|
380
|
-
})();
|
|
381
|
-
|
|
382
|
-
return (
|
|
383
|
-
<div
|
|
384
|
-
ref={setWrapRefCallback}
|
|
385
|
-
className="squisq-image-editor-surface"
|
|
386
|
-
style={{ background: workspaceBackground ?? '#1f1f24' }}
|
|
387
|
-
>
|
|
388
|
-
<div className="squisq-image-editor-canvas-wrap">
|
|
389
|
-
<svg
|
|
390
|
-
ref={svgRef}
|
|
391
|
-
viewBox={`0 0 ${doc.canvas.width} ${doc.canvas.height}`}
|
|
392
|
-
width={Math.round(doc.canvas.width * zoom)}
|
|
393
|
-
height={Math.round(doc.canvas.height * zoom)}
|
|
394
|
-
className={`squisq-image-editor-canvas squisq-image-editor-canvas--tool-${tool}`}
|
|
395
|
-
onPointerDown={onPointerDownEmpty}
|
|
396
|
-
>
|
|
397
|
-
{/* Canvas background */}
|
|
398
|
-
<rect
|
|
399
|
-
x={0}
|
|
400
|
-
y={0}
|
|
401
|
-
width={doc.canvas.width}
|
|
402
|
-
height={doc.canvas.height}
|
|
403
|
-
fill={
|
|
404
|
-
doc.canvas.background && doc.canvas.background !== 'transparent'
|
|
405
|
-
? doc.canvas.background
|
|
406
|
-
: `url(#${checkerId})`
|
|
407
|
-
}
|
|
408
|
-
/>
|
|
409
|
-
<defs>
|
|
410
|
-
<pattern id={checkerId} width="16" height="16" patternUnits="userSpaceOnUse">
|
|
411
|
-
<rect width="16" height="16" fill="#f0f0f0" />
|
|
412
|
-
<rect width="8" height="8" fill="#d0d0d0" />
|
|
413
|
-
<rect x="8" y="8" width="8" height="8" fill="#d0d0d0" />
|
|
414
|
-
</pattern>
|
|
415
|
-
</defs>
|
|
416
|
-
|
|
417
|
-
{/* Layers, back-to-front */}
|
|
418
|
-
{doc.layers.map((layer) => {
|
|
419
|
-
if (layer.visible === false) return null;
|
|
420
|
-
const onPointerDown = (e: React.PointerEvent<SVGGElement>) =>
|
|
421
|
-
onPointerDownLayer(e, layer);
|
|
422
|
-
const onDoubleClick =
|
|
423
|
-
layer.type === 'text'
|
|
424
|
-
? (e: React.MouseEvent<SVGGElement>) => {
|
|
425
|
-
e.stopPropagation();
|
|
426
|
-
dispatch({ type: 'select', layerId: layer.id });
|
|
427
|
-
setEditingLayerId(layer.id);
|
|
428
|
-
}
|
|
429
|
-
: undefined;
|
|
430
|
-
const opacity = layer.opacity ?? 1;
|
|
431
|
-
const isEditing = editingLayerId === layer.id;
|
|
432
|
-
return (
|
|
433
|
-
<g
|
|
434
|
-
key={layer.id}
|
|
435
|
-
data-layer-id={layer.id}
|
|
436
|
-
opacity={opacity}
|
|
437
|
-
visibility={isEditing ? 'hidden' : undefined}
|
|
438
|
-
pointerEvents={isEditing ? 'none' : undefined}
|
|
439
|
-
style={{ cursor: tool === 'select' && !layer.locked ? 'move' : undefined }}
|
|
440
|
-
onPointerDown={isEditing ? undefined : onPointerDown}
|
|
441
|
-
onDoubleClick={onDoubleClick}
|
|
442
|
-
>
|
|
443
|
-
{layer.type === 'image' && (
|
|
444
|
-
<EditorImageLayer
|
|
445
|
-
layer={layer}
|
|
446
|
-
canvas={doc.canvas}
|
|
447
|
-
resolveAssetUrl={resolveAssetUrl}
|
|
448
|
-
/>
|
|
449
|
-
)}
|
|
450
|
-
{layer.type === 'text' && <EditorTextLayer layer={layer} canvas={doc.canvas} />}
|
|
451
|
-
{layer.type === 'shape' && <EditorShapeLayer layer={layer} canvas={doc.canvas} />}
|
|
452
|
-
{layer.type === 'path' && (
|
|
453
|
-
<PathLayer layer={layer} viewport={doc.canvas} blockTime={0} />
|
|
454
|
-
)}
|
|
455
|
-
</g>
|
|
456
|
-
);
|
|
457
|
-
})}
|
|
458
|
-
|
|
459
|
-
{/* Selection handles */}
|
|
460
|
-
{selectedLayer &&
|
|
461
|
-
paddedSelectionBox &&
|
|
462
|
-
tool === 'select' &&
|
|
463
|
-
!selectedLayer.locked &&
|
|
464
|
-
editingLayerId !== selectedLayer.id && (
|
|
465
|
-
<SelectionHandles
|
|
466
|
-
box={paddedSelectionBox}
|
|
467
|
-
onHandlePointerDown={onPointerDownHandle}
|
|
468
|
-
/>
|
|
469
|
-
)}
|
|
470
|
-
|
|
471
|
-
{/* Inline text editor — foreignObject overlays the hidden text layer */}
|
|
472
|
-
{editingLayerId &&
|
|
473
|
-
(() => {
|
|
474
|
-
const editLayer = doc.layers.find((l) => l.id === editingLayerId);
|
|
475
|
-
if (!editLayer || editLayer.type !== 'text') return null;
|
|
476
|
-
const pos = layerBox(editLayer, doc);
|
|
477
|
-
const textLayer = editLayer as ImageEditLayer & { type: 'text' };
|
|
478
|
-
const s = textLayer.content;
|
|
479
|
-
const editBox = measureTextLayerBox(textLayer, pos);
|
|
480
|
-
return (
|
|
481
|
-
<foreignObject
|
|
482
|
-
key={`inline-edit-${editingLayerId}`}
|
|
483
|
-
x={editBox.x}
|
|
484
|
-
y={editBox.y}
|
|
485
|
-
width={editBox.width}
|
|
486
|
-
height={editBox.height}
|
|
487
|
-
style={{ overflow: 'visible' }}
|
|
488
|
-
>
|
|
489
|
-
<textarea
|
|
490
|
-
ref={editTextareaRef}
|
|
491
|
-
value={s.text}
|
|
492
|
-
onChange={(e) => {
|
|
493
|
-
dispatch({
|
|
494
|
-
type: 'update-layer',
|
|
495
|
-
layerId: editingLayerId,
|
|
496
|
-
patch: {
|
|
497
|
-
content: { ...s, text: e.target.value },
|
|
498
|
-
} as Partial<ImageEditLayer>,
|
|
499
|
-
});
|
|
500
|
-
}}
|
|
501
|
-
onKeyDown={(e) => {
|
|
502
|
-
if (e.key === 'Escape') {
|
|
503
|
-
e.preventDefault();
|
|
504
|
-
setEditingLayerId(null);
|
|
505
|
-
}
|
|
506
|
-
}}
|
|
507
|
-
onPointerDown={(e) => e.stopPropagation()}
|
|
508
|
-
onClick={(e) => e.stopPropagation()}
|
|
509
|
-
wrap="off"
|
|
510
|
-
style={{
|
|
511
|
-
display: 'block',
|
|
512
|
-
width: '100%',
|
|
513
|
-
height: '100%',
|
|
514
|
-
padding: 0,
|
|
515
|
-
margin: 0,
|
|
516
|
-
border: 'none',
|
|
517
|
-
outline: '2px dashed rgba(59,130,246,0.8)',
|
|
518
|
-
outlineOffset: '2px',
|
|
519
|
-
resize: 'none',
|
|
520
|
-
background: 'transparent',
|
|
521
|
-
fontFamily: s.style.fontFamily ?? 'sans-serif',
|
|
522
|
-
// The foreignObject participates in the SVG viewBox transform, so its
|
|
523
|
-
// contents use canvas units and inherit zoom from the SVG exactly once.
|
|
524
|
-
fontSize: `${s.style.fontSize}px`,
|
|
525
|
-
fontWeight: s.style.fontWeight ?? 'normal',
|
|
526
|
-
color: s.style.color,
|
|
527
|
-
textAlign: (s.style.textAlign ?? 'left') as 'left' | 'center' | 'right',
|
|
528
|
-
lineHeight: s.style.lineHeight ?? 1.4,
|
|
529
|
-
caretColor: s.style.color,
|
|
530
|
-
overflow: 'hidden',
|
|
531
|
-
boxSizing: 'border-box',
|
|
532
|
-
}}
|
|
533
|
-
/>
|
|
534
|
-
</foreignObject>
|
|
535
|
-
);
|
|
536
|
-
})()}
|
|
537
|
-
|
|
538
|
-
{/* Shape drag-to-draw preview */}
|
|
539
|
-
{shapeLineDrag && (
|
|
540
|
-
<line
|
|
541
|
-
x1={shapeLineDrag.startX}
|
|
542
|
-
y1={shapeLineDrag.startY}
|
|
543
|
-
x2={shapeLineDrag.currentX}
|
|
544
|
-
y2={shapeLineDrag.currentY}
|
|
545
|
-
stroke="#39f"
|
|
546
|
-
strokeWidth={2}
|
|
547
|
-
strokeDasharray="6 4"
|
|
548
|
-
pointerEvents="none"
|
|
549
|
-
/>
|
|
550
|
-
)}
|
|
551
|
-
|
|
552
|
-
{/* Zoom-rect drag preview */}
|
|
553
|
-
{zoomDrag &&
|
|
554
|
-
(() => {
|
|
555
|
-
const r = normalizeCropRect(zoomDrag);
|
|
556
|
-
return (
|
|
557
|
-
<g pointerEvents="none">
|
|
558
|
-
<rect
|
|
559
|
-
x={r.x}
|
|
560
|
-
y={r.y}
|
|
561
|
-
width={r.width}
|
|
562
|
-
height={r.height}
|
|
563
|
-
fill="rgba(255,200,0,0.08)"
|
|
564
|
-
stroke="#f90"
|
|
565
|
-
strokeWidth={2}
|
|
566
|
-
strokeDasharray="6 4"
|
|
567
|
-
vectorEffect="non-scaling-stroke"
|
|
568
|
-
/>
|
|
569
|
-
</g>
|
|
570
|
-
);
|
|
571
|
-
})()}
|
|
572
|
-
|
|
573
|
-
{/* Crop rectangle preview */}
|
|
574
|
-
{cropDrag &&
|
|
575
|
-
(() => {
|
|
576
|
-
const r = normalizeCropRect(cropDrag);
|
|
577
|
-
return (
|
|
578
|
-
<g pointerEvents="none">
|
|
579
|
-
<rect
|
|
580
|
-
x={r.x}
|
|
581
|
-
y={r.y}
|
|
582
|
-
width={r.width}
|
|
583
|
-
height={r.height}
|
|
584
|
-
fill="rgba(255,255,255,0.05)"
|
|
585
|
-
stroke="#39f"
|
|
586
|
-
strokeWidth={2}
|
|
587
|
-
strokeDasharray="6 4"
|
|
588
|
-
/>
|
|
589
|
-
</g>
|
|
590
|
-
);
|
|
591
|
-
})()}
|
|
592
|
-
</svg>
|
|
593
|
-
</div>
|
|
594
|
-
</div>
|
|
595
|
-
);
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
// ============================================
|
|
599
|
-
// Helpers
|
|
600
|
-
// ============================================
|
|
601
|
-
|
|
602
|
-
/** Resolve a layer's pixel box, ignoring anchor/percentage strings (the editor authors numeric coords). */
|
|
603
|
-
function layerBox(layer: ImageEditLayer, doc: ImageEditDoc): CanvasRect {
|
|
604
|
-
const p = layer.position;
|
|
605
|
-
const x = typeof p.x === 'number' ? p.x : 0;
|
|
606
|
-
const y = typeof p.y === 'number' ? p.y : 0;
|
|
607
|
-
const width = typeof p.width === 'number' ? p.width : doc.canvas.width;
|
|
608
|
-
const height = typeof p.height === 'number' ? p.height : doc.canvas.height;
|
|
609
|
-
return { x, y, width, height };
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
const MIN_DIM = 4;
|
|
613
|
-
|
|
614
|
-
function applyHandle(box: CanvasRect, handle: Handle | 'move', dx: number, dy: number): CanvasRect {
|
|
615
|
-
if (handle === 'move') return { ...box, x: box.x + dx, y: box.y + dy };
|
|
616
|
-
let { x, y, width, height } = box;
|
|
617
|
-
// Each handle adjusts a subset of (x, y, width, height).
|
|
618
|
-
if (handle.includes('w')) {
|
|
619
|
-
const newWidth = Math.max(MIN_DIM, width - dx);
|
|
620
|
-
x = x + (width - newWidth);
|
|
621
|
-
width = newWidth;
|
|
622
|
-
} else if (handle.includes('e')) {
|
|
623
|
-
width = Math.max(MIN_DIM, width + dx);
|
|
624
|
-
}
|
|
625
|
-
if (handle.includes('n')) {
|
|
626
|
-
const newHeight = Math.max(MIN_DIM, height - dy);
|
|
627
|
-
y = y + (height - newHeight);
|
|
628
|
-
height = newHeight;
|
|
629
|
-
} else if (handle.includes('s')) {
|
|
630
|
-
height = Math.max(MIN_DIM, height + dy);
|
|
631
|
-
}
|
|
632
|
-
return { x, y, width, height };
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
function normalizeCropRect(d: CropDragState): CanvasRect {
|
|
636
|
-
const x = Math.min(d.startCanvasX, d.currentX);
|
|
637
|
-
const y = Math.min(d.startCanvasY, d.currentY);
|
|
638
|
-
const width = Math.abs(d.currentX - d.startCanvasX);
|
|
639
|
-
const height = Math.abs(d.currentY - d.startCanvasY);
|
|
640
|
-
return { x, y, width, height };
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
/**
|
|
644
|
-
* Measure the visual bounding box of a text layer using a 2D canvas.
|
|
645
|
-
* Falls back to the layer's authored width/height if measurement isn't
|
|
646
|
-
* available (SSR, headless test envs without canvas). The returned box
|
|
647
|
-
* tightly wraps the rendered glyphs so the selection rectangle hugs the
|
|
648
|
-
* actual text rather than an arbitrary author-supplied frame.
|
|
649
|
-
*/
|
|
650
|
-
let measureCtx: CanvasRenderingContext2D | null | undefined;
|
|
651
|
-
function getMeasureCtx(): CanvasRenderingContext2D | null {
|
|
652
|
-
if (measureCtx !== undefined) return measureCtx;
|
|
653
|
-
if (typeof document === 'undefined') {
|
|
654
|
-
measureCtx = null;
|
|
655
|
-
return null;
|
|
656
|
-
}
|
|
657
|
-
try {
|
|
658
|
-
const c = document.createElement('canvas');
|
|
659
|
-
measureCtx = c.getContext('2d');
|
|
660
|
-
} catch {
|
|
661
|
-
measureCtx = null;
|
|
662
|
-
}
|
|
663
|
-
return measureCtx ?? null;
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
function measureTextLayerBox(
|
|
667
|
-
layer: ImageEditLayer & { type: 'text' },
|
|
668
|
-
fallback: CanvasRect,
|
|
669
|
-
): CanvasRect {
|
|
670
|
-
const ctx = getMeasureCtx();
|
|
671
|
-
if (!ctx) return fallback;
|
|
672
|
-
const { text, style } = layer.content;
|
|
673
|
-
const fontSize = style.fontSize;
|
|
674
|
-
const fontWeight = style.fontWeight ?? 'normal';
|
|
675
|
-
const fontFamily = style.fontFamily ?? 'sans-serif';
|
|
676
|
-
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
677
|
-
const lines = (text ?? '').split('\n');
|
|
678
|
-
let maxWidth = 0;
|
|
679
|
-
for (const line of lines) {
|
|
680
|
-
const w = ctx.measureText(line || ' ').width;
|
|
681
|
-
if (w > maxWidth) maxWidth = w;
|
|
682
|
-
}
|
|
683
|
-
const lineHeight = style.lineHeight ?? 1.4;
|
|
684
|
-
const lineHeightPx = fontSize * lineHeight;
|
|
685
|
-
// Use full line boxes so the SVG selection and textarea editor share
|
|
686
|
-
// identical bounds without clipping the caret or the final line.
|
|
687
|
-
const totalHeight = Math.max(1, lines.length) * lineHeightPx;
|
|
688
|
-
// Mirror the textAnchor logic in EditorTextLayer.
|
|
689
|
-
const anchor =
|
|
690
|
-
style.textAlign === 'center' ? 'middle' : style.textAlign === 'right' ? 'end' : 'start';
|
|
691
|
-
const x =
|
|
692
|
-
anchor === 'middle'
|
|
693
|
-
? fallback.x - maxWidth / 2
|
|
694
|
-
: anchor === 'end'
|
|
695
|
-
? fallback.x - maxWidth
|
|
696
|
-
: fallback.x;
|
|
697
|
-
return {
|
|
698
|
-
x,
|
|
699
|
-
y: fallback.y,
|
|
700
|
-
width: Math.max(MIN_DIM, Math.ceil(maxWidth)),
|
|
701
|
-
height: Math.max(MIN_DIM, Math.ceil(totalHeight)),
|
|
702
|
-
};
|
|
703
|
-
}
|