@airalogy/aimd-recorder 1.14.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.
Files changed (178) hide show
  1. package/README.md +240 -0
  2. package/README.zh-CN.md +224 -0
  3. package/dist/AimdCodeField-DC3DPvgc.js +172 -0
  4. package/dist/AimdDnaSequenceField-__jdnFS9.js +59466 -0
  5. package/dist/AimdEditor.vue_vue_type_style_index_0_lang-Bq79rKNw.js +28370 -0
  6. package/dist/AimdMarkdownField-98ImHGbc.js +66 -0
  7. package/dist/AimdMarkdownNoteField-CuUJZkgT.js +185 -0
  8. package/dist/_.contribution-IP0cDNhs.js +38 -0
  9. package/dist/_commonjsHelpers-CZXJ37nu.js +40 -0
  10. package/dist/abap-Bs8kvjsq.js +1398 -0
  11. package/dist/aimd-recorder.css +1 -0
  12. package/dist/apex-Dspwrl5m.js +326 -0
  13. package/dist/assets/css.worker-Dmf38jDD.js +79 -0
  14. package/dist/assets/editor.worker-CpuX8TSY.js +12 -0
  15. package/dist/assets/html.worker-DotaEs7v.js +456 -0
  16. package/dist/assets/json.worker-B4VHhH6h.js +44 -0
  17. package/dist/assets/ts.worker-sl17zBDo.js +51316 -0
  18. package/dist/azcli-k1BiOau1.js +68 -0
  19. package/dist/bat-BIP72KWc.js +100 -0
  20. package/dist/bicep-VhQeYhS3.js +102 -0
  21. package/dist/cameligo-DnuAXtHg.js +174 -0
  22. package/dist/clojure-BAuDPsal.js +761 -0
  23. package/dist/coffee-BV06qSnj.js +232 -0
  24. package/dist/components.js +3595 -0
  25. package/dist/composables.js +72 -0
  26. package/dist/cpp-Cz8Jeyrf.js +389 -0
  27. package/dist/csharp-F6hhlXFb.js +326 -0
  28. package/dist/csp-KzlEHCyI.js +53 -0
  29. package/dist/css-e4FKntZ3.js +187 -0
  30. package/dist/cssMode-uz9U6SUU.js +1571 -0
  31. package/dist/cypher-CpvDGKRr.js +263 -0
  32. package/dist/dart-DdHeFWR0.js +281 -0
  33. package/dist/dockerfile-aoNXDcio.js +130 -0
  34. package/dist/ecl-C4Q0DYuA.js +456 -0
  35. package/dist/editor.api-CgD9w0gG.js +77091 -0
  36. package/dist/editor.main-Du02abDl.js +33783 -0
  37. package/dist/elixir-C9X42b0T.js +569 -0
  38. package/dist/flow9-Bff42pfB.js +142 -0
  39. package/dist/freemarker2-DFLX4iQ-.js +989 -0
  40. package/dist/fsharp-C6vPAsHG.js +217 -0
  41. package/dist/go-DEr8zx9f.js +218 -0
  42. package/dist/graphql-BiNBBRFh.js +151 -0
  43. package/dist/handlebars-aiZbIuWY.js +419 -0
  44. package/dist/hcl-2T9lKuFg.js +183 -0
  45. package/dist/html-phzTMObN.js +308 -0
  46. package/dist/htmlMode-BF7wMi3N.js +1581 -0
  47. package/dist/index.js +89 -0
  48. package/dist/ini-CZGtssYM.js +71 -0
  49. package/dist/ini.contribution-DyWtiacL.js +8 -0
  50. package/dist/java-B260NeH3.js +232 -0
  51. package/dist/javascript-BKKOItMi.js +70 -0
  52. package/dist/jsonMode-CDYK4B5l.js +1996 -0
  53. package/dist/julia-kuNLM9c5.js +511 -0
  54. package/dist/kotlin-Bz6kAefA.js +253 -0
  55. package/dist/less-AZUtS89I.js +162 -0
  56. package/dist/lexon-BcmgnyRb.js +157 -0
  57. package/dist/liquid-B2eRz5SA.js +240 -0
  58. package/dist/lua-Dz1RbATg.js +162 -0
  59. package/dist/m3-CIMooLFn.js +210 -0
  60. package/dist/markdown-C1BsCK9P.js +229 -0
  61. package/dist/markdown.contribution-DYYk6zUU.js +7 -0
  62. package/dist/mdx-BfV1HDRG.js +165 -0
  63. package/dist/mips-BovsnfLQ.js +198 -0
  64. package/dist/monaco.contribution-C-bQhUTI.js +114 -0
  65. package/dist/monaco.contribution-CCVvl3Jx.js +72 -0
  66. package/dist/monaco.contribution-Col-f0XR.js +164 -0
  67. package/dist/monaco.contribution-b5xkgQ-s.js +112 -0
  68. package/dist/msdax-CiRB2ZB-.js +375 -0
  69. package/dist/mysql-BwdpPE19.js +878 -0
  70. package/dist/objective-c-D4nfHfso.js +183 -0
  71. package/dist/pascal--j5-PYtz.js +251 -0
  72. package/dist/pascaligo-C7TcJxNL.js +164 -0
  73. package/dist/perl-BzEWqUb5.js +626 -0
  74. package/dist/pgsql-B_KZ0mGX.js +851 -0
  75. package/dist/php-A19QHzQV.js +500 -0
  76. package/dist/pla-COeKY1hA.js +137 -0
  77. package/dist/postiats-BfMp7zJL.js +907 -0
  78. package/dist/powerquery-B0TS_5Gb.js +890 -0
  79. package/dist/powershell-5jSbKdYB.js +239 -0
  80. package/dist/protobuf-YgnOutn3.js +420 -0
  81. package/dist/pug-DLej6kn5.js +402 -0
  82. package/dist/python-CQrZx9mR.js +301 -0
  83. package/dist/python.contribution-CcKs2Ktm.js +8 -0
  84. package/dist/qsharp-AhjqtSHB.js +301 -0
  85. package/dist/r-D-F4P80w.js +243 -0
  86. package/dist/razor-DWmpHMcx.js +550 -0
  87. package/dist/redis-hwwIlMnV.js +302 -0
  88. package/dist/redshift-Bcm_kjEG.js +809 -0
  89. package/dist/restructuredtext-CrdzfPP-.js +174 -0
  90. package/dist/ruby-QC2UNm1s.js +511 -0
  91. package/dist/rust-5aqiQ3L5.js +343 -0
  92. package/dist/sb-B1g1ZfLJ.js +115 -0
  93. package/dist/scala-C2U4R5Gb.js +370 -0
  94. package/dist/scheme-DeFRbslr.js +108 -0
  95. package/dist/scss-RSTCiCmI.js +262 -0
  96. package/dist/shell-BpF-iNu8.js +221 -0
  97. package/dist/shell.contribution-B80NZ3iO.js +7 -0
  98. package/dist/solidity-C00r2mbQ.js +1367 -0
  99. package/dist/sophia-DVW5ZKDB.js +199 -0
  100. package/dist/sparql-B0nmpxeX.js +201 -0
  101. package/dist/sql-CJwKPERn.js +853 -0
  102. package/dist/sql.contribution-DQy36aE5.js +7 -0
  103. package/dist/st-DPlKG5DT.js +416 -0
  104. package/dist/swift-YZBAbVHV.js +309 -0
  105. package/dist/systemverilog-CDQSaH63.js +576 -0
  106. package/dist/tcl-Dlu8sp6Q.js +232 -0
  107. package/dist/tsMode-Bh-v12OL.js +887 -0
  108. package/dist/twig-C5TO7-kw.js +392 -0
  109. package/dist/typescript-c7y6IZVP.js +343 -0
  110. package/dist/typespec-BsmjDEu7.js +117 -0
  111. package/dist/useFieldRendering-BIqeE3qo.js +6083 -0
  112. package/dist/vb-ChHvIDrk.js +372 -0
  113. package/dist/wgsl-XEp1ju8c.js +439 -0
  114. package/dist/xml-MAEprTnF.js +95 -0
  115. package/dist/xml.contribution-XMOaon8i.js +27 -0
  116. package/dist/yaml-C1p8rXG7.js +206 -0
  117. package/dist/yaml.contribution-DRRVhJmJ.js +8 -0
  118. package/package.json +88 -0
  119. package/src/__tests__/client-assigner.test.ts +341 -0
  120. package/src/__tests__/code-types.test.ts +30 -0
  121. package/src/__tests__/markdown-field.test.ts +34 -0
  122. package/src/__tests__/markdown-note-field.test.ts +135 -0
  123. package/src/__tests__/quiz-recorder.test.ts +195 -0
  124. package/src/__tests__/recorder-editor-content-sync.test.ts +172 -0
  125. package/src/__tests__/recorder-editor.test.ts +143 -0
  126. package/src/__tests__/recorder-render.test.ts +57 -0
  127. package/src/__tests__/recorder-wysiwyg-field-host.test.ts +33 -0
  128. package/src/__tests__/step-field.test.ts +270 -0
  129. package/src/__tests__/type-plugins.test.ts +59 -0
  130. package/src/__tests__/var-field-render.test.ts +26 -0
  131. package/src/client-assigner.ts +269 -0
  132. package/src/code-types.ts +94 -0
  133. package/src/components/AimdCodeField.vue +228 -0
  134. package/src/components/AimdDnaSequenceField.vue +983 -0
  135. package/src/components/AimdMarkdownField.vue +168 -0
  136. package/src/components/AimdMarkdownNoteField.vue +457 -0
  137. package/src/components/AimdQuizRecorder.vue +792 -0
  138. package/src/components/AimdRecorder.vue +1959 -0
  139. package/src/components/AimdRecorderEditor.vue +2281 -0
  140. package/src/components/AimdRecorderWysiwygFieldHost.vue +93 -0
  141. package/src/components/AimdRecorderWysiwygSurface.vue +487 -0
  142. package/src/components/AimdSeqVizViewer.vue +122 -0
  143. package/src/components/AimdStepCheckField.vue +453 -0
  144. package/src/components/AimdVarField.vue +213 -0
  145. package/src/components/AimdVarTableField.vue +516 -0
  146. package/src/components/DnaAnnotationEditor.vue +477 -0
  147. package/src/components/DnaSequenceEditor.vue +44 -0
  148. package/src/components/DnaSequenceToolbar.vue +98 -0
  149. package/src/components/DnaSequenceViewer.vue +162 -0
  150. package/src/components/index.ts +21 -0
  151. package/src/components/recorderMilkdownPlugin.ts +632 -0
  152. package/src/composables/__tests__/useDnaSequence.test.ts +467 -0
  153. package/src/composables/__tests__/useFieldAdapters.test.ts +115 -0
  154. package/src/composables/__tests__/useFocusManagement.test.ts +66 -0
  155. package/src/composables/__tests__/useRecordState.test.ts +586 -0
  156. package/src/composables/__tests__/useStepTimers.test.ts +64 -0
  157. package/src/composables/__tests__/useVarHelpers.test.ts +424 -0
  158. package/src/composables/__tests__/workbenchFieldEditing.test.ts +97 -0
  159. package/src/composables/index.ts +94 -0
  160. package/src/composables/useClientAssignerRunner.ts +74 -0
  161. package/src/composables/useDnaSequence.ts +400 -0
  162. package/src/composables/useDnaSequenceField.ts +316 -0
  163. package/src/composables/useFieldAdapters.ts +61 -0
  164. package/src/composables/useFieldRendering.ts +269 -0
  165. package/src/composables/useFocusManagement.ts +144 -0
  166. package/src/composables/useRecordState.ts +429 -0
  167. package/src/composables/useStepTimers.ts +243 -0
  168. package/src/composables/useVarHelpers.ts +545 -0
  169. package/src/composables/useVarTableDragDrop.ts +236 -0
  170. package/src/composables/workbenchFieldEditing.ts +705 -0
  171. package/src/css.d.ts +9 -0
  172. package/src/index.ts +65 -0
  173. package/src/locales.ts +559 -0
  174. package/src/monaco-code.ts +75 -0
  175. package/src/styles/aimd.css +994 -0
  176. package/src/type-plugins.ts +132 -0
  177. package/src/type-utils.ts +3 -0
  178. package/src/types.ts +191 -0
