@airalogy/aimd-editor 1.14.1 → 1.16.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 +6 -0
- package/README.zh-CN.md +6 -0
- package/dist/{AimdEditorTopBar.vue_vue_type_script_setup_true_lang-DvxTMJdO.js → AimdEditorTopBar.vue_vue_type_script_setup_true_lang-DqlyxLJI.js} +958 -950
- package/dist/AimdSourceEditor.vue_vue_type_script_setup_true_lang-DdjWUzYX.js +495 -0
- package/dist/{AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-BiW39W8D.js → AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-DuOq6WfS.js} +4343 -4206
- package/dist/aimd-editor.css +1 -1
- package/dist/embedded.js +2 -2
- package/dist/index.js +4 -4
- package/dist/monaco.js +1 -1
- package/dist/{theme-CB0dPzmd.js → theme-5__dt-6K.js} +2 -2
- package/dist/vue.js +3 -3
- package/dist/wysiwyg.js +1 -1
- package/package.json +3 -3
- package/src/__tests__/embedded-images.test.ts +65 -0
- package/src/__tests__/quoted-inline-roundtrip.test.ts +30 -2
- package/src/language-config.ts +2 -2
- package/src/vue/AimdSourceEditor.vue +9 -1
- package/src/vue/AimdWysiwygEditor.vue +2 -0
- package/src/vue/embeddedImages.css +23 -0
- package/src/vue/embeddedImages.ts +61 -0
- package/src/vue/locales.ts +18 -0
- package/src/vue/milkdown-aimd-plugin.ts +57 -37
- package/src/vue/milkdown-embedded-images.ts +77 -0
- package/src/vue/monaco-embedded-images.ts +113 -0
- package/src/vue/useEditorContent.ts +25 -6
- package/dist/AimdSourceEditor.vue_vue_type_script_setup_true_lang-ey9GBO3X.js +0 -424
|
@@ -12,9 +12,9 @@ import type { NodeSchema, MarkdownNode } from '@milkdown/kit/transformer'
|
|
|
12
12
|
import type { Node as ProsemirrorNode } from '@milkdown/kit/prose/model'
|
|
13
13
|
import type { EditorView, NodeView } from '@milkdown/kit/prose/view'
|
|
14
14
|
import { restoreAimdInlineTemplates } from '@airalogy/aimd-core'
|
|
15
|
-
import { findAimdInlineTemplates, parseCheckContent, parseStepContent, parseVarDefinition } from '@airalogy/aimd-core/parser'
|
|
15
|
+
import { findAimdInlineTemplates, splitAimdInlineText, parseCheckContent, parseStepContent, parseVarDefinition } from '@airalogy/aimd-core/parser'
|
|
16
16
|
import type { AimdEditorMessages } from './locales'
|
|
17
|
-
import { hardbreakAttr, hardbreakSchema } from '@milkdown/kit/preset/commonmark'
|
|
17
|
+
import { hardbreakAttr, hardbreakSchema, textSchema } from '@milkdown/kit/preset/commonmark'
|
|
18
18
|
import { $node, $view, $remark, $inputRule } from '@milkdown/kit/utils'
|
|
19
19
|
import { InputRule } from '@milkdown/kit/prose/inputrules'
|
|
20
20
|
import { normalizeAimdInlineTemplateMarkdownEscapes } from './aimdInlineMarkdownNormalization'
|
|
@@ -25,6 +25,7 @@ const AIMD_COLORS: Record<string, string> = {
|
|
|
25
25
|
var_table: '#059669',
|
|
26
26
|
step: '#d97706',
|
|
27
27
|
check: '#dc2626',
|
|
28
|
+
task: '#5b21b6',
|
|
28
29
|
ref_step: '#0891b2',
|
|
29
30
|
ref_var: '#0891b2',
|
|
30
31
|
ref_fig: '#0891b2',
|
|
@@ -55,6 +56,7 @@ function remarkAimdInline(this: { data(): { toMarkdownExtensions?: unknown[] } }
|
|
|
55
56
|
handlers: {
|
|
56
57
|
// AIMD is structured source, not Markdown prose to escape or re-indent.
|
|
57
58
|
aimdField: (node: any) => String(node.value),
|
|
59
|
+
aimdLiteralTask: (node: any) => String(node.value),
|
|
58
60
|
},
|
|
59
61
|
})
|
|
60
62
|
function transformer(tree: any) {
|
|
@@ -71,37 +73,19 @@ function remarkAimdInline(this: { data(): { toMarkdownExtensions?: unknown[] } }
|
|
|
71
73
|
|
|
72
74
|
if (node.type === 'text' && typeof node.value === 'string') {
|
|
73
75
|
const value = normalizeAimdInlineTemplateMarkdownEscapes(node.value)
|
|
74
|
-
const
|
|
75
|
-
if (
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
data: {
|
|
88
|
-
hName: 'aimd-field',
|
|
89
|
-
hProperties: { fieldType: match.type, fieldContent: match.content },
|
|
90
|
-
},
|
|
91
|
-
fieldType: match.type,
|
|
92
|
-
fieldContent: match.content,
|
|
93
|
-
})
|
|
94
|
-
lastIndex = match.end
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (lastIndex < value.length) {
|
|
98
|
-
children.push({ type: 'text', value: value.slice(lastIndex) })
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (children.length > 0) {
|
|
102
|
-
// Replace this text node with the split children
|
|
103
|
-
node._aimdChildren = children
|
|
104
|
-
}
|
|
76
|
+
const parts = splitAimdInlineText(value)
|
|
77
|
+
if (!parts.length || (parts.length === 1 && parts[0] === node.value)) return
|
|
78
|
+
node._aimdChildren = parts.map(part => typeof part === 'string'
|
|
79
|
+
? { type: 'text', value: part }
|
|
80
|
+
: {
|
|
81
|
+
type: 'aimdField',
|
|
82
|
+
data: {
|
|
83
|
+
hName: 'aimd-field',
|
|
84
|
+
hProperties: { fieldType: part.type, fieldContent: part.content },
|
|
85
|
+
},
|
|
86
|
+
fieldType: part.type,
|
|
87
|
+
fieldContent: part.content,
|
|
88
|
+
})
|
|
105
89
|
}
|
|
106
90
|
|
|
107
91
|
// Recurse into children
|
|
@@ -470,9 +454,36 @@ export function createAimdAuthoringPlugins(options: () => AimdFieldViewOptions):
|
|
|
470
454
|
}
|
|
471
455
|
return new AimdFieldNodeView(node, view, getPos)
|
|
472
456
|
})
|
|
473
|
-
return [aimdRemarkPlugin, aimdFieldNode, fieldView, inlineHardbreakSchema, aimdFieldInputRule].flat()
|
|
457
|
+
return [aimdRemarkPlugin, aimdFieldNode, fieldView, literalTaskTextSchema, inlineHardbreakSchema, aimdFieldInputRule].flat()
|
|
474
458
|
}
|
|
475
459
|
|
|
460
|
+
// Real declarations are aimd_field nodes. Preserve task-shaped prose as a
|
|
461
|
+
// Markdown escape even when Milkdown serializes adjoining spaces verbatim.
|
|
462
|
+
const literalTaskTextSchema = $node('text', () => {
|
|
463
|
+
const schema = textSchema.schema
|
|
464
|
+
return {
|
|
465
|
+
...schema,
|
|
466
|
+
toMarkdown: {
|
|
467
|
+
...schema.toMarkdown,
|
|
468
|
+
runner(state, node) {
|
|
469
|
+
const value = node.text ?? ''
|
|
470
|
+
if (!value.includes('task')) return schema.toMarkdown.runner(state, node)
|
|
471
|
+
const tasks = findAimdInlineTemplates(value, { includeEscapedTasks: true }).filter(match => match.type === 'task')
|
|
472
|
+
if (!tasks.length) return schema.toMarkdown.runner(state, node)
|
|
473
|
+
let cursor = 0
|
|
474
|
+
for (const task of tasks) {
|
|
475
|
+
const slashes = task.escapeLength ?? 0
|
|
476
|
+
const prefix = value.slice(cursor, task.start - slashes)
|
|
477
|
+
if (prefix) state.addNode('text', undefined, prefix)
|
|
478
|
+
state.addNode('aimdLiteralTask', undefined, '\\'.repeat(2 * slashes + 1) + task.raw)
|
|
479
|
+
cursor = task.end
|
|
480
|
+
}
|
|
481
|
+
if (cursor < value.length) state.addNode('text', undefined, value.slice(cursor))
|
|
482
|
+
},
|
|
483
|
+
},
|
|
484
|
+
}
|
|
485
|
+
})
|
|
486
|
+
|
|
476
487
|
// ─── 5. Schema override: render inline hardbreak (`\n`) as <br> in WYSIWYG ───
|
|
477
488
|
// Use schema-level toDOM override for stable rendering across editor view init/order.
|
|
478
489
|
export const inlineHardbreakSchema = hardbreakSchema.extendSchema((prev) => {
|
|
@@ -489,12 +500,20 @@ export const inlineHardbreakSchema = hardbreakSchema.extendSchema((prev) => {
|
|
|
489
500
|
|
|
490
501
|
export const aimdFieldInputRule = $inputRule((ctx) => {
|
|
491
502
|
return new InputRule(
|
|
492
|
-
|
|
503
|
+
/(\\*)\{\{(\w+)\|([^}]*)\}\}$/,
|
|
493
504
|
(state, match, start, end) => {
|
|
494
|
-
const [, fieldType, fieldContent] = match
|
|
505
|
+
const [, slashes, fieldType, fieldContent] = match
|
|
506
|
+
if (fieldType === 'task' && slashes.length) {
|
|
507
|
+
const prefix = '\\'.repeat(Math.floor(slashes.length / 2))
|
|
508
|
+
const tr = state.tr.delete(start, end)
|
|
509
|
+
if (prefix) tr.insertText(prefix, start)
|
|
510
|
+
const pos = start + prefix.length
|
|
511
|
+
if (slashes.length % 2) return tr.insertText(match[0].slice(slashes.length), pos)
|
|
512
|
+
return tr.insert(pos, aimdFieldNode.type(ctx).create({ fieldType, fieldContent }))
|
|
513
|
+
}
|
|
495
514
|
const nodeType = aimdFieldNode.type(ctx)
|
|
496
515
|
const node = nodeType.create({ fieldType, fieldContent })
|
|
497
|
-
return state.tr.replaceRangeWith(start, end, node)
|
|
516
|
+
return state.tr.replaceRangeWith(start + slashes.length, end, node)
|
|
498
517
|
}
|
|
499
518
|
)
|
|
500
519
|
})
|
|
@@ -505,6 +524,7 @@ export const aimdMilkdownPlugins: MilkdownPlugin[] = [
|
|
|
505
524
|
aimdRemarkPlugin,
|
|
506
525
|
aimdFieldNode,
|
|
507
526
|
aimdFieldView,
|
|
527
|
+
literalTaskTextSchema,
|
|
508
528
|
inlineHardbreakSchema,
|
|
509
529
|
aimdFieldInputRule,
|
|
510
530
|
].flat()
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Node } from '@milkdown/kit/prose/model'
|
|
2
|
+
import { Plugin, PluginKey } from '@milkdown/kit/prose/state'
|
|
3
|
+
import { Decoration, DecorationSet } from '@milkdown/kit/prose/view'
|
|
4
|
+
import { $prose } from '@milkdown/kit/utils'
|
|
5
|
+
import { embeddedImages, imageControls, type EmbeddedImage, type ImageLabels } from './embeddedImages'
|
|
6
|
+
|
|
7
|
+
type Match = EmbeddedImage & { imageNode: boolean }
|
|
8
|
+
const key = new PluginKey<{ matches: Match[], expanded: Set<number>, decorations: DecorationSet }>('aimd-embedded-images')
|
|
9
|
+
|
|
10
|
+
// Unchanged ProseMirror nodes keep their identity across transactions.
|
|
11
|
+
const imageCache = new WeakMap<Node, EmbeddedImage[]>()
|
|
12
|
+
function scan(doc: Node) {
|
|
13
|
+
const matches: Match[] = []
|
|
14
|
+
doc.descendants((node, pos) => {
|
|
15
|
+
const imageNode = node.type.name === 'image'
|
|
16
|
+
const text = imageNode ? node.attrs.src : node.text
|
|
17
|
+
if (typeof text !== 'string') return
|
|
18
|
+
let images = imageCache.get(node)
|
|
19
|
+
if (!images) {
|
|
20
|
+
images = embeddedImages(text)
|
|
21
|
+
imageCache.set(node, images)
|
|
22
|
+
}
|
|
23
|
+
for (const image of images) {
|
|
24
|
+
matches.push({ ...image, imageNode, from: pos + (imageNode ? 0 : image.from), to: pos + (imageNode ? node.nodeSize : image.to) })
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
return matches
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function embeddedImagePlugin(labels: () => ImageLabels) {
|
|
31
|
+
function build(doc: Node, matches: Match[], expanded: Set<number>) {
|
|
32
|
+
const decorations: Decoration[] = []
|
|
33
|
+
for (const match of matches) {
|
|
34
|
+
const open = expanded.has(match.from)
|
|
35
|
+
if (!open && !match.imageNode)
|
|
36
|
+
decorations.push(Decoration.inline(match.from, match.to, { style: 'display:none' }))
|
|
37
|
+
decorations.push(Decoration.widget(match.imageNode ? match.to : match.from, (view) => {
|
|
38
|
+
const controls = imageControls(match, labels(), open, () => {
|
|
39
|
+
view.dispatch(view.state.tr.setMeta(key, match.from))
|
|
40
|
+
view.dom.querySelector<HTMLButtonElement>('[data-aimd-image-pos="' + match.from + '"] button')?.focus()
|
|
41
|
+
})
|
|
42
|
+
controls.dataset.aimdImagePos = String(match.from)
|
|
43
|
+
if (match.imageNode && open) {
|
|
44
|
+
const source = document.createElement('code')
|
|
45
|
+
source.className = 'aimd-embedded-image-raw'
|
|
46
|
+
source.textContent = match.uri
|
|
47
|
+
controls.append(source)
|
|
48
|
+
}
|
|
49
|
+
return controls
|
|
50
|
+
}, { side: match.imageNode ? 1 : -1, stopEvent: () => true, ignoreSelection: true }))
|
|
51
|
+
}
|
|
52
|
+
return { matches, expanded, decorations: DecorationSet.create(doc, decorations) }
|
|
53
|
+
}
|
|
54
|
+
return $prose(() => new Plugin({
|
|
55
|
+
key,
|
|
56
|
+
state: {
|
|
57
|
+
init: (_, state) => build(state.doc, scan(state.doc), new Set()),
|
|
58
|
+
apply(tr, previous) {
|
|
59
|
+
const toggle = tr.getMeta(key)
|
|
60
|
+
const matches = tr.docChanged ? scan(tr.doc) : previous.matches
|
|
61
|
+
const expanded = tr.docChanged
|
|
62
|
+
? new Set([...previous.expanded].map(pos => tr.mapping.map(pos)).filter(pos => matches.some(m => m.from === pos)))
|
|
63
|
+
: new Set(previous.expanded)
|
|
64
|
+
if (typeof toggle === 'number') {
|
|
65
|
+
if (expanded.has(toggle)) expanded.delete(toggle)
|
|
66
|
+
else expanded.add(toggle)
|
|
67
|
+
} else if (tr.selectionSet && tr.selection.empty) {
|
|
68
|
+
const inside = matches.find(m => !m.imageNode && m.from < tr.selection.from && tr.selection.from < m.to)
|
|
69
|
+
if (inside) expanded.add(inside.from)
|
|
70
|
+
}
|
|
71
|
+
if (!tr.docChanged && toggle === undefined && expanded.size === previous.expanded.size) return previous
|
|
72
|
+
return build(tr.doc, matches, expanded)
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
props: { decorations: state => key.getState(state)?.decorations },
|
|
76
|
+
}))
|
|
77
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { editor, IRange } from 'monaco-editor'
|
|
2
|
+
import { embeddedImages, imageControls, type EmbeddedImage, type ImageLabels } from './embeddedImages'
|
|
3
|
+
|
|
4
|
+
type FoldableEditor = editor.IStandaloneCodeEditor & { setHiddenAreas(ranges: IRange[], owner: object): void }
|
|
5
|
+
|
|
6
|
+
// Monaco folds logical lines. The zone keeps the Markdown/YAML on either side
|
|
7
|
+
// readable, while copy, search, edits and undo still address the original model.
|
|
8
|
+
export function installSourceImageFolds(editor: FoldableEditor, fallback: HTMLElement, labels: () => ImageLabels) {
|
|
9
|
+
const owner = {}
|
|
10
|
+
const expanded = editor.createDecorationsCollection()
|
|
11
|
+
const layer = document.createElement('div')
|
|
12
|
+
layer.className = 'aimd-source-image-layer'
|
|
13
|
+
const widget = { getId: () => 'aimd.embedded-images', getDomNode: () => layer, getPosition: () => null }
|
|
14
|
+
editor.addOverlayWidget(widget)
|
|
15
|
+
function positionLayer() {
|
|
16
|
+
const layout = editor.getLayoutInfo()
|
|
17
|
+
layer.style.left = layout.contentLeft + 'px'
|
|
18
|
+
layer.style.width = layout.contentWidth + 'px'
|
|
19
|
+
layer.style.height = layout.height + 'px'
|
|
20
|
+
}
|
|
21
|
+
positionLayer()
|
|
22
|
+
const layout = editor.onDidLayoutChange(positionLayer)
|
|
23
|
+
let zones: string[] = []
|
|
24
|
+
let previousRows = new Map<number, { text: string, images: EmbeddedImage[] }>()
|
|
25
|
+
const lineRange = (line: number): IRange => ({ startLineNumber: line, endLineNumber: line, startColumn: 1, endColumn: 1 })
|
|
26
|
+
function render(focusLine?: number) {
|
|
27
|
+
const model = editor.getModel()
|
|
28
|
+
const container = editor.getDomNode()
|
|
29
|
+
if (!model || !container) return
|
|
30
|
+
const open = new Set(expanded.getRanges().map(range => range.startLineNumber))
|
|
31
|
+
const rows: { line: number, text: string, images: EmbeddedImage[] }[] = []
|
|
32
|
+
for (let line = 1; line <= model.getLineCount(); line++) {
|
|
33
|
+
const text = model.getLineContent(line)
|
|
34
|
+
const previous = previousRows.get(line)
|
|
35
|
+
const images = previous?.text === text ? previous.images : embeddedImages(text)
|
|
36
|
+
if (images.length) rows.push({ line, text, images })
|
|
37
|
+
}
|
|
38
|
+
previousRows = new Map(rows.map(row => [row.line, row]))
|
|
39
|
+
if (!rows.length && !zones.length && !fallback.childElementCount) return
|
|
40
|
+
layer.replaceChildren()
|
|
41
|
+
const hidden = rows.filter(row => !open.has(row.line))
|
|
42
|
+
// Monaco requires one visible line. For an image-only document put the
|
|
43
|
+
// same summary beside the editor until the user opens a line.
|
|
44
|
+
const onlyImages = hidden.length === model.getLineCount()
|
|
45
|
+
fallback.hidden = !onlyImages
|
|
46
|
+
fallback.replaceChildren()
|
|
47
|
+
container.style.display = onlyImages ? 'none' : ''
|
|
48
|
+
editor.setHiddenAreas(onlyImages ? [] : hidden.map(row => lineRange(row.line)), owner)
|
|
49
|
+
editor.changeViewZones(accessor => {
|
|
50
|
+
for (const id of zones) accessor.removeZone(id)
|
|
51
|
+
zones = []
|
|
52
|
+
for (const { line, text, images } of rows) {
|
|
53
|
+
const row = document.createElement('div')
|
|
54
|
+
row.className = 'aimd-source-image-line'
|
|
55
|
+
row.dataset.imageLine = String(line)
|
|
56
|
+
let last = 0
|
|
57
|
+
for (const image of images) {
|
|
58
|
+
if (!open.has(line)) row.append(text.slice(last, image.from))
|
|
59
|
+
row.append(imageControls(image, labels(), open.has(line), () => {
|
|
60
|
+
if (open.has(line)) open.delete(line)
|
|
61
|
+
else open.add(line)
|
|
62
|
+
expanded.set([...open].map(n => ({ range: lineRange(n), options: {} })))
|
|
63
|
+
render(line)
|
|
64
|
+
}))
|
|
65
|
+
last = image.to
|
|
66
|
+
}
|
|
67
|
+
if (!open.has(line)) row.append(text.slice(last))
|
|
68
|
+
if (onlyImages) fallback.append(row)
|
|
69
|
+
else {
|
|
70
|
+
// Monaco marks its view-zone wrapper aria-hidden. Reserve the space
|
|
71
|
+
// there and put the interactive controls in its accessible overlay.
|
|
72
|
+
layer.append(row)
|
|
73
|
+
zones.push(accessor.addZone({
|
|
74
|
+
afterLineNumber: line - 1, heightInPx: 36, showInHiddenAreas: true,
|
|
75
|
+
domNode: document.createElement('div'),
|
|
76
|
+
onDomNodeTop(top) {
|
|
77
|
+
row.style.top = top + 'px'
|
|
78
|
+
row.hidden = top <= -36 || top >= editor.getLayoutInfo().height
|
|
79
|
+
},
|
|
80
|
+
}))
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
if (focusLine) {
|
|
85
|
+
editor.layout()
|
|
86
|
+
const root = onlyImages ? fallback : container
|
|
87
|
+
root.querySelector<HTMLButtonElement>(`[data-image-line="${focusLine}"] button`)?.focus({ preventScroll: true })
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
render()
|
|
91
|
+
const changes = editor.onDidChangeModelContent(event => {
|
|
92
|
+
if (event.isFlush) expanded.clear()
|
|
93
|
+
else if (event.changes.every(({ range, text }) =>
|
|
94
|
+
range.startLineNumber === range.endLineNumber && !/[\r\n]/.test(text)
|
|
95
|
+
&& !previousRows.has(range.startLineNumber)
|
|
96
|
+
&& !/data:image\//i.test(editor.getModel()!.getLineContent(range.startLineNumber)),
|
|
97
|
+
)) return
|
|
98
|
+
render()
|
|
99
|
+
})
|
|
100
|
+
const models = editor.onDidChangeModel(() => { expanded.clear(); render() })
|
|
101
|
+
return {
|
|
102
|
+
refresh: render,
|
|
103
|
+
dispose() {
|
|
104
|
+
layout.dispose()
|
|
105
|
+
editor.removeOverlayWidget(widget)
|
|
106
|
+
changes.dispose()
|
|
107
|
+
models.dispose()
|
|
108
|
+
expanded.clear()
|
|
109
|
+
editor.setHiddenAreas([], owner)
|
|
110
|
+
editor.changeViewZones(accessor => zones.forEach(id => accessor.removeZone(id)))
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ref, shallowRef, computed, watch, nextTick } from 'vue'
|
|
2
2
|
import { protectAimdInlineTemplates, restoreAimdInlineTemplates } from '@airalogy/aimd-core'
|
|
3
3
|
import { parseAndExtract } from '@airalogy/aimd-renderer'
|
|
4
|
-
import type
|
|
4
|
+
import { editorViewCtx, type Editor } from '@milkdown/kit/core'
|
|
5
|
+
import type { Node } from '@milkdown/kit/prose/model'
|
|
5
6
|
import { replaceAll, getMarkdown, insert } from '@milkdown/kit/utils'
|
|
6
7
|
import { insertTableCommand } from '@milkdown/kit/preset/gfm'
|
|
7
8
|
import { callCommand } from '@milkdown/kit/utils'
|
|
@@ -27,6 +28,12 @@ export function useEditorContent(options: UseEditorContentOptions) {
|
|
|
27
28
|
const milkdownEditorRef = shallowRef<Editor | null>(null)
|
|
28
29
|
|
|
29
30
|
let isSyncing = false
|
|
31
|
+
let syncedVisualDoc: Node | undefined
|
|
32
|
+
let syncedVisualSource = initialContent
|
|
33
|
+
|
|
34
|
+
function visualDoc(editor: Editor) {
|
|
35
|
+
return editor.action(ctx => ctx.get(editorViewCtx).state.doc)
|
|
36
|
+
}
|
|
30
37
|
|
|
31
38
|
function commitUserContent(nextContent: string) {
|
|
32
39
|
if (nextContent === content.value) {
|
|
@@ -57,7 +64,7 @@ export function useEditorContent(options: UseEditorContentOptions) {
|
|
|
57
64
|
}
|
|
58
65
|
|
|
59
66
|
function shouldReparseInsertedText(text: string): boolean {
|
|
60
|
-
return /\{\{(?:var_table|var|quiz|step|check|ref_step|ref_var|ref_fig|cite|fig)\|/.test(text)
|
|
67
|
+
return /\{\{(?:var_table|var|quiz|step|check|task|ref_step|ref_var|ref_fig|cite|fig)\|/.test(text)
|
|
61
68
|
|| /```(?:quiz|fig|assigner|connectors)\b/.test(text)
|
|
62
69
|
}
|
|
63
70
|
|
|
@@ -92,9 +99,13 @@ export function useEditorContent(options: UseEditorContentOptions) {
|
|
|
92
99
|
|
|
93
100
|
if (editorMode.value === 'wysiwyg' && milkdownEditorRef.value) {
|
|
94
101
|
try {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
102
|
+
// A visual-only interaction (such as expanding an image) must not
|
|
103
|
+
// reserialize the file on the way back to source mode.
|
|
104
|
+
if (syncedVisualDoc?.eq(visualDoc(milkdownEditorRef.value))) {
|
|
105
|
+
commitUserContent(syncedVisualSource)
|
|
106
|
+
} else {
|
|
107
|
+
const md = milkdownEditorRef.value.action(getMarkdown())
|
|
108
|
+
if (typeof md === 'string') commitUserContent(normalizeMilkdownMarkdown(md, content.value))
|
|
98
109
|
}
|
|
99
110
|
} catch {}
|
|
100
111
|
}
|
|
@@ -115,6 +126,8 @@ export function useEditorContent(options: UseEditorContentOptions) {
|
|
|
115
126
|
try {
|
|
116
127
|
isSyncing = true
|
|
117
128
|
milkdownEditorRef.value.action(replaceAll(toMilkdownMarkdown(content.value)))
|
|
129
|
+
syncedVisualDoc = visualDoc(milkdownEditorRef.value)
|
|
130
|
+
syncedVisualSource = content.value
|
|
118
131
|
isSyncing = false
|
|
119
132
|
} catch { isSyncing = false }
|
|
120
133
|
}
|
|
@@ -129,7 +142,11 @@ export function useEditorContent(options: UseEditorContentOptions) {
|
|
|
129
142
|
if (editorMode.value === 'source' && monacoEditor.value) {
|
|
130
143
|
monacoEditor.value.setValue(val)
|
|
131
144
|
} else if (editorMode.value === 'wysiwyg' && milkdownEditorRef.value) {
|
|
132
|
-
try {
|
|
145
|
+
try {
|
|
146
|
+
milkdownEditorRef.value.action(replaceAll(toMilkdownMarkdown(val)))
|
|
147
|
+
syncedVisualDoc = visualDoc(milkdownEditorRef.value)
|
|
148
|
+
syncedVisualSource = val
|
|
149
|
+
} catch {}
|
|
133
150
|
}
|
|
134
151
|
isSyncing = false
|
|
135
152
|
}
|
|
@@ -236,6 +253,8 @@ export function useEditorContent(options: UseEditorContentOptions) {
|
|
|
236
253
|
|
|
237
254
|
function onMilkdownReady(editor: Editor) {
|
|
238
255
|
milkdownEditorRef.value = editor
|
|
256
|
+
syncedVisualDoc = visualDoc(editor)
|
|
257
|
+
syncedVisualSource = content.value
|
|
239
258
|
}
|
|
240
259
|
|
|
241
260
|
return {
|