@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,429 @@
1
+ /**
2
+ * Record-state normalisation and field-default helpers.
3
+ *
4
+ * Extracted from AimdRecorder.vue so the logic can be reused
5
+ * by both the recorder and the host-app rendering pipeline.
6
+ */
7
+
8
+ import type { AimdQuizField, AimdVarTableField } from "@airalogy/aimd-core/types"
9
+ import type {
10
+ AimdCheckRecordItem,
11
+ AimdProtocolRecordData,
12
+ AimdStepRecordItem,
13
+ } from "../types"
14
+ import { createEmptyProtocolRecordData } from "../types"
15
+ import {
16
+ createEmptyCheckRecordItem,
17
+ createEmptyStepRecordItem,
18
+ normalizeStepTimerState,
19
+ } from "./useStepTimers"
20
+
21
+ // ---------------------------------------------------------------------------
22
+ // Deep clone
23
+ // ---------------------------------------------------------------------------
24
+
25
+ export function cloneRecordData(value: AimdProtocolRecordData): AimdProtocolRecordData {
26
+ return JSON.parse(JSON.stringify(value))
27
+ }
28
+
29
+ function stableSerialize(value: unknown): string {
30
+ if (value == null || typeof value !== "object") {
31
+ return JSON.stringify(value)
32
+ }
33
+
34
+ if (Array.isArray(value)) {
35
+ return `[${value.map(item => stableSerialize(item)).join(",")}]`
36
+ }
37
+
38
+ const entries = Object.entries(value as Record<string, unknown>)
39
+ .sort(([leftKey], [rightKey]) => leftKey.localeCompare(rightKey))
40
+
41
+ return `{${entries.map(([key, item]) => `${JSON.stringify(key)}:${stableSerialize(item)}`).join(",")}}`
42
+ }
43
+
44
+ export function getRecordDataSignature(value: Partial<AimdProtocolRecordData> | undefined): string {
45
+ return stableSerialize(normalizeIncomingRecord(value))
46
+ }
47
+
48
+ // ---------------------------------------------------------------------------
49
+ // Step / check normalisation
50
+ // ---------------------------------------------------------------------------
51
+
52
+ export function normalizeCheckLike(value: unknown): AimdCheckRecordItem {
53
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
54
+ return createEmptyCheckRecordItem()
55
+ }
56
+
57
+ const obj = value as Record<string, unknown>
58
+ return {
59
+ checked: Boolean(obj.checked),
60
+ annotation: typeof obj.annotation === "string" ? obj.annotation : "",
61
+ }
62
+ }
63
+
64
+ export function normalizeStepLike(value: unknown): AimdStepRecordItem {
65
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
66
+ return createEmptyStepRecordItem()
67
+ }
68
+
69
+ const obj = value as Record<string, unknown>
70
+ return {
71
+ checked: Boolean(obj.checked),
72
+ annotation: typeof obj.annotation === "string" ? obj.annotation : "",
73
+ ...normalizeStepTimerState(obj),
74
+ }
75
+ }
76
+
77
+ // ---------------------------------------------------------------------------
78
+ // Incoming record normalisation
79
+ // ---------------------------------------------------------------------------
80
+
81
+ export function normalizeIncomingRecord(value: Partial<AimdProtocolRecordData> | undefined): AimdProtocolRecordData {
82
+ const normalized = createEmptyProtocolRecordData()
83
+ if (!value || typeof value !== "object") {
84
+ return normalized
85
+ }
86
+
87
+ if (value.var && typeof value.var === "object") {
88
+ normalized.var = { ...value.var }
89
+ }
90
+ if (value.quiz && typeof value.quiz === "object") {
91
+ normalized.quiz = { ...value.quiz }
92
+ }
93
+ if (value.step && typeof value.step === "object") {
94
+ for (const [key, item] of Object.entries(value.step)) {
95
+ normalized.step[key] = normalizeStepLike(item)
96
+ }
97
+ }
98
+ if (value.check && typeof value.check === "object") {
99
+ for (const [key, item] of Object.entries(value.check)) {
100
+ normalized.check[key] = normalizeCheckLike(item)
101
+ }
102
+ }
103
+
104
+ return normalized
105
+ }
106
+
107
+ // ---------------------------------------------------------------------------
108
+ // Section replacement (reactive-safe)
109
+ // ---------------------------------------------------------------------------
110
+
111
+ export function replaceSection(target: Record<string, unknown>, source: Record<string, unknown>) {
112
+ for (const key of Object.keys(target)) {
113
+ delete target[key]
114
+ }
115
+ Object.assign(target, source)
116
+ }
117
+
118
+ export function applyNormalizedRecord(localRecord: AimdProtocolRecordData, normalized: AimdProtocolRecordData) {
119
+ replaceSection(localRecord.var as Record<string, unknown>, normalized.var)
120
+ replaceSection(localRecord.step as Record<string, unknown>, normalized.step as Record<string, unknown>)
121
+ replaceSection(localRecord.check as Record<string, unknown>, normalized.check as Record<string, unknown>)
122
+ replaceSection(localRecord.quiz as Record<string, unknown>, normalized.quiz)
123
+ }
124
+
125
+ export function applyIncomingRecord(localRecord: AimdProtocolRecordData, value: Partial<AimdProtocolRecordData> | undefined) {
126
+ applyNormalizedRecord(localRecord, normalizeIncomingRecord(value))
127
+ }
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // Field normalisation helpers
131
+ // ---------------------------------------------------------------------------
132
+
133
+ export function normalizeStepFields(raw: unknown): Array<{ name: string }> {
134
+ if (!Array.isArray(raw)) {
135
+ return []
136
+ }
137
+ return raw
138
+ .map((item) => {
139
+ if (typeof item === "string" && item.trim()) {
140
+ return { name: item.trim() }
141
+ }
142
+ if (item && typeof item === "object" && typeof (item as any).name === "string") {
143
+ return { name: (item as any).name as string }
144
+ }
145
+ return null
146
+ })
147
+ .filter((item): item is { name: string } => item !== null)
148
+ }
149
+
150
+ export function normalizeCheckFields(raw: unknown): Array<{ name: string, label?: string }> {
151
+ if (!Array.isArray(raw)) {
152
+ return []
153
+ }
154
+
155
+ const normalized: Array<{ name: string, label?: string }> = []
156
+ for (const item of raw) {
157
+ if (typeof item === "string" && item.trim()) {
158
+ const label = item.trim()
159
+ normalized.push({ name: label, label })
160
+ continue
161
+ }
162
+
163
+ if (item && typeof item === "object") {
164
+ const obj = item as Record<string, unknown>
165
+ if (typeof obj.name === "string") {
166
+ normalized.push({
167
+ name: obj.name,
168
+ label: typeof obj.label === "string" ? obj.label : undefined,
169
+ })
170
+ }
171
+ }
172
+ }
173
+
174
+ return normalized
175
+ }
176
+
177
+ export function normalizeQuizFields(raw: unknown): AimdQuizField[] {
178
+ if (!Array.isArray(raw)) {
179
+ return []
180
+ }
181
+
182
+ return raw.filter((item): item is AimdQuizField => (
183
+ !!item
184
+ && typeof item === "object"
185
+ && typeof (item as any).id === "string"
186
+ && typeof (item as any).type === "string"
187
+ && typeof (item as any).stem === "string"
188
+ ))
189
+ }
190
+
191
+ export function normalizeVarTableFields(raw: unknown): AimdVarTableField[] {
192
+ if (!Array.isArray(raw)) {
193
+ return []
194
+ }
195
+
196
+ return raw.filter((item): item is AimdVarTableField => (
197
+ !!item
198
+ && typeof item === "object"
199
+ && typeof (item as any).id === "string"
200
+ && Array.isArray((item as any).subvars)
201
+ ))
202
+ }
203
+
204
+ // ---------------------------------------------------------------------------
205
+ // Quiz default values
206
+ // ---------------------------------------------------------------------------
207
+
208
+ export function getQuizDefaultValue(quiz: AimdQuizField): unknown {
209
+ if (quiz.type === "choice") {
210
+ const optionKeys = new Set((quiz.options || []).map(option => option.key))
211
+ if (quiz.mode === "multiple") {
212
+ if (Array.isArray(quiz.default)) {
213
+ return quiz.default.filter((item): item is string => typeof item === "string" && optionKeys.has(item))
214
+ }
215
+ return []
216
+ }
217
+
218
+ if (typeof quiz.default === "string" && optionKeys.has(quiz.default)) {
219
+ return quiz.default
220
+ }
221
+ return ""
222
+ }
223
+
224
+ if (quiz.type === "blank") {
225
+ const blankKeys = (quiz.blanks || []).map(blank => blank.key)
226
+ if (quiz.default && typeof quiz.default === "object" && !Array.isArray(quiz.default)) {
227
+ const objDefault = quiz.default as Record<string, unknown>
228
+ const normalized: Record<string, string> = {}
229
+ for (const key of blankKeys) {
230
+ const value = objDefault[key]
231
+ normalized[key] = typeof value === "string" ? value : ""
232
+ }
233
+ return normalized
234
+ }
235
+
236
+ if (typeof quiz.default === "string" && blankKeys.length === 1) {
237
+ return { [blankKeys[0]]: quiz.default }
238
+ }
239
+
240
+ const blankValueMap: Record<string, string> = {}
241
+ for (const key of blankKeys) {
242
+ blankValueMap[key] = ""
243
+ }
244
+ return blankValueMap
245
+ }
246
+
247
+ if (quiz.type === "scale") {
248
+ const itemKeys = (quiz.items || []).map(item => item.key)
249
+ const optionKeys = new Set((quiz.options || []).map(option => option.key))
250
+ const defaultMap: Record<string, string> = {}
251
+ const rawDefault = quiz.default && typeof quiz.default === "object" && !Array.isArray(quiz.default)
252
+ ? quiz.default as Record<string, unknown>
253
+ : {}
254
+
255
+ for (const key of itemKeys) {
256
+ const value = rawDefault[key]
257
+ defaultMap[key] = typeof value === "string" && optionKeys.has(value) ? value : ""
258
+ }
259
+
260
+ return defaultMap
261
+ }
262
+
263
+ if (typeof quiz.default === "string") {
264
+ return quiz.default
265
+ }
266
+ return ""
267
+ }
268
+
269
+ // ---------------------------------------------------------------------------
270
+ // Var-table row helpers
271
+ // ---------------------------------------------------------------------------
272
+
273
+ export function createEmptyVarTableRow(columns: string[]): Record<string, string> {
274
+ const row: Record<string, string> = {}
275
+ for (const column of columns) {
276
+ row[column] = ""
277
+ }
278
+ return row
279
+ }
280
+
281
+ export function normalizeVarTableRows(raw: unknown, columns: string[]): Record<string, string>[] {
282
+ if (!Array.isArray(raw)) {
283
+ return [createEmptyVarTableRow(columns)]
284
+ }
285
+
286
+ const rows = raw.map((item) => {
287
+ if (!item || typeof item !== "object" || Array.isArray(item)) {
288
+ return createEmptyVarTableRow(columns)
289
+ }
290
+ const source = item as Record<string, unknown>
291
+ const row: Record<string, string> = {}
292
+ for (const column of columns) {
293
+ const value = source[column]
294
+ row[column] = typeof value === "string" ? value : `${value ?? ""}`
295
+ }
296
+ return row
297
+ })
298
+
299
+ if (rows.length === 0) {
300
+ rows.push(createEmptyVarTableRow(columns))
301
+ }
302
+
303
+ return rows
304
+ }
305
+
306
+ export interface VarTablePastedCell {
307
+ rowIndex: number
308
+ column: string
309
+ value: string
310
+ }
311
+
312
+ export interface ApplyVarTablePasteResult {
313
+ rowsAdded: number
314
+ changedCells: VarTablePastedCell[]
315
+ }
316
+
317
+ export function parsePastedVarTableText(text: string): string[][] {
318
+ if (text === "") {
319
+ return []
320
+ }
321
+
322
+ const normalized = text.replace(/\r\n?/g, "\n")
323
+ const lines = normalized.split("\n")
324
+ while (lines.length > 0 && lines[lines.length - 1] === "") {
325
+ lines.pop()
326
+ }
327
+
328
+ return lines.map(line => line.split("\t"))
329
+ }
330
+
331
+ export function applyPastedVarTableGrid(
332
+ rows: Record<string, string>[],
333
+ columns: string[],
334
+ startRowIndex: number,
335
+ startColumnIndex: number,
336
+ pastedGrid: string[][],
337
+ options?: { disabledColumns?: string[] },
338
+ ): ApplyVarTablePasteResult {
339
+ const changedCells: VarTablePastedCell[] = []
340
+ if (
341
+ columns.length === 0
342
+ || pastedGrid.length === 0
343
+ || startRowIndex < 0
344
+ || startColumnIndex < 0
345
+ ) {
346
+ return { rowsAdded: 0, changedCells }
347
+ }
348
+
349
+ const disabledColumns = new Set(options?.disabledColumns ?? [])
350
+ let rowsAdded = 0
351
+
352
+ for (const [rowOffset, pastedRow] of pastedGrid.entries()) {
353
+ const targetRowIndex = startRowIndex + rowOffset
354
+ const writableCells = pastedRow
355
+ .map((value, columnOffset) => ({
356
+ value,
357
+ columnIndex: startColumnIndex + columnOffset,
358
+ }))
359
+ .filter(({ columnIndex }) => columnIndex >= 0 && columnIndex < columns.length)
360
+ .map(({ value, columnIndex }) => ({
361
+ value,
362
+ column: columns[columnIndex],
363
+ }))
364
+ .filter(({ column }) => !disabledColumns.has(column))
365
+
366
+ if (writableCells.length === 0) {
367
+ continue
368
+ }
369
+
370
+ while (targetRowIndex >= rows.length) {
371
+ rows.push(createEmptyVarTableRow(columns))
372
+ rowsAdded += 1
373
+ }
374
+
375
+ const targetRow = rows[targetRowIndex]
376
+ for (const { column, value } of writableCells) {
377
+ if (targetRow[column] === value) {
378
+ continue
379
+ }
380
+ targetRow[column] = value
381
+ changedCells.push({ rowIndex: targetRowIndex, column, value })
382
+ }
383
+ }
384
+
385
+ return { rowsAdded, changedCells }
386
+ }
387
+
388
+ // ---------------------------------------------------------------------------
389
+ // Ensure defaults from extracted fields
390
+ // ---------------------------------------------------------------------------
391
+
392
+ import type { ExtractedAimdFields } from "@airalogy/aimd-core/types"
393
+
394
+ export function ensureDefaultsFromFields(localRecord: AimdProtocolRecordData, fields: ExtractedAimdFields): boolean {
395
+ let changed = false
396
+
397
+ for (const vt of normalizeVarTableFields(fields.var_table)) {
398
+ const columns = vt.subvars.map(subvar => subvar.id)
399
+ const rows = localRecord.var[vt.id]
400
+ const normalizedRows = normalizeVarTableRows(rows, columns)
401
+ if (JSON.stringify(normalizedRows) !== JSON.stringify(rows)) {
402
+ localRecord.var[vt.id] = normalizedRows
403
+ changed = true
404
+ }
405
+ }
406
+
407
+ for (const step of normalizeStepFields(fields.step)) {
408
+ if (!(step.name in localRecord.step)) {
409
+ localRecord.step[step.name] = createEmptyStepRecordItem()
410
+ changed = true
411
+ }
412
+ }
413
+
414
+ for (const check of normalizeCheckFields(fields.check)) {
415
+ if (!(check.name in localRecord.check)) {
416
+ localRecord.check[check.name] = createEmptyCheckRecordItem()
417
+ changed = true
418
+ }
419
+ }
420
+
421
+ for (const quiz of normalizeQuizFields(fields.quiz)) {
422
+ if (!(quiz.id in localRecord.quiz)) {
423
+ localRecord.quiz[quiz.id] = getQuizDefaultValue(quiz)
424
+ changed = true
425
+ }
426
+ }
427
+
428
+ return changed
429
+ }
@@ -0,0 +1,243 @@
1
+ import type { AimdStepField, AimdStepTimerMode } from "@airalogy/aimd-core/types"
2
+ import type { AimdCheckRecordItem, AimdStepRecordItem } from "../types"
3
+
4
+ export function createEmptyCheckRecordItem(): AimdCheckRecordItem {
5
+ return {
6
+ checked: false,
7
+ annotation: "",
8
+ }
9
+ }
10
+
11
+ export function createEmptyStepRecordItem(): AimdStepRecordItem {
12
+ return {
13
+ ...createEmptyCheckRecordItem(),
14
+ elapsed_ms: 0,
15
+ timer_started_at_ms: null,
16
+ started_at_ms: null,
17
+ ended_at_ms: null,
18
+ }
19
+ }
20
+
21
+ function normalizeFiniteNumber(value: unknown, fallback: number | null = 0): number | null {
22
+ if (value == null) {
23
+ return fallback
24
+ }
25
+
26
+ const numeric = typeof value === "number"
27
+ ? value
28
+ : (typeof value === "string" && value.trim() ? Number(value) : Number.NaN)
29
+
30
+ if (!Number.isFinite(numeric)) {
31
+ return fallback
32
+ }
33
+
34
+ return Math.max(0, numeric)
35
+ }
36
+
37
+ export function isStepTimerRunning(step: Pick<AimdStepRecordItem, "timer_started_at_ms">): boolean {
38
+ return typeof step.timer_started_at_ms === "number" && Number.isFinite(step.timer_started_at_ms)
39
+ }
40
+
41
+ export function getStepElapsedMs(step: Pick<AimdStepRecordItem, "elapsed_ms" | "timer_started_at_ms">, nowMs = Date.now()): number {
42
+ const storedElapsedMs = normalizeFiniteNumber(step.elapsed_ms, 0) ?? 0
43
+ if (!isStepTimerRunning(step)) {
44
+ return storedElapsedMs
45
+ }
46
+
47
+ const timerStartedAtMs = normalizeFiniteNumber(step.timer_started_at_ms, null)
48
+ if (timerStartedAtMs == null) {
49
+ return storedElapsedMs
50
+ }
51
+
52
+ return storedElapsedMs + Math.max(0, nowMs - timerStartedAtMs)
53
+ }
54
+
55
+ export function startStepTimer(step: AimdStepRecordItem, nowMs = Date.now()): boolean {
56
+ if (isStepTimerRunning(step)) {
57
+ return false
58
+ }
59
+
60
+ if (step.started_at_ms == null) {
61
+ step.started_at_ms = nowMs
62
+ }
63
+ step.timer_started_at_ms = nowMs
64
+ step.ended_at_ms = null
65
+ return true
66
+ }
67
+
68
+ export function pauseStepTimer(step: AimdStepRecordItem, nowMs = Date.now()): boolean {
69
+ if (!isStepTimerRunning(step)) {
70
+ return false
71
+ }
72
+
73
+ step.elapsed_ms = getStepElapsedMs(step, nowMs)
74
+ step.timer_started_at_ms = null
75
+ if (step.started_at_ms == null) {
76
+ step.started_at_ms = nowMs
77
+ }
78
+ step.ended_at_ms = nowMs
79
+ return true
80
+ }
81
+
82
+ export function resetStepTimer(step: AimdStepRecordItem): boolean {
83
+ const hadTimingData = (
84
+ step.elapsed_ms > 0
85
+ || step.timer_started_at_ms != null
86
+ || step.started_at_ms != null
87
+ || step.ended_at_ms != null
88
+ )
89
+
90
+ step.elapsed_ms = 0
91
+ step.timer_started_at_ms = null
92
+ step.started_at_ms = null
93
+ step.ended_at_ms = null
94
+
95
+ return hadTimingData
96
+ }
97
+
98
+ export function setStepChecked(step: AimdStepRecordItem, checked: boolean, nowMs = Date.now()): boolean {
99
+ if (checked && isStepTimerRunning(step)) {
100
+ pauseStepTimer(step, nowMs)
101
+ }
102
+
103
+ const changed = step.checked !== checked
104
+ step.checked = checked
105
+ return changed
106
+ }
107
+
108
+ export function formatStepDuration(ms: number, locale: string | undefined): string {
109
+ const totalSeconds = Math.max(0, Math.round(ms / 1000))
110
+ const days = Math.floor(totalSeconds / (24 * 3600))
111
+ const hours = Math.floor((totalSeconds % (24 * 3600)) / 3600)
112
+ const minutes = Math.floor((totalSeconds % 3600) / 60)
113
+ const seconds = totalSeconds % 60
114
+ const zh = locale?.toLowerCase().startsWith("zh")
115
+
116
+ const parts: string[] = []
117
+ if (days > 0) {
118
+ parts.push(zh ? `${days}天` : `${days}d`)
119
+ }
120
+ if (hours > 0) {
121
+ parts.push(zh ? `${hours}小时` : `${hours}h`)
122
+ }
123
+ if (minutes > 0) {
124
+ parts.push(zh ? `${minutes}分` : `${minutes}m`)
125
+ }
126
+ if (seconds > 0 || parts.length === 0) {
127
+ parts.push(zh ? `${seconds}秒` : `${seconds}s`)
128
+ }
129
+
130
+ return zh ? parts.join("") : parts.join(" ")
131
+ }
132
+
133
+ export function resolveStepTimerMode(
134
+ step: Pick<AimdStepField, "timer_mode" | "estimated_duration_ms">,
135
+ ): AimdStepTimerMode {
136
+ const configuredMode = step.timer_mode ?? "elapsed"
137
+ const estimate = normalizeFiniteNumber(step.estimated_duration_ms, null)
138
+
139
+ if ((configuredMode === "countdown" || configuredMode === "both") && (estimate == null || estimate <= 0)) {
140
+ return "elapsed"
141
+ }
142
+
143
+ return configuredMode
144
+ }
145
+
146
+ export function getStepRemainingMs(
147
+ step: Pick<AimdStepRecordItem, "elapsed_ms" | "timer_started_at_ms">,
148
+ estimatedDurationMs: number | undefined,
149
+ nowMs = Date.now(),
150
+ ): number | undefined {
151
+ const estimate = normalizeFiniteNumber(estimatedDurationMs, null)
152
+ if (estimate == null || estimate <= 0) {
153
+ return undefined
154
+ }
155
+
156
+ return estimate - getStepElapsedMs(step, nowMs)
157
+ }
158
+
159
+ export function isStepTimerWarning(
160
+ remainingMs: number | undefined,
161
+ estimatedDurationMs: number | undefined,
162
+ ): boolean {
163
+ const estimate = normalizeFiniteNumber(estimatedDurationMs, null)
164
+ if (estimate == null || estimate <= 0 || remainingMs == null || remainingMs <= 0) {
165
+ return false
166
+ }
167
+
168
+ return remainingMs <= 10_000 || remainingMs / estimate <= 0.2
169
+ }
170
+
171
+ export function getProtocolEstimatedDurationMs(steps: AimdStepField[] | undefined): number {
172
+ if (!steps?.length) {
173
+ return 0
174
+ }
175
+
176
+ const byId = new Map(steps.map(step => [step.id, step]))
177
+ let total = 0
178
+
179
+ for (const step of steps) {
180
+ if (typeof step.estimated_duration_ms !== "number" || !Number.isFinite(step.estimated_duration_ms) || step.estimated_duration_ms <= 0) {
181
+ continue
182
+ }
183
+
184
+ let coveredByAncestor = false
185
+ let parentId = step.parent_id
186
+ while (parentId) {
187
+ const parent = byId.get(parentId)
188
+ if (!parent) {
189
+ break
190
+ }
191
+ if (typeof parent.estimated_duration_ms === "number" && Number.isFinite(parent.estimated_duration_ms) && parent.estimated_duration_ms > 0) {
192
+ coveredByAncestor = true
193
+ break
194
+ }
195
+ parentId = parent.parent_id
196
+ }
197
+
198
+ if (!coveredByAncestor) {
199
+ total += step.estimated_duration_ms
200
+ }
201
+ }
202
+
203
+ return total
204
+ }
205
+
206
+ export function getProtocolRecordedDurationMs(
207
+ steps: Record<string, AimdStepRecordItem>,
208
+ nowMs = Date.now(),
209
+ ): number {
210
+ return Object.values(steps).reduce((total, step) => total + getStepElapsedMs(step, nowMs), 0)
211
+ }
212
+
213
+ export function hasRecordedStepDuration(step: Pick<AimdStepRecordItem, "elapsed_ms" | "timer_started_at_ms" | "started_at_ms" | "ended_at_ms">): boolean {
214
+ return (
215
+ getStepElapsedMs(step as Pick<AimdStepRecordItem, "elapsed_ms" | "timer_started_at_ms">) > 0
216
+ || step.started_at_ms != null
217
+ || step.ended_at_ms != null
218
+ )
219
+ }
220
+
221
+ export function normalizeStepTimerState(value: unknown): Pick<AimdStepRecordItem, "elapsed_ms" | "timer_started_at_ms" | "started_at_ms" | "ended_at_ms"> {
222
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
223
+ return {
224
+ elapsed_ms: 0,
225
+ timer_started_at_ms: null,
226
+ started_at_ms: null,
227
+ ended_at_ms: null,
228
+ }
229
+ }
230
+
231
+ const obj = value as Record<string, unknown>
232
+ const elapsed_ms = normalizeFiniteNumber(obj.elapsed_ms, 0) ?? 0
233
+ const timer_started_at_ms = normalizeFiniteNumber(obj.timer_started_at_ms, null)
234
+ const started_at_ms = normalizeFiniteNumber(obj.started_at_ms, null)
235
+ const ended_at_ms = normalizeFiniteNumber(obj.ended_at_ms, null)
236
+
237
+ return {
238
+ elapsed_ms,
239
+ timer_started_at_ms,
240
+ started_at_ms,
241
+ ended_at_ms,
242
+ }
243
+ }