@@ -0,0 +1,269 @@
1
+ import { validateClientAssignerFunctionSource } from "@airalogy/aimd-core/parser"
2
+ import type { AimdClientAssignerField } from "@airalogy/aimd-core/types"
3
+
4
+ type ClientAssignerExecutor = (dependentFields: Record<string, unknown>) => unknown
5
+
6
+ const executorCache = new Map<string, ClientAssignerExecutor>()
7
+
8
+ const SAFE_MATH = Object.freeze({
9
+ abs: Math.abs,
10
+ ceil: Math.ceil,
11
+ floor: Math.floor,
12
+ max: Math.max,
13
+ min: Math.min,
14
+ pow: Math.pow,
15
+ round: Math.round,
16
+ trunc: Math.trunc,
17
+ })
18
+
19
+ const SAFE_OBJECT = Object.freeze({
20
+ entries: Object.entries,
21
+ fromEntries: Object.fromEntries,
22
+ keys: Object.keys,
23
+ values: Object.values,
24
+ })
25
+
26
+ const SAFE_ARRAY = Object.freeze({
27
+ isArray: Array.isArray,
28
+ })
29
+
30
+ const SAFE_JSON = Object.freeze({
31
+ parse: JSON.parse,
32
+ stringify: JSON.stringify,
33
+ })
34
+
35
+ function isObjectLike(value: unknown): value is Record<string, unknown> {
36
+ return !!value && typeof value === "object" && !Array.isArray(value)
37
+ }
38
+
39
+ function cloneValue<T>(value: T): T {
40
+ if (Array.isArray(value)) {
41
+ return value.map(item => cloneValue(item)) as T
42
+ }
43
+ if (isObjectLike(value)) {
44
+ return Object.fromEntries(
45
+ Object.entries(value).map(([key, item]) => [key, cloneValue(item)]),
46
+ ) as T
47
+ }
48
+ return value
49
+ }
50
+
51
+ function isDeepEqual(left: unknown, right: unknown): boolean {
52
+ if (Object.is(left, right)) {
53
+ return true
54
+ }
55
+
56
+ if (Array.isArray(left) && Array.isArray(right)) {
57
+ if (left.length !== right.length) {
58
+ return false
59
+ }
60
+ return left.every((item, index) => isDeepEqual(item, right[index]))
61
+ }
62
+
63
+ if (isObjectLike(left) && isObjectLike(right)) {
64
+ const leftKeys = Object.keys(left)
65
+ const rightKeys = Object.keys(right)
66
+ if (leftKeys.length !== rightKeys.length) {
67
+ return false
68
+ }
69
+ return leftKeys.every(key => key in right && isDeepEqual(left[key], right[key]))
70
+ }
71
+
72
+ return false
73
+ }
74
+
75
+ function isReadyValue(value: unknown): boolean {
76
+ if (value === null || typeof value === "undefined") {
77
+ return false
78
+ }
79
+ if (typeof value === "string") {
80
+ return value.trim().length > 0
81
+ }
82
+ return true
83
+ }
84
+
85
+ function getAssignerDependencies(assigner: AimdClientAssignerField, fieldOwners: Map<string, string>): Set<string> {
86
+ const dependencies = new Set<string>()
87
+ for (const field of assigner.dependent_fields) {
88
+ const owner = fieldOwners.get(field)
89
+ if (owner && owner !== assigner.id) {
90
+ dependencies.add(owner)
91
+ }
92
+ }
93
+ return dependencies
94
+ }
95
+
96
+ function sortClientAssigners(assigners: AimdClientAssignerField[]): AimdClientAssignerField[] {
97
+ const assignerById = new Map(assigners.map(assigner => [assigner.id, assigner]))
98
+ const fieldOwners = new Map<string, string>()
99
+
100
+ for (const assigner of assigners) {
101
+ for (const field of assigner.assigned_fields) {
102
+ fieldOwners.set(field, assigner.id)
103
+ }
104
+ }
105
+
106
+ const visited = new Set<string>()
107
+ const order: AimdClientAssignerField[] = []
108
+
109
+ function visit(assigner: AimdClientAssignerField): void {
110
+ if (visited.has(assigner.id)) {
111
+ return
112
+ }
113
+ visited.add(assigner.id)
114
+ for (const dependencyId of getAssignerDependencies(assigner, fieldOwners)) {
115
+ const dependency = assignerById.get(dependencyId)
116
+ if (dependency) {
117
+ visit(dependency)
118
+ }
119
+ }
120
+ order.push(assigner)
121
+ }
122
+
123
+ for (const assigner of assigners) {
124
+ visit(assigner)
125
+ }
126
+
127
+ return order
128
+ }
129
+
130
+ function shouldRunAutoFirst(assigner: AimdClientAssignerField, values: Record<string, unknown>): boolean {
131
+ return assigner.assigned_fields.every(field => !isReadyValue(values[field]))
132
+ }
133
+
134
+ function areDependenciesReady(assigner: AimdClientAssignerField, values: Record<string, unknown>): boolean {
135
+ return assigner.dependent_fields.every(field => isReadyValue(values[field]))
136
+ }
137
+
138
+ function compileClientAssigner(assigner: AimdClientAssignerField): ClientAssignerExecutor {
139
+ const cached = executorCache.get(assigner.function_source)
140
+ if (cached) {
141
+ return cached
142
+ }
143
+
144
+ validateClientAssignerFunctionSource(assigner.function_source, assigner.id)
145
+
146
+ const executorFactory = new Function(
147
+ "__math__",
148
+ "__object__",
149
+ "__array__",
150
+ "__json__",
151
+ `"use strict";
152
+ const Math = __math__;
153
+ const Object = __object__;
154
+ const Array = __array__;
155
+ const JSON = __json__;
156
+ const window = undefined;
157
+ const document = undefined;
158
+ const globalThis = undefined;
159
+ const self = undefined;
160
+ const fetch = undefined;
161
+ const XMLHttpRequest = undefined;
162
+ const WebSocket = undefined;
163
+ const Function = undefined;
164
+ const setTimeout = undefined;
165
+ const setInterval = undefined;
166
+ const Date = undefined;
167
+ return (${assigner.function_source});
168
+ `,
169
+ )
170
+
171
+ const executor = executorFactory(
172
+ SAFE_MATH,
173
+ SAFE_OBJECT,
174
+ SAFE_ARRAY,
175
+ SAFE_JSON,
176
+ )
177
+
178
+ if (typeof executor !== "function") {
179
+ throw new Error(`client assigner "${assigner.id}" did not compile to a callable function`)
180
+ }
181
+
182
+ executorCache.set(assigner.function_source, executor as ClientAssignerExecutor)
183
+ return executor as ClientAssignerExecutor
184
+ }
185
+
186
+ function executeClientAssigner(
187
+ assigner: AimdClientAssignerField,
188
+ values: Record<string, unknown>,
189
+ ): Record<string, unknown> {
190
+ const executor = compileClientAssigner(assigner)
191
+ const dependentFields = Object.fromEntries(
192
+ assigner.dependent_fields.map(field => [field, cloneValue(values[field])]),
193
+ )
194
+
195
+ const result = executor(dependentFields)
196
+ if (!isObjectLike(result)) {
197
+ throw new Error(`client assigner "${assigner.id}" must return an object`)
198
+ }
199
+
200
+ const missingFields = assigner.assigned_fields.filter(field => !(field in result))
201
+ if (missingFields.length > 0) {
202
+ throw new Error(`client assigner "${assigner.id}" is missing assigned fields: ${missingFields.join(", ")}`)
203
+ }
204
+
205
+ const extraFields = Object.keys(result).filter(field => !assigner.assigned_fields.includes(field))
206
+ if (extraFields.length > 0) {
207
+ throw new Error(`client assigner "${assigner.id}" returned fields not declared in assigned_fields: ${extraFields.join(", ")}`)
208
+ }
209
+
210
+ return Object.fromEntries(
211
+ assigner.assigned_fields.map(field => [field, cloneValue(result[field])]),
212
+ ) as Record<string, unknown>
213
+ }
214
+
215
+ export interface ClientAssignerApplyOptions {
216
+ triggerIds?: string[]
217
+ }
218
+
219
+ export interface ClientAssignerApplyResult {
220
+ changed: boolean
221
+ changedFields: string[]
222
+ }
223
+
224
+ /**
225
+ * Apply frontend-only assigners to the mutable var scope.
226
+ */
227
+ export function applyClientAssigners(
228
+ assigners: AimdClientAssignerField[],
229
+ values: Record<string, unknown>,
230
+ options?: ClientAssignerApplyOptions,
231
+ ): ClientAssignerApplyResult {
232
+ if (assigners.length === 0) {
233
+ return { changed: false, changedFields: [] }
234
+ }
235
+
236
+ const explicitlyTriggeredIds = new Set(options?.triggerIds ?? [])
237
+ const changedFields = new Set<string>()
238
+
239
+ for (const assigner of sortClientAssigners(assigners)) {
240
+ if (!areDependenciesReady(assigner, values)) {
241
+ continue
242
+ }
243
+
244
+ const explicitlyTriggered = explicitlyTriggeredIds.has(assigner.id)
245
+
246
+ if (!explicitlyTriggered) {
247
+ if (assigner.mode === "manual") {
248
+ continue
249
+ }
250
+ if (assigner.mode === "auto_first" && !shouldRunAutoFirst(assigner, values)) {
251
+ continue
252
+ }
253
+ }
254
+
255
+ const nextAssignments = executeClientAssigner(assigner, values)
256
+
257
+ for (const [field, nextValue] of Object.entries(nextAssignments)) {
258
+ if (!isDeepEqual(values[field], nextValue)) {
259
+ values[field] = nextValue
260
+ changedFields.add(field)
261
+ }
262
+ }
263
+ }
264
+
265
+ return {
266
+ changed: changedFields.size > 0,
267
+ changedFields: Array.from(changedFields),
268
+ }
269
+ }
@@ -0,0 +1,94 @@
1
+ import type { AimdFieldMeta } from './types'
2
+ import { normalizeAimdTypeName } from './type-utils'
3
+
4
+ const CODE_LANGUAGE_BY_TYPE: Record<string, string> = {
5
+ codestr: 'plaintext',
6
+ pystr: 'python',
7
+ jsstr: 'javascript',
8
+ tsstr: 'typescript',
9
+ jsonstr: 'json',
10
+ yamlstr: 'yaml',
11
+ tomlstr: 'ini',
12
+ }
13
+
14
+ const CODE_LANGUAGE_ALIASES: Record<string, string> = {
15
+ code: 'plaintext',
16
+ plaintext: 'plaintext',
17
+ py: 'python',
18
+ python: 'python',
19
+ js: 'javascript',
20
+ javascript: 'javascript',
21
+ ts: 'typescript',
22
+ typescript: 'typescript',
23
+ json: 'json',
24
+ yaml: 'yaml',
25
+ yml: 'yaml',
26
+ toml: 'ini',
27
+ ini: 'ini',
28
+ sh: 'shell',
29
+ shell: 'shell',
30
+ bash: 'shell',
31
+ zsh: 'shell',
32
+ sql: 'sql',
33
+ html: 'html',
34
+ css: 'css',
35
+ scss: 'scss',
36
+ less: 'less',
37
+ xml: 'xml',
38
+ }
39
+
40
+ export const BUILT_IN_CODE_STR_TYPES = [
41
+ 'CodeStr',
42
+ 'PyStr',
43
+ 'JsStr',
44
+ 'TsStr',
45
+ 'JsonStr',
46
+ 'TomlStr',
47
+ 'YamlStr',
48
+ ] as const
49
+
50
+ function normalizeKnownCodeLanguage(value: string | undefined): string | null {
51
+ if (typeof value !== 'string' || !value.trim()) {
52
+ return null
53
+ }
54
+
55
+ const normalized = normalizeAimdTypeName(value)
56
+ return CODE_LANGUAGE_BY_TYPE[normalized] ?? CODE_LANGUAGE_ALIASES[normalized] ?? null
57
+ }
58
+
59
+ function normalizeCustomCodeLanguage(value: string | undefined): string | null {
60
+ if (typeof value !== 'string' || !value.trim()) {
61
+ return null
62
+ }
63
+
64
+ const normalized = normalizeAimdTypeName(value)
65
+ if (normalized === 'text') {
66
+ return 'plaintext'
67
+ }
68
+
69
+ return normalizeKnownCodeLanguage(value) ?? value.trim().toLowerCase()
70
+ }
71
+
72
+ export function resolveAimdCodeEditorLanguage(
73
+ type: string | undefined,
74
+ fieldMeta?: Pick<AimdFieldMeta, 'inputType' | 'codeLanguage'>,
75
+ ): string | null {
76
+ const explicitLanguage = normalizeCustomCodeLanguage(fieldMeta?.codeLanguage)
77
+ if (explicitLanguage) {
78
+ return explicitLanguage
79
+ }
80
+
81
+ const overrideLanguage = normalizeKnownCodeLanguage(fieldMeta?.inputType)
82
+ if (overrideLanguage) {
83
+ return overrideLanguage
84
+ }
85
+
86
+ return normalizeKnownCodeLanguage(type)
87
+ }
88
+
89
+ export function isAimdCodeEditorType(
90
+ type: string | undefined,
91
+ fieldMeta?: Pick<AimdFieldMeta, 'inputType' | 'codeLanguage'>,
92
+ ): boolean {
93
+ return resolveAimdCodeEditorLanguage(type, fieldMeta) !== null
94
+ }
@@ -0,0 +1,228 @@
1
+ <script setup lang="ts">
2
+ import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
3
+ import { ensureMonacoEnvironment, ensureMonacoLanguageContribution } from '../monaco-code'
4
+
5
+ const props = withDefaults(defineProps<{
6
+ modelValue?: string | number
7
+ language: string
8
+ disabled?: boolean
9
+ }>(), {
10
+ modelValue: '',
11
+ disabled: false,
12
+ })
13
+
14
+ const emit = defineEmits<{
15
+ (e: 'update:modelValue', value: string): void
16
+ (e: 'blur'): void
17
+ }>()
18
+
19
+ const editorContainer = ref<HTMLElement | null>(null)
20
+ const loading = ref(true)
21
+ const loadError = ref<string | null>(null)
22
+ const draftValue = ref(normalizeCodeFieldValue(props.modelValue))
23
+
24
+ let monacoModule: typeof import('monaco-editor') | null = null
25
+ let monacoEditor: import('monaco-editor').editor.IStandaloneCodeEditor | null = null
26
+ let monacoModel: import('monaco-editor').editor.ITextModel | null = null
27
+ let isSyncing = false
28
+
29
+ function normalizeCodeFieldValue(value: string | number | undefined): string {
30
+ if (typeof value === 'string') {
31
+ return value
32
+ }
33
+
34
+ if (typeof value === 'number') {
35
+ return String(value)
36
+ }
37
+
38
+ return ''
39
+ }
40
+
41
+ function emitDraftValue(nextValue: string) {
42
+ if (nextValue === draftValue.value) {
43
+ return
44
+ }
45
+
46
+ draftValue.value = nextValue
47
+ emit('update:modelValue', nextValue)
48
+ }
49
+
50
+ async function applyLanguage(language: string) {
51
+ if (!monacoModule || !monacoModel) {
52
+ return
53
+ }
54
+
55
+ await ensureMonacoLanguageContribution(language)
56
+ monacoModule.editor.setModelLanguage(monacoModel, language)
57
+ }
58
+
59
+ async function createEditor() {
60
+ if (!editorContainer.value || monacoEditor) {
61
+ return
62
+ }
63
+
64
+ loading.value = true
65
+ loadError.value = null
66
+
67
+ try {
68
+ ensureMonacoEnvironment()
69
+ monacoModule = await import('monaco-editor')
70
+ await ensureMonacoLanguageContribution(props.language)
71
+
72
+ monacoEditor = monacoModule.editor.create(editorContainer.value, {
73
+ value: draftValue.value,
74
+ language: props.language,
75
+ theme: 'vs',
76
+ minimap: { enabled: false },
77
+ scrollBeyondLastLine: false,
78
+ automaticLayout: true,
79
+ lineNumbers: 'on',
80
+ wordWrap: 'on',
81
+ tabSize: 2,
82
+ padding: { top: 12, bottom: 12 },
83
+ readOnly: props.disabled,
84
+ })
85
+
86
+ monacoModel = monacoEditor.getModel()
87
+
88
+ monacoEditor.onDidChangeModelContent(() => {
89
+ if (isSyncing || !monacoEditor) {
90
+ return
91
+ }
92
+
93
+ emitDraftValue(monacoEditor.getValue())
94
+ })
95
+
96
+ monacoEditor.onDidBlurEditorWidget(() => {
97
+ emit('blur')
98
+ })
99
+ } catch (error) {
100
+ loadError.value = error instanceof Error ? error.message : 'Failed to load Monaco editor.'
101
+ } finally {
102
+ loading.value = false
103
+ }
104
+ }
105
+
106
+ function syncEditorValue(nextValue: string) {
107
+ draftValue.value = nextValue
108
+
109
+ if (!monacoEditor || nextValue === monacoEditor.getValue()) {
110
+ return
111
+ }
112
+
113
+ isSyncing = true
114
+ monacoEditor.setValue(nextValue)
115
+ isSyncing = false
116
+ }
117
+
118
+ function onFallbackInput(event: Event) {
119
+ emitDraftValue((event.target as HTMLTextAreaElement).value)
120
+ }
121
+
122
+ onMounted(() => {
123
+ void createEditor()
124
+ })
125
+
126
+ onBeforeUnmount(() => {
127
+ monacoEditor?.dispose()
128
+ monacoModel?.dispose()
129
+ })
130
+
131
+ watch(() => props.modelValue, (value) => {
132
+ syncEditorValue(normalizeCodeFieldValue(value))
133
+ })
134
+
135
+ watch(() => props.disabled, (disabled) => {
136
+ monacoEditor?.updateOptions({ readOnly: disabled })
137
+ })
138
+
139
+ watch(() => props.language, async (language) => {
140
+ if (!monacoEditor) {
141
+ return
142
+ }
143
+
144
+ await applyLanguage(language)
145
+ })
146
+ </script>
147
+
148
+ <template>
149
+ <div class="aimd-code-field" :class="{ 'aimd-code-field--disabled': disabled }">
150
+ <div v-if="loadError" class="aimd-code-field__fallback-shell">
151
+ <textarea
152
+ class="aimd-code-field__fallback"
153
+ :value="draftValue"
154
+ :disabled="disabled"
155
+ spellcheck="false"
156
+ @input="onFallbackInput"
157
+ @blur="emit('blur')"
158
+ />
159
+ </div>
160
+
161
+ <template v-else>
162
+ <div v-if="loading" class="aimd-code-field__loading">Loading code editor…</div>
163
+ <div ref="editorContainer" class="aimd-code-field__editor" />
164
+ </template>
165
+ </div>
166
+ </template>
167
+
168
+ <style scoped>
169
+ .aimd-code-field {
170
+ position: relative;
171
+ width: 100%;
172
+ min-width: 0;
173
+ border-top: 1px solid var(--aimd-border-color, #90caf9);
174
+ background: #fff;
175
+ }
176
+
177
+ .aimd-code-field__loading,
178
+ .aimd-code-field__fallback-shell {
179
+ display: flex;
180
+ width: 100%;
181
+ min-height: 240px;
182
+ align-items: stretch;
183
+ }
184
+
185
+ .aimd-code-field__loading {
186
+ position: absolute;
187
+ inset: 0;
188
+ z-index: 1;
189
+ align-items: center;
190
+ justify-content: center;
191
+ color: #667085;
192
+ font-size: 13px;
193
+ background: linear-gradient(180deg, #fcfdff 0%, #f7fbff 100%);
194
+ }
195
+
196
+ .aimd-code-field__editor {
197
+ width: 100%;
198
+ min-height: 240px;
199
+ }
200
+
201
+ .aimd-code-field__editor :deep(.monaco-editor),
202
+ .aimd-code-field__editor :deep(.monaco-editor-background) {
203
+ background: #fff;
204
+ }
205
+
206
+ .aimd-code-field__fallback {
207
+ width: 100%;
208
+ min-height: 240px;
209
+ padding: 12px 14px;
210
+ border: 0 none;
211
+ resize: vertical;
212
+ outline: none;
213
+ box-sizing: border-box;
214
+ background: #fff;
215
+ color: #101828;
216
+ font-size: 13px;
217
+ line-height: 1.6;
218
+ font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
219
+ }
220
+
221
+ .aimd-code-field--disabled {
222
+ opacity: 0.98;
223
+ }
224
+
225
+ .aimd-code-field--disabled .aimd-code-field__fallback {
226
+ background: #f8fbff;
227
+ }
228
+ </style>