asma-core-helpers 0.26.0 → 0.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/collaboration/applyOperation.d.ts.map +1 -1
- package/lib/collaboration/applyOperation.js +354 -69
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/index.d.ts +1 -0
- package/lib/collaboration/index.d.ts.map +1 -1
- package/lib/collaboration/index.js +1 -0
- package/lib/collaboration/index.js.map +1 -1
- package/lib/collaboration/operations.d.ts +35 -1
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js +7 -0
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +125 -2
- package/lib/collaboration/schemas.d.ts.map +1 -1
- package/lib/collaboration/schemas.js +135 -10
- package/lib/collaboration/schemas.js.map +1 -1
- package/lib/collaboration/templateDocument.d.ts +28 -1
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.d.ts +4 -0
- package/lib/collaboration/testHelpers/documentGenerator.d.ts.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.js +100 -4
- package/lib/collaboration/testHelpers/documentGenerator.js.map +1 -1
- package/package.json +1 -1
- package/src/collaboration/applyOperation.test.ts +379 -12
- package/src/collaboration/applyOperation.ts +411 -76
- package/src/collaboration/index.ts +1 -0
- package/src/collaboration/operations.ts +35 -0
- package/src/collaboration/property.test.ts +24 -1
- package/src/collaboration/schemas.test.ts +136 -1
- package/src/collaboration/schemas.ts +167 -5
- package/src/collaboration/templateDocument.ts +27 -1
- package/src/collaboration/testHelpers/documentGenerator.ts +101 -5
|
@@ -5,7 +5,6 @@ import type {
|
|
|
5
5
|
QnrQuestion,
|
|
6
6
|
QnrTemplateDocument,
|
|
7
7
|
QuestionId,
|
|
8
|
-
RuleCondition,
|
|
9
8
|
} from './templateDocument.js'
|
|
10
9
|
import { bindingTargetKey } from './templateDocument.js'
|
|
11
10
|
|
|
@@ -115,6 +114,38 @@ const omitKey = <V>(record: Record<string, V>, key: string): Record<string, V> =
|
|
|
115
114
|
return rest
|
|
116
115
|
}
|
|
117
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The document paths a grid operation owns, and no other op may write.
|
|
119
|
+
*
|
|
120
|
+
* `grid.columnIds` is the ownership array a question's single structural owner is read from
|
|
121
|
+
* (`gridColumn.create`/`move` maintain it), and `presentation.rowEditor.layoutByQuestionId`
|
|
122
|
+
* holds one placement per column (`gridColumn.setLayout` writes exactly one). An op that
|
|
123
|
+
* carries a scalar or a flat primitive array can only write such a map *wholesale*, so
|
|
124
|
+
* reaching either path through `question.updateField` replaces or clears it — columns left
|
|
125
|
+
* owned by nobody, or every other column's placement dropped in one edit. That is precisely
|
|
126
|
+
* the atomicity the grid ops exist to provide, so the paths are reserved for them.
|
|
127
|
+
*/
|
|
128
|
+
const GRID_OWNED_FIELD_PATHS = ['grid.columnIds', 'presentation.rowEditor.layoutByQuestionId'] as const
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The reserved path `field` would write, if any. Both directions count: the path itself, a
|
|
132
|
+
* path *below* it (`grid.columnIds.0`), and an *ancestor* whose write would take the reserved
|
|
133
|
+
* map with it (`grid`, `presentation.rowEditor`).
|
|
134
|
+
*/
|
|
135
|
+
const reservedGridFieldPath = (field: string): string | undefined =>
|
|
136
|
+
GRID_OWNED_FIELD_PATHS.find(
|
|
137
|
+
(owned) => field === owned || field.startsWith(`${owned}.`) || owned.startsWith(`${field}.`),
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
const requireGridQuestion = (doc: QnrTemplateDocument, questionId: QuestionId): QnrQuestion => {
|
|
141
|
+
const question = doc.questionsById?.[questionId]
|
|
142
|
+
if (!question) throw new OperationConflictError(`Unknown question "${questionId}"`)
|
|
143
|
+
if (question.type !== 'QuestionGrid') {
|
|
144
|
+
throw new OperationConflictError(`Question "${questionId}" is not a question grid`)
|
|
145
|
+
}
|
|
146
|
+
return question
|
|
147
|
+
}
|
|
148
|
+
|
|
118
149
|
/**
|
|
119
150
|
* Spreads `patch` onto `base`, **omitting** keys whose value is `undefined` rather than
|
|
120
151
|
* assigning it. Required by `exactOptionalPropertyTypes`: `{ ...doc, x: undefined }` is
|
|
@@ -151,6 +182,10 @@ const OPTIONAL_COLLECTIONS = [
|
|
|
151
182
|
'visibilityRuleOrderByQuestionId',
|
|
152
183
|
'highlightRulesById',
|
|
153
184
|
'highlightRuleOrderByQuestionId',
|
|
185
|
+
'narrativeRulesById',
|
|
186
|
+
'narrativeRuleOrderByQuestionId',
|
|
187
|
+
'qnrRulesById',
|
|
188
|
+
'qnrRuleOrderByQuestionId',
|
|
154
189
|
'dataMappingsById',
|
|
155
190
|
'mappingNodesById',
|
|
156
191
|
'mappingBindingsById',
|
|
@@ -202,13 +237,13 @@ const setOrRemoveOrder = (
|
|
|
202
237
|
* one owner: setting it under a different question MOVES it there, so it can never sit in two
|
|
203
238
|
* order arrays at once — and the previous owner's key goes when its last rule leaves (DOC-LAW-2).
|
|
204
239
|
*/
|
|
205
|
-
const setScopedRule = (
|
|
206
|
-
rulesById: Record<string,
|
|
240
|
+
const setScopedRule = <T extends object>(
|
|
241
|
+
rulesById: Record<string, T> | undefined,
|
|
207
242
|
orderByQuestionId: Record<QuestionId, string[]> | undefined,
|
|
208
243
|
ruleId: string,
|
|
209
244
|
questionId: QuestionId,
|
|
210
|
-
|
|
211
|
-
): { rulesById: Record<string,
|
|
245
|
+
rule: T,
|
|
246
|
+
): { rulesById: Record<string, T>; orderByQuestionId: Record<QuestionId, string[]> } => {
|
|
212
247
|
const orders: Record<QuestionId, string[]> = {}
|
|
213
248
|
for (const [ownerId, ruleIds] of Object.entries(orderByQuestionId ?? {})) {
|
|
214
249
|
const kept = ownerId === questionId ? ruleIds : ruleIds.filter((id) => id !== ruleId)
|
|
@@ -217,7 +252,24 @@ const setScopedRule = (
|
|
|
217
252
|
const order = orders[questionId] ?? []
|
|
218
253
|
orders[questionId] = order.includes(ruleId) ? order : [...order, ruleId]
|
|
219
254
|
return {
|
|
220
|
-
rulesById: { ...(rulesById ?? {}), [ruleId]:
|
|
255
|
+
rulesById: { ...(rulesById ?? {}), [ruleId]: rule },
|
|
256
|
+
orderByQuestionId: orders,
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Removes one scoped rule and any order key emptied by that removal. */
|
|
261
|
+
const deleteScopedRule = <T>(
|
|
262
|
+
rulesById: Record<string, T> | undefined,
|
|
263
|
+
orderByQuestionId: Record<QuestionId, string[]> | undefined,
|
|
264
|
+
ruleId: string,
|
|
265
|
+
): { rulesById: Record<string, T>; orderByQuestionId: Record<QuestionId, string[]> } => {
|
|
266
|
+
const orders: Record<QuestionId, string[]> = {}
|
|
267
|
+
for (const [questionId, ruleIds] of Object.entries(orderByQuestionId ?? {})) {
|
|
268
|
+
const kept = ruleIds.filter((id) => id !== ruleId)
|
|
269
|
+
if (kept.length > 0) orders[questionId] = kept
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
rulesById: rulesById ? omitKey(rulesById, ruleId) : {},
|
|
221
273
|
orderByQuestionId: orders,
|
|
222
274
|
}
|
|
223
275
|
}
|
|
@@ -287,29 +339,193 @@ const scrubBindingOrders = (doc: QnrTemplateDocument, bindingIds: ReadonlySet<st
|
|
|
287
339
|
return changed ? { ...doc, dataMappingsById: next } : doc
|
|
288
340
|
}
|
|
289
341
|
|
|
290
|
-
|
|
342
|
+
type ScrubbedPresentation = { value: Record<string, unknown>; changed: boolean }
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Removes a question key from convention-owned presentation maps at any depth. The
|
|
346
|
+
* presentation bag deliberately has an open index, so enumerating today's layout and
|
|
347
|
+
* width maps would make tomorrow's map retain dangling column state.
|
|
348
|
+
*/
|
|
349
|
+
const scrubPresentationQuestionMaps = (
|
|
350
|
+
value: Record<string, unknown>,
|
|
351
|
+
questionId: QuestionId,
|
|
352
|
+
): ScrubbedPresentation => {
|
|
353
|
+
let changed = false
|
|
354
|
+
const next: Record<string, unknown> = {}
|
|
355
|
+
for (const [key, child] of Object.entries(value)) {
|
|
356
|
+
if (typeof child !== 'object' || child === null || Array.isArray(child)) {
|
|
357
|
+
next[key] = child
|
|
358
|
+
continue
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (key.endsWith('ByQuestionId')) {
|
|
362
|
+
const map = child as Record<string, unknown>
|
|
363
|
+
if (!(questionId in map)) {
|
|
364
|
+
next[key] = child
|
|
365
|
+
continue
|
|
366
|
+
}
|
|
367
|
+
changed = true
|
|
368
|
+
const kept = omitKey(map, questionId)
|
|
369
|
+
if (Object.keys(kept).length > 0) next[key] = kept
|
|
370
|
+
continue
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const nested = scrubPresentationQuestionMaps(child as Record<string, unknown>, questionId)
|
|
374
|
+
changed ||= nested.changed
|
|
375
|
+
if (!nested.changed || Object.keys(nested.value).length > 0) next[key] = nested.value
|
|
376
|
+
}
|
|
377
|
+
return { value: changed ? next : value, changed }
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Removes a deleted question's id from every record owned by a grid: its ordered
|
|
382
|
+
* columns, authored row cells, and convention-named presentation maps.
|
|
383
|
+
*/
|
|
291
384
|
const dropFromGridColumns = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTemplateDocument => {
|
|
292
|
-
if (!doc.questionsById) return doc
|
|
293
385
|
let changed = false
|
|
294
|
-
const
|
|
295
|
-
for (const [qid, question] of Object.entries(doc.questionsById)) {
|
|
386
|
+
const nextQuestions: typeof doc.questionsById = {}
|
|
387
|
+
for (const [qid, question] of Object.entries(doc.questionsById ?? {})) {
|
|
296
388
|
// Defensive: the per-type bags are open, so a malformed `columnIds` must not crash
|
|
297
389
|
// the reducer — it is a schema violation for validation to catch, not a crash here.
|
|
298
390
|
const columnIds = question.grid?.columnIds
|
|
299
|
-
|
|
300
|
-
|
|
391
|
+
let nextQuestion: QnrQuestion = question
|
|
392
|
+
if (Array.isArray(columnIds) && columnIds.includes(questionId)) {
|
|
393
|
+
changed = true
|
|
394
|
+
const kept = columnIds.filter((id) => id !== questionId)
|
|
395
|
+
const grid = { ...question.grid }
|
|
396
|
+
if (kept.length === 0) delete grid.columnIds
|
|
397
|
+
else grid.columnIds = kept
|
|
398
|
+
nextQuestion = { ...nextQuestion, grid }
|
|
399
|
+
if (Object.keys(grid).length === 0) delete nextQuestion.grid
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (question.presentation) {
|
|
403
|
+
const scrubbed = scrubPresentationQuestionMaps(question.presentation, questionId)
|
|
404
|
+
if (scrubbed.changed) {
|
|
405
|
+
changed = true
|
|
406
|
+
nextQuestion = { ...nextQuestion }
|
|
407
|
+
if (Object.keys(scrubbed.value).length === 0) delete nextQuestion.presentation
|
|
408
|
+
else nextQuestion.presentation = scrubbed.value
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
nextQuestions[qid] = nextQuestion
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const nextRows: typeof doc.gridRowsById = {}
|
|
415
|
+
for (const [rowId, row] of Object.entries(doc.gridRowsById ?? {})) {
|
|
416
|
+
if (!row.cells || !(questionId in row.cells)) {
|
|
417
|
+
nextRows[rowId] = row
|
|
301
418
|
continue
|
|
302
419
|
}
|
|
303
420
|
changed = true
|
|
304
|
-
const
|
|
305
|
-
const
|
|
306
|
-
if (
|
|
307
|
-
else
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
421
|
+
const cells = omitKey(row.cells, questionId)
|
|
422
|
+
const nextRow = { ...row }
|
|
423
|
+
if (Object.keys(cells).length === 0) delete nextRow.cells
|
|
424
|
+
else nextRow.cells = cells
|
|
425
|
+
nextRows[rowId] = nextRow
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (!changed) return doc
|
|
429
|
+
return patchDocument(doc, {
|
|
430
|
+
questionsById: doc.questionsById ? nextQuestions : undefined,
|
|
431
|
+
gridRowsById: doc.gridRowsById ? nextRows : undefined,
|
|
432
|
+
})
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Every question a grid owns, transitively, the grid itself excluded. `seen` makes it total on
|
|
437
|
+
* malformed data: an ownership cycle is not authorable but is importable, and a delete must
|
|
438
|
+
* terminate on one rather than blow the stack.
|
|
439
|
+
*/
|
|
440
|
+
const collectOwnedColumnIds = (doc: QnrTemplateDocument, gridQuestionId: QuestionId): QuestionId[] => {
|
|
441
|
+
const owned: QuestionId[] = []
|
|
442
|
+
const seen = new Set<QuestionId>([gridQuestionId])
|
|
443
|
+
const queue: QuestionId[] = [gridQuestionId]
|
|
444
|
+
while (queue.length > 0) {
|
|
445
|
+
const current = queue.pop() as QuestionId
|
|
446
|
+
const question = doc.questionsById?.[current]
|
|
447
|
+
// Only a grid owns columns; the per-type bag is open, so a malformed `columnIds` must
|
|
448
|
+
// not crash the reducer — that is a schema violation for validation to report.
|
|
449
|
+
const columnIds = question?.type === 'QuestionGrid' ? question.grid?.columnIds : undefined
|
|
450
|
+
if (!Array.isArray(columnIds)) continue
|
|
451
|
+
for (const columnId of columnIds) {
|
|
452
|
+
if (seen.has(columnId)) continue
|
|
453
|
+
seen.add(columnId)
|
|
454
|
+
owned.push(columnId)
|
|
455
|
+
queue.push(columnId)
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return owned
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Removes one question and everything it alone owns: its alternatives, its own rules, its
|
|
463
|
+
* predefined grid rows, its place in any grid's column list and every binding aimed at it.
|
|
464
|
+
* `question.delete` applies it to the deleted question and to each column that grid owns.
|
|
465
|
+
*/
|
|
466
|
+
const deleteQuestion = (doc: QnrTemplateDocument, questionId: QuestionId): QnrTemplateDocument => {
|
|
467
|
+
// A grid's predefined rows go with it: the order key is dropped below, and a row record
|
|
468
|
+
// no order array reaches is unreachable state that still changes `document_hash`.
|
|
469
|
+
const rowIds = doc.gridRowOrderByQuestionId?.[questionId] ?? []
|
|
470
|
+
let gridRowsById = doc.gridRowsById
|
|
471
|
+
if (gridRowsById) {
|
|
472
|
+
gridRowsById = rowIds.reduce((acc, id) => omitKey(acc, id), gridRowsById)
|
|
473
|
+
}
|
|
474
|
+
const alternativeIds = doc.alternativeOrderByQuestionId?.[questionId] ?? []
|
|
475
|
+
let alternativesById = doc.alternativesById
|
|
476
|
+
if (alternativesById) {
|
|
477
|
+
alternativesById = alternativeIds.reduce((acc, id) => omitKey(acc, id), alternativesById)
|
|
478
|
+
}
|
|
479
|
+
// The question's own rules go with it, the same way its alternatives do: an orphaned
|
|
480
|
+
// rule record is referenced by no order array and only pollutes the hash. Rules of
|
|
481
|
+
// OTHER questions whose condition points at this one are deliberately KEPT — silently
|
|
482
|
+
// dropping a surviving question's authored logic would be data loss, so the dangling
|
|
483
|
+
// `sourceQuestionId` is left for validation to surface instead.
|
|
484
|
+
const visibilityRuleIds = doc.visibilityRuleOrderByQuestionId?.[questionId] ?? []
|
|
485
|
+
let visibilityRulesById = doc.visibilityRulesById
|
|
486
|
+
if (visibilityRulesById) {
|
|
487
|
+
visibilityRulesById = visibilityRuleIds.reduce((acc, id) => omitKey(acc, id), visibilityRulesById)
|
|
488
|
+
}
|
|
489
|
+
const highlightRuleIds = doc.highlightRuleOrderByQuestionId?.[questionId] ?? []
|
|
490
|
+
let highlightRulesById = doc.highlightRulesById
|
|
491
|
+
if (highlightRulesById) {
|
|
492
|
+
highlightRulesById = highlightRuleIds.reduce((acc, id) => omitKey(acc, id), highlightRulesById)
|
|
311
493
|
}
|
|
312
|
-
|
|
494
|
+
const narrativeRuleIds = doc.narrativeRuleOrderByQuestionId?.[questionId] ?? []
|
|
495
|
+
let narrativeRulesById = doc.narrativeRulesById
|
|
496
|
+
if (narrativeRulesById) {
|
|
497
|
+
narrativeRulesById = narrativeRuleIds.reduce((acc, id) => omitKey(acc, id), narrativeRulesById)
|
|
498
|
+
}
|
|
499
|
+
const qnrRuleIds = doc.qnrRuleOrderByQuestionId?.[questionId] ?? []
|
|
500
|
+
let qnrRulesById = doc.qnrRulesById
|
|
501
|
+
if (qnrRulesById) {
|
|
502
|
+
qnrRulesById = qnrRuleIds.reduce((acc, id) => omitKey(acc, id), qnrRulesById)
|
|
503
|
+
}
|
|
504
|
+
const withoutQuestion = patchDocument(doc, {
|
|
505
|
+
questionsById: doc.questionsById && omitKey(doc.questionsById, questionId),
|
|
506
|
+
questionOrder: doc.questionOrder.filter((id) => id !== questionId),
|
|
507
|
+
alternativesById,
|
|
508
|
+
alternativeOrderByQuestionId:
|
|
509
|
+
doc.alternativeOrderByQuestionId && omitKey(doc.alternativeOrderByQuestionId, questionId),
|
|
510
|
+
gridRowsById,
|
|
511
|
+
gridRowOrderByQuestionId: doc.gridRowOrderByQuestionId && omitKey(doc.gridRowOrderByQuestionId, questionId),
|
|
512
|
+
visibilityRulesById,
|
|
513
|
+
visibilityRuleOrderByQuestionId:
|
|
514
|
+
doc.visibilityRuleOrderByQuestionId && omitKey(doc.visibilityRuleOrderByQuestionId, questionId),
|
|
515
|
+
highlightRulesById,
|
|
516
|
+
highlightRuleOrderByQuestionId:
|
|
517
|
+
doc.highlightRuleOrderByQuestionId && omitKey(doc.highlightRuleOrderByQuestionId, questionId),
|
|
518
|
+
narrativeRulesById,
|
|
519
|
+
narrativeRuleOrderByQuestionId:
|
|
520
|
+
doc.narrativeRuleOrderByQuestionId && omitKey(doc.narrativeRuleOrderByQuestionId, questionId),
|
|
521
|
+
qnrRulesById,
|
|
522
|
+
qnrRuleOrderByQuestionId:
|
|
523
|
+
doc.qnrRuleOrderByQuestionId && omitKey(doc.qnrRuleOrderByQuestionId, questionId),
|
|
524
|
+
})
|
|
525
|
+
// A binding pointing at a deleted question is an unresolvable reference the
|
|
526
|
+
// compiler would reject at publication; drop it with its target. A grid column
|
|
527
|
+
// list pointing at it is the same class of reference and goes the same way.
|
|
528
|
+
return dropBindingsForQuestion(dropFromGridColumns(withoutQuestion, questionId), questionId)
|
|
313
529
|
}
|
|
314
530
|
|
|
315
531
|
export const applyOperation = (document: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument => {
|
|
@@ -358,6 +574,29 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
358
574
|
if (!question) {
|
|
359
575
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
360
576
|
}
|
|
577
|
+
// `type` is the question's identity, not one of its fields: `question.create` sets it and
|
|
578
|
+
// every per-type shape hangs off it. A field write moves it alone — a grid flipped to
|
|
579
|
+
// another type keeps the `grid.columnIds` the document schema admits on no other type,
|
|
580
|
+
// its columns then owned by nobody and no longer cascading with its delete.
|
|
581
|
+
if (op.field === 'type' || op.field.startsWith('type.')) {
|
|
582
|
+
throw new OperationConflictError(
|
|
583
|
+
`Field "${op.field}" writes the question type, which only question.create may set`,
|
|
584
|
+
)
|
|
585
|
+
}
|
|
586
|
+
const reserved = reservedGridFieldPath(op.field)
|
|
587
|
+
if (reserved !== undefined) {
|
|
588
|
+
throw new OperationConflictError(
|
|
589
|
+
`Field "${op.field}" writes "${reserved}", which only the gridColumn operations may author`,
|
|
590
|
+
)
|
|
591
|
+
}
|
|
592
|
+
// `grid` is the grid's own configuration and the document schema admits it on no
|
|
593
|
+
// other type, so authoring it through an ordinary question would store a bag no
|
|
594
|
+
// reader projects and the publication schema then refuses.
|
|
595
|
+
if (question.type !== 'QuestionGrid' && op.field.split('.')[0] === 'grid') {
|
|
596
|
+
throw new OperationConflictError(
|
|
597
|
+
`Question "${op.questionId}" is not a question grid, so "${op.field}" is not authorable on it`,
|
|
598
|
+
)
|
|
599
|
+
}
|
|
361
600
|
return {
|
|
362
601
|
...doc,
|
|
363
602
|
questionsById: {
|
|
@@ -371,50 +610,96 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
371
610
|
if (!doc.questionsById?.[op.questionId]) {
|
|
372
611
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
373
612
|
}
|
|
613
|
+
if (!doc.questionOrder.includes(op.questionId)) {
|
|
614
|
+
throw new OperationConflictError(`Question "${op.questionId}" is not top-level`)
|
|
615
|
+
}
|
|
374
616
|
return { ...doc, questionOrder: moveInOrder(doc.questionOrder, op.questionId, op.toIndex) }
|
|
375
617
|
}
|
|
376
618
|
|
|
377
619
|
case 'question.delete': {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
620
|
+
// A grid owns its columns, so they go with it: they sit in no order array of their
|
|
621
|
+
// own, and leaving them behind is the orphan half of the ownership invariant. The
|
|
622
|
+
// whole ownership subtree is collected first, then each question is removed on its
|
|
623
|
+
// own terms — its alternatives, rules and bindings included.
|
|
624
|
+
const ids = [op.questionId, ...collectOwnedColumnIds(doc, op.questionId)]
|
|
625
|
+
return ids.reduce((acc, id) => deleteQuestion(acc, id), doc)
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
case 'gridColumn.create': {
|
|
629
|
+
const gridQuestion = requireGridQuestion(doc, op.questionId)
|
|
630
|
+
if (doc.questionsById?.[op.columnQuestionId]) {
|
|
631
|
+
throw new OperationConflictError(`Question "${op.columnQuestionId}" already exists`)
|
|
632
|
+
}
|
|
633
|
+
const columnIds = gridQuestion.grid?.columnIds ?? []
|
|
634
|
+
return {
|
|
635
|
+
...doc,
|
|
636
|
+
questionsById: {
|
|
637
|
+
...doc.questionsById,
|
|
638
|
+
[op.questionId]: {
|
|
639
|
+
...gridQuestion,
|
|
640
|
+
grid: {
|
|
641
|
+
...(gridQuestion.grid ?? {}),
|
|
642
|
+
columnIds: insertAt(columnIds, op.columnQuestionId, op.atIndex),
|
|
643
|
+
},
|
|
644
|
+
},
|
|
645
|
+
[op.columnQuestionId]: { type: op.questionType },
|
|
646
|
+
},
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
case 'gridColumn.move': {
|
|
651
|
+
const gridQuestion = requireGridQuestion(doc, op.questionId)
|
|
652
|
+
const columnIds = gridQuestion.grid?.columnIds
|
|
653
|
+
if (!doc.questionsById?.[op.columnQuestionId] || !columnIds?.includes(op.columnQuestionId)) {
|
|
654
|
+
throw new OperationConflictError(
|
|
655
|
+
`Question "${op.columnQuestionId}" does not belong to grid "${op.questionId}"`,
|
|
656
|
+
)
|
|
657
|
+
}
|
|
658
|
+
return {
|
|
659
|
+
...doc,
|
|
660
|
+
questionsById: {
|
|
661
|
+
...doc.questionsById,
|
|
662
|
+
[op.questionId]: {
|
|
663
|
+
...gridQuestion,
|
|
664
|
+
grid: {
|
|
665
|
+
...gridQuestion.grid,
|
|
666
|
+
columnIds: moveInOrder(columnIds, op.columnQuestionId, op.toIndex),
|
|
667
|
+
},
|
|
668
|
+
},
|
|
669
|
+
},
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
case 'gridColumn.setLayout': {
|
|
674
|
+
const gridQuestion = requireGridQuestion(doc, op.questionId)
|
|
675
|
+
const columnIds = gridQuestion.grid?.columnIds
|
|
676
|
+
if (!doc.questionsById?.[op.columnQuestionId] || !columnIds?.includes(op.columnQuestionId)) {
|
|
677
|
+
throw new OperationConflictError(
|
|
678
|
+
`Question "${op.columnQuestionId}" does not belong to grid "${op.questionId}"`,
|
|
679
|
+
)
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const presentation = { ...(gridQuestion.presentation ?? {}) }
|
|
683
|
+
const rowEditor = { ...(presentation.rowEditor ?? {}) }
|
|
684
|
+
const layoutByQuestionId = { ...(rowEditor.layoutByQuestionId ?? {}) }
|
|
685
|
+
if (op.placement === null) delete layoutByQuestionId[op.columnQuestionId]
|
|
686
|
+
else layoutByQuestionId[op.columnQuestionId] = op.placement
|
|
687
|
+
|
|
688
|
+
if (Object.keys(layoutByQuestionId).length === 0) delete rowEditor.layoutByQuestionId
|
|
689
|
+
else rowEditor.layoutByQuestionId = layoutByQuestionId
|
|
690
|
+
if (Object.keys(rowEditor).length === 0) delete presentation.rowEditor
|
|
691
|
+
else presentation.rowEditor = rowEditor
|
|
692
|
+
|
|
693
|
+
const nextQuestion = { ...gridQuestion }
|
|
694
|
+
if (Object.keys(presentation).length === 0) delete nextQuestion.presentation
|
|
695
|
+
else nextQuestion.presentation = presentation
|
|
696
|
+
return {
|
|
697
|
+
...doc,
|
|
698
|
+
questionsById: {
|
|
699
|
+
...doc.questionsById,
|
|
700
|
+
[op.questionId]: nextQuestion,
|
|
701
|
+
},
|
|
702
|
+
}
|
|
418
703
|
}
|
|
419
704
|
|
|
420
705
|
case 'gridRow.create': {
|
|
@@ -870,21 +1155,20 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
870
1155
|
doc.visibilityRuleOrderByQuestionId,
|
|
871
1156
|
op.ruleId,
|
|
872
1157
|
op.questionId,
|
|
873
|
-
op.condition,
|
|
1158
|
+
{ condition: op.condition },
|
|
874
1159
|
)
|
|
875
1160
|
return { ...doc, visibilityRulesById: rulesById, visibilityRuleOrderByQuestionId: orderByQuestionId }
|
|
876
1161
|
}
|
|
877
1162
|
|
|
878
1163
|
case 'visibilityRule.delete': {
|
|
879
|
-
const
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
]),
|
|
1164
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(
|
|
1165
|
+
doc.visibilityRulesById,
|
|
1166
|
+
doc.visibilityRuleOrderByQuestionId,
|
|
1167
|
+
op.ruleId,
|
|
884
1168
|
)
|
|
885
1169
|
return patchDocument(doc, {
|
|
886
|
-
visibilityRulesById:
|
|
887
|
-
visibilityRuleOrderByQuestionId:
|
|
1170
|
+
visibilityRulesById: rulesById,
|
|
1171
|
+
visibilityRuleOrderByQuestionId: orderByQuestionId,
|
|
888
1172
|
})
|
|
889
1173
|
}
|
|
890
1174
|
|
|
@@ -897,21 +1181,72 @@ const reduce = (doc: QnrTemplateDocument, op: TemplateOp): QnrTemplateDocument =
|
|
|
897
1181
|
doc.highlightRuleOrderByQuestionId,
|
|
898
1182
|
op.ruleId,
|
|
899
1183
|
op.questionId,
|
|
900
|
-
op.condition,
|
|
1184
|
+
{ condition: op.condition },
|
|
901
1185
|
)
|
|
902
1186
|
return { ...doc, highlightRulesById: rulesById, highlightRuleOrderByQuestionId: orderByQuestionId }
|
|
903
1187
|
}
|
|
904
1188
|
|
|
905
1189
|
case 'highlightRule.delete': {
|
|
906
|
-
const
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1190
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(
|
|
1191
|
+
doc.highlightRulesById,
|
|
1192
|
+
doc.highlightRuleOrderByQuestionId,
|
|
1193
|
+
op.ruleId,
|
|
1194
|
+
)
|
|
1195
|
+
return patchDocument(doc, {
|
|
1196
|
+
highlightRulesById: rulesById,
|
|
1197
|
+
highlightRuleOrderByQuestionId: orderByQuestionId,
|
|
1198
|
+
})
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
case 'narrativeRule.set': {
|
|
1202
|
+
if (!doc.questionsById?.[op.questionId]) {
|
|
1203
|
+
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
1204
|
+
}
|
|
1205
|
+
const { rulesById, orderByQuestionId } = setScopedRule(
|
|
1206
|
+
doc.narrativeRulesById,
|
|
1207
|
+
doc.narrativeRuleOrderByQuestionId,
|
|
1208
|
+
op.ruleId,
|
|
1209
|
+
op.questionId,
|
|
1210
|
+
{ condition: op.condition },
|
|
1211
|
+
)
|
|
1212
|
+
return { ...doc, narrativeRulesById: rulesById, narrativeRuleOrderByQuestionId: orderByQuestionId }
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
case 'narrativeRule.delete': {
|
|
1216
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(
|
|
1217
|
+
doc.narrativeRulesById,
|
|
1218
|
+
doc.narrativeRuleOrderByQuestionId,
|
|
1219
|
+
op.ruleId,
|
|
1220
|
+
)
|
|
1221
|
+
return patchDocument(doc, {
|
|
1222
|
+
narrativeRulesById: rulesById,
|
|
1223
|
+
narrativeRuleOrderByQuestionId: orderByQuestionId,
|
|
1224
|
+
})
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
case 'qnrRule.set': {
|
|
1228
|
+
if (!doc.questionsById?.[op.questionId]) {
|
|
1229
|
+
throw new OperationConflictError(`Unknown question "${op.questionId}"`)
|
|
1230
|
+
}
|
|
1231
|
+
const { rulesById, orderByQuestionId } = setScopedRule(
|
|
1232
|
+
doc.qnrRulesById,
|
|
1233
|
+
doc.qnrRuleOrderByQuestionId,
|
|
1234
|
+
op.ruleId,
|
|
1235
|
+
op.questionId,
|
|
1236
|
+
{ condition: op.condition, templateFamilyId: op.templateFamilyId },
|
|
1237
|
+
)
|
|
1238
|
+
return { ...doc, qnrRulesById: rulesById, qnrRuleOrderByQuestionId: orderByQuestionId }
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
case 'qnrRule.delete': {
|
|
1242
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(
|
|
1243
|
+
doc.qnrRulesById,
|
|
1244
|
+
doc.qnrRuleOrderByQuestionId,
|
|
1245
|
+
op.ruleId,
|
|
911
1246
|
)
|
|
912
1247
|
return patchDocument(doc, {
|
|
913
|
-
|
|
914
|
-
|
|
1248
|
+
qnrRulesById: rulesById,
|
|
1249
|
+
qnrRuleOrderByQuestionId: orderByQuestionId,
|
|
915
1250
|
})
|
|
916
1251
|
}
|
|
917
1252
|
|
|
@@ -6,9 +6,12 @@ import type {
|
|
|
6
6
|
DocScalar,
|
|
7
7
|
FilterId,
|
|
8
8
|
HighlightRuleId,
|
|
9
|
+
LayoutPlacement,
|
|
9
10
|
MappingId,
|
|
11
|
+
NarrativeRuleId,
|
|
10
12
|
NodeId,
|
|
11
13
|
QuestionId,
|
|
14
|
+
QnrRuleId,
|
|
12
15
|
RowId,
|
|
13
16
|
RuleCondition,
|
|
14
17
|
TabId,
|
|
@@ -53,6 +56,20 @@ export type TemplateOp =
|
|
|
53
56
|
| { type: 'question.updateField'; questionId: QuestionId; field: string; value: OpValue }
|
|
54
57
|
| { type: 'question.move'; questionId: QuestionId; toIndex: number }
|
|
55
58
|
| { type: 'question.delete'; questionId: QuestionId }
|
|
59
|
+
| {
|
|
60
|
+
type: 'gridColumn.create'
|
|
61
|
+
questionId: QuestionId
|
|
62
|
+
columnQuestionId: QuestionId
|
|
63
|
+
questionType: QuestionType
|
|
64
|
+
atIndex?: number
|
|
65
|
+
}
|
|
66
|
+
| { type: 'gridColumn.move'; questionId: QuestionId; columnQuestionId: QuestionId; toIndex: number }
|
|
67
|
+
| {
|
|
68
|
+
type: 'gridColumn.setLayout'
|
|
69
|
+
questionId: QuestionId
|
|
70
|
+
columnQuestionId: QuestionId
|
|
71
|
+
placement: LayoutPlacement | null
|
|
72
|
+
}
|
|
56
73
|
// Authoring-side grid rows are the template's *predefined* rows; instance-added rows
|
|
57
74
|
// are answer ops (answerOperations.ts). `gridRow.move` is anchor-relative (OQ-V2-24).
|
|
58
75
|
| { type: 'gridRow.create'; questionId: QuestionId; rowId: RowId; label?: string; atIndex?: number }
|
|
@@ -125,6 +142,17 @@ export type TemplateOp =
|
|
|
125
142
|
| { type: 'visibilityRule.delete'; ruleId: VisibilityRuleId }
|
|
126
143
|
| { type: 'highlightRule.set'; ruleId: HighlightRuleId; questionId: QuestionId; condition: RuleCondition }
|
|
127
144
|
| { type: 'highlightRule.delete'; ruleId: HighlightRuleId }
|
|
145
|
+
| { type: 'narrativeRule.set'; ruleId: NarrativeRuleId; questionId: QuestionId; condition: RuleCondition }
|
|
146
|
+
| { type: 'narrativeRule.delete'; ruleId: NarrativeRuleId }
|
|
147
|
+
| {
|
|
148
|
+
type: 'qnrRule.set'
|
|
149
|
+
ruleId: QnrRuleId
|
|
150
|
+
questionId: QuestionId
|
|
151
|
+
condition: RuleCondition
|
|
152
|
+
/** Family id only (OQ-F18/M-066); there is deliberately no version member. */
|
|
153
|
+
templateFamilyId: string
|
|
154
|
+
}
|
|
155
|
+
| { type: 'qnrRule.delete'; ruleId: QnrRuleId }
|
|
128
156
|
|
|
129
157
|
export type TemplateOpType = TemplateOp['type']
|
|
130
158
|
|
|
@@ -142,6 +170,9 @@ const OP_TYPE_COVERAGE: Record<TemplateOpType, true> = {
|
|
|
142
170
|
'question.updateField': true,
|
|
143
171
|
'question.move': true,
|
|
144
172
|
'question.delete': true,
|
|
173
|
+
'gridColumn.create': true,
|
|
174
|
+
'gridColumn.move': true,
|
|
175
|
+
'gridColumn.setLayout': true,
|
|
145
176
|
'gridRow.create': true,
|
|
146
177
|
'gridRow.move': true,
|
|
147
178
|
'gridRow.delete': true,
|
|
@@ -171,6 +202,10 @@ const OP_TYPE_COVERAGE: Record<TemplateOpType, true> = {
|
|
|
171
202
|
'visibilityRule.delete': true,
|
|
172
203
|
'highlightRule.set': true,
|
|
173
204
|
'highlightRule.delete': true,
|
|
205
|
+
'narrativeRule.set': true,
|
|
206
|
+
'narrativeRule.delete': true,
|
|
207
|
+
'qnrRule.set': true,
|
|
208
|
+
'qnrRule.delete': true,
|
|
174
209
|
}
|
|
175
210
|
|
|
176
211
|
export const IMPLEMENTED_OP_TYPES = Object.keys(OP_TYPE_COVERAGE) as readonly TemplateOpType[]
|