@bendyline/squisq-editor-react 1.6.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -3
- package/dist/index.d.ts +1304 -208
- package/dist/index.js +22838 -14494
- package/dist/index.js.map +1 -1
- package/dist/monaco.d.ts +1 -0
- package/dist/monaco.js +4 -0
- package/dist/monaco.js.map +1 -0
- package/dist/styles/index.css +2384 -408
- package/package.json +9 -5
- package/src/BlockPropertiesPopover.tsx +48 -17
- package/src/DocumentSettingsDialog.tsx +13 -21
- package/src/EditorContext.tsx +199 -42
- package/src/EditorShell.tsx +403 -270
- package/src/ImageEditor.tsx +22 -1
- package/src/InlinePreviewGutter.tsx +16 -8
- package/src/MediaBin.tsx +276 -40
- package/src/OutlinePanel.tsx +202 -7
- package/src/PlainHtmlPreview.tsx +40 -1
- package/src/PreviewControls.tsx +466 -93
- package/src/PreviewPanel.tsx +313 -77
- package/src/RawEditor.tsx +13 -2
- package/src/StatusBar.tsx +5 -2
- package/src/TemplateAnnotation.ts +34 -10
- package/src/TemplateContentPreview.tsx +56 -0
- package/src/TemplatePicker.tsx +355 -134
- package/src/ThemeCustomizerPanel.tsx +30 -15
- package/src/ThemePicker.tsx +10 -5
- package/src/TimelineBlockPreview.tsx +1 -1
- package/src/Toolbar.tsx +719 -238
- package/src/TransitionPicker.tsx +44 -5
- package/src/ViewMenuPanel.tsx +12 -14
- package/src/ViewSwitcher.tsx +4 -4
- package/src/WysiwygEditor.tsx +167 -92
- package/src/__tests__/blockPropertiesPopoverTheme.test.tsx +33 -0
- package/src/__tests__/blockTagActivity.test.ts +183 -0
- package/src/__tests__/buildDocumentPreviewMarkdown.test.ts +75 -0
- package/src/__tests__/buildPreviewDocContent.test.ts +48 -0
- package/src/__tests__/buildPreviewDocTransition.test.ts +22 -3
- package/src/__tests__/canvasSurfaceTextEditing.test.tsx +60 -0
- package/src/__tests__/customLayoutManagerTheme.test.tsx +34 -0
- package/src/__tests__/documentSettingsDialog.test.tsx +29 -1
- package/src/__tests__/editorScrollHandoff.test.tsx +102 -0
- package/src/__tests__/editorScrollSync.test.ts +65 -0
- package/src/__tests__/editorShellProps.test.tsx +390 -1
- package/src/__tests__/emojiPicker.test.tsx +2 -42
- package/src/__tests__/headingTransition.test.ts +53 -15
- package/src/__tests__/imageEditAffordance.test.tsx +5 -1
- package/src/__tests__/imageEditModalContract.test.tsx +119 -0
- package/src/__tests__/imageEditorShell.test.tsx +38 -1
- package/src/__tests__/jsonEditor.test.tsx +56 -1
- package/src/__tests__/layersPanel.test.tsx +75 -5
- package/src/__tests__/majorApiTypes.test.ts +26 -0
- package/src/__tests__/mediaBinDrop.test.tsx +90 -0
- package/src/__tests__/mediaReferences.test.ts +82 -0
- package/src/__tests__/narrationSave.test.ts +148 -0
- package/src/__tests__/outlinePanel.test.tsx +93 -3
- package/src/__tests__/outlineSource.test.ts +217 -0
- package/src/__tests__/plainHtmlPreview.test.tsx +17 -1
- package/src/__tests__/presentationMode.test.tsx +290 -0
- package/src/__tests__/previewControls.test.tsx +312 -6
- package/src/__tests__/previewPanelAudioFailure.test.tsx +46 -0
- package/src/__tests__/previewPanelPresentation.test.tsx +126 -0
- package/src/__tests__/previewPanelTransforms.test.tsx +117 -0
- package/src/__tests__/rawEditorModelIsolation.test.ts +19 -0
- package/src/__tests__/resolveBlockVisual.test.ts +43 -0
- package/src/__tests__/statusBar.test.tsx +27 -0
- package/src/__tests__/teleprompter.test.tsx +190 -0
- package/src/__tests__/templateAnnotationRoundTrip.test.ts +49 -2
- package/src/__tests__/templateContentPreview.test.ts +101 -0
- package/src/__tests__/templatePickerPortal.test.tsx +62 -0
- package/src/__tests__/tiptapBridge.test.ts +70 -0
- package/src/__tests__/tiptapCodeBlockRoundTrip.test.ts +109 -0
- package/src/__tests__/transformStyleId.test.ts +13 -0
- package/src/__tests__/useImageEditor.test.tsx +67 -0
- package/src/__tests__/useMediaRecorder.test.ts +75 -0
- package/src/__tests__/viewMenuPanel.test.tsx +130 -0
- package/src/__tests__/wysiwygImageUpload.test.ts +69 -0
- package/src/__tests__/wysiwygTemplateBadgeFocus.test.tsx +56 -0
- package/src/asciiDiagram/AsciiDiagramExtension.ts +343 -0
- package/src/asciiDiagram/AsciiDiagramWidget.tsx +224 -0
- package/src/asciiDiagram/RepairableDiagramExtension.ts +200 -0
- package/src/asciiDiagram/__tests__/AsciiDiagramExtension.test.ts +145 -0
- package/src/asciiDiagram/__tests__/RepairableDiagramExtension.test.ts +159 -0
- package/src/asciiDiagram/__tests__/asciiDiagramCommands.test.ts +227 -0
- package/src/asciiDiagram/__tests__/asciiDiagramOps.test.ts +114 -0
- package/src/asciiDiagram/__tests__/asciiPaste.test.ts +43 -0
- package/src/asciiDiagram/asciiDiagramCommands.ts +148 -0
- package/src/asciiDiagram/asciiDiagramData.ts +116 -0
- package/src/asciiDiagram/asciiDiagramOps.ts +219 -0
- package/src/asciiDiagram/asciiPaste.ts +19 -0
- package/src/blockTagActivity.ts +233 -0
- package/src/buildDocumentPreviewMarkdown.ts +168 -0
- package/src/buildPreviewDoc.ts +19 -12
- package/src/customTemplates/CustomLayoutManager.tsx +3 -2
- package/src/customTemplates/CustomTemplateContext.tsx +6 -0
- package/src/customTemplates/TemplateDesigner.tsx +7 -4
- package/src/customTemplates/__tests__/library.test.ts +8 -0
- package/src/customTemplates/designer.css +158 -99
- package/src/customTemplates/library.ts +16 -3
- package/src/customThemes/CustomThemeContext.tsx +6 -0
- package/src/customThemes/CustomThemeDialog.tsx +35 -5
- package/src/customThemes/ImportThemeSection.tsx +178 -0
- package/src/customThemes/__tests__/customThemeLibrary.test.ts +9 -0
- package/src/customThemes/__tests__/importThemeSection.test.tsx +192 -0
- package/src/customThemes/customThemeLibrary.ts +7 -4
- package/src/customThemes/index.ts +7 -1
- package/src/customThemes/themeDraft.ts +23 -7
- package/src/diagram/DiagramCanvas.tsx +9 -4
- package/src/diagram/types.ts +35 -0
- package/src/editorScrollSync.ts +73 -0
- package/src/emojiData.ts +3 -23
- package/src/headingTransition.ts +85 -25
- package/src/imageEditor/CanvasSurface.tsx +29 -23
- package/src/imageEditor/LayersPanel.tsx +78 -3
- package/src/imageEditor/Toolbar.tsx +5 -1
- package/src/imageEditor/icons.tsx +4 -0
- package/src/imageEditor/image-editor.css +75 -0
- package/src/imageEditor/layers/SelectionHandles.tsx +1 -1
- package/src/imageEditor/useImageEditor.ts +71 -14
- package/src/index.ts +193 -20
- package/src/jsonEditor/JsonEditorContext.tsx +12 -6
- package/src/jsonEditor/RenderNode.tsx +24 -3
- package/src/jsonEditor/editors.tsx +86 -19
- package/src/mediaEntries.ts +12 -0
- package/src/mediaReferences.ts +299 -0
- package/src/monaco.ts +35 -0
- package/src/monacoWorkers.ts +89 -0
- package/src/outlineSource.ts +171 -0
- package/src/presentation/PresentationMode.tsx +596 -0
- package/src/rawEditorIsolation.ts +18 -0
- package/src/recorder/hooks/useMediaRecorder.ts +97 -53
- package/src/resolveBlockVisual.ts +10 -4
- package/src/scene/Scene.tsx +101 -25
- package/src/scene/SceneBlockExtension.ts +17 -10
- package/src/scene/SceneBlockWidget.tsx +11 -4
- package/src/scene/SceneSelection.tsx +19 -15
- package/src/scene/SceneSideToolbar.tsx +89 -0
- package/src/scene/SceneViewport.tsx +7 -3
- package/src/scene/ShapePalette.tsx +17 -2
- package/src/scene/__tests__/DiagramAdapter.test.ts +86 -0
- package/src/scene/__tests__/SceneBlockExtension.test.ts +33 -0
- package/src/scene/__tests__/sceneIsolation.test.tsx +93 -0
- package/src/scene/adapters/DiagramAdapter.ts +12 -101
- package/src/scene/commands/SceneCommand.ts +4 -1
- package/src/scene/index.ts +1 -6
- package/src/scene/layers/DiagramEdges.tsx +24 -9
- package/src/scene/layers/edgeGeometry.ts +31 -4
- package/src/scene/layers/nodeCard.tsx +27 -8
- package/src/scene/scene.css +145 -2
- package/src/scene/text/SceneTextOverlay.tsx +5 -5
- package/src/scene/text/sceneTextChannel.ts +26 -20
- package/src/scene/text/sceneTextConfig.ts +4 -0
- package/src/scene/tools/ConnectTool.ts +126 -28
- package/src/scene/tools/DrawingConnectTool.ts +86 -16
- package/src/scene/tools/SceneTool.ts +10 -0
- package/src/scene/tools/SelectTool.ts +34 -18
- package/src/scene/tools/ShapeTool.ts +2 -3
- package/src/styles/ascii-diagram.css +79 -0
- package/src/styles/ascii-timeline.css +499 -0
- package/src/styles/editor.css +1566 -219
- package/src/styles/index.css +3 -0
- package/src/styles/tree-view.css +139 -0
- package/src/teleprompter/TeleprompterControls.tsx +218 -0
- package/src/teleprompter/TeleprompterSelfView.tsx +22 -0
- package/src/teleprompter/TeleprompterSurface.tsx +267 -0
- package/src/teleprompter/TeleprompterView.tsx +338 -0
- package/src/teleprompter/canvasRenderer.ts +161 -0
- package/src/teleprompter/floatingWindow.ts +352 -0
- package/src/teleprompter/index.ts +61 -0
- package/src/teleprompter/pcmWorklet.ts +62 -0
- package/src/teleprompter/recording/insertPreamble.ts +80 -0
- package/src/teleprompter/recording/narrationSave.ts +134 -0
- package/src/teleprompter/recording/useNarrationRecorder.ts +367 -0
- package/src/teleprompter/scrollModel.ts +85 -0
- package/src/teleprompter/teleprompterTheme.ts +303 -0
- package/src/teleprompter/types.ts +38 -0
- package/src/teleprompter/useFloatingWindow.ts +74 -0
- package/src/teleprompter/useMicAnalysis.ts +211 -0
- package/src/teleprompter/useTeleprompter.ts +421 -0
- package/src/templateContentPreviewResolver.ts +360 -0
- package/src/timeline/TimelineEditorWidget.tsx +702 -0
- package/src/timeline/TimelineViewExtension.ts +252 -0
- package/src/timeline/__tests__/TimelineEditorWidget.test.tsx +355 -0
- package/src/timeline/__tests__/TimelineViewExtension.test.ts +163 -0
- package/src/timeline/__tests__/timelineCommands.test.ts +327 -0
- package/src/timeline/__tests__/timelineOps.test.ts +222 -0
- package/src/timeline/__tests__/timelinePaste.test.ts +34 -0
- package/src/timeline/timelineCommands.ts +367 -0
- package/src/timeline/timelineData.ts +54 -0
- package/src/timeline/timelineOps.ts +277 -0
- package/src/timeline/timelinePaste.ts +8 -0
- package/src/tiptapBridge.ts +84 -49
- package/src/transformStyleId.ts +17 -0
- package/src/treeview/TreeOutlineWidget.tsx +240 -0
- package/src/treeview/TreeViewExtension.ts +250 -0
- package/src/treeview/__tests__/TreeViewExtension.test.ts +122 -0
- package/src/treeview/__tests__/treeOps.test.ts +125 -0
- package/src/treeview/__tests__/treePaste.test.ts +40 -0
- package/src/treeview/__tests__/treeViewCommands.test.ts +163 -0
- package/src/treeview/treeOps.ts +172 -0
- package/src/treeview/treePaste.ts +13 -0
- package/src/treeview/treeViewCommands.ts +94 -0
- package/src/treeview/treeViewData.ts +41 -0
- package/src/useMonacoLoader.ts +11 -34
- package/src/wysiwygImageUpload.ts +113 -0
- package/src/diagram/DiagramExtension.ts +0 -209
- package/src/diagram/DiagramWidget.tsx +0 -270
- package/src/diagram/useDiagramData.ts +0 -126
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { parseTree, renderTree, type Tree, type TreeNode } from '@bendyline/squisq/doc';
|
|
3
|
+
import {
|
|
4
|
+
addItemOp,
|
|
5
|
+
indentItemOp,
|
|
6
|
+
moveItemDownOp,
|
|
7
|
+
moveItemUpOp,
|
|
8
|
+
outdentItemOp,
|
|
9
|
+
removeItemOp,
|
|
10
|
+
renameItemOp,
|
|
11
|
+
sanitizeTreeLabel,
|
|
12
|
+
toggleDirOp,
|
|
13
|
+
} from '../treeOps';
|
|
14
|
+
|
|
15
|
+
function tree(art: string): Tree {
|
|
16
|
+
return parseTree(art);
|
|
17
|
+
}
|
|
18
|
+
function flat(t: Tree): string[] {
|
|
19
|
+
const out: string[] = [];
|
|
20
|
+
const walk = (ns: TreeNode[], d: number) => {
|
|
21
|
+
for (const n of ns) {
|
|
22
|
+
out.push(`${d}:${n.label}`);
|
|
23
|
+
walk(n.children, d + 1);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
walk(t.roots, 0);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const BASE = ['root/', '├── a', '├── b/', '│ └── b1', '└── c'].join('\n');
|
|
31
|
+
|
|
32
|
+
describe('sanitizeTreeLabel', () => {
|
|
33
|
+
it('strips connector glyphs and newlines', () => {
|
|
34
|
+
expect(sanitizeTreeLabel('a ├── b │ c\nd')).toBe('a b c d');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('treeOps', () => {
|
|
39
|
+
it('renameItemOp renames without mutating the input', () => {
|
|
40
|
+
const t = tree(BASE);
|
|
41
|
+
const next = renameItemOp(t, 'a', 'alpha');
|
|
42
|
+
expect(flat(next)).toContain('1:alpha');
|
|
43
|
+
expect(flat(t)).toContain('1:a'); // original untouched
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('addItemOp adds a child and a sibling', () => {
|
|
47
|
+
const child = addItemOp(tree(BASE), 'a', 'child', 'a1');
|
|
48
|
+
const aNode = child.roots[0].children.find((n) => n.label === 'a');
|
|
49
|
+
expect(aNode?.children.map((c) => c.label)).toEqual(['a1']);
|
|
50
|
+
|
|
51
|
+
const sib = addItemOp(tree(BASE), 'a', 'siblingAfter', 'a2');
|
|
52
|
+
expect(sib.roots[0].children.map((c) => c.label)).toEqual(['a', 'a2', 'b/', 'c']);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('addItemOp with isDir adds a trailing-slash folder with a folder default name', () => {
|
|
56
|
+
// Explicit label gets a slash.
|
|
57
|
+
const named = addItemOp(tree(BASE), 'a', 'siblingAfter', 'assets', true);
|
|
58
|
+
const assets = named.roots[0].children.find((n) => n.label === 'assets/');
|
|
59
|
+
expect(assets?.isDir).toBe(true);
|
|
60
|
+
|
|
61
|
+
// Default name is a fresh `folderN/`.
|
|
62
|
+
const auto = addItemOp(tree(BASE), 'a', 'siblingAfter', undefined, true);
|
|
63
|
+
const folder = auto.roots[0].children.find((n) => n.label.startsWith('folder'));
|
|
64
|
+
expect(folder?.label).toBe('folder1/');
|
|
65
|
+
expect(folder?.isDir).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('removeItemOp drops a node and its subtree', () => {
|
|
69
|
+
const next = removeItemOp(tree(BASE), 'b');
|
|
70
|
+
expect(flat(next)).toEqual(['0:root/', '1:a', '1:c']);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('indentItemOp re-parents under the preceding sibling', () => {
|
|
74
|
+
const next = indentItemOp(tree(BASE), 'c'); // c indents under b/
|
|
75
|
+
expect(flat(next)).toEqual(['0:root/', '1:a', '1:b/', '2:b1', '2:c']);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('indentItemOp is a no-op for the first sibling', () => {
|
|
79
|
+
const t = tree(BASE);
|
|
80
|
+
expect(indentItemOp(t, 'a')).toBe(t);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('outdentItemOp promotes a node to a sibling of its parent (subtree carried)', () => {
|
|
84
|
+
const next = outdentItemOp(tree(BASE), 'b1'); // b1 outdents next to b/
|
|
85
|
+
expect(flat(next)).toEqual(['0:root/', '1:a', '1:b/', '1:b1', '1:c']);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('outdentItemOp is a no-op for a root', () => {
|
|
89
|
+
const t = tree(BASE);
|
|
90
|
+
expect(outdentItemOp(t, 'root')).toBe(t);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('moveItemUp / moveItemDown reorder siblings within bounds', () => {
|
|
94
|
+
expect(flat(moveItemDownOp(tree(BASE), 'a'))).toEqual([
|
|
95
|
+
'0:root/',
|
|
96
|
+
'1:b/',
|
|
97
|
+
'2:b1',
|
|
98
|
+
'1:a',
|
|
99
|
+
'1:c',
|
|
100
|
+
]);
|
|
101
|
+
const t = tree(BASE);
|
|
102
|
+
expect(moveItemUpOp(t, 'a')).toBe(t); // already first
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('toggleDirOp toggles the trailing slash', () => {
|
|
106
|
+
const on = toggleDirOp(tree(BASE), 'a'); // a → a/
|
|
107
|
+
expect(on.roots[0].children[0].label).toBe('a/');
|
|
108
|
+
const off = toggleDirOp(on, 'a'); // a/ → a
|
|
109
|
+
expect(off.roots[0].children.find((n) => n.label === 'a')).toBeDefined();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('every op keeps the tree renderable + byte-stable', () => {
|
|
113
|
+
for (const op of [
|
|
114
|
+
(t: Tree) => renameItemOp(t, 'a', 'alpha'),
|
|
115
|
+
(t: Tree) => addItemOp(t, 'b', 'child', 'b2'),
|
|
116
|
+
(t: Tree) => indentItemOp(t, 'c'),
|
|
117
|
+
(t: Tree) => outdentItemOp(t, 'b1'),
|
|
118
|
+
(t: Tree) => moveItemDownOp(t, 'a'),
|
|
119
|
+
]) {
|
|
120
|
+
const next = op(tree(BASE));
|
|
121
|
+
const r1 = renderTree(next);
|
|
122
|
+
expect(renderTree(parseTree(r1))).toBe(r1);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { shouldPasteAsTreeFence } from '../treePaste';
|
|
3
|
+
|
|
4
|
+
const FILE_TREE = ['src/', '├── index.ts', '├── utils/', '│ └── math.ts', '└── config.ts'].join(
|
|
5
|
+
'\n',
|
|
6
|
+
);
|
|
7
|
+
const ASCII_TREE = ['root', '|-- a', '|-- b', '`-- c'].join('\n');
|
|
8
|
+
const BOX_DIAGRAM = [
|
|
9
|
+
'┌────────┐',
|
|
10
|
+
'│ Alpha │',
|
|
11
|
+
'└───┬────┘',
|
|
12
|
+
' ▼',
|
|
13
|
+
'┌────────┐',
|
|
14
|
+
'│ Beta │',
|
|
15
|
+
'└────────┘',
|
|
16
|
+
].join('\n');
|
|
17
|
+
|
|
18
|
+
describe('shouldPasteAsTreeFence', () => {
|
|
19
|
+
it('accepts bare Unicode file-tree art', () => {
|
|
20
|
+
expect(shouldPasteAsTreeFence(FILE_TREE)).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('accepts bare ASCII tree art', () => {
|
|
24
|
+
expect(shouldPasteAsTreeFence(ASCII_TREE)).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('rejects a box diagram (belongs to the diagram gate)', () => {
|
|
28
|
+
expect(shouldPasteAsTreeFence(BOX_DIAGRAM)).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('rejects a GFM table', () => {
|
|
32
|
+
expect(shouldPasteAsTreeFence(['| a | b |', '|---|---|', '| 1 | 2 |'].join('\n'))).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('rejects text already containing a fence, and prose', () => {
|
|
36
|
+
expect(shouldPasteAsTreeFence('```\n' + FILE_TREE + '\n```')).toBe(false);
|
|
37
|
+
expect(shouldPasteAsTreeFence('just a normal paragraph of text')).toBe(false);
|
|
38
|
+
expect(shouldPasteAsTreeFence('')).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command pipeline through a REAL tiptap editor: fence text in →
|
|
3
|
+
* applyTreeCommand → fence text out, verified by re-parsing.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Editor } from '@tiptap/core';
|
|
7
|
+
import StarterKit from '@tiptap/starter-kit';
|
|
8
|
+
import { afterEach, beforeAll, describe, expect, it } from 'vitest';
|
|
9
|
+
import { parseTree } from '@bendyline/squisq/doc';
|
|
10
|
+
import { markdownToTiptap } from '../../tiptapBridge';
|
|
11
|
+
import { HeadingWithTemplate } from '../../TemplateAnnotation';
|
|
12
|
+
import { TreeViewExtension, TREEVIEW_KEY } from '../TreeViewExtension';
|
|
13
|
+
import { applyTreeCommand } from '../treeViewCommands';
|
|
14
|
+
|
|
15
|
+
beforeAll(() => {
|
|
16
|
+
if (typeof globalThis.ResizeObserver === 'undefined') {
|
|
17
|
+
class RO {
|
|
18
|
+
observe(): void {}
|
|
19
|
+
unobserve(): void {}
|
|
20
|
+
disconnect(): void {}
|
|
21
|
+
}
|
|
22
|
+
globalThis.ResizeObserver = RO as unknown as typeof ResizeObserver;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const ART = ['src/', '├── index.ts', '├── utils/', '│ └── math.ts', '└── config.ts'].join('\n');
|
|
27
|
+
|
|
28
|
+
let editors: Editor[] = [];
|
|
29
|
+
function makeEditor(md: string): Editor {
|
|
30
|
+
const editor = new Editor({
|
|
31
|
+
extensions: [
|
|
32
|
+
StarterKit.configure({
|
|
33
|
+
heading: false,
|
|
34
|
+
codeBlock: { HTMLAttributes: { class: 'squisq-code-block' } },
|
|
35
|
+
}),
|
|
36
|
+
HeadingWithTemplate.configure({ levels: [1, 2, 3, 4, 5, 6] }),
|
|
37
|
+
TreeViewExtension,
|
|
38
|
+
],
|
|
39
|
+
content: markdownToTiptap(md),
|
|
40
|
+
});
|
|
41
|
+
editors.push(editor);
|
|
42
|
+
return editor;
|
|
43
|
+
}
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
for (const e of editors) e.destroy();
|
|
46
|
+
editors = [];
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
function firstId(editor: Editor): string {
|
|
50
|
+
const s = TREEVIEW_KEY.getState(editor.state);
|
|
51
|
+
expect(s?.entries.length).toBeGreaterThan(0);
|
|
52
|
+
return s?.entries[0].id as string;
|
|
53
|
+
}
|
|
54
|
+
function fenceOf(editor: Editor): { text: string; language: string | null } {
|
|
55
|
+
let text = '';
|
|
56
|
+
let language: string | null = null;
|
|
57
|
+
editor.state.doc.descendants((node) => {
|
|
58
|
+
if (node.type.name === 'codeBlock' && text === '') {
|
|
59
|
+
text = node.textContent;
|
|
60
|
+
language = (node.attrs as { language?: string | null }).language ?? null;
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
});
|
|
65
|
+
return { text, language };
|
|
66
|
+
}
|
|
67
|
+
function labels(editor: Editor): string[] {
|
|
68
|
+
const out: string[] = [];
|
|
69
|
+
const walk = (ns: ReturnType<typeof parseTree>['roots']) =>
|
|
70
|
+
ns.forEach((n) => (out.push(n.label), walk(n.children)));
|
|
71
|
+
walk(parseTree(fenceOf(editor).text).roots);
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
describe('applyTreeCommand', () => {
|
|
76
|
+
it('renameItem rewrites the fence', () => {
|
|
77
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
78
|
+
const id = firstId(editor);
|
|
79
|
+
expect(
|
|
80
|
+
applyTreeCommand(editor, id, { kind: 'renameItem', id: 'index-ts', label: 'main.ts' }),
|
|
81
|
+
).toBe(true);
|
|
82
|
+
expect(labels(editor)).toContain('main.ts');
|
|
83
|
+
expect(labels(editor)).not.toContain('index.ts');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('addItem (child) grows the tree', () => {
|
|
87
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
88
|
+
const id = firstId(editor);
|
|
89
|
+
expect(
|
|
90
|
+
applyTreeCommand(editor, id, {
|
|
91
|
+
kind: 'addItem',
|
|
92
|
+
targetId: 'utils',
|
|
93
|
+
position: 'child',
|
|
94
|
+
label: 'io.ts',
|
|
95
|
+
}),
|
|
96
|
+
).toBe(true);
|
|
97
|
+
const utils = parseTree(fenceOf(editor).text).roots[0].children.find(
|
|
98
|
+
(n) => n.label === 'utils/',
|
|
99
|
+
);
|
|
100
|
+
expect(utils?.children.map((c) => c.label)).toContain('io.ts');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('indentItem / outdentItem restructure', () => {
|
|
104
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
105
|
+
const id = firstId(editor);
|
|
106
|
+
// config.ts indents under utils/
|
|
107
|
+
expect(applyTreeCommand(editor, id, { kind: 'indentItem', id: 'config-ts' })).toBe(true);
|
|
108
|
+
const utils = parseTree(fenceOf(editor).text).roots[0].children.find(
|
|
109
|
+
(n) => n.label === 'utils/',
|
|
110
|
+
);
|
|
111
|
+
expect(utils?.children.map((c) => c.label)).toContain('config.ts');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('removeItem drops a node', () => {
|
|
115
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
116
|
+
const id = firstId(editor);
|
|
117
|
+
expect(applyTreeCommand(editor, id, { kind: 'removeItem', id: 'config-ts' })).toBe(true);
|
|
118
|
+
expect(labels(editor)).not.toContain('config.ts');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('promotes the fence language to the explicit `tree` tag on edit', () => {
|
|
122
|
+
// A once-edited tree should carry `language: 'tree'` so its identity
|
|
123
|
+
// survives a later flatten → markdown → re-import round-trip.
|
|
124
|
+
const editor = makeEditor('```text\n' + ART + '\n```\n');
|
|
125
|
+
const id = firstId(editor);
|
|
126
|
+
expect(fenceOf(editor).language).toBe('text');
|
|
127
|
+
applyTreeCommand(editor, id, { kind: 'renameItem', id: 'index-ts', label: 'app.ts' });
|
|
128
|
+
expect(fenceOf(editor).language).toBe('tree');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('keeps the block id stable across the language-promotion rewrite', () => {
|
|
132
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
133
|
+
const id = firstId(editor);
|
|
134
|
+
applyTreeCommand(editor, id, { kind: 'renameItem', id: 'index-ts', label: 'app.ts' });
|
|
135
|
+
expect(firstId(editor)).toBe(id);
|
|
136
|
+
expect(fenceOf(editor).language).toBe('tree');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('one undo restores the original fence bytes', () => {
|
|
140
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
141
|
+
const id = firstId(editor);
|
|
142
|
+
const before = fenceOf(editor).text;
|
|
143
|
+
applyTreeCommand(editor, id, { kind: 'renameItem', id: 'index-ts', label: 'app.ts' });
|
|
144
|
+
expect(fenceOf(editor).text).not.toBe(before);
|
|
145
|
+
editor.commands.undo();
|
|
146
|
+
expect(fenceOf(editor).text).toBe(before);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('no-op commands leave the document untouched', () => {
|
|
150
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
151
|
+
const id = firstId(editor);
|
|
152
|
+
const json = JSON.stringify(editor.state.doc.toJSON());
|
|
153
|
+
expect(applyTreeCommand(editor, id, { kind: 'indentItem', id: 'src' })).toBe(false); // root
|
|
154
|
+
expect(JSON.stringify(editor.state.doc.toJSON())).toBe(json);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('returns false for an unknown block id', () => {
|
|
158
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
159
|
+
expect(applyTreeCommand(editor, 'tree-999', { kind: 'removeItem', id: 'index-ts' })).toBe(
|
|
160
|
+
false,
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
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
|
+
/** Locate a node by id in a (cloned) tree, returning its sibling array + index + parent. */
|
|
45
|
+
function locate(roots: TreeNode[], id: string, parent: TreeNode | null = null): Loc | null {
|
|
46
|
+
for (let i = 0; i < roots.length; i++) {
|
|
47
|
+
if (roots[i].id === id) return { siblings: roots, index: i, node: roots[i], parent };
|
|
48
|
+
const inner = locate(roots[i].children, id, roots[i]);
|
|
49
|
+
if (inner) return inner;
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function markDir(n: TreeNode): void {
|
|
55
|
+
n.isDir = n.label.endsWith('/') || n.children.length > 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function renameItemOp(tree: Tree, id: string, label: string): Tree {
|
|
59
|
+
const clean = sanitizeTreeLabel(label);
|
|
60
|
+
const next = cloneTree(tree);
|
|
61
|
+
const loc = locate(next.roots, id);
|
|
62
|
+
if (!loc || clean.length === 0 || clean === loc.node.label) return tree;
|
|
63
|
+
loc.node.label = clean;
|
|
64
|
+
markDir(loc.node);
|
|
65
|
+
return next;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function addItemOp(
|
|
69
|
+
tree: Tree,
|
|
70
|
+
targetId: string,
|
|
71
|
+
position: 'child' | 'siblingAfter',
|
|
72
|
+
label?: string,
|
|
73
|
+
isDir?: boolean,
|
|
74
|
+
): Tree {
|
|
75
|
+
const next = cloneTree(tree);
|
|
76
|
+
const loc = locate(next.roots, targetId);
|
|
77
|
+
if (!loc) return tree;
|
|
78
|
+
let name = sanitizeTreeLabel(label ?? '') || freshLabel(next, isDir);
|
|
79
|
+
// A folder carries a trailing slash so it re-parses as a directory.
|
|
80
|
+
if (isDir && !name.endsWith('/')) name += '/';
|
|
81
|
+
const node: TreeNode = {
|
|
82
|
+
id: `__new-${name}`,
|
|
83
|
+
label: name,
|
|
84
|
+
children: [],
|
|
85
|
+
...(isDir ? { isDir: true } : {}),
|
|
86
|
+
};
|
|
87
|
+
if (position === 'child') {
|
|
88
|
+
loc.node.children.push(node);
|
|
89
|
+
markDir(loc.node);
|
|
90
|
+
} else {
|
|
91
|
+
loc.siblings.splice(loc.index + 1, 0, node);
|
|
92
|
+
}
|
|
93
|
+
return next;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function removeItemOp(tree: Tree, id: string): Tree {
|
|
97
|
+
const next = cloneTree(tree);
|
|
98
|
+
const loc = locate(next.roots, id);
|
|
99
|
+
if (!loc) return tree;
|
|
100
|
+
loc.siblings.splice(loc.index, 1);
|
|
101
|
+
if (loc.parent) markDir(loc.parent);
|
|
102
|
+
return next;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Node becomes the last child of its immediate preceding sibling. */
|
|
106
|
+
export function indentItemOp(tree: Tree, id: string): Tree {
|
|
107
|
+
const next = cloneTree(tree);
|
|
108
|
+
const loc = locate(next.roots, id);
|
|
109
|
+
if (!loc || loc.index === 0) return tree; // nothing to indent under
|
|
110
|
+
const [moved] = loc.siblings.splice(loc.index, 1);
|
|
111
|
+
const newParent = loc.siblings[loc.index - 1];
|
|
112
|
+
newParent.children.push(moved);
|
|
113
|
+
markDir(newParent);
|
|
114
|
+
return next;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Node becomes a sibling of its parent, inserted directly after it (subtree carried). */
|
|
118
|
+
export function outdentItemOp(tree: Tree, id: string): Tree {
|
|
119
|
+
const next = cloneTree(tree);
|
|
120
|
+
const loc = locate(next.roots, id);
|
|
121
|
+
if (!loc || !loc.parent) return tree; // roots can't outdent
|
|
122
|
+
const grand = locate(next.roots, loc.parent.id);
|
|
123
|
+
if (!grand) return tree;
|
|
124
|
+
const [moved] = loc.siblings.splice(loc.index, 1);
|
|
125
|
+
grand.siblings.splice(grand.index + 1, 0, moved);
|
|
126
|
+
markDir(loc.parent);
|
|
127
|
+
return next;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function moveItemUpOp(tree: Tree, id: string): Tree {
|
|
131
|
+
const next = cloneTree(tree);
|
|
132
|
+
const loc = locate(next.roots, id);
|
|
133
|
+
if (!loc || loc.index === 0) return tree;
|
|
134
|
+
[loc.siblings[loc.index - 1], loc.siblings[loc.index]] = [
|
|
135
|
+
loc.siblings[loc.index],
|
|
136
|
+
loc.siblings[loc.index - 1],
|
|
137
|
+
];
|
|
138
|
+
return next;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function moveItemDownOp(tree: Tree, id: string): Tree {
|
|
142
|
+
const next = cloneTree(tree);
|
|
143
|
+
const loc = locate(next.roots, id);
|
|
144
|
+
if (!loc || loc.index >= loc.siblings.length - 1) return tree;
|
|
145
|
+
[loc.siblings[loc.index], loc.siblings[loc.index + 1]] = [
|
|
146
|
+
loc.siblings[loc.index + 1],
|
|
147
|
+
loc.siblings[loc.index],
|
|
148
|
+
];
|
|
149
|
+
return next;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Toggle the trailing-slash directory marker on a node's label. */
|
|
153
|
+
export function toggleDirOp(tree: Tree, id: string): Tree {
|
|
154
|
+
const next = cloneTree(tree);
|
|
155
|
+
const loc = locate(next.roots, id);
|
|
156
|
+
if (!loc) return tree;
|
|
157
|
+
loc.node.label = loc.node.label.endsWith('/')
|
|
158
|
+
? loc.node.label.slice(0, -1)
|
|
159
|
+
: `${loc.node.label}/`;
|
|
160
|
+
markDir(loc.node);
|
|
161
|
+
return next;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function freshLabel(tree: Tree, isDir?: boolean): string {
|
|
165
|
+
const labels = new Set<string>();
|
|
166
|
+
const walk = (ns: TreeNode[]): void => ns.forEach((n) => (labels.add(n.label), walk(n.children)));
|
|
167
|
+
walk(tree.roots);
|
|
168
|
+
const base = isDir ? 'folder' : 'item';
|
|
169
|
+
let i = 1;
|
|
170
|
+
while (labels.has(`${base}${i}`) || labels.has(`${base}${i}/`)) i++;
|
|
171
|
+
return `${base}${i}`;
|
|
172
|
+
}
|
|
@@ -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,94 @@
|
|
|
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
|
+
moveItemDownOp,
|
|
17
|
+
moveItemUpOp,
|
|
18
|
+
outdentItemOp,
|
|
19
|
+
removeItemOp,
|
|
20
|
+
renameItemOp,
|
|
21
|
+
toggleDirOp,
|
|
22
|
+
} from './treeOps';
|
|
23
|
+
|
|
24
|
+
export type TreeCommand =
|
|
25
|
+
| {
|
|
26
|
+
kind: 'addItem';
|
|
27
|
+
targetId: string;
|
|
28
|
+
position: 'child' | 'siblingAfter';
|
|
29
|
+
label?: string;
|
|
30
|
+
isDir?: boolean;
|
|
31
|
+
}
|
|
32
|
+
| { kind: 'renameItem'; id: string; label: string }
|
|
33
|
+
| { kind: 'indentItem'; id: string }
|
|
34
|
+
| { kind: 'outdentItem'; id: string }
|
|
35
|
+
| { kind: 'moveItemUp'; id: string }
|
|
36
|
+
| { kind: 'moveItemDown'; id: string }
|
|
37
|
+
| { kind: 'removeItem'; id: string }
|
|
38
|
+
| { kind: 'toggleDir'; id: string };
|
|
39
|
+
|
|
40
|
+
/** Re-export so the widget can rewrite raw fence text without a second copy. */
|
|
41
|
+
export { replaceAsciiFenceText as replaceTreeFenceText } from '../asciiDiagram/asciiDiagramCommands';
|
|
42
|
+
|
|
43
|
+
function countNodes(nodes: readonly Tree['roots'][number][]): number {
|
|
44
|
+
let n = 0;
|
|
45
|
+
for (const node of nodes) n += 1 + countNodes(node.children);
|
|
46
|
+
return n;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function applyOp(editor: Editor, blockId: string, op: (t: Tree) => Tree): boolean {
|
|
50
|
+
const pos = findTreeBlockPos(editor, blockId);
|
|
51
|
+
if (pos === null) return false;
|
|
52
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
53
|
+
if (!node || node.type.name !== 'codeBlock') return false;
|
|
54
|
+
const tree = parseTreeForNode(node);
|
|
55
|
+
if (!tree) return false;
|
|
56
|
+
|
|
57
|
+
const next = op(tree);
|
|
58
|
+
if (next === tree) return false;
|
|
59
|
+
const rendered = renderTree(next);
|
|
60
|
+
|
|
61
|
+
// Verify before committing: the rendered art must re-parse to the same
|
|
62
|
+
// node count, or we'd write a tree the outline can't read back.
|
|
63
|
+
if (countNodes(parseTree(rendered).roots) !== countNodes(next.roots)) return false;
|
|
64
|
+
|
|
65
|
+
// Promote the fence language to the explicit `tree` tag so this block's
|
|
66
|
+
// identity survives a later flatten → markdown → re-import round-trip.
|
|
67
|
+
return replaceAsciiFenceText(editor, pos, rendered, 'tree');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function applyTreeCommand(editor: Editor, blockId: string, cmd: TreeCommand): boolean {
|
|
71
|
+
switch (cmd.kind) {
|
|
72
|
+
case 'addItem':
|
|
73
|
+
return applyOp(editor, blockId, (t) =>
|
|
74
|
+
addItemOp(t, cmd.targetId, cmd.position, cmd.label, cmd.isDir),
|
|
75
|
+
);
|
|
76
|
+
case 'renameItem':
|
|
77
|
+
return applyOp(editor, blockId, (t) => renameItemOp(t, cmd.id, cmd.label));
|
|
78
|
+
case 'indentItem':
|
|
79
|
+
return applyOp(editor, blockId, (t) => indentItemOp(t, cmd.id));
|
|
80
|
+
case 'outdentItem':
|
|
81
|
+
return applyOp(editor, blockId, (t) => outdentItemOp(t, cmd.id));
|
|
82
|
+
case 'moveItemUp':
|
|
83
|
+
return applyOp(editor, blockId, (t) => moveItemUpOp(t, cmd.id));
|
|
84
|
+
case 'moveItemDown':
|
|
85
|
+
return applyOp(editor, blockId, (t) => moveItemDownOp(t, cmd.id));
|
|
86
|
+
case 'removeItem':
|
|
87
|
+
return applyOp(editor, blockId, (t) => removeItemOp(t, cmd.id));
|
|
88
|
+
case 'toggleDir':
|
|
89
|
+
return applyOp(editor, blockId, (t) => toggleDirOp(t, cmd.id));
|
|
90
|
+
}
|
|
91
|
+
const _exhaustive: never = cmd;
|
|
92
|
+
void _exhaustive;
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
@@ -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
|
+
}
|