@growgroup/visual-editor 0.1.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.
Files changed (219) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +52 -0
  3. package/README.md +143 -0
  4. package/dist/editor.css +2 -0
  5. package/package.json +94 -0
  6. package/src/components/DeckSlideRender.tsx +21 -0
  7. package/src/components/SaveNote.tsx +21 -0
  8. package/src/components/auth/AuthProvider.tsx +44 -0
  9. package/src/components/ui/accordion.tsx +57 -0
  10. package/src/components/ui/alert-dialog.tsx +141 -0
  11. package/src/components/ui/badge.tsx +36 -0
  12. package/src/components/ui/button.tsx +56 -0
  13. package/src/components/ui/card.tsx +76 -0
  14. package/src/components/ui/checkbox.tsx +30 -0
  15. package/src/components/ui/collapsible.tsx +11 -0
  16. package/src/components/ui/context-menu.tsx +200 -0
  17. package/src/components/ui/dialog.tsx +122 -0
  18. package/src/components/ui/dropdown-menu.tsx +201 -0
  19. package/src/components/ui/input.tsx +22 -0
  20. package/src/components/ui/label.tsx +26 -0
  21. package/src/components/ui/popover.tsx +33 -0
  22. package/src/components/ui/scroll-area.tsx +48 -0
  23. package/src/components/ui/scrubbable-label.tsx +95 -0
  24. package/src/components/ui/select.tsx +159 -0
  25. package/src/components/ui/separator.tsx +31 -0
  26. package/src/components/ui/slider.tsx +28 -0
  27. package/src/components/ui/switch.tsx +29 -0
  28. package/src/components/ui/tabs.tsx +55 -0
  29. package/src/components/ui/textarea.tsx +22 -0
  30. package/src/components/ui/tooltip.tsx +32 -0
  31. package/src/components/viewer/useDeck.ts +58 -0
  32. package/src/editor/EditorContext.tsx +632 -0
  33. package/src/editor/EditorToolbar.tsx +446 -0
  34. package/src/editor/FrontendVisualEditor.tsx +2595 -0
  35. package/src/editor/autosave.ts +34 -0
  36. package/src/editor/components/AiPromptPopover.tsx +378 -0
  37. package/src/editor/components/BreakpointGuides.tsx +323 -0
  38. package/src/editor/components/BreakpointSelector.tsx +180 -0
  39. package/src/editor/components/ComponentPropertyEditor.tsx +426 -0
  40. package/src/editor/components/CssEditorDialog.tsx +168 -0
  41. package/src/editor/components/EditorCanvas.tsx +987 -0
  42. package/src/editor/components/EditorContextMenu.tsx +515 -0
  43. package/src/editor/components/EditorFooter.tsx +81 -0
  44. package/src/editor/components/EditorHeader.tsx +524 -0
  45. package/src/editor/components/EditorLayerPanel.tsx +1197 -0
  46. package/src/editor/components/EditorPropertyPanel.tsx +2996 -0
  47. package/src/editor/components/EditorSidebar.tsx +226 -0
  48. package/src/editor/components/FigmaColorPicker.tsx +1577 -0
  49. package/src/editor/components/HtmlEditorDialog.tsx +71 -0
  50. package/src/editor/components/HtmlImportDialog.tsx +346 -0
  51. package/src/editor/components/JsEditorDialog.tsx +175 -0
  52. package/src/editor/components/LayerTreeItem.tsx +494 -0
  53. package/src/editor/components/MasterComponentEditor.tsx +846 -0
  54. package/src/editor/components/MediaLibraryDialog.tsx +447 -0
  55. package/src/editor/components/PageSettingsDialog.tsx +776 -0
  56. package/src/editor/components/VariablePicker.tsx +355 -0
  57. package/src/editor/components/VariablesDialog.tsx +564 -0
  58. package/src/editor/components/VariantEditor.tsx +687 -0
  59. package/src/editor/components/VariantMatrix.tsx +507 -0
  60. package/src/editor/components/component-panel/CategoryAccordion.tsx +139 -0
  61. package/src/editor/components/component-panel/ComponentGrid.tsx +51 -0
  62. package/src/editor/components/component-panel/ComponentItem.tsx +368 -0
  63. package/src/editor/components/component-panel/ComponentPanel.tsx +275 -0
  64. package/src/editor/components/component-panel/ComponentSearch.tsx +82 -0
  65. package/src/editor/components/component-panel/VariantList.tsx +260 -0
  66. package/src/editor/components/component-panel/index.ts +12 -0
  67. package/src/editor/components/index.ts +48 -0
  68. package/src/editor/components/multi-page/CanvasZoomControls.tsx +71 -0
  69. package/src/editor/components/multi-page/InfiniteCanvas.tsx +95 -0
  70. package/src/editor/components/multi-page/MultiPageCanvasView.tsx +1404 -0
  71. package/src/editor/components/multi-page/PageFrameOverlay.tsx +76 -0
  72. package/src/editor/components/multi-page/PageLabel.tsx +34 -0
  73. package/src/editor/components/multi-page/PageLivePreview.tsx +242 -0
  74. package/src/editor/components/multi-page/PageThumbnail.tsx +59 -0
  75. package/src/editor/components/multi-page/index.ts +7 -0
  76. package/src/editor/components/ppt/PptChrome.tsx +2508 -0
  77. package/src/editor/components/ppt/PptComments.tsx +589 -0
  78. package/src/editor/components/ppt/PptDesignProposals.tsx +275 -0
  79. package/src/editor/components/ppt/PptFormatPane.tsx +320 -0
  80. package/src/editor/components/ppt/PptNotes.tsx +246 -0
  81. package/src/editor/components/property-panel/AlignmentPanel.tsx +110 -0
  82. package/src/editor/components/property-panel/AutoLayoutPanel.tsx +615 -0
  83. package/src/editor/components/property-panel/BorderSection.tsx +104 -0
  84. package/src/editor/components/property-panel/CompactNumberInput.tsx +237 -0
  85. package/src/editor/components/property-panel/CompactSizeInput.tsx +148 -0
  86. package/src/editor/components/property-panel/EffectSection.tsx +157 -0
  87. package/src/editor/components/property-panel/FillSection.tsx +69 -0
  88. package/src/editor/components/property-panel/GoogleFontPicker.tsx +192 -0
  89. package/src/editor/components/property-panel/ImageSection.tsx +206 -0
  90. package/src/editor/components/property-panel/ImgSrcSection.tsx +125 -0
  91. package/src/editor/components/property-panel/InstanceOverrideSection.tsx +413 -0
  92. package/src/editor/components/property-panel/LayoutSection.tsx +105 -0
  93. package/src/editor/components/property-panel/LinkSection.tsx +103 -0
  94. package/src/editor/components/property-panel/PropertyVariablePicker.tsx +311 -0
  95. package/src/editor/components/property-panel/ScalePanel.tsx +212 -0
  96. package/src/editor/components/property-panel/TypographySection.tsx +230 -0
  97. package/src/editor/components/property-panel/UnitAwareNumberInput.tsx +238 -0
  98. package/src/editor/components/property-panel/VariableAwareColorInput.tsx +477 -0
  99. package/src/editor/components/property-panel/VariableAwareInput.tsx +606 -0
  100. package/src/editor/components/property-panel/VariableAwareSizeInput.tsx +588 -0
  101. package/src/editor/components/property-panel/VariableAwareUnitInput.tsx +546 -0
  102. package/src/editor/components/property-panel/index.ts +24 -0
  103. package/src/editor/components/property-panel/unit-utils.ts +261 -0
  104. package/src/editor/components/shell/EditorTopBar.tsx +392 -0
  105. package/src/editor/components/shell/LeftPanel.tsx +47 -0
  106. package/src/editor/components/shell/PagesPanel.tsx +118 -0
  107. package/src/editor/components/variables-panel/ColorValueCell.tsx +165 -0
  108. package/src/editor/components/variables-panel/TextValueCell.tsx +116 -0
  109. package/src/editor/components/variables-panel/VariablesPanel.tsx +518 -0
  110. package/src/editor/components/variables-panel/index.ts +7 -0
  111. package/src/editor/constants.ts +468 -0
  112. package/src/editor/contexts/EditorArtboardContext.tsx +229 -0
  113. package/src/editor/contexts/EditorComponentsContext.tsx +1320 -0
  114. package/src/editor/contexts/EditorDocumentContext.tsx +113 -0
  115. package/src/editor/contexts/EditorHistoryContext.tsx +516 -0
  116. package/src/editor/contexts/EditorRefsContext.tsx +204 -0
  117. package/src/editor/contexts/EditorSelectionContext.tsx +54 -0
  118. package/src/editor/contexts/EditorToolContext.tsx +58 -0
  119. package/src/editor/contexts/EditorUIStateContext.tsx +122 -0
  120. package/src/editor/contexts/EditorVariablesContext.tsx +397 -0
  121. package/src/editor/contexts/EditorViewContext.tsx +101 -0
  122. package/src/editor/contexts/MultiPageCanvasContext.tsx +498 -0
  123. package/src/editor/contexts/index.ts +17 -0
  124. package/src/editor/editor-skin.css +122 -0
  125. package/src/editor/hooks/index.ts +38 -0
  126. package/src/editor/hooks/useAiReplace.ts +397 -0
  127. package/src/editor/hooks/useAltMeasure.ts +67 -0
  128. package/src/editor/hooks/useBrowserZoomPrevention.ts +121 -0
  129. package/src/editor/hooks/useCanvasControls.ts +1222 -0
  130. package/src/editor/hooks/useComponentEditMode.ts +658 -0
  131. package/src/editor/hooks/useComponentInstances.ts +515 -0
  132. package/src/editor/hooks/useContextMenuHandler.ts +157 -0
  133. package/src/editor/hooks/useCoordinateTransform.ts +207 -0
  134. package/src/editor/hooks/useDragResize.ts +1317 -0
  135. package/src/editor/hooks/useDrawingMode.ts +472 -0
  136. package/src/editor/hooks/useEditorColors.ts +144 -0
  137. package/src/editor/hooks/useEditorMessages.ts +108 -0
  138. package/src/editor/hooks/useElementActions.ts +1754 -0
  139. package/src/editor/hooks/useElementSelection.ts +1573 -0
  140. package/src/editor/hooks/useFocusManagement.ts +249 -0
  141. package/src/editor/hooks/useGoogleFonts.ts +305 -0
  142. package/src/editor/hooks/useIframeInitializer.ts +584 -0
  143. package/src/editor/hooks/useIframeSetup.ts +360 -0
  144. package/src/editor/hooks/useImageUpload.ts +270 -0
  145. package/src/editor/hooks/useInfiniteCanvas.ts +301 -0
  146. package/src/editor/hooks/useKeyboardShortcuts.ts +205 -0
  147. package/src/editor/hooks/useMarqueeSelection.ts +388 -0
  148. package/src/editor/hooks/useMediaLibrary.ts +129 -0
  149. package/src/editor/hooks/usePageSettingsManager.ts +739 -0
  150. package/src/editor/hooks/useResizablePanel.ts +163 -0
  151. package/src/editor/hooks/useRichPaste.ts +255 -0
  152. package/src/editor/hooks/useTouchGestures.ts +197 -0
  153. package/src/editor/index.ts +27 -0
  154. package/src/editor/types/page-settings.ts +513 -0
  155. package/src/editor/types.ts +382 -0
  156. package/src/editor/utils/align-elements.ts +169 -0
  157. package/src/editor/utils/api-json.ts +21 -0
  158. package/src/editor/utils/aspect-lock.ts +50 -0
  159. package/src/editor/utils/component-renderer.ts +702 -0
  160. package/src/editor/utils/component-sync.ts +845 -0
  161. package/src/editor/utils/crop-mode.ts +265 -0
  162. package/src/editor/utils/dom-utils.ts +2238 -0
  163. package/src/editor/utils/element-effects.ts +218 -0
  164. package/src/editor/utils/eyedropper.ts +35 -0
  165. package/src/editor/utils/figma-export.ts +560 -0
  166. package/src/editor/utils/figma-kiwi-decoder.ts +722 -0
  167. package/src/editor/utils/figma-kiwi-encoder.ts +583 -0
  168. package/src/editor/utils/figma-paste.ts +2262 -0
  169. package/src/editor/utils/flex-reorder.ts +231 -0
  170. package/src/editor/utils/flex-utils.ts +1230 -0
  171. package/src/editor/utils/geometry.ts +232 -0
  172. package/src/editor/utils/grid-layout.ts +210 -0
  173. package/src/editor/utils/html-utils.ts +470 -0
  174. package/src/editor/utils/index.ts +11 -0
  175. package/src/editor/utils/ink-style.ts +57 -0
  176. package/src/editor/utils/inline-format.ts +236 -0
  177. package/src/editor/utils/measure-distance.ts +120 -0
  178. package/src/editor/utils/override-apply.ts +526 -0
  179. package/src/editor/utils/override-detection.ts +613 -0
  180. package/src/editor/utils/paste-processors.ts +609 -0
  181. package/src/editor/utils/paste-sanitizer.ts +244 -0
  182. package/src/editor/utils/restore-flow.ts +188 -0
  183. package/src/editor/utils/shape-library.ts +171 -0
  184. package/src/editor/utils/slide-root.ts +29 -0
  185. package/src/editor/utils/smart-guides.ts +218 -0
  186. package/src/editor/utils/style-utils.ts +685 -0
  187. package/src/editor/utils/table-edit.ts +151 -0
  188. package/src/editor/utils/tailwind-mappings.ts +778 -0
  189. package/src/editor/utils/tailwind-utils.ts +804 -0
  190. package/src/editor/utils/text-highlight.ts +216 -0
  191. package/src/editor/utils/theme-to-variables.ts +341 -0
  192. package/src/editor/utils/variant-resolver.ts +456 -0
  193. package/src/editor/utils/viewport-utils.ts +295 -0
  194. package/src/hooks/useEditorHistory.ts +195 -0
  195. package/src/hooks/useEditorShortcuts.ts +260 -0
  196. package/src/index.ts +40 -0
  197. package/src/io.ts +134 -0
  198. package/src/lib/agent/slide-agent/types.ts +547 -0
  199. package/src/lib/agent/website-agent/types.ts +805 -0
  200. package/src/lib/api/auth-fetch.ts +58 -0
  201. package/src/lib/deck.ts +69 -0
  202. package/src/lib/export.ts +47 -0
  203. package/src/lib/firebase/config.ts +29 -0
  204. package/src/lib/firebase/css-variables.ts +378 -0
  205. package/src/lib/firebase/editor-components.ts +677 -0
  206. package/src/lib/firebase/storage.ts +469 -0
  207. package/src/lib/utils.ts +6 -0
  208. package/src/styles/editor.css +52 -0
  209. package/src/styles/skin.css +122 -0
  210. package/src/types/css-variables.ts +645 -0
  211. package/src/types/editor-components.ts +944 -0
  212. package/src/types/editor.ts +420 -0
  213. package/src/types/page-master.ts +197 -0
  214. package/src/types/page.ts +419 -0
  215. package/src/types/slide.ts +402 -0
  216. package/src/types/website-theme.ts +788 -0
  217. package/src/types/website.ts +374 -0
  218. package/src/vendor/firebase-firestore.ts +13 -0
  219. package/src/vendor/firebase-functions.ts +17 -0
