@airalogy/aimd-editor 1.12.0 → 1.13.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 +24 -0
- package/README.zh-CN.md +24 -0
- package/dist/AimdEditorTopBar.vue_vue_type_script_setup_true_lang-x8F65dCh.js +2277 -0
- package/dist/{AimdSourceEditor.vue_vue_type_script_setup_true_lang-CJJTlVZ7.js → AimdSourceEditor.vue_vue_type_script_setup_true_lang-m07PHAyw.js} +29 -27
- package/dist/{AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-Dme3UQEf.js → AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-8nbo78nG.js} +6118 -5822
- package/dist/aimd-editor.css +1 -1
- package/dist/authoring.js +15 -8
- package/dist/embedded.js +2 -2
- package/dist/fields-DZsN-S-N.js +51 -0
- package/dist/fig-DzERhLmG.js +219 -0
- package/dist/index.js +54 -45
- package/dist/{index-BqB8Xukp.js → lexer-Dj-IVtCT.js} +932 -1053
- package/dist/vue.js +28 -26
- package/dist/wysiwyg.js +1 -1
- package/package.json +4 -3
- package/src/__tests__/check-authoring.test.ts +46 -0
- package/src/__tests__/code-block-metadata.test.ts +40 -0
- package/src/__tests__/input-authoring.test.ts +93 -0
- package/src/__tests__/input-dialog-layout.test.ts +52 -0
- package/src/__tests__/input-toolbar.test.ts +88 -0
- package/src/__tests__/multiline-field-roundtrip.test.ts +70 -0
- package/src/__tests__/quoted-inline-roundtrip.test.ts +80 -0
- package/src/__tests__/step-authoring.test.ts +62 -0
- package/src/__tests__/type-picker.test.ts +48 -0
- package/src/authoring/fields.ts +86 -0
- package/src/authoring/index.ts +1 -0
- package/src/vue/AimdEditor.vue +112 -0
- package/src/vue/AimdEditorToolbar.vue +4 -1
- package/src/vue/AimdFieldDialog.vue +14 -9
- package/src/vue/AimdInputFieldDialog.vue +166 -0
- package/src/vue/AimdInputToolbar.vue +77 -0
- package/src/vue/AimdStepFieldDialog.vue +112 -0
- package/src/vue/AimdTypePicker.vue +42 -0
- package/src/vue/AimdWysiwygEditor.vue +24 -2
- package/src/vue/aimdInlineMarkdownNormalization.ts +37 -5
- package/src/vue/index.ts +2 -0
- package/src/vue/locales.ts +94 -7
- package/src/vue/milkdown-aimd-plugin.ts +148 -16
- package/src/vue/milkdown-code-block-metadata.ts +46 -0
- package/src/vue/types.ts +6 -4
- package/dist/AimdEditorTopBar.vue_vue_type_script_setup_true_lang-BXsn4cFX.js +0 -1171
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed, nextTick, ref, watch } from 'vue'
|
|
3
|
+
import { parseCheckContent, parseStepContent } from '@airalogy/aimd-core/parser'
|
|
4
|
+
import { AIMD_FIELD_ID_PATTERN, collectAimdFieldIds, createAimdFieldId, updateAimdCheckField, updateAimdStepField } from '../authoring/fields'
|
|
5
|
+
import type { AimdEditorMessages } from './locales'
|
|
6
|
+
|
|
7
|
+
const props = defineProps<{
|
|
8
|
+
visible: boolean
|
|
9
|
+
content: string
|
|
10
|
+
fieldContent?: string
|
|
11
|
+
fieldType?: 'step' | 'check'
|
|
12
|
+
reservedIds?: string[]
|
|
13
|
+
messages: AimdEditorMessages
|
|
14
|
+
}>()
|
|
15
|
+
const emit = defineEmits<{
|
|
16
|
+
(e: 'update:visible', value: boolean): void
|
|
17
|
+
(e: 'save', content: string): void
|
|
18
|
+
}>()
|
|
19
|
+
const dialog = ref<HTMLDialogElement>()
|
|
20
|
+
const nameInput = ref<HTMLInputElement>()
|
|
21
|
+
const id = ref('')
|
|
22
|
+
const title = ref('')
|
|
23
|
+
const level = ref(1)
|
|
24
|
+
const check = ref(true)
|
|
25
|
+
const duration = ref('')
|
|
26
|
+
const timer = ref('elapsed')
|
|
27
|
+
const error = ref('')
|
|
28
|
+
const advanced = ref(false)
|
|
29
|
+
const checkedMessage = ref('')
|
|
30
|
+
const isCheck = computed(() => props.fieldType === 'check')
|
|
31
|
+
const labels = computed(() => isCheck.value ? props.messages.checkField : props.messages.stepField)
|
|
32
|
+
|
|
33
|
+
watch(() => props.visible, async visible => {
|
|
34
|
+
if (!visible) { dialog.value?.close(); return }
|
|
35
|
+
const step = props.fieldContent && !isCheck.value ? parseStepContent(props.fieldContent) : undefined
|
|
36
|
+
const checkpoint = props.fieldContent && isCheck.value ? parseCheckContent(props.fieldContent) : undefined
|
|
37
|
+
id.value = (checkpoint ?? step)?.id ?? createAimdFieldId([...collectAimdFieldIds(props.content), ...(props.reservedIds ?? [])], isCheck.value ? 'check' : 'step')
|
|
38
|
+
title.value = (checkpoint ?? step)?.title ?? ''
|
|
39
|
+
checkedMessage.value = checkpoint?.checked_message ?? ''
|
|
40
|
+
level.value = step?.level ?? 1
|
|
41
|
+
check.value = step?.check ?? true
|
|
42
|
+
duration.value = String(step?.props.duration ?? '')
|
|
43
|
+
timer.value = step?.timer_mode ?? 'elapsed'
|
|
44
|
+
error.value = ''
|
|
45
|
+
advanced.value = false
|
|
46
|
+
await nextTick()
|
|
47
|
+
dialog.value?.showModal()
|
|
48
|
+
nameInput.value?.focus()
|
|
49
|
+
}, { immediate: true })
|
|
50
|
+
|
|
51
|
+
function save() {
|
|
52
|
+
const existing = new Set([...collectAimdFieldIds(props.content), ...(props.reservedIds ?? [])])
|
|
53
|
+
const nextId = id.value.trim() || createAimdFieldId(existing, isCheck.value ? 'check' : 'step')
|
|
54
|
+
if (!AIMD_FIELD_ID_PATTERN.test(nextId)) { error.value = props.messages.inputField.invalidId; advanced.value = true; return }
|
|
55
|
+
if (!props.fieldContent && existing.has(nextId)) { error.value = props.messages.inputField.duplicateId; advanced.value = true; return }
|
|
56
|
+
if (isCheck.value) {
|
|
57
|
+
emit('save', updateAimdCheckField(props.fieldContent, { id: nextId, title: title.value, checkedMessage: checkedMessage.value }))
|
|
58
|
+
emit('update:visible', false)
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
const content = updateAimdStepField(props.fieldContent, { id: nextId, title: title.value, level: level.value, check: check.value, duration: duration.value, timer: timer.value })
|
|
62
|
+
const step = parseStepContent(content)
|
|
63
|
+
if ((duration.value.trim() && step.estimated_duration_ms === undefined) || (timer.value !== 'elapsed' && !step.estimated_duration_ms)) {
|
|
64
|
+
error.value = props.messages.stepField.invalidDuration
|
|
65
|
+
advanced.value = true
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
emit('save', content)
|
|
69
|
+
emit('update:visible', false)
|
|
70
|
+
}
|
|
71
|
+
</script>
|
|
72
|
+
|
|
73
|
+
<template>
|
|
74
|
+
<Teleport to="body">
|
|
75
|
+
<dialog ref="dialog" class="aimd-step-dialog" :aria-label="fieldContent ? labels.edit : labels.insert" @close="emit('update:visible', false)" @cancel="emit('update:visible', false)">
|
|
76
|
+
<form @submit.prevent="save">
|
|
77
|
+
<h2>{{ fieldContent ? labels.edit : labels.insert }}</h2>
|
|
78
|
+
<label>{{ labels.name }}<input ref="nameInput" v-model="title" name="title" autocomplete="off"></label>
|
|
79
|
+
<label v-if="!isCheck" class="aimd-step-dialog__check"><input v-model="check" name="check" type="checkbox">{{ messages.stepField.check }}</label>
|
|
80
|
+
<details :open="advanced" @toggle="advanced = ($event.target as HTMLDetailsElement).open">
|
|
81
|
+
<summary>{{ messages.inputField.advanced }}</summary>
|
|
82
|
+
<label>{{ labels.id }}<input v-model="id" name="id" :readonly="!!fieldContent" autocomplete="off" spellcheck="false"></label>
|
|
83
|
+
<label v-if="isCheck">{{ messages.checkField.checkedMessage }}<input v-model="checkedMessage" name="checkedMessage"></label>
|
|
84
|
+
<template v-else>
|
|
85
|
+
<label>{{ messages.dialog.level }}<select v-model="level" name="level"><option v-for="n in 3" :key="n" :value="n">{{ n }}</option></select></label>
|
|
86
|
+
<label>{{ messages.stepField.duration }}<input v-model="duration" name="duration" placeholder="2m"></label>
|
|
87
|
+
<label>{{ messages.stepField.timer }}<select v-model="timer" name="timer"><option value="elapsed">{{ messages.stepField.elapsed }}</option><option value="countdown">{{ messages.stepField.countdown }}</option><option value="both">{{ messages.stepField.both }}</option></select></label>
|
|
88
|
+
</template>
|
|
89
|
+
</details>
|
|
90
|
+
<p v-if="error" role="alert">{{ error }}</p>
|
|
91
|
+
<footer><button type="button" @click="emit('update:visible', false)">{{ messages.common.cancel }}</button><button type="submit" class="aimd-step-dialog__save">{{ fieldContent ? messages.inputField.save : messages.common.insert }}</button></footer>
|
|
92
|
+
</form>
|
|
93
|
+
</dialog>
|
|
94
|
+
</Teleport>
|
|
95
|
+
</template>
|
|
96
|
+
|
|
97
|
+
<style scoped>
|
|
98
|
+
.aimd-step-dialog { position: fixed; inset: 0; margin: auto; width: min(420px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); box-sizing: border-box; padding: 20px; border: 1px solid #d1d5db; border-radius: 8px; background: white; color: #24292f; font: 14px/1.5 system-ui, sans-serif; overflow: auto; }
|
|
99
|
+
.aimd-step-dialog::backdrop { background: rgb(0 0 0 / 25%); }
|
|
100
|
+
h2 { font-size: 18px; margin: 0 0 16px; }
|
|
101
|
+
label { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; }
|
|
102
|
+
input, select { box-sizing: border-box; width: 100%; min-width: 0; padding: 8px; border: 1px solid #cbd5e1; border-radius: 4px; background: white; color: inherit; font: inherit; }
|
|
103
|
+
input[readonly] { background: #f6f8fa; color: #57606a; }
|
|
104
|
+
.aimd-step-dialog__check { flex-direction: row; align-items: center; }
|
|
105
|
+
input[type="checkbox"] { width: 16px; height: 16px; }
|
|
106
|
+
summary { cursor: pointer; padding: 8px 0; color: #57606a; }
|
|
107
|
+
footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
|
|
108
|
+
button { padding: 7px 16px; border: 1px solid #cbd5e1; border-radius: 4px; background: white; cursor: pointer; font: inherit; }
|
|
109
|
+
.aimd-step-dialog__save { background: #1769d2; border-color: #1769d2; color: white; }
|
|
110
|
+
p[role="alert"] { color: #b42318; }
|
|
111
|
+
:is(input, select, button, summary):focus-visible { outline: 2px solid #1769d2; outline-offset: 2px; }
|
|
112
|
+
</style>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { computed } from 'vue'
|
|
3
|
+
import { getAimdBuiltInTypeMetadata, getAimdBuiltInTypeNames, isAimdNullableType, unwrapAimdNullableType } from '@airalogy/aimd-core/utils'
|
|
4
|
+
import type { AimdEditorMessages } from './locales'
|
|
5
|
+
import type { AimdVarTypePresetOption } from './types'
|
|
6
|
+
|
|
7
|
+
const props = defineProps<{ modelValue: string; messages: AimdEditorMessages; varTypePlugins?: AimdVarTypePresetOption[] }>()
|
|
8
|
+
const emit = defineEmits<{ (e: 'update:modelValue', value: string): void }>()
|
|
9
|
+
const builtIns = getAimdBuiltInTypeNames()
|
|
10
|
+
function typeLabel(name: string) {
|
|
11
|
+
const title = getAimdBuiltInTypeMetadata(name)?.title
|
|
12
|
+
return title && title !== name ? `${title} (${name})` : name
|
|
13
|
+
}
|
|
14
|
+
const plugins = computed(() => [...new Map((props.varTypePlugins ?? []).map(option => [option.value, option])).values()].filter(option => !builtIns.includes(option.value)))
|
|
15
|
+
const selected = computed(() => {
|
|
16
|
+
const type = unwrapAimdNullableType(props.modelValue)
|
|
17
|
+
return getAimdBuiltInTypeMetadata(type)?.type_name ?? (plugins.value.some(option => option.value === type) ? type : '')
|
|
18
|
+
})
|
|
19
|
+
function selectType(event: Event) {
|
|
20
|
+
const value = (event.target as HTMLSelectElement).value
|
|
21
|
+
if (value) emit('update:modelValue', value + (isAimdNullableType(props.modelValue) ? ' | None' : ''))
|
|
22
|
+
}
|
|
23
|
+
</script>
|
|
24
|
+
|
|
25
|
+
<template>
|
|
26
|
+
<div class="aimd-type-picker">
|
|
27
|
+
<label>{{ messages.inputField.knownTypes }}
|
|
28
|
+
<select name="knownType" :value="selected" @change="selectType">
|
|
29
|
+
<option value="">{{ messages.inputField.customExpression }}</option>
|
|
30
|
+
<optgroup :label="messages.inputField.builtInTypes"><option v-for="name in builtIns" :key="name" :value="name">{{ typeLabel(name) }}</option></optgroup>
|
|
31
|
+
<optgroup v-if="plugins.length" :label="messages.inputField.pluginTypes"><option v-for="option in plugins" :key="option.value" :value="option.value">{{ option.label }} ({{ option.value }})</option></optgroup>
|
|
32
|
+
</select>
|
|
33
|
+
</label>
|
|
34
|
+
<label>{{ messages.dialog.customType }}<input name="type" :value="modelValue" :placeholder="messages.placeholders.type" spellcheck="false" @input="emit('update:modelValue', ($event.target as HTMLInputElement).value)"></label>
|
|
35
|
+
</div>
|
|
36
|
+
</template>
|
|
37
|
+
|
|
38
|
+
<style scoped>
|
|
39
|
+
label { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; }
|
|
40
|
+
input, select { box-sizing: border-box; width: 100%; min-width: 0; padding: 8px; border: 1px solid #cbd5e1; border-radius: 4px; background: white; color: inherit; font: inherit; }
|
|
41
|
+
:is(input, select):focus-visible { outline: 2px solid #1769d2; outline-offset: 2px; }
|
|
42
|
+
</style>
|
|
@@ -25,9 +25,10 @@ import { protectAimdInlineTemplates } from '@airalogy/aimd-core'
|
|
|
25
25
|
import '@milkdown/theme-nord/style.css'
|
|
26
26
|
import '@milkdown/kit/prose/tables/style/tables.css'
|
|
27
27
|
|
|
28
|
-
import { aimdMilkdownPlugins } from './milkdown-aimd-plugin'
|
|
28
|
+
import { aimdMilkdownPlugins, createAimdAuthoringPlugins, type AimdInputEditRequest } from './milkdown-aimd-plugin'
|
|
29
29
|
import { createProgrammaticMarkdownSyncGuard } from './programmaticMarkdownSyncGuard'
|
|
30
30
|
import { normalizeComparableAimdMarkdown } from './comparableAimdMarkdown'
|
|
31
|
+
import { codeBlockMetadataSchema } from './milkdown-code-block-metadata'
|
|
31
32
|
import type { AimdFieldType } from './types'
|
|
32
33
|
import type { AimdEditorMessages } from './locales'
|
|
33
34
|
|
|
@@ -40,18 +41,33 @@ const props = withDefaults(defineProps<{
|
|
|
40
41
|
resolvedMessages: AimdEditorMessages
|
|
41
42
|
localizedFieldTypes: AimdFieldType[]
|
|
42
43
|
milkdownPlugins?: MilkdownPlugin[]
|
|
44
|
+
fieldDisplay?: 'compact' | 'detailed'
|
|
45
|
+
editInputFields?: boolean
|
|
43
46
|
}>(), {
|
|
44
47
|
active: true,
|
|
45
48
|
readonly: false,
|
|
46
49
|
milkdownPlugins: undefined,
|
|
50
|
+
fieldDisplay: 'detailed',
|
|
51
|
+
editInputFields: false,
|
|
47
52
|
})
|
|
48
53
|
|
|
49
54
|
const emit = defineEmits<{
|
|
50
55
|
(e: 'markdown-updated', ctx: any, markdown: string, prev: string): void
|
|
51
56
|
(e: 'ready', editor: Editor): void
|
|
52
57
|
(e: 'open-aimd-dialog', type: string): void
|
|
58
|
+
(e: 'edit-input', request: AimdInputEditRequest): void
|
|
59
|
+
(e: 'edit-step', request: AimdInputEditRequest): void
|
|
60
|
+
(e: 'edit-check', request: AimdInputEditRequest): void
|
|
53
61
|
}>()
|
|
54
62
|
|
|
63
|
+
const authoringPlugins = createAimdAuthoringPlugins(() => ({
|
|
64
|
+
compact: props.fieldDisplay === 'compact',
|
|
65
|
+
messages: props.resolvedMessages,
|
|
66
|
+
edit: request => emit('edit-input', request),
|
|
67
|
+
editStep: request => emit('edit-step', request),
|
|
68
|
+
editCheck: request => emit('edit-check', request),
|
|
69
|
+
}))
|
|
70
|
+
|
|
55
71
|
function toMilkdownMarkdown(markdown: string): string {
|
|
56
72
|
return protectAimdInlineTemplates(markdown).content
|
|
57
73
|
}
|
|
@@ -280,6 +296,11 @@ const tableIcons = {
|
|
|
280
296
|
}
|
|
281
297
|
|
|
282
298
|
const milkdownEditorRef = shallowRef<Editor | null>(null)
|
|
299
|
+
watch(() => [props.fieldDisplay, props.resolvedMessages, props.readonly], () => {
|
|
300
|
+
milkdownEditorRef.value?.action(ctx => {
|
|
301
|
+
ctx.get(editorViewCtx).dom.dispatchEvent(new Event('aimd-field-display-change'))
|
|
302
|
+
})
|
|
303
|
+
}, { flush: 'post' })
|
|
283
304
|
let isSyncingContent = false
|
|
284
305
|
let lastKnownMarkdown = normalizeComparableAimdMarkdown(props.content)
|
|
285
306
|
const programmaticMarkdownSyncGuard = createProgrammaticMarkdownSyncGuard()
|
|
@@ -313,13 +334,14 @@ const MilkdownEditorInner = defineComponent({
|
|
|
313
334
|
})
|
|
314
335
|
})
|
|
315
336
|
.use(commonmark)
|
|
337
|
+
.use(codeBlockMetadataSchema)
|
|
316
338
|
.use(gfm)
|
|
317
339
|
.use(history)
|
|
318
340
|
.use(listener)
|
|
319
341
|
.use(clipboard)
|
|
320
342
|
.use(indent)
|
|
321
343
|
.use(trailing)
|
|
322
|
-
.use(innerProps.milkdownPlugins ?? aimdMilkdownPlugins)
|
|
344
|
+
.use(innerProps.milkdownPlugins ?? (props.editInputFields ? authoringPlugins : aimdMilkdownPlugins))
|
|
323
345
|
.use(tableBlock)
|
|
324
346
|
.use(placeholderPlugin)
|
|
325
347
|
.config((ctx) => {
|
|
@@ -1,11 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const MARKDOWN_ESCAPABLE_PATTERN =
|
|
1
|
+
import { findAimdInlineTemplates } from '@airalogy/aimd-core/parser'
|
|
2
|
+
|
|
3
|
+
const MARKDOWN_ESCAPABLE_PATTERN = /[\\!"#$%&'()*+,./:;<=>?@[\\\]^_`{|}~-]/
|
|
4
4
|
|
|
5
5
|
function normalizeSingleAimdInlineTemplate(template: string): string {
|
|
6
|
-
|
|
6
|
+
let result = ''
|
|
7
|
+
let quote: string | undefined
|
|
8
|
+
for (let index = 0; index < template.length; index++) {
|
|
9
|
+
const char = template[index]
|
|
10
|
+
if (quote) {
|
|
11
|
+
result += char
|
|
12
|
+
if (char === '\\' && index + 1 < template.length) result += template[++index]
|
|
13
|
+
else if (char === quote) quote = undefined
|
|
14
|
+
continue
|
|
15
|
+
}
|
|
16
|
+
if (char === '"' || char === "'") quote = char
|
|
17
|
+
if (char === '\\' && MARKDOWN_ESCAPABLE_PATTERN.test(template[index + 1] ?? '')) {
|
|
18
|
+
result += template[++index]
|
|
19
|
+
continue
|
|
20
|
+
}
|
|
21
|
+
// Older visual-editor drafts escaped leading indentation as character references.
|
|
22
|
+
if (char === '&' && /^[ \t]*$/.test(result.slice(result.lastIndexOf('\n') + 1))) {
|
|
23
|
+
const space = template.slice(index).match(/^&#(?:x0*20|0*32);/i)
|
|
24
|
+
if (space) {
|
|
25
|
+
result += ' '
|
|
26
|
+
index += space[0].length - 1
|
|
27
|
+
continue
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
result += char
|
|
31
|
+
}
|
|
32
|
+
return result
|
|
7
33
|
}
|
|
8
34
|
|
|
9
35
|
export function normalizeAimdInlineTemplateMarkdownEscapes(content: string): string {
|
|
10
|
-
|
|
36
|
+
let normalized = ''
|
|
37
|
+
let lastIndex = 0
|
|
38
|
+
for (const match of findAimdInlineTemplates(content, { allowMarkdownEscapedTypes: true })) {
|
|
39
|
+
normalized += content.slice(lastIndex, match.start) + normalizeSingleAimdInlineTemplate(match.raw)
|
|
40
|
+
lastIndex = match.end
|
|
41
|
+
}
|
|
42
|
+
return normalized + content.slice(lastIndex)
|
|
11
43
|
}
|
package/src/vue/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { default as AimdEditor } from './AimdEditor.vue'
|
|
2
|
+
export { default as AimdInputFieldDialog } from './AimdInputFieldDialog.vue'
|
|
3
|
+
export { default as AimdStepFieldDialog } from './AimdStepFieldDialog.vue'
|
|
2
4
|
export { default as AimdEditorToolbar } from './AimdEditorToolbar.vue'
|
|
3
5
|
export { default as AimdEditorTopBar } from './AimdEditorTopBar.vue'
|
|
4
6
|
export { default as AimdSourceEditor } from './AimdSourceEditor.vue'
|
package/src/vue/locales.ts
CHANGED
|
@@ -13,6 +13,53 @@ type DeepPartial<T> = {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export interface AimdEditorMessages {
|
|
16
|
+
checkField: { insert: string; edit: string; name: string; id: string; checkedMessage: string }
|
|
17
|
+
stepField: {
|
|
18
|
+
insert: string
|
|
19
|
+
edit: string
|
|
20
|
+
name: string
|
|
21
|
+
id: string
|
|
22
|
+
check: string
|
|
23
|
+
duration: string
|
|
24
|
+
timer: string
|
|
25
|
+
elapsed: string
|
|
26
|
+
countdown: string
|
|
27
|
+
both: string
|
|
28
|
+
invalidDuration: string
|
|
29
|
+
}
|
|
30
|
+
inputField: {
|
|
31
|
+
insert: string
|
|
32
|
+
edit: string
|
|
33
|
+
save: string
|
|
34
|
+
name: string
|
|
35
|
+
advanced: string
|
|
36
|
+
id: string
|
|
37
|
+
invalidId: string
|
|
38
|
+
duplicateId: string
|
|
39
|
+
invalidOptions: string
|
|
40
|
+
options: string
|
|
41
|
+
text: string
|
|
42
|
+
integer: string
|
|
43
|
+
number: string
|
|
44
|
+
choice: string
|
|
45
|
+
multipleChoice: string
|
|
46
|
+
boolean: string
|
|
47
|
+
checkbox: string
|
|
48
|
+
date: string
|
|
49
|
+
time: string
|
|
50
|
+
datetime: string
|
|
51
|
+
list: string
|
|
52
|
+
custom: string
|
|
53
|
+
knownTypes: string
|
|
54
|
+
builtInTypes: string
|
|
55
|
+
pluginTypes: string
|
|
56
|
+
customExpression: string
|
|
57
|
+
details: string
|
|
58
|
+
more: string
|
|
59
|
+
file: string
|
|
60
|
+
fileFormat: string
|
|
61
|
+
insertType: (label: string) => string
|
|
62
|
+
}
|
|
16
63
|
mode: {
|
|
17
64
|
source: string
|
|
18
65
|
sourceTitle: string
|
|
@@ -234,10 +281,30 @@ function detectRuntimeLocale(): string | undefined {
|
|
|
234
281
|
}
|
|
235
282
|
|
|
236
283
|
const EN_US_MESSAGES: AimdEditorMessages = {
|
|
284
|
+
checkField: { insert: 'Insert checkpoint', edit: 'Checkpoint settings', name: 'Checkpoint name (optional)', id: 'Checkpoint ID', checkedMessage: 'Message after checking (optional)' },
|
|
285
|
+
stepField: {
|
|
286
|
+
insert: 'Insert step', edit: 'Step settings', name: 'Step name (optional)',
|
|
287
|
+
id: 'Step ID', check: 'Completion checkbox', duration: 'Estimated duration', timer: 'Timer display',
|
|
288
|
+
elapsed: 'Elapsed', countdown: 'Countdown', both: 'Both', invalidDuration: 'Enter a duration such as 30s or 2m. Countdown requires a duration.',
|
|
289
|
+
},
|
|
290
|
+
inputField: {
|
|
291
|
+
insert: 'Insert input', edit: 'Input settings', save: 'Save', name: 'Label (optional)',
|
|
292
|
+
advanced: 'Advanced settings', id: 'Field ID (generated automatically)',
|
|
293
|
+
invalidId: 'Use letters, numbers and underscores; start with a letter or underscore.',
|
|
294
|
+
duplicateId: 'This ID is already in use.', invalidOptions: 'Enter at least one distinct option.',
|
|
295
|
+
options: 'Choices (one per line)', text: 'Text', integer: 'Integer', number: 'Decimal',
|
|
296
|
+
multipleChoice: 'Multiple choice', more: 'More inputs', file: 'Attachment', insertType: label => `Insert ${label.toLowerCase()} input`,
|
|
297
|
+
checkbox: 'Checkbox',
|
|
298
|
+
fileFormat: 'File format',
|
|
299
|
+
time: 'Time', datetime: 'Date and time',
|
|
300
|
+
choice: 'Single choice', boolean: 'Yes / no', date: 'Date', list: 'Text list', custom: 'Advanced / custom',
|
|
301
|
+
knownTypes: 'Available types', builtInTypes: 'Airalogy built-in types', pluginTypes: 'Additional types', customExpression: 'Custom type expression',
|
|
302
|
+
details: 'Show field details',
|
|
303
|
+
},
|
|
237
304
|
mode: {
|
|
238
305
|
source: 'Source',
|
|
239
306
|
sourceTitle: 'Source Mode',
|
|
240
|
-
wysiwyg: '
|
|
307
|
+
wysiwyg: 'Visual',
|
|
241
308
|
wysiwygTitle: 'WYSIWYG Mode',
|
|
242
309
|
},
|
|
243
310
|
common: {
|
|
@@ -250,8 +317,8 @@ const EN_US_MESSAGES: AimdEditorMessages = {
|
|
|
250
317
|
available: 'Available',
|
|
251
318
|
},
|
|
252
319
|
fieldTypes: {
|
|
253
|
-
var: { label: '
|
|
254
|
-
var_table: { label: '
|
|
320
|
+
var: { label: 'Input', desc: 'Insert an input field' },
|
|
321
|
+
var_table: { label: 'Data table', desc: 'Insert a table for repeated entries' },
|
|
255
322
|
quiz: { label: 'Quiz', desc: 'Define a quiz item' },
|
|
256
323
|
step: { label: 'Step', desc: 'Define a step' },
|
|
257
324
|
check: { label: 'Checkpoint', desc: 'Define a checkpoint' },
|
|
@@ -437,10 +504,30 @@ const EN_US_MESSAGES: AimdEditorMessages = {
|
|
|
437
504
|
}
|
|
438
505
|
|
|
439
506
|
const ZH_CN_MESSAGES: AimdEditorMessages = {
|
|
507
|
+
checkField: { insert: '插入检查点', edit: '检查点设置', name: '检查点名称(选填)', id: '检查点 ID', checkedMessage: '勾选后提示(选填)' },
|
|
508
|
+
stepField: {
|
|
509
|
+
insert: '插入步骤', edit: '步骤设置', name: '步骤名称(选填)',
|
|
510
|
+
id: '步骤 ID', check: '完成复选框', duration: '预计时长', timer: '计时显示',
|
|
511
|
+
elapsed: '正计时', countdown: '倒计时', both: '同时显示', invalidDuration: '请输入 30s 或 2m 等时长。倒计时需要预计时长。',
|
|
512
|
+
},
|
|
513
|
+
inputField: {
|
|
514
|
+
insert: '插入输入框', edit: '输入框设置', save: '保存', name: '名称(选填)',
|
|
515
|
+
advanced: '高级设置', id: '字段 ID(自动生成)',
|
|
516
|
+
invalidId: '请使用字母、数字和下划线,并以字母或下划线开头。',
|
|
517
|
+
duplicateId: '此 ID 已被使用。', invalidOptions: '请至少填写一个选项,且选项不能重复。',
|
|
518
|
+
options: '选项(每行一个)', text: '文本', integer: '整数', number: '小数',
|
|
519
|
+
multipleChoice: '多选', more: '更多输入框', file: '附件', insertType: label => `插入${label}输入框`,
|
|
520
|
+
checkbox: '复选框',
|
|
521
|
+
fileFormat: '文件格式',
|
|
522
|
+
time: '时间', datetime: '日期时间',
|
|
523
|
+
choice: '单选', boolean: '是/否', date: '日期', list: '文本列表', custom: '高级 / 自定义',
|
|
524
|
+
knownTypes: '已有类型', builtInTypes: 'Airalogy 内置类型', pluginTypes: '扩展类型', customExpression: '自定义类型表达式',
|
|
525
|
+
details: '显示字段信息',
|
|
526
|
+
},
|
|
440
527
|
mode: {
|
|
441
528
|
source: '源码',
|
|
442
529
|
sourceTitle: '源码模式',
|
|
443
|
-
wysiwyg: '
|
|
530
|
+
wysiwyg: '可视化',
|
|
444
531
|
wysiwygTitle: '所见即所得模式',
|
|
445
532
|
},
|
|
446
533
|
common: {
|
|
@@ -453,13 +540,13 @@ const ZH_CN_MESSAGES: AimdEditorMessages = {
|
|
|
453
540
|
available: '可用项',
|
|
454
541
|
},
|
|
455
542
|
fieldTypes: {
|
|
456
|
-
var: { label: '
|
|
457
|
-
var_table: { label: '
|
|
543
|
+
var: { label: '输入框', desc: '插入输入框' },
|
|
544
|
+
var_table: { label: '数据表', desc: '插入可重复填写的数据表' },
|
|
458
545
|
quiz: { label: '题目', desc: '定义一个题目块' },
|
|
459
546
|
step: { label: '步骤', desc: '定义一个步骤' },
|
|
460
547
|
check: { label: '检查点', desc: '定义一个检查点' },
|
|
461
548
|
ref_step: { label: '引用步骤', desc: '引用已定义的步骤' },
|
|
462
|
-
ref_var: { label: '
|
|
549
|
+
ref_var: { label: '引用输入框', desc: '引用已定义的输入框' },
|
|
463
550
|
ref_fig: { label: '引用图片', desc: '引用已定义的图片' },
|
|
464
551
|
cite: { label: '引用文献', desc: '插入文献引用' },
|
|
465
552
|
},
|
|
@@ -12,9 +12,12 @@ 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'
|
|
16
|
+
import type { AimdEditorMessages } from './locales'
|
|
15
17
|
import { hardbreakAttr, hardbreakSchema } from '@milkdown/kit/preset/commonmark'
|
|
16
18
|
import { $node, $view, $remark, $inputRule } from '@milkdown/kit/utils'
|
|
17
19
|
import { InputRule } from '@milkdown/kit/prose/inputrules'
|
|
20
|
+
import { normalizeAimdInlineTemplateMarkdownEscapes } from './aimdInlineMarkdownNormalization'
|
|
18
21
|
|
|
19
22
|
// ─── AIMD field type colors (matches types.ts) ───
|
|
20
23
|
const AIMD_COLORS: Record<string, string> = {
|
|
@@ -45,9 +48,15 @@ function getColor(fieldType: string): string {
|
|
|
45
48
|
|
|
46
49
|
// ─── 1. Remark plugin: parse {{type|content}} into custom MDAST nodes ───
|
|
47
50
|
|
|
48
|
-
function remarkAimdInline() {
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
+
function remarkAimdInline(this: { data(): { toMarkdownExtensions?: unknown[] } }) {
|
|
52
|
+
const data = this.data()
|
|
53
|
+
const extensions = (data.toMarkdownExtensions ??= [])
|
|
54
|
+
extensions.push({
|
|
55
|
+
handlers: {
|
|
56
|
+
// AIMD is structured source, not Markdown prose to escape or re-indent.
|
|
57
|
+
aimdField: (node: any) => String(node.value),
|
|
58
|
+
},
|
|
59
|
+
})
|
|
51
60
|
function transformer(tree: any) {
|
|
52
61
|
visitNode(tree)
|
|
53
62
|
}
|
|
@@ -61,30 +70,28 @@ function remarkAimdInline() {
|
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
if (node.type === 'text' && typeof node.value === 'string') {
|
|
64
|
-
const value
|
|
65
|
-
|
|
66
|
-
if (
|
|
73
|
+
const value = normalizeAimdInlineTemplateMarkdownEscapes(node.value)
|
|
74
|
+
const matches = findAimdInlineTemplates(value)
|
|
75
|
+
if (matches.length === 0) return
|
|
67
76
|
|
|
68
77
|
// Split text around AIMD fields
|
|
69
|
-
AIMD_RE.lastIndex = 0
|
|
70
78
|
const children: any[] = []
|
|
71
79
|
let lastIndex = 0
|
|
72
|
-
let match: RegExpExecArray | null
|
|
73
80
|
|
|
74
|
-
|
|
75
|
-
if (match.
|
|
76
|
-
children.push({ type: 'text', value: value.slice(lastIndex, match.
|
|
81
|
+
for (const match of matches) {
|
|
82
|
+
if (match.start > lastIndex) {
|
|
83
|
+
children.push({ type: 'text', value: value.slice(lastIndex, match.start) })
|
|
77
84
|
}
|
|
78
85
|
children.push({
|
|
79
86
|
type: 'aimdField',
|
|
80
87
|
data: {
|
|
81
88
|
hName: 'aimd-field',
|
|
82
|
-
hProperties: { fieldType: match
|
|
89
|
+
hProperties: { fieldType: match.type, fieldContent: match.content },
|
|
83
90
|
},
|
|
84
|
-
fieldType: match
|
|
85
|
-
fieldContent: match
|
|
91
|
+
fieldType: match.type,
|
|
92
|
+
fieldContent: match.content,
|
|
86
93
|
})
|
|
87
|
-
lastIndex = match.
|
|
94
|
+
lastIndex = match.end
|
|
88
95
|
}
|
|
89
96
|
|
|
90
97
|
if (lastIndex < value.length) {
|
|
@@ -154,7 +161,7 @@ export const aimdFieldNode = $node('aimd_field', () => ({
|
|
|
154
161
|
toMarkdown: {
|
|
155
162
|
match: (node: ProsemirrorNode) => node.type.name === 'aimd_field',
|
|
156
163
|
runner: (state, node) => {
|
|
157
|
-
state.addNode('
|
|
164
|
+
state.addNode('aimdField', undefined, `{{${node.attrs.fieldType}|${node.attrs.fieldContent}}}`)
|
|
158
165
|
},
|
|
159
166
|
},
|
|
160
167
|
} as NodeSchema))
|
|
@@ -341,6 +348,131 @@ export const aimdFieldView = $view(aimdFieldNode, () => {
|
|
|
341
348
|
return (node, view, getPos) => new AimdFieldNodeView(node, view, getPos)
|
|
342
349
|
})
|
|
343
350
|
|
|
351
|
+
export interface AimdInputEditRequest {
|
|
352
|
+
content: string
|
|
353
|
+
apply: (content: string) => void
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
interface AimdFieldViewOptions {
|
|
357
|
+
compact: boolean
|
|
358
|
+
messages: AimdEditorMessages
|
|
359
|
+
edit: (request: AimdInputEditRequest) => void
|
|
360
|
+
editStep?: (request: AimdInputEditRequest) => void
|
|
361
|
+
editCheck?: (request: AimdInputEditRequest) => void
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
class AimdInputNodeView implements NodeView {
|
|
365
|
+
dom: HTMLButtonElement
|
|
366
|
+
private refresh = () => {
|
|
367
|
+
const { compact, messages } = this.options()
|
|
368
|
+
if (this.node.attrs.fieldType === 'step' || this.node.attrs.fieldType === 'check') {
|
|
369
|
+
const isCheck = this.node.attrs.fieldType === 'check'
|
|
370
|
+
const step = (isCheck ? parseCheckContent : parseStepContent)(this.node.attrs.fieldContent)
|
|
371
|
+
const labels = isCheck ? messages.checkField : messages.stepField
|
|
372
|
+
this.dom.replaceChildren()
|
|
373
|
+
const name = document.createElement('span')
|
|
374
|
+
name.textContent = `${messages.fieldTypes[isCheck ? 'check' : 'step'].label}${step.title ? `: ${step.title}` : ''}`
|
|
375
|
+
this.dom.append(name)
|
|
376
|
+
if (!compact) {
|
|
377
|
+
const key = document.createElement('small')
|
|
378
|
+
key.textContent = step.id
|
|
379
|
+
this.dom.append(key)
|
|
380
|
+
}
|
|
381
|
+
this.dom.className = `aimd-input-chip ${isCheck ? 'aimd-check-chip' : 'aimd-step-chip'}`
|
|
382
|
+
this.dom.title = labels.edit
|
|
383
|
+
this.dom.setAttribute('aria-label', `${labels.edit}: ${step.title || step.id}`)
|
|
384
|
+
this.dom.disabled = !this.view.editable
|
|
385
|
+
return
|
|
386
|
+
}
|
|
387
|
+
const definition = parseVarDefinition(this.node.attrs.fieldContent)
|
|
388
|
+
const label = typeof definition.kwargs?.title === 'string' ? definition.kwargs.title : messages.fieldTypes.var.label
|
|
389
|
+
this.dom.textContent = compact ? label : `var | ${this.node.attrs.fieldContent}`
|
|
390
|
+
this.dom.className = `aimd-input-chip${compact ? '' : ' aimd-input-chip--detailed'}`
|
|
391
|
+
this.dom.title = messages.inputField.edit
|
|
392
|
+
this.dom.setAttribute('aria-label', `${messages.inputField.edit}: ${label}`)
|
|
393
|
+
this.dom.disabled = !this.view.editable
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
constructor(private node: ProsemirrorNode, private view: EditorView, private getPos: () => number | undefined, private options: () => AimdFieldViewOptions) {
|
|
397
|
+
this.dom = document.createElement('button')
|
|
398
|
+
this.dom.type = 'button'
|
|
399
|
+
this.dom.contentEditable = 'false'
|
|
400
|
+
this.dom.dataset.fieldType = this.node.attrs.fieldType
|
|
401
|
+
this.refresh()
|
|
402
|
+
this.view.dom.addEventListener('aimd-field-display-change', this.refresh)
|
|
403
|
+
this.dom.addEventListener('click', event => {
|
|
404
|
+
event.preventDefault()
|
|
405
|
+
if (!this.view.editable) return
|
|
406
|
+
const isStep = this.node.attrs.fieldType === 'step'
|
|
407
|
+
const isCheck = this.node.attrs.fieldType === 'check'
|
|
408
|
+
const edit = isCheck ? this.options().editCheck : isStep ? this.options().editStep : this.options().edit
|
|
409
|
+
edit?.({
|
|
410
|
+
content: this.node.attrs.fieldContent,
|
|
411
|
+
apply: content => {
|
|
412
|
+
const pos = this.getPos()
|
|
413
|
+
if (pos === undefined || !this.view.editable) return
|
|
414
|
+
const current = this.view.state.doc.nodeAt(pos)
|
|
415
|
+
if (!current || !current.eq(this.node)) return
|
|
416
|
+
const parse = isCheck ? parseCheckContent : isStep ? parseStepContent : parseVarDefinition
|
|
417
|
+
const oldId = parse(this.node.attrs.fieldContent).id
|
|
418
|
+
const newId = parse(content).id
|
|
419
|
+
const tr = this.view.state.tr.setNodeMarkup(pos, undefined, { ...this.node.attrs, fieldContent: content })
|
|
420
|
+
if (!isCheck && oldId !== newId) {
|
|
421
|
+
this.view.state.doc.descendants((child, childPos) => {
|
|
422
|
+
if (child.type.name === 'aimd_field' && child.attrs.fieldType === (isStep ? 'ref_step' : 'ref_var') && child.attrs.fieldContent.trim() === oldId) {
|
|
423
|
+
tr.setNodeMarkup(childPos, undefined, { ...child.attrs, fieldContent: newId })
|
|
424
|
+
}
|
|
425
|
+
})
|
|
426
|
+
}
|
|
427
|
+
this.view.dispatch(tr)
|
|
428
|
+
this.view.focus()
|
|
429
|
+
},
|
|
430
|
+
})
|
|
431
|
+
})
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
update(node: ProsemirrorNode): boolean {
|
|
435
|
+
if (node.type.name !== 'aimd_field' || node.attrs.fieldType !== this.node.attrs.fieldType || (node.attrs.fieldType === 'var' && parseVarDefinition(node.attrs.fieldContent).subvars)) return false
|
|
436
|
+
if (node.attrs.fieldType === 'step' || node.attrs.fieldType === 'check') {
|
|
437
|
+
try {
|
|
438
|
+
(node.attrs.fieldType === 'check' ? parseCheckContent : parseStepContent)(node.attrs.fieldContent)
|
|
439
|
+
}
|
|
440
|
+
catch {
|
|
441
|
+
// Let the raw node view represent an incomplete source edit.
|
|
442
|
+
return false
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
this.node = node
|
|
446
|
+
this.refresh()
|
|
447
|
+
return true
|
|
448
|
+
}
|
|
449
|
+
stopEvent() { return true }
|
|
450
|
+
ignoreMutation() { return true }
|
|
451
|
+
destroy() { this.view.dom.removeEventListener('aimd-field-display-change', this.refresh) }
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
export function createAimdAuthoringPlugins(options: () => AimdFieldViewOptions): MilkdownPlugin[] {
|
|
455
|
+
const fieldView = $view(aimdFieldNode, () => (node, view, getPos) => {
|
|
456
|
+
try {
|
|
457
|
+
if (node.attrs.fieldType === 'step' && options().editStep) {
|
|
458
|
+
parseStepContent(node.attrs.fieldContent)
|
|
459
|
+
return new AimdInputNodeView(node, view, getPos, options)
|
|
460
|
+
}
|
|
461
|
+
if (node.attrs.fieldType === 'check' && options().editCheck) {
|
|
462
|
+
parseCheckContent(node.attrs.fieldContent)
|
|
463
|
+
return new AimdInputNodeView(node, view, getPos, options)
|
|
464
|
+
}
|
|
465
|
+
if (node.attrs.fieldType === 'var' && !parseVarDefinition(node.attrs.fieldContent).subvars) {
|
|
466
|
+
return new AimdInputNodeView(node, view, getPos, options)
|
|
467
|
+
}
|
|
468
|
+
} catch {
|
|
469
|
+
// Keep invalid drafts editable as raw syntax rather than breaking the whole editor.
|
|
470
|
+
}
|
|
471
|
+
return new AimdFieldNodeView(node, view, getPos)
|
|
472
|
+
})
|
|
473
|
+
return [aimdRemarkPlugin, aimdFieldNode, fieldView, inlineHardbreakSchema, aimdFieldInputRule].flat()
|
|
474
|
+
}
|
|
475
|
+
|
|
344
476
|
// ─── 5. Schema override: render inline hardbreak (`\n`) as <br> in WYSIWYG ───
|
|
345
477
|
// Use schema-level toDOM override for stable rendering across editor view init/order.
|
|
346
478
|
export const inlineHardbreakSchema = hardbreakSchema.extendSchema((prev) => {
|