asma-core-helpers 0.20.1 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/collaboration/answerOperations.d.ts +11 -0
- package/lib/collaboration/answerOperations.d.ts.map +1 -1
- package/lib/collaboration/answerOperations.js +1 -0
- package/lib/collaboration/answerOperations.js.map +1 -1
- package/lib/collaboration/applyOperation.d.ts +5 -0
- package/lib/collaboration/applyOperation.d.ts.map +1 -1
- package/lib/collaboration/applyOperation.js +281 -67
- package/lib/collaboration/applyOperation.js.map +1 -1
- package/lib/collaboration/conflicts.d.ts +37 -0
- package/lib/collaboration/conflicts.d.ts.map +1 -0
- package/lib/collaboration/conflicts.js +2 -0
- package/lib/collaboration/conflicts.js.map +1 -0
- package/lib/collaboration/index.d.ts +14 -5
- package/lib/collaboration/index.d.ts.map +1 -1
- package/lib/collaboration/index.js +14 -5
- package/lib/collaboration/index.js.map +1 -1
- package/lib/collaboration/instanceLifecycle.d.ts +31 -0
- package/lib/collaboration/instanceLifecycle.d.ts.map +1 -0
- package/lib/collaboration/instanceLifecycle.js +27 -0
- package/lib/collaboration/instanceLifecycle.js.map +1 -0
- package/lib/collaboration/operations.d.ts +15 -7
- package/lib/collaboration/operations.d.ts.map +1 -1
- package/lib/collaboration/operations.js +2 -1
- package/lib/collaboration/operations.js.map +1 -1
- package/lib/collaboration/schemas.d.ts +466 -0
- package/lib/collaboration/schemas.d.ts.map +1 -0
- package/lib/collaboration/schemas.js +450 -0
- package/lib/collaboration/schemas.js.map +1 -0
- package/lib/collaboration/templateDocument.d.ts +25 -8
- package/lib/collaboration/templateDocument.d.ts.map +1 -1
- package/lib/collaboration/templateDocument.js +4 -0
- package/lib/collaboration/templateDocument.js.map +1 -1
- package/lib/collaboration/testHelpers/documentGenerator.d.ts +40 -0
- package/lib/collaboration/testHelpers/documentGenerator.d.ts.map +1 -0
- package/lib/collaboration/testHelpers/documentGenerator.js +199 -0
- package/lib/collaboration/testHelpers/documentGenerator.js.map +1 -0
- package/lib/collaboration/testHelpers/seededRandom.d.ts +13 -0
- package/lib/collaboration/testHelpers/seededRandom.d.ts.map +1 -0
- package/lib/collaboration/testHelpers/seededRandom.js +41 -0
- package/lib/collaboration/testHelpers/seededRandom.js.map +1 -0
- package/package.json +1 -1
- package/src/collaboration/answerOperations.ts +13 -0
- package/src/collaboration/applyOperation.test.ts +292 -0
- package/src/collaboration/applyOperation.ts +320 -66
- package/src/collaboration/conflicts.ts +40 -0
- package/src/collaboration/index.ts +14 -5
- package/src/collaboration/instanceLifecycle.ts +38 -0
- package/src/collaboration/operations.ts +13 -4
- package/src/collaboration/property.test.ts +156 -0
- package/src/collaboration/schemas.test.ts +236 -0
- package/src/collaboration/schemas.ts +587 -0
- package/src/collaboration/templateDocument.ts +30 -4
- package/src/collaboration/testHelpers/documentGenerator.ts +240 -0
- package/src/collaboration/testHelpers/seededRandom.ts +42 -0
|
@@ -10,7 +10,12 @@ import type { QuestionId, RowId } from './templateDocument.js';
|
|
|
10
10
|
* different mutability (OQ-V2-8) and different law regimes (the explicit `null` clear
|
|
11
11
|
* marker is legal in answer state, never in the template document).
|
|
12
12
|
*
|
|
13
|
+
* `gridRow.move` is anchor-relative (`afterRowId`, never an index) exactly like the
|
|
14
|
+
* authoring vocabulary — row order is versioned answer state (`qnr_answer_grid_row_orders`),
|
|
15
|
+
* and an index denotes different rows to two clients that disagree by one insert (OQ-V2-24).
|
|
16
|
+
*
|
|
13
17
|
* @see asma-modules/_docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:380 (filling vocabulary)
|
|
18
|
+
* @see asma-modules/_docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:390 (answer row order + anchor-relative move)
|
|
14
19
|
*/
|
|
15
20
|
export type AnswerOp = {
|
|
16
21
|
type: 'answer.set';
|
|
@@ -24,6 +29,12 @@ export type AnswerOp = {
|
|
|
24
29
|
questionId: QuestionId;
|
|
25
30
|
rowId: RowId;
|
|
26
31
|
afterRowId?: RowId;
|
|
32
|
+
} | {
|
|
33
|
+
type: 'gridRow.move';
|
|
34
|
+
questionId: QuestionId;
|
|
35
|
+
rowId: RowId;
|
|
36
|
+
/** Anchor-relative (OQ-V2-24, both vocabularies): `null` moves to the front — never an index. */
|
|
37
|
+
afterRowId: RowId | null;
|
|
27
38
|
} | {
|
|
28
39
|
type: 'gridRow.remove';
|
|
29
40
|
questionId: QuestionId;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"answerOperations.d.ts","sourceRoot":"","sources":["../../src/collaboration/answerOperations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACtD,OAAO,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAA;AAE9D
|
|
1
|
+
{"version":3,"file":"answerOperations.d.ts","sourceRoot":"","sources":["../../src/collaboration/answerOperations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AACtD,OAAO,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAA;AAE9D;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,MAAM,QAAQ,GACd;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,UAAU,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,WAAW,CAAA;CAAE,GAClE;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,UAAU,EAAE,UAAU,CAAA;CAAE,GAChD;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,UAAU,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,UAAU,CAAC,EAAE,KAAK,CAAA;CAAE,GACjF;IACI,IAAI,EAAE,cAAc,CAAA;IACpB,UAAU,EAAE,UAAU,CAAA;IACtB,KAAK,EAAE,KAAK,CAAA;IACZ,iGAAiG;IACjG,UAAU,EAAE,KAAK,GAAG,IAAI,CAAA;CAC3B,GACD;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,UAAU,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE,GAChE;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,UAAU,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,gBAAgB,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,WAAW,CAAA;CAAE,CAAA;AAExH,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAA;AAW3C,eAAO,MAAM,2BAA2B,EAA2C,SAAS,YAAY,EAAE,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"answerOperations.js","sourceRoot":"","sources":["../../src/collaboration/answerOperations.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"answerOperations.js","sourceRoot":"","sources":["../../src/collaboration/answerOperations.ts"],"names":[],"mappings":"AAqCA,MAAM,uBAAuB,GAA+B;IACxD,YAAY,EAAE,IAAI;IAClB,cAAc,EAAE,IAAI;IACpB,aAAa,EAAE,IAAI;IACnB,cAAc,EAAE,IAAI;IACpB,gBAAgB,EAAE,IAAI;IACtB,gBAAgB,EAAE,IAAI;CACzB,CAAA;AAED,MAAM,CAAC,MAAM,2BAA2B,GAAG,MAAM,CAAC,IAAI,CAAC,uBAAuB,CAA4B,CAAA"}
|
|
@@ -9,6 +9,11 @@ import type { QnrTemplateDocument } from './templateDocument.js';
|
|
|
9
9
|
* Idempotency by `op_id` and revision assignment on the wire are persistence concerns
|
|
10
10
|
* and deliberately live outside this function.
|
|
11
11
|
*
|
|
12
|
+
* Create ops mint a transient empty record when no initial field is supplied (`{}`): it
|
|
13
|
+
* carries no information, `reduceToMinimalForm` erases it from the canonical form, and
|
|
14
|
+
* `findDocLawViolations` flags it at publish time — so it never reaches a stored version
|
|
15
|
+
* and two clients taking different routes to the same content still agree on the hash.
|
|
16
|
+
*
|
|
12
17
|
* @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:380 (vocabulary)
|
|
13
18
|
* @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:384 (authoring dataflow)
|
|
14
19
|
*/
|
|
@@ -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,
|
|
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,EAGtB,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;AAgQD,eAAO,MAAM,cAAc,aAAc,mBAAmB,MAAM,UAAU,KAAG,mBAG9E,CAAA"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { bindingTargetKey } from './templateDocument.js';
|
|
1
2
|
/**
|
|
2
3
|
* The authoring reducer. Pure: takes a document and an op, returns a new document with
|
|
3
4
|
* `revision` bumped by one. The frontend applies it locally before the op leaves the
|
|
@@ -7,6 +8,11 @@
|
|
|
7
8
|
* Idempotency by `op_id` and revision assignment on the wire are persistence concerns
|
|
8
9
|
* and deliberately live outside this function.
|
|
9
10
|
*
|
|
11
|
+
* Create ops mint a transient empty record when no initial field is supplied (`{}`): it
|
|
12
|
+
* carries no information, `reduceToMinimalForm` erases it from the canonical form, and
|
|
13
|
+
* `findDocLawViolations` flags it at publish time — so it never reaches a stored version
|
|
14
|
+
* and two clients taking different routes to the same content still agree on the hash.
|
|
15
|
+
*
|
|
10
16
|
* @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:380 (vocabulary)
|
|
11
17
|
* @see _docs/editor/qnrs/cross/2026-07-12-20-20-architecture-qnr-v2-model-collaboration-sync.md:384 (authoring dataflow)
|
|
12
18
|
*/
|
|
@@ -38,9 +44,7 @@ export class OperationConflictError extends CollaborationError {
|
|
|
38
44
|
}
|
|
39
45
|
}
|
|
40
46
|
/** Serialises a binding target so the "one binding per target" invariant is checkable. */
|
|
41
|
-
const targetKey =
|
|
42
|
-
? `question:${target.questionId}`
|
|
43
|
-
: `gridColumn:${target.gridQuestionId}:${target.columnQuestionId}`;
|
|
47
|
+
const targetKey = bindingTargetKey;
|
|
44
48
|
/**
|
|
45
49
|
* Sets a key, or removes it when the op carried an explicit unset.
|
|
46
50
|
*
|
|
@@ -155,6 +159,133 @@ const dropBindingsForQuestion = (doc, questionId) => {
|
|
|
155
159
|
});
|
|
156
160
|
return { ...doc, mappingBindingsById: Object.fromEntries(kept) };
|
|
157
161
|
};
|
|
162
|
+
/**
|
|
163
|
+
* Writes a question's order array, or drops its key when the last member left — DOC-LAW-2
|
|
164
|
+
* stores no empty array, and `{ q1: [] }` would also hash differently from the key's absence.
|
|
165
|
+
*/
|
|
166
|
+
const setOrRemoveOrder = (orderByQuestionId, questionId, ids) => {
|
|
167
|
+
const map = { ...(orderByQuestionId ?? {}) };
|
|
168
|
+
if (ids.length === 0)
|
|
169
|
+
delete map[questionId];
|
|
170
|
+
else
|
|
171
|
+
map[questionId] = ids;
|
|
172
|
+
return map;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Shared setter for the per-question rule collections (visibility/highlight). A rule has exactly
|
|
176
|
+
* one owner: setting it under a different question MOVES it there, so it can never sit in two
|
|
177
|
+
* order arrays at once — and the previous owner's key goes when its last rule leaves (DOC-LAW-2).
|
|
178
|
+
*/
|
|
179
|
+
const setScopedRule = (rulesById, orderByQuestionId, ruleId, questionId, condition) => {
|
|
180
|
+
const orders = {};
|
|
181
|
+
for (const [ownerId, ruleIds] of Object.entries(orderByQuestionId ?? {})) {
|
|
182
|
+
const kept = ownerId === questionId ? ruleIds : ruleIds.filter((id) => id !== ruleId);
|
|
183
|
+
if (kept.length > 0)
|
|
184
|
+
orders[ownerId] = kept;
|
|
185
|
+
}
|
|
186
|
+
const order = orders[questionId] ?? [];
|
|
187
|
+
orders[questionId] = order.includes(ruleId) ? order : [...order, ruleId];
|
|
188
|
+
return {
|
|
189
|
+
rulesById: { ...(rulesById ?? {}), [ruleId]: { condition } },
|
|
190
|
+
orderByQuestionId: orders,
|
|
191
|
+
};
|
|
192
|
+
};
|
|
193
|
+
/**
|
|
194
|
+
* Walks `parentNodeId` links to the root of the node's traversal tree. A cycle is malformed
|
|
195
|
+
* data; bailing with `undefined` treats the node as unattached rather than hanging the reducer.
|
|
196
|
+
*/
|
|
197
|
+
const rootOfNode = (doc, nodeId) => {
|
|
198
|
+
const visited = new Set();
|
|
199
|
+
let current = nodeId;
|
|
200
|
+
while (current !== undefined && !visited.has(current)) {
|
|
201
|
+
visited.add(current);
|
|
202
|
+
const node = doc.mappingNodesById?.[current];
|
|
203
|
+
if (node === undefined)
|
|
204
|
+
return undefined;
|
|
205
|
+
if (node.parentNodeId === undefined)
|
|
206
|
+
return current;
|
|
207
|
+
current = node.parentNodeId;
|
|
208
|
+
}
|
|
209
|
+
return undefined;
|
|
210
|
+
};
|
|
211
|
+
/** The data mapping whose traversal tree contains `nodeId`, when the tree is attached to one. */
|
|
212
|
+
const mappingForNode = (doc, nodeId) => {
|
|
213
|
+
const rootId = rootOfNode(doc, nodeId);
|
|
214
|
+
if (rootId === undefined)
|
|
215
|
+
return undefined;
|
|
216
|
+
return Object.entries(doc.dataMappingsById ?? {}).find(([, mapping]) => mapping.rootNodeId === rootId);
|
|
217
|
+
};
|
|
218
|
+
/** Every node of the traversal tree under `rootNodeId`, root included. */
|
|
219
|
+
const collectTreeNodeIds = (doc, rootNodeId) => {
|
|
220
|
+
const ids = new Set();
|
|
221
|
+
const queue = [rootNodeId];
|
|
222
|
+
while (queue.length > 0) {
|
|
223
|
+
const current = queue.pop();
|
|
224
|
+
if (ids.has(current))
|
|
225
|
+
continue;
|
|
226
|
+
ids.add(current);
|
|
227
|
+
for (const [nodeId, node] of Object.entries(doc.mappingNodesById ?? {})) {
|
|
228
|
+
if (node.parentNodeId === current)
|
|
229
|
+
queue.push(nodeId);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return ids;
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* Removes the given binding ids from every `bindingOrder` — a deleted binding must not leave
|
|
236
|
+
* its id behind in the order array (dangling prevention by construction, §2.2a).
|
|
237
|
+
*/
|
|
238
|
+
const scrubBindingOrders = (doc, bindingIds) => {
|
|
239
|
+
if (!doc.dataMappingsById)
|
|
240
|
+
return doc;
|
|
241
|
+
let changed = false;
|
|
242
|
+
const next = {};
|
|
243
|
+
for (const [mappingId, mapping] of Object.entries(doc.dataMappingsById)) {
|
|
244
|
+
if (!mapping.bindingOrder?.some((id) => bindingIds.has(id))) {
|
|
245
|
+
next[mappingId] = mapping;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
changed = true;
|
|
249
|
+
const kept = mapping.bindingOrder.filter((id) => !bindingIds.has(id));
|
|
250
|
+
if (kept.length > 0) {
|
|
251
|
+
next[mappingId] = { ...mapping, bindingOrder: kept };
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
// DOC-LAW-2: an emptied order array is dropped, never stored.
|
|
255
|
+
const { bindingOrder: _removed, ...rest } = mapping;
|
|
256
|
+
next[mappingId] = rest;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return changed ? { ...doc, dataMappingsById: next } : doc;
|
|
260
|
+
};
|
|
261
|
+
/** Removes a deleted question's id from every grid's column list (OQ-V2-17 `grid.columnIds`). */
|
|
262
|
+
const dropFromGridColumns = (doc, questionId) => {
|
|
263
|
+
if (!doc.questionsById)
|
|
264
|
+
return doc;
|
|
265
|
+
let changed = false;
|
|
266
|
+
const next = {};
|
|
267
|
+
for (const [qid, question] of Object.entries(doc.questionsById)) {
|
|
268
|
+
// Defensive: the per-type bags are open, so a malformed `columnIds` must not crash
|
|
269
|
+
// the reducer — it is a schema violation for validation to catch, not a crash here.
|
|
270
|
+
const columnIds = question.grid?.columnIds;
|
|
271
|
+
if (!Array.isArray(columnIds) || !columnIds.includes(questionId)) {
|
|
272
|
+
next[qid] = question;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
changed = true;
|
|
276
|
+
const kept = columnIds.filter((id) => id !== questionId);
|
|
277
|
+
const grid = { ...question.grid };
|
|
278
|
+
if (kept.length === 0)
|
|
279
|
+
delete grid.columnIds;
|
|
280
|
+
else
|
|
281
|
+
grid.columnIds = kept;
|
|
282
|
+
const nextQuestion = { ...question, grid };
|
|
283
|
+
if (Object.keys(grid).length === 0)
|
|
284
|
+
delete nextQuestion.grid;
|
|
285
|
+
next[qid] = nextQuestion;
|
|
286
|
+
}
|
|
287
|
+
return changed ? { ...doc, questionsById: next } : doc;
|
|
288
|
+
};
|
|
158
289
|
export const applyOperation = (document, op) => {
|
|
159
290
|
const next = reduce(document, op);
|
|
160
291
|
return prune({ ...next, revision: document.revision + 1 });
|
|
@@ -219,19 +350,37 @@ const reduce = (doc, op) => {
|
|
|
219
350
|
if (alternativesById) {
|
|
220
351
|
alternativesById = alternativeIds.reduce((acc, id) => omitKey(acc, id), alternativesById);
|
|
221
352
|
}
|
|
353
|
+
// The question's own rules go with it, the same way its alternatives do: an orphaned
|
|
354
|
+
// rule record is referenced by no order array and only pollutes the hash. Rules of
|
|
355
|
+
// OTHER questions whose condition points at this one are deliberately KEPT — silently
|
|
356
|
+
// dropping a surviving question's authored logic would be data loss, so the dangling
|
|
357
|
+
// `sourceQuestionId` is left for validation to surface instead.
|
|
358
|
+
const visibilityRuleIds = doc.visibilityRuleOrderByQuestionId?.[op.questionId] ?? [];
|
|
359
|
+
let visibilityRulesById = doc.visibilityRulesById;
|
|
360
|
+
if (visibilityRulesById) {
|
|
361
|
+
visibilityRulesById = visibilityRuleIds.reduce((acc, id) => omitKey(acc, id), visibilityRulesById);
|
|
362
|
+
}
|
|
363
|
+
const highlightRuleIds = doc.highlightRuleOrderByQuestionId?.[op.questionId] ?? [];
|
|
364
|
+
let highlightRulesById = doc.highlightRulesById;
|
|
365
|
+
if (highlightRulesById) {
|
|
366
|
+
highlightRulesById = highlightRuleIds.reduce((acc, id) => omitKey(acc, id), highlightRulesById);
|
|
367
|
+
}
|
|
222
368
|
const withoutQuestion = patchDocument(doc, {
|
|
223
369
|
questionsById: doc.questionsById && omitKey(doc.questionsById, op.questionId),
|
|
224
370
|
questionOrder: doc.questionOrder.filter((id) => id !== op.questionId),
|
|
225
371
|
alternativesById,
|
|
226
372
|
alternativeOrderByQuestionId: doc.alternativeOrderByQuestionId && omitKey(doc.alternativeOrderByQuestionId, op.questionId),
|
|
227
373
|
gridRowOrderByQuestionId: doc.gridRowOrderByQuestionId && omitKey(doc.gridRowOrderByQuestionId, op.questionId),
|
|
374
|
+
visibilityRulesById,
|
|
228
375
|
visibilityRuleOrderByQuestionId: doc.visibilityRuleOrderByQuestionId && omitKey(doc.visibilityRuleOrderByQuestionId, op.questionId),
|
|
376
|
+
highlightRulesById,
|
|
229
377
|
highlightRuleOrderByQuestionId: doc.highlightRuleOrderByQuestionId &&
|
|
230
378
|
omitKey(doc.highlightRuleOrderByQuestionId, op.questionId),
|
|
231
379
|
});
|
|
232
380
|
// A binding pointing at a deleted question is an unresolvable reference the
|
|
233
|
-
// compiler would reject at publication; drop it with its target.
|
|
234
|
-
|
|
381
|
+
// compiler would reject at publication; drop it with its target. A grid column
|
|
382
|
+
// list pointing at it is the same class of reference and goes the same way.
|
|
383
|
+
return dropBindingsForQuestion(dropFromGridColumns(withoutQuestion, op.questionId), op.questionId);
|
|
235
384
|
}
|
|
236
385
|
case 'gridRow.create': {
|
|
237
386
|
if (!doc.questionsById?.[op.questionId]) {
|
|
@@ -276,14 +425,14 @@ const reduce = (doc, op) => {
|
|
|
276
425
|
};
|
|
277
426
|
}
|
|
278
427
|
case 'gridRow.delete': {
|
|
279
|
-
const order = doc.gridRowOrderByQuestionId?.[op.questionId]
|
|
428
|
+
const order = doc.gridRowOrderByQuestionId?.[op.questionId];
|
|
429
|
+
if (!order?.includes(op.rowId)) {
|
|
430
|
+
throw new OperationConflictError(`Grid row "${op.rowId}" does not belong to question "${op.questionId}"`);
|
|
431
|
+
}
|
|
280
432
|
const remaining = order.filter((id) => id !== op.rowId);
|
|
281
433
|
return patchDocument(doc, {
|
|
282
434
|
gridRowsById: doc.gridRowsById && omitKey(doc.gridRowsById, op.rowId),
|
|
283
|
-
gridRowOrderByQuestionId:
|
|
284
|
-
...(doc.gridRowOrderByQuestionId ?? {}),
|
|
285
|
-
[op.questionId]: remaining,
|
|
286
|
-
},
|
|
435
|
+
gridRowOrderByQuestionId: setOrRemoveOrder(doc.gridRowOrderByQuestionId, op.questionId, remaining),
|
|
287
436
|
});
|
|
288
437
|
}
|
|
289
438
|
case 'gridRow.updateCell': {
|
|
@@ -291,6 +440,9 @@ const reduce = (doc, op) => {
|
|
|
291
440
|
if (!row) {
|
|
292
441
|
throw new OperationConflictError(`Unknown grid row "${op.rowId}"`);
|
|
293
442
|
}
|
|
443
|
+
if (!doc.gridRowOrderByQuestionId?.[op.questionId]?.includes(op.rowId)) {
|
|
444
|
+
throw new OperationConflictError(`Grid row "${op.rowId}" does not belong to question "${op.questionId}"`);
|
|
445
|
+
}
|
|
294
446
|
const cells = writeField({ ...(row.cells ?? {}) }, op.columnQuestionId, op.value);
|
|
295
447
|
return {
|
|
296
448
|
...doc,
|
|
@@ -350,14 +502,14 @@ const reduce = (doc, op) => {
|
|
|
350
502
|
};
|
|
351
503
|
}
|
|
352
504
|
case 'alternative.delete': {
|
|
353
|
-
const order = doc.alternativeOrderByQuestionId?.[op.questionId]
|
|
505
|
+
const order = doc.alternativeOrderByQuestionId?.[op.questionId];
|
|
506
|
+
if (!order?.includes(op.alternativeId)) {
|
|
507
|
+
throw new OperationConflictError(`Alternative "${op.alternativeId}" does not belong to question "${op.questionId}"`);
|
|
508
|
+
}
|
|
354
509
|
const remaining = order.filter((id) => id !== op.alternativeId);
|
|
355
510
|
return patchDocument(doc, {
|
|
356
511
|
alternativesById: doc.alternativesById && omitKey(doc.alternativesById, op.alternativeId),
|
|
357
|
-
alternativeOrderByQuestionId:
|
|
358
|
-
...(doc.alternativeOrderByQuestionId ?? {}),
|
|
359
|
-
[op.questionId]: remaining,
|
|
360
|
-
},
|
|
512
|
+
alternativeOrderByQuestionId: setOrRemoveOrder(doc.alternativeOrderByQuestionId, op.questionId, remaining),
|
|
361
513
|
});
|
|
362
514
|
}
|
|
363
515
|
case 'tab.create': {
|
|
@@ -392,22 +544,6 @@ const reduce = (doc, op) => {
|
|
|
392
544
|
tabOrder: (doc.tabOrder ?? []).filter((id) => id !== op.tabId),
|
|
393
545
|
});
|
|
394
546
|
}
|
|
395
|
-
case 'layout.updateQuestion': {
|
|
396
|
-
const question = doc.questionsById?.[op.questionId];
|
|
397
|
-
if (!question) {
|
|
398
|
-
throw new OperationConflictError(`Unknown question "${op.questionId}"`);
|
|
399
|
-
}
|
|
400
|
-
let presentation = { ...(question.presentation ?? {}) };
|
|
401
|
-
for (const [key, value] of Object.entries(op.patch)) {
|
|
402
|
-
presentation = writeField(presentation, key, value);
|
|
403
|
-
}
|
|
404
|
-
const next = { ...question };
|
|
405
|
-
if (Object.keys(presentation).length === 0)
|
|
406
|
-
delete next.presentation;
|
|
407
|
-
else
|
|
408
|
-
next.presentation = presentation;
|
|
409
|
-
return { ...doc, questionsById: { ...doc.questionsById, [op.questionId]: next } };
|
|
410
|
-
}
|
|
411
547
|
case 'action.create': {
|
|
412
548
|
if (doc.actionsById?.[op.actionId]) {
|
|
413
549
|
throw new OperationConflictError(`Action "${op.actionId}" already exists`);
|
|
@@ -435,6 +571,60 @@ const reduce = (doc, op) => {
|
|
|
435
571
|
actionsById: doc.actionsById && omitKey(doc.actionsById, op.actionId),
|
|
436
572
|
});
|
|
437
573
|
}
|
|
574
|
+
case 'dataMapping.create': {
|
|
575
|
+
if (doc.dataMappingsById?.[op.mappingId]) {
|
|
576
|
+
throw new OperationConflictError(`Data mapping "${op.mappingId}" already exists`);
|
|
577
|
+
}
|
|
578
|
+
const root = doc.mappingNodesById?.[op.rootNodeId];
|
|
579
|
+
if (!root) {
|
|
580
|
+
throw new OperationConflictError(`Unknown mapping node "${op.rootNodeId}"`);
|
|
581
|
+
}
|
|
582
|
+
if (root.parentNodeId !== undefined) {
|
|
583
|
+
throw new OperationConflictError(`Mapping root "${op.rootNodeId}" is not a root node — it has a parent`);
|
|
584
|
+
}
|
|
585
|
+
// Adopt the tree's existing bindings into `bindingOrder` — bindings authored before
|
|
586
|
+
// the tree was attached (structure first, §2.2a Gate 2) must not stay unordered.
|
|
587
|
+
const treeNodeIds = collectTreeNodeIds(doc, op.rootNodeId);
|
|
588
|
+
const bindingOrder = Object.entries(doc.mappingBindingsById ?? {})
|
|
589
|
+
.filter(([, binding]) => treeNodeIds.has(binding.nodeId))
|
|
590
|
+
.map(([bindingId]) => bindingId);
|
|
591
|
+
return {
|
|
592
|
+
...doc,
|
|
593
|
+
dataMappingsById: {
|
|
594
|
+
...(doc.dataMappingsById ?? {}),
|
|
595
|
+
[op.mappingId]: {
|
|
596
|
+
sourceId: op.sourceId,
|
|
597
|
+
rootNodeId: op.rootNodeId,
|
|
598
|
+
...(bindingOrder.length === 0 ? {} : { bindingOrder }),
|
|
599
|
+
},
|
|
600
|
+
},
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
case 'dataMapping.delete': {
|
|
604
|
+
const mapping = doc.dataMappingsById?.[op.mappingId];
|
|
605
|
+
// An already-gone mapping is a replayed duplicate in the common case — converge
|
|
606
|
+
// silently, the way mappingNode.delete and mappingBinding.delete do.
|
|
607
|
+
if (!mapping)
|
|
608
|
+
return doc;
|
|
609
|
+
// Cascade the whole traversal tree — nodes, their filters and bindings — so an
|
|
610
|
+
// orphaned subtree never remains as dead hash weight. The entry's own
|
|
611
|
+
// `bindingOrder` goes with it, so no order scrub is needed here.
|
|
612
|
+
const treeNodeIds = collectTreeNodeIds(doc, mapping.rootNodeId);
|
|
613
|
+
const filterIds = new Set();
|
|
614
|
+
for (const nodeId of treeNodeIds) {
|
|
615
|
+
for (const filterId of doc.mappingNodesById?.[nodeId]?.filterOrder ?? [])
|
|
616
|
+
filterIds.add(filterId);
|
|
617
|
+
}
|
|
618
|
+
return patchDocument(doc, {
|
|
619
|
+
dataMappingsById: doc.dataMappingsById && omitKey(doc.dataMappingsById, op.mappingId),
|
|
620
|
+
mappingNodesById: doc.mappingNodesById &&
|
|
621
|
+
Object.fromEntries(Object.entries(doc.mappingNodesById).filter(([id]) => !treeNodeIds.has(id))),
|
|
622
|
+
mappingFiltersById: doc.mappingFiltersById &&
|
|
623
|
+
Object.fromEntries(Object.entries(doc.mappingFiltersById).filter(([id]) => !filterIds.has(id))),
|
|
624
|
+
mappingBindingsById: doc.mappingBindingsById &&
|
|
625
|
+
Object.fromEntries(Object.entries(doc.mappingBindingsById).filter(([, binding]) => !treeNodeIds.has(binding.nodeId))),
|
|
626
|
+
});
|
|
627
|
+
}
|
|
438
628
|
case 'mappingNode.create': {
|
|
439
629
|
if (doc.mappingNodesById?.[op.nodeId]) {
|
|
440
630
|
throw new OperationConflictError(`Mapping node "${op.nodeId}" already exists`);
|
|
@@ -462,8 +652,13 @@ const reduce = (doc, op) => {
|
|
|
462
652
|
else
|
|
463
653
|
next.relationshipId = op.patch.relationshipId;
|
|
464
654
|
}
|
|
465
|
-
if (op.patch.filterOrder !== undefined)
|
|
466
|
-
|
|
655
|
+
if (op.patch.filterOrder !== undefined) {
|
|
656
|
+
// An empty order is an explicit unset (DOC-LAW-2 stores no empty array).
|
|
657
|
+
if (op.patch.filterOrder.length === 0)
|
|
658
|
+
delete next.filterOrder;
|
|
659
|
+
else
|
|
660
|
+
next.filterOrder = op.patch.filterOrder;
|
|
661
|
+
}
|
|
467
662
|
return { ...doc, mappingNodesById: { ...doc.mappingNodesById, [op.nodeId]: next } };
|
|
468
663
|
}
|
|
469
664
|
case 'mappingNode.delete': {
|
|
@@ -472,12 +667,16 @@ const reduce = (doc, op) => {
|
|
|
472
667
|
if (filtersById) {
|
|
473
668
|
filtersById = filterIds.reduce((acc, id) => omitKey(acc, id), filtersById);
|
|
474
669
|
}
|
|
670
|
+
const removedBindingIds = new Set(Object.entries(doc.mappingBindingsById ?? {})
|
|
671
|
+
.filter(([, binding]) => binding.nodeId === op.nodeId)
|
|
672
|
+
.map(([bindingId]) => bindingId));
|
|
475
673
|
const bindings = Object.entries(doc.mappingBindingsById ?? {}).filter(([, binding]) => binding.nodeId !== op.nodeId);
|
|
476
|
-
|
|
674
|
+
const withoutNode = patchDocument(doc, {
|
|
477
675
|
mappingNodesById: doc.mappingNodesById && omitKey(doc.mappingNodesById, op.nodeId),
|
|
478
676
|
mappingFiltersById: filtersById,
|
|
479
677
|
mappingBindingsById: Object.fromEntries(bindings),
|
|
480
678
|
});
|
|
679
|
+
return scrubBindingOrders(withoutNode, removedBindingIds);
|
|
481
680
|
}
|
|
482
681
|
case 'mappingBinding.create': {
|
|
483
682
|
if (!doc.mappingNodesById?.[op.nodeId]) {
|
|
@@ -488,13 +687,31 @@ const reduce = (doc, op) => {
|
|
|
488
687
|
if (clash) {
|
|
489
688
|
throw new OperationConflictError(`Target ${wanted} is already bound by "${clash[0]}" — at most one binding per target`);
|
|
490
689
|
}
|
|
491
|
-
|
|
690
|
+
const created = {
|
|
492
691
|
...doc,
|
|
493
692
|
mappingBindingsById: {
|
|
494
693
|
...(doc.mappingBindingsById ?? {}),
|
|
495
694
|
[op.bindingId]: { nodeId: op.nodeId, fieldId: op.fieldId, target: op.target },
|
|
496
695
|
},
|
|
497
696
|
};
|
|
697
|
+
// The owning mapping root's `bindingOrder` lists the new binding — when the tree is
|
|
698
|
+
// attached to one. A binding on a not-yet-attached tree is legal (structure first,
|
|
699
|
+
// mapping afterwards — §2.2a Gate 2), and `dataMapping.create` adopts its bindings'
|
|
700
|
+
// ids when the tree attaches.
|
|
701
|
+
const owner = mappingForNode(created, op.nodeId);
|
|
702
|
+
if (owner === undefined)
|
|
703
|
+
return created;
|
|
704
|
+
const [mappingId, mapping] = owner;
|
|
705
|
+
const bindingOrder = mapping.bindingOrder ?? [];
|
|
706
|
+
if (bindingOrder.includes(op.bindingId))
|
|
707
|
+
return created;
|
|
708
|
+
return {
|
|
709
|
+
...created,
|
|
710
|
+
dataMappingsById: {
|
|
711
|
+
...created.dataMappingsById,
|
|
712
|
+
[mappingId]: { ...mapping, bindingOrder: [...bindingOrder, op.bindingId] },
|
|
713
|
+
},
|
|
714
|
+
};
|
|
498
715
|
}
|
|
499
716
|
case 'mappingBinding.update': {
|
|
500
717
|
const binding = doc.mappingBindingsById?.[op.bindingId];
|
|
@@ -504,6 +721,15 @@ const reduce = (doc, op) => {
|
|
|
504
721
|
if (op.patch.nodeId && !doc.mappingNodesById?.[op.patch.nodeId]) {
|
|
505
722
|
throw new OperationConflictError(`Unknown mapping node "${op.patch.nodeId}"`);
|
|
506
723
|
}
|
|
724
|
+
// A target change must clear the same cardinality check as a create — otherwise an
|
|
725
|
+
// update is a back door around "at most one binding per target" (§2.2a).
|
|
726
|
+
if (op.patch.target !== undefined) {
|
|
727
|
+
const wanted = targetKey(op.patch.target);
|
|
728
|
+
const clash = Object.entries(doc.mappingBindingsById ?? {}).find(([bindingId, other]) => bindingId !== op.bindingId && targetKey(other.target) === wanted);
|
|
729
|
+
if (clash) {
|
|
730
|
+
throw new OperationConflictError(`Target ${wanted} is already bound by "${clash[0]}" — at most one binding per target`);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
507
733
|
return {
|
|
508
734
|
...doc,
|
|
509
735
|
mappingBindingsById: {
|
|
@@ -513,9 +739,9 @@ const reduce = (doc, op) => {
|
|
|
513
739
|
};
|
|
514
740
|
}
|
|
515
741
|
case 'mappingBinding.delete':
|
|
516
|
-
return patchDocument(doc, {
|
|
742
|
+
return scrubBindingOrders(patchDocument(doc, {
|
|
517
743
|
mappingBindingsById: doc.mappingBindingsById && omitKey(doc.mappingBindingsById, op.bindingId),
|
|
518
|
-
});
|
|
744
|
+
}), new Set([op.bindingId]));
|
|
519
745
|
case 'mappingFilter.set': {
|
|
520
746
|
const node = doc.mappingNodesById?.[op.nodeId];
|
|
521
747
|
if (!node) {
|
|
@@ -536,30 +762,25 @@ const reduce = (doc, op) => {
|
|
|
536
762
|
};
|
|
537
763
|
}
|
|
538
764
|
case 'mappingFilter.delete': {
|
|
539
|
-
const nodes = Object.fromEntries(Object.entries(doc.mappingNodesById ?? {}).map(([nodeId, node]) =>
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
765
|
+
const nodes = Object.fromEntries(Object.entries(doc.mappingNodesById ?? {}).map(([nodeId, node]) => {
|
|
766
|
+
if (!node.filterOrder?.includes(op.filterId))
|
|
767
|
+
return [nodeId, node];
|
|
768
|
+
const filterOrder = node.filterOrder.filter((id) => id !== op.filterId);
|
|
769
|
+
// DOC-LAW-2: an emptied order array is dropped, never stored as [].
|
|
770
|
+
const { filterOrder: _removed, ...rest } = node;
|
|
771
|
+
return [nodeId, filterOrder.length === 0 ? rest : { ...node, filterOrder }];
|
|
772
|
+
}));
|
|
545
773
|
return patchDocument(doc, {
|
|
546
774
|
mappingFiltersById: doc.mappingFiltersById && omitKey(doc.mappingFiltersById, op.filterId),
|
|
547
775
|
mappingNodesById: nodes,
|
|
548
776
|
});
|
|
549
777
|
}
|
|
550
778
|
case 'visibilityRule.set': {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
[op.ruleId]: { condition: op.condition },
|
|
557
|
-
},
|
|
558
|
-
visibilityRuleOrderByQuestionId: {
|
|
559
|
-
...(doc.visibilityRuleOrderByQuestionId ?? {}),
|
|
560
|
-
[op.questionId]: order.includes(op.ruleId) ? order : [...order, op.ruleId],
|
|
561
|
-
},
|
|
562
|
-
};
|
|
779
|
+
if (!doc.questionsById?.[op.questionId]) {
|
|
780
|
+
throw new OperationConflictError(`Unknown question "${op.questionId}"`);
|
|
781
|
+
}
|
|
782
|
+
const { rulesById, orderByQuestionId } = setScopedRule(doc.visibilityRulesById, doc.visibilityRuleOrderByQuestionId, op.ruleId, op.questionId, op.condition);
|
|
783
|
+
return { ...doc, visibilityRulesById: rulesById, visibilityRuleOrderByQuestionId: orderByQuestionId };
|
|
563
784
|
}
|
|
564
785
|
case 'visibilityRule.delete': {
|
|
565
786
|
const orders = Object.fromEntries(Object.entries(doc.visibilityRuleOrderByQuestionId ?? {}).map(([questionId, ruleIds]) => [
|
|
@@ -572,18 +793,11 @@ const reduce = (doc, op) => {
|
|
|
572
793
|
});
|
|
573
794
|
}
|
|
574
795
|
case 'highlightRule.set': {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
[op.ruleId]: { condition: op.condition },
|
|
581
|
-
},
|
|
582
|
-
highlightRuleOrderByQuestionId: {
|
|
583
|
-
...(doc.highlightRuleOrderByQuestionId ?? {}),
|
|
584
|
-
[op.questionId]: order.includes(op.ruleId) ? order : [...order, op.ruleId],
|
|
585
|
-
},
|
|
586
|
-
};
|
|
796
|
+
if (!doc.questionsById?.[op.questionId]) {
|
|
797
|
+
throw new OperationConflictError(`Unknown question "${op.questionId}"`);
|
|
798
|
+
}
|
|
799
|
+
const { rulesById, orderByQuestionId } = setScopedRule(doc.highlightRulesById, doc.highlightRuleOrderByQuestionId, op.ruleId, op.questionId, op.condition);
|
|
800
|
+
return { ...doc, highlightRulesById: rulesById, highlightRuleOrderByQuestionId: orderByQuestionId };
|
|
587
801
|
}
|
|
588
802
|
case 'highlightRule.delete': {
|
|
589
803
|
const orders = Object.fromEntries(Object.entries(doc.highlightRuleOrderByQuestionId ?? {}).map(([questionId, ruleIds]) => [
|