@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.
- package/README.md +12 -3
- package/dist/index.d.ts +1275 -183
- package/dist/index.js +20345 -12777
- 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 +1607 -241
- package/package.json +9 -5
- package/src/BlockPropertiesPopover.tsx +30 -15
- package/src/DocumentSettingsDialog.tsx +39 -33
- package/src/EditorContext.tsx +199 -42
- package/src/EditorShell.tsx +349 -261
- package/src/ImageEditor.tsx +22 -1
- package/src/InlinePreviewGutter.tsx +16 -8
- package/src/MediaBin.tsx +107 -14
- package/src/OutlinePanel.tsx +202 -7
- package/src/PlainHtmlPreview.tsx +40 -1
- package/src/PreviewControls.tsx +362 -89
- package/src/PreviewPanel.tsx +312 -78
- package/src/RawEditor.tsx +13 -2
- package/src/RecorderEntry.tsx +2 -0
- package/src/StatusBar.tsx +5 -2
- package/src/TemplateAnnotation.ts +12 -3
- package/src/TemplatePicker.tsx +75 -21
- package/src/ThemeCustomizerPanel.tsx +8 -0
- package/src/ThemePicker.tsx +10 -5
- package/src/TimelineBlockPreview.tsx +1 -1
- package/src/Toolbar.tsx +357 -51
- package/src/TransitionPicker.tsx +36 -4
- package/src/ViewMenuPanel.tsx +12 -14
- package/src/WysiwygEditor.tsx +124 -91
- 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 +21 -1
- package/src/__tests__/canvasSurfaceTextEditing.test.tsx +60 -0
- package/src/__tests__/codeContextSectionView.test.tsx +8 -6
- package/src/__tests__/customLayoutManagerTheme.test.tsx +34 -0
- package/src/__tests__/documentSettingsDialog.test.tsx +51 -1
- package/src/__tests__/editorScrollHandoff.test.tsx +102 -0
- package/src/__tests__/editorScrollSync.test.ts +65 -0
- package/src/__tests__/editorShellProps.test.tsx +123 -1
- package/src/__tests__/emojiPicker.test.tsx +2 -42
- package/src/__tests__/headingTransition.test.ts +0 -12
- package/src/__tests__/imageEditAffordance.test.tsx +5 -1
- package/src/__tests__/imageEditModalContract.test.tsx +119 -0
- package/src/__tests__/imageEditorShell.test.tsx +15 -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__/mediaAttachmentFlow.test.ts +2 -2
- package/src/__tests__/mediaBinDrop.test.tsx +90 -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 +382 -5
- 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__/recorderTheme.test.tsx +42 -0
- package/src/__tests__/resolveBlockVisual.test.ts +43 -0
- package/src/__tests__/selectionConversions.test.ts +80 -0
- package/src/__tests__/statusBar.test.tsx +27 -0
- package/src/__tests__/teleprompter.test.tsx +190 -0
- package/src/__tests__/templateAnnotationRoundTrip.test.ts +26 -0
- package/src/__tests__/templatePickerPortal.test.tsx +62 -0
- package/src/__tests__/tiptapBridge.test.ts +71 -7
- package/src/__tests__/tiptapCodeBlockRoundTrip.test.ts +109 -0
- package/src/__tests__/tiptapImageRoundTrip.test.ts +1 -1
- package/src/__tests__/toolbarSelectionConversion.test.tsx +164 -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 +254 -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 +284 -0
- package/src/asciiDiagram/__tests__/asciiDiagramOps.test.ts +114 -0
- package/src/asciiDiagram/__tests__/asciiPaste.test.ts +43 -0
- package/src/asciiDiagram/asciiDiagramCommands.ts +174 -0
- package/src/asciiDiagram/asciiDiagramData.ts +149 -0
- package/src/asciiDiagram/asciiDiagramOps.ts +238 -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/codeContext/types.ts +1 -1
- 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/__tests__/useMemoryLayerAdapter.test.ts +13 -0
- package/src/customTemplates/designer.css +158 -99
- package/src/customTemplates/library.ts +16 -3
- package/src/customTemplates/useMemoryLayerAdapter.ts +7 -1
- 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 +25 -6
- package/src/diagram/types.ts +35 -0
- package/src/editorScrollSync.ts +73 -0
- package/src/emojiData.ts +3 -23
- package/src/frontmatterSettings.ts +23 -0
- package/src/headingTransition.ts +0 -15
- 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 +198 -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/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/RecorderButton.tsx +9 -1
- package/src/recorder/RecorderModal.tsx +84 -41
- package/src/recorder/RecorderPanel.tsx +9 -1
- package/src/recorder/hooks/useMediaRecorder.ts +97 -53
- package/src/resolveBlockVisual.ts +10 -4
- package/src/scene/Scene.tsx +48 -18
- package/src/scene/SceneBlockExtension.ts +17 -10
- package/src/scene/SceneBlockWidget.tsx +5 -1
- 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 +119 -0
- package/src/scene/adapters/DiagramAdapter.ts +12 -101
- package/src/scene/adapters/DrawingAdapter.ts +6 -1
- package/src/scene/adapters/LayoutAdapter.ts +3 -0
- package/src/scene/commands/SceneCommand.ts +17 -3
- package/src/scene/index.ts +1 -6
- package/src/scene/layers/DiagramEdges.tsx +21 -7
- package/src/scene/layers/edgeGeometry.ts +9 -1
- package/src/scene/layers/nodeCard.tsx +27 -8
- package/src/scene/scene.css +5 -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 +13 -4
- package/src/scene/tools/SceneTool.ts +8 -0
- package/src/scene/tools/SelectTool.ts +39 -23
- package/src/scene/tools/ShapeTool.ts +2 -3
- package/src/selectionConversions.ts +155 -0
- package/src/styles/ascii-diagram.css +79 -0
- package/src/styles/ascii-timeline.css +596 -0
- package/src/styles/editor.css +736 -120
- package/src/styles/index.css +3 -0
- package/src/styles/tree-view.css +189 -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 +12 -5
- package/src/timeline/TimelineEditorWidget.tsx +861 -0
- package/src/timeline/TimelineViewExtension.ts +252 -0
- package/src/timeline/__tests__/TimelineEditorWidget.test.tsx +414 -0
- package/src/timeline/__tests__/TimelineViewExtension.test.ts +163 -0
- package/src/timeline/__tests__/timelineCommands.test.ts +359 -0
- package/src/timeline/__tests__/timelineOps.test.ts +277 -0
- package/src/timeline/__tests__/timelinePaste.test.ts +34 -0
- package/src/timeline/timelineCommands.ts +421 -0
- package/src/timeline/timelineData.ts +54 -0
- package/src/timeline/timelineOps.ts +381 -0
- package/src/timeline/timelinePaste.ts +8 -0
- package/src/tiptapBridge.ts +47 -21
- package/src/transformStyleId.ts +17 -0
- package/src/treeview/TreeOutlineWidget.tsx +390 -0
- package/src/treeview/TreeViewExtension.ts +250 -0
- package/src/treeview/__tests__/TreeOutlineWidget.test.tsx +156 -0
- package/src/treeview/__tests__/TreeViewExtension.test.ts +122 -0
- package/src/treeview/__tests__/treeOps.test.ts +177 -0
- package/src/treeview/__tests__/treePaste.test.ts +40 -0
- package/src/treeview/__tests__/treeViewCommands.test.ts +179 -0
- package/src/treeview/treeOps.ts +231 -0
- package/src/treeview/treePaste.ts +13 -0
- package/src/treeview/treeViewCommands.ts +99 -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 -272
- package/src/diagram/useDiagramData.ts +0 -126
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Editor } from '@tiptap/core';
|
|
2
|
+
import StarterKit from '@tiptap/starter-kit';
|
|
3
|
+
import { cleanup, createEvent, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
4
|
+
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import { parseTree } from '@bendyline/squisq/doc';
|
|
6
|
+
import { markdownToTiptap } from '../../tiptapBridge';
|
|
7
|
+
import { HeadingWithTemplate } from '../../TemplateAnnotation';
|
|
8
|
+
import { TreeOutlineWidget } from '../TreeOutlineWidget';
|
|
9
|
+
import { TREEVIEW_KEY, TreeViewExtension } from '../TreeViewExtension';
|
|
10
|
+
|
|
11
|
+
const ART = ['src/', '├── index.ts', '├── utils/', '│ └── math.ts', '└── config.ts'].join('\n');
|
|
12
|
+
|
|
13
|
+
const editors: Editor[] = [];
|
|
14
|
+
|
|
15
|
+
beforeAll(() => {
|
|
16
|
+
if (typeof globalThis.ResizeObserver !== 'undefined') return;
|
|
17
|
+
class ResizeObserverStub {
|
|
18
|
+
observe(): void {}
|
|
19
|
+
unobserve(): void {}
|
|
20
|
+
disconnect(): void {}
|
|
21
|
+
}
|
|
22
|
+
globalThis.ResizeObserver = ResizeObserverStub as unknown as typeof ResizeObserver;
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
cleanup();
|
|
27
|
+
for (const editor of editors) editor.destroy();
|
|
28
|
+
editors.length = 0;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
function makeEditor(): Editor {
|
|
32
|
+
const editor = new Editor({
|
|
33
|
+
extensions: [
|
|
34
|
+
StarterKit.configure({
|
|
35
|
+
heading: false,
|
|
36
|
+
codeBlock: { HTMLAttributes: { class: 'squisq-code-block' } },
|
|
37
|
+
}),
|
|
38
|
+
HeadingWithTemplate.configure({ levels: [1, 2, 3, 4, 5, 6] }),
|
|
39
|
+
TreeViewExtension,
|
|
40
|
+
],
|
|
41
|
+
content: markdownToTiptap(`\`\`\`tree\n${ART}\n\`\`\`\n`),
|
|
42
|
+
});
|
|
43
|
+
editors.push(editor);
|
|
44
|
+
return editor;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function blockIdOf(editor: Editor): string {
|
|
48
|
+
const entries = TREEVIEW_KEY.getState(editor.state)?.entries ?? [];
|
|
49
|
+
expect(entries).toHaveLength(1);
|
|
50
|
+
return entries[0].id;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function treeOf(editor: Editor) {
|
|
54
|
+
let text = '';
|
|
55
|
+
editor.state.doc.descendants((node) => {
|
|
56
|
+
if (node.type.name !== 'codeBlock') return true;
|
|
57
|
+
text = node.textContent;
|
|
58
|
+
return false;
|
|
59
|
+
});
|
|
60
|
+
return parseTree(text);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function renderWidget(): Editor {
|
|
64
|
+
const editor = makeEditor();
|
|
65
|
+
render(<TreeOutlineWidget editor={editor} blockId={blockIdOf(editor)} fallbackPos={0} />);
|
|
66
|
+
return editor;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function makeDataTransfer(): DataTransfer {
|
|
70
|
+
const values = new Map<string, string>();
|
|
71
|
+
return {
|
|
72
|
+
effectAllowed: 'uninitialized',
|
|
73
|
+
dropEffect: 'none',
|
|
74
|
+
setData: vi.fn((type: string, value: string) => values.set(type, value)),
|
|
75
|
+
getData: vi.fn((type: string) => values.get(type) ?? ''),
|
|
76
|
+
get types() {
|
|
77
|
+
return [...values.keys()];
|
|
78
|
+
},
|
|
79
|
+
} as unknown as DataTransfer;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function rowFor(label: string): HTMLElement {
|
|
83
|
+
const input = screen.getByDisplayValue(label);
|
|
84
|
+
const row = input.closest<HTMLElement>('.squisq-tree-row');
|
|
85
|
+
expect(row).not.toBeNull();
|
|
86
|
+
return row as HTMLElement;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function setRowRect(row: HTMLElement): void {
|
|
90
|
+
vi.spyOn(row, 'getBoundingClientRect').mockReturnValue({
|
|
91
|
+
x: 0,
|
|
92
|
+
y: 0,
|
|
93
|
+
top: 0,
|
|
94
|
+
right: 300,
|
|
95
|
+
bottom: 30,
|
|
96
|
+
left: 0,
|
|
97
|
+
width: 300,
|
|
98
|
+
height: 30,
|
|
99
|
+
toJSON: () => ({}),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function fireDragAt(
|
|
104
|
+
type: 'dragOver' | 'drop',
|
|
105
|
+
row: HTMLElement,
|
|
106
|
+
transfer: DataTransfer,
|
|
107
|
+
clientY: number,
|
|
108
|
+
): void {
|
|
109
|
+
const event = createEvent[type](row, { dataTransfer: transfer });
|
|
110
|
+
Object.defineProperty(event, 'clientY', { value: clientY });
|
|
111
|
+
fireEvent(row, event);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
describe('TreeOutlineWidget drag and drop', () => {
|
|
115
|
+
it('drops before a row to reorder siblings', async () => {
|
|
116
|
+
const editor = renderWidget();
|
|
117
|
+
const indexRow = rowFor('index.ts');
|
|
118
|
+
const transfer = makeDataTransfer();
|
|
119
|
+
|
|
120
|
+
fireEvent.dragStart(screen.getByTitle('Drag config.ts to move'), { dataTransfer: transfer });
|
|
121
|
+
setRowRect(indexRow);
|
|
122
|
+
expect(transfer.effectAllowed).toBe('move');
|
|
123
|
+
fireDragAt('dragOver', indexRow, transfer, 1);
|
|
124
|
+
expect(indexRow.closest('.squisq-tree-item')?.classList).toContain(
|
|
125
|
+
'squisq-tree-item--drop-before',
|
|
126
|
+
);
|
|
127
|
+
fireDragAt('drop', indexRow, transfer, 1);
|
|
128
|
+
|
|
129
|
+
await waitFor(() =>
|
|
130
|
+
expect(treeOf(editor).roots[0].children.map((node) => node.label)).toEqual([
|
|
131
|
+
'config.ts',
|
|
132
|
+
'index.ts',
|
|
133
|
+
'utils/',
|
|
134
|
+
]),
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('drops onto the middle of a row to indent as its last child', async () => {
|
|
139
|
+
const editor = renderWidget();
|
|
140
|
+
const utilsRow = rowFor('utils/');
|
|
141
|
+
const transfer = makeDataTransfer();
|
|
142
|
+
|
|
143
|
+
fireEvent.dragStart(screen.getByTitle('Drag config.ts to move'), { dataTransfer: transfer });
|
|
144
|
+
setRowRect(utilsRow);
|
|
145
|
+
fireDragAt('dragOver', utilsRow, transfer, 15);
|
|
146
|
+
expect(utilsRow.closest('.squisq-tree-item')?.classList).toContain(
|
|
147
|
+
'squisq-tree-item--drop-child',
|
|
148
|
+
);
|
|
149
|
+
fireDragAt('drop', utilsRow, transfer, 15);
|
|
150
|
+
|
|
151
|
+
await waitFor(() => {
|
|
152
|
+
const utils = treeOf(editor).roots[0].children.find((node) => node.label === 'utils/');
|
|
153
|
+
expect(utils?.children.map((node) => node.label)).toEqual(['math.ts', 'config.ts']);
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registry / decoration behavior of TreeViewExtension + mutual exclusion
|
|
3
|
+
* with the diagram extension.
|
|
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 { markdownToTiptap } from '../../tiptapBridge';
|
|
10
|
+
import { HeadingWithTemplate } from '../../TemplateAnnotation';
|
|
11
|
+
import { TreeViewExtension, TREEVIEW_KEY, findTreeBlockPos } from '../TreeViewExtension';
|
|
12
|
+
import { AsciiDiagramExtension, ASCII_DIAGRAM_KEY } from '../../asciiDiagram/AsciiDiagramExtension';
|
|
13
|
+
import { replaceAsciiFenceText } from '../../asciiDiagram/asciiDiagramCommands';
|
|
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 TREE = ['src/', '├── a.ts', '└── sub/', ' └── b.ts'].join('\n');
|
|
27
|
+
const TREE_B = ['docs/', '├── guide.md', '└── api.md'].join('\n');
|
|
28
|
+
const DIAGRAM = [
|
|
29
|
+
'┌────────┐',
|
|
30
|
+
'│ Alpha │',
|
|
31
|
+
'└───┬────┘',
|
|
32
|
+
' ▼',
|
|
33
|
+
'┌────────┐',
|
|
34
|
+
'│ Beta │',
|
|
35
|
+
'└────────┘',
|
|
36
|
+
].join('\n');
|
|
37
|
+
|
|
38
|
+
let editors: Editor[] = [];
|
|
39
|
+
function makeEditor(md: string, options?: { enabled?: boolean }): Editor {
|
|
40
|
+
const editor = new Editor({
|
|
41
|
+
extensions: [
|
|
42
|
+
StarterKit.configure({
|
|
43
|
+
heading: false,
|
|
44
|
+
codeBlock: { HTMLAttributes: { class: 'squisq-code-block' } },
|
|
45
|
+
}),
|
|
46
|
+
HeadingWithTemplate.configure({ levels: [1, 2, 3, 4, 5, 6] }),
|
|
47
|
+
AsciiDiagramExtension,
|
|
48
|
+
options ? TreeViewExtension.configure(options) : TreeViewExtension,
|
|
49
|
+
],
|
|
50
|
+
content: markdownToTiptap(md),
|
|
51
|
+
});
|
|
52
|
+
editors.push(editor);
|
|
53
|
+
return editor;
|
|
54
|
+
}
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
for (const e of editors) e.destroy();
|
|
57
|
+
editors = [];
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const treeEntries = (e: Editor) => TREEVIEW_KEY.getState(e.state)?.entries ?? [];
|
|
61
|
+
const diagramEntries = (e: Editor) => ASCII_DIAGRAM_KEY.getState(e.state)?.entries ?? [];
|
|
62
|
+
|
|
63
|
+
describe('TreeViewExtension registry', () => {
|
|
64
|
+
it('registers each qualifying tree fence with a distinct id', () => {
|
|
65
|
+
const editor = makeEditor('```\n' + TREE + '\n```\n\nMid.\n\n```\n' + TREE_B + '\n```\n');
|
|
66
|
+
const entries = treeEntries(editor);
|
|
67
|
+
expect(entries).toHaveLength(2);
|
|
68
|
+
expect(entries[0].id).not.toBe(entries[1].id);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('ignores real-language and non-tree fences', () => {
|
|
72
|
+
const editor = makeEditor('```bash\n' + TREE + '\n```\n\n```\nplain code\nno tree\n```\n');
|
|
73
|
+
expect(treeEntries(editor)).toHaveLength(0);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('mutual exclusion: a box diagram gets the diagram widget, not the tree', () => {
|
|
77
|
+
const editor = makeEditor('```\n' + DIAGRAM + '\n```\n');
|
|
78
|
+
expect(treeEntries(editor)).toHaveLength(0);
|
|
79
|
+
expect(diagramEntries(editor)).toHaveLength(1);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('keeps ids stable when content above changes', () => {
|
|
83
|
+
const editor = makeEditor('First.\n\n```\n' + TREE + '\n```\n');
|
|
84
|
+
const [before] = treeEntries(editor);
|
|
85
|
+
editor.commands.insertContentAt(0, '<p>inserted</p>');
|
|
86
|
+
const [after] = treeEntries(editor);
|
|
87
|
+
expect(after.id).toBe(before.id);
|
|
88
|
+
expect(after.pos).toBeGreaterThan(before.pos);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('keeps the id across a self-inflicted fence rewrite', () => {
|
|
92
|
+
const editor = makeEditor('```\n' + TREE + '\n```\n');
|
|
93
|
+
const [before] = treeEntries(editor);
|
|
94
|
+
expect(replaceAsciiFenceText(editor, before.pos, TREE.replace('a.ts', 'main.ts'))).toBe(true);
|
|
95
|
+
const [after] = treeEntries(editor);
|
|
96
|
+
expect(after.id).toBe(before.id);
|
|
97
|
+
expect(findTreeBlockPos(editor, before.id)).toBe(after.pos);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('hysteresis: degraded to a single node still stays interactive', () => {
|
|
101
|
+
const editor = makeEditor('```\n' + TREE + '\n```\n');
|
|
102
|
+
const [entry] = treeEntries(editor);
|
|
103
|
+
replaceAsciiFenceText(editor, entry.pos, 'src/\n└── only.ts');
|
|
104
|
+
const after = treeEntries(editor);
|
|
105
|
+
expect(after).toHaveLength(1);
|
|
106
|
+
expect(after[0].id).toBe(entry.id);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('drops a block whose fence is rewritten into a box diagram', () => {
|
|
110
|
+
const editor = makeEditor('```\n' + TREE + '\n```\n');
|
|
111
|
+
const [entry] = treeEntries(editor);
|
|
112
|
+
replaceAsciiFenceText(editor, entry.pos, DIAGRAM);
|
|
113
|
+
expect(treeEntries(editor)).toHaveLength(0);
|
|
114
|
+
// The diagram extension now owns the fence.
|
|
115
|
+
expect(diagramEntries(editor)).toHaveLength(1);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('enabled: false disables the plugin', () => {
|
|
119
|
+
const editor = makeEditor('```\n' + TREE + '\n```\n', { enabled: false });
|
|
120
|
+
expect(TREEVIEW_KEY.getState(editor.state)).toBeUndefined();
|
|
121
|
+
});
|
|
122
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
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
|
+
moveItemOp,
|
|
7
|
+
moveItemDownOp,
|
|
8
|
+
moveItemUpOp,
|
|
9
|
+
outdentItemOp,
|
|
10
|
+
removeItemOp,
|
|
11
|
+
renameItemOp,
|
|
12
|
+
sanitizeTreeLabel,
|
|
13
|
+
toggleDirOp,
|
|
14
|
+
} from '../treeOps';
|
|
15
|
+
|
|
16
|
+
function tree(art: string): Tree {
|
|
17
|
+
return parseTree(art);
|
|
18
|
+
}
|
|
19
|
+
function flat(t: Tree): string[] {
|
|
20
|
+
const out: string[] = [];
|
|
21
|
+
const walk = (ns: TreeNode[], d: number) => {
|
|
22
|
+
for (const n of ns) {
|
|
23
|
+
out.push(`${d}:${n.label}`);
|
|
24
|
+
walk(n.children, d + 1);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
walk(t.roots, 0);
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const BASE = ['root/', '├── a', '├── b/', '│ └── b1', '└── c'].join('\n');
|
|
32
|
+
|
|
33
|
+
describe('sanitizeTreeLabel', () => {
|
|
34
|
+
it('strips connector glyphs and newlines', () => {
|
|
35
|
+
expect(sanitizeTreeLabel('a ├── b │ c\nd')).toBe('a b c d');
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('treeOps', () => {
|
|
40
|
+
it('renameItemOp renames without mutating the input', () => {
|
|
41
|
+
const t = tree(BASE);
|
|
42
|
+
const next = renameItemOp(t, 'a', 'alpha');
|
|
43
|
+
expect(flat(next)).toContain('1:alpha');
|
|
44
|
+
expect(flat(t)).toContain('1:a'); // original untouched
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('addItemOp adds a child and a sibling', () => {
|
|
48
|
+
const child = addItemOp(tree(BASE), 'a', 'child', 'a1');
|
|
49
|
+
const aNode = child.roots[0].children.find((n) => n.label === 'a');
|
|
50
|
+
expect(aNode?.children.map((c) => c.label)).toEqual(['a1']);
|
|
51
|
+
|
|
52
|
+
const sib = addItemOp(tree(BASE), 'a', 'siblingAfter', 'a2');
|
|
53
|
+
expect(sib.roots[0].children.map((c) => c.label)).toEqual(['a', 'a2', 'b/', 'c']);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('addItemOp with isDir adds a trailing-slash folder with a folder default name', () => {
|
|
57
|
+
// Explicit label gets a slash.
|
|
58
|
+
const named = addItemOp(tree(BASE), 'a', 'siblingAfter', 'assets', true);
|
|
59
|
+
const assets = named.roots[0].children.find((n) => n.label === 'assets/');
|
|
60
|
+
expect(assets?.isDir).toBe(true);
|
|
61
|
+
|
|
62
|
+
// Default name is a fresh `folderN/`.
|
|
63
|
+
const auto = addItemOp(tree(BASE), 'a', 'siblingAfter', undefined, true);
|
|
64
|
+
const folder = auto.roots[0].children.find((n) => n.label.startsWith('folder'));
|
|
65
|
+
expect(folder?.label).toBe('folder1/');
|
|
66
|
+
expect(folder?.isDir).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('removeItemOp drops a node and its subtree', () => {
|
|
70
|
+
const next = removeItemOp(tree(BASE), 'b');
|
|
71
|
+
expect(flat(next)).toEqual(['0:root/', '1:a', '1:c']);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('indentItemOp re-parents under the preceding sibling', () => {
|
|
75
|
+
const next = indentItemOp(tree(BASE), 'c'); // c indents under b/
|
|
76
|
+
expect(flat(next)).toEqual(['0:root/', '1:a', '1:b/', '2:b1', '2:c']);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('indentItemOp is a no-op for the first sibling', () => {
|
|
80
|
+
const t = tree(BASE);
|
|
81
|
+
expect(indentItemOp(t, 'a')).toBe(t);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('outdentItemOp promotes a node to a sibling of its parent (subtree carried)', () => {
|
|
85
|
+
const next = outdentItemOp(tree(BASE), 'b1'); // b1 outdents next to b/
|
|
86
|
+
expect(flat(next)).toEqual(['0:root/', '1:a', '1:b/', '1:b1', '1:c']);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('outdentItemOp is a no-op for a root', () => {
|
|
90
|
+
const t = tree(BASE);
|
|
91
|
+
expect(outdentItemOp(t, 'root')).toBe(t);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('moveItemOp reorders siblings before or after a target', () => {
|
|
95
|
+
expect(flat(moveItemOp(tree(BASE), 'c', 'a', 'before'))).toEqual([
|
|
96
|
+
'0:root/',
|
|
97
|
+
'1:c',
|
|
98
|
+
'1:a',
|
|
99
|
+
'1:b/',
|
|
100
|
+
'2:b1',
|
|
101
|
+
]);
|
|
102
|
+
expect(flat(moveItemOp(tree(BASE), 'b', 'c', 'after'))).toEqual([
|
|
103
|
+
'0:root/',
|
|
104
|
+
'1:a',
|
|
105
|
+
'1:c',
|
|
106
|
+
'1:b/',
|
|
107
|
+
'2:b1',
|
|
108
|
+
]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('moveItemOp reparents nodes and carries their subtrees', () => {
|
|
112
|
+
expect(flat(moveItemOp(tree(BASE), 'c', 'a', 'child'))).toEqual([
|
|
113
|
+
'0:root/',
|
|
114
|
+
'1:a',
|
|
115
|
+
'2:c',
|
|
116
|
+
'1:b/',
|
|
117
|
+
'2:b1',
|
|
118
|
+
]);
|
|
119
|
+
expect(flat(moveItemOp(tree(BASE), 'b', 'a', 'child'))).toEqual([
|
|
120
|
+
'0:root/',
|
|
121
|
+
'1:a',
|
|
122
|
+
'2:b/',
|
|
123
|
+
'3:b1',
|
|
124
|
+
'1:c',
|
|
125
|
+
]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('moveItemOp can outdent relative to a shallower target', () => {
|
|
129
|
+
expect(flat(moveItemOp(tree(BASE), 'b1', 'b', 'before'))).toEqual([
|
|
130
|
+
'0:root/',
|
|
131
|
+
'1:a',
|
|
132
|
+
'1:b1',
|
|
133
|
+
'1:b/',
|
|
134
|
+
'1:c',
|
|
135
|
+
]);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('moveItemOp rejects self-drops and drops into the source subtree', () => {
|
|
139
|
+
const t = tree(BASE);
|
|
140
|
+
expect(moveItemOp(t, 'b', 'b', 'child')).toBe(t);
|
|
141
|
+
expect(moveItemOp(t, 'b', 'b1', 'child')).toBe(t);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('moveItemUp / moveItemDown reorder siblings within bounds', () => {
|
|
145
|
+
expect(flat(moveItemDownOp(tree(BASE), 'a'))).toEqual([
|
|
146
|
+
'0:root/',
|
|
147
|
+
'1:b/',
|
|
148
|
+
'2:b1',
|
|
149
|
+
'1:a',
|
|
150
|
+
'1:c',
|
|
151
|
+
]);
|
|
152
|
+
const t = tree(BASE);
|
|
153
|
+
expect(moveItemUpOp(t, 'a')).toBe(t); // already first
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('toggleDirOp toggles the trailing slash', () => {
|
|
157
|
+
const on = toggleDirOp(tree(BASE), 'a'); // a → a/
|
|
158
|
+
expect(on.roots[0].children[0].label).toBe('a/');
|
|
159
|
+
const off = toggleDirOp(on, 'a'); // a/ → a
|
|
160
|
+
expect(off.roots[0].children.find((n) => n.label === 'a')).toBeDefined();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('every op keeps the tree renderable + byte-stable', () => {
|
|
164
|
+
for (const op of [
|
|
165
|
+
(t: Tree) => renameItemOp(t, 'a', 'alpha'),
|
|
166
|
+
(t: Tree) => addItemOp(t, 'b', 'child', 'b2'),
|
|
167
|
+
(t: Tree) => indentItemOp(t, 'c'),
|
|
168
|
+
(t: Tree) => outdentItemOp(t, 'b1'),
|
|
169
|
+
(t: Tree) => moveItemOp(t, 'c', 'a', 'child'),
|
|
170
|
+
(t: Tree) => moveItemDownOp(t, 'a'),
|
|
171
|
+
]) {
|
|
172
|
+
const next = op(tree(BASE));
|
|
173
|
+
const r1 = renderTree(next);
|
|
174
|
+
expect(renderTree(parseTree(r1))).toBe(r1);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
});
|
|
@@ -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,179 @@
|
|
|
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('moveItem reorders and reparents through one fence rewrite', () => {
|
|
115
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
116
|
+
const id = firstId(editor);
|
|
117
|
+
expect(
|
|
118
|
+
applyTreeCommand(editor, id, {
|
|
119
|
+
kind: 'moveItem',
|
|
120
|
+
id: 'config-ts',
|
|
121
|
+
targetId: 'utils',
|
|
122
|
+
position: 'child',
|
|
123
|
+
}),
|
|
124
|
+
).toBe(true);
|
|
125
|
+
const tree = parseTree(fenceOf(editor).text);
|
|
126
|
+
const utils = tree.roots[0].children.find((n) => n.label === 'utils/');
|
|
127
|
+
expect(utils?.children.map((child) => child.label)).toEqual(['math.ts', 'config.ts']);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('removeItem drops a node', () => {
|
|
131
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
132
|
+
const id = firstId(editor);
|
|
133
|
+
expect(applyTreeCommand(editor, id, { kind: 'removeItem', id: 'config-ts' })).toBe(true);
|
|
134
|
+
expect(labels(editor)).not.toContain('config.ts');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('promotes the fence language to the explicit `tree` tag on edit', () => {
|
|
138
|
+
// A once-edited tree should carry `language: 'tree'` so its identity
|
|
139
|
+
// survives a later flatten → markdown → re-import round-trip.
|
|
140
|
+
const editor = makeEditor('```text\n' + ART + '\n```\n');
|
|
141
|
+
const id = firstId(editor);
|
|
142
|
+
expect(fenceOf(editor).language).toBe('text');
|
|
143
|
+
applyTreeCommand(editor, id, { kind: 'renameItem', id: 'index-ts', label: 'app.ts' });
|
|
144
|
+
expect(fenceOf(editor).language).toBe('tree');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('keeps the block id stable across the language-promotion rewrite', () => {
|
|
148
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
149
|
+
const id = firstId(editor);
|
|
150
|
+
applyTreeCommand(editor, id, { kind: 'renameItem', id: 'index-ts', label: 'app.ts' });
|
|
151
|
+
expect(firstId(editor)).toBe(id);
|
|
152
|
+
expect(fenceOf(editor).language).toBe('tree');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('one undo restores the original fence bytes', () => {
|
|
156
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
157
|
+
const id = firstId(editor);
|
|
158
|
+
const before = fenceOf(editor).text;
|
|
159
|
+
applyTreeCommand(editor, id, { kind: 'renameItem', id: 'index-ts', label: 'app.ts' });
|
|
160
|
+
expect(fenceOf(editor).text).not.toBe(before);
|
|
161
|
+
editor.commands.undo();
|
|
162
|
+
expect(fenceOf(editor).text).toBe(before);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('no-op commands leave the document untouched', () => {
|
|
166
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
167
|
+
const id = firstId(editor);
|
|
168
|
+
const json = JSON.stringify(editor.state.doc.toJSON());
|
|
169
|
+
expect(applyTreeCommand(editor, id, { kind: 'indentItem', id: 'src' })).toBe(false); // root
|
|
170
|
+
expect(JSON.stringify(editor.state.doc.toJSON())).toBe(json);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('returns false for an unknown block id', () => {
|
|
174
|
+
const editor = makeEditor('```\n' + ART + '\n```\n');
|
|
175
|
+
expect(applyTreeCommand(editor, 'tree-999', { kind: 'removeItem', id: 'index-ts' })).toBe(
|
|
176
|
+
false,
|
|
177
|
+
);
|
|
178
|
+
});
|
|
179
|
+
});
|