@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,236 @@
1
+ /**
2
+ * インライン書式(マーカー・太字)のツールバー。
3
+ *
4
+ * 【設計方針】
5
+ * `<span class="px-[6px] bg-mark-green">改善サイクル</span>` のようなインライン要素は
6
+ * 「箱」ではなく**文字の属性**として扱う(Figmaのキャラクタースタイル相当)。
7
+ * - ドラッグ・リサイズの対象にしない(data-inline としてドラッグ対象外なのは現状どおり)
8
+ * - 新規作成・解除は、テキスト編集中(contenteditable)の**範囲選択**に対して行う
9
+ * - 語彙はデザインシステムの Mark に拘束する(緑=強調 / 赤=課題 / 太字)。
10
+ * 任意の色は出さない。マーカーの意味(赤は課題のみ・1スライド1箇所)を
11
+ * エディタから崩せないようにするため
12
+ *
13
+ * テキストをダブルクリックして編集に入り、文字を選択するとツールバーが浮かぶ。
14
+ */
15
+
16
+ const TOOLBAR_ID = 'gg-inline-format-toolbar';
17
+
18
+ /** デザインシステムのインライン語彙。ここ以外の書式は出さない */
19
+ const FORMATS = [
20
+ { key: 'mark-green', label: 'マーカー', cls: 'px-[6px] bg-mark-green', swatch: '#cfe6d6' },
21
+ { key: 'mark-red', label: 'マーカー(課題)', cls: 'px-[6px] bg-mark-red', swatch: '#ffd9d1' },
22
+ { key: 'bold', label: '太字', cls: 'font-bold', swatch: null },
23
+ ] as const;
24
+
25
+ /** この属性を持つspanを「書式スパン」とみなす(解除の対象) */
26
+ const FORMAT_CLASSES = ['bg-mark-green', 'bg-mark-red', 'font-bold'];
27
+
28
+ const isFormatSpan = (el: Element | null): el is HTMLElement =>
29
+ !!el &&
30
+ el.tagName === 'SPAN' &&
31
+ FORMAT_CLASSES.some((c) => (el as HTMLElement).classList.contains(c));
32
+
33
+ /** 選択がテキスト編集中の要素の中にあるときだけ、その要素を返す */
34
+ function editingHost(doc: Document): HTMLElement | null {
35
+ const sel = doc.getSelection();
36
+ if (!sel || sel.rangeCount === 0 || sel.isCollapsed) return null;
37
+ const host = doc.querySelector('[contenteditable="true"]') as HTMLElement | null;
38
+ if (!host) return null;
39
+ const range = sel.getRangeAt(0);
40
+ if (!host.contains(range.commonAncestorContainer)) return null;
41
+ if (!range.toString().trim()) return null;
42
+ return host;
43
+ }
44
+
45
+ /** spanを外して中身だけ残す */
46
+ function unwrap(el: HTMLElement): void {
47
+ const parent = el.parentNode;
48
+ if (!parent) return;
49
+ while (el.firstChild) parent.insertBefore(el.firstChild, el);
50
+ parent.removeChild(el);
51
+ parent.normalize();
52
+ }
53
+
54
+ /** 範囲内(および範囲を含む)書式スパンをすべて外す */
55
+ function clearFormats(doc: Document, range: Range): void {
56
+ // 範囲を包んでいるスパン
57
+ let node: Node | null = range.commonAncestorContainer;
58
+ while (node && node.nodeType === 3) node = node.parentNode;
59
+ let cur = node as Element | null;
60
+ while (cur && cur.getAttribute?.('contenteditable') !== 'true') {
61
+ if (isFormatSpan(cur)) {
62
+ unwrap(cur as HTMLElement);
63
+ return; // 包んでいたものを外したら終わり(選択は文字列として残る)
64
+ }
65
+ cur = cur.parentElement;
66
+ }
67
+ // 範囲の中にあるスパン
68
+ const host = doc.querySelector('[contenteditable="true"]');
69
+ if (!host) return;
70
+ [...host.querySelectorAll('span')].filter(isFormatSpan).forEach((s) => {
71
+ if (range.intersectsNode(s)) unwrap(s);
72
+ });
73
+ }
74
+
75
+ /** 選択範囲に書式スパンを適用する */
76
+ function applyFormat(doc: Document, cls: string): void {
77
+ const sel = doc.getSelection();
78
+ if (!sel || sel.rangeCount === 0) return;
79
+ const range = sel.getRangeAt(0);
80
+ if (range.collapsed) return;
81
+
82
+ // 入れ子や部分交差を作らないよう、先に範囲に関わる書式を外す
83
+ clearFormats(doc, range);
84
+
85
+ const span = doc.createElement('span');
86
+ span.className = cls;
87
+ // 既存のインライン要素と同じ扱いにする(ドラッグ対象外・保存対象)
88
+ span.setAttribute('data-editable', 'true');
89
+ span.setAttribute('data-inline', 'true');
90
+ span.setAttribute(
91
+ 'data-element-id',
92
+ `el-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`,
93
+ );
94
+ try {
95
+ range.surroundContents(span);
96
+ } catch {
97
+ // 選択が要素境界をまたぐ場合は extract で包む
98
+ span.appendChild(range.extractContents());
99
+ range.insertNode(span);
100
+ }
101
+ // 適用後はスパンの中身を選択したままにする(続けて解除・変更ができる)
102
+ sel.removeAllRanges();
103
+ const r = doc.createRange();
104
+ r.selectNodeContents(span);
105
+ sel.addRange(r);
106
+ }
107
+
108
+ function notifyChange(doc: Document): void {
109
+ const artboard = doc.getElementById('artboard');
110
+ window.postMessage(
111
+ { type: 'SLIDE_CONTENT_CHANGED', html: artboard ? artboard.innerHTML : '' },
112
+ '*',
113
+ );
114
+ }
115
+
116
+ function buildToolbar(doc: Document): HTMLElement {
117
+ const bar = doc.createElement('div');
118
+ bar.id = TOOLBAR_ID;
119
+ bar.style.cssText = [
120
+ 'position: fixed',
121
+ 'z-index: 10000',
122
+ 'display: none',
123
+ 'align-items: center',
124
+ 'gap: 2px',
125
+ 'padding: 4px',
126
+ 'border-radius: 8px',
127
+ 'background: rgba(24,24,27,0.95)',
128
+ 'box-shadow: 0 8px 24px rgba(0,0,0,0.45)',
129
+ 'font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", sans-serif',
130
+ ].join(';');
131
+
132
+ const mkBtn = (label: string, swatch: string | null, onClick: () => void) => {
133
+ const b = doc.createElement('button');
134
+ b.type = 'button';
135
+ b.style.cssText = [
136
+ 'display: inline-flex',
137
+ 'align-items: center',
138
+ 'gap: 6px',
139
+ 'padding: 5px 10px',
140
+ 'border: 0',
141
+ 'border-radius: 6px',
142
+ 'background: transparent',
143
+ 'color: #e4e4e7',
144
+ 'font-size: 12px',
145
+ 'font-weight: 600',
146
+ 'cursor: pointer',
147
+ 'white-space: nowrap',
148
+ ].join(';');
149
+ if (swatch) {
150
+ const dot = doc.createElement('span');
151
+ dot.style.cssText = `width:10px;height:10px;border-radius:3px;background:${swatch};display:inline-block`;
152
+ b.appendChild(dot);
153
+ }
154
+ b.appendChild(doc.createTextNode(label));
155
+ b.addEventListener('mouseenter', () => (b.style.background = 'rgba(255,255,255,0.12)'));
156
+ b.addEventListener('mouseleave', () => (b.style.background = 'transparent'));
157
+ // mousedown でフォーカスと選択が失われないようにするのが肝
158
+ b.addEventListener('mousedown', (e) => {
159
+ e.preventDefault();
160
+ e.stopPropagation();
161
+ });
162
+ b.addEventListener('click', (e) => {
163
+ e.preventDefault();
164
+ e.stopPropagation();
165
+ onClick();
166
+ });
167
+ return b;
168
+ };
169
+
170
+ for (const f of FORMATS) {
171
+ bar.appendChild(
172
+ mkBtn(f.label, f.swatch, () => {
173
+ applyFormat(doc, f.cls);
174
+ notifyChange(doc);
175
+ position(doc, bar);
176
+ }),
177
+ );
178
+ }
179
+ const sep = doc.createElement('span');
180
+ sep.style.cssText = 'width:1px;height:16px;background:rgba(255,255,255,0.2);margin:0 2px';
181
+ bar.appendChild(sep);
182
+ bar.appendChild(
183
+ mkBtn('解除', null, () => {
184
+ const sel = doc.getSelection();
185
+ if (sel && sel.rangeCount > 0) {
186
+ clearFormats(doc, sel.getRangeAt(0));
187
+ notifyChange(doc);
188
+ }
189
+ position(doc, bar);
190
+ }),
191
+ );
192
+ doc.body.appendChild(bar);
193
+ return bar;
194
+ }
195
+
196
+ /** 選択範囲の上にツールバーを置く(画面上端に近いときは下) */
197
+ function position(doc: Document, bar: HTMLElement): void {
198
+ const host = editingHost(doc);
199
+ if (!host) {
200
+ bar.style.display = 'none';
201
+ return;
202
+ }
203
+ const rect = doc.getSelection()!.getRangeAt(0).getBoundingClientRect();
204
+ if (!rect || (rect.width === 0 && rect.height === 0)) {
205
+ bar.style.display = 'none';
206
+ return;
207
+ }
208
+ bar.style.display = 'flex';
209
+ // 一度表示してから実寸で位置決め
210
+ const bw = bar.offsetWidth;
211
+ const bh = bar.offsetHeight;
212
+ const win = doc.defaultView!;
213
+ let x = rect.left + rect.width / 2 - bw / 2;
214
+ x = Math.max(8, Math.min(win.innerWidth - bw - 8, x));
215
+ let y = rect.top - bh - 8;
216
+ if (y < 8) y = rect.bottom + 8;
217
+ bar.style.left = `${x}px`;
218
+ bar.style.top = `${y}px`;
219
+ }
220
+
221
+ /**
222
+ * インライン書式ツールバーを iframe に取り付ける。
223
+ * @returns 後片付け関数
224
+ */
225
+ export function setupInlineFormatToolbar(iframeDoc: Document): () => void {
226
+ const bar = buildToolbar(iframeDoc);
227
+ const onSelectionChange = () => position(iframeDoc, bar);
228
+ // 選択の変化・スクロールで追従。selectionchange は document にしか飛ばない
229
+ iframeDoc.addEventListener('selectionchange', onSelectionChange);
230
+ iframeDoc.addEventListener('scroll', onSelectionChange, true);
231
+ return () => {
232
+ iframeDoc.removeEventListener('selectionchange', onSelectionChange);
233
+ iframeDoc.removeEventListener('scroll', onSelectionChange, true);
234
+ bar.remove();
235
+ };
236
+ }
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Alt(Option)ホバーの距離メジャー(Figmaの赤い計測線)。
3
+ *
4
+ * 要素を選択した状態でAltを押しながら別の要素にカーソルを載せると、
5
+ * 選択要素とホバー要素の間の距離を赤い線とバッジで示す。
6
+ *
7
+ * 座標はすべてアートボード座標(1920×1080の素のpx)で計算し、レイヤーを
8
+ * アートボード直下に置く。ズームはアートボードごと transform されるので
9
+ * 線もバッジも自動で追従する。レイヤーは表示専用(pointer-events:none)で、
10
+ * 保存対象にもならない(getCleanHtml が #gg-measure-layer を除去する)。
11
+ */
12
+
13
+ const LAYER_ID = 'gg-measure-layer';
14
+ const RED = '#f24822'; // Figmaの計測色
15
+
16
+ type Rect = { left: number; top: number; right: number; bottom: number; width: number; height: number };
17
+
18
+ /** クライアント座標の矩形をアートボード座標へ */
19
+ function toArtboardRect(el: HTMLElement, artboard: HTMLElement): Rect {
20
+ const r = el.getBoundingClientRect();
21
+ const a = artboard.getBoundingClientRect();
22
+ const scale = a.width / artboard.offsetWidth || 1;
23
+ const left = (r.left - a.left) / scale;
24
+ const top = (r.top - a.top) / scale;
25
+ return { left, top, right: left + r.width / scale, bottom: top + r.height / scale, width: r.width / scale, height: r.height / scale };
26
+ }
27
+
28
+ type Seg = { x1: number; y1: number; x2: number; y2: number };
29
+
30
+ /** 2矩形間の計測線。軸ごとに「離れていれば近い辺の間」「重なっていれば辺のずれ」 */
31
+ function computeSegs(a: Rect, b: Rect): Seg[] {
32
+ const out: Seg[] = [];
33
+
34
+ // 垂直方向の重なり範囲の中心(無ければAの中心)。水平線のy位置に使う
35
+ const oy1 = Math.max(a.top, b.top);
36
+ const oy2 = Math.min(a.bottom, b.bottom);
37
+ const cy = oy1 < oy2 ? (oy1 + oy2) / 2 : a.top + a.height / 2;
38
+ // 水平方向の重なり範囲の中心。垂直線のx位置に使う
39
+ const ox1 = Math.max(a.left, b.left);
40
+ const ox2 = Math.min(a.right, b.right);
41
+ const cx = ox1 < ox2 ? (ox1 + ox2) / 2 : a.left + a.width / 2;
42
+
43
+ // ── X軸 ──
44
+ if (a.right <= b.left) {
45
+ out.push({ x1: a.right, y1: cy, x2: b.left, y2: cy });
46
+ } else if (b.right <= a.left) {
47
+ out.push({ x1: b.right, y1: cy, x2: a.left, y2: cy });
48
+ } else {
49
+ // 重なっている(内包・交差): 左辺どうし・右辺どうしのずれを出す
50
+ if (Math.abs(a.left - b.left) >= 0.5)
51
+ out.push({ x1: Math.min(a.left, b.left), y1: cy, x2: Math.max(a.left, b.left), y2: cy });
52
+ if (Math.abs(a.right - b.right) >= 0.5)
53
+ out.push({ x1: Math.min(a.right, b.right), y1: cy, x2: Math.max(a.right, b.right), y2: cy });
54
+ }
55
+
56
+ // ── Y軸 ──
57
+ if (a.bottom <= b.top) {
58
+ out.push({ x1: cx, y1: a.bottom, x2: cx, y2: b.top });
59
+ } else if (b.bottom <= a.top) {
60
+ out.push({ x1: cx, y1: b.bottom, x2: cx, y2: a.top });
61
+ } else {
62
+ if (Math.abs(a.top - b.top) >= 0.5)
63
+ out.push({ x1: cx, y1: Math.min(a.top, b.top), x2: cx, y2: Math.max(a.top, b.top) });
64
+ if (Math.abs(a.bottom - b.bottom) >= 0.5)
65
+ out.push({ x1: cx, y1: Math.min(a.bottom, b.bottom), x2: cx, y2: Math.max(a.bottom, b.bottom) });
66
+ }
67
+
68
+ return out;
69
+ }
70
+
71
+ export function clearMeasure(doc: Document): void {
72
+ doc.getElementById(LAYER_ID)?.remove();
73
+ }
74
+
75
+ /** 選択要素(sel)とホバー要素(target)の距離を描く */
76
+ export function drawMeasure(doc: Document, sel: HTMLElement, target: HTMLElement): void {
77
+ const artboard = doc.getElementById('artboard');
78
+ if (!artboard) return;
79
+
80
+ clearMeasure(doc);
81
+ const layer = doc.createElement('div');
82
+ layer.id = LAYER_ID;
83
+ layer.style.cssText = 'position:absolute;inset:0;z-index:9400;pointer-events:none;';
84
+
85
+ const a = toArtboardRect(sel, artboard);
86
+ const b = toArtboardRect(target, artboard);
87
+
88
+ // ホバー先を薄い赤枠で示す(どこと測っているかが分かるように)
89
+ const outline = doc.createElement('div');
90
+ outline.style.cssText =
91
+ `position:absolute;left:${b.left}px;top:${b.top}px;width:${b.width}px;height:${b.height}px;` +
92
+ `outline:1px dashed ${RED};outline-offset:-1px;`;
93
+ layer.appendChild(outline);
94
+
95
+ for (const s of computeSegs(a, b)) {
96
+ const len = Math.hypot(s.x2 - s.x1, s.y2 - s.y1);
97
+ if (len < 0.5) continue;
98
+ const horizontal = Math.abs(s.y2 - s.y1) < Math.abs(s.x2 - s.x1);
99
+
100
+ const line = doc.createElement('div');
101
+ line.style.cssText = horizontal
102
+ ? `position:absolute;left:${Math.min(s.x1, s.x2)}px;top:${s.y1 - 0.75}px;width:${len}px;height:1.5px;background:${RED};`
103
+ : `position:absolute;left:${s.x1 - 0.75}px;top:${Math.min(s.y1, s.y2)}px;width:1.5px;height:${len}px;background:${RED};`;
104
+ layer.appendChild(line);
105
+
106
+ const badge = doc.createElement('div');
107
+ badge.textContent = String(Math.round(len));
108
+ badge.style.cssText =
109
+ `position:absolute;left:${(s.x1 + s.x2) / 2}px;top:${(s.y1 + s.y2) / 2}px;` +
110
+ `transform:translate(-50%,-50%);background:${RED};color:#fff;` +
111
+ 'font:600 12px/1.5 -apple-system,BlinkMacSystemFont,"Hiragino Sans",sans-serif;' +
112
+ 'padding:0 5px;border-radius:3px;white-space:nowrap;';
113
+ // 横線のバッジは線の少し下、縦線のバッジは線の少し右に逃がす(線と重ねない)
114
+ if (horizontal) badge.style.marginTop = '12px';
115
+ else badge.style.marginLeft = '18px';
116
+ layer.appendChild(badge);
117
+ }
118
+
119
+ artboard.appendChild(layer);
120
+ }