@airalogy/aimd-editor 1.10.0 → 1.12.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.
@@ -274,11 +274,13 @@ defineExpose({
274
274
  .aimd-editor-toolbar {
275
275
  display: flex;
276
276
  align-items: center;
277
+ align-content: flex-start;
277
278
  gap: 2px;
279
+ row-gap: 4px;
278
280
  padding: 4px 8px;
279
281
  background: #fafbfc;
280
282
  border-bottom: 1px solid #e0e0e0;
281
- overflow-x: auto;
283
+ overflow: visible;
282
284
  flex-wrap: wrap;
283
285
  }
284
286
 
@@ -21,6 +21,7 @@ const emit = defineEmits<{
21
21
  const CLIENT_ASSIGNER_FENCE = /^\s*(```|~~~)\s*assigner(?:\s+.*\bruntime\s*=\s*(?:"client"|'client'|client)\b.*)\s*$/
22
22
  const SERVER_ASSIGNER_FENCE = /^\s*(```|~~~)\s*assigner(?:\s+.*)?\s*$/
23
23
  const QUIZ_FENCE = /^\s*(```|~~~)\s*quiz(?:\s+.*)?\s*$/
24
+ const CONNECTORS_FENCE = /^\s*(```|~~~)\s*connectors(?:\s+.*)?\s*$/
24
25
  const GENERIC_CODE_FENCE = /^\s*(```|~~~)\s*((?:\w|[/#-])+)(?:\s+.*)?\s*$/
25
26
  const EMPTY_CODE_FENCE = /^\s*(```|~~~)\s*$/
26
27
  const AIMD_DIAGNOSTIC_OWNER = 'airalogy-aimd'
@@ -29,6 +30,7 @@ const editorContainer = ref<HTMLElement | null>(null)
29
30
 
30
31
  let monacoEditorInstance: any = null
31
32
  let monacoModule: any = null
33
+ let aimdCodeActionProviderDisposable: any = null
32
34
  let isSyncing = false
33
35
  const loading = ref(true)
34
36
 
@@ -77,6 +79,7 @@ function registerAimdLanguage(monaco: any) {
77
79
  [/\{\{/, { token: 'delimiter.bracket.aimd', next: '@aimdField' }],
78
80
  [/^#{1,6}\s.*$/, 'keyword.md'],
79
81
  [QUIZ_FENCE, { token: 'string.code', next: '@embeddedCodeblock', nextEmbedded: 'yaml' }],
82
+ [CONNECTORS_FENCE, { token: 'string.code', next: '@embeddedCodeblock', nextEmbedded: 'yaml' }],
80
83
  [CLIENT_ASSIGNER_FENCE, { token: 'string.code', next: '@embeddedCodeblock', nextEmbedded: 'javascript' }],
81
84
  [SERVER_ASSIGNER_FENCE, { token: 'string.code', next: '@embeddedCodeblock', nextEmbedded: 'python' }],
82
85
  [GENERIC_CODE_FENCE, { token: 'string.code', next: '@embeddedCodeblock', nextEmbedded: '$2' }],
@@ -215,7 +218,7 @@ function updateAimdDiagnostics() {
215
218
  const model = monacoEditorInstance.getModel()
216
219
  if (!model) return
217
220
 
218
- const markers = collectAimdDiagnostics(model.getValue()).map((diagnostic) => {
221
+ const markers = collectAimdDiagnostics(model.getValue(), props.resolvedMessages.diagnostics).map((diagnostic) => {
219
222
  const start = model.getPositionAt(diagnostic.startOffset)
220
223
  const end = model.getPositionAt(diagnostic.endOffset)
221
224
  return {
@@ -223,6 +226,7 @@ function updateAimdDiagnostics() {
223
226
  ? monacoModule.MarkerSeverity.Error
224
227
  : monacoModule.MarkerSeverity.Warning,
225
228
  message: diagnostic.message,
229
+ code: diagnostic.code,
226
230
  source: 'AIMD',
227
231
  startLineNumber: start.lineNumber,
228
232
  startColumn: start.column,
@@ -234,6 +238,65 @@ function updateAimdDiagnostics() {
234
238
  monacoModule.editor.setModelMarkers(model, AIMD_DIAGNOSTIC_OWNER, markers)
235
239
  }
236
240
 
241
+ function rangesOverlap(startOffset: number, endOffset: number, rangeStart: number, rangeEnd: number): boolean {
242
+ return startOffset <= rangeEnd && endOffset >= rangeStart
243
+ }
244
+
245
+ function registerAimdCodeActions(monaco: any) {
246
+ aimdCodeActionProviderDisposable?.dispose()
247
+ aimdCodeActionProviderDisposable = monaco.languages.registerCodeActionProvider('aimd', {
248
+ provideCodeActions(model: any, range: any, context: any) {
249
+ if (props.readonly || model !== monacoEditorInstance?.getModel()) {
250
+ return { actions: [], dispose() {} }
251
+ }
252
+
253
+ const rangeStart = model.getOffsetAt({
254
+ lineNumber: range.startLineNumber,
255
+ column: range.startColumn,
256
+ })
257
+ const rangeEnd = model.getOffsetAt({
258
+ lineNumber: range.endLineNumber,
259
+ column: range.endColumn,
260
+ })
261
+ const actions = collectAimdDiagnostics(model.getValue(), props.resolvedMessages.diagnostics)
262
+ .filter(diagnostic => diagnostic.quickFix && rangesOverlap(
263
+ diagnostic.startOffset,
264
+ diagnostic.endOffset,
265
+ rangeStart,
266
+ rangeEnd,
267
+ ))
268
+ .map((diagnostic) => {
269
+ const edit = diagnostic.quickFix!.edit
270
+ const editStart = model.getPositionAt(edit.startOffset)
271
+ const editEnd = model.getPositionAt(edit.endOffset)
272
+ return {
273
+ title: diagnostic.quickFix!.title,
274
+ kind: 'quickfix',
275
+ isPreferred: true,
276
+ diagnostics: context.markers.filter((marker: any) => marker.code === diagnostic.code),
277
+ edit: {
278
+ edits: [{
279
+ resource: model.uri,
280
+ textEdit: {
281
+ range: {
282
+ startLineNumber: editStart.lineNumber,
283
+ startColumn: editStart.column,
284
+ endLineNumber: editEnd.lineNumber,
285
+ endColumn: editEnd.column,
286
+ },
287
+ text: edit.text,
288
+ },
289
+ versionId: model.getVersionId(),
290
+ }],
291
+ },
292
+ }
293
+ })
294
+
295
+ return { actions, dispose() {} }
296
+ },
297
+ })
298
+ }
299
+
237
300
  function clearAimdDiagnostics() {
238
301
  if (!monacoModule || !monacoEditorInstance) return
239
302
 
@@ -265,6 +328,7 @@ function createEditor(monaco: any) {
265
328
  readOnly: props.readonly,
266
329
  ...safeMonacoOptions,
267
330
  })
331
+ registerAimdCodeActions(monaco)
268
332
  monacoEditorInstance.onDidChangeModelContent(() => {
269
333
  updateAimdDiagnostics()
270
334
  if (!isSyncing) {
@@ -290,6 +354,7 @@ onMounted(async () => {
290
354
 
291
355
  onBeforeUnmount(() => {
292
356
  clearAimdDiagnostics()
357
+ aimdCodeActionProviderDisposable?.dispose()
293
358
  monacoEditorInstance?.dispose()
294
359
  })
295
360
 
@@ -1,18 +1,45 @@
1
1
  import { parseMediaContent, parseVarDefinition, validateMediaDefinition, validateVarDefinition } from '@airalogy/aimd-core/parser'
2
+ import {
3
+ normalizeAimdFigureIds,
4
+ scanAimdFigureBlocks,
5
+ type AimdTextEdit,
6
+ } from '../authoring'
2
7
 
3
8
  export type AimdDiagnosticSeverity = 'warning' | 'error'
4
9
 
5
10
  export interface AimdDiagnostic {
11
+ code?: string
6
12
  message: string
7
13
  severity: AimdDiagnosticSeverity
8
14
  startOffset: number
9
15
  endOffset: number
16
+ quickFix?: {
17
+ title: string
18
+ edit: AimdTextEdit
19
+ }
20
+ }
21
+
22
+ export interface AimdDiagnosticMessages {
23
+ figureMissingId: string
24
+ figureMissingSrc: string
25
+ figureDuplicateId: (id: string) => string
26
+ generateFigureId: string
10
27
  }
11
28
 
12
29
  const AIMD_VAR_TEMPLATE_PATTERN = /\{\{\s*(var_table|var)\s*\|([\s\S]*?)\}\}/g
13
30
  const AIMD_MEDIA_BLOCK_PATTERN = /^[ \t]*(```|~~~)[ \t]*media(?:[^\n]*)?\r?\n([\s\S]*?)^[ \t]*\1[ \t]*$/gm
14
31
 
15
- export function collectAimdDiagnostics(content: string): AimdDiagnostic[] {
32
+ const DEFAULT_DIAGNOSTIC_MESSAGES: AimdDiagnosticMessages = {
33
+ figureMissingId: 'This figure is missing the required ID. Generate or enter a unique ID.',
34
+ figureMissingSrc: 'This figure is missing the required src field.',
35
+ figureDuplicateId: id => `Figure ID "${id}" is already used in this document.`,
36
+ generateFigureId: 'Generate figure ID',
37
+ }
38
+
39
+ export function collectAimdDiagnostics(
40
+ content: string,
41
+ messages: AimdDiagnosticMessages = DEFAULT_DIAGNOSTIC_MESSAGES,
42
+ ): AimdDiagnostic[] {
16
43
  const diagnostics: AimdDiagnostic[] = []
17
44
 
18
45
  for (const match of content.matchAll(AIMD_VAR_TEMPLATE_PATTERN)) {
@@ -57,5 +84,58 @@ export function collectAimdDiagnostics(content: string): AimdDiagnostic[] {
57
84
  }
58
85
  }
59
86
 
87
+ const figures = scanAimdFigureBlocks(content)
88
+ const generatedByBlock = new Map(
89
+ normalizeAimdFigureIds(content).generated.map(item => [item.blockStartOffset, item]),
90
+ )
91
+ const seenFigureIds = new Set<string>()
92
+
93
+ for (const figure of figures) {
94
+ const id = figure.id?.value ?? ''
95
+ const src = figure.src?.value ?? ''
96
+
97
+ if (!id) {
98
+ const generated = generatedByBlock.get(figure.startOffset)
99
+ diagnostics.push({
100
+ code: 'fig-missing-id',
101
+ message: messages.figureMissingId,
102
+ severity: 'error',
103
+ startOffset: figure.startOffset,
104
+ endOffset: figure.endOffset,
105
+ ...(generated
106
+ ? {
107
+ quickFix: {
108
+ title: messages.generateFigureId,
109
+ edit: generated.edit,
110
+ },
111
+ }
112
+ : {}),
113
+ })
114
+ }
115
+
116
+ if (!src) {
117
+ diagnostics.push({
118
+ code: 'fig-missing-src',
119
+ message: messages.figureMissingSrc,
120
+ severity: 'error',
121
+ startOffset: figure.startOffset,
122
+ endOffset: figure.endOffset,
123
+ })
124
+ }
125
+
126
+ if (id) {
127
+ if (seenFigureIds.has(id)) {
128
+ diagnostics.push({
129
+ code: 'fig-duplicate-id',
130
+ message: messages.figureDuplicateId(id),
131
+ severity: 'error',
132
+ startOffset: figure.startOffset,
133
+ endOffset: figure.endOffset,
134
+ })
135
+ }
136
+ seenFigureIds.add(id)
137
+ }
138
+ }
139
+
60
140
  return diagnostics
61
141
  }
@@ -207,6 +207,12 @@ export interface AimdEditorMessages {
207
207
  quizBlockLabel: string
208
208
  quizBlock: string
209
209
  }
210
+ diagnostics: {
211
+ figureMissingId: string
212
+ figureMissingSrc: string
213
+ figureDuplicateId: (id: string) => string
214
+ generateFigureId: string
215
+ }
210
216
  }
211
217
 
212
218
  export type AimdEditorMessagesInput = DeepPartial<AimdEditorMessages>
@@ -422,6 +428,12 @@ const EN_US_MESSAGES: AimdEditorMessages = {
422
428
  quizBlockLabel: 'quiz block',
423
429
  quizBlock: 'Insert AIMD quiz code block',
424
430
  },
431
+ diagnostics: {
432
+ figureMissingId: 'This figure is missing the required ID. Generate or enter a unique ID.',
433
+ figureMissingSrc: 'This figure is missing the required src field.',
434
+ figureDuplicateId: id => `Figure ID "${id}" is already used in this document.`,
435
+ generateFigureId: 'Generate figure ID',
436
+ },
425
437
  }
426
438
 
427
439
  const ZH_CN_MESSAGES: AimdEditorMessages = {
@@ -619,6 +631,12 @@ const ZH_CN_MESSAGES: AimdEditorMessages = {
619
631
  quizBlockLabel: '题目代码块',
620
632
  quizBlock: '插入 AIMD 题目代码块',
621
633
  },
634
+ diagnostics: {
635
+ figureMissingId: '该图片缺少必填的 ID,请生成或填写一个唯一 ID。',
636
+ figureMissingSrc: '该图片缺少必填的 src 字段。',
637
+ figureDuplicateId: id => `图片 ID“${id}”在当前文档中已存在。`,
638
+ generateFigureId: '生成图片 ID',
639
+ },
622
640
  }
623
641
 
624
642
  const BASE_MESSAGES: Record<AimdEditorLocale, AimdEditorMessages> = {
@@ -58,7 +58,7 @@ export function useEditorContent(options: UseEditorContentOptions) {
58
58
 
59
59
  function shouldReparseInsertedText(text: string): boolean {
60
60
  return /\{\{(?:var_table|var|quiz|step|check|ref_step|ref_var|ref_fig|cite|fig)\|/.test(text)
61
- || /```(?:quiz|fig|assigner)\b/.test(text)
61
+ || /```(?:quiz|fig|assigner|connectors)\b/.test(text)
62
62
  }
63
63
 
64
64
  function reparseMilkdownMarkdown() {