@bendyline/squisq-editor-react 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/dist/index.d.ts +505 -15
- package/dist/index.js +6035 -2283
- package/dist/index.js.map +1 -1
- package/dist/styles/index.css +870 -40
- package/package.json +6 -5
- package/src/EditorContext.tsx +27 -0
- package/src/EditorShell.tsx +24 -0
- package/src/PreviewControls.tsx +20 -5
- package/src/PreviewPanel.tsx +5 -1
- package/src/RawEditor.tsx +12 -0
- package/src/RecorderEntry.tsx +3 -0
- package/src/Toolbar.tsx +315 -17
- package/src/WysiwygEditor.tsx +25 -11
- package/src/__tests__/buildPreviewDocContent.test.ts +17 -0
- package/src/__tests__/editorShellProps.test.tsx +65 -0
- package/src/__tests__/findMode.test.tsx +104 -0
- package/src/__tests__/findModel.test.ts +55 -0
- package/src/__tests__/markdownCodeFence.test.ts +45 -0
- package/src/__tests__/mediaReferences.test.ts +15 -0
- package/src/__tests__/previewControls.test.tsx +31 -0
- package/src/__tests__/tiptapBridge.test.ts +9 -0
- package/src/__tests__/toolbarSelectionConversion.test.tsx +26 -0
- package/src/__tests__/writeCanvasSettings.test.ts +15 -0
- package/src/asciiDiagram/__tests__/AsciiDiagramExtension.test.ts +20 -1
- package/src/buildPreviewDoc.ts +9 -7
- package/src/codeSnippet/CodeSnippetExtension.ts +205 -0
- package/src/codeSnippet/CodeSnippetWidget.tsx +109 -0
- package/src/codeSnippet/__tests__/CodeSnippetExtension.test.ts +95 -0
- package/src/codeSnippet/__tests__/codeSnippetLanguages.test.ts +50 -0
- package/src/codeSnippet/codeSnippetCommands.ts +21 -0
- package/src/codeSnippet/codeSnippetData.ts +43 -0
- package/src/codeSnippet/codeSnippetLanguages.ts +216 -0
- package/src/diagram/DiagramCanvas.tsx +1 -0
- package/src/find/FindHighlightExtension.ts +81 -0
- package/src/find/FindToolbar.tsx +314 -0
- package/src/find/findModel.ts +77 -0
- package/src/index.ts +89 -0
- package/src/markdownCodeFence.ts +72 -0
- package/src/mediaReferences.ts +5 -3
- package/src/mermaid/MermaidDiagramCanvas.tsx +693 -0
- package/src/mermaid/MermaidDiagramExtension.ts +243 -0
- package/src/mermaid/MermaidDiagramTypeThumbnail.tsx +184 -0
- package/src/mermaid/MermaidDiagramWidget.tsx +653 -0
- package/src/mermaid/MermaidShapePalette.tsx +245 -0
- package/src/mermaid/__tests__/MermaidDiagramExtension.test.ts +361 -0
- package/src/mermaid/__tests__/mermaidDiagramTypes.test.ts +35 -0
- package/src/mermaid/__tests__/mermaidRenderer.test.ts +49 -0
- package/src/mermaid/__tests__/mermaidSourceOps.test.ts +213 -0
- package/src/mermaid/__tests__/mermaidSyntax.test.ts +71 -0
- package/src/mermaid/mermaidCommands.ts +34 -0
- package/src/mermaid/mermaidData.ts +31 -0
- package/src/mermaid/mermaidDiagramTypes.ts +325 -0
- package/src/mermaid/mermaidModel.ts +31 -0
- package/src/mermaid/mermaidRenderer.ts +181 -0
- package/src/mermaid/mermaidShapes.ts +113 -0
- package/src/mermaid/mermaidSourceOps.ts +454 -0
- package/src/scene/Scene.tsx +46 -15
- package/src/scene/SceneBlockToolbar.tsx +29 -14
- package/src/scene/SceneViewControls.tsx +43 -0
- package/src/scene/__tests__/fitScale.test.ts +19 -0
- package/src/scene/__tests__/useScenePanZoom.test.ts +28 -1
- package/src/scene/fitScale.ts +17 -0
- package/src/scene/hooks/useScenePanZoom.ts +11 -4
- package/src/scene/scene.css +85 -3
- package/src/styles/code-snippet.css +76 -0
- package/src/styles/editor.css +322 -2
- package/src/styles/index.css +2 -0
- package/src/styles/mermaid-diagram.css +487 -0
- package/src/writeCanvasSettings.ts +30 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiptap extension for ordinary explicit-language code fences.
|
|
3
|
+
*
|
|
4
|
+
* The ProseMirror code block remains authoritative. This plugin hides it and
|
|
5
|
+
* mounts a Monaco editor whose edits rewrite only the node's text content, so
|
|
6
|
+
* fence language and source round-trip through Markdown without a parallel model.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Extension } from '@tiptap/core';
|
|
10
|
+
import type { Node as PMNode } from '@tiptap/pm/model';
|
|
11
|
+
import { Plugin, PluginKey, type Transaction } from '@tiptap/pm/state';
|
|
12
|
+
import { Decoration, DecorationSet } from '@tiptap/pm/view';
|
|
13
|
+
import type { Editor } from '@tiptap/react';
|
|
14
|
+
import { createElement } from 'react';
|
|
15
|
+
import { createRoot, type Root } from 'react-dom/client';
|
|
16
|
+
import { CodeSnippetWidget } from './CodeSnippetWidget';
|
|
17
|
+
import { isCodeSnippetFenceLanguage } from './codeSnippetLanguages';
|
|
18
|
+
|
|
19
|
+
export interface CodeSnippetBlockEntry {
|
|
20
|
+
/** Synthetic session id, stable while this code block remains in the doc. */
|
|
21
|
+
id: string;
|
|
22
|
+
/** Current ProseMirror position of the `codeBlock`. */
|
|
23
|
+
pos: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface CodeSnippetPluginState {
|
|
27
|
+
entries: CodeSnippetBlockEntry[];
|
|
28
|
+
decorations: DecorationSet;
|
|
29
|
+
seq: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface CodeSnippetExtensionOptions {
|
|
33
|
+
/** When false, leave all ordinary language-tagged fences as code blocks. */
|
|
34
|
+
enabled?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const CODE_SNIPPET_KEY = new PluginKey<CodeSnippetPluginState>('squisq-code-snippet');
|
|
38
|
+
|
|
39
|
+
export function isCodeSnippetNode(node: PMNode): boolean {
|
|
40
|
+
const language = (node.attrs as { language?: unknown }).language;
|
|
41
|
+
return (
|
|
42
|
+
node.type.name === 'codeBlock' &&
|
|
43
|
+
isCodeSnippetFenceLanguage(typeof language === 'string' ? language : null)
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function findCodeSnippetBlockPos(editor: Editor, blockId: string): number | null {
|
|
48
|
+
const state = CODE_SNIPPET_KEY.getState(editor.state);
|
|
49
|
+
return state?.entries.find((entry) => entry.id === blockId)?.pos ?? null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface WidgetRootRef {
|
|
53
|
+
root: Root;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function buildDecorations(
|
|
57
|
+
doc: PMNode,
|
|
58
|
+
entries: readonly CodeSnippetBlockEntry[],
|
|
59
|
+
editor: Editor,
|
|
60
|
+
): DecorationSet {
|
|
61
|
+
const decorations: Decoration[] = [];
|
|
62
|
+
for (const entry of entries) {
|
|
63
|
+
const node = doc.nodeAt(entry.pos);
|
|
64
|
+
if (!node || !isCodeSnippetNode(node)) continue;
|
|
65
|
+
decorations.push(
|
|
66
|
+
Decoration.node(entry.pos, entry.pos + node.nodeSize, {
|
|
67
|
+
class: 'squisq-code-snippet-fence-hidden',
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
70
|
+
const blockId = entry.id;
|
|
71
|
+
decorations.push(
|
|
72
|
+
Decoration.widget(
|
|
73
|
+
entry.pos + node.nodeSize,
|
|
74
|
+
(view) => {
|
|
75
|
+
const container = document.createElement('div');
|
|
76
|
+
container.className = 'squisq-code-snippet-widget-host';
|
|
77
|
+
container.contentEditable = 'false';
|
|
78
|
+
container.addEventListener('mousedown', (event) => event.stopPropagation());
|
|
79
|
+
container.addEventListener('keydown', (event) => event.stopPropagation());
|
|
80
|
+
const root = createRoot(container);
|
|
81
|
+
root.render(
|
|
82
|
+
createElement(CodeSnippetWidget, {
|
|
83
|
+
editor,
|
|
84
|
+
blockId,
|
|
85
|
+
host: view.dom.parentElement ?? view.dom,
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
(
|
|
89
|
+
container as HTMLElement & { __squisqCodeSnippetRoot?: WidgetRootRef }
|
|
90
|
+
).__squisqCodeSnippetRoot = { root };
|
|
91
|
+
return container;
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
destroy: (dom) => {
|
|
95
|
+
const ref = (dom as HTMLElement & { __squisqCodeSnippetRoot?: WidgetRootRef })
|
|
96
|
+
.__squisqCodeSnippetRoot;
|
|
97
|
+
if (ref) setTimeout(() => ref.root.unmount(), 0);
|
|
98
|
+
},
|
|
99
|
+
ignoreSelection: true,
|
|
100
|
+
key: `squisq-code-snippet-${entry.id}`,
|
|
101
|
+
side: 1,
|
|
102
|
+
},
|
|
103
|
+
),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return DecorationSet.create(doc, decorations);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function remapEntries(
|
|
110
|
+
tr: Transaction,
|
|
111
|
+
previous: CodeSnippetPluginState,
|
|
112
|
+
doc: PMNode,
|
|
113
|
+
): { entries: CodeSnippetBlockEntry[]; seq: number } {
|
|
114
|
+
const mapped = new Map<number, string>();
|
|
115
|
+
const claimed = new Set<string>();
|
|
116
|
+
|
|
117
|
+
for (const entry of previous.entries) {
|
|
118
|
+
const result = tr.mapping.mapResult(entry.pos, 1);
|
|
119
|
+
if (!result.deleted) {
|
|
120
|
+
mapped.set(result.pos, entry.id);
|
|
121
|
+
claimed.add(entry.id);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Replacing node content can mark its opening token as replaced even though
|
|
126
|
+
// the same language-tagged code block still occupies the mapped position.
|
|
127
|
+
for (const entry of previous.entries) {
|
|
128
|
+
if (claimed.has(entry.id)) continue;
|
|
129
|
+
const result = tr.mapping.mapResult(entry.pos, 1);
|
|
130
|
+
if (mapped.has(result.pos)) continue;
|
|
131
|
+
if (doc.nodeAt(result.pos)?.type.name === 'codeBlock') {
|
|
132
|
+
mapped.set(result.pos, entry.id);
|
|
133
|
+
claimed.add(entry.id);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let seq = previous.seq;
|
|
138
|
+
const entries: CodeSnippetBlockEntry[] = [];
|
|
139
|
+
doc.descendants((node, pos) => {
|
|
140
|
+
if (node.type.name !== 'codeBlock') return;
|
|
141
|
+
if (!isCodeSnippetNode(node)) return false;
|
|
142
|
+
entries.push({ id: mapped.get(pos) ?? `code-snippet-${++seq}`, pos });
|
|
143
|
+
return false;
|
|
144
|
+
});
|
|
145
|
+
return { entries, seq };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function applyState(
|
|
149
|
+
tr: Transaction,
|
|
150
|
+
previous: CodeSnippetPluginState,
|
|
151
|
+
editor: Editor,
|
|
152
|
+
doc: PMNode,
|
|
153
|
+
): CodeSnippetPluginState {
|
|
154
|
+
if (!tr.docChanged) return previous;
|
|
155
|
+
const { entries, seq } = remapEntries(tr, previous, doc);
|
|
156
|
+
return {
|
|
157
|
+
entries,
|
|
158
|
+
seq,
|
|
159
|
+
decorations: buildDecorations(doc, entries, editor),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export const CodeSnippetExtension = Extension.create<CodeSnippetExtensionOptions>({
|
|
164
|
+
name: 'squisqCodeSnippet',
|
|
165
|
+
|
|
166
|
+
addOptions() {
|
|
167
|
+
return { enabled: true };
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
addProseMirrorPlugins() {
|
|
171
|
+
if (this.options.enabled === false) return [];
|
|
172
|
+
const editor = this.editor as Editor;
|
|
173
|
+
return [
|
|
174
|
+
new Plugin<CodeSnippetPluginState>({
|
|
175
|
+
key: CODE_SNIPPET_KEY,
|
|
176
|
+
state: {
|
|
177
|
+
init: (_config, state) => {
|
|
178
|
+
let seq = 0;
|
|
179
|
+
const entries: CodeSnippetBlockEntry[] = [];
|
|
180
|
+
state.doc.descendants((node, pos) => {
|
|
181
|
+
if (node.type.name !== 'codeBlock') return;
|
|
182
|
+
if (!isCodeSnippetNode(node)) return false;
|
|
183
|
+
entries.push({ id: `code-snippet-${++seq}`, pos });
|
|
184
|
+
return false;
|
|
185
|
+
});
|
|
186
|
+
return {
|
|
187
|
+
entries,
|
|
188
|
+
seq,
|
|
189
|
+
decorations: buildDecorations(state.doc, entries, editor),
|
|
190
|
+
};
|
|
191
|
+
},
|
|
192
|
+
apply: (tr, previous, _oldState, newState) =>
|
|
193
|
+
applyState(tr, previous, editor, newState.doc),
|
|
194
|
+
},
|
|
195
|
+
props: {
|
|
196
|
+
decorations(state) {
|
|
197
|
+
return this.getState(state)?.decorations ?? DecorationSet.empty;
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
}),
|
|
201
|
+
];
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
export default CodeSnippetExtension;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/** Monaco inset mounted in place of an ordinary language-tagged code fence. */
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
4
|
+
import MonacoEditor, { type OnChange } from '@monaco-editor/react';
|
|
5
|
+
import type { Editor } from '@tiptap/react';
|
|
6
|
+
import { Icon } from '../Icon';
|
|
7
|
+
import { useMonacoLoader } from '../useMonacoLoader';
|
|
8
|
+
import { replaceCodeSnippetText } from './codeSnippetCommands';
|
|
9
|
+
import { useCodeSnippetData } from './codeSnippetData';
|
|
10
|
+
import { codeSnippetFenceLanguageToken } from './codeSnippetLanguages';
|
|
11
|
+
|
|
12
|
+
export interface CodeSnippetWidgetProps {
|
|
13
|
+
editor: Editor;
|
|
14
|
+
blockId: string;
|
|
15
|
+
host?: HTMLElement | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function schemeFromHost(host: HTMLElement | null | undefined): 'light' | 'dark' {
|
|
19
|
+
return host?.closest<HTMLElement>('[data-theme]')?.dataset.theme === 'dark' ? 'dark' : 'light';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function useHostColorScheme(host: HTMLElement | null | undefined): 'light' | 'dark' {
|
|
23
|
+
const [scheme, setScheme] = useState<'light' | 'dark'>(() => schemeFromHost(host));
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
const themedAncestor = host?.closest<HTMLElement>('[data-theme]');
|
|
26
|
+
setScheme(schemeFromHost(host));
|
|
27
|
+
if (!themedAncestor || typeof MutationObserver === 'undefined') return;
|
|
28
|
+
const observer = new MutationObserver(() => setScheme(schemeFromHost(host)));
|
|
29
|
+
observer.observe(themedAncestor, { attributes: true, attributeFilter: ['data-theme'] });
|
|
30
|
+
return () => observer.disconnect();
|
|
31
|
+
}, [host]);
|
|
32
|
+
return scheme;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function useEditorEditable(editor: Editor): boolean {
|
|
36
|
+
const [editable, setEditable] = useState(editor.isEditable);
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const sync = () => setEditable(editor.isEditable);
|
|
39
|
+
editor.on('update', sync);
|
|
40
|
+
editor.on('transaction', sync);
|
|
41
|
+
return () => {
|
|
42
|
+
editor.off('update', sync);
|
|
43
|
+
editor.off('transaction', sync);
|
|
44
|
+
};
|
|
45
|
+
}, [editor]);
|
|
46
|
+
return editable;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function CodeSnippetWidget({ editor, blockId, host }: CodeSnippetWidgetProps) {
|
|
50
|
+
const data = useCodeSnippetData(editor, blockId);
|
|
51
|
+
const { ready } = useMonacoLoader();
|
|
52
|
+
const colorScheme = useHostColorScheme(host);
|
|
53
|
+
const editable = useEditorEditable(editor);
|
|
54
|
+
|
|
55
|
+
const handleChange: OnChange = useCallback(
|
|
56
|
+
(value) => {
|
|
57
|
+
if (!editable) return;
|
|
58
|
+
replaceCodeSnippetText(editor, blockId, value ?? '');
|
|
59
|
+
},
|
|
60
|
+
[blockId, editable, editor],
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
if (!data) return null;
|
|
64
|
+
const modelSuffix = codeSnippetFenceLanguageToken(data.fenceLanguage).replace(
|
|
65
|
+
/[^a-z0-9_-]/g,
|
|
66
|
+
'-',
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<div className="squisq-code-snippet-shell" data-language={data.fenceLanguage}>
|
|
71
|
+
<div className="squisq-code-snippet-header">
|
|
72
|
+
<span className="squisq-code-snippet-title">
|
|
73
|
+
<Icon icon="fa-solid fa-file-code" />
|
|
74
|
+
<span>{data.label}</span>
|
|
75
|
+
</span>
|
|
76
|
+
<code>{`\`\`\`${data.fenceLanguage}`}</code>
|
|
77
|
+
</div>
|
|
78
|
+
<div className="squisq-code-snippet-editor" aria-label={`${data.label} code editor`}>
|
|
79
|
+
{ready ? (
|
|
80
|
+
<MonacoEditor
|
|
81
|
+
path={`inmemory://squisq/code-snippet/${blockId}.${modelSuffix || 'txt'}`}
|
|
82
|
+
language={data.monacoLanguage}
|
|
83
|
+
theme={colorScheme === 'dark' ? 'vs-dark' : 'vs'}
|
|
84
|
+
value={data.source}
|
|
85
|
+
onChange={handleChange}
|
|
86
|
+
options={{
|
|
87
|
+
automaticLayout: true,
|
|
88
|
+
contextmenu: true,
|
|
89
|
+
folding: true,
|
|
90
|
+
fontSize: 13,
|
|
91
|
+
lineNumbers: 'on',
|
|
92
|
+
minimap: { enabled: false },
|
|
93
|
+
padding: { top: 10, bottom: 10 },
|
|
94
|
+
readOnly: !editable,
|
|
95
|
+
renderLineHighlight: 'line',
|
|
96
|
+
scrollBeyondLastLine: false,
|
|
97
|
+
tabSize: 2,
|
|
98
|
+
wordWrap: 'off',
|
|
99
|
+
}}
|
|
100
|
+
/>
|
|
101
|
+
) : (
|
|
102
|
+
<div className="squisq-code-snippet-loading">Loading code editor…</div>
|
|
103
|
+
)}
|
|
104
|
+
</div>
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export default CodeSnippetWidget;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** @vitest-environment jsdom */
|
|
2
|
+
|
|
3
|
+
import { Editor } from '@tiptap/core';
|
|
4
|
+
import StarterKit from '@tiptap/starter-kit';
|
|
5
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
6
|
+
import { markdownToTiptap, tiptapToMarkdown } from '../../tiptapBridge';
|
|
7
|
+
import { replaceCodeSnippetText } from '../codeSnippetCommands';
|
|
8
|
+
import {
|
|
9
|
+
CODE_SNIPPET_KEY,
|
|
10
|
+
CodeSnippetExtension,
|
|
11
|
+
findCodeSnippetBlockPos,
|
|
12
|
+
} from '../CodeSnippetExtension';
|
|
13
|
+
|
|
14
|
+
vi.mock('../CodeSnippetWidget', () => ({
|
|
15
|
+
CodeSnippetWidget: () => null,
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
const editors: Editor[] = [];
|
|
19
|
+
|
|
20
|
+
function makeEditor(markdown: string, enabled = true): Editor {
|
|
21
|
+
const editor = new Editor({
|
|
22
|
+
extensions: [StarterKit, CodeSnippetExtension.configure({ enabled })],
|
|
23
|
+
content: markdownToTiptap(markdown),
|
|
24
|
+
});
|
|
25
|
+
editors.push(editor);
|
|
26
|
+
return editor;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function entriesOf(editor: Editor) {
|
|
30
|
+
return CODE_SNIPPET_KEY.getState(editor.state)?.entries ?? [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
afterEach(() => {
|
|
34
|
+
for (const editor of editors) editor.destroy();
|
|
35
|
+
editors.length = 0;
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('CodeSnippetExtension', () => {
|
|
39
|
+
it('claims all explicit ordinary code languages, including unknown ones', () => {
|
|
40
|
+
const editor = makeEditor(
|
|
41
|
+
'```typescript\nconst n = 1;\n```\n\n```kusto\nTable | take 5\n```\n',
|
|
42
|
+
);
|
|
43
|
+
expect(entriesOf(editor)).toHaveLength(2);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('does not steal plain, Mermaid, diagram, tree, or timeline fences', () => {
|
|
47
|
+
const editor = makeEditor(
|
|
48
|
+
[
|
|
49
|
+
'```text\nplain\n```',
|
|
50
|
+
'```mermaid\nflowchart LR\n a --> b\n```',
|
|
51
|
+
'```diagram\n+---+\n| A |\n+---+\n```',
|
|
52
|
+
'```tree\nsrc/\n└── index.ts\n```',
|
|
53
|
+
'```timeline\nMilestones: ● Start ─►\n```',
|
|
54
|
+
'```\nuntagged\n```',
|
|
55
|
+
'',
|
|
56
|
+
].join('\n\n'),
|
|
57
|
+
);
|
|
58
|
+
expect(entriesOf(editor)).toHaveLength(0);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('keeps a stable block id and rewrites only the fence body', () => {
|
|
62
|
+
const editor = makeEditor('```typescript\nconst before = true;\n```\n');
|
|
63
|
+
const [before] = entriesOf(editor);
|
|
64
|
+
editor.commands.insertContentAt(0, '<p>Intro</p>');
|
|
65
|
+
const [shifted] = entriesOf(editor);
|
|
66
|
+
expect(shifted.id).toBe(before.id);
|
|
67
|
+
expect(shifted.pos).toBeGreaterThan(before.pos);
|
|
68
|
+
expect(findCodeSnippetBlockPos(editor, before.id)).toBe(shifted.pos);
|
|
69
|
+
|
|
70
|
+
expect(replaceCodeSnippetText(editor, before.id, 'const after: number = 2;')).toBe(true);
|
|
71
|
+
expect(tiptapToMarkdown(editor.getHTML())).toContain(
|
|
72
|
+
'```typescript\nconst after: number = 2;\n```',
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('supports clearing the Monaco model without losing the fence language', () => {
|
|
77
|
+
const editor = makeEditor('```json\n{"key": true}\n```\n');
|
|
78
|
+
const [entry] = entriesOf(editor);
|
|
79
|
+
expect(replaceCodeSnippetText(editor, entry.id, '')).toBe(true);
|
|
80
|
+
const node = editor.state.doc.nodeAt(findCodeSnippetBlockPos(editor, entry.id) ?? -1);
|
|
81
|
+
expect(node?.textContent).toBe('');
|
|
82
|
+
expect(node?.attrs.language).toBe('json');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('round-trips the language tag and source byte-for-byte', () => {
|
|
86
|
+
const markdown = '```typescript\nconst n: number = 1;\n```\n';
|
|
87
|
+
const editor = makeEditor(markdown);
|
|
88
|
+
expect(tiptapToMarkdown(editor.getHTML())).toBe(markdown);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('can be disabled', () => {
|
|
92
|
+
const editor = makeEditor('```typescript\nconst n = 1;\n```\n', false);
|
|
93
|
+
expect(CODE_SNIPPET_KEY.getState(editor.state)).toBeUndefined();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
CODE_SNIPPET_LANGUAGES,
|
|
4
|
+
codeSnippetFenceLanguageToken,
|
|
5
|
+
codeSnippetLanguageLabel,
|
|
6
|
+
codeSnippetMarkdown,
|
|
7
|
+
isCodeSnippetFenceLanguage,
|
|
8
|
+
monacoLanguageForFence,
|
|
9
|
+
} from '../codeSnippetLanguages';
|
|
10
|
+
|
|
11
|
+
describe('code snippet languages', () => {
|
|
12
|
+
it('offers unique explicit fence languages', () => {
|
|
13
|
+
const languages = CODE_SNIPPET_LANGUAGES.map((entry) => entry.fenceLanguage);
|
|
14
|
+
expect(new Set(languages).size).toBe(languages.length);
|
|
15
|
+
expect(languages).toContain('typescript');
|
|
16
|
+
expect(languages).toContain('json');
|
|
17
|
+
expect(languages).toContain('python');
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('keeps specialized authored-view languages out of the Monaco inset', () => {
|
|
21
|
+
for (const language of [
|
|
22
|
+
'',
|
|
23
|
+
'text',
|
|
24
|
+
'txt',
|
|
25
|
+
'plaintext',
|
|
26
|
+
'ascii',
|
|
27
|
+
'diagram',
|
|
28
|
+
'tree',
|
|
29
|
+
'timeline',
|
|
30
|
+
'mermaid',
|
|
31
|
+
]) {
|
|
32
|
+
expect(isCodeSnippetFenceLanguage(language), language).toBe(false);
|
|
33
|
+
}
|
|
34
|
+
expect(isCodeSnippetFenceLanguage('typescript')).toBe(true);
|
|
35
|
+
expect(isCodeSnippetFenceLanguage('kusto')).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('uses the first fence token for Monaco while preserving metadata for round trips', () => {
|
|
39
|
+
expect(codeSnippetFenceLanguageToken(' json data ')).toBe('json');
|
|
40
|
+
expect(monacoLanguageForFence('ts highlight-lines')).toBe('typescript');
|
|
41
|
+
expect(monacoLanguageForFence('yml')).toBe('yaml');
|
|
42
|
+
expect(codeSnippetLanguageLabel('csharp')).toBe('C#');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('creates an explicit language-tagged Markdown fence', () => {
|
|
46
|
+
expect(codeSnippetMarkdown('typescript', 'const n: number = 1;')).toBe(
|
|
47
|
+
'\n```typescript\nconst n: number = 1;\n```\n',
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Lossless edits for language-tagged code fences. */
|
|
2
|
+
|
|
3
|
+
import type { Editor } from '@tiptap/react';
|
|
4
|
+
import { findCodeSnippetBlockPos, isCodeSnippetNode } from './CodeSnippetExtension';
|
|
5
|
+
|
|
6
|
+
export function replaceCodeSnippetText(editor: Editor, blockId: string, nextText: string): boolean {
|
|
7
|
+
const pos = findCodeSnippetBlockPos(editor, blockId);
|
|
8
|
+
if (pos === null) return false;
|
|
9
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
10
|
+
if (!node || !isCodeSnippetNode(node)) return false;
|
|
11
|
+
if (node.textContent === nextText) return true;
|
|
12
|
+
|
|
13
|
+
const from = pos + 1;
|
|
14
|
+
const to = pos + node.nodeSize - 1;
|
|
15
|
+
const tr =
|
|
16
|
+
nextText.length > 0
|
|
17
|
+
? editor.state.tr.replaceWith(from, to, editor.state.schema.text(nextText))
|
|
18
|
+
: editor.state.tr.delete(from, to);
|
|
19
|
+
editor.view.dispatch(tr);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Live source/language view for one registered code snippet fence. */
|
|
2
|
+
|
|
3
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
4
|
+
import type { Editor } from '@tiptap/react';
|
|
5
|
+
import { findCodeSnippetBlockPos, isCodeSnippetNode } from './CodeSnippetExtension';
|
|
6
|
+
import { codeSnippetLanguageLabel, monacoLanguageForFence } from './codeSnippetLanguages';
|
|
7
|
+
|
|
8
|
+
export interface CodeSnippetData {
|
|
9
|
+
/** Fence language exactly as stored in the ProseMirror node. */
|
|
10
|
+
fenceLanguage: string;
|
|
11
|
+
label: string;
|
|
12
|
+
monacoLanguage: string;
|
|
13
|
+
/** Source exactly as stored inside the code block. */
|
|
14
|
+
source: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function useCodeSnippetData(editor: Editor, blockId: string): CodeSnippetData | null {
|
|
18
|
+
const [version, setVersion] = useState(0);
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
const onTransaction = () => setVersion((value) => value + 1);
|
|
21
|
+
editor.on('transaction', onTransaction);
|
|
22
|
+
return () => {
|
|
23
|
+
editor.off('transaction', onTransaction);
|
|
24
|
+
};
|
|
25
|
+
}, [editor]);
|
|
26
|
+
|
|
27
|
+
return useMemo(() => {
|
|
28
|
+
const pos = findCodeSnippetBlockPos(editor, blockId);
|
|
29
|
+
if (pos === null) return null;
|
|
30
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
31
|
+
if (!node || !isCodeSnippetNode(node)) return null;
|
|
32
|
+
const rawLanguage = (node.attrs as { language?: unknown }).language;
|
|
33
|
+
const fenceLanguage = typeof rawLanguage === 'string' ? rawLanguage : '';
|
|
34
|
+
return {
|
|
35
|
+
fenceLanguage,
|
|
36
|
+
label: codeSnippetLanguageLabel(fenceLanguage),
|
|
37
|
+
monacoLanguage: monacoLanguageForFence(fenceLanguage),
|
|
38
|
+
source: node.textContent,
|
|
39
|
+
};
|
|
40
|
+
// `version` is the transaction-backed invalidation token.
|
|
41
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
42
|
+
}, [editor, blockId, version]);
|
|
43
|
+
}
|