@bendyline/squisq-editor-react 1.6.2 → 2.0.1

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 (218) hide show
  1. package/README.md +12 -3
  2. package/dist/index.d.ts +1275 -183
  3. package/dist/index.js +20345 -12777
  4. package/dist/index.js.map +1 -1
  5. package/dist/monaco.d.ts +1 -0
  6. package/dist/monaco.js +4 -0
  7. package/dist/monaco.js.map +1 -0
  8. package/dist/styles/index.css +1607 -241
  9. package/package.json +9 -5
  10. package/src/BlockPropertiesPopover.tsx +30 -15
  11. package/src/DocumentSettingsDialog.tsx +39 -33
  12. package/src/EditorContext.tsx +199 -42
  13. package/src/EditorShell.tsx +349 -261
  14. package/src/ImageEditor.tsx +22 -1
  15. package/src/InlinePreviewGutter.tsx +16 -8
  16. package/src/MediaBin.tsx +107 -14
  17. package/src/OutlinePanel.tsx +202 -7
  18. package/src/PlainHtmlPreview.tsx +40 -1
  19. package/src/PreviewControls.tsx +362 -89
  20. package/src/PreviewPanel.tsx +312 -78
  21. package/src/RawEditor.tsx +13 -2
  22. package/src/RecorderEntry.tsx +2 -0
  23. package/src/StatusBar.tsx +5 -2
  24. package/src/TemplateAnnotation.ts +12 -3
  25. package/src/TemplatePicker.tsx +75 -21
  26. package/src/ThemeCustomizerPanel.tsx +8 -0
  27. package/src/ThemePicker.tsx +10 -5
  28. package/src/TimelineBlockPreview.tsx +1 -1
  29. package/src/Toolbar.tsx +357 -51
  30. package/src/TransitionPicker.tsx +36 -4
  31. package/src/ViewMenuPanel.tsx +12 -14
  32. package/src/WysiwygEditor.tsx +124 -91
  33. package/src/__tests__/blockPropertiesPopoverTheme.test.tsx +33 -0
  34. package/src/__tests__/blockTagActivity.test.ts +183 -0
  35. package/src/__tests__/buildDocumentPreviewMarkdown.test.ts +75 -0
  36. package/src/__tests__/buildPreviewDocContent.test.ts +48 -0
  37. package/src/__tests__/buildPreviewDocTransition.test.ts +21 -1
  38. package/src/__tests__/canvasSurfaceTextEditing.test.tsx +60 -0
  39. package/src/__tests__/codeContextSectionView.test.tsx +8 -6
  40. package/src/__tests__/customLayoutManagerTheme.test.tsx +34 -0
  41. package/src/__tests__/documentSettingsDialog.test.tsx +51 -1
  42. package/src/__tests__/editorScrollHandoff.test.tsx +102 -0
  43. package/src/__tests__/editorScrollSync.test.ts +65 -0
  44. package/src/__tests__/editorShellProps.test.tsx +123 -1
  45. package/src/__tests__/emojiPicker.test.tsx +2 -42
  46. package/src/__tests__/headingTransition.test.ts +0 -12
  47. package/src/__tests__/imageEditAffordance.test.tsx +5 -1
  48. package/src/__tests__/imageEditModalContract.test.tsx +119 -0
  49. package/src/__tests__/imageEditorShell.test.tsx +15 -1
  50. package/src/__tests__/jsonEditor.test.tsx +56 -1
  51. package/src/__tests__/layersPanel.test.tsx +75 -5
  52. package/src/__tests__/majorApiTypes.test.ts +26 -0
  53. package/src/__tests__/mediaAttachmentFlow.test.ts +2 -2
  54. package/src/__tests__/mediaBinDrop.test.tsx +90 -0
  55. package/src/__tests__/narrationSave.test.ts +148 -0
  56. package/src/__tests__/outlinePanel.test.tsx +93 -3
  57. package/src/__tests__/outlineSource.test.ts +217 -0
  58. package/src/__tests__/plainHtmlPreview.test.tsx +17 -1
  59. package/src/__tests__/presentationMode.test.tsx +290 -0
  60. package/src/__tests__/previewControls.test.tsx +382 -5
  61. package/src/__tests__/previewPanelAudioFailure.test.tsx +46 -0
  62. package/src/__tests__/previewPanelPresentation.test.tsx +126 -0
  63. package/src/__tests__/previewPanelTransforms.test.tsx +117 -0
  64. package/src/__tests__/rawEditorModelIsolation.test.ts +19 -0
  65. package/src/__tests__/recorderTheme.test.tsx +42 -0
  66. package/src/__tests__/resolveBlockVisual.test.ts +43 -0
  67. package/src/__tests__/selectionConversions.test.ts +80 -0
  68. package/src/__tests__/statusBar.test.tsx +27 -0
  69. package/src/__tests__/teleprompter.test.tsx +190 -0
  70. package/src/__tests__/templateAnnotationRoundTrip.test.ts +26 -0
  71. package/src/__tests__/templatePickerPortal.test.tsx +62 -0
  72. package/src/__tests__/tiptapBridge.test.ts +71 -7
  73. package/src/__tests__/tiptapCodeBlockRoundTrip.test.ts +109 -0
  74. package/src/__tests__/tiptapImageRoundTrip.test.ts +1 -1
  75. package/src/__tests__/toolbarSelectionConversion.test.tsx +164 -0
  76. package/src/__tests__/transformStyleId.test.ts +13 -0
  77. package/src/__tests__/useImageEditor.test.tsx +67 -0
  78. package/src/__tests__/useMediaRecorder.test.ts +75 -0
  79. package/src/__tests__/viewMenuPanel.test.tsx +130 -0
  80. package/src/__tests__/wysiwygImageUpload.test.ts +69 -0
  81. package/src/__tests__/wysiwygTemplateBadgeFocus.test.tsx +56 -0
  82. package/src/asciiDiagram/AsciiDiagramExtension.ts +343 -0
  83. package/src/asciiDiagram/AsciiDiagramWidget.tsx +254 -0
  84. package/src/asciiDiagram/RepairableDiagramExtension.ts +200 -0
  85. package/src/asciiDiagram/__tests__/AsciiDiagramExtension.test.ts +145 -0
  86. package/src/asciiDiagram/__tests__/RepairableDiagramExtension.test.ts +159 -0
  87. package/src/asciiDiagram/__tests__/asciiDiagramCommands.test.ts +284 -0
  88. package/src/asciiDiagram/__tests__/asciiDiagramOps.test.ts +114 -0
  89. package/src/asciiDiagram/__tests__/asciiPaste.test.ts +43 -0
  90. package/src/asciiDiagram/asciiDiagramCommands.ts +174 -0
  91. package/src/asciiDiagram/asciiDiagramData.ts +149 -0
  92. package/src/asciiDiagram/asciiDiagramOps.ts +238 -0
  93. package/src/asciiDiagram/asciiPaste.ts +19 -0
  94. package/src/blockTagActivity.ts +233 -0
  95. package/src/buildDocumentPreviewMarkdown.ts +168 -0
  96. package/src/buildPreviewDoc.ts +19 -12
  97. package/src/codeContext/types.ts +1 -1
  98. package/src/customTemplates/CustomLayoutManager.tsx +3 -2
  99. package/src/customTemplates/CustomTemplateContext.tsx +6 -0
  100. package/src/customTemplates/TemplateDesigner.tsx +7 -4
  101. package/src/customTemplates/__tests__/library.test.ts +8 -0
  102. package/src/customTemplates/__tests__/useMemoryLayerAdapter.test.ts +13 -0
  103. package/src/customTemplates/designer.css +158 -99
  104. package/src/customTemplates/library.ts +16 -3
  105. package/src/customTemplates/useMemoryLayerAdapter.ts +7 -1
  106. package/src/customThemes/CustomThemeContext.tsx +6 -0
  107. package/src/customThemes/CustomThemeDialog.tsx +35 -5
  108. package/src/customThemes/ImportThemeSection.tsx +178 -0
  109. package/src/customThemes/__tests__/customThemeLibrary.test.ts +9 -0
  110. package/src/customThemes/__tests__/importThemeSection.test.tsx +192 -0
  111. package/src/customThemes/customThemeLibrary.ts +7 -4
  112. package/src/customThemes/index.ts +7 -1
  113. package/src/customThemes/themeDraft.ts +23 -7
  114. package/src/diagram/DiagramCanvas.tsx +25 -6
  115. package/src/diagram/types.ts +35 -0
  116. package/src/editorScrollSync.ts +73 -0
  117. package/src/emojiData.ts +3 -23
  118. package/src/frontmatterSettings.ts +23 -0
  119. package/src/headingTransition.ts +0 -15
  120. package/src/imageEditor/CanvasSurface.tsx +29 -23
  121. package/src/imageEditor/LayersPanel.tsx +78 -3
  122. package/src/imageEditor/Toolbar.tsx +5 -1
  123. package/src/imageEditor/icons.tsx +4 -0
  124. package/src/imageEditor/image-editor.css +75 -0
  125. package/src/imageEditor/layers/SelectionHandles.tsx +1 -1
  126. package/src/imageEditor/useImageEditor.ts +71 -14
  127. package/src/index.ts +198 -20
  128. package/src/jsonEditor/JsonEditorContext.tsx +12 -6
  129. package/src/jsonEditor/RenderNode.tsx +24 -3
  130. package/src/jsonEditor/editors.tsx +86 -19
  131. package/src/mediaEntries.ts +12 -0
  132. package/src/monaco.ts +35 -0
  133. package/src/monacoWorkers.ts +89 -0
  134. package/src/outlineSource.ts +171 -0
  135. package/src/presentation/PresentationMode.tsx +596 -0
  136. package/src/rawEditorIsolation.ts +18 -0
  137. package/src/recorder/RecorderButton.tsx +9 -1
  138. package/src/recorder/RecorderModal.tsx +84 -41
  139. package/src/recorder/RecorderPanel.tsx +9 -1
  140. package/src/recorder/hooks/useMediaRecorder.ts +97 -53
  141. package/src/resolveBlockVisual.ts +10 -4
  142. package/src/scene/Scene.tsx +48 -18
  143. package/src/scene/SceneBlockExtension.ts +17 -10
  144. package/src/scene/SceneBlockWidget.tsx +5 -1
  145. package/src/scene/SceneViewport.tsx +7 -3
  146. package/src/scene/ShapePalette.tsx +17 -2
  147. package/src/scene/__tests__/DiagramAdapter.test.ts +86 -0
  148. package/src/scene/__tests__/SceneBlockExtension.test.ts +33 -0
  149. package/src/scene/__tests__/sceneIsolation.test.tsx +119 -0
  150. package/src/scene/adapters/DiagramAdapter.ts +12 -101
  151. package/src/scene/adapters/DrawingAdapter.ts +6 -1
  152. package/src/scene/adapters/LayoutAdapter.ts +3 -0
  153. package/src/scene/commands/SceneCommand.ts +17 -3
  154. package/src/scene/index.ts +1 -6
  155. package/src/scene/layers/DiagramEdges.tsx +21 -7
  156. package/src/scene/layers/edgeGeometry.ts +9 -1
  157. package/src/scene/layers/nodeCard.tsx +27 -8
  158. package/src/scene/scene.css +5 -2
  159. package/src/scene/text/SceneTextOverlay.tsx +5 -5
  160. package/src/scene/text/sceneTextChannel.ts +26 -20
  161. package/src/scene/text/sceneTextConfig.ts +4 -0
  162. package/src/scene/tools/ConnectTool.ts +13 -4
  163. package/src/scene/tools/SceneTool.ts +8 -0
  164. package/src/scene/tools/SelectTool.ts +39 -23
  165. package/src/scene/tools/ShapeTool.ts +2 -3
  166. package/src/selectionConversions.ts +155 -0
  167. package/src/styles/ascii-diagram.css +79 -0
  168. package/src/styles/ascii-timeline.css +596 -0
  169. package/src/styles/editor.css +736 -120
  170. package/src/styles/index.css +3 -0
  171. package/src/styles/tree-view.css +189 -0
  172. package/src/teleprompter/TeleprompterControls.tsx +218 -0
  173. package/src/teleprompter/TeleprompterSelfView.tsx +22 -0
  174. package/src/teleprompter/TeleprompterSurface.tsx +267 -0
  175. package/src/teleprompter/TeleprompterView.tsx +338 -0
  176. package/src/teleprompter/canvasRenderer.ts +161 -0
  177. package/src/teleprompter/floatingWindow.ts +352 -0
  178. package/src/teleprompter/index.ts +61 -0
  179. package/src/teleprompter/pcmWorklet.ts +62 -0
  180. package/src/teleprompter/recording/insertPreamble.ts +80 -0
  181. package/src/teleprompter/recording/narrationSave.ts +134 -0
  182. package/src/teleprompter/recording/useNarrationRecorder.ts +367 -0
  183. package/src/teleprompter/scrollModel.ts +85 -0
  184. package/src/teleprompter/teleprompterTheme.ts +303 -0
  185. package/src/teleprompter/types.ts +38 -0
  186. package/src/teleprompter/useFloatingWindow.ts +74 -0
  187. package/src/teleprompter/useMicAnalysis.ts +211 -0
  188. package/src/teleprompter/useTeleprompter.ts +421 -0
  189. package/src/templateContentPreviewResolver.ts +12 -5
  190. package/src/timeline/TimelineEditorWidget.tsx +861 -0
  191. package/src/timeline/TimelineViewExtension.ts +252 -0
  192. package/src/timeline/__tests__/TimelineEditorWidget.test.tsx +414 -0
  193. package/src/timeline/__tests__/TimelineViewExtension.test.ts +163 -0
  194. package/src/timeline/__tests__/timelineCommands.test.ts +359 -0
  195. package/src/timeline/__tests__/timelineOps.test.ts +277 -0
  196. package/src/timeline/__tests__/timelinePaste.test.ts +34 -0
  197. package/src/timeline/timelineCommands.ts +421 -0
  198. package/src/timeline/timelineData.ts +54 -0
  199. package/src/timeline/timelineOps.ts +381 -0
  200. package/src/timeline/timelinePaste.ts +8 -0
  201. package/src/tiptapBridge.ts +47 -21
  202. package/src/transformStyleId.ts +17 -0
  203. package/src/treeview/TreeOutlineWidget.tsx +390 -0
  204. package/src/treeview/TreeViewExtension.ts +250 -0
  205. package/src/treeview/__tests__/TreeOutlineWidget.test.tsx +156 -0
  206. package/src/treeview/__tests__/TreeViewExtension.test.ts +122 -0
  207. package/src/treeview/__tests__/treeOps.test.ts +177 -0
  208. package/src/treeview/__tests__/treePaste.test.ts +40 -0
  209. package/src/treeview/__tests__/treeViewCommands.test.ts +179 -0
  210. package/src/treeview/treeOps.ts +231 -0
  211. package/src/treeview/treePaste.ts +13 -0
  212. package/src/treeview/treeViewCommands.ts +99 -0
  213. package/src/treeview/treeViewData.ts +41 -0
  214. package/src/useMonacoLoader.ts +11 -34
  215. package/src/wysiwygImageUpload.ts +113 -0
  216. package/src/diagram/DiagramExtension.ts +0 -209
  217. package/src/diagram/DiagramWidget.tsx +0 -272
  218. package/src/diagram/useDiagramData.ts +0 -126
