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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"applyOperation.d.ts","sourceRoot":"","sources":["../../src/collaboration/applyOperation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,KAAK,EAIR,mBAAmB,
|
|
1
|
+
{"version":3,"file":"applyOperation.d.ts","sourceRoot":"","sources":["../../src/collaboration/applyOperation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,KAAK,EAIR,mBAAmB,EAEtB,MAAM,uBAAuB,CAAA;AAG9B;;;;;;;;;;;;;;;;GAgBG;AAEH;;;;;;GAMG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IACzC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IAErB,YAAY,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAK5D;CACJ;AAED,qBAAa,qBAAsB,SAAQ,kBAAkB;IACzD,YAAY,IAAI,EAAE,MAAM,EAEvB;CACJ;AAED,qBAAa,sBAAuB,SAAQ,kBAAkB;IAC1D,YAAY,OAAO,EAAE,MAAM,EAE1B;CACJ;AAydD,eAAO,MAAM,cAAc,aAAc,mBAAmB,MAAM,UAAU,KAAG,mBAG9E,CAAA"}
|
|
@@ -91,6 +91,33 @@ const omitKey = (record, key) => {
|
|
|
91
91
|
const { [key]: _removed, ...rest } = record;
|
|
92
92
|
return rest;
|
|
93
93
|
};
|
|
94
|
+
/**
|
|
95
|
+
* The document paths a grid operation owns, and no other op may write.
|
|
96
|
+
*
|
|
97
|
+
* `grid.columnIds` is the ownership array a question's single structural owner is read from
|
|
98
|
+
* (`gridColumn.create`/`move` maintain it), and `presentation.rowEditor.layoutByQuestionId`
|
|
99
|
+
* holds one placement per column (`gridColumn.setLayout` writes exactly one). An op that
|
|
100
|
+
* carries a scalar or a flat primitive array can only write such a map *wholesale*, so
|
|
101
|
+
* reaching either path through `question.updateField` replaces or clears it — columns left
|
|
102
|
+
* owned by nobody, or every other column's placement dropped in one edit. That is precisely
|
|
103
|
+
* the atomicity the grid ops exist to provide, so the paths are reserved for them.
|
|
104
|
+
*/
|
|
105
|
+
const GRID_OWNED_FIELD_PATHS = ['grid.columnIds', 'presentation.rowEditor.layoutByQuestionId'];
|
|
106
|
+
/**
|
|
107
|
+
* The reserved path `field` would write, if any. Both directions count: the path itself, a
|
|
108
|
+
* path *below* it (`grid.columnIds.0`), and an *ancestor* whose write would take the reserved
|
|
109
|
+
* map with it (`grid`, `presentation.rowEditor`).
|
|
110
|
+
*/
|
|
111
|
+
const reservedGridFieldPath = (field) => GRID_OWNED_FIELD_PATHS.find((owned) => field === owned || field.startsWith(`${owned}.`) || owned.startsWith(`${field}.`));
|
|
112
|
+
const requireGridQuestion = (doc, questionId) => {
|
|
113
|
+
const question = doc.questionsById?.[questionId];
|
|
114
|
+
if (!question)
|
|
115
|
+
throw new OperationConflictError(`Unknown question "${questionId}"`);
|
|
116
|
+
if (question.type !== 'QuestionGrid') {
|
|
117
|
+
throw new OperationConflictError(`Question "${questionId}" is not a question grid`);
|
|
118
|
+
}
|
|
119
|
+
return question;
|
|
120
|
+
};
|
|
94
121
|
/**
|
|
95
122
|
* Spreads `patch` onto `base`, **omitting** keys whose value is `undefined` rather than
|
|
96
123
|
* assigning it. Required by `exactOptionalPropertyTypes`: `{ ...doc, x: undefined }` is
|
|
@@ -128,6 +155,10 @@ const OPTIONAL_COLLECTIONS = [
|
|
|
128
155
|
'visibilityRuleOrderByQuestionId',
|
|
129
156
|
'highlightRulesById',
|
|
130
157
|
'highlightRuleOrderByQuestionId',
|
|
158
|
+
'narrativeRulesById',
|
|
159
|
+
'narrativeRuleOrderByQuestionId',
|
|
160
|
+
'qnrRulesById',
|
|
161
|
+
'qnrRuleOrderByQuestionId',
|
|
131
162
|
'dataMappingsById',
|
|
132
163
|
'mappingNodesById',
|
|
133
164
|
'mappingBindingsById',
|
|
@@ -176,7 +207,7 @@ const setOrRemoveOrder = (orderByQuestionId, questionId, ids) => {
|
|
|
176
207
|
* one owner: setting it under a different question MOVES it there, so it can never sit in two
|
|
177
208
|
* order arrays at once — and the previous owner's key goes when its last rule leaves (DOC-LAW-2).
|
|
178
209
|
*/
|
|
179
|
-
const setScopedRule = (rulesById, orderByQuestionId, ruleId, questionId,
|
|
210
|
+
const setScopedRule = (rulesById, orderByQuestionId, ruleId, questionId, rule) => {
|
|
180
211
|
const orders = {};
|
|
181
212
|
for (const [ownerId, ruleIds] of Object.entries(orderByQuestionId ?? {})) {
|
|
182
213
|
const kept = ownerId === questionId ? ruleIds : ruleIds.filter((id) => id !== ruleId);
|
|
@@ -186,7 +217,20 @@ const setScopedRule = (rulesById, orderByQuestionId, ruleId, questionId, conditi
|
|
|
186
217
|
const order = orders[questionId] ?? [];
|
|
187
218
|
orders[questionId] = order.includes(ruleId) ? order : [...order, ruleId];
|
|
188
219
|
return {
|
|
189
|
-
rulesById: { ...(rulesById ?? {}), [ruleId]:
|
|
220
|
+
rulesById: { ...(rulesById ?? {}), [ruleId]: rule },
|
|
221
|
+
orderByQuestionId: orders,
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
/** Removes one scoped rule and any order key emptied by that removal. */
|
|
225
|
+
const deleteScopedRule = (rulesById, orderByQuestionId, ruleId) => {
|
|
226
|
+
const orders = {};
|
|
227
|
+
for (const [questionId, ruleIds] of Object.entries(orderByQuestionId ?? {})) {
|
|
228
|
+
const kept = ruleIds.filter((id) => id !== ruleId);
|
|
229
|
+
if (kept.length > 0)
|
|
230
|
+
orders[questionId] = kept;
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
rulesById: rulesById ? omitKey(rulesById, ruleId) : {},
|
|
190
234
|
orderByQuestionId: orders,
|
|
191
235
|
};
|
|
192
236
|
};
|
|
@@ -258,33 +302,187 @@ const scrubBindingOrders = (doc, bindingIds) => {
|
|
|
258
302
|
}
|
|
259
303
|
return changed ? { ...doc, dataMappingsById: next } : doc;
|
|
260
304
|
};
|
|
261
|
-
/**
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
305
|
+
/**
|
|
306
|
+
* Removes a question key from convention-owned presentation maps at any depth. The
|
|
307
|
+
* presentation bag deliberately has an open index, so enumerating today's layout and
|
|
308
|
+
* width maps would make tomorrow's map retain dangling column state.
|
|
309
|
+
*/
|
|
310
|
+
const scrubPresentationQuestionMaps = (value, questionId) => {
|
|
265
311
|
let changed = false;
|
|
266
312
|
const next = {};
|
|
267
|
-
for (const [
|
|
313
|
+
for (const [key, child] of Object.entries(value)) {
|
|
314
|
+
if (typeof child !== 'object' || child === null || Array.isArray(child)) {
|
|
315
|
+
next[key] = child;
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (key.endsWith('ByQuestionId')) {
|
|
319
|
+
const map = child;
|
|
320
|
+
if (!(questionId in map)) {
|
|
321
|
+
next[key] = child;
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
changed = true;
|
|
325
|
+
const kept = omitKey(map, questionId);
|
|
326
|
+
if (Object.keys(kept).length > 0)
|
|
327
|
+
next[key] = kept;
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
const nested = scrubPresentationQuestionMaps(child, questionId);
|
|
331
|
+
changed ||= nested.changed;
|
|
332
|
+
if (!nested.changed || Object.keys(nested.value).length > 0)
|
|
333
|
+
next[key] = nested.value;
|
|
334
|
+
}
|
|
335
|
+
return { value: changed ? next : value, changed };
|
|
336
|
+
};
|
|
337
|
+
/**
|
|
338
|
+
* Removes a deleted question's id from every record owned by a grid: its ordered
|
|
339
|
+
* columns, authored row cells, and convention-named presentation maps.
|
|
340
|
+
*/
|
|
341
|
+
const dropFromGridColumns = (doc, questionId) => {
|
|
342
|
+
let changed = false;
|
|
343
|
+
const nextQuestions = {};
|
|
344
|
+
for (const [qid, question] of Object.entries(doc.questionsById ?? {})) {
|
|
268
345
|
// Defensive: the per-type bags are open, so a malformed `columnIds` must not crash
|
|
269
346
|
// the reducer — it is a schema violation for validation to catch, not a crash here.
|
|
270
347
|
const columnIds = question.grid?.columnIds;
|
|
271
|
-
|
|
272
|
-
|
|
348
|
+
let nextQuestion = question;
|
|
349
|
+
if (Array.isArray(columnIds) && columnIds.includes(questionId)) {
|
|
350
|
+
changed = true;
|
|
351
|
+
const kept = columnIds.filter((id) => id !== questionId);
|
|
352
|
+
const grid = { ...question.grid };
|
|
353
|
+
if (kept.length === 0)
|
|
354
|
+
delete grid.columnIds;
|
|
355
|
+
else
|
|
356
|
+
grid.columnIds = kept;
|
|
357
|
+
nextQuestion = { ...nextQuestion, grid };
|
|
358
|
+
if (Object.keys(grid).length === 0)
|
|
359
|
+
delete nextQuestion.grid;
|
|
360
|
+
}
|
|
361
|
+
if (question.presentation) {
|
|
362
|
+
const scrubbed = scrubPresentationQuestionMaps(question.presentation, questionId);
|
|
363
|
+
if (scrubbed.changed) {
|
|
364
|
+
changed = true;
|
|
365
|
+
nextQuestion = { ...nextQuestion };
|
|
366
|
+
if (Object.keys(scrubbed.value).length === 0)
|
|
367
|
+
delete nextQuestion.presentation;
|
|
368
|
+
else
|
|
369
|
+
nextQuestion.presentation = scrubbed.value;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
nextQuestions[qid] = nextQuestion;
|
|
373
|
+
}
|
|
374
|
+
const nextRows = {};
|
|
375
|
+
for (const [rowId, row] of Object.entries(doc.gridRowsById ?? {})) {
|
|
376
|
+
if (!row.cells || !(questionId in row.cells)) {
|
|
377
|
+
nextRows[rowId] = row;
|
|
273
378
|
continue;
|
|
274
379
|
}
|
|
275
380
|
changed = true;
|
|
276
|
-
const
|
|
277
|
-
const
|
|
278
|
-
if (
|
|
279
|
-
delete
|
|
381
|
+
const cells = omitKey(row.cells, questionId);
|
|
382
|
+
const nextRow = { ...row };
|
|
383
|
+
if (Object.keys(cells).length === 0)
|
|
384
|
+
delete nextRow.cells;
|
|
280
385
|
else
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
386
|
+
nextRow.cells = cells;
|
|
387
|
+
nextRows[rowId] = nextRow;
|
|
388
|
+
}
|
|
389
|
+
if (!changed)
|
|
390
|
+
return doc;
|
|
391
|
+
return patchDocument(doc, {
|
|
392
|
+
questionsById: doc.questionsById ? nextQuestions : undefined,
|
|
393
|
+
gridRowsById: doc.gridRowsById ? nextRows : undefined,
|
|
394
|
+
});
|
|
395
|
+
};
|
|
396
|
+
/**
|
|
397
|
+
* Every question a grid owns, transitively, the grid itself excluded. `seen` makes it total on
|
|
398
|
+
* malformed data: an ownership cycle is not authorable but is importable, and a delete must
|
|
399
|
+
* terminate on one rather than blow the stack.
|
|
400
|
+
*/
|
|
401
|
+
const collectOwnedColumnIds = (doc, gridQuestionId) => {
|
|
402
|
+
const owned = [];
|
|
403
|
+
const seen = new Set([gridQuestionId]);
|
|
404
|
+
const queue = [gridQuestionId];
|
|
405
|
+
while (queue.length > 0) {
|
|
406
|
+
const current = queue.pop();
|
|
407
|
+
const question = doc.questionsById?.[current];
|
|
408
|
+
// Only a grid owns columns; the per-type bag is open, so a malformed `columnIds` must
|
|
409
|
+
// not crash the reducer — that is a schema violation for validation to report.
|
|
410
|
+
const columnIds = question?.type === 'QuestionGrid' ? question.grid?.columnIds : undefined;
|
|
411
|
+
if (!Array.isArray(columnIds))
|
|
412
|
+
continue;
|
|
413
|
+
for (const columnId of columnIds) {
|
|
414
|
+
if (seen.has(columnId))
|
|
415
|
+
continue;
|
|
416
|
+
seen.add(columnId);
|
|
417
|
+
owned.push(columnId);
|
|
418
|
+
queue.push(columnId);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return owned;
|
|
422
|
+
};
|
|
423
|
+
/**
|
|
424
|
+
* Removes one question and everything it alone owns: its alternatives, its own rules, its
|
|
425
|
+
* predefined grid rows, its place in any grid's column list and every binding aimed at it.
|
|
426
|
+
* `question.delete` applies it to the deleted question and to each column that grid owns.
|
|
427
|
+
*/
|
|
428
|
+
const deleteQuestion = (doc, questionId) => {
|
|
429
|
+
// A grid's predefined rows go with it: the order key is dropped below, and a row record
|
|
430
|
+
// no order array reaches is unreachable state that still changes `document_hash`.
|
|
431
|
+
const rowIds = doc.gridRowOrderByQuestionId?.[questionId] ?? [];
|
|
432
|
+
let gridRowsById = doc.gridRowsById;
|
|
433
|
+
if (gridRowsById) {
|
|
434
|
+
gridRowsById = rowIds.reduce((acc, id) => omitKey(acc, id), gridRowsById);
|
|
435
|
+
}
|
|
436
|
+
const alternativeIds = doc.alternativeOrderByQuestionId?.[questionId] ?? [];
|
|
437
|
+
let alternativesById = doc.alternativesById;
|
|
438
|
+
if (alternativesById) {
|
|
439
|
+
alternativesById = alternativeIds.reduce((acc, id) => omitKey(acc, id), alternativesById);
|
|
440
|
+
}
|
|
441
|
+
// The question's own rules go with it, the same way its alternatives do: an orphaned
|
|
442
|
+
// rule record is referenced by no order array and only pollutes the hash. Rules of
|
|
443
|
+
// OTHER questions whose condition points at this one are deliberately KEPT — silently
|
|
444
|
+
// dropping a surviving question's authored logic would be data loss, so the dangling
|
|
445
|
+
// `sourceQuestionId` is left for validation to surface instead.
|
|
446
|
+
const visibilityRuleIds = doc.visibilityRuleOrderByQuestionId?.[questionId] ?? [];
|
|
447
|
+
let visibilityRulesById = doc.visibilityRulesById;
|
|
448
|
+
if (visibilityRulesById) {
|
|
449
|
+
visibilityRulesById = visibilityRuleIds.reduce((acc, id) => omitKey(acc, id), visibilityRulesById);
|
|
450
|
+
}
|
|
451
|
+
const highlightRuleIds = doc.highlightRuleOrderByQuestionId?.[questionId] ?? [];
|
|
452
|
+
let highlightRulesById = doc.highlightRulesById;
|
|
453
|
+
if (highlightRulesById) {
|
|
454
|
+
highlightRulesById = highlightRuleIds.reduce((acc, id) => omitKey(acc, id), highlightRulesById);
|
|
286
455
|
}
|
|
287
|
-
|
|
456
|
+
const narrativeRuleIds = doc.narrativeRuleOrderByQuestionId?.[questionId] ?? [];
|
|
457
|
+
let narrativeRulesById = doc.narrativeRulesById;
|
|
458
|
+
if (narrativeRulesById) {
|
|
459
|
+
narrativeRulesById = narrativeRuleIds.reduce((acc, id) => omitKey(acc, id), narrativeRulesById);
|
|
460
|
+
}
|
|
461
|
+
const qnrRuleIds = doc.qnrRuleOrderByQuestionId?.[questionId] ?? [];
|
|
462
|
+
let qnrRulesById = doc.qnrRulesById;
|
|
463
|
+
if (qnrRulesById) {
|
|
464
|
+
qnrRulesById = qnrRuleIds.reduce((acc, id) => omitKey(acc, id), qnrRulesById);
|
|
465
|
+
}
|
|
466
|
+
const withoutQuestion = patchDocument(doc, {
|
|
467
|
+
questionsById: doc.questionsById && omitKey(doc.questionsById, questionId),
|
|
468
|
+
questionOrder: doc.questionOrder.filter((id) => id !== questionId),
|
|
469
|
+
alternativesById,
|
|
470
|
+
alternativeOrderByQuestionId: doc.alternativeOrderByQuestionId && omitKey(doc.alternativeOrderByQuestionId, questionId),
|
|
471
|
+
gridRowsById,
|
|
472
|
+
gridRowOrderByQuestionId: doc.gridRowOrderByQuestionId && omitKey(doc.gridRowOrderByQuestionId, questionId),
|
|
473
|
+
visibilityRulesById,
|
|
474
|
+
visibilityRuleOrderByQuestionId: doc.visibilityRuleOrderByQuestionId && omitKey(doc.visibilityRuleOrderByQuestionId, questionId),
|
|
475
|
+
highlightRulesById,
|
|
476
|
+
highlightRuleOrderByQuestionId: doc.highlightRuleOrderByQuestionId && omitKey(doc.highlightRuleOrderByQuestionId, questionId),
|
|
477
|
+
narrativeRulesById,
|
|
478
|
+
narrativeRuleOrderByQuestionId: doc.narrativeRuleOrderByQuestionId && omitKey(doc.narrativeRuleOrderByQuestionId, questionId),
|
|
479
|
+
qnrRulesById,
|
|
480
|
+
qnrRuleOrderByQuestionId: doc.qnrRuleOrderByQuestionId && omitKey(doc.qnrRuleOrderByQuestionId, questionId),
|
|
481
|
+
});
|
|
482
|
+
// A binding pointing at a deleted question is an unresolvable reference the
|
|
483
|
+
// compiler would reject at publication; drop it with its target. A grid column
|
|
484
|
+
// list pointing at it is the same class of reference and goes the same way.
|
|
485
|
+
return dropBindingsForQuestion(dropFromGridColumns(withoutQuestion, questionId), questionId);
|
|
288
486
|
};
|
|
289
487
|
export const applyOperation = (document, op) => {
|
|
290
488
|
const next = reduce(document, op);
|
|
@@ -330,6 +528,23 @@ const reduce = (doc, op) => {
|
|
|
330
528
|
if (!question) {
|
|
331
529
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`);
|
|
332
530
|
}
|
|
531
|
+
// `type` is the question's identity, not one of its fields: `question.create` sets it and
|
|
532
|
+
// every per-type shape hangs off it. A field write moves it alone — a grid flipped to
|
|
533
|
+
// another type keeps the `grid.columnIds` the document schema admits on no other type,
|
|
534
|
+
// its columns then owned by nobody and no longer cascading with its delete.
|
|
535
|
+
if (op.field === 'type' || op.field.startsWith('type.')) {
|
|
536
|
+
throw new OperationConflictError(`Field "${op.field}" writes the question type, which only question.create may set`);
|
|
537
|
+
}
|
|
538
|
+
const reserved = reservedGridFieldPath(op.field);
|
|
539
|
+
if (reserved !== undefined) {
|
|
540
|
+
throw new OperationConflictError(`Field "${op.field}" writes "${reserved}", which only the gridColumn operations may author`);
|
|
541
|
+
}
|
|
542
|
+
// `grid` is the grid's own configuration and the document schema admits it on no
|
|
543
|
+
// other type, so authoring it through an ordinary question would store a bag no
|
|
544
|
+
// reader projects and the publication schema then refuses.
|
|
545
|
+
if (question.type !== 'QuestionGrid' && op.field.split('.')[0] === 'grid') {
|
|
546
|
+
throw new OperationConflictError(`Question "${op.questionId}" is not a question grid, so "${op.field}" is not authorable on it`);
|
|
547
|
+
}
|
|
333
548
|
return {
|
|
334
549
|
...doc,
|
|
335
550
|
questionsById: {
|
|
@@ -342,45 +557,93 @@ const reduce = (doc, op) => {
|
|
|
342
557
|
if (!doc.questionsById?.[op.questionId]) {
|
|
343
558
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`);
|
|
344
559
|
}
|
|
560
|
+
if (!doc.questionOrder.includes(op.questionId)) {
|
|
561
|
+
throw new OperationConflictError(`Question "${op.questionId}" is not top-level`);
|
|
562
|
+
}
|
|
345
563
|
return { ...doc, questionOrder: moveInOrder(doc.questionOrder, op.questionId, op.toIndex) };
|
|
346
564
|
}
|
|
347
565
|
case 'question.delete': {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
566
|
+
// A grid owns its columns, so they go with it: they sit in no order array of their
|
|
567
|
+
// own, and leaving them behind is the orphan half of the ownership invariant. The
|
|
568
|
+
// whole ownership subtree is collected first, then each question is removed on its
|
|
569
|
+
// own terms — its alternatives, rules and bindings included.
|
|
570
|
+
const ids = [op.questionId, ...collectOwnedColumnIds(doc, op.questionId)];
|
|
571
|
+
return ids.reduce((acc, id) => deleteQuestion(acc, id), doc);
|
|
572
|
+
}
|
|
573
|
+
case 'gridColumn.create': {
|
|
574
|
+
const gridQuestion = requireGridQuestion(doc, op.questionId);
|
|
575
|
+
if (doc.questionsById?.[op.columnQuestionId]) {
|
|
576
|
+
throw new OperationConflictError(`Question "${op.columnQuestionId}" already exists`);
|
|
577
|
+
}
|
|
578
|
+
const columnIds = gridQuestion.grid?.columnIds ?? [];
|
|
579
|
+
return {
|
|
580
|
+
...doc,
|
|
581
|
+
questionsById: {
|
|
582
|
+
...doc.questionsById,
|
|
583
|
+
[op.questionId]: {
|
|
584
|
+
...gridQuestion,
|
|
585
|
+
grid: {
|
|
586
|
+
...(gridQuestion.grid ?? {}),
|
|
587
|
+
columnIds: insertAt(columnIds, op.columnQuestionId, op.atIndex),
|
|
588
|
+
},
|
|
589
|
+
},
|
|
590
|
+
[op.columnQuestionId]: { type: op.questionType },
|
|
591
|
+
},
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
case 'gridColumn.move': {
|
|
595
|
+
const gridQuestion = requireGridQuestion(doc, op.questionId);
|
|
596
|
+
const columnIds = gridQuestion.grid?.columnIds;
|
|
597
|
+
if (!doc.questionsById?.[op.columnQuestionId] || !columnIds?.includes(op.columnQuestionId)) {
|
|
598
|
+
throw new OperationConflictError(`Question "${op.columnQuestionId}" does not belong to grid "${op.questionId}"`);
|
|
599
|
+
}
|
|
600
|
+
return {
|
|
601
|
+
...doc,
|
|
602
|
+
questionsById: {
|
|
603
|
+
...doc.questionsById,
|
|
604
|
+
[op.questionId]: {
|
|
605
|
+
...gridQuestion,
|
|
606
|
+
grid: {
|
|
607
|
+
...gridQuestion.grid,
|
|
608
|
+
columnIds: moveInOrder(columnIds, op.columnQuestionId, op.toIndex),
|
|
609
|
+
},
|
|
610
|
+
},
|
|
611
|
+
},
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
case 'gridColumn.setLayout': {
|
|
615
|
+
const gridQuestion = requireGridQuestion(doc, op.questionId);
|
|
616
|
+
const columnIds = gridQuestion.grid?.columnIds;
|
|
617
|
+
if (!doc.questionsById?.[op.columnQuestionId] || !columnIds?.includes(op.columnQuestionId)) {
|
|
618
|
+
throw new OperationConflictError(`Question "${op.columnQuestionId}" does not belong to grid "${op.questionId}"`);
|
|
619
|
+
}
|
|
620
|
+
const presentation = { ...(gridQuestion.presentation ?? {}) };
|
|
621
|
+
const rowEditor = { ...(presentation.rowEditor ?? {}) };
|
|
622
|
+
const layoutByQuestionId = { ...(rowEditor.layoutByQuestionId ?? {}) };
|
|
623
|
+
if (op.placement === null)
|
|
624
|
+
delete layoutByQuestionId[op.columnQuestionId];
|
|
625
|
+
else
|
|
626
|
+
layoutByQuestionId[op.columnQuestionId] = op.placement;
|
|
627
|
+
if (Object.keys(layoutByQuestionId).length === 0)
|
|
628
|
+
delete rowEditor.layoutByQuestionId;
|
|
629
|
+
else
|
|
630
|
+
rowEditor.layoutByQuestionId = layoutByQuestionId;
|
|
631
|
+
if (Object.keys(rowEditor).length === 0)
|
|
632
|
+
delete presentation.rowEditor;
|
|
633
|
+
else
|
|
634
|
+
presentation.rowEditor = rowEditor;
|
|
635
|
+
const nextQuestion = { ...gridQuestion };
|
|
636
|
+
if (Object.keys(presentation).length === 0)
|
|
637
|
+
delete nextQuestion.presentation;
|
|
638
|
+
else
|
|
639
|
+
nextQuestion.presentation = presentation;
|
|
640
|
+
return {
|
|
641
|
+
...doc,
|
|
642
|
+
questionsById: {
|
|
643
|
+
...doc.questionsById,
|
|
644
|
+
[op.questionId]: nextQuestion,
|
|
645
|
+
},
|
|
646
|
+
};
|
|
384
647
|
}
|
|
385
648
|
case 'gridRow.create': {
|
|
386
649
|
if (!doc.questionsById?.[op.questionId]) {
|
|
@@ -779,34 +1042,56 @@ const reduce = (doc, op) => {
|
|
|
779
1042
|
if (!doc.questionsById?.[op.questionId]) {
|
|
780
1043
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`);
|
|
781
1044
|
}
|
|
782
|
-
const { rulesById, orderByQuestionId } = setScopedRule(doc.visibilityRulesById, doc.visibilityRuleOrderByQuestionId, op.ruleId, op.questionId, op.condition);
|
|
1045
|
+
const { rulesById, orderByQuestionId } = setScopedRule(doc.visibilityRulesById, doc.visibilityRuleOrderByQuestionId, op.ruleId, op.questionId, { condition: op.condition });
|
|
783
1046
|
return { ...doc, visibilityRulesById: rulesById, visibilityRuleOrderByQuestionId: orderByQuestionId };
|
|
784
1047
|
}
|
|
785
1048
|
case 'visibilityRule.delete': {
|
|
786
|
-
const
|
|
787
|
-
questionId,
|
|
788
|
-
ruleIds.filter((id) => id !== op.ruleId),
|
|
789
|
-
]));
|
|
1049
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(doc.visibilityRulesById, doc.visibilityRuleOrderByQuestionId, op.ruleId);
|
|
790
1050
|
return patchDocument(doc, {
|
|
791
|
-
visibilityRulesById:
|
|
792
|
-
visibilityRuleOrderByQuestionId:
|
|
1051
|
+
visibilityRulesById: rulesById,
|
|
1052
|
+
visibilityRuleOrderByQuestionId: orderByQuestionId,
|
|
793
1053
|
});
|
|
794
1054
|
}
|
|
795
1055
|
case 'highlightRule.set': {
|
|
796
1056
|
if (!doc.questionsById?.[op.questionId]) {
|
|
797
1057
|
throw new OperationConflictError(`Unknown question "${op.questionId}"`);
|
|
798
1058
|
}
|
|
799
|
-
const { rulesById, orderByQuestionId } = setScopedRule(doc.highlightRulesById, doc.highlightRuleOrderByQuestionId, op.ruleId, op.questionId, op.condition);
|
|
1059
|
+
const { rulesById, orderByQuestionId } = setScopedRule(doc.highlightRulesById, doc.highlightRuleOrderByQuestionId, op.ruleId, op.questionId, { condition: op.condition });
|
|
800
1060
|
return { ...doc, highlightRulesById: rulesById, highlightRuleOrderByQuestionId: orderByQuestionId };
|
|
801
1061
|
}
|
|
802
1062
|
case 'highlightRule.delete': {
|
|
803
|
-
const
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
1063
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(doc.highlightRulesById, doc.highlightRuleOrderByQuestionId, op.ruleId);
|
|
1064
|
+
return patchDocument(doc, {
|
|
1065
|
+
highlightRulesById: rulesById,
|
|
1066
|
+
highlightRuleOrderByQuestionId: orderByQuestionId,
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
case 'narrativeRule.set': {
|
|
1070
|
+
if (!doc.questionsById?.[op.questionId]) {
|
|
1071
|
+
throw new OperationConflictError(`Unknown question "${op.questionId}"`);
|
|
1072
|
+
}
|
|
1073
|
+
const { rulesById, orderByQuestionId } = setScopedRule(doc.narrativeRulesById, doc.narrativeRuleOrderByQuestionId, op.ruleId, op.questionId, { condition: op.condition });
|
|
1074
|
+
return { ...doc, narrativeRulesById: rulesById, narrativeRuleOrderByQuestionId: orderByQuestionId };
|
|
1075
|
+
}
|
|
1076
|
+
case 'narrativeRule.delete': {
|
|
1077
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(doc.narrativeRulesById, doc.narrativeRuleOrderByQuestionId, op.ruleId);
|
|
1078
|
+
return patchDocument(doc, {
|
|
1079
|
+
narrativeRulesById: rulesById,
|
|
1080
|
+
narrativeRuleOrderByQuestionId: orderByQuestionId,
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
case 'qnrRule.set': {
|
|
1084
|
+
if (!doc.questionsById?.[op.questionId]) {
|
|
1085
|
+
throw new OperationConflictError(`Unknown question "${op.questionId}"`);
|
|
1086
|
+
}
|
|
1087
|
+
const { rulesById, orderByQuestionId } = setScopedRule(doc.qnrRulesById, doc.qnrRuleOrderByQuestionId, op.ruleId, op.questionId, { condition: op.condition, templateFamilyId: op.templateFamilyId });
|
|
1088
|
+
return { ...doc, qnrRulesById: rulesById, qnrRuleOrderByQuestionId: orderByQuestionId };
|
|
1089
|
+
}
|
|
1090
|
+
case 'qnrRule.delete': {
|
|
1091
|
+
const { rulesById, orderByQuestionId } = deleteScopedRule(doc.qnrRulesById, doc.qnrRuleOrderByQuestionId, op.ruleId);
|
|
807
1092
|
return patchDocument(doc, {
|
|
808
|
-
|
|
809
|
-
|
|
1093
|
+
qnrRulesById: rulesById,
|
|
1094
|
+
qnrRuleOrderByQuestionId: orderByQuestionId,
|
|
810
1095
|
});
|
|
811
1096
|
}
|
|
812
1097
|
default: {
|