@airalogy/aimd-editor 1.13.0 → 1.14.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 +1 -1
- package/README.zh-CN.md +1 -1
- package/dist/AimdEditorTopBar.vue_vue_type_script_setup_true_lang-DxnCT_yh.js +2352 -0
- package/dist/{AimdSourceEditor.vue_vue_type_script_setup_true_lang-BO9WFdXW.js → AimdSourceEditor.vue_vue_type_script_setup_true_lang-ey9GBO3X.js} +2 -2
- package/dist/{AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-ZL9sYztW.js → AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-BR8KCFMg.js} +3371 -3319
- package/dist/aimd-editor.css +1 -1
- package/dist/authoring.js +2 -2
- package/dist/embedded.js +2 -2
- package/dist/{fields-XXsn5a8D.js → fields-CC93ner-.js} +2 -2
- package/dist/{fig-C5iwVPzh.js → fig-oCdVp2EF.js} +1 -1
- package/dist/index.js +5 -5
- package/dist/{lexer-CgImjRcS.js → lexer-DKU2sEoR.js} +805 -789
- package/dist/vue.js +3 -3
- package/dist/wysiwyg.js +1 -1
- package/package.json +2 -2
- package/src/__tests__/code-block-metadata.test.ts +40 -0
- package/src/__tests__/input-authoring.test.ts +56 -0
- package/src/__tests__/input-toolbar.test.ts +2 -2
- package/src/authoring/fields.ts +2 -1
- package/src/vue/AimdInputFieldDialog.vue +99 -11
- package/src/vue/AimdWysiwygEditor.vue +2 -0
- package/src/vue/locales.ts +12 -0
- package/src/vue/milkdown-code-block-metadata.ts +46 -0
- package/dist/AimdEditorTopBar.vue_vue_type_script_setup_true_lang-DF2y70yv.js +0 -2277
package/dist/vue.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { _ as d, a as e, b as s, c as o, A as r, d as t, u as m } from "./AimdEditorTopBar.vue_vue_type_script_setup_true_lang-
|
|
2
|
-
import { _ as l } from "./AimdSourceEditor.vue_vue_type_script_setup_true_lang-
|
|
3
|
-
import { A as I, a as D, _, D as T, M as F, b as u, c as M, d as g, e as n, f as c, g as p, h as L, i as O, j as S, k as y, l as b, m as f, n as x, r as N } from "./AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-
|
|
1
|
+
import { _ as d, a as e, b as s, c as o, A as r, d as t, u as m } from "./AimdEditorTopBar.vue_vue_type_script_setup_true_lang-DxnCT_yh.js";
|
|
2
|
+
import { _ as l } from "./AimdSourceEditor.vue_vue_type_script_setup_true_lang-ey9GBO3X.js";
|
|
3
|
+
import { A as I, a as D, _, D as T, M as F, b as u, c as M, d as g, e as n, f as c, g as p, h as L, i as O, j as S, k as y, l as b, m as f, n as x, r as N } from "./AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-BR8KCFMg.js";
|
|
4
4
|
export {
|
|
5
5
|
I as AIMD_FIELD_TYPES,
|
|
6
6
|
D as AIMD_FIELD_TYPE_DEFINITIONS,
|
package/dist/wysiwyg.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _ as a, D as r, i, j as d, k as t, r as A } from "./AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-
|
|
1
|
+
import { _ as a, D as r, i, j as d, k as t, r as A } from "./AimdWysiwygEditor.vue_vue_type_script_setup_true_lang-BR8KCFMg.js";
|
|
2
2
|
export {
|
|
3
3
|
a as AimdWysiwygEditor,
|
|
4
4
|
r as DEFAULT_AIMD_EDITOR_LOCALE,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@airalogy/aimd-editor",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.14.0",
|
|
5
5
|
"description": "AIMD (Airalogy Markdown) editor with Monaco source mode and Milkdown WYSIWYG mode",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"repository": {
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
"@milkdown/kit": "^7.18.0",
|
|
65
65
|
"@milkdown/theme-nord": "^7.18.0",
|
|
66
66
|
"@milkdown/vue": "^7.18.0",
|
|
67
|
-
"@airalogy/aimd-core": "^2.
|
|
67
|
+
"@airalogy/aimd-core": "^2.18.1",
|
|
68
68
|
"@airalogy/aimd-renderer": "^2.14.0"
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { afterEach, expect, it } from 'vitest'
|
|
2
|
+
import { Editor, defaultValueCtx, editorViewCtx, rootCtx } from '@milkdown/kit/core'
|
|
3
|
+
import { commonmark } from '@milkdown/kit/preset/commonmark'
|
|
4
|
+
import { DOMParser, DOMSerializer } from '@milkdown/kit/prose/model'
|
|
5
|
+
import { getMarkdown, replaceAll } from '@milkdown/kit/utils'
|
|
6
|
+
import { codeBlockMetadataSchema } from '../vue/milkdown-code-block-metadata'
|
|
7
|
+
|
|
8
|
+
const editors: Editor[] = []
|
|
9
|
+
afterEach(async () => {
|
|
10
|
+
for (const editor of editors.splice(0)) await editor.destroy()
|
|
11
|
+
document.body.replaceChildren()
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it.each([
|
|
15
|
+
['assigner runtime=client', 'runtime=client'],
|
|
16
|
+
['python title="original.py" linenos', 'title="original.py" linenos'],
|
|
17
|
+
['fig', ''],
|
|
18
|
+
['', ''],
|
|
19
|
+
])('preserves fence information %s through visual edits and DOM copies', async (info, meta) => {
|
|
20
|
+
const original = `\`\`\`${info}\nvalue = 1\n\`\`\``
|
|
21
|
+
const root = document.createElement('div')
|
|
22
|
+
document.body.append(root)
|
|
23
|
+
const editor = await Editor.make().config(ctx => {
|
|
24
|
+
ctx.set(rootCtx, root)
|
|
25
|
+
ctx.set(defaultValueCtx, original)
|
|
26
|
+
}).use(commonmark).use(codeBlockMetadataSchema).create()
|
|
27
|
+
editors.push(editor)
|
|
28
|
+
for (let i = 0; i < 3; i++) {
|
|
29
|
+
expect(editor.action(getMarkdown()).trim()).toBe(original)
|
|
30
|
+
editor.action(replaceAll(editor.action(getMarkdown())))
|
|
31
|
+
}
|
|
32
|
+
editor.action(ctx => {
|
|
33
|
+
const view = ctx.get(editorViewCtx)
|
|
34
|
+
const holder = document.createElement('div')
|
|
35
|
+
holder.append(DOMSerializer.fromSchema(view.state.schema).serializeFragment(view.state.doc.content))
|
|
36
|
+
const copied = DOMParser.fromSchema(view.state.schema).parse(holder)
|
|
37
|
+
expect(copied.firstChild?.attrs.meta).toBe(meta)
|
|
38
|
+
expect(copied.firstChild?.textContent).toBe('value = 1')
|
|
39
|
+
})
|
|
40
|
+
})
|
|
@@ -75,6 +75,38 @@ describe('input authoring', () => {
|
|
|
75
75
|
expect(wrapper.emitted('save')?.[0]).toEqual(['sample_name: str'])
|
|
76
76
|
})
|
|
77
77
|
|
|
78
|
+
it.each([
|
|
79
|
+
['str', 'str'], ['int', 'int'], ['float', 'float'], ['choice', 'Literal["A", "B"]'],
|
|
80
|
+
['multiple-choice', 'list[Literal["A", "B"]]'], ['bool | None', 'bool'], ['date', 'date'],
|
|
81
|
+
['time', 'time'], ['datetime', 'datetime'], ['list[str]', 'list[str]'], ['file', 'FileIdPDF'],
|
|
82
|
+
['custom', 'list[str | None]'],
|
|
83
|
+
])('can leave a new %s unset and reopen it', async (preset, type) => {
|
|
84
|
+
const wrapper = await openDialog()
|
|
85
|
+
await wrapper.get('[name="preset"]').setValue(preset)
|
|
86
|
+
if (preset.includes('choice')) await wrapper.get('[name="options"]').setValue('A\nB')
|
|
87
|
+
if (preset === 'custom') await wrapper.get('[name="type"]').setValue(type)
|
|
88
|
+
const toggle = wrapper.get<HTMLInputElement>('[role="switch"]')
|
|
89
|
+
expect(toggle.element.checked).toBe(true)
|
|
90
|
+
await toggle.setValue(false)
|
|
91
|
+
await wrapper.get('form').trigger('submit')
|
|
92
|
+
const saved = String(wrapper.emitted('save')?.[0]?.[0])
|
|
93
|
+
expect(saved).toBe(`field_1: ${type} | None = None`)
|
|
94
|
+
const editing = await openDialog('', saved)
|
|
95
|
+
expect(editing.get<HTMLInputElement>('[name="required"]').element.checked).toBe(false)
|
|
96
|
+
await editing.get('[name="required"]').setValue(true)
|
|
97
|
+
await editing.get('form').trigger('submit')
|
|
98
|
+
expect(editing.emitted('save')?.[0]).toEqual([`field_1: ${preset === 'bool | None' ? 'Literal[True, False]' : type}`])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it.each([['unit: str = "mg"', 'mg'], ['amount: int = 0', '0'], ['flag: bool = False', 'False']])('keeps the prefill independent of required: %s', async (source, initial) => {
|
|
102
|
+
const wrapper = await openDialog('', source)
|
|
103
|
+
expect(wrapper.get<HTMLInputElement>('[name="required"]').element.checked).toBe(true)
|
|
104
|
+
expect(wrapper.get<HTMLInputElement>('[name="default"]').element.value).toBe(initial)
|
|
105
|
+
await wrapper.get('[name="required"]').setValue(false)
|
|
106
|
+
await wrapper.get('form').trigger('submit')
|
|
107
|
+
expect(wrapper.emitted('save')?.[0]).toEqual([source.replace(' =', ' | None =')])
|
|
108
|
+
})
|
|
109
|
+
|
|
78
110
|
it('creates and edits dropdown choices without requiring Literal syntax', async () => {
|
|
79
111
|
const wrapper = await openDialog()
|
|
80
112
|
await wrapper.get('select[name="preset"]').setValue('choice')
|
|
@@ -90,4 +122,28 @@ describe('input authoring', () => {
|
|
|
90
122
|
expect(updated.id).toBe('field_1')
|
|
91
123
|
expect(updated.enum).toEqual(['Ready', 'Waiting'])
|
|
92
124
|
})
|
|
125
|
+
|
|
126
|
+
it('edits multiple choice defaults using the actual options', async () => {
|
|
127
|
+
const wrapper = await openDialog()
|
|
128
|
+
await wrapper.get('[name="preset"]').setValue('multiple-choice')
|
|
129
|
+
await wrapper.get('[name="options"]').setValue('A=B\nOther')
|
|
130
|
+
await wrapper.get('select[multiple]').setValue(['A=B'])
|
|
131
|
+
await wrapper.get('[name="required"]').setValue(false)
|
|
132
|
+
await wrapper.get('form').trigger('submit')
|
|
133
|
+
const parsed = parseVarDefinition(String(wrapper.emitted('save')?.[0]?.[0]))
|
|
134
|
+
expect(parsed.type).toBe('list[Literal["A=B", "Other"]] | None')
|
|
135
|
+
expect(parsed.default).toEqual(['A=B'])
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
it('only converts legacy Optional notation on request', async () => {
|
|
139
|
+
const source = 'comment: Optional[str] = None'
|
|
140
|
+
const wrapper = await openDialog('', source)
|
|
141
|
+
expect(wrapper.get<HTMLInputElement>('[name="required"]').element.checked).toBe(false)
|
|
142
|
+
await wrapper.get('form').trigger('submit')
|
|
143
|
+
expect(wrapper.emitted('save')?.[0]).toEqual([source])
|
|
144
|
+
const editing = await openDialog('', source)
|
|
145
|
+
await editing.get('button[type="button"]').trigger('click')
|
|
146
|
+
await editing.get('form').trigger('submit')
|
|
147
|
+
expect(editing.emitted('save')?.[0]).toEqual(['comment: str | None = None'])
|
|
148
|
+
})
|
|
93
149
|
})
|
|
@@ -27,7 +27,7 @@ describe('common input toolbar', () => {
|
|
|
27
27
|
})
|
|
28
28
|
|
|
29
29
|
it.each([
|
|
30
|
-
['int', 'int'], ['float', 'float'], ['bool | None', '
|
|
30
|
+
['int', 'int'], ['float', 'float'], ['bool | None', 'Literal[True, False]'],
|
|
31
31
|
['date', 'date'], ['time', 'time'], ['datetime', 'datetime'], ['file', 'FileIdPDF'],
|
|
32
32
|
['choice', 'Literal["A", "B"]'], ['multiple-choice', 'list[Literal["A", "B"]]'],
|
|
33
33
|
])('opens %s already selected without requiring a manual ID', async (preset, type) => {
|
|
@@ -73,7 +73,7 @@ describe('common input toolbar', () => {
|
|
|
73
73
|
it.each([
|
|
74
74
|
['choice', 'Literal["A", "C"]'],
|
|
75
75
|
['multiple-choice', 'list[Literal["A", "C"]]'],
|
|
76
|
-
['bool | None', '
|
|
76
|
+
['bool | None', 'Literal[True, False]'],
|
|
77
77
|
])('replaces legacy enum metadata when changing to %s', async (preset, type) => {
|
|
78
78
|
const field = 'status: str, enum=["A", "B"], description="Keep"'
|
|
79
79
|
const wrapper = mount(AimdInputFieldDialog, { props: { visible: false, content: `{{var|${field}}}`, fieldContent: field, messages }, global: { stubs: { teleport: true } } })
|
package/src/authoring/fields.ts
CHANGED
|
@@ -43,12 +43,13 @@ export function serializeAimdFieldValue(value: unknown): string {
|
|
|
43
43
|
/** Edit common properties while retaining defaults and other parsed field metadata. */
|
|
44
44
|
export function updateAimdInputField(
|
|
45
45
|
original: string | undefined,
|
|
46
|
-
values: { id: string; type: string; title: string; defaultRaw: string; clearEnum?: boolean },
|
|
46
|
+
values: { id: string; type: string; title: string; defaultRaw: string; clearEnum?: boolean; clearRequiredOverride?: boolean },
|
|
47
47
|
): string {
|
|
48
48
|
const definition = original ? parseVarDefinition(original) : undefined
|
|
49
49
|
if (definition?.subvars) throw new Error('Table definitions must be edited with the table editor.')
|
|
50
50
|
const kwargs = { ...definition?.kwargs }
|
|
51
51
|
if (values.clearEnum) delete kwargs.enum
|
|
52
|
+
if (values.clearRequiredOverride) delete kwargs.required
|
|
52
53
|
if (values.title.trim()) kwargs.title = values.title.trim()
|
|
53
54
|
else delete kwargs.title
|
|
54
55
|
let result = values.id.trim()
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
2
|
import { computed, nextTick, ref, watch } from 'vue'
|
|
3
|
-
import { parseVarDefinition, parseVarListEnumValues, validateVarDefinition } from '@airalogy/aimd-core/parser'
|
|
4
|
-
import { unwrapAimdNullableType, isAimdNullableType, getAimdBuiltInTypeNames, getAimdBuiltInTypeMetadata } from '@airalogy/aimd-core/utils'
|
|
3
|
+
import { parseVarDefinition, parseVarDefaultValue, parseVarListEnumValues, validateVarDefinition } from '@airalogy/aimd-core/parser'
|
|
4
|
+
import { unwrapAimdNullableType, isAimdNullableType, setAimdTypeNullable, getAimdBuiltInTypeNames, getAimdBuiltInTypeMetadata } from '@airalogy/aimd-core/utils'
|
|
5
5
|
import { AIMD_FIELD_ID_PATTERN, collectAimdFieldIds, createAimdFieldId, updateAimdInputField } from '../authoring/fields'
|
|
6
6
|
import type { AimdEditorMessages } from './locales'
|
|
7
7
|
import type { AimdVarTypePresetOption } from './types'
|
|
@@ -30,6 +30,47 @@ const optionsText = ref('')
|
|
|
30
30
|
const preset = ref('str')
|
|
31
31
|
const error = ref('')
|
|
32
32
|
const advanced = ref(false)
|
|
33
|
+
const requiredOverride = ref<boolean>()
|
|
34
|
+
const requirementEdited = ref(false)
|
|
35
|
+
const required = computed(() => requiredOverride.value ?? !isAimdNullableType(type.value))
|
|
36
|
+
const yesNo = 'bool | None'
|
|
37
|
+
const requiredYesNo = 'Literal[True, False]'
|
|
38
|
+
const choices = computed(() => optionsText.value.split(/\r?\n/).map(value => value.trim()).filter(Boolean))
|
|
39
|
+
const pipedType = computed(() => /^(?:(?:typing|typing_extensions)\.)?Optional\[/.test(type.value.trim())
|
|
40
|
+
? setAimdTypeNullable(setAimdTypeNullable(type.value, false), true) : '')
|
|
41
|
+
const multiDefault = computed(() => {
|
|
42
|
+
const value = parseVarDefaultValue(defaultRaw.value)
|
|
43
|
+
return Array.isArray(value) ? value : []
|
|
44
|
+
})
|
|
45
|
+
function setMultiDefault(event: Event) {
|
|
46
|
+
const selected = Array.from((event.target as HTMLSelectElement).selectedOptions, option => option.value)
|
|
47
|
+
defaultRaw.value = selected.length ? JSON.stringify(selected) : required.value ? '' : 'None'
|
|
48
|
+
}
|
|
49
|
+
const defaultChoices = computed(() => preset.value === yesNo || preset.value === 'bool'
|
|
50
|
+
? [['True', props.messages.inputField.yes], ['False', props.messages.inputField.no]]
|
|
51
|
+
: preset.value === 'choice' ? choices.value.map(choice => [JSON.stringify(choice), choice]) : [])
|
|
52
|
+
function matchedDefault(raw: string) {
|
|
53
|
+
const value = parseVarDefaultValue(raw)
|
|
54
|
+
return defaultChoices.value.find(([candidate]) => Object.is(parseVarDefaultValue(candidate), value))?.[0]
|
|
55
|
+
}
|
|
56
|
+
const stringDefault = computed(() => ['str', 'date', 'time', 'datetime'].includes(preset.value))
|
|
57
|
+
const defaultInputType = computed(() => {
|
|
58
|
+
if (preset.value === 'int' || preset.value === 'float') return 'number'
|
|
59
|
+
return preset.value === 'date' || preset.value === 'time' ? preset.value : 'text'
|
|
60
|
+
})
|
|
61
|
+
const displayDefault = computed({
|
|
62
|
+
get() {
|
|
63
|
+
if (defaultRaw.value === 'None') return ''
|
|
64
|
+
if (stringDefault.value && /^(['"]).*\1$/s.test(defaultRaw.value)) return String(parseVarDefaultValue(defaultRaw.value))
|
|
65
|
+
if (defaultChoices.value.length && defaultRaw.value) {
|
|
66
|
+
return matchedDefault(defaultRaw.value) ?? defaultRaw.value
|
|
67
|
+
}
|
|
68
|
+
return defaultRaw.value
|
|
69
|
+
},
|
|
70
|
+
set(value: string) {
|
|
71
|
+
defaultRaw.value = value === '' ? (required.value ? '' : 'None') : stringDefault.value ? JSON.stringify(value) : value
|
|
72
|
+
},
|
|
73
|
+
})
|
|
33
74
|
const fileTypes = getAimdBuiltInTypeNames().filter(name => getAimdBuiltInTypeMetadata(name)?.storage_kind === 'file-id')
|
|
34
75
|
const presets = computed(() => [
|
|
35
76
|
['str', props.messages.inputField.text],
|
|
@@ -55,7 +96,11 @@ watch(() => props.visible, async visible => {
|
|
|
55
96
|
title.value = typeof definition?.kwargs?.title === 'string' ? definition.kwargs.title : ''
|
|
56
97
|
type.value = definition?.type ?? (props.initialPreset === 'file' ? 'FileIdPDF' : ['choice', 'multiple-choice', 'custom'].includes(props.initialPreset ?? '') ? 'str' : props.initialPreset ?? 'str')
|
|
57
98
|
defaultRaw.value = definition?.defaultRaw ?? ''
|
|
58
|
-
|
|
99
|
+
requiredOverride.value = typeof definition?.kwargs?.required === 'boolean' ? definition.kwargs.required : undefined
|
|
100
|
+
requirementEdited.value = false
|
|
101
|
+
const base = unwrapAimdNullableType(type.value)
|
|
102
|
+
preset.value = type.value === requiredYesNo || (base === 'bool' && isAimdNullableType(type.value)) ? yesNo
|
|
103
|
+
: presets.value.some(([value]) => value === base) ? base : 'custom'
|
|
59
104
|
const listChoices = parseVarListEnumValues(type.value)
|
|
60
105
|
const choices = listChoices.length ? listChoices : definition?.enum
|
|
61
106
|
const editableChoices = Array.isArray(definition?.kwargs?.enum) || /^(?:(?:typing|typing_extensions)\.)?(?:(?:list|List)\s*\[\s*(?:(?:typing|typing_extensions)\.)?)?Literal\s*\[/.test(unwrapAimdNullableType(type.value))
|
|
@@ -63,6 +108,7 @@ watch(() => props.visible, async visible => {
|
|
|
63
108
|
if (canEditChoices) preset.value = listChoices.length ? 'multiple-choice' : 'choice'
|
|
64
109
|
if (fileTypes.includes(unwrapAimdNullableType(type.value))) preset.value = 'file'
|
|
65
110
|
if (!definition && props.initialPreset) preset.value = props.initialPreset
|
|
111
|
+
if (!definition && preset.value === yesNo) type.value = requiredYesNo
|
|
66
112
|
optionsText.value = canEditChoices ? choices!.join('\n') : ''
|
|
67
113
|
advanced.value = preset.value === 'custom'
|
|
68
114
|
error.value = ''
|
|
@@ -72,11 +118,23 @@ watch(() => props.visible, async visible => {
|
|
|
72
118
|
}, { immediate: true })
|
|
73
119
|
|
|
74
120
|
function selectPreset() {
|
|
75
|
-
|
|
76
|
-
if (preset.value ===
|
|
121
|
+
const nullable = !required.value
|
|
122
|
+
if (preset.value === yesNo) type.value = nullable ? yesNo : requiredYesNo
|
|
123
|
+
else if (!['custom', 'choice', 'multiple-choice', 'file'].includes(preset.value)) type.value = setAimdTypeNullable(preset.value, nullable)
|
|
124
|
+
if (preset.value === 'file' && !fileTypes.includes(unwrapAimdNullableType(type.value))) type.value = setAimdTypeNullable('FileIdPDF', nullable)
|
|
77
125
|
if (preset.value === 'custom') advanced.value = true
|
|
78
126
|
}
|
|
79
127
|
|
|
128
|
+
function changeRequired(on: boolean) {
|
|
129
|
+
requirementEdited.value = true
|
|
130
|
+
requiredOverride.value = undefined
|
|
131
|
+
if (preset.value === yesNo) type.value = on ? requiredYesNo : yesNo
|
|
132
|
+
else type.value = setAimdTypeNullable(type.value, !on)
|
|
133
|
+
if (preset.value === 'bool' && !on) preset.value = yesNo
|
|
134
|
+
if (on && defaultRaw.value === 'None') defaultRaw.value = ''
|
|
135
|
+
if (!on && !defaultRaw.value.trim()) defaultRaw.value = 'None'
|
|
136
|
+
}
|
|
137
|
+
|
|
80
138
|
function selectFileType(event: Event) {
|
|
81
139
|
type.value = (event.target as HTMLSelectElement).value + (isAimdNullableType(type.value) ? ' | None' : '')
|
|
82
140
|
}
|
|
@@ -90,15 +148,26 @@ function save() {
|
|
|
90
148
|
if (nextId !== originalId && existing.has(nextId)) { error.value = props.messages.inputField.duplicateId; advanced.value = true; return }
|
|
91
149
|
let nextType = type.value
|
|
92
150
|
if (preset.value === 'choice' || preset.value === 'multiple-choice') {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
nextType = `Literal[${choices.map(value => JSON.stringify(value)).join(', ')}]`
|
|
151
|
+
if (!choices.value.length || new Set(choices.value).size !== choices.value.length) { error.value = props.messages.inputField.invalidOptions; return }
|
|
152
|
+
nextType = `Literal[${choices.value.map(value => JSON.stringify(value)).join(', ')}]`
|
|
96
153
|
if (preset.value === 'multiple-choice') nextType = `list[${nextType}]`
|
|
97
154
|
if (isAimdNullableType(type.value)) nextType += ' | None'
|
|
155
|
+
const oldChoices = preset.value === 'multiple-choice' ? parseVarListEnumValues(original?.type) : original?.enum
|
|
156
|
+
if (original?.type === type.value && !original?.kwargs?.enum &&
|
|
157
|
+
JSON.stringify(choices.value) === JSON.stringify(oldChoices) &&
|
|
158
|
+
(parseVarListEnumValues(original.type).length > 0) === (preset.value === 'multiple-choice')) nextType = type.value
|
|
159
|
+
}
|
|
160
|
+
const raw = defaultRaw.value.trim()
|
|
161
|
+
const invalidMultiDefault = preset.value === 'multiple-choice' && raw && raw !== 'None' &&
|
|
162
|
+
(!Array.isArray(parseVarDefaultValue(raw)) || multiDefault.value.some(value => !choices.value.includes(value)))
|
|
163
|
+
if ((required.value && raw === 'None') || (defaultChoices.value.length && raw && raw !== 'None' && !matchedDefault(raw)) || invalidMultiDefault) {
|
|
164
|
+
error.value = props.messages.inputField.invalidDefault
|
|
165
|
+
return
|
|
98
166
|
}
|
|
99
167
|
const result = updateAimdInputField(props.fieldContent, {
|
|
100
|
-
id: nextId, type: nextType, title: title.value, defaultRaw:
|
|
101
|
-
|
|
168
|
+
id: nextId, type: nextType, title: title.value, defaultRaw: raw,
|
|
169
|
+
clearRequiredOverride: requirementEdited.value,
|
|
170
|
+
clearEnum: preset.value === 'choice' || preset.value === 'multiple-choice' || unwrapAimdNullableType(nextType) !== unwrapAimdNullableType(original?.type),
|
|
102
171
|
})
|
|
103
172
|
const problems = validateVarDefinition(parseVarDefinition(result))
|
|
104
173
|
if (problems.length) { error.value = problems.join(' '); advanced.value = true; return }
|
|
@@ -118,13 +187,26 @@ function save() {
|
|
|
118
187
|
<option v-for="[value, label] in presets" :key="value" :value="value">{{ label }}</option>
|
|
119
188
|
</select>
|
|
120
189
|
</label>
|
|
190
|
+
<label class="aimd-input-dialog__required">{{ messages.inputField.required }}
|
|
191
|
+
<input name="required" type="checkbox" role="switch" :checked="required" @change="changeRequired(($event.target as HTMLInputElement).checked)">
|
|
192
|
+
</label>
|
|
121
193
|
<label v-if="preset === 'choice' || preset === 'multiple-choice'">{{ messages.inputField.options }}<textarea v-model="optionsText" name="options" rows="4" /></label>
|
|
122
194
|
<label v-if="preset === 'file'">{{ messages.inputField.fileFormat }}<select name="fileType" :value="unwrapAimdNullableType(type)" @change="selectFileType"><option v-for="fileType in fileTypes" :key="fileType" :value="fileType">{{ fileType.replace(/^FileId/, '') }}</option></select></label>
|
|
195
|
+
<label>{{ messages.dialog.defaultValue }}
|
|
196
|
+
<select v-if="preset === 'multiple-choice'" name="default" multiple :size="Math.min(4, Math.max(2, choices.length))" @change="setMultiDefault">
|
|
197
|
+
<option v-for="choice in choices" :key="choice" :value="choice" :selected="multiDefault.includes(choice)">{{ choice }}</option>
|
|
198
|
+
</select>
|
|
199
|
+
<select v-else-if="defaultChoices.length || preset === 'choice'" v-model="displayDefault" name="default">
|
|
200
|
+
<option value="">{{ messages.inputField.noDefault }}</option>
|
|
201
|
+
<option v-for="[value, label] in defaultChoices" :key="value" :value="value">{{ label }}</option>
|
|
202
|
+
</select>
|
|
203
|
+
<input v-else :value="displayDefault" name="default" :type="defaultInputType" :step="preset === 'float' ? 'any' : undefined" :placeholder="messages.inputField.noDefault" @input="displayDefault = ($event.target as HTMLInputElement).value">
|
|
204
|
+
</label>
|
|
205
|
+
<button v-if="pipedType" type="button" @click="type = pipedType">{{ messages.inputField.useType(pipedType) }}</button>
|
|
123
206
|
<details :open="advanced" @toggle="advanced = ($event.target as HTMLDetailsElement).open">
|
|
124
207
|
<summary>{{ messages.inputField.advanced }}</summary>
|
|
125
208
|
<label>{{ messages.inputField.id }}<input v-model="id" name="id" autocomplete="off" spellcheck="false"></label>
|
|
126
209
|
<AimdTypePicker v-model="type" :messages="messages" :var-type-plugins="varTypePlugins" @update:model-value="preset = 'custom'" />
|
|
127
|
-
<label>{{ messages.dialog.defaultValue }}<input v-model="defaultRaw" name="default"></label>
|
|
128
210
|
</details>
|
|
129
211
|
<p v-if="error" class="aimd-input-dialog__error" role="alert">{{ error }}</p>
|
|
130
212
|
<footer>
|
|
@@ -158,6 +240,12 @@ h2 { font-size: 18px; margin: 0 0 16px; }
|
|
|
158
240
|
label { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; }
|
|
159
241
|
input, select, textarea { box-sizing: border-box; width: 100%; min-width: 0; padding: 8px; border: 1px solid #cbd5e1; border-radius: 4px; background: white; color: inherit; font: inherit; }
|
|
160
242
|
summary { cursor: pointer; padding: 8px 0; color: #57606a; }
|
|
243
|
+
.aimd-input-dialog__required { flex-direction: row; align-items: center; justify-content: space-between; min-height: 32px; cursor: pointer; }
|
|
244
|
+
.aimd-input-dialog__required input { appearance: none; width: 32px; height: 18px; border-radius: 12px; padding: 2px; background: #64748b; cursor: pointer; }
|
|
245
|
+
.aimd-input-dialog__required input::before { content: ''; display: block; width: 12px; height: 12px; border-radius: 50%; background: white; }
|
|
246
|
+
.aimd-input-dialog__required input:checked { background: #1769d2; border-color: #1769d2; }
|
|
247
|
+
.aimd-input-dialog__required input:checked::before { transform: translateX(13px); }
|
|
248
|
+
@media (forced-colors: active) { .aimd-input-dialog__required input { appearance: auto; } }
|
|
161
249
|
footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
|
|
162
250
|
button { padding: 7px 16px; border: 1px solid #cbd5e1; border-radius: 4px; background: white; cursor: pointer; font: inherit; }
|
|
163
251
|
.aimd-input-dialog__save { background: #1769d2; border-color: #1769d2; color: white; }
|
|
@@ -28,6 +28,7 @@ import '@milkdown/kit/prose/tables/style/tables.css'
|
|
|
28
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
|
|
|
@@ -333,6 +334,7 @@ const MilkdownEditorInner = defineComponent({
|
|
|
333
334
|
})
|
|
334
335
|
})
|
|
335
336
|
.use(commonmark)
|
|
337
|
+
.use(codeBlockMetadataSchema)
|
|
336
338
|
.use(gfm)
|
|
337
339
|
.use(history)
|
|
338
340
|
.use(listener)
|
package/src/vue/locales.ts
CHANGED
|
@@ -29,6 +29,12 @@ export interface AimdEditorMessages {
|
|
|
29
29
|
}
|
|
30
30
|
inputField: {
|
|
31
31
|
insert: string
|
|
32
|
+
required: string
|
|
33
|
+
noDefault: string
|
|
34
|
+
yes: string
|
|
35
|
+
no: string
|
|
36
|
+
invalidDefault: string
|
|
37
|
+
useType: (type: string) => string
|
|
32
38
|
edit: string
|
|
33
39
|
save: string
|
|
34
40
|
name: string
|
|
@@ -295,6 +301,9 @@ const EN_US_MESSAGES: AimdEditorMessages = {
|
|
|
295
301
|
options: 'Choices (one per line)', text: 'Text', integer: 'Integer', number: 'Decimal',
|
|
296
302
|
multipleChoice: 'Multiple choice', more: 'More inputs', file: 'Attachment', insertType: label => `Insert ${label.toLowerCase()} input`,
|
|
297
303
|
checkbox: 'Checkbox',
|
|
304
|
+
required: 'Required', noDefault: 'No preset value', yes: 'Yes', no: 'No',
|
|
305
|
+
invalidDefault: 'Choose a default that is valid for this input, or leave it unset.',
|
|
306
|
+
useType: type => `Use ${type}`,
|
|
298
307
|
fileFormat: 'File format',
|
|
299
308
|
time: 'Time', datetime: 'Date and time',
|
|
300
309
|
choice: 'Single choice', boolean: 'Yes / no', date: 'Date', list: 'Text list', custom: 'Advanced / custom',
|
|
@@ -518,6 +527,9 @@ const ZH_CN_MESSAGES: AimdEditorMessages = {
|
|
|
518
527
|
options: '选项(每行一个)', text: '文本', integer: '整数', number: '小数',
|
|
519
528
|
multipleChoice: '多选', more: '更多输入框', file: '附件', insertType: label => `插入${label}输入框`,
|
|
520
529
|
checkbox: '复选框',
|
|
530
|
+
required: '必填', noDefault: '不预填', yes: '是', no: '否',
|
|
531
|
+
invalidDefault: '请选择适用于此输入框的默认值,或留空不预填。',
|
|
532
|
+
useType: type => `改用 ${type}`,
|
|
521
533
|
fileFormat: '文件格式',
|
|
522
534
|
time: '时间', datetime: '日期时间',
|
|
523
535
|
choice: '单选', boolean: '是/否', date: '日期', list: '文本列表', custom: '高级 / 自定义',
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { codeBlockAttr, codeBlockSchema } from '@milkdown/kit/preset/commonmark'
|
|
2
|
+
|
|
3
|
+
// Code-fence metadata is executable AIMD configuration, not disposable presentation text.
|
|
4
|
+
export const codeBlockMetadataSchema = codeBlockSchema.extendSchema(previous => ctx => {
|
|
5
|
+
const schema = previous(ctx)
|
|
6
|
+
return {
|
|
7
|
+
...schema,
|
|
8
|
+
attrs: {
|
|
9
|
+
...schema.attrs,
|
|
10
|
+
meta: { default: '', validate: 'string' },
|
|
11
|
+
},
|
|
12
|
+
parseDOM: [{
|
|
13
|
+
tag: 'pre',
|
|
14
|
+
preserveWhitespace: 'full',
|
|
15
|
+
getAttrs: element => ({
|
|
16
|
+
language: (element as HTMLElement).getAttribute('data-language') ?? '',
|
|
17
|
+
meta: (element as HTMLElement).getAttribute('data-code-meta') ?? '',
|
|
18
|
+
}),
|
|
19
|
+
}],
|
|
20
|
+
toDOM: node => {
|
|
21
|
+
const attrs = ctx.get(codeBlockAttr.key)(node)
|
|
22
|
+
return ['pre', {
|
|
23
|
+
...attrs.pre,
|
|
24
|
+
'data-language': node.attrs.language,
|
|
25
|
+
'data-code-meta': node.attrs.meta,
|
|
26
|
+
}, ['code', attrs.code, 0]]
|
|
27
|
+
},
|
|
28
|
+
parseMarkdown: {
|
|
29
|
+
...schema.parseMarkdown,
|
|
30
|
+
runner: (state, node, type) => {
|
|
31
|
+
state.openNode(type, { language: node.lang ?? '', meta: node.meta ?? '' })
|
|
32
|
+
if (node.value) state.addText(String(node.value))
|
|
33
|
+
state.closeNode()
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
toMarkdown: {
|
|
37
|
+
...schema.toMarkdown,
|
|
38
|
+
runner: (state, node) => {
|
|
39
|
+
state.addNode('code', undefined, node.textContent, {
|
|
40
|
+
lang: node.attrs.language,
|
|
41
|
+
meta: node.attrs.meta || undefined,
|
|
42
|
+
})
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
})
|