@@ -0,0 +1,231 @@
1
+ /**
2
+ * Pure outline operations over the tree model — the write-direction half of
3
+ * the fence-is-source-of-truth loop:
4
+ *
5
+ * fence text → parseTree → op → renderTree → fence text
6
+ *
7
+ * Every op returns a NEW Tree (inputs untouched). These are the genuinely
8
+ * new operations with no diagram analog: indent / outdent (re-parent a node
9
+ * carrying its subtree), move-up / move-down (reorder siblings), plus
10
+ * add / rename / remove / toggle-directory. Connector rails re-derive in
11
+ * render, so ops only touch structure + labels.
12
+ */
13
+
14
+ import type { Tree, TreeNode } from '@bendyline/squisq/doc';
15
+
16
+ /** Make an arbitrary rename safe as a tree label: no connector glyphs / newlines. */
17
+ export function sanitizeTreeLabel(label: string): string {
18
+ return label
19
+ .replace(/[├└│─┃┏┓┗┛┣┫┳┻╋╰╭╮╯|`]/gu, ' ')
20
+ .replace(/\s+/g, ' ')
21
+ .trim();
22
+ }
23
+
24
+ function cloneNode(n: TreeNode): TreeNode {
25
+ return {
26
+ id: n.id,
27
+ label: n.label,
28
+ children: n.children.map(cloneNode),
29
+ ...(n.isDir ? { isDir: true } : {}),
30
+ ...(n.comment ? { comment: n.comment } : {}),
31
+ };
32
+ }
33
+ function cloneTree(t: Tree): Tree {
34
+ return { roots: t.roots.map(cloneNode), style: t.style, warnings: [...t.warnings] };
35
+ }
36
+
37
+ interface Loc {
38
+ siblings: TreeNode[];
39
+ index: number;
40
+ node: TreeNode;
41
+ parent: TreeNode | null;
42
+ }
43
+
44
+ export type TreeDropPosition = 'before' | 'child' | 'after';
45
+
46
+ /** Locate a node by id in a (cloned) tree, returning its sibling array + index + parent. */
47
+ function locate(roots: TreeNode[], id: string, parent: TreeNode | null = null): Loc | null {
48
+ for (let i = 0; i < roots.length; i++) {
49
+ if (roots[i].id === id) return { siblings: roots, index: i, node: roots[i], parent };
50
+ const inner = locate(roots[i].children, id, roots[i]);
51
+ if (inner) return inner;
52
+ }
53
+ return null;
54
+ }
55
+
56
+ function markDir(n: TreeNode): void {
57
+ n.isDir = n.label.endsWith('/') || n.children.length > 0;
58
+ }
59
+
60
+ function containsNode(node: TreeNode, id: string): boolean {
61
+ return node.id === id || node.children.some((child) => containsNode(child, id));
62
+ }
63
+
64
+ export function renameItemOp(tree: Tree, id: string, label: string): Tree {
65
+ const clean = sanitizeTreeLabel(label);
66
+ const next = cloneTree(tree);
67
+ const loc = locate(next.roots, id);
68
+ if (!loc || clean.length === 0 || clean === loc.node.label) return tree;
69
+ loc.node.label = clean;
70
+ markDir(loc.node);
71
+ return next;
72
+ }
73
+
74
+ export function addItemOp(
75
+ tree: Tree,
76
+ targetId: string,
77
+ position: 'child' | 'siblingAfter',
78
+ label?: string,
79
+ isDir?: boolean,
80
+ ): Tree {
81
+ const next = cloneTree(tree);
82
+ const loc = locate(next.roots, targetId);
83
+ if (!loc) return tree;
84
+ let name = sanitizeTreeLabel(label ?? '') || freshLabel(next, isDir);
85
+ // A folder carries a trailing slash so it re-parses as a directory.
86
+ if (isDir && !name.endsWith('/')) name += '/';
87
+ const node: TreeNode = {
88
+ id: `__new-${name}`,
89
+ label: name,
90
+ children: [],
91
+ ...(isDir ? { isDir: true } : {}),
92
+ };
93
+ if (position === 'child') {
94
+ loc.node.children.push(node);
95
+ markDir(loc.node);
96
+ } else {
97
+ loc.siblings.splice(loc.index + 1, 0, node);
98
+ }
99
+ return next;
100
+ }
101
+
102
+ export function removeItemOp(tree: Tree, id: string): Tree {
103
+ const next = cloneTree(tree);
104
+ const loc = locate(next.roots, id);
105
+ if (!loc) return tree;
106
+ loc.siblings.splice(loc.index, 1);
107
+ if (loc.parent) markDir(loc.parent);
108
+ return next;
109
+ }
110
+
111
+ /** Node becomes the last child of its immediate preceding sibling. */
112
+ export function indentItemOp(tree: Tree, id: string): Tree {
113
+ const next = cloneTree(tree);
114
+ const loc = locate(next.roots, id);
115
+ if (!loc || loc.index === 0) return tree; // nothing to indent under
116
+ const [moved] = loc.siblings.splice(loc.index, 1);
117
+ const newParent = loc.siblings[loc.index - 1];
118
+ newParent.children.push(moved);
119
+ markDir(newParent);
120
+ return next;
121
+ }
122
+
123
+ /** Node becomes a sibling of its parent, inserted directly after it (subtree carried). */
124
+ export function outdentItemOp(tree: Tree, id: string): Tree {
125
+ const next = cloneTree(tree);
126
+ const loc = locate(next.roots, id);
127
+ if (!loc || !loc.parent) return tree; // roots can't outdent
128
+ const grand = locate(next.roots, loc.parent.id);
129
+ if (!grand) return tree;
130
+ const [moved] = loc.siblings.splice(loc.index, 1);
131
+ grand.siblings.splice(grand.index + 1, 0, moved);
132
+ markDir(loc.parent);
133
+ return next;
134
+ }
135
+
136
+ /**
137
+ * Move a node and its complete subtree relative to any other node. Dropping
138
+ * before/after adopts the target's parent; dropping as a child appends to the
139
+ * target. A node can never be moved into its own subtree.
140
+ */
141
+ export function moveItemOp(
142
+ tree: Tree,
143
+ id: string,
144
+ targetId: string,
145
+ position: TreeDropPosition,
146
+ ): Tree {
147
+ const currentSource = locate(tree.roots, id);
148
+ const currentTarget = locate(tree.roots, targetId);
149
+ if (
150
+ !currentSource ||
151
+ !currentTarget ||
152
+ id === targetId ||
153
+ containsNode(currentSource.node, targetId)
154
+ ) {
155
+ return tree;
156
+ }
157
+
158
+ // Avoid a fence rewrite when the drop describes the node's current slot.
159
+ if (currentSource.siblings === currentTarget.siblings) {
160
+ if (position === 'before' && currentSource.index === currentTarget.index - 1) return tree;
161
+ if (position === 'after' && currentSource.index === currentTarget.index + 1) return tree;
162
+ }
163
+ if (
164
+ position === 'child' &&
165
+ currentSource.parent?.id === currentTarget.node.id &&
166
+ currentSource.index === currentSource.siblings.length - 1
167
+ ) {
168
+ return tree;
169
+ }
170
+
171
+ const next = cloneTree(tree);
172
+ const source = locate(next.roots, id);
173
+ if (!source) return tree;
174
+ const [moved] = source.siblings.splice(source.index, 1);
175
+ if (source.parent) markDir(source.parent);
176
+
177
+ // Locate again after removal so same-sibling indices are current.
178
+ const target = locate(next.roots, targetId);
179
+ if (!target) return tree;
180
+ if (position === 'child') {
181
+ target.node.children.push(moved);
182
+ markDir(target.node);
183
+ } else {
184
+ target.siblings.splice(target.index + (position === 'after' ? 1 : 0), 0, moved);
185
+ }
186
+ return next;
187
+ }
188
+
189
+ export function moveItemUpOp(tree: Tree, id: string): Tree {
190
+ const next = cloneTree(tree);
191
+ const loc = locate(next.roots, id);
192
+ if (!loc || loc.index === 0) return tree;
193
+ [loc.siblings[loc.index - 1], loc.siblings[loc.index]] = [
194
+ loc.siblings[loc.index],
195
+ loc.siblings[loc.index - 1],
196
+ ];
197
+ return next;
198
+ }
199
+
200
+ export function moveItemDownOp(tree: Tree, id: string): Tree {
201
+ const next = cloneTree(tree);
202
+ const loc = locate(next.roots, id);
203
+ if (!loc || loc.index >= loc.siblings.length - 1) return tree;
204
+ [loc.siblings[loc.index], loc.siblings[loc.index + 1]] = [
205
+ loc.siblings[loc.index + 1],
206
+ loc.siblings[loc.index],
207
+ ];
208
+ return next;
209
+ }
210
+
211
+ /** Toggle the trailing-slash directory marker on a node's label. */
212
+ export function toggleDirOp(tree: Tree, id: string): Tree {
213
+ const next = cloneTree(tree);
214
+ const loc = locate(next.roots, id);
215
+ if (!loc) return tree;
216
+ loc.node.label = loc.node.label.endsWith('/')
217
+ ? loc.node.label.slice(0, -1)
218
+ : `${loc.node.label}/`;
219
+ markDir(loc.node);
220
+ return next;
221
+ }
222
+
223
+ function freshLabel(tree: Tree, isDir?: boolean): string {
224
+ const labels = new Set<string>();
225
+ const walk = (ns: TreeNode[]): void => ns.forEach((n) => (labels.add(n.label), walk(n.children)));
226
+ walk(tree.roots);
227
+ const base = isDir ? 'folder' : 'item';
228
+ let i = 1;
229
+ while (labels.has(`${base}${i}`) || labels.has(`${base}${i}/`)) i++;
230
+ return `${base}${i}`;
231
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Paste gate for bare (unfenced) ASCII tree art. `├──`/`└──` file-tree lines
3
+ * would otherwise route through the markdown converter and mangle; when this
4
+ * matches, the paste handler drops the text verbatim into a fresh code block
5
+ * that the TreeViewExtension picks up as an interactive outline.
6
+ */
7
+
8
+ import { detectTree } from '@bendyline/squisq/doc';
9
+
10
+ export function shouldPasteAsTreeFence(text: string): boolean {
11
+ if (!text || text.includes('```')) return false;
12
+ return detectTree(text).isTree;
13
+ }
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Write direction of the tree loop: outline command → fence rewrite.
3
+ *
4
+ * parse → op → render → verify-reparse → single-transaction fence replace
5
+ * (one undo step). Reuses the generic `replaceAsciiFenceText` (it only
6
+ * touches codeBlock text, attributes untouched).
7
+ */
8
+
9
+ import type { Editor } from '@tiptap/react';
10
+ import { parseTree, renderTree, type Tree } from '@bendyline/squisq/doc';
11
+ import { replaceAsciiFenceText } from '../asciiDiagram/asciiDiagramCommands';
12
+ import { findTreeBlockPos, parseTreeForNode } from './TreeViewExtension';
13
+ import {
14
+ addItemOp,
15
+ indentItemOp,
16
+ moveItemOp,
17
+ moveItemDownOp,
18
+ moveItemUpOp,
19
+ outdentItemOp,
20
+ removeItemOp,
21
+ renameItemOp,
22
+ toggleDirOp,
23
+ type TreeDropPosition,
24
+ } from './treeOps';
25
+
26
+ export type TreeCommand =
27
+ | {
28
+ kind: 'addItem';
29
+ targetId: string;
30
+ position: 'child' | 'siblingAfter';
31
+ label?: string;
32
+ isDir?: boolean;
33
+ }
34
+ | { kind: 'renameItem'; id: string; label: string }
35
+ | { kind: 'indentItem'; id: string }
36
+ | { kind: 'outdentItem'; id: string }
37
+ | { kind: 'moveItem'; id: string; targetId: string; position: TreeDropPosition }
38
+ | { kind: 'moveItemUp'; id: string }
39
+ | { kind: 'moveItemDown'; id: string }
40
+ | { kind: 'removeItem'; id: string }
41
+ | { kind: 'toggleDir'; id: string };
42
+
43
+ /** Re-export so the widget can rewrite raw fence text without a second copy. */
44
+ export { replaceAsciiFenceText as replaceTreeFenceText } from '../asciiDiagram/asciiDiagramCommands';
45
+
46
+ function countNodes(nodes: readonly Tree['roots'][number][]): number {
47
+ let n = 0;
48
+ for (const node of nodes) n += 1 + countNodes(node.children);
49
+ return n;
50
+ }
51
+
52
+ function applyOp(editor: Editor, blockId: string, op: (t: Tree) => Tree): boolean {
53
+ const pos = findTreeBlockPos(editor, blockId);
54
+ if (pos === null) return false;
55
+ const node = editor.state.doc.nodeAt(pos);
56
+ if (!node || node.type.name !== 'codeBlock') return false;
57
+ const tree = parseTreeForNode(node);
58
+ if (!tree) return false;
59
+
60
+ const next = op(tree);
61
+ if (next === tree) return false;
62
+ const rendered = renderTree(next);
63
+
64
+ // Verify before committing: the rendered art must re-parse to the same
65
+ // node count, or we'd write a tree the outline can't read back.
66
+ if (countNodes(parseTree(rendered).roots) !== countNodes(next.roots)) return false;
67
+
68
+ // Promote the fence language to the explicit `tree` tag so this block's
69
+ // identity survives a later flatten → markdown → re-import round-trip.
70
+ return replaceAsciiFenceText(editor, pos, rendered, 'tree');
71
+ }
72
+
73
+ export function applyTreeCommand(editor: Editor, blockId: string, cmd: TreeCommand): boolean {
74
+ switch (cmd.kind) {
75
+ case 'addItem':
76
+ return applyOp(editor, blockId, (t) =>
77
+ addItemOp(t, cmd.targetId, cmd.position, cmd.label, cmd.isDir),
78
+ );
79
+ case 'renameItem':
80
+ return applyOp(editor, blockId, (t) => renameItemOp(t, cmd.id, cmd.label));
81
+ case 'indentItem':
82
+ return applyOp(editor, blockId, (t) => indentItemOp(t, cmd.id));
83
+ case 'outdentItem':
84
+ return applyOp(editor, blockId, (t) => outdentItemOp(t, cmd.id));
85
+ case 'moveItem':
86
+ return applyOp(editor, blockId, (t) => moveItemOp(t, cmd.id, cmd.targetId, cmd.position));
87
+ case 'moveItemUp':
88
+ return applyOp(editor, blockId, (t) => moveItemUpOp(t, cmd.id));
89
+ case 'moveItemDown':
90
+ return applyOp(editor, blockId, (t) => moveItemDownOp(t, cmd.id));
91
+ case 'removeItem':
92
+ return applyOp(editor, blockId, (t) => removeItemOp(t, cmd.id));
93
+ case 'toggleDir':
94
+ return applyOp(editor, blockId, (t) => toggleDirOp(t, cmd.id));
95
+ }
96
+ const _exhaustive: never = cmd;
97
+ void _exhaustive;
98
+ return false;
99
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Read direction of the tree loop: fence text → outline view.
3
+ *
4
+ * `useTreeViewData` re-derives on every editor transaction; parsing is
5
+ * memoized per PMNode by the extension's WeakMap caches, so untouched
6
+ * fences cost nothing.
7
+ */
8
+
9
+ import { useEffect, useMemo, useState } from 'react';
10
+ import type { Editor } from '@tiptap/react';
11
+ import type { Tree } from '@bendyline/squisq/doc';
12
+ import { findTreeBlockPos, parseTreeForNode } from './TreeViewExtension';
13
+
14
+ export interface TreeViewData {
15
+ tree: Tree;
16
+ text: string;
17
+ warnings: string[];
18
+ }
19
+
20
+ /** Live view of one registered tree block; null when it no longer parses. */
21
+ export function useTreeViewData(editor: Editor, blockId: string): TreeViewData | null {
22
+ const [version, setVersion] = useState(0);
23
+ useEffect(() => {
24
+ const onUpdate = () => setVersion((v) => v + 1);
25
+ editor.on('transaction', onUpdate);
26
+ return () => {
27
+ editor.off('transaction', onUpdate);
28
+ };
29
+ }, [editor]);
30
+
31
+ return useMemo(() => {
32
+ const pos = findTreeBlockPos(editor, blockId);
33
+ if (pos === null) return null;
34
+ const node = editor.state.doc.nodeAt(pos);
35
+ if (!node || node.type.name !== 'codeBlock') return null;
36
+ const tree = parseTreeForNode(node);
37
+ if (!tree) return null;
38
+ return { tree, text: node.textContent, warnings: tree.warnings };
39
+ // eslint-disable-next-line react-hooks/exhaustive-deps
40
+ }, [editor, blockId, version]);
41
+ }
@@ -49,42 +49,19 @@ export function useMonacoLoader(): UseMonacoLoaderResult {
49
49
  useEffect(() => {
50
50
  if (state.ready) return;
51
51
  if (!monacoPromise) {
52
- // Import the explicit ESM entry file rather than the bare
53
- // package specifier `'monaco-editor'`. The package's
54
- // `package.json` has no `main` / no `exports` (only `module`),
55
- // which trips Vite's strict resolver on both static and
56
- // dynamic imports of the bare name. Pointing at the file
57
- // directly the same path monaco's `module` field points at
58
- // turns this into a regular file-path resolve that doesn't
59
- // care about the manifest's entry fields. Works identically
60
- // in Vite dev / build, vitest's transform pipeline, and any
61
- // bundler-using downstream consumer.
52
+ // Load Monaco through squisq's canonical `./monaco` entry (see that
53
+ // file for exactly which slice of Monaco ships and why). Centralizing
54
+ // the choice there means the editor and any downstream app that imports
55
+ // `@bendyline/squisq-editor-react/monaco` share ONE instance and feature
56
+ // set no host aliasing, no hand-rolled slim entry that silently drops
57
+ // the suggest widget. The import is dynamic so consumers that only use
58
+ // types or `JsonEditor` never pull Monaco into their graph.
62
59
  //
63
- // Use `editor.main.js`, NOT `editor.api.js`. The `api` entry is
64
- // the bare standalone API with zero language contributions —
65
- // RawEditor's `defaultLanguage="typescript"` then mounts with
66
- // no tokenizer registered, and every file renders as
67
- // undifferentiated foreground text (the regression that surfaced
68
- // when a `.ts` file in the chat workspace previewer showed up
69
- // with no syntax coloring). `editor.main.js` re-exports the API
70
- // and additionally pulls in `basic-languages/monaco.contribution`
71
- // (TM grammars for ~70 languages) plus the four rich language
72
- // services (css / html / json / typescript). This is what makes
73
- // `defaultLanguage` actually do anything. The cost is the language
74
- // bundle — but since we load it lazily on first EditorShell mount,
75
- // it stays out of the resolver graph for consumers that only
76
- // import types or `JsonEditor` from this package.
77
- // `editor.main.js` ships without a sibling `.d.ts` (only
78
- // `editor.api.d.ts` is published), so the subpath import has no
79
- // resolvable declaration. The `declare module` shim in
80
- // `src/types/monaco-shims.d.ts` makes it import as `any`; the
81
- // `as unknown as` cast below pins the surface to the full namespace.
82
- // At runtime `main` re-exports the entire `editor.api` surface
83
- // alongside the language contributions, so the cast is sound.
60
+ // `./monaco` re-exports `editor.api` (the only barrel with a `.d.ts`)
61
+ // after side-effect-importing `editor.main.js`; the `as unknown as` cast
62
+ // pins the settled module to the full monaco namespace.
84
63
  monacoPromise = (
85
- import('monaco-editor/esm/vs/editor/editor.main.js') as unknown as Promise<
86
- typeof import('monaco-editor')
87
- >
64
+ import('./monaco.js') as unknown as Promise<typeof import('monaco-editor')>
88
65
  ).then((m) => {
89
66
  loader.config({ monaco: m });
90
67
  monacoNamespace = m;
@@ -0,0 +1,113 @@
1
+ import type { MediaProvider } from '@bendyline/squisq/schemas';
2
+ import type { Node as ProseMirrorNode } from '@tiptap/pm/model';
3
+ import type { EditorView as ProseMirrorView } from '@tiptap/pm/view';
4
+
5
+ type ImageMutationView = Pick<ProseMirrorView, 'state' | 'dispatch'>;
6
+
7
+ /**
8
+ * Upload images while stable placeholder nodes retain the original event
9
+ * position as the surrounding ProseMirror document continues to change.
10
+ */
11
+ export async function uploadAndInsertImages(
12
+ view: ImageMutationView,
13
+ files: File[],
14
+ mediaProvider: MediaProvider,
15
+ onMediaUploaded?: () => void,
16
+ ): Promise<void> {
17
+ const placeholders = files.map(() => `squisq-upload:${uniquePasteToken()}`);
18
+ const imageType = view.state.schema.nodes.image;
19
+ if (!imageType) return;
20
+
21
+ let placeholderTr = view.state.tr;
22
+ for (const token of placeholders) {
23
+ placeholderTr = placeholderTr.replaceSelectionWith(
24
+ imageType.create({
25
+ src: 'data:image/gif;base64,R0lGODlhAQABAAAAACw=',
26
+ alt: 'Uploading image',
27
+ title: token,
28
+ }),
29
+ );
30
+ }
31
+ view.dispatch(placeholderTr);
32
+
33
+ for (let index = 0; index < files.length; index++) {
34
+ const file = files[index];
35
+ const token = placeholders[index];
36
+ try {
37
+ const buffer = await file.arrayBuffer();
38
+ const mimeType = file.type || 'image/png';
39
+ const name =
40
+ file.name && file.name !== 'image.png'
41
+ ? file.name
42
+ : `pasted-${uniquePasteToken()}.${extFromMime(mimeType)}`;
43
+ const relativePath = await mediaProvider.addMedia(name, buffer, mimeType);
44
+ const altText = name.replace(/\.[^.]+$/, '').replace(/[-_]/g, ' ');
45
+ replaceUploadPlaceholder(view, token, relativePath, altText);
46
+ onMediaUploaded?.();
47
+ } catch (err) {
48
+ removeUploadPlaceholder(view, token);
49
+ console.error('Failed to upload dropped image:', err);
50
+ }
51
+ }
52
+ }
53
+
54
+ function findUploadPlaceholder(
55
+ view: ImageMutationView,
56
+ token: string,
57
+ ): { pos: number; node: ProseMirrorNode } | null {
58
+ let found: { pos: number; node: ProseMirrorNode } | null = null;
59
+ view.state.doc.descendants((node, pos) => {
60
+ if (node.type === view.state.schema.nodes.image && node.attrs.title === token) {
61
+ found = { pos, node };
62
+ return false;
63
+ }
64
+ return undefined;
65
+ });
66
+ return found;
67
+ }
68
+
69
+ function replaceUploadPlaceholder(
70
+ view: ImageMutationView,
71
+ token: string,
72
+ src: string,
73
+ alt: string,
74
+ ): void {
75
+ const found = findUploadPlaceholder(view, token);
76
+ if (!found) return;
77
+ view.dispatch(
78
+ view.state.tr.setNodeMarkup(found.pos, undefined, {
79
+ ...found.node.attrs,
80
+ src,
81
+ alt,
82
+ title: null,
83
+ }),
84
+ );
85
+ }
86
+
87
+ function removeUploadPlaceholder(view: ImageMutationView, token: string): void {
88
+ const found = findUploadPlaceholder(view, token);
89
+ if (!found) return;
90
+ view.dispatch(view.state.tr.delete(found.pos, found.pos + found.node.nodeSize));
91
+ }
92
+
93
+ let pasteCounter = 0;
94
+ function uniquePasteToken(): string {
95
+ if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
96
+ return crypto.randomUUID();
97
+ }
98
+ pasteCounter = (pasteCounter + 1) % 1_000_000;
99
+ return `${Date.now()}-${pasteCounter.toString(36)}`;
100
+ }
101
+
102
+ function extFromMime(mime: string): string {
103
+ const map: Record<string, string> = {
104
+ 'image/png': 'png',
105
+ 'image/jpeg': 'jpg',
106
+ 'image/jpg': 'jpg',
107
+ 'image/gif': 'gif',
108
+ 'image/webp': 'webp',
109
+ 'image/svg+xml': 'svg',
110
+ 'image/avif': 'avif',
111
+ };
112
+ return map[mime.toLowerCase()] ?? 'png';
113
+ }