@@ -0,0 +1,265 @@
1
+ /**
2
+ * 画像のトリミング(PowerPointの「トリミング」相当)。
3
+ *
4
+ * 仕組み: 画像を overflow:hidden のラッパー(.gg-crop)で包み、
5
+ * - 枠ハンドルのドラッグ = ラッパーの矩形を変える(画像は動かない=見た目固定)
6
+ * - 枠の内側のドラッグ = ラッパー内で画像の位置を動かす(見せる範囲の調整)
7
+ * Enter/枠外クリックで確定、Escで開始時の状態へ完全に戻す。
8
+ *
9
+ * ラッパーは通常のDOM編集としてそのまま保存され、書き戻しエンジンが
10
+ * 構造変更(要素の挿入+スタイル)として原本TSXへ運ぶ。切り抜きが
11
+ * 初期状態(全表示)のまま確定された場合はラッパーを外して元の<img>に戻す。
12
+ * ハンドル等のUI(.gg-crop-ui)は表示専用で、確定/中断時に必ず取り除く。
13
+ */
14
+
15
+ const UI_CLASS = 'gg-crop-ui';
16
+ const WRAP_CLASS = 'gg-crop';
17
+
18
+ export type CropSession = {
19
+ commit: () => void;
20
+ cancel: () => void;
21
+ readonly active: boolean;
22
+ };
23
+
24
+ const px = (n: number) => `${Math.round(n * 10) / 10}px`;
25
+
26
+ export function enterCropMode(
27
+ iframeDoc: Document,
28
+ img: HTMLImageElement,
29
+ onDone: (changed: boolean) => void,
30
+ ): CropSession | null {
31
+ const artboard = iframeDoc.getElementById('artboard');
32
+ const win = iframeDoc.defaultView;
33
+ if (!artboard || !win) return null;
34
+
35
+ // ── ラッパーを用意(既存の .gg-crop を再編集 or 新規に包む) ──
36
+ let wrapper = img.parentElement?.classList.contains(WRAP_CLASS)
37
+ ? (img.parentElement as HTMLElement)
38
+ : null;
39
+ const created = !wrapper;
40
+ const snapshotTarget = wrapper ?? img;
41
+ const snapshotParent = snapshotTarget.parentElement!;
42
+ const snapshotNext = snapshotTarget.nextSibling;
43
+ const snapshotHtml = snapshotTarget.outerHTML;
44
+
45
+ if (!wrapper) {
46
+ wrapper = iframeDoc.createElement('div');
47
+ wrapper.className = WRAP_CLASS;
48
+ const cs = win.getComputedStyle(img);
49
+ wrapper.style.position = cs.position === 'absolute' || cs.position === 'fixed' ? cs.position : 'absolute';
50
+ wrapper.style.left = px(img.offsetLeft);
51
+ wrapper.style.top = px(img.offsetTop);
52
+ wrapper.style.width = px(img.offsetWidth);
53
+ wrapper.style.height = px(img.offsetHeight);
54
+ wrapper.style.overflow = 'hidden';
55
+ img.parentElement!.insertBefore(wrapper, img);
56
+ // 画像はラッパー座標系の(0,0)に等倍で置き直す
57
+ img.style.position = 'absolute';
58
+ img.style.left = '0px';
59
+ img.style.top = '0px';
60
+ img.style.width = px(img.offsetWidth);
61
+ img.style.height = px(img.offsetHeight);
62
+ img.style.maxWidth = 'none';
63
+ img.style.margin = '0';
64
+ wrapper.appendChild(img);
65
+ }
66
+
67
+ // クロップ中はエディタの選択・ドラッグ機構を止める(bodyクラスで通知)。
68
+ // 止めないと選択枠(白ハンドル)がクロップUIの上に再描画され、
69
+ // それを掴んだ操作が「枠外クリック」となって勝手に確定・終了してしまう
70
+ iframeDoc.body.classList.add('gg-cropping');
71
+
72
+ // ── UIレイヤー(枠+8ハンドル)。アートボード座標なのでズームに自動追従 ──
73
+ const ui = iframeDoc.createElement('div');
74
+ ui.className = UI_CLASS;
75
+ ui.style.cssText = 'position:absolute;inset:0;z-index:9500;pointer-events:none;';
76
+ artboard.appendChild(ui);
77
+
78
+ const frame = iframeDoc.createElement('div');
79
+ frame.style.cssText =
80
+ 'position:absolute;border:2px solid #111;box-shadow:0 0 0 20000px rgba(0,0,0,0.25);' +
81
+ 'pointer-events:auto;cursor:move;';
82
+ ui.appendChild(frame);
83
+
84
+ const HANDLES = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w'] as const;
85
+ type Handle = (typeof HANDLES)[number];
86
+ for (const h of HANDLES) {
87
+ const el = iframeDoc.createElement('div');
88
+ el.setAttribute('data-handle', h);
89
+ // PowerPoint風の黒い鉤型ハンドル(簡略: 太い短線)
90
+ const base =
91
+ 'position:absolute;background:#111;pointer-events:auto;';
92
+ const size = h.length === 2 ? 'width:16px;height:16px;' : h === 'n' || h === 's' ? 'width:22px;height:6px;' : 'width:6px;height:22px;';
93
+ el.style.cssText = base + size + `cursor:${h}-resize;`;
94
+ frame.appendChild(el);
95
+ }
96
+
97
+ // 枠はアートボード直下のUIレイヤーに描くため、位置は**アートボード座標**が要る。
98
+ // offsetLeft/offsetTop は offsetParent(入れ子の親)基準なので、画像がパネル等の
99
+ // 中にあると枠だけ左上へ大きくずれる(実際に起きた)。rectから換算する
100
+ const artboardPos = (el: HTMLElement) => {
101
+ const a = artboard.getBoundingClientRect();
102
+ const s = a.width / artboard.offsetWidth || 1;
103
+ const r = el.getBoundingClientRect();
104
+ return {
105
+ left: (r.left - a.left) / s,
106
+ top: (r.top - a.top) / s,
107
+ width: r.width / s,
108
+ height: r.height / s,
109
+ };
110
+ };
111
+
112
+ const layout = () => {
113
+ const p = artboardPos(wrapper!);
114
+ frame.style.left = px(p.left);
115
+ frame.style.top = px(p.top);
116
+ frame.style.width = px(p.width);
117
+ frame.style.height = px(p.height);
118
+ const w = p.width;
119
+ const h = p.height;
120
+ const pos: Record<Handle, [number, number]> = {
121
+ nw: [-3, -3], n: [w / 2 - 11, -3], ne: [w - 13, -3], e: [w - 3, h / 2 - 11],
122
+ se: [w - 13, h - 13], s: [w / 2 - 11, h - 3], sw: [-3, h - 13], w: [-3, h / 2 - 11],
123
+ };
124
+ frame.querySelectorAll<HTMLElement>('[data-handle]').forEach((el) => {
125
+ const [x, y] = pos[el.getAttribute('data-handle') as Handle];
126
+ el.style.left = px(x);
127
+ el.style.top = px(y);
128
+ });
129
+ };
130
+ layout();
131
+
132
+ // ── ドラッグ処理(枠=リサイズ / 内側=画像移動) ──
133
+ const scaleOf = () => {
134
+ // アートボードのズーム倍率(クライアント座標→アートボード座標の換算)
135
+ const r = artboard.getBoundingClientRect();
136
+ return r.width / artboard.offsetWidth || 1;
137
+ };
138
+
139
+ let dragging: { kind: 'frame'; handle: Handle } | { kind: 'image' } | null = null;
140
+ let start = { x: 0, y: 0, wl: 0, wt: 0, ww: 0, wh: 0, il: 0, it: 0 };
141
+
142
+ const clampImage = () => {
143
+ // 画像がラッパーを覆い続けるように位置を制限(白場を作らない)
144
+ const minL = wrapper!.offsetWidth - img.offsetWidth;
145
+ const minT = wrapper!.offsetHeight - img.offsetHeight;
146
+ img.style.left = px(Math.min(0, Math.max(minL, img.offsetLeft)));
147
+ img.style.top = px(Math.min(0, Math.max(minT, img.offsetTop)));
148
+ };
149
+
150
+ const onMove = (e: MouseEvent) => {
151
+ if (!dragging) return;
152
+ e.preventDefault();
153
+ const k = scaleOf();
154
+ const dx = (e.clientX - start.x) / k;
155
+ const dy = (e.clientY - start.y) / k;
156
+ if (dragging.kind === 'image') {
157
+ img.style.left = px(start.il + dx);
158
+ img.style.top = px(start.it + dy);
159
+ clampImage();
160
+ return;
161
+ }
162
+ const h = dragging.handle;
163
+ // 枠の辺を動かす。画像の見た目位置は固定(左/上辺はラッパー移動+画像を逆補正)
164
+ if (h.includes('e')) {
165
+ wrapper!.style.width = px(Math.max(24, Math.min(start.ww + dx, img.offsetWidth + start.il)));
166
+ }
167
+ if (h.includes('s')) {
168
+ wrapper!.style.height = px(Math.max(24, Math.min(start.wh + dy, img.offsetHeight + start.it)));
169
+ }
170
+ if (h.includes('w')) {
171
+ const move = Math.max(start.il, Math.min(dx, start.ww - 24));
172
+ wrapper!.style.left = px(start.wl + move);
173
+ wrapper!.style.width = px(start.ww - move);
174
+ img.style.left = px(start.il - move);
175
+ }
176
+ if (h.includes('n')) {
177
+ const move = Math.max(start.it, Math.min(dy, start.wh - 24));
178
+ wrapper!.style.top = px(start.wt + move);
179
+ wrapper!.style.height = px(start.wh - move);
180
+ img.style.top = px(start.it - move);
181
+ }
182
+ layout();
183
+ };
184
+
185
+ const onDown = (e: MouseEvent) => {
186
+ const target = e.target as HTMLElement;
187
+ if (!ui.contains(target)) return;
188
+ e.preventDefault();
189
+ e.stopPropagation();
190
+ const handle = target.getAttribute('data-handle') as Handle | null;
191
+ dragging = handle ? { kind: 'frame', handle } : { kind: 'image' };
192
+ start = {
193
+ x: e.clientX, y: e.clientY,
194
+ wl: wrapper!.offsetLeft, wt: wrapper!.offsetTop,
195
+ ww: wrapper!.offsetWidth, wh: wrapper!.offsetHeight,
196
+ il: img.offsetLeft, it: img.offsetTop,
197
+ };
198
+ };
199
+ const onUp = () => { dragging = null; };
200
+
201
+ // 枠外クリック=確定 / Enter=確定 / Esc=取り消し
202
+ const onDocDown = (e: MouseEvent) => {
203
+ if (!ui.contains(e.target as Node)) finish(true);
204
+ };
205
+ const onKey = (e: KeyboardEvent) => {
206
+ if (e.key === 'Enter') { e.preventDefault(); e.stopPropagation(); finish(true); }
207
+ if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); finish(false); }
208
+ };
209
+
210
+ ui.addEventListener('mousedown', onDown);
211
+ iframeDoc.addEventListener('mousemove', onMove, true);
212
+ iframeDoc.addEventListener('mouseup', onUp, true);
213
+ // 直後のクリックで即確定しないよう、次のティックから枠外検知を有効化
214
+ const armTimer = setTimeout(() => iframeDoc.addEventListener('mousedown', onDocDown, true), 0);
215
+ iframeDoc.addEventListener('keydown', onKey, true);
216
+
217
+ let done = false;
218
+ const cleanup = () => {
219
+ iframeDoc.body.classList.remove('gg-cropping');
220
+ clearTimeout(armTimer);
221
+ ui.removeEventListener('mousedown', onDown);
222
+ iframeDoc.removeEventListener('mousemove', onMove, true);
223
+ iframeDoc.removeEventListener('mouseup', onUp, true);
224
+ iframeDoc.removeEventListener('mousedown', onDocDown, true);
225
+ iframeDoc.removeEventListener('keydown', onKey, true);
226
+ ui.remove();
227
+ };
228
+
229
+ const finish = (commit: boolean) => {
230
+ if (done) return;
231
+ done = true;
232
+ cleanup();
233
+ if (!commit) {
234
+ // 開始時の姿へ完全に戻す
235
+ const tmp = iframeDoc.createElement('div');
236
+ tmp.innerHTML = snapshotHtml;
237
+ const restored = tmp.firstElementChild!;
238
+ wrapper!.remove();
239
+ snapshotParent.insertBefore(restored, snapshotNext);
240
+ onDone(false);
241
+ return;
242
+ }
243
+ // 切り抜き無し(全表示のまま)なら、ラッパーを外して素の<img>へ戻す
244
+ const uncropped =
245
+ Math.abs(img.offsetLeft) < 1 && Math.abs(img.offsetTop) < 1 &&
246
+ Math.abs(img.offsetWidth - wrapper!.offsetWidth) < 1 &&
247
+ Math.abs(img.offsetHeight - wrapper!.offsetHeight) < 1;
248
+ if (uncropped && created) {
249
+ const tmp = iframeDoc.createElement('div');
250
+ tmp.innerHTML = snapshotHtml;
251
+ const restored = tmp.firstElementChild!;
252
+ wrapper!.remove();
253
+ snapshotParent.insertBefore(restored, snapshotNext);
254
+ onDone(false);
255
+ return;
256
+ }
257
+ onDone(true);
258
+ };
259
+
260
+ return {
261
+ commit: () => finish(true),
262
+ cancel: () => finish(false),
263
+ get active() { return !done; },
264
+ };
265
+ }