@nice2dev/ui-graphics 1.3.15 → 1.3.16
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/CHANGELOG.md +9 -0
- package/dist/cjs/animation/AnimatedPerson.js +470 -143
- package/dist/cjs/animation/AnimationEditor.js +289 -101
- package/dist/cjs/animation/Audience.js +83 -61
- package/dist/cjs/animation/BodyRenderer.js +69 -28
- package/dist/cjs/animation/animationHelper.js +281 -288
- package/dist/cjs/animation/characterTypes.js +200 -203
- package/dist/cjs/animation/choreoDSL.js +205 -219
- package/dist/cjs/animation/choreography.js +4 -32
- package/dist/cjs/animation/karaokeIntegration.js +68 -77
- package/dist/cjs/animation/lottieExport.js +108 -114
- package/dist/cjs/animation/motionPath.js +225 -248
- package/dist/cjs/animation/physics.js +227 -254
- package/dist/cjs/animation/rig/RigPlayer.js +53 -56
- package/dist/cjs/animation/rig/RiggedBody.js +71 -26
- package/dist/cjs/animation/rig/choreoRigBridge.js +72 -77
- package/dist/cjs/animation/rig/ik.js +13 -18
- package/dist/cjs/animation/rig/poseOps.js +34 -41
- package/dist/cjs/animation/rig/presets.js +64 -72
- package/dist/cjs/animation/rig/rigMath.js +25 -32
- package/dist/cjs/animation/rig/rigTypes.js +59 -63
- package/dist/cjs/animation/shapes/arms.js +31 -13
- package/dist/cjs/animation/shapes/eyes.js +129 -47
- package/dist/cjs/animation/shapes/hair.js +72 -45
- package/dist/cjs/animation/shapes/heads.js +131 -57
- package/dist/cjs/animation/shapes/headwear.js +47 -38
- package/dist/cjs/animation/shapes/legs.js +31 -13
- package/dist/cjs/animation/shapes/mouths.js +120 -44
- package/dist/cjs/animation/shapes/noses.js +80 -25
- package/dist/cjs/animation/shapes/outfits.js +101 -39
- package/dist/cjs/animation/shapes/torsos.js +15 -11
- package/dist/cjs/animation/spineExport.js +103 -121
- package/dist/cjs/core/LocalUI.js +108 -43
- package/dist/cjs/core/NiceBrushEngine.js +758 -552
- package/dist/cjs/core/collaboration.js +249 -251
- package/dist/cjs/core/colorBlindness.js +100 -115
- package/dist/cjs/core/dragDrop.js +155 -158
- package/dist/cjs/core/editorAsk.js +51 -41
- package/dist/cjs/core/fixes.js +140 -171
- package/dist/cjs/core/gridGuides.js +166 -201
- package/dist/cjs/core/historyVisual.js +156 -135
- package/dist/cjs/core/i18n.js +13 -14
- package/dist/cjs/core/integrations.js +238 -249
- package/dist/cjs/core/minimap.js +149 -151
- package/dist/cjs/core/plugins.js +98 -113
- package/dist/cjs/core/rtl.js +71 -83
- package/dist/cjs/core/shortcuts.js +196 -225
- package/dist/cjs/core/theme.js +142 -149
- package/dist/cjs/core/yjsCollaboration.js +232 -293
- package/dist/cjs/design/design-tools.js +12 -22
- package/dist/cjs/font/NiceFontEditor.js +877 -316
- package/dist/cjs/font/fontFormatUtils.js +388 -448
- package/dist/cjs/game/GameAsset2dEditor.js +1201 -392
- package/dist/cjs/game/GameAsset2dEditor.module.css.js +146 -5
- package/dist/cjs/game/gameAssetTypes.js +7 -16
- package/dist/cjs/game/gameAssetUtils.js +390 -413
- package/dist/cjs/game/useGameAssetEditor.js +820 -763
- package/dist/cjs/index.js +81 -84
- package/dist/cjs/nice2dev-ui-graphics.css +1 -1
- package/dist/cjs/node_modules/lib0/array.js +8 -70
- package/dist/cjs/node_modules/lib0/binary.js +3 -20
- package/dist/cjs/node_modules/lib0/broadcastchannel.js +24 -86
- package/dist/cjs/node_modules/lib0/buffer.js +15 -82
- package/dist/cjs/node_modules/lib0/conditions.js +3 -16
- package/dist/cjs/node_modules/lib0/decoding.js +108 -287
- package/dist/cjs/node_modules/lib0/dom.js +14 -44
- package/dist/cjs/node_modules/lib0/encoding.js +68 -418
- package/dist/cjs/node_modules/lib0/environment.js +26 -92
- package/dist/cjs/node_modules/lib0/error.js +5 -29
- package/dist/cjs/node_modules/lib0/function.js +27 -66
- package/dist/cjs/node_modules/lib0/iterator.js +8 -33
- package/dist/cjs/node_modules/lib0/logging.common.js +12 -22
- package/dist/cjs/node_modules/lib0/logging.js +32 -71
- package/dist/cjs/node_modules/lib0/map.js +16 -86
- package/dist/cjs/node_modules/lib0/math.js +3 -32
- package/dist/cjs/node_modules/lib0/number.js +4 -15
- package/dist/cjs/node_modules/lib0/object.js +14 -88
- package/dist/cjs/node_modules/lib0/observable.js +38 -78
- package/dist/cjs/node_modules/lib0/pair.js +3 -20
- package/dist/cjs/node_modules/lib0/prng.js +8 -77
- package/dist/cjs/node_modules/lib0/promise.js +6 -30
- package/dist/cjs/node_modules/lib0/random.js +8 -23
- package/dist/cjs/node_modules/lib0/schema.js +313 -687
- package/dist/cjs/node_modules/lib0/set.js +3 -10
- package/dist/cjs/node_modules/lib0/storage.js +20 -55
- package/dist/cjs/node_modules/lib0/string.js +17 -72
- package/dist/cjs/node_modules/lib0/symbol.js +2 -12
- package/dist/cjs/node_modules/lib0/time.js +2 -15
- package/dist/cjs/node_modules/lib0/trait/equality.js +3 -28
- package/dist/cjs/node_modules/lib0/url.js +4 -18
- package/dist/cjs/node_modules/lib0/webcrypto.js +2 -5
- package/dist/cjs/node_modules/y-protocols/auth.js +5 -19
- package/dist/cjs/node_modules/y-protocols/awareness.js +53 -116
- package/dist/cjs/node_modules/y-protocols/sync.js +16 -103
- package/dist/cjs/node_modules/y-websocket/src/y-websocket.js +77 -219
- package/dist/cjs/node_modules/yjs/dist/yjs.js +2128 -4868
- package/dist/cjs/photo/FilterThumb.js +37 -25
- package/dist/cjs/photo/NiceAITools.js +679 -467
- package/dist/cjs/photo/PhotoEditor.js +34 -20
- package/dist/cjs/photo/PhotoEditor.module.css.js +296 -5
- package/dist/cjs/photo/PhotoEditorCanvas.js +250 -91
- package/dist/cjs/photo/PhotoEditorLeftPanel.js +1086 -224
- package/dist/cjs/photo/PhotoEditorToolbar.js +135 -19
- package/dist/cjs/photo/photoEditorActions.js +89 -72
- package/dist/cjs/photo/photoEditorTypes.js +29 -32
- package/dist/cjs/photo/photoFilters.js +959 -985
- package/dist/cjs/photo/photoOverlays.js +899 -945
- package/dist/cjs/photo/photoSelection.js +427 -499
- package/dist/cjs/photo/usePhotoEditor.js +739 -741
- package/dist/cjs/pixel/HSVPicker.js +191 -175
- package/dist/cjs/pixel/PixelEditor.js +159 -48
- package/dist/cjs/pixel/PixelEditor.module.css.js +209 -5
- package/dist/cjs/pixel/PixelEditorCanvas.js +29 -16
- package/dist/cjs/pixel/PixelEditorMenuBar.js +160 -162
- package/dist/cjs/pixel/PixelEditorRightPanel.js +377 -45
- package/dist/cjs/pixel/PixelEditorStatusBar.js +47 -20
- package/dist/cjs/pixel/PixelEditorTimeline.js +106 -23
- package/dist/cjs/pixel/PixelEditorToolbar.js +99 -21
- package/dist/cjs/pixel/SpriteGeneratorPanel.js +483 -303
- package/dist/cjs/pixel/animalGeneratorPresets.js +776 -601
- package/dist/cjs/pixel/asepriteFormat.js +511 -560
- package/dist/cjs/pixel/buildingGeneratorPresets.js +693 -520
- package/dist/cjs/pixel/characterGeneratorPresets.js +644 -544
- package/dist/cjs/pixel/pixel-pro.js +52 -63
- package/dist/cjs/pixel/pixelEditorExports.js +388 -428
- package/dist/cjs/pixel/pixelEditorTypes.js +91 -108
- package/dist/cjs/pixel/pixelEditorUtils.js +519 -561
- package/dist/cjs/pixel/spriteGeneratorCore.js +206 -257
- package/dist/cjs/pixel/usePixelEditor.js +1321 -1343
- package/dist/cjs/texture/Nice3DTexturePainter.js +707 -440
- package/dist/cjs/ui/NiceUIDesigner.js +954 -527
- package/dist/cjs/vector/NiceSvgToCode.js +586 -446
- package/dist/cjs/vector/VectorEditor.js +290 -117
- package/dist/cjs/vector/VectorEditor.module.css.js +86 -5
- package/dist/cjs/vector/VectorEditorMenuBar.js +333 -275
- package/dist/cjs/vector/VectorEditorRightPanel.js +1094 -371
- package/dist/cjs/vector/VectorEditorShapeRenderer.js +198 -60
- package/dist/cjs/vector/VectorEditorStatusBar.js +39 -13
- package/dist/cjs/vector/useVectorEditor.js +848 -889
- package/dist/cjs/vector/vectorAnimation.js +110 -137
- package/dist/cjs/vector/vectorBooleanOps.js +398 -485
- package/dist/cjs/vector/vectorEditorExport.js +201 -203
- package/dist/cjs/vector/vectorEditorImport.js +173 -189
- package/dist/cjs/vector/vectorEditorTypes.js +42 -49
- package/dist/cjs/vector/vectorGradients.js +151 -174
- package/dist/cjs/vector/vectorIconExport.js +52 -59
- package/dist/cjs/vector/vectorIconImport.js +169 -219
- package/dist/esm/animation/AnimatedPerson.js +470 -141
- package/dist/esm/animation/AnimationEditor.js +290 -100
- package/dist/esm/animation/Audience.js +83 -59
- package/dist/esm/animation/BodyRenderer.js +71 -25
- package/dist/esm/animation/animationHelper.js +284 -287
- package/dist/esm/animation/characterTypes.js +204 -202
- package/dist/esm/animation/choreoDSL.js +212 -218
- package/dist/esm/animation/choreography.js +8 -31
- package/dist/esm/animation/karaokeIntegration.js +74 -76
- package/dist/esm/animation/lottieExport.js +112 -113
- package/dist/esm/animation/motionPath.js +235 -247
- package/dist/esm/animation/physics.js +235 -253
- package/dist/esm/animation/rig/RigPlayer.js +53 -54
- package/dist/esm/animation/rig/RiggedBody.js +71 -24
- package/dist/esm/animation/rig/choreoRigBridge.js +74 -76
- package/dist/esm/animation/rig/ik.js +14 -17
- package/dist/esm/animation/rig/poseOps.js +37 -40
- package/dist/esm/animation/rig/presets.js +70 -71
- package/dist/esm/animation/rig/rigMath.js +28 -31
- package/dist/esm/animation/rig/rigTypes.js +62 -62
- package/dist/esm/animation/shapes/arms.js +32 -12
- package/dist/esm/animation/shapes/eyes.js +135 -46
- package/dist/esm/animation/shapes/hair.js +80 -44
- package/dist/esm/animation/shapes/heads.js +140 -56
- package/dist/esm/animation/shapes/headwear.js +54 -37
- package/dist/esm/animation/shapes/legs.js +32 -12
- package/dist/esm/animation/shapes/mouths.js +127 -43
- package/dist/esm/animation/shapes/noses.js +87 -24
- package/dist/esm/animation/shapes/outfits.js +106 -38
- package/dist/esm/animation/shapes/torsos.js +16 -10
- package/dist/esm/animation/spineExport.js +106 -120
- package/dist/esm/core/LocalUI.js +111 -42
- package/dist/esm/core/NiceBrushEngine.js +761 -551
- package/dist/esm/core/collaboration.js +255 -250
- package/dist/esm/core/colorBlindness.js +106 -114
- package/dist/esm/core/dragDrop.js +157 -157
- package/dist/esm/core/editorAsk.js +53 -40
- package/dist/esm/core/fixes.js +145 -170
- package/dist/esm/core/gridGuides.js +178 -200
- package/dist/esm/core/historyVisual.js +158 -134
- package/dist/esm/core/i18n.js +15 -13
- package/dist/esm/core/integrations.js +244 -248
- package/dist/esm/core/minimap.js +151 -150
- package/dist/esm/core/plugins.js +102 -112
- package/dist/esm/core/rtl.js +77 -82
- package/dist/esm/core/shortcuts.js +200 -224
- package/dist/esm/core/theme.js +150 -148
- package/dist/esm/core/yjsCollaboration.js +240 -292
- package/dist/esm/design/design-tools.js +13 -21
- package/dist/esm/font/NiceFontEditor.js +880 -315
- package/dist/esm/font/fontFormatUtils.js +394 -447
- package/dist/esm/game/GameAsset2dEditor.js +1202 -391
- package/dist/esm/game/GameAsset2dEditor.module.css.js +146 -3
- package/dist/esm/game/gameAssetTypes.js +8 -15
- package/dist/esm/game/gameAssetUtils.js +408 -412
- package/dist/esm/game/useGameAssetEditor.js +821 -762
- package/dist/esm/index.js +332 -69
- package/dist/esm/nice2dev-ui-graphics.css +1 -1
- package/dist/esm/node_modules/lib0/array.js +15 -69
- package/dist/esm/node_modules/lib0/binary.js +14 -19
- package/dist/esm/node_modules/lib0/broadcastchannel.js +27 -85
- package/dist/esm/node_modules/lib0/buffer.js +20 -80
- package/dist/esm/node_modules/lib0/conditions.js +4 -15
- package/dist/esm/node_modules/lib0/decoding.js +128 -286
- package/dist/esm/node_modules/lib0/dom.js +20 -43
- package/dist/esm/node_modules/lib0/encoding.js +90 -415
- package/dist/esm/node_modules/lib0/environment.js +33 -91
- package/dist/esm/node_modules/lib0/error.js +8 -28
- package/dist/esm/node_modules/lib0/function.js +31 -65
- package/dist/esm/node_modules/lib0/iterator.js +11 -32
- package/dist/esm/node_modules/lib0/logging.common.js +22 -21
- package/dist/esm/node_modules/lib0/logging.js +44 -70
- package/dist/esm/node_modules/lib0/map.js +21 -85
- package/dist/esm/node_modules/lib0/math.js +9 -31
- package/dist/esm/node_modules/lib0/number.js +7 -14
- package/dist/esm/node_modules/lib0/object.js +26 -87
- package/dist/esm/node_modules/lib0/observable.js +40 -77
- package/dist/esm/node_modules/lib0/pair.js +5 -19
- package/dist/esm/node_modules/lib0/prng.js +15 -76
- package/dist/esm/node_modules/lib0/promise.js +7 -29
- package/dist/esm/node_modules/lib0/random.js +10 -22
- package/dist/esm/node_modules/lib0/schema.js +367 -686
- package/dist/esm/node_modules/lib0/set.js +4 -9
- package/dist/esm/node_modules/lib0/storage.js +23 -54
- package/dist/esm/node_modules/lib0/string.js +26 -71
- package/dist/esm/node_modules/lib0/symbol.js +3 -11
- package/dist/esm/node_modules/lib0/time.js +3 -14
- package/dist/esm/node_modules/lib0/trait/equality.js +5 -27
- package/dist/esm/node_modules/lib0/url.js +5 -17
- package/dist/esm/node_modules/lib0/webcrypto.js +3 -4
- package/dist/esm/node_modules/y-protocols/auth.js +7 -18
- package/dist/esm/node_modules/y-protocols/awareness.js +58 -115
- package/dist/esm/node_modules/y-protocols/sync.js +26 -102
- package/dist/esm/node_modules/y-websocket/src/y-websocket.js +82 -218
- package/dist/esm/node_modules/yjs/dist/yjs.js +2263 -4899
- package/dist/esm/photo/FilterThumb.js +37 -23
- package/dist/esm/photo/NiceAITools.js +685 -466
- package/dist/esm/photo/PhotoEditor.js +34 -18
- package/dist/esm/photo/PhotoEditor.module.css.js +296 -3
- package/dist/esm/photo/PhotoEditorCanvas.js +250 -89
- package/dist/esm/photo/PhotoEditorLeftPanel.js +1086 -222
- package/dist/esm/photo/PhotoEditorToolbar.js +135 -17
- package/dist/esm/photo/photoEditorActions.js +93 -71
- package/dist/esm/photo/photoEditorTypes.js +32 -31
- package/dist/esm/photo/photoFilters.js +967 -984
- package/dist/esm/photo/photoOverlays.js +913 -944
- package/dist/esm/photo/photoSelection.js +438 -498
- package/dist/esm/photo/usePhotoEditor.js +740 -740
- package/dist/esm/pixel/HSVPicker.js +192 -174
- package/dist/esm/pixel/PixelEditor.js +160 -47
- package/dist/esm/pixel/PixelEditor.module.css.js +209 -3
- package/dist/esm/pixel/PixelEditorCanvas.js +29 -14
- package/dist/esm/pixel/PixelEditorMenuBar.js +160 -160
- package/dist/esm/pixel/PixelEditorRightPanel.js +377 -43
- package/dist/esm/pixel/PixelEditorStatusBar.js +47 -18
- package/dist/esm/pixel/PixelEditorTimeline.js +106 -21
- package/dist/esm/pixel/PixelEditorToolbar.js +99 -19
- package/dist/esm/pixel/SpriteGeneratorPanel.js +483 -301
- package/dist/esm/pixel/animalGeneratorPresets.js +782 -600
- package/dist/esm/pixel/asepriteFormat.js +517 -559
- package/dist/esm/pixel/buildingGeneratorPresets.js +699 -519
- package/dist/esm/pixel/characterGeneratorPresets.js +650 -543
- package/dist/esm/pixel/pixel-pro.js +53 -62
- package/dist/esm/pixel/pixelEditorExports.js +397 -427
- package/dist/esm/pixel/pixelEditorTypes.js +103 -107
- package/dist/esm/pixel/pixelEditorUtils.js +551 -560
- package/dist/esm/pixel/spriteGeneratorCore.js +223 -256
- package/dist/esm/pixel/usePixelEditor.js +1322 -1342
- package/dist/esm/texture/Nice3DTexturePainter.js +709 -439
- package/dist/esm/ui/NiceUIDesigner.js +957 -526
- package/dist/esm/vector/NiceSvgToCode.js +589 -445
- package/dist/esm/vector/VectorEditor.js +291 -116
- package/dist/esm/vector/VectorEditor.module.css.js +86 -3
- package/dist/esm/vector/VectorEditorMenuBar.js +333 -273
- package/dist/esm/vector/VectorEditorRightPanel.js +1094 -369
- package/dist/esm/vector/VectorEditorShapeRenderer.js +199 -59
- package/dist/esm/vector/VectorEditorStatusBar.js +39 -11
- package/dist/esm/vector/useVectorEditor.js +849 -888
- package/dist/esm/vector/vectorAnimation.js +115 -136
- package/dist/esm/vector/vectorBooleanOps.js +401 -484
- package/dist/esm/vector/vectorEditorExport.js +207 -202
- package/dist/esm/vector/vectorEditorImport.js +174 -188
- package/dist/esm/vector/vectorEditorTypes.js +50 -48
- package/dist/esm/vector/vectorGradients.js +158 -173
- package/dist/esm/vector/vectorIconExport.js +53 -58
- package/dist/esm/vector/vectorIconImport.js +171 -218
- package/package.json +9 -15
|
@@ -1,905 +1,866 @@
|
|
|
1
|
-
import { useState, useRef, useMemo, useCallback, useEffect } from
|
|
2
|
-
import { useEditorAsk } from
|
|
3
|
-
import { booleanOperation, flattenToOutline } from
|
|
4
|
-
import { buildSVGString, exportSVGFile, exportPNGFile, exportJPGFile, exportWebPFile, exportBMPFile } from
|
|
5
|
-
import { parseSVGToShapes } from
|
|
6
|
-
import { cloneShapes,
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* useVectorEditor — extracted logic + state for VectorEditor.
|
|
10
|
-
* Keeps every useState / useRef / useCallback / useEffect in a single hook
|
|
11
|
-
* so the component file is pure layout.
|
|
12
|
-
*/
|
|
1
|
+
import { useState, useRef, useMemo, useCallback, useEffect } from "react";
|
|
2
|
+
import { useEditorAsk } from "../core/editorAsk.js";
|
|
3
|
+
import { booleanOperation, flattenToOutline } from "./vectorBooleanOps.js";
|
|
4
|
+
import { buildSVGString, exportSVGFile, exportPNGFile, exportJPGFile, exportWebPFile, exportBMPFile } from "./vectorEditorExport.js";
|
|
5
|
+
import { parseSVGToShapes } from "./vectorEditorImport.js";
|
|
6
|
+
import { cloneShapes, snapToGrid, sid } from "./vectorEditorTypes.js";
|
|
13
7
|
function useVectorEditor({ artW, artH, onSaveToLibrary }) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
setSelId(hit);
|
|
140
|
-
if (hit) {
|
|
141
|
-
drawing.current = true;
|
|
142
|
-
startPt.current = p;
|
|
143
|
-
}
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
if (tool === 'pen') {
|
|
147
|
-
penPts.current.push(p);
|
|
148
|
-
if (penPts.current.length === 1) {
|
|
149
|
-
const ns = {
|
|
150
|
-
id: sid(),
|
|
151
|
-
type: 'polyline',
|
|
152
|
-
x: p.x,
|
|
153
|
-
y: p.y,
|
|
154
|
-
w: 0,
|
|
155
|
-
h: 0,
|
|
156
|
-
points: [{ x: p.x, y: p.y }],
|
|
157
|
-
fill: 'none',
|
|
158
|
-
stroke: strokeCol,
|
|
159
|
-
strokeWidth: strokeW,
|
|
160
|
-
opacity: shapeOpacity,
|
|
161
|
-
rotation: 0,
|
|
162
|
-
};
|
|
163
|
-
pendingShape.current = ns;
|
|
164
|
-
setShapes((prev) => [...prev, ns]);
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
const pts = [...penPts.current];
|
|
168
|
-
const xs = pts.map((pp) => pp.x);
|
|
169
|
-
const ys = pts.map((pp) => pp.y);
|
|
170
|
-
setShapes((prev) => prev.map((s) => {
|
|
171
|
-
var _a;
|
|
172
|
-
return s.id === ((_a = pendingShape.current) === null || _a === void 0 ? void 0 : _a.id)
|
|
173
|
-
? {
|
|
174
|
-
...s,
|
|
175
|
-
points: pts,
|
|
176
|
-
x: Math.min(...xs),
|
|
177
|
-
y: Math.min(...ys),
|
|
178
|
-
w: Math.max(...xs) - Math.min(...xs),
|
|
179
|
-
h: Math.max(...ys) - Math.min(...ys),
|
|
180
|
-
}
|
|
181
|
-
: s;
|
|
182
|
-
}));
|
|
183
|
-
}
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
if (tool === 'text') {
|
|
187
|
-
// the text comes from a dialog: the shape is added once it is entered
|
|
188
|
-
void ask('prompt', 'Enter text:', 'Hello').then((txt) => {
|
|
189
|
-
if (!txt) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
const ns = {
|
|
193
|
-
id: sid(),
|
|
194
|
-
type: 'text',
|
|
195
|
-
x: p.x,
|
|
196
|
-
y: p.y,
|
|
197
|
-
w: txt.length * fontSize * 0.6,
|
|
198
|
-
h: fontSize,
|
|
199
|
-
...defaultBase,
|
|
200
|
-
stroke: 'none',
|
|
201
|
-
strokeWidth: 0,
|
|
202
|
-
text: txt,
|
|
203
|
-
fontSize,
|
|
204
|
-
fontFamily,
|
|
205
|
-
};
|
|
206
|
-
const next = [...shapes, ns];
|
|
207
|
-
snap(next);
|
|
208
|
-
setShapes(next);
|
|
209
|
-
setSelId(ns.id);
|
|
210
|
-
});
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
if (tool === 'polygon') {
|
|
214
|
-
const r2 = 40;
|
|
215
|
-
const ns = {
|
|
216
|
-
id: sid(),
|
|
217
|
-
type: 'polygon',
|
|
218
|
-
x: p.x - r2,
|
|
219
|
-
y: p.y - r2,
|
|
220
|
-
w: r2 * 2,
|
|
221
|
-
h: r2 * 2,
|
|
222
|
-
...defaultBase,
|
|
223
|
-
sides: polygonSides,
|
|
224
|
-
};
|
|
225
|
-
drawing.current = true;
|
|
226
|
-
startPt.current = p;
|
|
227
|
-
pendingShape.current = ns;
|
|
228
|
-
setShapes((prev) => [...prev, ns]);
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
if (tool === 'star') {
|
|
232
|
-
const r2 = 40;
|
|
233
|
-
const ns = {
|
|
234
|
-
id: sid(),
|
|
235
|
-
type: 'star',
|
|
236
|
-
x: p.x - r2,
|
|
237
|
-
y: p.y - r2,
|
|
238
|
-
w: r2 * 2,
|
|
239
|
-
h: r2 * 2,
|
|
240
|
-
...defaultBase,
|
|
241
|
-
sides: starPoints2,
|
|
242
|
-
innerRadius: starInnerRatio,
|
|
243
|
-
};
|
|
244
|
-
drawing.current = true;
|
|
245
|
-
startPt.current = p;
|
|
246
|
-
pendingShape.current = ns;
|
|
247
|
-
setShapes((prev) => [...prev, ns]);
|
|
248
|
-
return;
|
|
249
|
-
}
|
|
250
|
-
// rect / roundrect / ellipse / line / arrow — start drag
|
|
251
|
-
drawing.current = true;
|
|
252
|
-
startPt.current = p;
|
|
253
|
-
const typeMap = {
|
|
254
|
-
line: 'line',
|
|
255
|
-
arrow: 'arrow',
|
|
256
|
-
ellipse: 'ellipse',
|
|
257
|
-
roundrect: 'roundrect',
|
|
258
|
-
};
|
|
259
|
-
const shapeType = typeMap[tool] || 'rect';
|
|
260
|
-
const isLineLike = shapeType === 'line' || shapeType === 'arrow';
|
|
261
|
-
const ns = {
|
|
8
|
+
const { pendingAsk, ask } = useEditorAsk();
|
|
9
|
+
const [shapes, setShapes] = useState([]);
|
|
10
|
+
const [selId, setSelId] = useState(null);
|
|
11
|
+
const [tool, setTool] = useState("pointer");
|
|
12
|
+
const [fillCol, setFillCol] = useState("#5566cc");
|
|
13
|
+
const [strokeCol, setStrokeCol] = useState("#000000");
|
|
14
|
+
const [strokeW, setStrokeW] = useState(2);
|
|
15
|
+
const [fontSize, setFontSize] = useState(24);
|
|
16
|
+
const [fontFamily, setFontFamily] = useState("sans-serif");
|
|
17
|
+
const [shapeOpacity, setShapeOpacity] = useState(1);
|
|
18
|
+
const [cornerRadius, setCornerRadius] = useState(12);
|
|
19
|
+
const [polygonSides, setPolygonSides] = useState(6);
|
|
20
|
+
const [starPoints2, setStarPoints2] = useState(5);
|
|
21
|
+
const [starInnerRatio, setStarInnerRatio] = useState(0.4);
|
|
22
|
+
const [zoom, setZoom] = useState(1);
|
|
23
|
+
const [px, setPx] = useState(0);
|
|
24
|
+
const [py, setPy] = useState(0);
|
|
25
|
+
const [dlgSvg, setDlgSvg] = useState(false);
|
|
26
|
+
const [svgText, setSvgText] = useState("");
|
|
27
|
+
const [snapGrid, setSnapGrid] = useState(false);
|
|
28
|
+
const [gridSize, setGridSize] = useState(16);
|
|
29
|
+
const [hasClipboard, setHasClipboard] = useState(false);
|
|
30
|
+
const boxRef = useRef(null);
|
|
31
|
+
const drawing = useRef(false);
|
|
32
|
+
const dragStarted = useRef(false);
|
|
33
|
+
const startPt = useRef({ x: 0, y: 0 });
|
|
34
|
+
const pendingShape = useRef(null);
|
|
35
|
+
const penPts = useRef([]);
|
|
36
|
+
const panning = useRef(false);
|
|
37
|
+
const panStart = useRef({ mx: 0, my: 0, px: 0, py: 0 });
|
|
38
|
+
const hist = useRef([]);
|
|
39
|
+
const hIdx = useRef(-1);
|
|
40
|
+
const fInp = useRef(null);
|
|
41
|
+
const clipboard = useRef(null);
|
|
42
|
+
const shapesRef = useRef([]);
|
|
43
|
+
shapesRef.current = shapes;
|
|
44
|
+
const selShape = useMemo(() => shapes.find((s) => s.id === selId) || null, [shapes, selId]);
|
|
45
|
+
const snap = useCallback((s) => {
|
|
46
|
+
hist.current = hist.current.slice(0, hIdx.current + 1);
|
|
47
|
+
hist.current.push({ shapes: cloneShapes(s) });
|
|
48
|
+
if (hist.current.length > 50) {
|
|
49
|
+
hist.current.shift();
|
|
50
|
+
hIdx.current--;
|
|
51
|
+
}
|
|
52
|
+
hIdx.current = hist.current.length - 1;
|
|
53
|
+
}, []);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
if (hist.current.length === 0) {
|
|
56
|
+
snap([]);
|
|
57
|
+
}
|
|
58
|
+
}, [snap]);
|
|
59
|
+
const undo = useCallback(() => {
|
|
60
|
+
if (hIdx.current <= 0) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
hIdx.current--;
|
|
64
|
+
setShapes(cloneShapes(hist.current[hIdx.current].shapes));
|
|
65
|
+
}, []);
|
|
66
|
+
const redo = useCallback(() => {
|
|
67
|
+
if (hIdx.current >= hist.current.length - 1) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
hIdx.current++;
|
|
71
|
+
setShapes(cloneShapes(hist.current[hIdx.current].shapes));
|
|
72
|
+
}, []);
|
|
73
|
+
const scr2art = useCallback(
|
|
74
|
+
(cx, cy) => {
|
|
75
|
+
const r = boxRef.current?.getBoundingClientRect();
|
|
76
|
+
if (!r) {
|
|
77
|
+
return { x: 0, y: 0 };
|
|
78
|
+
}
|
|
79
|
+
const fit = Math.min(r.width / artW, r.height / artH) || 1;
|
|
80
|
+
let x = (cx - r.left - r.width / 2 - px) / (zoom * fit) + artW / 2;
|
|
81
|
+
let y = (cy - r.top - r.height / 2 - py) / (zoom * fit) + artH / 2;
|
|
82
|
+
if (snapGrid) {
|
|
83
|
+
x = snapToGrid(x, gridSize);
|
|
84
|
+
y = snapToGrid(y, gridSize);
|
|
85
|
+
}
|
|
86
|
+
return { x, y };
|
|
87
|
+
},
|
|
88
|
+
[px, py, zoom, artW, artH, snapGrid, gridSize]
|
|
89
|
+
);
|
|
90
|
+
const onDown = useCallback(
|
|
91
|
+
(e) => {
|
|
92
|
+
e.preventDefault();
|
|
93
|
+
if (e.button === 1) {
|
|
94
|
+
panning.current = true;
|
|
95
|
+
panStart.current = { mx: e.clientX, my: e.clientY, px, py };
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (e.button !== 0) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
dragStarted.current = false;
|
|
102
|
+
const p = scr2art(e.clientX, e.clientY);
|
|
103
|
+
const defaultBase = {
|
|
104
|
+
fill: fillCol,
|
|
105
|
+
stroke: strokeCol,
|
|
106
|
+
strokeWidth: strokeW,
|
|
107
|
+
opacity: shapeOpacity,
|
|
108
|
+
rotation: 0
|
|
109
|
+
};
|
|
110
|
+
if (tool === "pointer") {
|
|
111
|
+
let hit = null;
|
|
112
|
+
for (let i = shapes.length - 1; i >= 0; i--) {
|
|
113
|
+
const s = shapes[i];
|
|
114
|
+
if (s.locked || s.hidden) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (p.x >= s.x && p.y >= s.y && p.x <= s.x + s.w && p.y <= s.y + s.h) {
|
|
118
|
+
hit = s.id;
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
setSelId(hit);
|
|
123
|
+
if (hit) {
|
|
124
|
+
drawing.current = true;
|
|
125
|
+
startPt.current = p;
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (tool === "pen") {
|
|
130
|
+
penPts.current.push(p);
|
|
131
|
+
if (penPts.current.length === 1) {
|
|
132
|
+
const ns2 = {
|
|
262
133
|
id: sid(),
|
|
263
|
-
type:
|
|
134
|
+
type: "polyline",
|
|
264
135
|
x: p.x,
|
|
265
136
|
y: p.y,
|
|
266
137
|
w: 0,
|
|
267
138
|
h: 0,
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
fill: isLineLike ? 'none' : fillCol,
|
|
139
|
+
points: [{ x: p.x, y: p.y }],
|
|
140
|
+
fill: "none",
|
|
271
141
|
stroke: strokeCol,
|
|
272
142
|
strokeWidth: strokeW,
|
|
273
143
|
opacity: shapeOpacity,
|
|
274
|
-
rotation: 0
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
if (!drawing.current) {
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
const p = scr2art(e.clientX, e.clientY);
|
|
308
|
-
// Treat the gesture as a drag only once movement exceeds the threshold —
|
|
309
|
-
// protects polygon/star/rect/ellipse from being shrunk to zero by the very
|
|
310
|
-
// first mousemove event after a click that landed on the same screen pixel.
|
|
311
|
-
if (!dragStarted.current) {
|
|
312
|
-
const dxs = p.x - startPt.current.x;
|
|
313
|
-
const dys = p.y - startPt.current.y;
|
|
314
|
-
if (Math.hypot(dxs, dys) < 2) {
|
|
315
|
-
return;
|
|
316
|
-
}
|
|
317
|
-
dragStarted.current = true;
|
|
318
|
-
}
|
|
319
|
-
if (tool === 'pointer' && selId) {
|
|
320
|
-
const dx = p.x - startPt.current.x;
|
|
321
|
-
const dy = p.y - startPt.current.y;
|
|
322
|
-
startPt.current = p;
|
|
323
|
-
setShapes((prev) => prev.map((s) => {
|
|
324
|
-
var _a;
|
|
325
|
-
return s.id === selId
|
|
326
|
-
? {
|
|
327
|
-
...s,
|
|
328
|
-
x: s.x + dx,
|
|
329
|
-
y: s.y + dy,
|
|
330
|
-
x2: s.x2 != null ? s.x2 + dx : undefined,
|
|
331
|
-
y2: s.y2 != null ? s.y2 + dy : undefined,
|
|
332
|
-
points: (_a = s.points) === null || _a === void 0 ? void 0 : _a.map((pt) => ({ x: pt.x + dx, y: pt.y + dy })),
|
|
333
|
-
}
|
|
334
|
-
: s;
|
|
335
|
-
}));
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
const pending = pendingShape.current;
|
|
339
|
-
if (!pending) {
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
// The pending shape's ref follows the drag as well. onUp sizes the result from the ref,
|
|
343
|
-
// which kept the 0×0 shape of the press — so every dragged rectangle, ellipse, line and
|
|
344
|
-
// arrow counted as "tiny" and was deleted on release: nothing appeared on the canvas.
|
|
345
|
-
const resize = (patch) => {
|
|
346
|
-
pendingShape.current = { ...pending, ...patch };
|
|
347
|
-
setShapes((prev) => prev.map((s) => (s.id === pending.id ? { ...s, ...patch } : s)));
|
|
348
|
-
};
|
|
349
|
-
const t = pending.type;
|
|
350
|
-
if (t === 'polygon' || t === 'star') {
|
|
351
|
-
const cx = startPt.current.x;
|
|
352
|
-
const cy = startPt.current.y;
|
|
353
|
-
const dx = p.x - cx;
|
|
354
|
-
const dy = p.y - cy;
|
|
355
|
-
const r2 = Math.sqrt(dx * dx + dy * dy);
|
|
356
|
-
resize({ x: cx - r2, y: cy - r2, w: r2 * 2, h: r2 * 2 });
|
|
357
|
-
return;
|
|
358
|
-
}
|
|
359
|
-
if (t === 'rect' || t === 'roundrect' || t === 'ellipse') {
|
|
360
|
-
resize({
|
|
361
|
-
x: Math.min(startPt.current.x, p.x),
|
|
362
|
-
y: Math.min(startPt.current.y, p.y),
|
|
363
|
-
w: Math.abs(p.x - startPt.current.x),
|
|
364
|
-
h: Math.abs(p.y - startPt.current.y),
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
else if (t === 'line' || t === 'arrow') {
|
|
368
|
-
resize({ w: Math.abs(p.x - pending.x), h: Math.abs(p.y - pending.y), x2: p.x, y2: p.y });
|
|
369
|
-
}
|
|
370
|
-
}, [tool, scr2art, selId]);
|
|
371
|
-
const onUp = useCallback(() => {
|
|
372
|
-
var _a, _b;
|
|
373
|
-
if (panning.current) {
|
|
374
|
-
panning.current = false;
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
if (drawing.current) {
|
|
378
|
-
drawing.current = false;
|
|
379
|
-
if (pendingShape.current) {
|
|
380
|
-
const ps = pendingShape.current;
|
|
381
|
-
// Click without drag → apply a sensible default size so the user actually
|
|
382
|
-
// sees the shape they just placed (the disappearing-shape fix).
|
|
383
|
-
if (!dragStarted.current && ps.type !== 'polyline' && ps.type !== 'text') {
|
|
384
|
-
const cx = startPt.current.x;
|
|
385
|
-
const cy = startPt.current.y;
|
|
386
|
-
let patch;
|
|
387
|
-
if (ps.type === 'line' || ps.type === 'arrow') {
|
|
388
|
-
patch = { x: cx, y: cy, w: 80, h: 0, x2: cx + 80, y2: cy };
|
|
389
|
-
}
|
|
390
|
-
else if (ps.type === 'polygon' || ps.type === 'star') {
|
|
391
|
-
const r = 40;
|
|
392
|
-
patch = { x: cx - r, y: cy - r, w: r * 2, h: r * 2 };
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
// rect / roundrect / ellipse — 64×64 centered on click point
|
|
396
|
-
const s = 64;
|
|
397
|
-
patch = { x: cx - s / 2, y: cy - s / 2, w: s, h: s };
|
|
398
|
-
}
|
|
399
|
-
const psId = ps.id;
|
|
400
|
-
setShapes((prev) => prev.map((s) => (s.id === psId ? { ...s, ...patch } : s)));
|
|
401
|
-
snap(shapesRef.current.map((s) => (s.id === psId ? { ...s, ...patch } : s)));
|
|
402
|
-
setSelId(psId);
|
|
403
|
-
pendingShape.current = null;
|
|
404
|
-
dragStarted.current = false;
|
|
405
|
-
return;
|
|
406
|
-
}
|
|
407
|
-
// Real drag — still guard against zero-sized result (e.g. snap-to-grid collisions)
|
|
408
|
-
const minD = 3;
|
|
409
|
-
const isTiny = ps.type === 'line' || ps.type === 'arrow'
|
|
410
|
-
? Math.abs(((_a = ps.x2) !== null && _a !== void 0 ? _a : ps.x) - ps.x) < minD && Math.abs(((_b = ps.y2) !== null && _b !== void 0 ? _b : ps.y) - ps.y) < minD
|
|
411
|
-
: Math.abs(ps.w) < minD && Math.abs(ps.h) < minD;
|
|
412
|
-
if (isTiny) {
|
|
413
|
-
setShapes((prev) => prev.filter((s) => s.id !== ps.id));
|
|
414
|
-
pendingShape.current = null;
|
|
415
|
-
dragStarted.current = false;
|
|
416
|
-
return;
|
|
417
|
-
}
|
|
418
|
-
// the ref holds the final geometry even when the last move has not rendered yet
|
|
419
|
-
snap(shapesRef.current.map((s) => (s.id === ps.id ? ps : s)));
|
|
420
|
-
setSelId(ps.id);
|
|
421
|
-
pendingShape.current = null;
|
|
422
|
-
dragStarted.current = false;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
}, [snap]);
|
|
426
|
-
const onWheel = useCallback((e) => {
|
|
427
|
-
setZoom((z) => {
|
|
428
|
-
const n = e.deltaY < 0 ? z * 1.15 : z / 1.15;
|
|
429
|
-
return Math.max(0.1, Math.min(10, n));
|
|
430
|
-
});
|
|
431
|
-
}, []);
|
|
432
|
-
useEffect(() => {
|
|
433
|
-
const el = boxRef.current;
|
|
434
|
-
if (!el) {
|
|
435
|
-
return;
|
|
436
|
-
}
|
|
437
|
-
const handler = (e) => e.preventDefault();
|
|
438
|
-
el.addEventListener('wheel', handler, { passive: false });
|
|
439
|
-
return () => el.removeEventListener('wheel', handler);
|
|
440
|
-
}, []);
|
|
441
|
-
const onDblClick = useCallback(() => {
|
|
442
|
-
if (tool === 'pen' && pendingShape.current) {
|
|
443
|
-
snap(shapesRef.current);
|
|
444
|
-
setSelId(pendingShape.current.id);
|
|
445
|
-
pendingShape.current = null;
|
|
446
|
-
penPts.current = [];
|
|
447
|
-
}
|
|
448
|
-
}, [tool, snap]);
|
|
449
|
-
/* ── shape operations ── */
|
|
450
|
-
const deleteShape = useCallback(() => {
|
|
451
|
-
if (!selId) {
|
|
452
|
-
return;
|
|
453
|
-
}
|
|
454
|
-
const next = shapes.filter((s) => s.id !== selId);
|
|
455
|
-
snap(next);
|
|
456
|
-
setShapes(next);
|
|
457
|
-
setSelId(null);
|
|
458
|
-
}, [selId, shapes, snap]);
|
|
459
|
-
const bringToFront = useCallback(() => {
|
|
460
|
-
if (!selId) {
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
const next = [...shapes.filter((s) => s.id !== selId), shapes.find((s) => s.id === selId)];
|
|
464
|
-
snap(next);
|
|
465
|
-
setShapes(next);
|
|
466
|
-
}, [selId, shapes, snap]);
|
|
467
|
-
const sendToBack = useCallback(() => {
|
|
468
|
-
if (!selId) {
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
const next = [shapes.find((s) => s.id === selId), ...shapes.filter((s) => s.id !== selId)];
|
|
472
|
-
snap(next);
|
|
473
|
-
setShapes(next);
|
|
474
|
-
}, [selId, shapes, snap]);
|
|
475
|
-
const updateSel = useCallback((patch) => {
|
|
476
|
-
if (!selId) {
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
setShapes((prev) => prev.map((s) => (s.id === selId ? { ...s, ...patch } : s)));
|
|
480
|
-
}, [selId]);
|
|
481
|
-
/* ── clipboard ── */
|
|
482
|
-
const copyShape = useCallback(() => {
|
|
483
|
-
if (!selShape) {
|
|
484
|
-
return;
|
|
485
|
-
}
|
|
486
|
-
clipboard.current = JSON.parse(JSON.stringify(selShape));
|
|
487
|
-
setHasClipboard(true);
|
|
488
|
-
}, [selShape]);
|
|
489
|
-
const pasteShape = useCallback(() => {
|
|
490
|
-
if (!clipboard.current) {
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
493
|
-
const ns = {
|
|
494
|
-
...JSON.parse(JSON.stringify(clipboard.current)),
|
|
144
|
+
rotation: 0
|
|
145
|
+
};
|
|
146
|
+
pendingShape.current = ns2;
|
|
147
|
+
setShapes((prev) => [...prev, ns2]);
|
|
148
|
+
} else {
|
|
149
|
+
const pts = [...penPts.current];
|
|
150
|
+
const xs = pts.map((pp) => pp.x);
|
|
151
|
+
const ys = pts.map((pp) => pp.y);
|
|
152
|
+
setShapes(
|
|
153
|
+
(prev) => prev.map(
|
|
154
|
+
(s) => s.id === pendingShape.current?.id ? {
|
|
155
|
+
...s,
|
|
156
|
+
points: pts,
|
|
157
|
+
x: Math.min(...xs),
|
|
158
|
+
y: Math.min(...ys),
|
|
159
|
+
w: Math.max(...xs) - Math.min(...xs),
|
|
160
|
+
h: Math.max(...ys) - Math.min(...ys)
|
|
161
|
+
} : s
|
|
162
|
+
)
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (tool === "text") {
|
|
168
|
+
void ask("prompt", "Enter text:", "Hello").then((txt) => {
|
|
169
|
+
if (!txt) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const ns2 = {
|
|
495
173
|
id: sid(),
|
|
496
|
-
|
|
497
|
-
|
|
174
|
+
type: "text",
|
|
175
|
+
x: p.x,
|
|
176
|
+
y: p.y,
|
|
177
|
+
w: txt.length * fontSize * 0.6,
|
|
178
|
+
h: fontSize,
|
|
179
|
+
...defaultBase,
|
|
180
|
+
stroke: "none",
|
|
181
|
+
strokeWidth: 0,
|
|
182
|
+
text: txt,
|
|
183
|
+
fontSize,
|
|
184
|
+
fontFamily
|
|
185
|
+
};
|
|
186
|
+
const next = [...shapes, ns2];
|
|
187
|
+
snap(next);
|
|
188
|
+
setShapes(next);
|
|
189
|
+
setSelId(ns2.id);
|
|
190
|
+
});
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (tool === "polygon") {
|
|
194
|
+
const r2 = 40;
|
|
195
|
+
const ns2 = {
|
|
196
|
+
id: sid(),
|
|
197
|
+
type: "polygon",
|
|
198
|
+
x: p.x - r2,
|
|
199
|
+
y: p.y - r2,
|
|
200
|
+
w: r2 * 2,
|
|
201
|
+
h: r2 * 2,
|
|
202
|
+
...defaultBase,
|
|
203
|
+
sides: polygonSides
|
|
498
204
|
};
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
205
|
+
drawing.current = true;
|
|
206
|
+
startPt.current = p;
|
|
207
|
+
pendingShape.current = ns2;
|
|
208
|
+
setShapes((prev) => [...prev, ns2]);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (tool === "star") {
|
|
212
|
+
const r2 = 40;
|
|
213
|
+
const ns2 = {
|
|
214
|
+
id: sid(),
|
|
215
|
+
type: "star",
|
|
216
|
+
x: p.x - r2,
|
|
217
|
+
y: p.y - r2,
|
|
218
|
+
w: r2 * 2,
|
|
219
|
+
h: r2 * 2,
|
|
220
|
+
...defaultBase,
|
|
221
|
+
sides: starPoints2,
|
|
222
|
+
innerRadius: starInnerRatio
|
|
513
223
|
};
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
224
|
+
drawing.current = true;
|
|
225
|
+
startPt.current = p;
|
|
226
|
+
pendingShape.current = ns2;
|
|
227
|
+
setShapes((prev) => [...prev, ns2]);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
drawing.current = true;
|
|
231
|
+
startPt.current = p;
|
|
232
|
+
const typeMap = {
|
|
233
|
+
line: "line",
|
|
234
|
+
arrow: "arrow",
|
|
235
|
+
ellipse: "ellipse",
|
|
236
|
+
roundrect: "roundrect"
|
|
237
|
+
};
|
|
238
|
+
const shapeType = typeMap[tool] || "rect";
|
|
239
|
+
const isLineLike = shapeType === "line" || shapeType === "arrow";
|
|
240
|
+
const ns = {
|
|
241
|
+
id: sid(),
|
|
242
|
+
type: shapeType,
|
|
243
|
+
x: p.x,
|
|
244
|
+
y: p.y,
|
|
245
|
+
w: 0,
|
|
246
|
+
h: 0,
|
|
247
|
+
x2: p.x,
|
|
248
|
+
y2: p.y,
|
|
249
|
+
fill: isLineLike ? "none" : fillCol,
|
|
250
|
+
stroke: strokeCol,
|
|
251
|
+
strokeWidth: strokeW,
|
|
252
|
+
opacity: shapeOpacity,
|
|
253
|
+
rotation: 0,
|
|
254
|
+
rx: shapeType === "roundrect" ? cornerRadius : void 0
|
|
255
|
+
};
|
|
256
|
+
pendingShape.current = ns;
|
|
257
|
+
setShapes((prev) => [...prev, ns]);
|
|
258
|
+
},
|
|
259
|
+
[
|
|
260
|
+
tool,
|
|
261
|
+
shapes,
|
|
262
|
+
scr2art,
|
|
263
|
+
px,
|
|
264
|
+
py,
|
|
265
|
+
fillCol,
|
|
266
|
+
strokeCol,
|
|
267
|
+
strokeW,
|
|
268
|
+
fontSize,
|
|
269
|
+
fontFamily,
|
|
270
|
+
snap,
|
|
271
|
+
shapeOpacity,
|
|
272
|
+
polygonSides,
|
|
273
|
+
starPoints2,
|
|
274
|
+
starInnerRatio,
|
|
275
|
+
cornerRadius,
|
|
276
|
+
ask
|
|
277
|
+
]
|
|
278
|
+
);
|
|
279
|
+
const onMove = useCallback(
|
|
280
|
+
(e) => {
|
|
281
|
+
if (panning.current) {
|
|
282
|
+
setPx(panStart.current.px + (e.clientX - panStart.current.mx));
|
|
283
|
+
setPy(panStart.current.py + (e.clientY - panStart.current.my));
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (!drawing.current) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const p = scr2art(e.clientX, e.clientY);
|
|
290
|
+
if (!dragStarted.current) {
|
|
291
|
+
const dxs = p.x - startPt.current.x;
|
|
292
|
+
const dys = p.y - startPt.current.y;
|
|
293
|
+
if (Math.hypot(dxs, dys) < 2) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
dragStarted.current = true;
|
|
297
|
+
}
|
|
298
|
+
if (tool === "pointer" && selId) {
|
|
299
|
+
const dx = p.x - startPt.current.x;
|
|
300
|
+
const dy = p.y - startPt.current.y;
|
|
301
|
+
startPt.current = p;
|
|
302
|
+
setShapes(
|
|
303
|
+
(prev) => prev.map(
|
|
304
|
+
(s) => s.id === selId ? {
|
|
305
|
+
...s,
|
|
306
|
+
x: s.x + dx,
|
|
307
|
+
y: s.y + dy,
|
|
308
|
+
x2: s.x2 != null ? s.x2 + dx : void 0,
|
|
309
|
+
y2: s.y2 != null ? s.y2 + dy : void 0,
|
|
310
|
+
points: s.points?.map((pt) => ({ x: pt.x + dx, y: pt.y + dy }))
|
|
311
|
+
} : s
|
|
312
|
+
)
|
|
313
|
+
);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const pending = pendingShape.current;
|
|
317
|
+
if (!pending) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
const resize = (patch) => {
|
|
321
|
+
pendingShape.current = { ...pending, ...patch };
|
|
322
|
+
setShapes((prev) => prev.map((s) => s.id === pending.id ? { ...s, ...patch } : s));
|
|
323
|
+
};
|
|
324
|
+
const t = pending.type;
|
|
325
|
+
if (t === "polygon" || t === "star") {
|
|
326
|
+
const cx = startPt.current.x;
|
|
327
|
+
const cy = startPt.current.y;
|
|
328
|
+
const dx = p.x - cx;
|
|
329
|
+
const dy = p.y - cy;
|
|
330
|
+
const r2 = Math.sqrt(dx * dx + dy * dy);
|
|
331
|
+
resize({ x: cx - r2, y: cy - r2, w: r2 * 2, h: r2 * 2 });
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (t === "rect" || t === "roundrect" || t === "ellipse") {
|
|
335
|
+
resize({
|
|
336
|
+
x: Math.min(startPt.current.x, p.x),
|
|
337
|
+
y: Math.min(startPt.current.y, p.y),
|
|
338
|
+
w: Math.abs(p.x - startPt.current.x),
|
|
339
|
+
h: Math.abs(p.y - startPt.current.y)
|
|
618
340
|
});
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
341
|
+
} else if (t === "line" || t === "arrow") {
|
|
342
|
+
resize({ w: Math.abs(p.x - pending.x), h: Math.abs(p.y - pending.y), x2: p.x, y2: p.y });
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
[tool, scr2art, selId]
|
|
346
|
+
);
|
|
347
|
+
const onUp = useCallback(() => {
|
|
348
|
+
if (panning.current) {
|
|
349
|
+
panning.current = false;
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (drawing.current) {
|
|
353
|
+
drawing.current = false;
|
|
354
|
+
if (pendingShape.current) {
|
|
355
|
+
const ps = pendingShape.current;
|
|
356
|
+
if (!dragStarted.current && ps.type !== "polyline" && ps.type !== "text") {
|
|
357
|
+
const cx = startPt.current.x;
|
|
358
|
+
const cy = startPt.current.y;
|
|
359
|
+
let patch;
|
|
360
|
+
if (ps.type === "line" || ps.type === "arrow") {
|
|
361
|
+
patch = { x: cx, y: cy, w: 80, h: 0, x2: cx + 80, y2: cy };
|
|
362
|
+
} else if (ps.type === "polygon" || ps.type === "star") {
|
|
363
|
+
const r = 40;
|
|
364
|
+
patch = { x: cx - r, y: cy - r, w: r * 2, h: r * 2 };
|
|
365
|
+
} else {
|
|
366
|
+
const s = 64;
|
|
367
|
+
patch = { x: cx - s / 2, y: cy - s / 2, w: s, h: s };
|
|
368
|
+
}
|
|
369
|
+
const psId = ps.id;
|
|
370
|
+
setShapes((prev) => prev.map((s) => s.id === psId ? { ...s, ...patch } : s));
|
|
371
|
+
snap(shapesRef.current.map((s) => s.id === psId ? { ...s, ...patch } : s));
|
|
372
|
+
setSelId(psId);
|
|
373
|
+
pendingShape.current = null;
|
|
374
|
+
dragStarted.current = false;
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const minD = 3;
|
|
378
|
+
const isTiny = ps.type === "line" || ps.type === "arrow" ? Math.abs((ps.x2 ?? ps.x) - ps.x) < minD && Math.abs((ps.y2 ?? ps.y) - ps.y) < minD : Math.abs(ps.w) < minD && Math.abs(ps.h) < minD;
|
|
379
|
+
if (isTiny) {
|
|
380
|
+
setShapes((prev) => prev.filter((s) => s.id !== ps.id));
|
|
381
|
+
pendingShape.current = null;
|
|
382
|
+
dragStarted.current = false;
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
snap(shapesRef.current.map((s) => s.id === ps.id ? ps : s));
|
|
386
|
+
setSelId(ps.id);
|
|
387
|
+
pendingShape.current = null;
|
|
388
|
+
dragStarted.current = false;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}, [snap]);
|
|
392
|
+
const onWheel = useCallback((e) => {
|
|
393
|
+
setZoom((z) => {
|
|
394
|
+
const n = e.deltaY < 0 ? z * 1.15 : z / 1.15;
|
|
395
|
+
return Math.max(0.1, Math.min(10, n));
|
|
396
|
+
});
|
|
397
|
+
}, []);
|
|
398
|
+
useEffect(() => {
|
|
399
|
+
const el = boxRef.current;
|
|
400
|
+
if (!el) {
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const handler = (e) => e.preventDefault();
|
|
404
|
+
el.addEventListener("wheel", handler, { passive: false });
|
|
405
|
+
return () => el.removeEventListener("wheel", handler);
|
|
406
|
+
}, []);
|
|
407
|
+
const onDblClick = useCallback(() => {
|
|
408
|
+
if (tool === "pen" && pendingShape.current) {
|
|
409
|
+
snap(shapesRef.current);
|
|
410
|
+
setSelId(pendingShape.current.id);
|
|
411
|
+
pendingShape.current = null;
|
|
412
|
+
penPts.current = [];
|
|
413
|
+
}
|
|
414
|
+
}, [tool, snap]);
|
|
415
|
+
const deleteShape = useCallback(() => {
|
|
416
|
+
if (!selId) {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
const next = shapes.filter((s) => s.id !== selId);
|
|
420
|
+
snap(next);
|
|
421
|
+
setShapes(next);
|
|
422
|
+
setSelId(null);
|
|
423
|
+
}, [selId, shapes, snap]);
|
|
424
|
+
const bringToFront = useCallback(() => {
|
|
425
|
+
if (!selId) {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
const next = [...shapes.filter((s) => s.id !== selId), shapes.find((s) => s.id === selId)];
|
|
429
|
+
snap(next);
|
|
430
|
+
setShapes(next);
|
|
431
|
+
}, [selId, shapes, snap]);
|
|
432
|
+
const sendToBack = useCallback(() => {
|
|
433
|
+
if (!selId) {
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
const next = [shapes.find((s) => s.id === selId), ...shapes.filter((s) => s.id !== selId)];
|
|
437
|
+
snap(next);
|
|
438
|
+
setShapes(next);
|
|
439
|
+
}, [selId, shapes, snap]);
|
|
440
|
+
const updateSel = useCallback(
|
|
441
|
+
(patch) => {
|
|
442
|
+
if (!selId) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
setShapes((prev) => prev.map((s) => s.id === selId ? { ...s, ...patch } : s));
|
|
446
|
+
},
|
|
447
|
+
[selId]
|
|
448
|
+
);
|
|
449
|
+
const copyShape = useCallback(() => {
|
|
450
|
+
if (!selShape) {
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
clipboard.current = JSON.parse(JSON.stringify(selShape));
|
|
454
|
+
setHasClipboard(true);
|
|
455
|
+
}, [selShape]);
|
|
456
|
+
const pasteShape = useCallback(() => {
|
|
457
|
+
if (!clipboard.current) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
const ns = {
|
|
461
|
+
...JSON.parse(JSON.stringify(clipboard.current)),
|
|
462
|
+
id: sid(),
|
|
463
|
+
x: clipboard.current.x + 20,
|
|
464
|
+
y: clipboard.current.y + 20
|
|
465
|
+
};
|
|
466
|
+
const next = [...shapes, ns];
|
|
467
|
+
snap(next);
|
|
468
|
+
setShapes(next);
|
|
469
|
+
setSelId(ns.id);
|
|
470
|
+
}, [shapes, snap]);
|
|
471
|
+
const duplicateShape = useCallback(() => {
|
|
472
|
+
if (!selShape) {
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
const ns = {
|
|
476
|
+
...JSON.parse(JSON.stringify(selShape)),
|
|
477
|
+
id: sid(),
|
|
478
|
+
x: selShape.x + 20,
|
|
479
|
+
y: selShape.y + 20
|
|
480
|
+
};
|
|
481
|
+
const next = [...shapes, ns];
|
|
482
|
+
snap(next);
|
|
483
|
+
setShapes(next);
|
|
484
|
+
setSelId(ns.id);
|
|
485
|
+
}, [selShape, shapes, snap]);
|
|
486
|
+
const flipH = useCallback(() => {
|
|
487
|
+
if (!selId || !selShape) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
if (selShape.points) {
|
|
491
|
+
const cx = selShape.x + selShape.w / 2;
|
|
492
|
+
const next = shapes.map(
|
|
493
|
+
(s) => s.id === selId ? { ...s, points: s.points?.map((pt) => ({ x: cx * 2 - pt.x, y: pt.y })) } : s
|
|
494
|
+
);
|
|
495
|
+
snap(next);
|
|
496
|
+
setShapes(next);
|
|
497
|
+
} else if (selShape.type === "line" || selShape.type === "arrow") {
|
|
498
|
+
const next = shapes.map((s) => s.id === selId ? { ...s, x: s.x2 ?? s.x, x2: s.x } : s);
|
|
499
|
+
snap(next);
|
|
500
|
+
setShapes(next);
|
|
501
|
+
} else {
|
|
502
|
+
const newRot = selShape.rotation ? -selShape.rotation : 0;
|
|
503
|
+
const next = shapes.map((s) => s.id === selId ? { ...s, rotation: newRot } : s);
|
|
504
|
+
snap(next);
|
|
505
|
+
setShapes(next);
|
|
506
|
+
}
|
|
507
|
+
}, [selId, selShape, shapes, snap]);
|
|
508
|
+
const flipV = useCallback(() => {
|
|
509
|
+
if (!selId || !selShape) {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
if (selShape.points) {
|
|
513
|
+
const cy = selShape.y + selShape.h / 2;
|
|
514
|
+
const next = shapes.map(
|
|
515
|
+
(s) => s.id === selId ? { ...s, points: s.points?.map((pt) => ({ x: pt.x, y: cy * 2 - pt.y })) } : s
|
|
516
|
+
);
|
|
517
|
+
snap(next);
|
|
518
|
+
setShapes(next);
|
|
519
|
+
} else if (selShape.type === "line" || selShape.type === "arrow") {
|
|
520
|
+
const next = shapes.map((s) => s.id === selId ? { ...s, y: s.y2 ?? s.y, y2: s.y } : s);
|
|
521
|
+
snap(next);
|
|
522
|
+
setShapes(next);
|
|
523
|
+
} else {
|
|
524
|
+
const newRot = selShape.rotation ? (180 - selShape.rotation) % 360 : 180;
|
|
525
|
+
const next = shapes.map((s) => s.id === selId ? { ...s, rotation: newRot } : s);
|
|
526
|
+
snap(next);
|
|
527
|
+
setShapes(next);
|
|
528
|
+
}
|
|
529
|
+
}, [selId, selShape, shapes, snap]);
|
|
530
|
+
const alignShapes = useCallback(
|
|
531
|
+
(mode) => {
|
|
532
|
+
if (!selId || !selShape) {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
let patch = {};
|
|
536
|
+
switch (mode) {
|
|
537
|
+
case "left":
|
|
538
|
+
patch = { x: 0 };
|
|
539
|
+
break;
|
|
540
|
+
case "right":
|
|
541
|
+
patch = { x: artW - selShape.w };
|
|
542
|
+
break;
|
|
543
|
+
case "center":
|
|
544
|
+
patch = { x: (artW - selShape.w) / 2 };
|
|
545
|
+
break;
|
|
546
|
+
case "top":
|
|
547
|
+
patch = { y: 0 };
|
|
548
|
+
break;
|
|
549
|
+
case "bottom":
|
|
550
|
+
patch = { y: artH - selShape.h };
|
|
551
|
+
break;
|
|
552
|
+
case "middle":
|
|
553
|
+
patch = { y: (artH - selShape.h) / 2 };
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
const next = shapes.map((s) => s.id === selId ? { ...s, ...patch } : s);
|
|
557
|
+
snap(next);
|
|
558
|
+
setShapes(next);
|
|
559
|
+
},
|
|
560
|
+
[selId, selShape, shapes, snap, artW, artH]
|
|
561
|
+
);
|
|
562
|
+
const [animations, setAnimations] = useState({});
|
|
563
|
+
const [isPlaying, setIsPlaying] = useState(false);
|
|
564
|
+
const [previewTime, setPreviewTime] = useState(0);
|
|
565
|
+
const setShapeAnimation = useCallback((shapeId, anim) => {
|
|
566
|
+
setAnimations((prev) => {
|
|
567
|
+
const next = { ...prev };
|
|
568
|
+
if (anim) {
|
|
569
|
+
next[shapeId] = anim;
|
|
570
|
+
} else {
|
|
571
|
+
delete next[shapeId];
|
|
572
|
+
}
|
|
573
|
+
return next;
|
|
574
|
+
});
|
|
575
|
+
}, []);
|
|
576
|
+
useEffect(() => {
|
|
577
|
+
if (!isPlaying) {
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
let raf = 0;
|
|
581
|
+
const start = performance.now() - previewTime;
|
|
582
|
+
const tick = (now) => {
|
|
583
|
+
setPreviewTime(now - start);
|
|
584
|
+
raf = requestAnimationFrame(tick);
|
|
585
|
+
};
|
|
586
|
+
raf = requestAnimationFrame(tick);
|
|
587
|
+
return () => cancelAnimationFrame(raf);
|
|
588
|
+
}, [isPlaying]);
|
|
589
|
+
const [boolSecondShape, setBoolSecondShape] = useState(null);
|
|
590
|
+
const applyBoolean = useCallback(
|
|
591
|
+
(operation) => {
|
|
592
|
+
if (!selId || !boolSecondShape) {
|
|
593
|
+
return;
|
|
594
|
+
}
|
|
595
|
+
const shapeA = shapes.find((s) => s.id === selId);
|
|
596
|
+
const shapeB = shapes.find((s) => s.id === boolSecondShape);
|
|
597
|
+
if (!shapeA || !shapeB) {
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
const result = booleanOperation(shapeA, shapeB, operation);
|
|
601
|
+
if (!result) {
|
|
602
|
+
console.warn("[VectorEditor] Boolean operation resulted in empty shape");
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
let next = shapes.filter((s) => s.id !== selId && s.id !== boolSecondShape);
|
|
606
|
+
if (Array.isArray(result)) {
|
|
607
|
+
next = [...next, ...result];
|
|
608
|
+
setSelId(result[0]?.id || null);
|
|
609
|
+
} else {
|
|
610
|
+
next = [...next, result];
|
|
611
|
+
setSelId(result.id);
|
|
612
|
+
}
|
|
613
|
+
snap(next);
|
|
614
|
+
setShapes(next);
|
|
615
|
+
setBoolSecondShape(null);
|
|
616
|
+
},
|
|
617
|
+
[selId, boolSecondShape, shapes, snap]
|
|
618
|
+
);
|
|
619
|
+
const unionShapes = useCallback(() => applyBoolean("union"), [applyBoolean]);
|
|
620
|
+
const subtractShapes = useCallback(() => applyBoolean("difference"), [applyBoolean]);
|
|
621
|
+
const intersectShapes = useCallback(() => applyBoolean("intersection"), [applyBoolean]);
|
|
622
|
+
const excludeShapes = useCallback(() => applyBoolean("exclusion"), [applyBoolean]);
|
|
623
|
+
const outlineStroke = useCallback(() => {
|
|
624
|
+
if (!selId || !selShape) {
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
const result = flattenToOutline(selShape);
|
|
628
|
+
const next = shapes.map((s) => s.id === selId ? result : s);
|
|
629
|
+
snap(next);
|
|
630
|
+
setShapes(next);
|
|
631
|
+
setSelId(result.id);
|
|
632
|
+
}, [selId, selShape, shapes, snap]);
|
|
633
|
+
const importSVGString = useCallback(
|
|
634
|
+
(raw) => {
|
|
635
|
+
try {
|
|
636
|
+
const imported = parseSVGToShapes(raw);
|
|
637
|
+
if (imported.length === 0) {
|
|
638
|
+
console.warn("[VectorEditor] No supported shapes found in SVG.");
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const next = [...shapes, ...imported];
|
|
675
642
|
snap(next);
|
|
676
643
|
setShapes(next);
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
setTool(km[e.key.toLowerCase()]);
|
|
799
|
-
return;
|
|
800
|
-
}
|
|
801
|
-
if (e.key === 'Delete' || e.key === 'Backspace') {
|
|
802
|
-
deleteShape();
|
|
803
|
-
}
|
|
804
|
-
};
|
|
805
|
-
window.addEventListener('keydown', h);
|
|
806
|
-
return () => window.removeEventListener('keydown', h);
|
|
807
|
-
}, [undo, redo, deleteShape, copyShape, pasteShape, duplicateShape]);
|
|
808
|
-
/* ── return ── */
|
|
809
|
-
return {
|
|
810
|
-
// state
|
|
811
|
-
/** The question the editor waits on; render it with `EditorAskDialog`. */
|
|
812
|
-
pendingAsk,
|
|
813
|
-
shapes,
|
|
814
|
-
selId,
|
|
815
|
-
tool,
|
|
816
|
-
fillCol,
|
|
817
|
-
strokeCol,
|
|
818
|
-
strokeW,
|
|
819
|
-
fontSize,
|
|
820
|
-
fontFamily,
|
|
821
|
-
shapeOpacity,
|
|
822
|
-
cornerRadius,
|
|
823
|
-
polygonSides,
|
|
824
|
-
starPoints2,
|
|
825
|
-
starInnerRatio,
|
|
826
|
-
zoom,
|
|
827
|
-
px,
|
|
828
|
-
py,
|
|
829
|
-
dlgSvg,
|
|
830
|
-
svgText,
|
|
831
|
-
snapGrid,
|
|
832
|
-
gridSize,
|
|
833
|
-
hasClipboard,
|
|
834
|
-
boolSecondShape,
|
|
835
|
-
// derived
|
|
836
|
-
selShape,
|
|
837
|
-
// setters
|
|
838
|
-
setTool,
|
|
839
|
-
setFillCol,
|
|
840
|
-
setStrokeCol,
|
|
841
|
-
setStrokeW,
|
|
842
|
-
setFontSize,
|
|
843
|
-
setFontFamily,
|
|
844
|
-
setShapeOpacity,
|
|
845
|
-
setCornerRadius,
|
|
846
|
-
setPolygonSides,
|
|
847
|
-
setStarPoints2,
|
|
848
|
-
setStarInnerRatio,
|
|
849
|
-
setZoom,
|
|
850
|
-
setDlgSvg,
|
|
851
|
-
setSvgText,
|
|
852
|
-
setSnapGrid,
|
|
853
|
-
setGridSize,
|
|
854
|
-
setSelId,
|
|
855
|
-
setBoolSecondShape,
|
|
856
|
-
setShapes,
|
|
857
|
-
snap,
|
|
858
|
-
// refs
|
|
859
|
-
boxRef,
|
|
860
|
-
fInp,
|
|
861
|
-
// mouse handlers
|
|
862
|
-
onDown,
|
|
863
|
-
onMove,
|
|
864
|
-
onUp,
|
|
865
|
-
onWheel,
|
|
866
|
-
onDblClick,
|
|
867
|
-
// actions
|
|
868
|
-
undo,
|
|
869
|
-
redo,
|
|
870
|
-
deleteShape,
|
|
871
|
-
bringToFront,
|
|
872
|
-
sendToBack,
|
|
873
|
-
updateSel,
|
|
874
|
-
copyShape,
|
|
875
|
-
pasteShape,
|
|
876
|
-
duplicateShape,
|
|
877
|
-
flipH,
|
|
878
|
-
flipV,
|
|
879
|
-
alignShapes,
|
|
880
|
-
unionShapes,
|
|
881
|
-
subtractShapes,
|
|
882
|
-
intersectShapes,
|
|
883
|
-
excludeShapes,
|
|
884
|
-
outlineStroke,
|
|
885
|
-
importSVGString,
|
|
886
|
-
openFile,
|
|
887
|
-
newCanvas,
|
|
888
|
-
exportSVG,
|
|
889
|
-
exportPNG,
|
|
890
|
-
exportJPG,
|
|
891
|
-
exportWebP,
|
|
892
|
-
exportBMP,
|
|
893
|
-
switchTool,
|
|
894
|
-
saveToLibrary,
|
|
895
|
-
// animations
|
|
896
|
-
animations,
|
|
897
|
-
isPlaying,
|
|
898
|
-
previewTime,
|
|
899
|
-
setShapeAnimation,
|
|
900
|
-
setIsPlaying,
|
|
901
|
-
setPreviewTime,
|
|
644
|
+
setDlgSvg(false);
|
|
645
|
+
} catch (e) {
|
|
646
|
+
console.warn(
|
|
647
|
+
"[VectorEditor] SVG parse error:",
|
|
648
|
+
e instanceof Error ? e.message : "Failed to parse SVG."
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
},
|
|
652
|
+
[shapes, snap]
|
|
653
|
+
);
|
|
654
|
+
const openFile = useCallback(
|
|
655
|
+
async (file) => {
|
|
656
|
+
importSVGString(await file.text());
|
|
657
|
+
},
|
|
658
|
+
[importSVGString]
|
|
659
|
+
);
|
|
660
|
+
const newCanvas = useCallback(() => {
|
|
661
|
+
snap([]);
|
|
662
|
+
setShapes([]);
|
|
663
|
+
setSelId(null);
|
|
664
|
+
setZoom(1);
|
|
665
|
+
setPx(0);
|
|
666
|
+
setPy(0);
|
|
667
|
+
}, [snap]);
|
|
668
|
+
const saveToLibrary = useCallback(() => {
|
|
669
|
+
if (!onSaveToLibrary) {
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
const svg = buildSVGString(shapes, artW, artH);
|
|
673
|
+
const blob = new Blob([svg], { type: "image/svg+xml" });
|
|
674
|
+
const reader = new FileReader();
|
|
675
|
+
reader.onload = () => {
|
|
676
|
+
const dataUrl = reader.result;
|
|
677
|
+
void ask("prompt", "Save to library as:", "drawing.svg").then((name) => {
|
|
678
|
+
if (name && name.trim()) {
|
|
679
|
+
onSaveToLibrary(dataUrl, name.trim(), "image/svg+xml");
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
};
|
|
683
|
+
reader.readAsDataURL(blob);
|
|
684
|
+
}, [shapes, artW, artH, onSaveToLibrary, ask]);
|
|
685
|
+
const exportSVG = useCallback(
|
|
686
|
+
() => exportSVGFile(shapes, artW, artH, animations),
|
|
687
|
+
[shapes, artW, artH, animations]
|
|
688
|
+
);
|
|
689
|
+
const exportPNG = useCallback(() => exportPNGFile(shapes, artW, artH), [shapes, artW, artH]);
|
|
690
|
+
const exportJPG = useCallback(() => exportJPGFile(shapes, artW, artH), [shapes, artW, artH]);
|
|
691
|
+
const exportWebP = useCallback(() => exportWebPFile(shapes, artW, artH), [shapes, artW, artH]);
|
|
692
|
+
const exportBMP = useCallback(() => exportBMPFile(shapes, artW, artH), [shapes, artW, artH]);
|
|
693
|
+
const switchTool = useCallback(
|
|
694
|
+
(newTool) => {
|
|
695
|
+
if (tool === "pen" && newTool !== "pen" && pendingShape.current) {
|
|
696
|
+
snap(shapes);
|
|
697
|
+
pendingShape.current = null;
|
|
698
|
+
penPts.current = [];
|
|
699
|
+
}
|
|
700
|
+
setTool(newTool);
|
|
701
|
+
},
|
|
702
|
+
[tool, shapes, snap]
|
|
703
|
+
);
|
|
704
|
+
useEffect(() => {
|
|
705
|
+
const h = (e) => {
|
|
706
|
+
const t = e.target;
|
|
707
|
+
if (t.tagName === "INPUT" || t.tagName === "TEXTAREA") {
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
if (e.ctrlKey && e.key === "z") {
|
|
711
|
+
e.preventDefault();
|
|
712
|
+
undo();
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
if (e.ctrlKey && (e.key === "y" || e.shiftKey && e.key === "Z")) {
|
|
716
|
+
e.preventDefault();
|
|
717
|
+
redo();
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
if (e.ctrlKey && e.key === "c") {
|
|
721
|
+
e.preventDefault();
|
|
722
|
+
copyShape();
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
if (e.ctrlKey && e.key === "v") {
|
|
726
|
+
e.preventDefault();
|
|
727
|
+
pasteShape();
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
if (e.ctrlKey && e.key === "d") {
|
|
731
|
+
e.preventDefault();
|
|
732
|
+
duplicateShape();
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
if (e.key === "Escape") {
|
|
736
|
+
if (pendingShape.current) {
|
|
737
|
+
setShapes((prev) => prev.filter((s) => s.id !== pendingShape.current.id));
|
|
738
|
+
pendingShape.current = null;
|
|
739
|
+
drawing.current = false;
|
|
740
|
+
penPts.current = [];
|
|
741
|
+
} else {
|
|
742
|
+
setSelId(null);
|
|
743
|
+
}
|
|
744
|
+
return;
|
|
745
|
+
}
|
|
746
|
+
const km = {
|
|
747
|
+
v: "pointer",
|
|
748
|
+
r: "rect",
|
|
749
|
+
u: "roundrect",
|
|
750
|
+
c: "ellipse",
|
|
751
|
+
l: "line",
|
|
752
|
+
a: "arrow",
|
|
753
|
+
g: "polygon",
|
|
754
|
+
s: "star",
|
|
755
|
+
p: "pen",
|
|
756
|
+
t: "text"
|
|
757
|
+
};
|
|
758
|
+
if (km[e.key.toLowerCase()] && !e.ctrlKey && !e.altKey) {
|
|
759
|
+
setTool(km[e.key.toLowerCase()]);
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
if (e.key === "Delete" || e.key === "Backspace") {
|
|
763
|
+
deleteShape();
|
|
764
|
+
}
|
|
902
765
|
};
|
|
766
|
+
window.addEventListener("keydown", h);
|
|
767
|
+
return () => window.removeEventListener("keydown", h);
|
|
768
|
+
}, [undo, redo, deleteShape, copyShape, pasteShape, duplicateShape]);
|
|
769
|
+
return {
|
|
770
|
+
// state
|
|
771
|
+
/** The question the editor waits on; render it with `EditorAskDialog`. */
|
|
772
|
+
pendingAsk,
|
|
773
|
+
shapes,
|
|
774
|
+
selId,
|
|
775
|
+
tool,
|
|
776
|
+
fillCol,
|
|
777
|
+
strokeCol,
|
|
778
|
+
strokeW,
|
|
779
|
+
fontSize,
|
|
780
|
+
fontFamily,
|
|
781
|
+
shapeOpacity,
|
|
782
|
+
cornerRadius,
|
|
783
|
+
polygonSides,
|
|
784
|
+
starPoints2,
|
|
785
|
+
starInnerRatio,
|
|
786
|
+
zoom,
|
|
787
|
+
px,
|
|
788
|
+
py,
|
|
789
|
+
dlgSvg,
|
|
790
|
+
svgText,
|
|
791
|
+
snapGrid,
|
|
792
|
+
gridSize,
|
|
793
|
+
hasClipboard,
|
|
794
|
+
boolSecondShape,
|
|
795
|
+
// derived
|
|
796
|
+
selShape,
|
|
797
|
+
// setters
|
|
798
|
+
setTool,
|
|
799
|
+
setFillCol,
|
|
800
|
+
setStrokeCol,
|
|
801
|
+
setStrokeW,
|
|
802
|
+
setFontSize,
|
|
803
|
+
setFontFamily,
|
|
804
|
+
setShapeOpacity,
|
|
805
|
+
setCornerRadius,
|
|
806
|
+
setPolygonSides,
|
|
807
|
+
setStarPoints2,
|
|
808
|
+
setStarInnerRatio,
|
|
809
|
+
setZoom,
|
|
810
|
+
setDlgSvg,
|
|
811
|
+
setSvgText,
|
|
812
|
+
setSnapGrid,
|
|
813
|
+
setGridSize,
|
|
814
|
+
setSelId,
|
|
815
|
+
setBoolSecondShape,
|
|
816
|
+
setShapes,
|
|
817
|
+
snap,
|
|
818
|
+
// refs
|
|
819
|
+
boxRef,
|
|
820
|
+
fInp,
|
|
821
|
+
// mouse handlers
|
|
822
|
+
onDown,
|
|
823
|
+
onMove,
|
|
824
|
+
onUp,
|
|
825
|
+
onWheel,
|
|
826
|
+
onDblClick,
|
|
827
|
+
// actions
|
|
828
|
+
undo,
|
|
829
|
+
redo,
|
|
830
|
+
deleteShape,
|
|
831
|
+
bringToFront,
|
|
832
|
+
sendToBack,
|
|
833
|
+
updateSel,
|
|
834
|
+
copyShape,
|
|
835
|
+
pasteShape,
|
|
836
|
+
duplicateShape,
|
|
837
|
+
flipH,
|
|
838
|
+
flipV,
|
|
839
|
+
alignShapes,
|
|
840
|
+
unionShapes,
|
|
841
|
+
subtractShapes,
|
|
842
|
+
intersectShapes,
|
|
843
|
+
excludeShapes,
|
|
844
|
+
outlineStroke,
|
|
845
|
+
importSVGString,
|
|
846
|
+
openFile,
|
|
847
|
+
newCanvas,
|
|
848
|
+
exportSVG,
|
|
849
|
+
exportPNG,
|
|
850
|
+
exportJPG,
|
|
851
|
+
exportWebP,
|
|
852
|
+
exportBMP,
|
|
853
|
+
switchTool,
|
|
854
|
+
saveToLibrary,
|
|
855
|
+
// animations
|
|
856
|
+
animations,
|
|
857
|
+
isPlaying,
|
|
858
|
+
previewTime,
|
|
859
|
+
setShapeAnimation,
|
|
860
|
+
setIsPlaying,
|
|
861
|
+
setPreviewTime
|
|
862
|
+
};
|
|
903
863
|
}
|
|
904
|
-
|
|
905
|
-
|
|
864
|
+
export {
|
|
865
|
+
useVectorEditor
|
|
866
|
+
};
|