@mengine/medeo-client 2.1.1-dsl.9 → 3.0.1-dsl.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/README.md +13 -9
- package/dist/{dsl-COyzz7Fp.js → dsl-BngbR2zr.js} +2551 -249
- package/dist/dsl.d.ts +1 -2
- package/dist/dsl.js +1 -2
- package/dist/{index-C5wDKYqC.d.ts → index-B1Lqm_Z1.d.ts} +919 -21
- package/dist/{index-DAfOkcnZ.d.ts → index-pULoWyd9.d.ts} +205 -5
- package/dist/index.d.ts +2 -4
- package/dist/index.js +828 -7
- package/dist/schemas.d.ts +21 -21
- package/dist/schemas.js +57 -21
- package/dist/testing.d.ts +2 -2
- package/dist/testing.js +37 -16
- package/package.json +5 -6
- package/dist/index-CHiPztZr.d.ts +0 -194
- package/dist/legacy.d.ts +0 -1044
- package/dist/legacy.js +0 -4349
- package/dist/shared-iWnU2osE.js +0 -39
- package/dist/storage-BW3tI_ER.js +0 -801
- package/dist/video-draft-types-BnbfWrBx.js +0 -1486
- package/dist/video-draft-types-GqXnsOzA.d.ts +0 -869
|
@@ -1,6 +1,337 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
2
2
|
import { Cursor, LoroDoc, LoroList, LoroMap, LoroMovableList, LoroText, LoroTree, isContainer, isContainerId } from "loro-crdt";
|
|
3
|
-
|
|
3
|
+
//#region ../medeo-dsl/src/ids.ts
|
|
4
|
+
function bytesToBase64url(bytes) {
|
|
5
|
+
let binary = "";
|
|
6
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
7
|
+
return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
8
|
+
}
|
|
9
|
+
function base64urlToBytes(value) {
|
|
10
|
+
if (!/^[A-Za-z0-9_-]+$/.test(value) || value.length % 4 === 1) throw new Error("Invalid base64url");
|
|
11
|
+
const bytes = Uint8Array.from(atob(value.replaceAll("-", "+").replaceAll("_", "/")), (c) => c.charCodeAt(0));
|
|
12
|
+
if (bytesToBase64url(bytes) !== value) throw new Error("Non-canonical base64url");
|
|
13
|
+
return bytes;
|
|
14
|
+
}
|
|
15
|
+
function tupleHash(tuple) {
|
|
16
|
+
return bytesToBase64url(sha256(new TextEncoder().encode(JSON.stringify(tuple))));
|
|
17
|
+
}
|
|
18
|
+
function resourceId(kind, system, key) {
|
|
19
|
+
return `${kind}:${tupleHash([system, key])}`;
|
|
20
|
+
}
|
|
21
|
+
function relationId(table, value) {
|
|
22
|
+
const v = value;
|
|
23
|
+
switch (table) {
|
|
24
|
+
case "voice-timbre-relations": return `voice-timbre:${tupleHash([v.recordingId, v.voiceId])}`;
|
|
25
|
+
case "derived-from-relations": return `derived-from:${tupleHash([v.entityId, v.sourceId])}`;
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`Unknown relation table: ${table}`);
|
|
28
|
+
}
|
|
29
|
+
const defaultIdFactory = (kind) => `${kind}_${globalThis.crypto.randomUUID()}`;
|
|
30
|
+
const resourceKinds = {
|
|
31
|
+
"media-entities": "media",
|
|
32
|
+
"voice-entities": "voice"
|
|
33
|
+
};
|
|
34
|
+
/** A resource row's ID is derived from its locator, so one external resource is one entity. */
|
|
35
|
+
function assertResourceIdentity(table, id, value) {
|
|
36
|
+
const kind = resourceKinds[table];
|
|
37
|
+
if (kind && id !== resourceId(kind, value.system, value.key)) throw new Error("Resource identity does not match locator");
|
|
38
|
+
}
|
|
39
|
+
/** A relation row's ID is derived from its two ends. */
|
|
40
|
+
function assertRelationIdentity(table, id, value) {
|
|
41
|
+
if (id !== relationId(table, value)) throw new Error("Relation ID does not match endpoints");
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region ../medeo-dsl/src/fields.ts
|
|
45
|
+
const string = (v) => typeof v === "string";
|
|
46
|
+
const nonempty = (v) => string(v) && v.length > 0;
|
|
47
|
+
const finite = (v) => typeof v === "number" && Number.isFinite(v);
|
|
48
|
+
const fraction = (v) => finite(v) && v >= 0 && v <= 1;
|
|
49
|
+
const integer = (v) => typeof v === "number" && Number.isSafeInteger(v);
|
|
50
|
+
const nonnegative = (v) => integer(v) && v >= 0;
|
|
51
|
+
const positive = (v) => integer(v) && v > 0;
|
|
52
|
+
const oneOf = (...values) => (v) => values.includes(v);
|
|
53
|
+
const required = (check) => ({ check });
|
|
54
|
+
const optional$1 = (check) => ({
|
|
55
|
+
check,
|
|
56
|
+
optional: true
|
|
57
|
+
});
|
|
58
|
+
const reservedIds = [
|
|
59
|
+
"$cid",
|
|
60
|
+
"__proto__",
|
|
61
|
+
"constructor",
|
|
62
|
+
"prototype"
|
|
63
|
+
];
|
|
64
|
+
const id = (v) => nonempty(v) && !reservedIds.includes(v);
|
|
65
|
+
const cursor = (v) => v instanceof Cursor;
|
|
66
|
+
function isRecord(value) {
|
|
67
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) && !(value instanceof Cursor);
|
|
68
|
+
}
|
|
69
|
+
function assertId(value) {
|
|
70
|
+
if (!id(value)) throw new Error("Expected a nonempty, non-reserved ID");
|
|
71
|
+
}
|
|
72
|
+
function validLanguage(value) {
|
|
73
|
+
if (typeof value !== "string") return false;
|
|
74
|
+
if (/^x(?:-[a-z0-9]{1,8})+$/i.test(value)) return true;
|
|
75
|
+
try {
|
|
76
|
+
return value.length > 0 && Intl.getCanonicalLocales(value).length === 1;
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function object(fields) {
|
|
82
|
+
return (v) => {
|
|
83
|
+
try {
|
|
84
|
+
checkFields(v, fields);
|
|
85
|
+
return true;
|
|
86
|
+
} catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function checkFields(value, fields, partial = false) {
|
|
92
|
+
if (!isRecord(value)) throw new Error("Expected an object");
|
|
93
|
+
for (const key of Object.keys(value)) if (!Object.hasOwn(fields, key)) throw new Error(`Unknown field: ${key}`);
|
|
94
|
+
for (const [key, field] of Object.entries(fields)) {
|
|
95
|
+
if (partial && !Object.hasOwn(value, key)) continue;
|
|
96
|
+
const v = value[key];
|
|
97
|
+
if (v === void 0 && field.optional) continue;
|
|
98
|
+
if (!field.check(v)) throw new Error(`Invalid or missing field: ${key}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const styleFields = {
|
|
102
|
+
font: optional$1(object({
|
|
103
|
+
system: required(oneOf("font-library")),
|
|
104
|
+
key: required(nonempty)
|
|
105
|
+
})),
|
|
106
|
+
fontSize: optional$1(finite),
|
|
107
|
+
fontColor: optional$1(string),
|
|
108
|
+
fontWeight: optional$1(finite),
|
|
109
|
+
entranceAnimation: optional$1(string),
|
|
110
|
+
entranceAnimationDurationMs: optional$1(nonnegative),
|
|
111
|
+
strokeColor: optional$1(string),
|
|
112
|
+
strokeWidth: optional$1(finite),
|
|
113
|
+
positionX: optional$1(fraction),
|
|
114
|
+
positionY: optional$1(fraction)
|
|
115
|
+
};
|
|
116
|
+
const seqMarkerFields = {
|
|
117
|
+
inMs: required(nonnegative),
|
|
118
|
+
outMs: required(nonnegative),
|
|
119
|
+
rate: optional$1((v) => finite(v) && v > 0),
|
|
120
|
+
fillTimeline: optional$1(oneOf(true))
|
|
121
|
+
};
|
|
122
|
+
/** A sequence marker's own fields; the window being nonempty is checked where it is written. */
|
|
123
|
+
function assertSeqMarker(value) {
|
|
124
|
+
checkFields(value, seqMarkerFields);
|
|
125
|
+
}
|
|
126
|
+
const timeAnchorFields = {
|
|
127
|
+
to: required(id),
|
|
128
|
+
offsetMs: required(integer),
|
|
129
|
+
fallback_abs_ms: required(nonnegative)
|
|
130
|
+
};
|
|
131
|
+
/** A time anchor's own fields; what it points at is checked by `assertReferenceAdmitted`. */
|
|
132
|
+
function assertTimeAnchor(value) {
|
|
133
|
+
checkFields(value, timeAnchorFields);
|
|
134
|
+
}
|
|
135
|
+
const selectionFields = {
|
|
136
|
+
audioScriptId: required(id),
|
|
137
|
+
segmentId: required(id),
|
|
138
|
+
start: optional$1(cursor),
|
|
139
|
+
end: optional$1(cursor)
|
|
140
|
+
};
|
|
141
|
+
/** A whole segment, or a run inside it: both ends or neither. */
|
|
142
|
+
const segmentSelection = (fields) => (v) => object(fields)(v) && v.start === void 0 === (v.end === void 0);
|
|
143
|
+
function assertCaptionContent(value) {
|
|
144
|
+
if (!segmentSelection(selectionFields)(value)) throw new Error("Invalid segment selection");
|
|
145
|
+
}
|
|
146
|
+
const phonemeFields = {
|
|
147
|
+
...selectionFields,
|
|
148
|
+
phonemeScript: optional$1(nonempty)
|
|
149
|
+
};
|
|
150
|
+
/** D190: omitted pronunciation uses the selected source words; an authored pronunciation is never empty. */
|
|
151
|
+
function assertPhonemeScript(value) {
|
|
152
|
+
if (value !== void 0 && !nonempty(value)) throw new Error("phonemeScript must be nonempty when provided");
|
|
153
|
+
}
|
|
154
|
+
function assertPhoneme(value) {
|
|
155
|
+
if (!segmentSelection(phonemeFields)(value)) throw new Error("Invalid phoneme");
|
|
156
|
+
}
|
|
157
|
+
const segmentFields = {
|
|
158
|
+
id: required(id),
|
|
159
|
+
text: required(string),
|
|
160
|
+
startMs: optional$1(nonnegative),
|
|
161
|
+
endMs: optional$1(nonnegative)
|
|
162
|
+
};
|
|
163
|
+
/** A segment's time in the medium it was read off: both ends or neither, the end not before the start. */
|
|
164
|
+
const segment = (v) => {
|
|
165
|
+
if (!object(segmentFields)(v)) return false;
|
|
166
|
+
const { startMs, endMs } = v;
|
|
167
|
+
return startMs === void 0 ? endMs === void 0 : endMs !== void 0 && endMs >= startMs;
|
|
168
|
+
};
|
|
169
|
+
/** One segment as a script stores it; a segment added on its own is checked here, not with its row. */
|
|
170
|
+
function assertScriptSegment(value) {
|
|
171
|
+
if (!segment(value)) throw new Error("A segment is { id, text } with startMs and endMs both or neither, endMs not before startMs");
|
|
172
|
+
}
|
|
173
|
+
/** A script's segments: each with its own id, unique within the script. */
|
|
174
|
+
const segments = (v) => Array.isArray(v) && v.every(segment) && new Set(v.map((s) => s.id)).size === v.length;
|
|
175
|
+
const locator = {
|
|
176
|
+
system: required(oneOf("memota", "memota-speech")),
|
|
177
|
+
key: required(nonempty)
|
|
178
|
+
};
|
|
179
|
+
const entityFields = {
|
|
180
|
+
"timeline-entities": {},
|
|
181
|
+
"track-entities": {
|
|
182
|
+
role: required(oneOf("video_clip", "speech", "caption", "bgm")),
|
|
183
|
+
hidden: optional$1((v) => typeof v === "boolean")
|
|
184
|
+
},
|
|
185
|
+
"clip-entities": {
|
|
186
|
+
seqMarker: required(object(seqMarkerFields)),
|
|
187
|
+
volume: optional$1(finite),
|
|
188
|
+
source: required(id),
|
|
189
|
+
anchor: optional$1(object(timeAnchorFields))
|
|
190
|
+
},
|
|
191
|
+
"media-entities": {
|
|
192
|
+
...locator,
|
|
193
|
+
kind: required(oneOf("video", "audio", "voiceover-recording", "image")),
|
|
194
|
+
storageKey: optional$1(nonempty),
|
|
195
|
+
durationMs: optional$1(positive),
|
|
196
|
+
digest: optional$1(nonempty)
|
|
197
|
+
},
|
|
198
|
+
"audio-script-entities": {
|
|
199
|
+
segments: required(segments),
|
|
200
|
+
defaultLanguage: optional$1(validLanguage),
|
|
201
|
+
system: optional$1(oneOf("memota")),
|
|
202
|
+
key: optional$1(nonempty),
|
|
203
|
+
storageKey: optional$1(nonempty),
|
|
204
|
+
digest: optional$1(nonempty)
|
|
205
|
+
},
|
|
206
|
+
"phonetic-script-entities": {
|
|
207
|
+
phoneme: optional$1(segmentSelection(phonemeFields)),
|
|
208
|
+
system: optional$1(oneOf("memota")),
|
|
209
|
+
key: optional$1(nonempty),
|
|
210
|
+
storageKey: optional$1(nonempty),
|
|
211
|
+
digest: optional$1(nonempty)
|
|
212
|
+
},
|
|
213
|
+
"caption-entities": {
|
|
214
|
+
selection: optional$1(segmentSelection(selectionFields)),
|
|
215
|
+
text: optional$1(string),
|
|
216
|
+
style: optional$1(object(styleFields))
|
|
217
|
+
},
|
|
218
|
+
"voice-entities": {
|
|
219
|
+
system: required(oneOf("voice-library")),
|
|
220
|
+
key: required(nonempty),
|
|
221
|
+
name: optional$1(string)
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
const entityTables = Object.keys(entityFields);
|
|
225
|
+
/**
|
|
226
|
+
* Fields stored as one encoded string: written whole, never merged field by field. A patch to one
|
|
227
|
+
* is merged into the current value first, then the whole value is written.
|
|
228
|
+
*/
|
|
229
|
+
const atomicFields = {
|
|
230
|
+
"clip-entities": ["seqMarker", "anchor"],
|
|
231
|
+
"phonetic-script-entities": ["phoneme"],
|
|
232
|
+
"caption-entities": ["selection"]
|
|
233
|
+
};
|
|
234
|
+
const relationFields = {
|
|
235
|
+
"voice-timbre-relations": {
|
|
236
|
+
recordingId: required(id),
|
|
237
|
+
voiceId: required(id)
|
|
238
|
+
},
|
|
239
|
+
"derived-from-relations": {
|
|
240
|
+
entityId: required(id),
|
|
241
|
+
sourceId: required(id),
|
|
242
|
+
sourceDigest: optional$1(nonempty)
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
const relationTables = Object.keys(relationFields);
|
|
246
|
+
const fileFields = [
|
|
247
|
+
"system",
|
|
248
|
+
"key",
|
|
249
|
+
"storageKey",
|
|
250
|
+
"digest"
|
|
251
|
+
];
|
|
252
|
+
const phonemeScriptOf = (row) => isRecord(row.phoneme) ? row.phoneme.phonemeScript : void 0;
|
|
253
|
+
/**
|
|
254
|
+
* A PhoneticScript as a write leaves it: written for a segment (D181), and its phonemeScript is raw
|
|
255
|
+
* content stored as its file (D178) — with text the row names that file and its digest, without
|
|
256
|
+
* text it has no file, and rewritten text is a new file.
|
|
257
|
+
*/
|
|
258
|
+
function assertPhoneticScript(before, after) {
|
|
259
|
+
if (!isRecord(after.phoneme)) throw new Error("A PhoneticScript is written for a segment of its base script");
|
|
260
|
+
if (phonemeScriptOf(after) === void 0) {
|
|
261
|
+
if (fileFields.some((field) => after[field] !== void 0)) throw new Error("A PhoneticScript without text has no file");
|
|
262
|
+
} else if (after.system === void 0 || after.key === void 0 || after.digest === void 0) throw new Error("A PhoneticScript stores its phonemeScript as a file: name it by system and key with its digest");
|
|
263
|
+
if (before !== void 0 && phonemeScriptOf(after) !== phonemeScriptOf(before) && after.digest !== void 0 && after.digest === before.digest) throw new Error("A rewritten phonemeScript is stored again: write its new file");
|
|
264
|
+
}
|
|
265
|
+
/** A transcript names the file it was read from by system and key together; its facts come with that name. */
|
|
266
|
+
function assertTranscriptFile(row) {
|
|
267
|
+
if (row.system === void 0 !== (row.key === void 0)) throw new Error("A transcript names its transcription file by system and key");
|
|
268
|
+
if (row.key === void 0 && (row.storageKey !== void 0 || row.digest !== void 0)) throw new Error("A script without a transcription file has no file facts");
|
|
269
|
+
}
|
|
270
|
+
function assertEntity(table, value) {
|
|
271
|
+
checkFields(value, entityFields[table]);
|
|
272
|
+
if (table === "audio-script-entities") assertTranscriptFile(value);
|
|
273
|
+
if (table === "media-entities") {
|
|
274
|
+
if (value.kind === "image" && value.durationMs !== void 0) throw new Error("Image has no intrinsic duration");
|
|
275
|
+
if (value.kind !== "image" && !positive(value.durationMs)) throw new Error("Finite media requires durationMs");
|
|
276
|
+
if (["audio", "voiceover-recording"].includes(value.kind) && !nonempty(value.storageKey)) throw new Error("Audio requires storageKey");
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/** Validate only touched fields, so an unrelated edit can proceed in a merged, invalid document. */
|
|
280
|
+
function assertEntityPatch(table, patch, current) {
|
|
281
|
+
if (!isRecord(patch)) throw new Error("Expected an entity patch");
|
|
282
|
+
if (table === "audio-script-entities" && Object.hasOwn(patch, "segments")) throw new Error("Change segments with segment mutations and edit their text through the Text adapter");
|
|
283
|
+
if (table === "caption-entities" && current.style !== void 0 && Object.hasOwn(patch, "style") && patch.style === void 0) throw new Error("Reset individual style fields; retain the style Map");
|
|
284
|
+
if (table === "caption-entities" && current.text !== void 0 && patch.text !== void 0) throw new Error("Edit Caption text through the Text adapter");
|
|
285
|
+
const next = patchRow(table, current, patch);
|
|
286
|
+
for (const key of Object.keys(patch)) {
|
|
287
|
+
const field = entityFields[table][key];
|
|
288
|
+
if (!field || !field.optional && next[key] === void 0 || next[key] !== void 0 && !field.check(next[key])) throw new Error(`Invalid patch field: ${key}`);
|
|
289
|
+
}
|
|
290
|
+
if (table === "audio-script-entities" && fileFields.some((field) => Object.hasOwn(patch, field))) {
|
|
291
|
+
if (current.key !== void 0 && (next.system !== current.system || next.key !== current.key)) throw new Error("A transcript's transcription file is fixed once named");
|
|
292
|
+
assertTranscriptFile(next);
|
|
293
|
+
}
|
|
294
|
+
if (table === "media-entities" || table === "voice-entities") {
|
|
295
|
+
for (const key of [
|
|
296
|
+
"system",
|
|
297
|
+
"key",
|
|
298
|
+
"kind"
|
|
299
|
+
]) if (Object.hasOwn(patch, key) && patch[key] !== current[key]) throw new Error(`Resource identity field is immutable: ${key}`);
|
|
300
|
+
}
|
|
301
|
+
if (table === "media-entities") {
|
|
302
|
+
if (Object.hasOwn(patch, "durationMs")) {
|
|
303
|
+
if (next.kind === "image" ? next.durationMs !== void 0 : !positive(next.durationMs)) throw new Error("durationMs must match the media kind");
|
|
304
|
+
}
|
|
305
|
+
if (Object.hasOwn(patch, "storageKey") && ["audio", "voiceover-recording"].includes(next.kind) && !nonempty(next.storageKey)) throw new Error("Audio requires storageKey");
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function assertRelation(table, value) {
|
|
309
|
+
checkFields(value, relationFields[table]);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* The row after a patch. An atomic field merges into its current value, so a partial patch keeps
|
|
313
|
+
* the rest of it; an unreadable stored value is replaced instead, so writing a whole value repairs it.
|
|
314
|
+
*/
|
|
315
|
+
function patchRow(table, current, patch) {
|
|
316
|
+
const next = mergeFields(current, patch);
|
|
317
|
+
for (const key of atomicFields[table] ?? []) {
|
|
318
|
+
const old = current[key];
|
|
319
|
+
const value = patch[key];
|
|
320
|
+
if (value !== void 0 && isRecord(old) && old.invalid === true) next[key] = isRecord(value) ? mergeFields({}, value) : value;
|
|
321
|
+
}
|
|
322
|
+
return next;
|
|
323
|
+
}
|
|
324
|
+
/** Undefined deletes; nested patches retain sibling fields and an existing empty style Map. */
|
|
325
|
+
function mergeFields(target, patch) {
|
|
326
|
+
const result = { ...target };
|
|
327
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
328
|
+
if (reservedIds.includes(key)) throw new Error(`Reserved field: ${key}`);
|
|
329
|
+
if (value === void 0) delete result[key];
|
|
330
|
+
else result[key] = isRecord(value) ? mergeFields(isRecord(result[key]) ? result[key] : {}, value) : value;
|
|
331
|
+
}
|
|
332
|
+
return result;
|
|
333
|
+
}
|
|
334
|
+
//#endregion
|
|
4
335
|
//#region ../medeo-dsl/src/text.ts
|
|
5
336
|
function assertTextOffset(text, at) {
|
|
6
337
|
if (!Number.isSafeInteger(at) || at < 0 || at > text.length) throw new Error("Invalid UTF-16 text offset");
|
|
@@ -311,6 +642,63 @@ function assertContainTree(rows, tree) {
|
|
|
311
642
|
}
|
|
312
643
|
}
|
|
313
644
|
//#endregion
|
|
645
|
+
//#region ../medeo-dsl/src/clip-playback.ts
|
|
646
|
+
const playable = {
|
|
647
|
+
video_clip: ["video", "image"],
|
|
648
|
+
speech: ["audio", "voiceover-recording"],
|
|
649
|
+
caption: ["caption"],
|
|
650
|
+
bgm: ["audio"]
|
|
651
|
+
};
|
|
652
|
+
function supportsClipSpeed(source) {
|
|
653
|
+
return isRecord(source) && [
|
|
654
|
+
"video",
|
|
655
|
+
"audio",
|
|
656
|
+
"voiceover-recording"
|
|
657
|
+
].includes(String(source.kind));
|
|
658
|
+
}
|
|
659
|
+
/** Content-kind admission is shared by Actions, script compilation and the final transaction. */
|
|
660
|
+
function assertClipSourceRole(role, kind) {
|
|
661
|
+
if (!playable[role]?.includes(kind)) throw new Error(`A ${role} Track does not play ${kind}`);
|
|
662
|
+
}
|
|
663
|
+
function assertClipWindow(window, duration) {
|
|
664
|
+
if (!Number.isSafeInteger(window.inMs) || !Number.isSafeInteger(window.outMs) || window.inMs < 0 || window.outMs <= window.inMs) throw new Error("Invalid Clip window");
|
|
665
|
+
if (duration !== void 0 && window.outMs > duration) throw new Error(`Invalid Clip window: ${window.outMs}ms is past its content's ${duration}ms`);
|
|
666
|
+
}
|
|
667
|
+
/** Check authored playback facts; a remote merge is still read by projection with diagnostics. */
|
|
668
|
+
function assertClipPlayback(clip, source, role) {
|
|
669
|
+
if (!isRecord(source)) throw new Error("Invalid Clip source");
|
|
670
|
+
const kind = source.kind ?? "caption";
|
|
671
|
+
const marker = clip.seqMarker;
|
|
672
|
+
assertSeqMarker(marker);
|
|
673
|
+
if (marker.outMs <= marker.inMs) throw new Error("Clip window must be nonempty");
|
|
674
|
+
if (role !== void 0) assertClipSourceRole(role, kind);
|
|
675
|
+
if (marker.rate !== void 0 && !supportsClipSpeed(source)) throw new Error("Only video and sound change speed");
|
|
676
|
+
const duration = typeof source.durationMs === "number" ? source.durationMs : void 0;
|
|
677
|
+
if (marker.fillTimeline) {
|
|
678
|
+
if (kind !== "audio") throw new Error("Only BGM audio fills the Timeline");
|
|
679
|
+
if (marker.inMs !== 0 || marker.rate !== void 0 && marker.rate !== 1 || marker.outMs !== duration) throw new Error(`A Clip that fills the Timeline plays its whole source at 1×: inMs 0, outMs ${duration ?? "its length"}, no rate; the Timeline's end cuts it`);
|
|
680
|
+
}
|
|
681
|
+
assertClipWindow(marker);
|
|
682
|
+
}
|
|
683
|
+
function clipParents(tree) {
|
|
684
|
+
const result = /* @__PURE__ */ new Map();
|
|
685
|
+
for (const track of tree) for (const clip of track.children ?? []) result.set(clip.entityId, track.entityId);
|
|
686
|
+
return result;
|
|
687
|
+
}
|
|
688
|
+
/** A new parent or Track role revalidates that binding, without blocking unrelated merged rows. */
|
|
689
|
+
function assertClipPlacements(before, after, previousTree, nextTree) {
|
|
690
|
+
const previous = clipParents(previousTree);
|
|
691
|
+
for (const [id, parent] of clipParents(nextTree)) {
|
|
692
|
+
const track = after["track-entities"][parent];
|
|
693
|
+
const previousTrack = before["track-entities"][parent];
|
|
694
|
+
if (previous.get(id) === parent && (track === void 0 || isRecord(track) && isRecord(previousTrack) && previousTrack.role === track.role)) continue;
|
|
695
|
+
const role = requireEntity(after, "track-entities", parent).role;
|
|
696
|
+
const clip = after["clip-entities"][id];
|
|
697
|
+
if (!clip) continue;
|
|
698
|
+
assertClipPlayback(clip, after["media-entities"][clip.source] ?? after["caption-entities"][clip.source], role);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
//#endregion
|
|
314
702
|
//#region ../medeo-dsl/src/plan.ts
|
|
315
703
|
/** Include removed nested keys: a partial merge alone would conceal a deleted required field. */
|
|
316
704
|
function replacementPatch(before, after) {
|
|
@@ -326,6 +714,7 @@ function replacementPatch(before, after) {
|
|
|
326
714
|
*/
|
|
327
715
|
function assertChangeAdmitted(change) {
|
|
328
716
|
const { before, after } = change;
|
|
717
|
+
const parents = change.containment ? clipParents(change.containment.after) : void 0;
|
|
329
718
|
for (const [table, rows] of change.entities) for (const [id, fields] of rows) {
|
|
330
719
|
const value = after[table][id];
|
|
331
720
|
if (value === void 0) continue;
|
|
@@ -340,25 +729,17 @@ function assertChangeAdmitted(change) {
|
|
|
340
729
|
}
|
|
341
730
|
const row = value;
|
|
342
731
|
for (const field of referenceFields[table] ?? []) if (!old || fields.has(field)) assertReferenceAdmitted(after, table, id, field, row[field]);
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
732
|
+
if (table === "clip-entities" && (!old || fields.has("seqMarker") || fields.has("source"))) {
|
|
733
|
+
const parent = parents?.get(id);
|
|
734
|
+
assertClipPlayback(row, after["media-entities"][row.source] ?? after["caption-entities"][row.source], parent === void 0 ? void 0 : requireEntity(after, "track-entities", parent).role);
|
|
735
|
+
}
|
|
346
736
|
}
|
|
347
737
|
if (change.entities.has("timeline-entities") && Object.keys(after["timeline-entities"]).length > 1) throw new Error("Document already has a Timeline");
|
|
348
738
|
for (const [table, ids] of change.relations) for (const id of ids) {
|
|
349
739
|
const value = after[table][id];
|
|
350
740
|
if (value !== void 0) assertRelationAdmitted(after, table, id, value);
|
|
351
741
|
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* A Clip that fills the Timeline plays its whole source at 1×: the player loops a short source and
|
|
355
|
-
* cuts a long one at the Timeline's end (RFC 02 §3.3), so a window or speed of its own has no meaning.
|
|
356
|
-
*/
|
|
357
|
-
function assertFillsWithWholeSource(rows, marker, source) {
|
|
358
|
-
if (marker?.fillTimeline !== true) return;
|
|
359
|
-
const media = typeof source === "string" ? rows["media-entities"][source] : void 0;
|
|
360
|
-
const durationMs = typeof media?.durationMs === "number" ? media.durationMs : void 0;
|
|
361
|
-
if (marker.inMs !== 0 || marker.rate !== void 0 && marker.rate !== 1 || durationMs !== void 0 && marker.outMs !== durationMs) throw new Error(`A Clip that fills the Timeline plays its whole source at 1×: inMs 0, outMs ${durationMs ?? "its length"}, no rate; the Timeline's end cuts it`);
|
|
742
|
+
if (change.containment) assertClipPlacements(before, after, change.containment.before, change.containment.after);
|
|
362
743
|
}
|
|
363
744
|
//#endregion
|
|
364
745
|
//#region src/dsl/projection/types.ts
|
|
@@ -594,15 +975,11 @@ function requireSource(s, id, role) {
|
|
|
594
975
|
return media;
|
|
595
976
|
}
|
|
596
977
|
function assertMediaRole(media, role) {
|
|
597
|
-
if (!media
|
|
598
|
-
|
|
599
|
-
speech: ["audio", "voiceover-recording"],
|
|
600
|
-
bgm: ["audio"],
|
|
601
|
-
caption: []
|
|
602
|
-
}[role].includes(media.kind)) throw new Error(`Source is not allowed on ${role} Track`);
|
|
978
|
+
if (!media) throw new Error(`Missing source for ${role} Track`);
|
|
979
|
+
assertClipSourceRole(role, media.kind);
|
|
603
980
|
}
|
|
604
981
|
function validateWindow(window, duration) {
|
|
605
|
-
|
|
982
|
+
assertClipWindow(window, duration);
|
|
606
983
|
}
|
|
607
984
|
function trackOf(s, id) {
|
|
608
985
|
requireClip(s, id);
|
|
@@ -890,101 +1267,1639 @@ function replaceSourcePlan(s, clipId, input) {
|
|
|
890
1267
|
patch: {
|
|
891
1268
|
seqMarker: {
|
|
892
1269
|
inMs: input.inMs,
|
|
893
|
-
outMs: input.outMs
|
|
1270
|
+
outMs: input.outMs,
|
|
1271
|
+
...supportsClipSpeed(source.source) ? {} : { rate: void 0 }
|
|
894
1272
|
},
|
|
895
1273
|
source: source.id
|
|
896
1274
|
}
|
|
897
1275
|
}];
|
|
898
1276
|
}
|
|
899
|
-
const produce = new Immer({
|
|
900
|
-
autoFreeze: false,
|
|
901
|
-
useStrictShallowCopy: true
|
|
902
|
-
}).produce;
|
|
903
1277
|
//#endregion
|
|
904
|
-
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/
|
|
1278
|
+
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/schema/resolver.js
|
|
1279
|
+
function getMapFieldSchema(schema, key) {
|
|
1280
|
+
if (!schema) return void 0;
|
|
1281
|
+
if (Object.prototype.hasOwnProperty.call(schema.definition, key)) return schema.definition[key];
|
|
1282
|
+
if (schema.type === "loro-map" && "catchallType" in schema) return schema.catchallType;
|
|
1283
|
+
}
|
|
1284
|
+
function getChildSchema(schema, childKey) {
|
|
1285
|
+
if (!schema) return void 0;
|
|
1286
|
+
switch (schema.type) {
|
|
1287
|
+
case "schema":
|
|
1288
|
+
case "loro-map": return childKey === void 0 ? void 0 : getMapFieldSchema(schema, String(childKey));
|
|
1289
|
+
case "loro-list":
|
|
1290
|
+
case "loro-movable-list": return schema.itemSchema;
|
|
1291
|
+
case "loro-tree": return schema.nodeSchema;
|
|
1292
|
+
default: return;
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
function getChildContainerSchema(schema, childKey) {
|
|
1296
|
+
const childSchema = getChildSchema(schema, childKey);
|
|
1297
|
+
if (!childSchema) return void 0;
|
|
1298
|
+
switch (childSchema.type) {
|
|
1299
|
+
case "loro-map":
|
|
1300
|
+
case "loro-list":
|
|
1301
|
+
case "loro-movable-list":
|
|
1302
|
+
case "loro-text":
|
|
1303
|
+
case "loro-tree": return childSchema;
|
|
1304
|
+
default: return;
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
//#endregion
|
|
1308
|
+
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/constants.js
|
|
1309
|
+
const CID_KEY = "$cid";
|
|
1310
|
+
//#endregion
|
|
1311
|
+
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/core/container-inference.js
|
|
1312
|
+
function isPlainObjectValue(value) {
|
|
1313
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Date) && !(value instanceof RegExp) && typeof value !== "function";
|
|
1314
|
+
}
|
|
1315
|
+
function inferContainerTypeFromValue$1(value, defaults) {
|
|
1316
|
+
switch (getContainerValueKind(value)) {
|
|
1317
|
+
case "map": return "Map";
|
|
1318
|
+
case "array": return defaults?.defaultMovableList ? "MovableList" : "List";
|
|
1319
|
+
case "string": return defaults?.defaultLoroText ? "Text" : void 0;
|
|
1320
|
+
default: return;
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
function inferSchemaContainerTypeFromValue(value, defaults) {
|
|
1324
|
+
switch (inferContainerTypeFromValue$1(value, defaults)) {
|
|
1325
|
+
case "Map": return "loro-map";
|
|
1326
|
+
case "List": return "loro-list";
|
|
1327
|
+
case "MovableList": return "loro-movable-list";
|
|
1328
|
+
case "Text": return "loro-text";
|
|
1329
|
+
default: return;
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
function matchesContainerType(containerType, value) {
|
|
1333
|
+
const kind = getContainerValueKind(value);
|
|
1334
|
+
switch (containerType) {
|
|
1335
|
+
case "Map": return kind === "map";
|
|
1336
|
+
case "List":
|
|
1337
|
+
case "MovableList":
|
|
1338
|
+
case "Tree": return kind === "array";
|
|
1339
|
+
case "Text": return kind === "string";
|
|
1340
|
+
default: return false;
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
function getContainerValueKind(value) {
|
|
1344
|
+
if (isPlainObjectValue(value)) return "map";
|
|
1345
|
+
if (Array.isArray(value)) return "array";
|
|
1346
|
+
if (typeof value === "string") return "string";
|
|
1347
|
+
return "other";
|
|
1348
|
+
}
|
|
1349
|
+
//#endregion
|
|
1350
|
+
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/core/utils.js
|
|
905
1351
|
/**
|
|
906
|
-
*
|
|
1352
|
+
* Utility functions for Loro Mirror core
|
|
1353
|
+
*/
|
|
1354
|
+
/**
|
|
1355
|
+
* Check if a schema has a transform.
|
|
1356
|
+
* Distinguishes between the .transform() builder method (function) and an actual
|
|
1357
|
+
* TransformDefinition object (with decode/encode functions).
|
|
1358
|
+
*/
|
|
1359
|
+
function hasTransform(schema) {
|
|
1360
|
+
if (schema === void 0) return false;
|
|
1361
|
+
const t = schema.transform;
|
|
1362
|
+
return t != null && typeof t === "object";
|
|
1363
|
+
}
|
|
1364
|
+
/**
|
|
1365
|
+
* Get the transform from a schema, or undefined if none exists.
|
|
1366
|
+
*/
|
|
1367
|
+
function getTransform(schema) {
|
|
1368
|
+
if (hasTransform(schema)) return schema.transform;
|
|
1369
|
+
}
|
|
1370
|
+
/**
|
|
1371
|
+
* Get the effective equality strategy for a transform.
|
|
1372
|
+
* Default: "reference-equality"
|
|
1373
|
+
*/
|
|
1374
|
+
function getEqualityStrategy(schema, strategyIfNotTransformable) {
|
|
1375
|
+
const transform = getTransform(schema);
|
|
1376
|
+
if (!transform) return strategyIfNotTransformable;
|
|
1377
|
+
return transform.isEqual ?? "reference-equality";
|
|
1378
|
+
}
|
|
1379
|
+
/**
|
|
1380
|
+
* Apply decode transform to a CRDT value.
|
|
1381
|
+
* Null/undefined pass through as-is - transform is only called for real values.
|
|
1382
|
+
*/
|
|
1383
|
+
function applyDecode(schema, crdtValue) {
|
|
1384
|
+
if (crdtValue === null || crdtValue === void 0) return crdtValue;
|
|
1385
|
+
const transform = getTransform(schema);
|
|
1386
|
+
return transform ? transform.decode(crdtValue) : crdtValue;
|
|
1387
|
+
}
|
|
1388
|
+
/**
|
|
1389
|
+
* Recursively decode JSON values using schema transforms.
|
|
1390
|
+
* Mutates in-place. Used for snapshot initialization where toJSON() skips decode.
|
|
1391
|
+
*/
|
|
1392
|
+
function decodeNestedJsonValues(json, schema) {
|
|
1393
|
+
if (json === null || json === void 0 || !schema) return json;
|
|
1394
|
+
switch (schema.type) {
|
|
1395
|
+
case "loro-map":
|
|
1396
|
+
if (!isObject(json)) return json;
|
|
1397
|
+
for (const key of Object.keys(json)) {
|
|
1398
|
+
const fieldSchema = getChildSchema(schema, key);
|
|
1399
|
+
json[key] = decodeNestedJsonValues(json[key], fieldSchema);
|
|
1400
|
+
}
|
|
1401
|
+
return json;
|
|
1402
|
+
case "loro-list":
|
|
1403
|
+
case "loro-movable-list": {
|
|
1404
|
+
if (!Array.isArray(json)) return json;
|
|
1405
|
+
const itemSchema = getChildSchema(schema);
|
|
1406
|
+
for (let i = 0; i < json.length; i++) json[i] = decodeNestedJsonValues(json[i], itemSchema);
|
|
1407
|
+
return json;
|
|
1408
|
+
}
|
|
1409
|
+
case "loro-tree": {
|
|
1410
|
+
if (!Array.isArray(json)) return json;
|
|
1411
|
+
const nodeSchema = getChildSchema(schema);
|
|
1412
|
+
const walk = (nodes) => {
|
|
1413
|
+
for (const node of nodes) if (node != null && typeof node == "object") {
|
|
1414
|
+
if ("data" in node && node.data !== void 0) node.data = decodeNestedJsonValues(node.data, nodeSchema);
|
|
1415
|
+
if ("children" in node && Array.isArray(node.children)) walk(node.children);
|
|
1416
|
+
}
|
|
1417
|
+
};
|
|
1418
|
+
walk(json);
|
|
1419
|
+
return json;
|
|
1420
|
+
}
|
|
1421
|
+
case "loro-text":
|
|
1422
|
+
case "ignore": return json;
|
|
1423
|
+
default: return applyDecode(schema, json);
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Apply encode transform to a domain value.
|
|
1428
|
+
* Null/undefined pass through as-is - transform is only called for real values.
|
|
1429
|
+
*/
|
|
1430
|
+
function applyEncode(schema, domainValue) {
|
|
1431
|
+
if (domainValue === null || domainValue === void 0) return domainValue;
|
|
1432
|
+
const transform = getTransform(schema);
|
|
1433
|
+
return transform ? transform.encode(domainValue) : domainValue;
|
|
1434
|
+
}
|
|
1435
|
+
/**
|
|
1436
|
+
* Check if two domain values are equal according to the schema's equality strategy.
|
|
907
1437
|
*
|
|
908
|
-
*
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
1438
|
+
* Reference equality is always checked first (performance optimization).
|
|
1439
|
+
* If refs match, returns true (no change).
|
|
1440
|
+
* If refs differ, behavior depends on isEqual setting.
|
|
1441
|
+
*/
|
|
1442
|
+
function valuesEqual(schema, oldValue, newValue, strategyIfNotTransformable) {
|
|
1443
|
+
if (oldValue === newValue) return true;
|
|
1444
|
+
const strategy = getEqualityStrategy(schema, strategyIfNotTransformable);
|
|
1445
|
+
if (strategy === "reference-equality") return false;
|
|
1446
|
+
else if (strategy === "encoded-value-equality") return applyEncode(schema, oldValue) === applyEncode(schema, newValue);
|
|
1447
|
+
else if (strategy === "deep-equality") return deepEqual(oldValue, newValue);
|
|
1448
|
+
else return strategy(oldValue, newValue);
|
|
1449
|
+
}
|
|
1450
|
+
function defineCidProperty(target, cid) {
|
|
1451
|
+
if (!isObject(target) || Object.prototype.hasOwnProperty.call(target, "$cid")) return;
|
|
1452
|
+
Object.defineProperty(target, CID_KEY, { value: cid });
|
|
1453
|
+
}
|
|
1454
|
+
/**
|
|
1455
|
+
* Restore the read-only `$cid` contract on a freshly produced state.
|
|
914
1456
|
*
|
|
915
|
-
*
|
|
916
|
-
*
|
|
1457
|
+
* `defineCidProperty` stamps `$cid` as `{ writable: false, enumerable: false,
|
|
1458
|
+
* configurable: false }`. Immer's `useStrictShallowCopy` preserves the descriptor, but it
|
|
1459
|
+
* deliberately rewrites every non-writable data property to `{ writable: true,
|
|
1460
|
+
* configurable: true }` so the draft stays mutable — which would leave `$cid` writable on
|
|
1461
|
+
* every object Immer copied, letting callers reassign a published `$cid`.
|
|
1462
|
+
*
|
|
1463
|
+
* Two things keep this proportional to what Immer actually copied rather than to the size
|
|
1464
|
+
* of the state:
|
|
1465
|
+
*
|
|
1466
|
+
* - `prev` is the state `produce` was called with, and Immer shares everything it did not
|
|
1467
|
+
* copy, so a subtree reference-equal to `prev` cannot have been touched.
|
|
1468
|
+
* - An already-locked `$cid` means this object was not copied this round, so its children
|
|
1469
|
+
* are the same references they were when it was last hardened and are locked too. This
|
|
1470
|
+
* is what makes a reorder cheap: moved items are shared references that land at a new
|
|
1471
|
+
* index, so `prev` no longer lines up with them, but they are still locked.
|
|
917
1472
|
*/
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
1473
|
+
function hardenCidDescriptors(next, prev, visited) {
|
|
1474
|
+
if (next === prev) return;
|
|
1475
|
+
if (Array.isArray(next)) {
|
|
1476
|
+
if ((visited ?? (visited = /* @__PURE__ */ new Set())).has(next)) return;
|
|
1477
|
+
visited.add(next);
|
|
1478
|
+
const prevArr = Array.isArray(prev) ? prev : void 0;
|
|
1479
|
+
for (let i = 0; i < next.length; i++) hardenCidDescriptors(next[i], prevArr?.[i], visited);
|
|
1480
|
+
return;
|
|
922
1481
|
}
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
}, delay);
|
|
1482
|
+
if (!isObject(next)) return;
|
|
1483
|
+
const descriptor = Object.getOwnPropertyDescriptor(next, CID_KEY);
|
|
1484
|
+
if (descriptor) {
|
|
1485
|
+
if (!descriptor.configurable) return;
|
|
1486
|
+
if ("value" in descriptor) Object.defineProperty(next, CID_KEY, {
|
|
1487
|
+
value: descriptor.value,
|
|
1488
|
+
writable: false,
|
|
1489
|
+
enumerable: false,
|
|
1490
|
+
configurable: false
|
|
1491
|
+
});
|
|
934
1492
|
}
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
1493
|
+
if ((visited ?? (visited = /* @__PURE__ */ new Set())).has(next)) return;
|
|
1494
|
+
visited.add(next);
|
|
1495
|
+
const prevObj = isObject(prev) ? prev : void 0;
|
|
1496
|
+
for (const key of Object.keys(next)) hardenCidDescriptors(next[key], prevObj?.[key], visited);
|
|
1497
|
+
}
|
|
1498
|
+
/**
|
|
1499
|
+
* Check if a value is an object
|
|
1500
|
+
*/
|
|
1501
|
+
function isObject(value) {
|
|
1502
|
+
return isPlainObjectValue(value);
|
|
1503
|
+
}
|
|
1504
|
+
const UNSAFE_KEYS = new Set([
|
|
1505
|
+
"__proto__",
|
|
1506
|
+
"constructor",
|
|
1507
|
+
"prototype"
|
|
1508
|
+
]);
|
|
1509
|
+
/**
|
|
1510
|
+
* Recursively removes undefined values from an object.
|
|
1511
|
+
* This treats undefined values as non-existent fields.
|
|
1512
|
+
* Preserves non-enumerable properties like $cid.
|
|
1513
|
+
* Returns the original object if no undefined values are found.
|
|
1514
|
+
* Protects against prototype pollution by skipping unsafe keys.
|
|
1515
|
+
*/
|
|
1516
|
+
function stripUndefined(value) {
|
|
1517
|
+
return stripUndefinedInner(value, 0, void 0);
|
|
1518
|
+
}
|
|
1519
|
+
/**
|
|
1520
|
+
* Depth at which we start recording the recursion path to detect cycles.
|
|
1521
|
+
*
|
|
1522
|
+
* This runs over the whole state on every `setState`, and tracking every node costs more
|
|
1523
|
+
* than the stripping itself. Mirror state is a tree whose depth follows the schema, so a
|
|
1524
|
+
* real state never gets near this; only a cycle recurses without bound. Once the path is
|
|
1525
|
+
* being recorded, any cycle revisits one of its own nodes within a single lap, so starting
|
|
1526
|
+
* late still terminates.
|
|
1527
|
+
*/
|
|
1528
|
+
const CYCLE_GUARD_DEPTH = 64;
|
|
1529
|
+
/**
|
|
1530
|
+
* Placeholder for a key that must not reach the rebuilt object: an unsafe key, or one whose
|
|
1531
|
+
* value is `undefined` and is therefore treated as a non-existent field.
|
|
1532
|
+
*/
|
|
1533
|
+
const DROPPED = Symbol("dropped");
|
|
1534
|
+
function stripUndefinedInner(value, depth, path) {
|
|
1535
|
+
if (value === void 0) return value;
|
|
1536
|
+
if (Array.isArray(value)) {
|
|
1537
|
+
if (depth >= CYCLE_GUARD_DEPTH) {
|
|
1538
|
+
if ((path ?? (path = /* @__PURE__ */ new Set())).has(value)) return value;
|
|
1539
|
+
path.add(value);
|
|
940
1540
|
}
|
|
941
|
-
|
|
942
|
-
|
|
1541
|
+
let hasChanges = false;
|
|
1542
|
+
const result = value.map((item) => {
|
|
1543
|
+
const stripped = stripUndefinedInner(item, depth + 1, path);
|
|
1544
|
+
if (stripped !== item) hasChanges = true;
|
|
1545
|
+
return stripped;
|
|
1546
|
+
});
|
|
1547
|
+
path?.delete(value);
|
|
1548
|
+
return hasChanges ? result : value;
|
|
1549
|
+
}
|
|
1550
|
+
if (isObject(value)) {
|
|
1551
|
+
if (depth >= CYCLE_GUARD_DEPTH) {
|
|
1552
|
+
if ((path ?? (path = /* @__PURE__ */ new Set())).has(value)) return value;
|
|
1553
|
+
path.add(value);
|
|
1554
|
+
}
|
|
1555
|
+
const keys = Object.keys(value);
|
|
1556
|
+
const strippedValues = Array.from({ length: keys.length });
|
|
1557
|
+
let changed = false;
|
|
1558
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1559
|
+
const key = keys[i];
|
|
1560
|
+
if (UNSAFE_KEYS.has(key)) {
|
|
1561
|
+
strippedValues[i] = DROPPED;
|
|
1562
|
+
continue;
|
|
1563
|
+
}
|
|
1564
|
+
const val = value[key];
|
|
1565
|
+
if (val === void 0) {
|
|
1566
|
+
strippedValues[i] = DROPPED;
|
|
1567
|
+
changed = true;
|
|
1568
|
+
continue;
|
|
1569
|
+
}
|
|
1570
|
+
const stripped = stripUndefinedInner(val, depth + 1, path);
|
|
1571
|
+
strippedValues[i] = stripped;
|
|
1572
|
+
if (stripped !== val) changed = true;
|
|
1573
|
+
}
|
|
1574
|
+
path?.delete(value);
|
|
1575
|
+
if (!changed) return value;
|
|
1576
|
+
const result = Object.create(null);
|
|
1577
|
+
const allProps = Object.getOwnPropertyNames(value);
|
|
1578
|
+
for (const key of allProps) {
|
|
1579
|
+
if (UNSAFE_KEYS.has(key)) continue;
|
|
1580
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
1581
|
+
if (descriptor && !descriptor.enumerable) Object.defineProperty(result, key, descriptor);
|
|
1582
|
+
}
|
|
1583
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1584
|
+
const val = strippedValues[i];
|
|
1585
|
+
if (val === DROPPED) continue;
|
|
1586
|
+
Object.defineProperty(result, keys[i], {
|
|
1587
|
+
value: val,
|
|
1588
|
+
writable: true,
|
|
1589
|
+
enumerable: true,
|
|
1590
|
+
configurable: true
|
|
1591
|
+
});
|
|
1592
|
+
}
|
|
1593
|
+
return result;
|
|
943
1594
|
}
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
1595
|
+
return value;
|
|
1596
|
+
}
|
|
1597
|
+
/**
|
|
1598
|
+
* Performs a deep equality check between two values
|
|
1599
|
+
*/
|
|
1600
|
+
function deepEqual(a, b) {
|
|
1601
|
+
if (a === b) return true;
|
|
1602
|
+
if (a === null || b === null || typeof a !== "object" && typeof a !== "function" || typeof b !== "object" && typeof b !== "function") return false;
|
|
1603
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
1604
|
+
if (a.length !== b.length) return false;
|
|
1605
|
+
for (let i = 0; i < a.length; i++) if (!deepEqual(a[i], b[i])) return false;
|
|
1606
|
+
return true;
|
|
947
1607
|
}
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
cb?.();
|
|
1608
|
+
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
|
|
1609
|
+
if (a instanceof RegExp && b instanceof RegExp) return a.toString() === b.toString();
|
|
1610
|
+
if (!Array.isArray(a) && !Array.isArray(b)) {
|
|
1611
|
+
const keysA = Object.keys(a);
|
|
1612
|
+
const keysB = Object.keys(b);
|
|
1613
|
+
if (keysA.length !== keysB.length) return false;
|
|
1614
|
+
for (const key of keysA) {
|
|
1615
|
+
if (!Object.prototype.hasOwnProperty.call(b, key)) return false;
|
|
1616
|
+
if (!deepEqual(a[key], b[key])) return false;
|
|
958
1617
|
}
|
|
1618
|
+
return true;
|
|
959
1619
|
}
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/core/ephemeral.js
|
|
1620
|
+
return false;
|
|
1621
|
+
}
|
|
963
1622
|
/**
|
|
964
|
-
*
|
|
1623
|
+
* Recursively compare the `$cid` markers of two state trees.
|
|
1624
|
+
*
|
|
1625
|
+
* `deepEqual` only walks enumerable keys, so it cannot see `$cid` (which is stamped as a
|
|
1626
|
+
* non-enumerable descriptor). Used by `Mirror.checkStateConsistency` so that a tampered
|
|
1627
|
+
* or stale `$cid` is reported as a divergence instead of silently sitting in the state.
|
|
1628
|
+
* Only the markers are compared here; value equality is `deepEqual`'s job.
|
|
965
1629
|
*/
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
this.store = store;
|
|
974
|
-
this.syncAllPatchesFromStore();
|
|
1630
|
+
function cidsEqual(a, b, seen) {
|
|
1631
|
+
if (a === b) return true;
|
|
1632
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
1633
|
+
if (a.length !== b.length) return false;
|
|
1634
|
+
if (markSeen(a, b, seen ?? (seen = /* @__PURE__ */ new Map())) === "seen") return true;
|
|
1635
|
+
for (let i = 0; i < a.length; i++) if (!cidsEqual(a[i], b[i], seen)) return false;
|
|
1636
|
+
return true;
|
|
975
1637
|
}
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1638
|
+
if (!isObject(a) || !isObject(b)) return true;
|
|
1639
|
+
if (a["$cid"] !== b["$cid"]) return false;
|
|
1640
|
+
if (markSeen(a, b, seen ?? (seen = /* @__PURE__ */ new Map())) === "seen") return true;
|
|
1641
|
+
for (const key of Object.keys(a)) {
|
|
1642
|
+
if (!Object.prototype.hasOwnProperty.call(b, key)) continue;
|
|
1643
|
+
if (!cidsEqual(a[key], b[key], seen)) return false;
|
|
1644
|
+
}
|
|
1645
|
+
return true;
|
|
1646
|
+
}
|
|
1647
|
+
function markSeen(a, b, seen) {
|
|
1648
|
+
let partners = seen.get(a);
|
|
1649
|
+
if (!partners) {
|
|
1650
|
+
partners = /* @__PURE__ */ new Set();
|
|
1651
|
+
seen.set(a, partners);
|
|
1652
|
+
}
|
|
1653
|
+
if (partners.has(b)) return "seen";
|
|
1654
|
+
partners.add(b);
|
|
1655
|
+
return "new";
|
|
1656
|
+
}
|
|
1657
|
+
function containerIdToContainerType(containerId) {
|
|
1658
|
+
const parts = containerId.split(":");
|
|
1659
|
+
return parts[parts.length - 1];
|
|
1660
|
+
}
|
|
1661
|
+
function getRootContainerByType(doc, key, type) {
|
|
1662
|
+
if (type === "Text") return doc.getText(key);
|
|
1663
|
+
else if (type === "List") return doc.getList(key);
|
|
1664
|
+
else if (type === "MovableList") return doc.getMovableList(key);
|
|
1665
|
+
else if (type === "Map") return doc.getMap(key);
|
|
1666
|
+
else if (type === "Tree") return doc.getTree(key);
|
|
1667
|
+
else throw new Error();
|
|
1668
|
+
}
|
|
1669
|
+
function insertChildToMap(containerId, key, value, inferOptions) {
|
|
1670
|
+
const ct = tryInferContainerType(value, inferOptions);
|
|
1671
|
+
if (ct) return {
|
|
1672
|
+
container: containerId,
|
|
1673
|
+
key,
|
|
1674
|
+
value,
|
|
1675
|
+
kind: "insert-container",
|
|
1676
|
+
childContainerType: ct
|
|
1677
|
+
};
|
|
1678
|
+
return {
|
|
1679
|
+
container: containerId,
|
|
1680
|
+
key,
|
|
1681
|
+
value,
|
|
1682
|
+
kind: "insert"
|
|
1683
|
+
};
|
|
1684
|
+
}
|
|
1685
|
+
function tryUpdateToContainer(change, toUpdate, schema, inferOptions) {
|
|
1686
|
+
if (!toUpdate) return change;
|
|
1687
|
+
if (change.kind !== "insert" && change.kind !== "set") return change;
|
|
1688
|
+
const effectiveInferOptions = applySchemaToInferOptions(schema, inferOptions);
|
|
1689
|
+
const containerType = schema ? schemaToContainerType(schema) ?? tryInferContainerType(change.value, effectiveInferOptions) : tryInferContainerType(change.value, effectiveInferOptions);
|
|
1690
|
+
if (containerType == null || schema && hasTransform(schema)) {
|
|
1691
|
+
const encodedValue = applyEncode(schema, change.value);
|
|
1692
|
+
return encodedValue !== change.value ? {
|
|
1693
|
+
...change,
|
|
1694
|
+
value: encodedValue
|
|
1695
|
+
} : change;
|
|
1696
|
+
}
|
|
1697
|
+
if (change.kind === "insert") return {
|
|
1698
|
+
container: change.container,
|
|
1699
|
+
key: change.key,
|
|
1700
|
+
value: change.value,
|
|
1701
|
+
kind: "insert-container",
|
|
1702
|
+
childContainerType: containerType
|
|
1703
|
+
};
|
|
1704
|
+
if (change.kind === "set") return {
|
|
1705
|
+
container: change.container,
|
|
1706
|
+
key: change.key,
|
|
1707
|
+
value: change.value,
|
|
1708
|
+
kind: "set-container",
|
|
1709
|
+
childContainerType: containerType
|
|
1710
|
+
};
|
|
1711
|
+
return change;
|
|
1712
|
+
}
|
|
1713
|
+
function schemaToContainerType(schema) {
|
|
1714
|
+
const containerType = schema.getContainerType();
|
|
1715
|
+
return containerType === null ? void 0 : containerType;
|
|
1716
|
+
}
|
|
1717
|
+
function tryInferContainerType(value, defaults) {
|
|
1718
|
+
return inferContainerTypeFromValue$1(value, defaults);
|
|
1719
|
+
}
|
|
1720
|
+
function applySchemaToInferOptions(schema, base) {
|
|
1721
|
+
if (!schema || schema.type !== "any") return base;
|
|
1722
|
+
const next = { ...base };
|
|
1723
|
+
next.defaultLoroText = schema.options.defaultLoroText ?? false;
|
|
1724
|
+
if (schema.options.defaultMovableList !== void 0) next.defaultMovableList = schema.options.defaultMovableList;
|
|
1725
|
+
return next;
|
|
1726
|
+
}
|
|
1727
|
+
function isValueOfContainerType(containerType, value) {
|
|
1728
|
+
return matchesContainerType(containerType, value);
|
|
1729
|
+
}
|
|
1730
|
+
function inferContainerTypeFromValue(value, defaults) {
|
|
1731
|
+
return inferSchemaContainerTypeFromValue(value, defaults);
|
|
1732
|
+
}
|
|
1733
|
+
function isObjectLike(value) {
|
|
1734
|
+
return typeof value === "object";
|
|
1735
|
+
}
|
|
1736
|
+
function isArrayLike(value) {
|
|
1737
|
+
return Array.isArray(value);
|
|
1738
|
+
}
|
|
1739
|
+
function isStateAndSchemaOfType(values, stateGuard, schemaGuard) {
|
|
1740
|
+
return stateGuard(values.oldState) && stateGuard(values.newState) && (!values.schema || schemaGuard(values.schema));
|
|
1741
|
+
}
|
|
1742
|
+
function isTreeID(id) {
|
|
1743
|
+
if (!(typeof id === "string")) return false;
|
|
1744
|
+
return !!id.match(/[0-9]+@[0-9]+/);
|
|
1745
|
+
}
|
|
1746
|
+
/**
|
|
1747
|
+
* Stringify a value safely, handling non-JSON-serializable types.
|
|
1748
|
+
* Handles BigInt, Date, RegExp, functions, and custom objects.
|
|
1749
|
+
*/
|
|
1750
|
+
function safeStringify(value, indent = 2) {
|
|
1751
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
1752
|
+
function replacer(val) {
|
|
1753
|
+
if (val === null || val === void 0) return val;
|
|
1754
|
+
if (typeof val === "string" || typeof val === "number" || typeof val === "boolean") return val;
|
|
1755
|
+
if (typeof val === "bigint") return `[BigInt: ${val.toString()}]`;
|
|
1756
|
+
if (typeof val === "function") return `[Function: ${val.name || "anonymous"}]`;
|
|
1757
|
+
if (typeof val === "symbol") return `[Symbol: ${val.description || ""}]`;
|
|
1758
|
+
if (typeof val === "object") {
|
|
1759
|
+
if (seen.has(val)) return "[Circular]";
|
|
1760
|
+
seen.add(val);
|
|
1761
|
+
if (val instanceof Date) return `[Date: ${val.toISOString()}]`;
|
|
1762
|
+
if (val instanceof RegExp) return `[RegExp: ${val.toString()}]`;
|
|
1763
|
+
if (val instanceof Error) return `[Error: ${val.message}]`;
|
|
1764
|
+
if (Array.isArray(val)) return val.map(replacer);
|
|
1765
|
+
const result = {};
|
|
1766
|
+
for (const key of Object.keys(val)) result[key] = replacer(val[key]);
|
|
1767
|
+
return result;
|
|
1768
|
+
}
|
|
1769
|
+
return Object.prototype.toString.call(val);
|
|
1770
|
+
}
|
|
1771
|
+
return JSON.stringify(replacer(value), null, indent);
|
|
1772
|
+
}
|
|
1773
|
+
//#endregion
|
|
1774
|
+
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/schema/validators.js
|
|
1775
|
+
const schemaValidationCache = /* @__PURE__ */ new WeakMap();
|
|
1776
|
+
function isCacheableValue(value) {
|
|
1777
|
+
return typeof value === "object" && value !== null;
|
|
1778
|
+
}
|
|
1779
|
+
function isSchemaValidated(schema, value) {
|
|
1780
|
+
if (!isCacheableValue(value)) return false;
|
|
1781
|
+
return schemaValidationCache.get(value)?.has(schema) ?? false;
|
|
1782
|
+
}
|
|
1783
|
+
function markSchemaValidated(schema, value) {
|
|
1784
|
+
if (!isCacheableValue(value)) return;
|
|
1785
|
+
let cache = schemaValidationCache.get(value);
|
|
1786
|
+
if (!cache) {
|
|
1787
|
+
cache = /* @__PURE__ */ new WeakSet();
|
|
1788
|
+
schemaValidationCache.set(value, cache);
|
|
1789
|
+
}
|
|
1790
|
+
cache.add(schema);
|
|
1791
|
+
}
|
|
1792
|
+
/**
|
|
1793
|
+
* Type guard for LoroMapSchema
|
|
1794
|
+
*/
|
|
1795
|
+
function isLoroMapSchema(schema) {
|
|
1796
|
+
return !!schema && schema.type === "loro-map";
|
|
1797
|
+
}
|
|
1798
|
+
/**
|
|
1799
|
+
* Type guard for LoroListSchema
|
|
1800
|
+
*/
|
|
1801
|
+
function isLoroListSchema(schema) {
|
|
1802
|
+
return !!schema && schema.type === "loro-list";
|
|
1803
|
+
}
|
|
1804
|
+
function isListLikeSchema(schema) {
|
|
1805
|
+
return isLoroListSchema(schema) || isLoroMovableListSchema(schema);
|
|
1806
|
+
}
|
|
1807
|
+
function isLoroMovableListSchema(schema) {
|
|
1808
|
+
return !!schema && schema.type === "loro-movable-list";
|
|
1809
|
+
}
|
|
1810
|
+
/**
|
|
1811
|
+
* Type guard for RootSchemaType
|
|
1812
|
+
*/
|
|
1813
|
+
function isRootSchemaType(schema) {
|
|
1814
|
+
return !!schema && schema.type === "schema";
|
|
1815
|
+
}
|
|
1816
|
+
/**
|
|
1817
|
+
* Whether a schema subtree contains any `Ignore` field. Recursive schemas are
|
|
1818
|
+
* tolerated (each schema object is visited once).
|
|
1819
|
+
*/
|
|
1820
|
+
function schemaContainsIgnoreField(schema) {
|
|
1821
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1822
|
+
const walk = (s) => {
|
|
1823
|
+
if (!s || visited.has(s)) return false;
|
|
1824
|
+
visited.add(s);
|
|
1825
|
+
if (s.type === "ignore") return true;
|
|
1826
|
+
switch (s.type) {
|
|
1827
|
+
case "schema":
|
|
1828
|
+
case "loro-map": {
|
|
1829
|
+
const record = s;
|
|
1830
|
+
return Object.values(record.definition).some(walk) || walk(record.catchallType);
|
|
1831
|
+
}
|
|
1832
|
+
case "loro-list":
|
|
1833
|
+
case "loro-movable-list": return walk(s.itemSchema);
|
|
1834
|
+
case "loro-tree": return walk(s.nodeSchema);
|
|
1835
|
+
default: return false;
|
|
1836
|
+
}
|
|
1837
|
+
};
|
|
1838
|
+
return walk(schema);
|
|
1839
|
+
}
|
|
1840
|
+
/**
|
|
1841
|
+
* Type guard for LoroTreeSchema
|
|
1842
|
+
*/
|
|
1843
|
+
function isLoroTreeSchema(schema) {
|
|
1844
|
+
return !!schema && schema.type === "loro-tree";
|
|
1845
|
+
}
|
|
1846
|
+
/**
|
|
1847
|
+
* Check if a schema is for a Loro container
|
|
1848
|
+
*/
|
|
1849
|
+
function isContainerSchema(schema) {
|
|
1850
|
+
return !!schema && (schema.type === "loro-map" || schema.type === "loro-list" || schema.type === "loro-text" || schema.type === "loro-movable-list" || schema.type === "loro-tree");
|
|
1851
|
+
}
|
|
1852
|
+
/**
|
|
1853
|
+
* Validate a value against a schema
|
|
1854
|
+
*
|
|
1855
|
+
* When `options.ignoreUnknownProperties` is true, the root schema check does
|
|
1856
|
+
* not reject properties missing from the schema definition — typically
|
|
1857
|
+
* written by peers on a newer schema version. (Nested map schemas never
|
|
1858
|
+
* reject undeclared properties.)
|
|
1859
|
+
*/
|
|
1860
|
+
function validateSchema(schema, value, options) {
|
|
1861
|
+
const errors = [];
|
|
1862
|
+
const actualType = schema.type;
|
|
1863
|
+
if (schema.options.required && (value === void 0 || value === null)) {
|
|
1864
|
+
errors.push("Value is required");
|
|
1865
|
+
return {
|
|
1866
|
+
valid: false,
|
|
1867
|
+
errors
|
|
1868
|
+
};
|
|
1869
|
+
}
|
|
1870
|
+
if (value === void 0 || value === null) return { valid: true };
|
|
1871
|
+
const useCache = !options?.ignoreUnknownProperties;
|
|
1872
|
+
if (useCache && isSchemaValidated(schema, value)) return { valid: true };
|
|
1873
|
+
switch (actualType) {
|
|
1874
|
+
case "any":
|
|
1875
|
+
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean" && value !== null && value !== void 0 && !Array.isArray(value) && !isObject(value)) errors.push("Value must be JSON-like");
|
|
1876
|
+
break;
|
|
1877
|
+
case "string":
|
|
1878
|
+
case "number":
|
|
1879
|
+
case "boolean":
|
|
1880
|
+
validateTransformablePrimitive(schema, value, actualType, errors);
|
|
1881
|
+
break;
|
|
1882
|
+
case "ignore": break;
|
|
1883
|
+
case "loro-text":
|
|
1884
|
+
if (typeof value !== "string") errors.push("Content must be a string");
|
|
1885
|
+
break;
|
|
1886
|
+
case "loro-map":
|
|
1887
|
+
if (!isObject(value)) errors.push("Value must be an object");
|
|
1888
|
+
else if (isLoroMapSchema(schema)) {
|
|
1889
|
+
for (const key in schema.definition) if (Object.prototype.hasOwnProperty.call(schema.definition, key)) {
|
|
1890
|
+
const propSchema = schema.definition[key];
|
|
1891
|
+
const propValue = value[key];
|
|
1892
|
+
const result = validateSchema(propSchema, propValue);
|
|
1893
|
+
if (!result.valid && result.errors) {
|
|
1894
|
+
const prefixedErrors = result.errors.map((err) => `${key}: ${err}`);
|
|
1895
|
+
errors.push(...prefixedErrors);
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
break;
|
|
1900
|
+
case "loro-movable-list":
|
|
1901
|
+
case "loro-list":
|
|
1902
|
+
if (!Array.isArray(value)) errors.push("Value must be an array");
|
|
1903
|
+
else if (isLoroListSchema(schema) || isLoroMovableListSchema(schema)) {
|
|
1904
|
+
const itemSchema = schema.itemSchema;
|
|
1905
|
+
value.forEach((item, index) => {
|
|
1906
|
+
const result = validateSchema(itemSchema, item);
|
|
1907
|
+
if (!result.valid && result.errors) {
|
|
1908
|
+
const prefixedErrors = result.errors.map((err) => `Item ${index}: ${err}`);
|
|
1909
|
+
errors.push(...prefixedErrors);
|
|
1910
|
+
}
|
|
1911
|
+
});
|
|
1912
|
+
}
|
|
1913
|
+
break;
|
|
1914
|
+
case "loro-tree": {
|
|
1915
|
+
if (!Array.isArray(value)) {
|
|
1916
|
+
errors.push("Value must be an array of tree nodes");
|
|
1917
|
+
break;
|
|
1918
|
+
}
|
|
1919
|
+
if (!isLoroTreeSchema(schema)) {
|
|
1920
|
+
errors.push("Invalid tree schema");
|
|
1921
|
+
break;
|
|
1922
|
+
}
|
|
1923
|
+
const validateNode = (node, path) => {
|
|
1924
|
+
if (!isObject(node)) {
|
|
1925
|
+
errors.push(`${path}: Node must be an object`);
|
|
1926
|
+
return;
|
|
1927
|
+
}
|
|
1928
|
+
const n = node;
|
|
1929
|
+
if (n.id != null && typeof n.id !== "string") errors.push(`${path}: id must be a string if provided`);
|
|
1930
|
+
const dataResult = validateSchema(schema.nodeSchema, n.data);
|
|
1931
|
+
if (!dataResult.valid && dataResult.errors) errors.push(...dataResult.errors.map((e) => `${path}.data: ${e}`));
|
|
1932
|
+
if (!Array.isArray(n.children)) errors.push(`${path}: children must be an array`);
|
|
1933
|
+
else n.children.forEach((child, idx) => {
|
|
1934
|
+
validateNode(child, `${path}.children[${idx}]`);
|
|
1935
|
+
});
|
|
1936
|
+
};
|
|
1937
|
+
value.forEach((node, i) => {
|
|
1938
|
+
validateNode(node, `node[${i}]`);
|
|
1939
|
+
});
|
|
1940
|
+
break;
|
|
1941
|
+
}
|
|
1942
|
+
case "schema":
|
|
1943
|
+
if (!isObject(value)) errors.push("Value must be an object");
|
|
1944
|
+
else if (isRootSchemaType(schema)) if (!isObject(value)) errors.push("Value must be an object");
|
|
1945
|
+
else {
|
|
1946
|
+
for (const key in schema.definition) if (Object.prototype.hasOwnProperty.call(schema.definition, key)) {
|
|
1947
|
+
const propSchema = schema.definition[key];
|
|
1948
|
+
const propValue = value[key];
|
|
1949
|
+
const result = validateSchema(propSchema, propValue);
|
|
1950
|
+
if (!result.valid && result.errors) {
|
|
1951
|
+
const prefixedErrors = result.errors.map((err) => `${key}: ${err}`);
|
|
1952
|
+
errors.push(...prefixedErrors);
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
for (const key in value) if (!Object.prototype.hasOwnProperty.call(schema.definition, key)) {
|
|
1956
|
+
if (options?.ignoreUnknownProperties) continue;
|
|
1957
|
+
errors.push(`Unknown property: ${key}`);
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
else errors.push(`Should be a schema, but got ${actualType}`);
|
|
1961
|
+
break;
|
|
1962
|
+
default: errors.push(`Unknown schema type: ${actualType}`);
|
|
1963
|
+
}
|
|
1964
|
+
if (schema.options.validate && typeof schema.options.validate === "function") try {
|
|
1965
|
+
const customValidation = schema.options.validate(value);
|
|
1966
|
+
if (customValidation !== true) {
|
|
1967
|
+
const errorMessage = typeof customValidation === "string" ? customValidation : "Value failed custom validation";
|
|
1968
|
+
errors.push(errorMessage);
|
|
1969
|
+
}
|
|
1970
|
+
} catch (error) {
|
|
1971
|
+
errors.push(`Validation error: ${String(error)}`);
|
|
1972
|
+
}
|
|
1973
|
+
if (errors.length === 0) {
|
|
1974
|
+
if (useCache) markSchemaValidated(schema, value);
|
|
1975
|
+
return { valid: true };
|
|
1976
|
+
}
|
|
1977
|
+
return {
|
|
1978
|
+
valid: false,
|
|
1979
|
+
errors
|
|
1980
|
+
};
|
|
1981
|
+
}
|
|
1982
|
+
function validateTransformablePrimitive(schema, value, expectedType, errors) {
|
|
1983
|
+
const transform = getTransform(schema);
|
|
1984
|
+
if (transform) {
|
|
1985
|
+
if (transform.validate) try {
|
|
1986
|
+
const result = transform.validate(value);
|
|
1987
|
+
if (result !== true) errors.push(typeof result === "string" ? result : "Transform validation failed");
|
|
1988
|
+
} catch (error) {
|
|
1989
|
+
errors.push(`Transform validation error: ${String(error)}`);
|
|
1990
|
+
}
|
|
1991
|
+
if (transform.validateEncodedType) try {
|
|
1992
|
+
const encoded = applyEncode(schema, value);
|
|
1993
|
+
if (typeof encoded !== expectedType) errors.push(`Transform encode must return a ${expectedType}, got ${typeof encoded}`);
|
|
1994
|
+
} catch (error) {
|
|
1995
|
+
errors.push(`Transform encode validation error: ${String(error)}`);
|
|
1996
|
+
}
|
|
1997
|
+
} else if (typeof value !== expectedType) errors.push(`Value must be a ${expectedType}`);
|
|
1998
|
+
}
|
|
1999
|
+
/**
|
|
2000
|
+
* Get default value for a schema
|
|
2001
|
+
* Based on the schema type, it might return a plain value or a wrapped value
|
|
2002
|
+
*/
|
|
2003
|
+
function getDefaultValue(schema) {
|
|
2004
|
+
if ("defaultValue" in schema.options) return schema.options.defaultValue;
|
|
2005
|
+
switch (schema.type) {
|
|
2006
|
+
case "any": return;
|
|
2007
|
+
case "string":
|
|
2008
|
+
if (schema.options.required === false) return;
|
|
2009
|
+
if (getTransform(schema)) return;
|
|
2010
|
+
return "";
|
|
2011
|
+
case "number":
|
|
2012
|
+
if (schema.options.required === false) return;
|
|
2013
|
+
if (getTransform(schema)) return;
|
|
2014
|
+
return 0;
|
|
2015
|
+
case "boolean":
|
|
2016
|
+
if (schema.options.required === false) return;
|
|
2017
|
+
if (getTransform(schema)) return;
|
|
2018
|
+
return false;
|
|
2019
|
+
case "loro-text": {
|
|
2020
|
+
const value = schema.options.required ? "" : void 0;
|
|
2021
|
+
if (value === void 0) return void 0;
|
|
2022
|
+
return value;
|
|
2023
|
+
}
|
|
2024
|
+
case "loro-map":
|
|
2025
|
+
if (isLoroMapSchema(schema)) {
|
|
2026
|
+
const result = {};
|
|
2027
|
+
for (const key in schema.definition) if (Object.prototype.hasOwnProperty.call(schema.definition, key)) {
|
|
2028
|
+
const value = getDefaultValue(schema.definition[key]);
|
|
2029
|
+
if (value !== void 0) result[key] = value;
|
|
2030
|
+
}
|
|
2031
|
+
return result;
|
|
2032
|
+
}
|
|
2033
|
+
return {};
|
|
2034
|
+
case "loro-list": return [];
|
|
2035
|
+
case "loro-tree": {
|
|
2036
|
+
const value = schema.options.required ? [] : void 0;
|
|
2037
|
+
if (value === void 0) return void 0;
|
|
2038
|
+
return value;
|
|
2039
|
+
}
|
|
2040
|
+
case "schema":
|
|
2041
|
+
if (isRootSchemaType(schema)) {
|
|
2042
|
+
const result = {};
|
|
2043
|
+
for (const key in schema.definition) if (Object.prototype.hasOwnProperty.call(schema.definition, key)) {
|
|
2044
|
+
const value = getDefaultValue(schema.definition[key]);
|
|
2045
|
+
if (value !== void 0) result[key] = value;
|
|
2046
|
+
}
|
|
2047
|
+
return result;
|
|
2048
|
+
}
|
|
2049
|
+
return {};
|
|
2050
|
+
default: return;
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
//#endregion
|
|
2054
|
+
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/schema/index.js
|
|
2055
|
+
/**
|
|
2056
|
+
* Create a schema definition
|
|
2057
|
+
*/
|
|
2058
|
+
function schema(definition, options) {
|
|
2059
|
+
return {
|
|
2060
|
+
type: "schema",
|
|
2061
|
+
definition,
|
|
2062
|
+
options: options || {},
|
|
2063
|
+
getContainerType() {
|
|
2064
|
+
return "Map";
|
|
2065
|
+
}
|
|
2066
|
+
};
|
|
2067
|
+
}
|
|
2068
|
+
/**
|
|
2069
|
+
* Define a string field
|
|
2070
|
+
*/
|
|
2071
|
+
schema.String = (function(options) {
|
|
2072
|
+
const baseSchema = {
|
|
2073
|
+
type: "string",
|
|
2074
|
+
options: options || {},
|
|
2075
|
+
getContainerType: () => {
|
|
2076
|
+
return null;
|
|
2077
|
+
}
|
|
2078
|
+
};
|
|
2079
|
+
return {
|
|
2080
|
+
...baseSchema,
|
|
2081
|
+
transform: (def) => ({
|
|
2082
|
+
...baseSchema,
|
|
2083
|
+
transform: def
|
|
2084
|
+
})
|
|
2085
|
+
};
|
|
2086
|
+
});
|
|
2087
|
+
/**
|
|
2088
|
+
* Define an any field (runtime-inferred by Mirror)
|
|
2089
|
+
*/
|
|
2090
|
+
schema.Any = function(options) {
|
|
2091
|
+
return {
|
|
2092
|
+
type: "any",
|
|
2093
|
+
options: options || {},
|
|
2094
|
+
getContainerType: () => {
|
|
2095
|
+
return null;
|
|
2096
|
+
}
|
|
2097
|
+
};
|
|
2098
|
+
};
|
|
2099
|
+
/**
|
|
2100
|
+
* Define a number field
|
|
2101
|
+
*/
|
|
2102
|
+
schema.Number = function(options) {
|
|
2103
|
+
const baseSchema = {
|
|
2104
|
+
type: "number",
|
|
2105
|
+
options: options || {},
|
|
2106
|
+
getContainerType: () => {
|
|
2107
|
+
return null;
|
|
2108
|
+
}
|
|
2109
|
+
};
|
|
2110
|
+
return {
|
|
2111
|
+
...baseSchema,
|
|
2112
|
+
transform: (def) => ({
|
|
2113
|
+
...baseSchema,
|
|
2114
|
+
transform: def
|
|
2115
|
+
})
|
|
2116
|
+
};
|
|
2117
|
+
};
|
|
2118
|
+
/**
|
|
2119
|
+
* Define a boolean field
|
|
2120
|
+
*/
|
|
2121
|
+
schema.Boolean = function(options) {
|
|
2122
|
+
const baseSchema = {
|
|
2123
|
+
type: "boolean",
|
|
2124
|
+
options: options || {},
|
|
2125
|
+
getContainerType: () => {
|
|
2126
|
+
return null;
|
|
2127
|
+
}
|
|
2128
|
+
};
|
|
2129
|
+
return {
|
|
2130
|
+
...baseSchema,
|
|
2131
|
+
transform: (def) => ({
|
|
2132
|
+
...baseSchema,
|
|
2133
|
+
transform: def
|
|
2134
|
+
})
|
|
2135
|
+
};
|
|
2136
|
+
};
|
|
2137
|
+
/**
|
|
2138
|
+
* Define a field to be ignored (not synced with Loro)
|
|
2139
|
+
*/
|
|
2140
|
+
schema.Ignore = function(options) {
|
|
2141
|
+
return {
|
|
2142
|
+
type: "ignore",
|
|
2143
|
+
options: options || {},
|
|
2144
|
+
getContainerType: () => {
|
|
2145
|
+
return null;
|
|
2146
|
+
}
|
|
2147
|
+
};
|
|
2148
|
+
};
|
|
2149
|
+
/**
|
|
2150
|
+
* Define a Loro map
|
|
2151
|
+
*/
|
|
2152
|
+
schema.LoroMap = function(definition, options) {
|
|
2153
|
+
const baseSchema = {
|
|
2154
|
+
type: "loro-map",
|
|
2155
|
+
definition,
|
|
2156
|
+
options: options || {},
|
|
2157
|
+
getContainerType: () => {
|
|
2158
|
+
return "Map";
|
|
2159
|
+
}
|
|
2160
|
+
};
|
|
2161
|
+
const schemaWithCatchall = {
|
|
2162
|
+
...baseSchema,
|
|
2163
|
+
catchall: (catchallSchema) => {
|
|
2164
|
+
return {
|
|
2165
|
+
...baseSchema,
|
|
2166
|
+
catchallType: catchallSchema,
|
|
2167
|
+
catchall: (newCatchallSchema) => {
|
|
2168
|
+
return {
|
|
2169
|
+
...baseSchema,
|
|
2170
|
+
catchallType: newCatchallSchema,
|
|
2171
|
+
catchall: schemaWithCatchall.catchall
|
|
2172
|
+
};
|
|
2173
|
+
}
|
|
2174
|
+
};
|
|
2175
|
+
}
|
|
2176
|
+
};
|
|
2177
|
+
return schemaWithCatchall;
|
|
2178
|
+
};
|
|
2179
|
+
/**
|
|
2180
|
+
* Create a dynamic record schema (like zod's z.record)
|
|
2181
|
+
*/
|
|
2182
|
+
schema.LoroMapRecord = function(valueSchema, options) {
|
|
2183
|
+
return {
|
|
2184
|
+
type: "loro-map",
|
|
2185
|
+
definition: {},
|
|
2186
|
+
catchallType: valueSchema,
|
|
2187
|
+
options: options || {},
|
|
2188
|
+
getContainerType: () => {
|
|
2189
|
+
return "Map";
|
|
2190
|
+
},
|
|
2191
|
+
catchall: (newCatchallSchema) => {
|
|
2192
|
+
return schema.LoroMapRecord(newCatchallSchema, options);
|
|
2193
|
+
}
|
|
2194
|
+
};
|
|
2195
|
+
};
|
|
2196
|
+
/**
|
|
2197
|
+
* Define a Loro list
|
|
2198
|
+
*/
|
|
2199
|
+
schema.LoroList = function(itemSchema, idSelector, options) {
|
|
2200
|
+
return {
|
|
2201
|
+
type: "loro-list",
|
|
2202
|
+
itemSchema,
|
|
2203
|
+
idSelector,
|
|
2204
|
+
options: options || {},
|
|
2205
|
+
getContainerType: () => {
|
|
2206
|
+
return "List";
|
|
2207
|
+
}
|
|
2208
|
+
};
|
|
2209
|
+
};
|
|
2210
|
+
schema.LoroMovableList = function(itemSchema, idSelector, options) {
|
|
2211
|
+
return {
|
|
2212
|
+
type: "loro-movable-list",
|
|
2213
|
+
itemSchema,
|
|
2214
|
+
idSelector,
|
|
2215
|
+
options: options || {},
|
|
2216
|
+
getContainerType: () => {
|
|
2217
|
+
return "MovableList";
|
|
2218
|
+
}
|
|
2219
|
+
};
|
|
2220
|
+
};
|
|
2221
|
+
/**
|
|
2222
|
+
* Define a Loro text field
|
|
2223
|
+
*/
|
|
2224
|
+
schema.LoroText = function(options) {
|
|
2225
|
+
return {
|
|
2226
|
+
type: "loro-text",
|
|
2227
|
+
options: options || {},
|
|
2228
|
+
getContainerType: () => {
|
|
2229
|
+
return "Text";
|
|
2230
|
+
}
|
|
2231
|
+
};
|
|
2232
|
+
};
|
|
2233
|
+
/**
|
|
2234
|
+
* Define a Loro tree
|
|
2235
|
+
*
|
|
2236
|
+
* Each tree node has a `data` map described by `nodeSchema`.
|
|
2237
|
+
*/
|
|
2238
|
+
schema.LoroTree = function(nodeSchema, options) {
|
|
2239
|
+
return {
|
|
2240
|
+
type: "loro-tree",
|
|
2241
|
+
nodeSchema,
|
|
2242
|
+
options: options || {},
|
|
2243
|
+
getContainerType() {
|
|
2244
|
+
return "Tree";
|
|
2245
|
+
}
|
|
2246
|
+
};
|
|
2247
|
+
};
|
|
2248
|
+
//#endregion
|
|
2249
|
+
//#region ../../node_modules/.pnpm/immer@10.2.0/node_modules/immer/dist/immer.mjs
|
|
2250
|
+
var NOTHING = Symbol.for("immer-nothing");
|
|
2251
|
+
var DRAFTABLE = Symbol.for("immer-draftable");
|
|
2252
|
+
var DRAFT_STATE = Symbol.for("immer-state");
|
|
2253
|
+
var errors = [
|
|
2254
|
+
function(plugin) {
|
|
2255
|
+
return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`;
|
|
2256
|
+
},
|
|
2257
|
+
function(thing) {
|
|
2258
|
+
return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${thing}'`;
|
|
2259
|
+
},
|
|
2260
|
+
"This object has been frozen and should not be mutated",
|
|
2261
|
+
function(data) {
|
|
2262
|
+
return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + data;
|
|
2263
|
+
},
|
|
2264
|
+
"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
|
|
2265
|
+
"Immer forbids circular references",
|
|
2266
|
+
"The first or second argument to `produce` must be a function",
|
|
2267
|
+
"The third argument to `produce` must be a function or undefined",
|
|
2268
|
+
"First argument to `createDraft` must be a plain object, an array, or an immerable object",
|
|
2269
|
+
"First argument to `finishDraft` must be a draft returned by `createDraft`",
|
|
2270
|
+
function(thing) {
|
|
2271
|
+
return `'current' expects a draft, got: ${thing}`;
|
|
2272
|
+
},
|
|
2273
|
+
"Object.defineProperty() cannot be used on an Immer draft",
|
|
2274
|
+
"Object.setPrototypeOf() cannot be used on an Immer draft",
|
|
2275
|
+
"Immer only supports deleting array indices",
|
|
2276
|
+
"Immer only supports setting array indices and the 'length' property",
|
|
2277
|
+
function(thing) {
|
|
2278
|
+
return `'original' expects a draft, got: ${thing}`;
|
|
2279
|
+
}
|
|
2280
|
+
];
|
|
2281
|
+
function die(error, ...args) {
|
|
2282
|
+
{
|
|
2283
|
+
const e = errors[error];
|
|
2284
|
+
const msg = typeof e === "function" ? e.apply(null, args) : e;
|
|
2285
|
+
throw new Error(`[Immer] ${msg}`);
|
|
2286
|
+
}
|
|
2287
|
+
throw new Error(`[Immer] minified error nr: ${error}. Full error at: https://bit.ly/3cXEKWf`);
|
|
2288
|
+
}
|
|
2289
|
+
var getPrototypeOf = Object.getPrototypeOf;
|
|
2290
|
+
function isDraft(value) {
|
|
2291
|
+
return !!value && !!value[DRAFT_STATE];
|
|
2292
|
+
}
|
|
2293
|
+
function isDraftable(value) {
|
|
2294
|
+
if (!value) return false;
|
|
2295
|
+
return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!value.constructor?.[DRAFTABLE] || isMap(value) || isSet(value);
|
|
2296
|
+
}
|
|
2297
|
+
var objectCtorString = Object.prototype.constructor.toString();
|
|
2298
|
+
var cachedCtorStrings = /* @__PURE__ */ new WeakMap();
|
|
2299
|
+
function isPlainObject(value) {
|
|
2300
|
+
if (!value || typeof value !== "object") return false;
|
|
2301
|
+
const proto = Object.getPrototypeOf(value);
|
|
2302
|
+
if (proto === null || proto === Object.prototype) return true;
|
|
2303
|
+
const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
|
|
2304
|
+
if (Ctor === Object) return true;
|
|
2305
|
+
if (typeof Ctor !== "function") return false;
|
|
2306
|
+
let ctorString = cachedCtorStrings.get(Ctor);
|
|
2307
|
+
if (ctorString === void 0) {
|
|
2308
|
+
ctorString = Function.toString.call(Ctor);
|
|
2309
|
+
cachedCtorStrings.set(Ctor, ctorString);
|
|
2310
|
+
}
|
|
2311
|
+
return ctorString === objectCtorString;
|
|
2312
|
+
}
|
|
2313
|
+
function each(obj, iter, strict = true) {
|
|
2314
|
+
if (getArchtype(obj) === 0) (strict ? Reflect.ownKeys(obj) : Object.keys(obj)).forEach((key) => {
|
|
2315
|
+
iter(key, obj[key], obj);
|
|
2316
|
+
});
|
|
2317
|
+
else obj.forEach((entry, index) => iter(index, entry, obj));
|
|
2318
|
+
}
|
|
2319
|
+
function getArchtype(thing) {
|
|
2320
|
+
const state = thing[DRAFT_STATE];
|
|
2321
|
+
return state ? state.type_ : Array.isArray(thing) ? 1 : isMap(thing) ? 2 : isSet(thing) ? 3 : 0;
|
|
2322
|
+
}
|
|
2323
|
+
function has(thing, prop) {
|
|
2324
|
+
return getArchtype(thing) === 2 ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
|
|
2325
|
+
}
|
|
2326
|
+
function set(thing, propOrOldValue, value) {
|
|
2327
|
+
const t = getArchtype(thing);
|
|
2328
|
+
if (t === 2) thing.set(propOrOldValue, value);
|
|
2329
|
+
else if (t === 3) thing.add(value);
|
|
2330
|
+
else thing[propOrOldValue] = value;
|
|
2331
|
+
}
|
|
2332
|
+
function is(x, y) {
|
|
2333
|
+
if (x === y) return x !== 0 || 1 / x === 1 / y;
|
|
2334
|
+
else return x !== x && y !== y;
|
|
2335
|
+
}
|
|
2336
|
+
function isMap(target) {
|
|
2337
|
+
return target instanceof Map;
|
|
2338
|
+
}
|
|
2339
|
+
function isSet(target) {
|
|
2340
|
+
return target instanceof Set;
|
|
2341
|
+
}
|
|
2342
|
+
function latest(state) {
|
|
2343
|
+
return state.copy_ || state.base_;
|
|
2344
|
+
}
|
|
2345
|
+
function shallowCopy(base, strict) {
|
|
2346
|
+
if (isMap(base)) return new Map(base);
|
|
2347
|
+
if (isSet(base)) return new Set(base);
|
|
2348
|
+
if (Array.isArray(base)) return Array.prototype.slice.call(base);
|
|
2349
|
+
const isPlain = isPlainObject(base);
|
|
2350
|
+
if (strict === true || strict === "class_only" && !isPlain) {
|
|
2351
|
+
const descriptors = Object.getOwnPropertyDescriptors(base);
|
|
2352
|
+
delete descriptors[DRAFT_STATE];
|
|
2353
|
+
let keys = Reflect.ownKeys(descriptors);
|
|
2354
|
+
for (let i = 0; i < keys.length; i++) {
|
|
2355
|
+
const key = keys[i];
|
|
2356
|
+
const desc = descriptors[key];
|
|
2357
|
+
if (desc.writable === false) {
|
|
2358
|
+
desc.writable = true;
|
|
2359
|
+
desc.configurable = true;
|
|
2360
|
+
}
|
|
2361
|
+
if (desc.get || desc.set) descriptors[key] = {
|
|
2362
|
+
configurable: true,
|
|
2363
|
+
writable: true,
|
|
2364
|
+
enumerable: desc.enumerable,
|
|
2365
|
+
value: base[key]
|
|
2366
|
+
};
|
|
2367
|
+
}
|
|
2368
|
+
return Object.create(getPrototypeOf(base), descriptors);
|
|
2369
|
+
} else {
|
|
2370
|
+
const proto = getPrototypeOf(base);
|
|
2371
|
+
if (proto !== null && isPlain) return { ...base };
|
|
2372
|
+
return Object.assign(Object.create(proto), base);
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
function freeze(obj, deep = false) {
|
|
2376
|
+
if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj;
|
|
2377
|
+
if (getArchtype(obj) > 1) Object.defineProperties(obj, {
|
|
2378
|
+
set: dontMutateMethodOverride,
|
|
2379
|
+
add: dontMutateMethodOverride,
|
|
2380
|
+
clear: dontMutateMethodOverride,
|
|
2381
|
+
delete: dontMutateMethodOverride
|
|
2382
|
+
});
|
|
2383
|
+
Object.freeze(obj);
|
|
2384
|
+
if (deep) Object.values(obj).forEach((value) => freeze(value, true));
|
|
2385
|
+
return obj;
|
|
2386
|
+
}
|
|
2387
|
+
function dontMutateFrozenCollections() {
|
|
2388
|
+
die(2);
|
|
2389
|
+
}
|
|
2390
|
+
var dontMutateMethodOverride = { value: dontMutateFrozenCollections };
|
|
2391
|
+
function isFrozen(obj) {
|
|
2392
|
+
if (obj === null || typeof obj !== "object") return true;
|
|
2393
|
+
return Object.isFrozen(obj);
|
|
2394
|
+
}
|
|
2395
|
+
var plugins = {};
|
|
2396
|
+
function getPlugin(pluginKey) {
|
|
2397
|
+
const plugin = plugins[pluginKey];
|
|
2398
|
+
if (!plugin) die(0, pluginKey);
|
|
2399
|
+
return plugin;
|
|
2400
|
+
}
|
|
2401
|
+
var currentScope;
|
|
2402
|
+
function getCurrentScope() {
|
|
2403
|
+
return currentScope;
|
|
2404
|
+
}
|
|
2405
|
+
function createScope(parent_, immer_) {
|
|
2406
|
+
return {
|
|
2407
|
+
drafts_: [],
|
|
2408
|
+
parent_,
|
|
2409
|
+
immer_,
|
|
2410
|
+
canAutoFreeze_: true,
|
|
2411
|
+
unfinalizedDrafts_: 0
|
|
2412
|
+
};
|
|
2413
|
+
}
|
|
2414
|
+
function usePatchesInScope(scope, patchListener) {
|
|
2415
|
+
if (patchListener) {
|
|
2416
|
+
getPlugin("Patches");
|
|
2417
|
+
scope.patches_ = [];
|
|
2418
|
+
scope.inversePatches_ = [];
|
|
2419
|
+
scope.patchListener_ = patchListener;
|
|
2420
|
+
}
|
|
2421
|
+
}
|
|
2422
|
+
function revokeScope(scope) {
|
|
2423
|
+
leaveScope(scope);
|
|
2424
|
+
scope.drafts_.forEach(revokeDraft);
|
|
2425
|
+
scope.drafts_ = null;
|
|
2426
|
+
}
|
|
2427
|
+
function leaveScope(scope) {
|
|
2428
|
+
if (scope === currentScope) currentScope = scope.parent_;
|
|
2429
|
+
}
|
|
2430
|
+
function enterScope(immer2) {
|
|
2431
|
+
return currentScope = createScope(currentScope, immer2);
|
|
2432
|
+
}
|
|
2433
|
+
function revokeDraft(draft) {
|
|
2434
|
+
const state = draft[DRAFT_STATE];
|
|
2435
|
+
if (state.type_ === 0 || state.type_ === 1) state.revoke_();
|
|
2436
|
+
else state.revoked_ = true;
|
|
2437
|
+
}
|
|
2438
|
+
function processResult(result, scope) {
|
|
2439
|
+
scope.unfinalizedDrafts_ = scope.drafts_.length;
|
|
2440
|
+
const baseDraft = scope.drafts_[0];
|
|
2441
|
+
if (result !== void 0 && result !== baseDraft) {
|
|
2442
|
+
if (baseDraft[DRAFT_STATE].modified_) {
|
|
2443
|
+
revokeScope(scope);
|
|
2444
|
+
die(4);
|
|
2445
|
+
}
|
|
2446
|
+
if (isDraftable(result)) {
|
|
2447
|
+
result = finalize(scope, result);
|
|
2448
|
+
if (!scope.parent_) maybeFreeze(scope, result);
|
|
2449
|
+
}
|
|
2450
|
+
if (scope.patches_) getPlugin("Patches").generateReplacementPatches_(baseDraft[DRAFT_STATE].base_, result, scope.patches_, scope.inversePatches_);
|
|
2451
|
+
} else result = finalize(scope, baseDraft, []);
|
|
2452
|
+
revokeScope(scope);
|
|
2453
|
+
if (scope.patches_) scope.patchListener_(scope.patches_, scope.inversePatches_);
|
|
2454
|
+
return result !== NOTHING ? result : void 0;
|
|
2455
|
+
}
|
|
2456
|
+
function finalize(rootScope, value, path) {
|
|
2457
|
+
if (isFrozen(value)) return value;
|
|
2458
|
+
const useStrictIteration = rootScope.immer_.shouldUseStrictIteration();
|
|
2459
|
+
const state = value[DRAFT_STATE];
|
|
2460
|
+
if (!state) {
|
|
2461
|
+
each(value, (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path), useStrictIteration);
|
|
2462
|
+
return value;
|
|
2463
|
+
}
|
|
2464
|
+
if (state.scope_ !== rootScope) return value;
|
|
2465
|
+
if (!state.modified_) {
|
|
2466
|
+
maybeFreeze(rootScope, state.base_, true);
|
|
2467
|
+
return state.base_;
|
|
2468
|
+
}
|
|
2469
|
+
if (!state.finalized_) {
|
|
2470
|
+
state.finalized_ = true;
|
|
2471
|
+
state.scope_.unfinalizedDrafts_--;
|
|
2472
|
+
const result = state.copy_;
|
|
2473
|
+
let resultEach = result;
|
|
2474
|
+
let isSet2 = false;
|
|
2475
|
+
if (state.type_ === 3) {
|
|
2476
|
+
resultEach = new Set(result);
|
|
2477
|
+
result.clear();
|
|
2478
|
+
isSet2 = true;
|
|
2479
|
+
}
|
|
2480
|
+
each(resultEach, (key, childValue) => finalizeProperty(rootScope, state, result, key, childValue, path, isSet2), useStrictIteration);
|
|
2481
|
+
maybeFreeze(rootScope, result, false);
|
|
2482
|
+
if (path && rootScope.patches_) getPlugin("Patches").generatePatches_(state, path, rootScope.patches_, rootScope.inversePatches_);
|
|
2483
|
+
}
|
|
2484
|
+
return state.copy_;
|
|
2485
|
+
}
|
|
2486
|
+
function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
|
|
2487
|
+
if (childValue == null) return;
|
|
2488
|
+
if (typeof childValue !== "object" && !targetIsSet) return;
|
|
2489
|
+
const childIsFrozen = isFrozen(childValue);
|
|
2490
|
+
if (childIsFrozen && !targetIsSet) return;
|
|
2491
|
+
if (childValue === targetObject) die(5);
|
|
2492
|
+
if (isDraft(childValue)) {
|
|
2493
|
+
const res = finalize(rootScope, childValue, rootPath && parentState && parentState.type_ !== 3 && !has(parentState.assigned_, prop) ? rootPath.concat(prop) : void 0);
|
|
2494
|
+
set(targetObject, prop, res);
|
|
2495
|
+
if (isDraft(res)) rootScope.canAutoFreeze_ = false;
|
|
2496
|
+
else return;
|
|
2497
|
+
} else if (targetIsSet) targetObject.add(childValue);
|
|
2498
|
+
if (isDraftable(childValue) && !childIsFrozen) {
|
|
2499
|
+
if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) return;
|
|
2500
|
+
if (parentState && parentState.base_ && parentState.base_[prop] === childValue && childIsFrozen) return;
|
|
2501
|
+
finalize(rootScope, childValue);
|
|
2502
|
+
if ((!parentState || !parentState.scope_.parent_) && typeof prop !== "symbol" && (isMap(targetObject) ? targetObject.has(prop) : Object.prototype.propertyIsEnumerable.call(targetObject, prop))) maybeFreeze(rootScope, childValue);
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
function maybeFreeze(scope, value, deep = false) {
|
|
2506
|
+
if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) freeze(value, deep);
|
|
2507
|
+
}
|
|
2508
|
+
function createProxyProxy(base, parent) {
|
|
2509
|
+
const isArray = Array.isArray(base);
|
|
2510
|
+
const state = {
|
|
2511
|
+
type_: isArray ? 1 : 0,
|
|
2512
|
+
scope_: parent ? parent.scope_ : getCurrentScope(),
|
|
2513
|
+
modified_: false,
|
|
2514
|
+
finalized_: false,
|
|
2515
|
+
assigned_: {},
|
|
2516
|
+
parent_: parent,
|
|
2517
|
+
base_: base,
|
|
2518
|
+
draft_: null,
|
|
2519
|
+
copy_: null,
|
|
2520
|
+
revoke_: null,
|
|
2521
|
+
isManual_: false
|
|
2522
|
+
};
|
|
2523
|
+
let target = state;
|
|
2524
|
+
let traps = objectTraps;
|
|
2525
|
+
if (isArray) {
|
|
2526
|
+
target = [state];
|
|
2527
|
+
traps = arrayTraps;
|
|
2528
|
+
}
|
|
2529
|
+
const { revoke, proxy } = Proxy.revocable(target, traps);
|
|
2530
|
+
state.draft_ = proxy;
|
|
2531
|
+
state.revoke_ = revoke;
|
|
2532
|
+
return proxy;
|
|
2533
|
+
}
|
|
2534
|
+
var objectTraps = {
|
|
2535
|
+
get(state, prop) {
|
|
2536
|
+
if (prop === DRAFT_STATE) return state;
|
|
2537
|
+
const source = latest(state);
|
|
2538
|
+
if (!has(source, prop)) return readPropFromProto(state, source, prop);
|
|
2539
|
+
const value = source[prop];
|
|
2540
|
+
if (state.finalized_ || !isDraftable(value)) return value;
|
|
2541
|
+
if (value === peek(state.base_, prop)) {
|
|
2542
|
+
prepareCopy(state);
|
|
2543
|
+
return state.copy_[prop] = createProxy(value, state);
|
|
2544
|
+
}
|
|
2545
|
+
return value;
|
|
2546
|
+
},
|
|
2547
|
+
has(state, prop) {
|
|
2548
|
+
return prop in latest(state);
|
|
2549
|
+
},
|
|
2550
|
+
ownKeys(state) {
|
|
2551
|
+
return Reflect.ownKeys(latest(state));
|
|
2552
|
+
},
|
|
2553
|
+
set(state, prop, value) {
|
|
2554
|
+
const desc = getDescriptorFromProto(latest(state), prop);
|
|
2555
|
+
if (desc?.set) {
|
|
2556
|
+
desc.set.call(state.draft_, value);
|
|
2557
|
+
return true;
|
|
2558
|
+
}
|
|
2559
|
+
if (!state.modified_) {
|
|
2560
|
+
const current2 = peek(latest(state), prop);
|
|
2561
|
+
const currentState = current2?.[DRAFT_STATE];
|
|
2562
|
+
if (currentState && currentState.base_ === value) {
|
|
2563
|
+
state.copy_[prop] = value;
|
|
2564
|
+
state.assigned_[prop] = false;
|
|
2565
|
+
return true;
|
|
2566
|
+
}
|
|
2567
|
+
if (is(value, current2) && (value !== void 0 || has(state.base_, prop))) return true;
|
|
2568
|
+
prepareCopy(state);
|
|
2569
|
+
markChanged(state);
|
|
2570
|
+
}
|
|
2571
|
+
if (state.copy_[prop] === value && (value !== void 0 || prop in state.copy_) || Number.isNaN(value) && Number.isNaN(state.copy_[prop])) return true;
|
|
2572
|
+
state.copy_[prop] = value;
|
|
2573
|
+
state.assigned_[prop] = true;
|
|
2574
|
+
return true;
|
|
2575
|
+
},
|
|
2576
|
+
deleteProperty(state, prop) {
|
|
2577
|
+
if (peek(state.base_, prop) !== void 0 || prop in state.base_) {
|
|
2578
|
+
state.assigned_[prop] = false;
|
|
2579
|
+
prepareCopy(state);
|
|
2580
|
+
markChanged(state);
|
|
2581
|
+
} else delete state.assigned_[prop];
|
|
2582
|
+
if (state.copy_) delete state.copy_[prop];
|
|
2583
|
+
return true;
|
|
2584
|
+
},
|
|
2585
|
+
getOwnPropertyDescriptor(state, prop) {
|
|
2586
|
+
const owner = latest(state);
|
|
2587
|
+
const desc = Reflect.getOwnPropertyDescriptor(owner, prop);
|
|
2588
|
+
if (!desc) return desc;
|
|
2589
|
+
return {
|
|
2590
|
+
writable: true,
|
|
2591
|
+
configurable: state.type_ !== 1 || prop !== "length",
|
|
2592
|
+
enumerable: desc.enumerable,
|
|
2593
|
+
value: owner[prop]
|
|
2594
|
+
};
|
|
2595
|
+
},
|
|
2596
|
+
defineProperty() {
|
|
2597
|
+
die(11);
|
|
2598
|
+
},
|
|
2599
|
+
getPrototypeOf(state) {
|
|
2600
|
+
return getPrototypeOf(state.base_);
|
|
2601
|
+
},
|
|
2602
|
+
setPrototypeOf() {
|
|
2603
|
+
die(12);
|
|
2604
|
+
}
|
|
2605
|
+
};
|
|
2606
|
+
var arrayTraps = {};
|
|
2607
|
+
each(objectTraps, (key, fn) => {
|
|
2608
|
+
arrayTraps[key] = function() {
|
|
2609
|
+
arguments[0] = arguments[0][0];
|
|
2610
|
+
return fn.apply(this, arguments);
|
|
2611
|
+
};
|
|
2612
|
+
});
|
|
2613
|
+
arrayTraps.deleteProperty = function(state, prop) {
|
|
2614
|
+
if (isNaN(parseInt(prop))) die(13);
|
|
2615
|
+
return arrayTraps.set.call(this, state, prop, void 0);
|
|
2616
|
+
};
|
|
2617
|
+
arrayTraps.set = function(state, prop, value) {
|
|
2618
|
+
if (prop !== "length" && isNaN(parseInt(prop))) die(14);
|
|
2619
|
+
return objectTraps.set.call(this, state[0], prop, value, state[0]);
|
|
2620
|
+
};
|
|
2621
|
+
function peek(draft, prop) {
|
|
2622
|
+
const state = draft[DRAFT_STATE];
|
|
2623
|
+
return (state ? latest(state) : draft)[prop];
|
|
2624
|
+
}
|
|
2625
|
+
function readPropFromProto(state, source, prop) {
|
|
2626
|
+
const desc = getDescriptorFromProto(source, prop);
|
|
2627
|
+
return desc ? `value` in desc ? desc.value : desc.get?.call(state.draft_) : void 0;
|
|
2628
|
+
}
|
|
2629
|
+
function getDescriptorFromProto(source, prop) {
|
|
2630
|
+
if (!(prop in source)) return void 0;
|
|
2631
|
+
let proto = getPrototypeOf(source);
|
|
2632
|
+
while (proto) {
|
|
2633
|
+
const desc = Object.getOwnPropertyDescriptor(proto, prop);
|
|
2634
|
+
if (desc) return desc;
|
|
2635
|
+
proto = getPrototypeOf(proto);
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
function markChanged(state) {
|
|
2639
|
+
if (!state.modified_) {
|
|
2640
|
+
state.modified_ = true;
|
|
2641
|
+
if (state.parent_) markChanged(state.parent_);
|
|
2642
|
+
}
|
|
2643
|
+
}
|
|
2644
|
+
function prepareCopy(state) {
|
|
2645
|
+
if (!state.copy_) state.copy_ = shallowCopy(state.base_, state.scope_.immer_.useStrictShallowCopy_);
|
|
2646
|
+
}
|
|
2647
|
+
var Immer2 = class {
|
|
2648
|
+
constructor(config) {
|
|
2649
|
+
this.autoFreeze_ = true;
|
|
2650
|
+
this.useStrictShallowCopy_ = false;
|
|
2651
|
+
this.useStrictIteration_ = true;
|
|
2652
|
+
/**
|
|
2653
|
+
* The `produce` function takes a value and a "recipe function" (whose
|
|
2654
|
+
* return value often depends on the base state). The recipe function is
|
|
2655
|
+
* free to mutate its first argument however it wants. All mutations are
|
|
2656
|
+
* only ever applied to a __copy__ of the base state.
|
|
2657
|
+
*
|
|
2658
|
+
* Pass only a function to create a "curried producer" which relieves you
|
|
2659
|
+
* from passing the recipe function every time.
|
|
2660
|
+
*
|
|
2661
|
+
* Only plain objects and arrays are made mutable. All other objects are
|
|
2662
|
+
* considered uncopyable.
|
|
2663
|
+
*
|
|
2664
|
+
* Note: This function is __bound__ to its `Immer` instance.
|
|
2665
|
+
*
|
|
2666
|
+
* @param {any} base - the initial state
|
|
2667
|
+
* @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modified
|
|
2668
|
+
* @param {Function} patchListener - optional function that will be called with all the patches produced here
|
|
2669
|
+
* @returns {any} a new state, or the initial state if nothing was modified
|
|
2670
|
+
*/
|
|
2671
|
+
this.produce = (base, recipe, patchListener) => {
|
|
2672
|
+
if (typeof base === "function" && typeof recipe !== "function") {
|
|
2673
|
+
const defaultBase = recipe;
|
|
2674
|
+
recipe = base;
|
|
2675
|
+
const self = this;
|
|
2676
|
+
return function curriedProduce(base2 = defaultBase, ...args) {
|
|
2677
|
+
return self.produce(base2, (draft) => recipe.call(this, draft, ...args));
|
|
2678
|
+
};
|
|
2679
|
+
}
|
|
2680
|
+
if (typeof recipe !== "function") die(6);
|
|
2681
|
+
if (patchListener !== void 0 && typeof patchListener !== "function") die(7);
|
|
2682
|
+
let result;
|
|
2683
|
+
if (isDraftable(base)) {
|
|
2684
|
+
const scope = enterScope(this);
|
|
2685
|
+
const proxy = createProxy(base, void 0);
|
|
2686
|
+
let hasError = true;
|
|
2687
|
+
try {
|
|
2688
|
+
result = recipe(proxy);
|
|
2689
|
+
hasError = false;
|
|
2690
|
+
} finally {
|
|
2691
|
+
if (hasError) revokeScope(scope);
|
|
2692
|
+
else leaveScope(scope);
|
|
2693
|
+
}
|
|
2694
|
+
usePatchesInScope(scope, patchListener);
|
|
2695
|
+
return processResult(result, scope);
|
|
2696
|
+
} else if (!base || typeof base !== "object") {
|
|
2697
|
+
result = recipe(base);
|
|
2698
|
+
if (result === void 0) result = base;
|
|
2699
|
+
if (result === NOTHING) result = void 0;
|
|
2700
|
+
if (this.autoFreeze_) freeze(result, true);
|
|
2701
|
+
if (patchListener) {
|
|
2702
|
+
const p = [];
|
|
2703
|
+
const ip = [];
|
|
2704
|
+
getPlugin("Patches").generateReplacementPatches_(base, result, p, ip);
|
|
2705
|
+
patchListener(p, ip);
|
|
2706
|
+
}
|
|
2707
|
+
return result;
|
|
2708
|
+
} else die(1, base);
|
|
2709
|
+
};
|
|
2710
|
+
this.produceWithPatches = (base, recipe) => {
|
|
2711
|
+
if (typeof base === "function") return (state, ...args) => this.produceWithPatches(state, (draft) => base(draft, ...args));
|
|
2712
|
+
let patches, inversePatches;
|
|
2713
|
+
return [
|
|
2714
|
+
this.produce(base, recipe, (p, ip) => {
|
|
2715
|
+
patches = p;
|
|
2716
|
+
inversePatches = ip;
|
|
2717
|
+
}),
|
|
2718
|
+
patches,
|
|
2719
|
+
inversePatches
|
|
2720
|
+
];
|
|
2721
|
+
};
|
|
2722
|
+
if (typeof config?.autoFreeze === "boolean") this.setAutoFreeze(config.autoFreeze);
|
|
2723
|
+
if (typeof config?.useStrictShallowCopy === "boolean") this.setUseStrictShallowCopy(config.useStrictShallowCopy);
|
|
2724
|
+
if (typeof config?.useStrictIteration === "boolean") this.setUseStrictIteration(config.useStrictIteration);
|
|
2725
|
+
}
|
|
2726
|
+
createDraft(base) {
|
|
2727
|
+
if (!isDraftable(base)) die(8);
|
|
2728
|
+
if (isDraft(base)) base = current(base);
|
|
2729
|
+
const scope = enterScope(this);
|
|
2730
|
+
const proxy = createProxy(base, void 0);
|
|
2731
|
+
proxy[DRAFT_STATE].isManual_ = true;
|
|
2732
|
+
leaveScope(scope);
|
|
2733
|
+
return proxy;
|
|
2734
|
+
}
|
|
2735
|
+
finishDraft(draft, patchListener) {
|
|
2736
|
+
const state = draft && draft[DRAFT_STATE];
|
|
2737
|
+
if (!state || !state.isManual_) die(9);
|
|
2738
|
+
const { scope_: scope } = state;
|
|
2739
|
+
usePatchesInScope(scope, patchListener);
|
|
2740
|
+
return processResult(void 0, scope);
|
|
2741
|
+
}
|
|
2742
|
+
/**
|
|
2743
|
+
* Pass true to automatically freeze all copies created by Immer.
|
|
2744
|
+
*
|
|
2745
|
+
* By default, auto-freezing is enabled.
|
|
2746
|
+
*/
|
|
2747
|
+
setAutoFreeze(value) {
|
|
2748
|
+
this.autoFreeze_ = value;
|
|
2749
|
+
}
|
|
2750
|
+
/**
|
|
2751
|
+
* Pass true to enable strict shallow copy.
|
|
2752
|
+
*
|
|
2753
|
+
* By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
|
|
2754
|
+
*/
|
|
2755
|
+
setUseStrictShallowCopy(value) {
|
|
2756
|
+
this.useStrictShallowCopy_ = value;
|
|
2757
|
+
}
|
|
2758
|
+
/**
|
|
2759
|
+
* Pass false to use faster iteration that skips non-enumerable properties
|
|
2760
|
+
* but still handles symbols for compatibility.
|
|
2761
|
+
*
|
|
2762
|
+
* By default, strict iteration is enabled (includes all own properties).
|
|
2763
|
+
*/
|
|
2764
|
+
setUseStrictIteration(value) {
|
|
2765
|
+
this.useStrictIteration_ = value;
|
|
2766
|
+
}
|
|
2767
|
+
shouldUseStrictIteration() {
|
|
2768
|
+
return this.useStrictIteration_;
|
|
2769
|
+
}
|
|
2770
|
+
applyPatches(base, patches) {
|
|
2771
|
+
let i;
|
|
2772
|
+
for (i = patches.length - 1; i >= 0; i--) {
|
|
2773
|
+
const patch = patches[i];
|
|
2774
|
+
if (patch.path.length === 0 && patch.op === "replace") {
|
|
2775
|
+
base = patch.value;
|
|
2776
|
+
break;
|
|
2777
|
+
}
|
|
2778
|
+
}
|
|
2779
|
+
if (i > -1) patches = patches.slice(i + 1);
|
|
2780
|
+
const applyPatchesImpl = getPlugin("Patches").applyPatches_;
|
|
2781
|
+
if (isDraft(base)) return applyPatchesImpl(base, patches);
|
|
2782
|
+
return this.produce(base, (draft) => applyPatchesImpl(draft, patches));
|
|
2783
|
+
}
|
|
2784
|
+
};
|
|
2785
|
+
function createProxy(value, parent) {
|
|
2786
|
+
const draft = isMap(value) ? getPlugin("MapSet").proxyMap_(value, parent) : isSet(value) ? getPlugin("MapSet").proxySet_(value, parent) : createProxyProxy(value, parent);
|
|
2787
|
+
(parent ? parent.scope_ : getCurrentScope()).drafts_.push(draft);
|
|
2788
|
+
return draft;
|
|
2789
|
+
}
|
|
2790
|
+
function current(value) {
|
|
2791
|
+
if (!isDraft(value)) die(10, value);
|
|
2792
|
+
return currentImpl(value);
|
|
2793
|
+
}
|
|
2794
|
+
function currentImpl(value) {
|
|
2795
|
+
if (!isDraftable(value) || isFrozen(value)) return value;
|
|
2796
|
+
const state = value[DRAFT_STATE];
|
|
2797
|
+
let copy;
|
|
2798
|
+
let strict = true;
|
|
2799
|
+
if (state) {
|
|
2800
|
+
if (!state.modified_) return state.base_;
|
|
2801
|
+
state.finalized_ = true;
|
|
2802
|
+
copy = shallowCopy(value, state.scope_.immer_.useStrictShallowCopy_);
|
|
2803
|
+
strict = state.scope_.immer_.shouldUseStrictIteration();
|
|
2804
|
+
} else copy = shallowCopy(value, true);
|
|
2805
|
+
each(copy, (key, childValue) => {
|
|
2806
|
+
set(copy, key, currentImpl(childValue));
|
|
2807
|
+
}, strict);
|
|
2808
|
+
if (state) state.finalized_ = false;
|
|
2809
|
+
return copy;
|
|
2810
|
+
}
|
|
2811
|
+
var immer = new Immer2();
|
|
2812
|
+
immer.produce;
|
|
2813
|
+
var setAutoFreeze = /* @__PURE__ */ immer.setAutoFreeze.bind(immer);
|
|
2814
|
+
const produce = new Immer2({
|
|
2815
|
+
autoFreeze: false,
|
|
2816
|
+
useStrictShallowCopy: true
|
|
2817
|
+
}).produce;
|
|
2818
|
+
//#endregion
|
|
2819
|
+
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/core/debounce-timer.js
|
|
2820
|
+
/**
|
|
2821
|
+
* Deadline-based debounce timer.
|
|
2822
|
+
*
|
|
2823
|
+
* Instead of clearing and re-creating a setTimeout on every call
|
|
2824
|
+
* (expensive at high frequency, e.g. 60fps mousemove), this pushes out
|
|
2825
|
+
* a `debounceUntil` deadline. The single running timer callback checks
|
|
2826
|
+
* whether the deadline has been reached:
|
|
2827
|
+
* - If not, it re-schedules itself for the remaining time.
|
|
2828
|
+
* - If so, it executes the callback.
|
|
2829
|
+
*
|
|
2830
|
+
* This means at most one `setTimeout` is live at any time, and
|
|
2831
|
+
* high-frequency `schedule()` calls only update a numeric field.
|
|
2832
|
+
*/
|
|
2833
|
+
var DebounceTimer = class {
|
|
2834
|
+
constructor(defaultMs = 5e4) {
|
|
2835
|
+
this.debounceUntil = 0;
|
|
2836
|
+
this.defaultMs = defaultMs;
|
|
2837
|
+
}
|
|
2838
|
+
/**
|
|
2839
|
+
* Schedule (or postpone) the callback.
|
|
2840
|
+
* If a timer is already running, only the deadline is pushed out.
|
|
2841
|
+
*/
|
|
2842
|
+
schedule(callback, ms) {
|
|
2843
|
+
const delay = ms ?? this.defaultMs;
|
|
2844
|
+
this.debounceUntil = Date.now() + delay;
|
|
2845
|
+
this.callback = callback;
|
|
2846
|
+
if (this.timer == null) this.timer = setTimeout(() => {
|
|
2847
|
+
this.onFired();
|
|
2848
|
+
}, delay);
|
|
2849
|
+
}
|
|
2850
|
+
/** Cancel any pending execution. */
|
|
2851
|
+
clear() {
|
|
2852
|
+
if (this.timer != null) {
|
|
2853
|
+
clearTimeout(this.timer);
|
|
2854
|
+
this.timer = void 0;
|
|
2855
|
+
}
|
|
2856
|
+
this.callback = void 0;
|
|
2857
|
+
this.debounceUntil = 0;
|
|
2858
|
+
}
|
|
2859
|
+
/** Whether a timer is currently scheduled. */
|
|
2860
|
+
get pending() {
|
|
2861
|
+
return this.timer != null;
|
|
2862
|
+
}
|
|
2863
|
+
onFired() {
|
|
2864
|
+
this.timer = void 0;
|
|
2865
|
+
const remaining = this.debounceUntil - Date.now();
|
|
2866
|
+
if (remaining > 0) this.timer = setTimeout(() => {
|
|
2867
|
+
this.onFired();
|
|
2868
|
+
}, remaining);
|
|
2869
|
+
else {
|
|
2870
|
+
const cb = this.callback;
|
|
2871
|
+
this.callback = void 0;
|
|
2872
|
+
cb?.();
|
|
2873
|
+
}
|
|
2874
|
+
}
|
|
2875
|
+
};
|
|
2876
|
+
//#endregion
|
|
2877
|
+
//#region ../../node_modules/.pnpm/loro-mirror@2.3.3_patch_hash=a551a0e4aa4f61ec88b0d9e970a183b5ee2955107a2e0bb03b378bc510c2f395_loro-crdt@1.16.1/node_modules/loro-mirror/dist/core/ephemeral.js
|
|
2878
|
+
/**
|
|
2879
|
+
* Manages ephemeral patches: storage, composition, path resolution, and finalization.
|
|
2880
|
+
*/
|
|
2881
|
+
var EphemeralPatchManager = class {
|
|
2882
|
+
constructor(store) {
|
|
2883
|
+
/** Latest patch snapshot by container id */
|
|
2884
|
+
this.patches = /* @__PURE__ */ new Map();
|
|
2885
|
+
/** Tracks what the local peer last wrote, keyed by ContainerID -> fieldKey -> value */
|
|
2886
|
+
this.localValues = /* @__PURE__ */ new Map();
|
|
2887
|
+
this.debounce = new DebounceTimer();
|
|
2888
|
+
this.store = store;
|
|
2889
|
+
this.syncAllPatchesFromStore();
|
|
2890
|
+
}
|
|
2891
|
+
/** Subscribe to EphemeralStore changes. Returns unsubscribe function. */
|
|
2892
|
+
subscribe(listener) {
|
|
2893
|
+
return this.store.subscribe((event) => {
|
|
2894
|
+
const typedEvent = event;
|
|
2895
|
+
listener({
|
|
2896
|
+
by: typedEvent.by,
|
|
2897
|
+
added: typedEvent.added.map((id) => id),
|
|
2898
|
+
updated: typedEvent.updated.map((id) => id),
|
|
2899
|
+
removed: typedEvent.removed.map((id) => id),
|
|
2900
|
+
deltas: this.syncFromStoreEvent(typedEvent)
|
|
2901
|
+
});
|
|
2902
|
+
});
|
|
988
2903
|
}
|
|
989
2904
|
get hasLocalPatches() {
|
|
990
2905
|
return this.localValues.size > 0;
|
|
@@ -3905,6 +5820,296 @@ function mergeInitialIntoBaseWithSchema(base, init, rootSchema) {
|
|
|
3905
5820
|
}
|
|
3906
5821
|
}
|
|
3907
5822
|
//#endregion
|
|
5823
|
+
//#region ../medeo-dsl/src/schema/codec.ts
|
|
5824
|
+
function isInvalidAtomic(value) {
|
|
5825
|
+
return typeof value === "object" && value !== null && "invalid" in value && value.invalid === true;
|
|
5826
|
+
}
|
|
5827
|
+
function codec(encode, decode) {
|
|
5828
|
+
return {
|
|
5829
|
+
encode: (v) => isInvalidAtomic(v) ? v.raw : encode(v),
|
|
5830
|
+
decode: (raw) => {
|
|
5831
|
+
try {
|
|
5832
|
+
return decode(raw);
|
|
5833
|
+
} catch (error) {
|
|
5834
|
+
return {
|
|
5835
|
+
invalid: true,
|
|
5836
|
+
raw,
|
|
5837
|
+
reason: String(error)
|
|
5838
|
+
};
|
|
5839
|
+
}
|
|
5840
|
+
}
|
|
5841
|
+
};
|
|
5842
|
+
}
|
|
5843
|
+
/** A Clip's time anchor; the Clip holding it is the anchored end. */
|
|
5844
|
+
const timeAnchorCodec = codec((v) => {
|
|
5845
|
+
assertTimeAnchor(v);
|
|
5846
|
+
return JSON.stringify({
|
|
5847
|
+
to: v.to,
|
|
5848
|
+
offsetMs: v.offsetMs,
|
|
5849
|
+
fallback_abs_ms: v.fallback_abs_ms
|
|
5850
|
+
});
|
|
5851
|
+
}, (raw) => {
|
|
5852
|
+
const v = JSON.parse(raw);
|
|
5853
|
+
assertTimeAnchor(v);
|
|
5854
|
+
return v;
|
|
5855
|
+
});
|
|
5856
|
+
/** A segment selection as stored: a run's Cursors as their native bytes in unpadded base64url. */
|
|
5857
|
+
function encodeSelection(v) {
|
|
5858
|
+
return {
|
|
5859
|
+
audioScriptId: v.audioScriptId,
|
|
5860
|
+
segmentId: v.segmentId,
|
|
5861
|
+
...v.start === void 0 || v.end === void 0 ? {} : {
|
|
5862
|
+
start: bytesToBase64url(v.start.encode()),
|
|
5863
|
+
end: bytesToBase64url(v.end.encode())
|
|
5864
|
+
}
|
|
5865
|
+
};
|
|
5866
|
+
}
|
|
5867
|
+
function decodeSelection(v) {
|
|
5868
|
+
if (!v || typeof v !== "object") throw new Error("Expected a segment selection");
|
|
5869
|
+
const { start, end, ...rest } = v;
|
|
5870
|
+
if (start === void 0 && end === void 0) return rest;
|
|
5871
|
+
if (typeof start !== "string" || typeof end !== "string") throw new Error("Expected encoded Cursor pair");
|
|
5872
|
+
return {
|
|
5873
|
+
...rest,
|
|
5874
|
+
start: Cursor.decode(base64urlToBytes(start)),
|
|
5875
|
+
end: Cursor.decode(base64urlToBytes(end))
|
|
5876
|
+
};
|
|
5877
|
+
}
|
|
5878
|
+
/** A Caption's selection. */
|
|
5879
|
+
const captionContentCodec = codec((v) => {
|
|
5880
|
+
assertCaptionContent(v);
|
|
5881
|
+
return JSON.stringify(encodeSelection(v));
|
|
5882
|
+
}, (raw) => {
|
|
5883
|
+
const value = decodeSelection(JSON.parse(raw));
|
|
5884
|
+
assertCaptionContent(value);
|
|
5885
|
+
return value;
|
|
5886
|
+
});
|
|
5887
|
+
/** A PhoneticScript's selection and what to synthesize for it, stored as one string so they move together. */
|
|
5888
|
+
const phonemeCodec = codec((v) => {
|
|
5889
|
+
assertPhoneme(v);
|
|
5890
|
+
return JSON.stringify({
|
|
5891
|
+
...encodeSelection(v),
|
|
5892
|
+
...v.phonemeScript === void 0 ? {} : { phonemeScript: v.phonemeScript }
|
|
5893
|
+
});
|
|
5894
|
+
}, (raw) => {
|
|
5895
|
+
const value = decodeSelection(JSON.parse(raw));
|
|
5896
|
+
assertPhoneme(value);
|
|
5897
|
+
return value;
|
|
5898
|
+
});
|
|
5899
|
+
/** A row-owned binding couples the selection, pronunciation and its immutable file in one LWW value. */
|
|
5900
|
+
const phoneticBindingCodec = codec((value) => {
|
|
5901
|
+
assertEntity("phonetic-script-entities", value);
|
|
5902
|
+
assertPhoneticScript(void 0, { ...value });
|
|
5903
|
+
return JSON.stringify({
|
|
5904
|
+
phoneme: JSON.parse(phonemeCodec.encode(value.phoneme)),
|
|
5905
|
+
system: value.system,
|
|
5906
|
+
key: value.key,
|
|
5907
|
+
storageKey: value.storageKey,
|
|
5908
|
+
digest: value.digest
|
|
5909
|
+
});
|
|
5910
|
+
}, (raw) => {
|
|
5911
|
+
const value = JSON.parse(raw);
|
|
5912
|
+
if (!isRecord(value)) throw new Error("Expected a PhoneticScript binding");
|
|
5913
|
+
const phoneme = phonemeCodec.decode(JSON.stringify(value.phoneme));
|
|
5914
|
+
if (isInvalidAtomic(phoneme)) throw new Error(phoneme.reason);
|
|
5915
|
+
const binding = {
|
|
5916
|
+
...value,
|
|
5917
|
+
phoneme
|
|
5918
|
+
};
|
|
5919
|
+
assertEntity("phonetic-script-entities", binding);
|
|
5920
|
+
assertPhoneticScript(void 0, binding);
|
|
5921
|
+
return binding;
|
|
5922
|
+
});
|
|
5923
|
+
/** A Clip's sequence marker is stored as one string, so it is taken or replaced whole. */
|
|
5924
|
+
const seqMarkerCodec = codec((v) => {
|
|
5925
|
+
assertSeqMarker(v);
|
|
5926
|
+
return JSON.stringify({
|
|
5927
|
+
inMs: v.inMs,
|
|
5928
|
+
outMs: v.outMs,
|
|
5929
|
+
...v.rate === void 0 ? {} : { rate: v.rate },
|
|
5930
|
+
...v.fillTimeline === void 0 ? {} : { fillTimeline: v.fillTimeline }
|
|
5931
|
+
});
|
|
5932
|
+
}, (raw) => {
|
|
5933
|
+
const v = JSON.parse(raw);
|
|
5934
|
+
assertSeqMarker(v);
|
|
5935
|
+
return v;
|
|
5936
|
+
});
|
|
5937
|
+
//#endregion
|
|
5938
|
+
//#region ../medeo-dsl/src/schema/loro-shape.ts
|
|
5939
|
+
const optional = { required: false };
|
|
5940
|
+
const mergeable = { mergeableMapChildContainers: true };
|
|
5941
|
+
const strings = (keys) => Object.fromEntries(keys.map((key) => [key, schema.String()]));
|
|
5942
|
+
const relation = (keys) => schema.LoroMapRecord(schema.LoroMap(strings(keys)), mergeable);
|
|
5943
|
+
const transform = (codec) => ({
|
|
5944
|
+
...codec,
|
|
5945
|
+
isEqual: "encoded-value-equality",
|
|
5946
|
+
validateEncodedType: true
|
|
5947
|
+
});
|
|
5948
|
+
/** One field stored as one encoded string, so it is taken or replaced whole. */
|
|
5949
|
+
const atomicValue = (codec) => schema.String().transform(transform(codec));
|
|
5950
|
+
const optionalAtomicValue = (codec) => schema.String(optional).transform(transform(codec));
|
|
5951
|
+
/**
|
|
5952
|
+
* Only direct children inherit mergeable identity. Text and ordinary entity rows keep normal
|
|
5953
|
+
* identities, so deleting a row also drops what a peer wrote into it concurrently: a relation an
|
|
5954
|
+
* entity holds at most one of is a field of that row and never outlives it.
|
|
5955
|
+
*/
|
|
5956
|
+
const medeoDslLoroShape = schema({
|
|
5957
|
+
"timeline-entities": schema.LoroMapRecord(schema.LoroMap({})),
|
|
5958
|
+
"track-entities": schema.LoroMapRecord(schema.LoroMap({
|
|
5959
|
+
role: schema.String(),
|
|
5960
|
+
hidden: schema.Boolean(optional)
|
|
5961
|
+
})),
|
|
5962
|
+
"clip-entities": schema.LoroMapRecord(schema.LoroMap({
|
|
5963
|
+
seqMarker: atomicValue(seqMarkerCodec),
|
|
5964
|
+
volume: schema.Number(optional),
|
|
5965
|
+
source: schema.String(),
|
|
5966
|
+
anchor: optionalAtomicValue(timeAnchorCodec)
|
|
5967
|
+
})),
|
|
5968
|
+
"media-entities": schema.LoroMapRecord(schema.LoroMap({
|
|
5969
|
+
kind: schema.String(),
|
|
5970
|
+
system: schema.String(),
|
|
5971
|
+
key: schema.String(),
|
|
5972
|
+
storageKey: schema.String(optional),
|
|
5973
|
+
durationMs: schema.Number(optional),
|
|
5974
|
+
digest: schema.String(optional)
|
|
5975
|
+
}), mergeable),
|
|
5976
|
+
"audio-script-entities": schema.LoroMapRecord(schema.LoroMap({
|
|
5977
|
+
segments: schema.LoroMovableList(schema.LoroMap({
|
|
5978
|
+
id: schema.String(),
|
|
5979
|
+
text: schema.LoroText(),
|
|
5980
|
+
startMs: schema.Number(optional),
|
|
5981
|
+
endMs: schema.Number(optional)
|
|
5982
|
+
}), (segment) => segment.id),
|
|
5983
|
+
defaultLanguage: schema.String(optional),
|
|
5984
|
+
system: schema.String(optional),
|
|
5985
|
+
key: schema.String(optional),
|
|
5986
|
+
storageKey: schema.String(optional),
|
|
5987
|
+
digest: schema.String(optional)
|
|
5988
|
+
})),
|
|
5989
|
+
"phonetic-script-entities": schema.LoroMapRecord(schema.LoroMap({ binding: atomicValue(phoneticBindingCodec) })),
|
|
5990
|
+
"caption-entities": schema.LoroMapRecord(schema.LoroMap({
|
|
5991
|
+
selection: optionalAtomicValue(captionContentCodec),
|
|
5992
|
+
text: schema.LoroText(optional),
|
|
5993
|
+
style: schema.LoroMap({
|
|
5994
|
+
font: schema.LoroMap({
|
|
5995
|
+
system: schema.String(),
|
|
5996
|
+
key: schema.String()
|
|
5997
|
+
}, optional),
|
|
5998
|
+
fontSize: schema.Number(optional),
|
|
5999
|
+
fontColor: schema.String(optional),
|
|
6000
|
+
fontWeight: schema.Number(optional),
|
|
6001
|
+
entranceAnimation: schema.String(optional),
|
|
6002
|
+
entranceAnimationDurationMs: schema.Number(optional),
|
|
6003
|
+
strokeColor: schema.String(optional),
|
|
6004
|
+
strokeWidth: schema.Number(optional),
|
|
6005
|
+
positionX: schema.Number(optional),
|
|
6006
|
+
positionY: schema.Number(optional)
|
|
6007
|
+
}, {
|
|
6008
|
+
...optional,
|
|
6009
|
+
...mergeable
|
|
6010
|
+
})
|
|
6011
|
+
}, mergeable)),
|
|
6012
|
+
"voice-entities": schema.LoroMapRecord(schema.LoroMap({
|
|
6013
|
+
system: schema.String(),
|
|
6014
|
+
key: schema.String(),
|
|
6015
|
+
name: schema.String(optional)
|
|
6016
|
+
}), mergeable),
|
|
6017
|
+
"contain-relations": schema.LoroTree(schema.LoroMap({ entityId: schema.String() })),
|
|
6018
|
+
"voice-timbre-relations": relation(["recordingId", "voiceId"]),
|
|
6019
|
+
"derived-from-relations": schema.LoroMapRecord(schema.LoroMap({
|
|
6020
|
+
entityId: schema.String(),
|
|
6021
|
+
sourceId: schema.String(),
|
|
6022
|
+
sourceDigest: schema.String(optional)
|
|
6023
|
+
}), mergeable)
|
|
6024
|
+
});
|
|
6025
|
+
//#endregion
|
|
6026
|
+
//#region ../medeo-dsl/src/schema/document-format.ts
|
|
6027
|
+
/** Format discrimination does not register containers or alter the document. */
|
|
6028
|
+
function documentFormat(doc) {
|
|
6029
|
+
const roots = doc.getShallowValue();
|
|
6030
|
+
const legacy = [
|
|
6031
|
+
"medeo",
|
|
6032
|
+
"timeline",
|
|
6033
|
+
"tracks",
|
|
6034
|
+
"part_library"
|
|
6035
|
+
].some((key) => Object.hasOwn(roots, key));
|
|
6036
|
+
const dsl = Object.keys(roots).some((key) => key.endsWith("-entities") || key.endsWith("-relations"));
|
|
6037
|
+
return legacy && dsl ? "mixed" : legacy ? "legacy" : dsl ? "dsl" : "unknown";
|
|
6038
|
+
}
|
|
6039
|
+
var UnsupportedDocumentFormatError = class extends Error {
|
|
6040
|
+
code = "unsupported_document_format";
|
|
6041
|
+
constructor(message = "Expected a new DSL document; legacy, mixed or uninitialized content is not supported") {
|
|
6042
|
+
super(message);
|
|
6043
|
+
this.name = "UnsupportedDocumentFormatError";
|
|
6044
|
+
}
|
|
6045
|
+
};
|
|
6046
|
+
/** Run after initial import, before opening an editable handle. Empty new documents still have one Timeline. */
|
|
6047
|
+
function assertDslDocument(doc) {
|
|
6048
|
+
if (documentFormat(doc) !== "dsl" || doc.getMap("timeline-entities").keys().length !== 1) throw new UnsupportedDocumentFormatError();
|
|
6049
|
+
assertPhoneticBindingFormat(doc);
|
|
6050
|
+
}
|
|
6051
|
+
/** This storage cutover has no field-per-fact reader or mixed-version write path. */
|
|
6052
|
+
function assertPhoneticBindingFormat(doc) {
|
|
6053
|
+
if (!Object.hasOwn(doc.getShallowValue(), "phonetic-script-entities")) return;
|
|
6054
|
+
const table = doc.getMap("phonetic-script-entities");
|
|
6055
|
+
for (const id of table.keys()) {
|
|
6056
|
+
const row = table.get(id);
|
|
6057
|
+
if (!isContainer(row) || row.kind() !== "Map" || row.keys().some((key) => key !== "binding") || typeof row.get("binding") !== "string") throw new UnsupportedDocumentFormatError("PhoneticScript storage requires one atomic binding; old DSL documents need explicit migration");
|
|
6058
|
+
}
|
|
6059
|
+
}
|
|
6060
|
+
//#endregion
|
|
6061
|
+
//#region ../medeo-dsl/src/schema/text-styles.ts
|
|
6062
|
+
/** How rich-text marks extend at their edges: a language runs on into text typed after it. */
|
|
6063
|
+
const textStyles = { language: { expand: "after" } };
|
|
6064
|
+
//#endregion
|
|
6065
|
+
//#region src/dsl/document/phonetic-binding.ts
|
|
6066
|
+
/**
|
|
6067
|
+
* Actions and the transaction guard keep their domain-shaped rows. The native schema stores
|
|
6068
|
+
* a PhoneticScript's facts under one binding; this lens applies draft edits to that single value.
|
|
6069
|
+
* It never reads the retired field-per-fact representation.
|
|
6070
|
+
*/
|
|
6071
|
+
function logicalDslState(native) {
|
|
6072
|
+
const phonetics = new Proxy({}, {
|
|
6073
|
+
get: (_target, key) => typeof key === "string" ? native["phonetic-script-entities"]?.[key]?.binding : void 0,
|
|
6074
|
+
set: (_target, key, value) => {
|
|
6075
|
+
if (typeof key !== "string") throw new Error("Invalid PhoneticScript key");
|
|
6076
|
+
native["phonetic-script-entities"] ??= {};
|
|
6077
|
+
const existing = native["phonetic-script-entities"][key];
|
|
6078
|
+
if (existing) existing.binding = value;
|
|
6079
|
+
else native["phonetic-script-entities"][key] = { binding: value };
|
|
6080
|
+
return true;
|
|
6081
|
+
},
|
|
6082
|
+
deleteProperty: (_target, key) => {
|
|
6083
|
+
if (typeof key === "string") delete native["phonetic-script-entities"][key];
|
|
6084
|
+
return true;
|
|
6085
|
+
},
|
|
6086
|
+
ownKeys: () => Object.keys(native["phonetic-script-entities"] ?? {}),
|
|
6087
|
+
has: (_target, key) => typeof key === "string" && Object.hasOwn(native["phonetic-script-entities"] ?? {}, key),
|
|
6088
|
+
getOwnPropertyDescriptor: (_target, key) => typeof key === "string" && Object.hasOwn(native["phonetic-script-entities"] ?? {}, key) ? {
|
|
6089
|
+
configurable: true,
|
|
6090
|
+
enumerable: true,
|
|
6091
|
+
writable: true,
|
|
6092
|
+
value: native["phonetic-script-entities"][key].binding
|
|
6093
|
+
} : void 0
|
|
6094
|
+
});
|
|
6095
|
+
const view = {};
|
|
6096
|
+
for (const key of new Set([
|
|
6097
|
+
...Object.keys(native),
|
|
6098
|
+
...entityTables,
|
|
6099
|
+
...relationTables,
|
|
6100
|
+
"contain-relations"
|
|
6101
|
+
])) Object.defineProperty(view, key, {
|
|
6102
|
+
configurable: true,
|
|
6103
|
+
enumerable: true,
|
|
6104
|
+
get: () => key === "phonetic-script-entities" ? phonetics : Reflect.get(native, key),
|
|
6105
|
+
set: (value) => {
|
|
6106
|
+
if (key === "phonetic-script-entities") throw new Error("Cannot replace the PhoneticScript table");
|
|
6107
|
+
Reflect.set(native, key, value);
|
|
6108
|
+
}
|
|
6109
|
+
});
|
|
6110
|
+
return view;
|
|
6111
|
+
}
|
|
6112
|
+
//#endregion
|
|
3908
6113
|
//#region src/dsl/document/read.ts
|
|
3909
6114
|
/** Looking up a missing root must not create it. */
|
|
3910
6115
|
function rootMap(doc, name) {
|
|
@@ -3988,6 +6193,7 @@ function referenceTargets(table, row) {
|
|
|
3988
6193
|
}
|
|
3989
6194
|
}
|
|
3990
6195
|
function readMedeoDsl(doc, state) {
|
|
6196
|
+
const logical = logicalDslState(state);
|
|
3991
6197
|
const diagnostics = [];
|
|
3992
6198
|
const diagnostic = (code, path, error) => diagnostics.push({
|
|
3993
6199
|
code,
|
|
@@ -3997,7 +6203,7 @@ function readMedeoDsl(doc, state) {
|
|
|
3997
6203
|
const entities = Object.fromEntries(entityTables.map((t) => [t, {}]));
|
|
3998
6204
|
const relations = Object.fromEntries(relationTables.map((t) => [t, {}]));
|
|
3999
6205
|
const richText = {};
|
|
4000
|
-
for (const table of entityTables) for (const [id, raw] of Object.entries(
|
|
6206
|
+
for (const table of entityTables) for (const [id, raw] of Object.entries(logical[table] ?? {})) try {
|
|
4001
6207
|
assertId(id);
|
|
4002
6208
|
let v = raw;
|
|
4003
6209
|
for (const field of optionalReferences[table] ?? []) {
|
|
@@ -4408,7 +6614,10 @@ function applyMutations(tx, mutations) {
|
|
|
4408
6614
|
delete rows[m.table][m.id];
|
|
4409
6615
|
break;
|
|
4410
6616
|
case "insert-segment":
|
|
4411
|
-
tx.insertSegment(m.audioScriptId, m.segmentId, m.text, m.index
|
|
6617
|
+
tx.insertSegment(m.audioScriptId, m.segmentId, m.text, m.index, m.startMs === void 0 && m.endMs === void 0 ? void 0 : {
|
|
6618
|
+
startMs: m.startMs,
|
|
6619
|
+
endMs: m.endMs
|
|
6620
|
+
});
|
|
4412
6621
|
break;
|
|
4413
6622
|
case "move-segment":
|
|
4414
6623
|
tx.moveSegment(m.audioScriptId, m.segmentId, m.index);
|
|
@@ -4500,7 +6709,11 @@ function validateDraft(tracked) {
|
|
|
4500
6709
|
before,
|
|
4501
6710
|
after: state,
|
|
4502
6711
|
entities: tracked.entities,
|
|
4503
|
-
relations: tracked.relations
|
|
6712
|
+
relations: tracked.relations,
|
|
6713
|
+
containment: {
|
|
6714
|
+
before: plainTree(before["contain-relations"] ?? []),
|
|
6715
|
+
after: plainTree(state["contain-relations"] ?? [])
|
|
6716
|
+
}
|
|
4504
6717
|
});
|
|
4505
6718
|
if (!tracked.treeChanged) return;
|
|
4506
6719
|
const oldNodes = nodeIndex(before["contain-relations"] ?? []);
|
|
@@ -4577,8 +6790,11 @@ function runTransaction(doc, mirror, edit, options, setReject) {
|
|
|
4577
6790
|
const steps = [];
|
|
4578
6791
|
const newCaptionTexts = /* @__PURE__ */ new Map();
|
|
4579
6792
|
const bodies = /* @__PURE__ */ new Map();
|
|
4580
|
-
const
|
|
4581
|
-
|
|
6793
|
+
const nativeBefore = mirror.getState();
|
|
6794
|
+
const before = logicalDslState(nativeBefore);
|
|
6795
|
+
const phoneticRows = /* @__PURE__ */ new Map();
|
|
6796
|
+
mirror.setState((native) => {
|
|
6797
|
+
const raw = logicalDslState(native);
|
|
4582
6798
|
const tracked = new TransactionDraft(raw, before);
|
|
4583
6799
|
setReject((error) => tracked.fail(error));
|
|
4584
6800
|
/** The current text of a Caption's own Text or a script segment, as this transaction leaves it so far. */
|
|
@@ -4666,18 +6882,21 @@ function runTransaction(doc, mirror, edit, options, setReject) {
|
|
|
4666
6882
|
editCaptionText(captionId, edits, expectedBodyId) {
|
|
4667
6883
|
editBody({ captionId }, edits, expectedBodyId);
|
|
4668
6884
|
},
|
|
4669
|
-
insertSegment(audioScriptId, segmentId, text, index) {
|
|
6885
|
+
insertSegment(audioScriptId, segmentId, text, index, time) {
|
|
4670
6886
|
tracked.run(() => {
|
|
4671
6887
|
assertId(segmentId);
|
|
4672
6888
|
if (typeof text !== "string") throw new Error("Segment text is a string");
|
|
6889
|
+
const segment = {
|
|
6890
|
+
id: segmentId,
|
|
6891
|
+
text,
|
|
6892
|
+
...time
|
|
6893
|
+
};
|
|
6894
|
+
assertScriptSegment(segment);
|
|
4673
6895
|
const segments = segmentsOf(audioScriptId);
|
|
4674
6896
|
if (segments.some((segment) => segment.id === segmentId)) throw new Error(`Segment already exists: ${segmentId}`);
|
|
4675
6897
|
const at = index ?? segments.length;
|
|
4676
6898
|
if (!Number.isSafeInteger(at) || at < 0 || at > segments.length) throw new Error("Invalid segment index");
|
|
4677
|
-
segments.splice(at, 0,
|
|
4678
|
-
id: segmentId,
|
|
4679
|
-
text
|
|
4680
|
-
});
|
|
6899
|
+
segments.splice(at, 0, segment);
|
|
4681
6900
|
});
|
|
4682
6901
|
},
|
|
4683
6902
|
moveSegment(audioScriptId, segmentId, index) {
|
|
@@ -4767,6 +6986,15 @@ function runTransaction(doc, mirror, edit, options, setReject) {
|
|
|
4767
6986
|
for (const step of steps) if (step.type === "text") if ("captionId" in step.target) requireEntity(raw, "caption-entities", step.target.captionId);
|
|
4768
6987
|
else requireSegment(raw, step.target.audioScriptId, step.target.segmentId);
|
|
4769
6988
|
assertSelectionsAfter(raw, before, tracked, steps);
|
|
6989
|
+
const changedPhonetics = new Set([...tracked.entities.get("phonetic-script-entities")?.keys() ?? [], ...steps.flatMap((step) => step.type !== "text" && step.table === "phonetic-script-entities" ? [step.id] : [])]);
|
|
6990
|
+
for (const id of changedPhonetics) {
|
|
6991
|
+
const row = raw["phonetic-script-entities"][id];
|
|
6992
|
+
phoneticRows.set(id, row ? copyInput(row) : void 0);
|
|
6993
|
+
if (!row) continue;
|
|
6994
|
+
const old = nativeBefore["phonetic-script-entities"][id];
|
|
6995
|
+
if (old) native["phonetic-script-entities"][id] = old;
|
|
6996
|
+
else delete native["phonetic-script-entities"][id];
|
|
6997
|
+
}
|
|
4770
6998
|
for (const [id, fields] of tracked.entities.get("caption-entities") ?? []) {
|
|
4771
6999
|
const row = raw["caption-entities"][id];
|
|
4772
7000
|
if (fields.has("text") && row?.text !== void 0 && before["caption-entities"][id]?.text === void 0) {
|
|
@@ -4775,6 +7003,7 @@ function runTransaction(doc, mirror, edit, options, setReject) {
|
|
|
4775
7003
|
}
|
|
4776
7004
|
}
|
|
4777
7005
|
for (const table of Object.keys(selectionField)) {
|
|
7006
|
+
if (table === "phonetic-script-entities") continue;
|
|
4778
7007
|
const field = selectionField[table];
|
|
4779
7008
|
for (const [id, fields] of tracked.entities.get(table) ?? []) {
|
|
4780
7009
|
const row = raw[table][id];
|
|
@@ -4814,6 +7043,8 @@ function runTransaction(doc, mirror, edit, options, setReject) {
|
|
|
4814
7043
|
applyTextEdits(text, step.edits);
|
|
4815
7044
|
continue;
|
|
4816
7045
|
}
|
|
7046
|
+
const phonetic = step.table === "phonetic-script-entities" ? phoneticRows.get(step.id) : void 0;
|
|
7047
|
+
if (step.table === "phonetic-script-entities" && !phonetic) continue;
|
|
4817
7048
|
const row = entityRow(doc, step.table, step.id);
|
|
4818
7049
|
const field = selectionField[step.table];
|
|
4819
7050
|
if (step.type === "range") {
|
|
@@ -4832,12 +7063,21 @@ function runTransaction(doc, mirror, edit, options, setReject) {
|
|
|
4832
7063
|
if (!value.start || !value.end) throw new Error("Unable to construct prepared range");
|
|
4833
7064
|
}
|
|
4834
7065
|
if (step.phonemeScript !== void 0) value.phonemeScript = step.phonemeScript;
|
|
4835
|
-
if (
|
|
4836
|
-
|
|
4837
|
-
|
|
7066
|
+
if (phonetic) phonetic.phoneme = value;
|
|
7067
|
+
else {
|
|
7068
|
+
if (!row) throw new Error("Unable to construct prepared range");
|
|
7069
|
+
row.set(field, codecs[step.table].encode(value));
|
|
7070
|
+
}
|
|
7071
|
+
} else if (step.value !== void 0) if (phonetic) phonetic.phoneme = step.value;
|
|
7072
|
+
else {
|
|
4838
7073
|
if (!row) throw new Error("Prepared selection owner is missing");
|
|
4839
7074
|
row.set(field, codecs[step.table].encode(step.value));
|
|
4840
|
-
}
|
|
7075
|
+
}
|
|
7076
|
+
else if (row?.get(field) !== void 0) row.delete(field);
|
|
7077
|
+
}
|
|
7078
|
+
for (const [id, binding] of phoneticRows) {
|
|
7079
|
+
if (!binding) continue;
|
|
7080
|
+
(entityRow(doc, "phonetic-script-entities", id) ?? doc.getMap("phonetic-script-entities").setContainer(id, new LoroMap())).set("binding", phoneticBindingCodec.encode(binding));
|
|
4841
7081
|
}
|
|
4842
7082
|
}
|
|
4843
7083
|
});
|
|
@@ -4884,7 +7124,7 @@ function assertSelectionsAfter(raw, before, tracked, steps) {
|
|
|
4884
7124
|
if (!row) continue;
|
|
4885
7125
|
const phoneme = phonemes.has(id) ? phonemes.get(id) : row.phoneme;
|
|
4886
7126
|
const old = before["phonetic-script-entities"][id];
|
|
4887
|
-
assertPhoneticScript(old, {
|
|
7127
|
+
assertPhoneticScript(old ? { ...old } : void 0, {
|
|
4888
7128
|
...row,
|
|
4889
7129
|
phoneme: isRecord(phoneme) ? phoneme : void 0
|
|
4890
7130
|
});
|
|
@@ -4923,6 +7163,7 @@ var MedeoDsl = class MedeoDsl {
|
|
|
4923
7163
|
"tracks",
|
|
4924
7164
|
"part_library"
|
|
4925
7165
|
].some((key) => Object.hasOwn(roots, key))) throw new Error("Expected a new DSL document, not a legacy content model");
|
|
7166
|
+
assertPhoneticBindingFormat(doc);
|
|
4926
7167
|
doc.configTextStyle(textStyles);
|
|
4927
7168
|
this.mirror = new Mirror({
|
|
4928
7169
|
doc,
|
|
@@ -4986,6 +7227,11 @@ var MedeoDsl = class MedeoDsl {
|
|
|
4986
7227
|
editCaptionText(captionId, expectedBodyId, edits, audit) {
|
|
4987
7228
|
this.transact((tx) => tx.editCaptionText(captionId, edits, expectedBodyId), audit);
|
|
4988
7229
|
}
|
|
7230
|
+
/** Check the session's write capability before an Action starts host-owned resource I/O. */
|
|
7231
|
+
assertWritable() {
|
|
7232
|
+
this.assertReadyToWrite();
|
|
7233
|
+
this.options.mutationGuard?.run(() => {});
|
|
7234
|
+
}
|
|
4989
7235
|
/**
|
|
4990
7236
|
* A run inside one segment as native Cursors. Local ranges exclude insertions at their start and
|
|
4991
7237
|
* include insertions at their end. K01 remains deferred.
|
|
@@ -5018,13 +7264,7 @@ var MedeoDsl = class MedeoDsl {
|
|
|
5018
7264
|
if (this.ownsDoc) this.doc.free();
|
|
5019
7265
|
}
|
|
5020
7266
|
mutate(run) {
|
|
5021
|
-
this.
|
|
5022
|
-
if (this.writing || this.notifying) {
|
|
5023
|
-
const error = /* @__PURE__ */ new Error("A DSL mutation is already running");
|
|
5024
|
-
this.rejectTransaction?.(error);
|
|
5025
|
-
throw error;
|
|
5026
|
-
}
|
|
5027
|
-
if (this.doc.getPendingTxnLength() !== 0) throw new Error("Commit external native edits before a DSL mutation");
|
|
7267
|
+
this.assertReadyToWrite();
|
|
5028
7268
|
const apply = () => {
|
|
5029
7269
|
this.writing = true;
|
|
5030
7270
|
try {
|
|
@@ -5039,6 +7279,15 @@ var MedeoDsl = class MedeoDsl {
|
|
|
5039
7279
|
if (this.options.mutationGuard) this.options.mutationGuard.run(apply);
|
|
5040
7280
|
else apply();
|
|
5041
7281
|
}
|
|
7282
|
+
assertReadyToWrite() {
|
|
7283
|
+
this.assertActive();
|
|
7284
|
+
if (this.writing || this.notifying) {
|
|
7285
|
+
const error = /* @__PURE__ */ new Error("A DSL mutation is already running");
|
|
7286
|
+
this.rejectTransaction?.(error);
|
|
7287
|
+
throw error;
|
|
7288
|
+
}
|
|
7289
|
+
if (this.doc.getPendingTxnLength() !== 0) throw new Error("Commit external native edits before a DSL mutation");
|
|
7290
|
+
}
|
|
5042
7291
|
notify() {
|
|
5043
7292
|
if (this.disposed || !this.dirty) return;
|
|
5044
7293
|
this.dirty = false;
|
|
@@ -5054,6 +7303,150 @@ var MedeoDsl = class MedeoDsl {
|
|
|
5054
7303
|
}
|
|
5055
7304
|
};
|
|
5056
7305
|
//#endregion
|
|
7306
|
+
//#region src/dsl/projection/audio-scripts.ts
|
|
7307
|
+
/**
|
|
7308
|
+
* The scripts a recording says. It follows derived-from from the recording; a PhoneticScript is a
|
|
7309
|
+
* variant, so its words are the base its phoneme names, and derived-from is not followed past it.
|
|
7310
|
+
* When nothing is found that way, a script read off the recording itself is what it says.
|
|
7311
|
+
*/
|
|
7312
|
+
function spokenScripts(snapshot, recordingId) {
|
|
7313
|
+
const relations = Object.values(snapshot["derived-from-relations"]);
|
|
7314
|
+
const found = /* @__PURE__ */ new Map();
|
|
7315
|
+
const seen = /* @__PURE__ */ new Set();
|
|
7316
|
+
const visit = (id, phoneticScriptId) => {
|
|
7317
|
+
if (seen.has(id)) return;
|
|
7318
|
+
seen.add(id);
|
|
7319
|
+
if (snapshot["audio-script-entities"][id]) {
|
|
7320
|
+
if (!found.has(id)) found.set(id, phoneticScriptId);
|
|
7321
|
+
return;
|
|
7322
|
+
}
|
|
7323
|
+
const phonetic = snapshot["phonetic-script-entities"][id];
|
|
7324
|
+
if (phonetic) {
|
|
7325
|
+
visit(phonetic.phoneme.audioScriptId, id);
|
|
7326
|
+
return;
|
|
7327
|
+
}
|
|
7328
|
+
for (const r of relations) if (r.entityId === id) visit(r.sourceId, phoneticScriptId);
|
|
7329
|
+
};
|
|
7330
|
+
visit(recordingId);
|
|
7331
|
+
if (!found.size) {
|
|
7332
|
+
for (const r of relations) if (r.sourceId === recordingId && snapshot["audio-script-entities"][r.entityId]) found.set(r.entityId, void 0);
|
|
7333
|
+
}
|
|
7334
|
+
return [...found].map(([audioScriptId, phoneticScriptId]) => phoneticScriptId === void 0 ? { audioScriptId } : {
|
|
7335
|
+
audioScriptId,
|
|
7336
|
+
phoneticScriptId
|
|
7337
|
+
});
|
|
7338
|
+
}
|
|
7339
|
+
/** Every AudioScript in the document with its PhoneticScripts, the Speech Clips saying it and the Caption Clips showing it. */
|
|
7340
|
+
function toAudioScriptViews(snapshot) {
|
|
7341
|
+
const views = /* @__PURE__ */ new Map();
|
|
7342
|
+
const segmentIndex = (scriptId, segmentId) => {
|
|
7343
|
+
const index = snapshot["audio-script-entities"][scriptId]?.segments.findIndex((s) => s.id === segmentId) ?? -1;
|
|
7344
|
+
return index < 0 ? void 0 : index;
|
|
7345
|
+
};
|
|
7346
|
+
for (const [id, script] of Object.entries(snapshot["audio-script-entities"])) views.set(id, {
|
|
7347
|
+
id,
|
|
7348
|
+
segments: script.segments.map((segment) => {
|
|
7349
|
+
const rich = snapshot.richText[segment.id];
|
|
7350
|
+
const language = rich ? markedLanguage(langsFromDelta(rich.delta)) : void 0;
|
|
7351
|
+
return {
|
|
7352
|
+
id: segment.id,
|
|
7353
|
+
text: segment.text,
|
|
7354
|
+
...language === void 0 ? {} : { language },
|
|
7355
|
+
...segment.startMs === void 0 ? {} : {
|
|
7356
|
+
startMs: segment.startMs,
|
|
7357
|
+
endMs: segment.endMs
|
|
7358
|
+
}
|
|
7359
|
+
};
|
|
7360
|
+
}),
|
|
7361
|
+
sourceMediaIds: [],
|
|
7362
|
+
phoneticScripts: [],
|
|
7363
|
+
speeches: [],
|
|
7364
|
+
captions: []
|
|
7365
|
+
});
|
|
7366
|
+
for (const r of Object.values(snapshot["derived-from-relations"])) if (snapshot["media-entities"][r.sourceId]) views.get(r.entityId)?.sourceMediaIds.push(r.sourceId);
|
|
7367
|
+
for (const [id, phonetic] of Object.entries(snapshot["phonetic-script-entities"])) {
|
|
7368
|
+
const { audioScriptId, segmentId, phonemeScript } = phonetic.phoneme;
|
|
7369
|
+
const { digest } = phonetic;
|
|
7370
|
+
const segment = segmentIndex(audioScriptId, segmentId);
|
|
7371
|
+
const text = snapshot.phoneticText[id]?.text;
|
|
7372
|
+
views.get(audioScriptId)?.phoneticScripts.push({
|
|
7373
|
+
id,
|
|
7374
|
+
...segment === void 0 ? {} : { segment },
|
|
7375
|
+
...text === void 0 ? {} : { text },
|
|
7376
|
+
...phonemeScript === void 0 ? {} : { phonemeScript },
|
|
7377
|
+
...digest === void 0 ? {} : { digest }
|
|
7378
|
+
});
|
|
7379
|
+
}
|
|
7380
|
+
const layout = resolveDslLayout(snapshot);
|
|
7381
|
+
for (const view of views.values()) placeTranscript(snapshot, layout, view);
|
|
7382
|
+
const timbres = Object.values(snapshot["voice-timbre-relations"]);
|
|
7383
|
+
for (const track of layout.tracks) for (const { clipId } of track.clips) {
|
|
7384
|
+
const source = snapshot["clip-entities"][clipId]?.source;
|
|
7385
|
+
if (!source) continue;
|
|
7386
|
+
if (snapshot["media-entities"][source]?.kind === "voiceover-recording") {
|
|
7387
|
+
const voices = timbres.filter((r) => r.recordingId === source).flatMap((r) => {
|
|
7388
|
+
const voice = snapshot["voice-entities"][r.voiceId];
|
|
7389
|
+
if (!voice) return [];
|
|
7390
|
+
return [voice.name === void 0 ? {
|
|
7391
|
+
voiceId: r.voiceId,
|
|
7392
|
+
key: voice.key
|
|
7393
|
+
} : {
|
|
7394
|
+
voiceId: r.voiceId,
|
|
7395
|
+
key: voice.key,
|
|
7396
|
+
name: voice.name
|
|
7397
|
+
}];
|
|
7398
|
+
});
|
|
7399
|
+
for (const spoken of spokenScripts(snapshot, source)) {
|
|
7400
|
+
const applied = appliedSpeech(snapshot, source, spoken);
|
|
7401
|
+
views.get(spoken.audioScriptId)?.speeches.push({
|
|
7402
|
+
clipId,
|
|
7403
|
+
recordingId: source,
|
|
7404
|
+
...spoken.phoneticScriptId === void 0 ? {} : { phoneticScriptId: spoken.phoneticScriptId },
|
|
7405
|
+
voices,
|
|
7406
|
+
...applied === void 0 ? {} : { applied }
|
|
7407
|
+
});
|
|
7408
|
+
}
|
|
7409
|
+
continue;
|
|
7410
|
+
}
|
|
7411
|
+
const caption = snapshot["caption-entities"][source];
|
|
7412
|
+
if (!caption?.selection || caption.text !== void 0 || !snapshot.captionText[source]) continue;
|
|
7413
|
+
const { audioScriptId, segmentId } = caption.selection;
|
|
7414
|
+
const segment = segmentIndex(audioScriptId, segmentId);
|
|
7415
|
+
if (segment !== void 0) views.get(audioScriptId)?.captions.push({
|
|
7416
|
+
clipId,
|
|
7417
|
+
captionId: source,
|
|
7418
|
+
segment
|
|
7419
|
+
});
|
|
7420
|
+
}
|
|
7421
|
+
return [...views.values()];
|
|
7422
|
+
}
|
|
7423
|
+
/**
|
|
7424
|
+
* A transcript's segments on the Timeline: a segment's time is in the medium the script was read
|
|
7425
|
+
* off, so it lands where the earliest Clip playing that medium holds that moment in its window.
|
|
7426
|
+
*/
|
|
7427
|
+
function placeTranscript(snapshot, layout, view) {
|
|
7428
|
+
if (!view.sourceMediaIds.length) return;
|
|
7429
|
+
const plays = layout.tracks.flatMap((track) => track.clips).filter((clip) => view.sourceMediaIds.includes(snapshot["clip-entities"][clip.clipId]?.source ?? "")).sort((a, b) => a.startMs - b.startMs);
|
|
7430
|
+
for (const segment of view.segments) {
|
|
7431
|
+
const at = segment.startMs;
|
|
7432
|
+
if (at === void 0) continue;
|
|
7433
|
+
const clip = plays.find((c) => c.inMs <= at && at < c.outMs);
|
|
7434
|
+
if (clip) segment.timelineMs = Math.round(clip.startMs + (at - clip.inMs) / clip.rate);
|
|
7435
|
+
}
|
|
7436
|
+
}
|
|
7437
|
+
/**
|
|
7438
|
+
* Whether a recording still matches what it was synthesized from: the digest its derived-from
|
|
7439
|
+
* records against the source's file digest now. A recording derived from a script directly is
|
|
7440
|
+
* not judged: a written script has no file, and a transcript's file is what it was read from,
|
|
7441
|
+
* not what a take was synthesized from (D190).
|
|
7442
|
+
*/
|
|
7443
|
+
function appliedSpeech(snapshot, recordingId, spoken) {
|
|
7444
|
+
const sourceId = spoken.phoneticScriptId ?? spoken.audioScriptId;
|
|
7445
|
+
const recorded = Object.values(snapshot["derived-from-relations"]).find((r) => r.entityId === recordingId && r.sourceId === sourceId)?.sourceDigest;
|
|
7446
|
+
const current = spoken.phoneticScriptId === void 0 ? void 0 : snapshot["phonetic-script-entities"][sourceId]?.digest;
|
|
7447
|
+
return recorded === void 0 || current === void 0 ? void 0 : recorded === current;
|
|
7448
|
+
}
|
|
7449
|
+
//#endregion
|
|
5057
7450
|
//#region src/dsl/voiceover-actions.ts
|
|
5058
7451
|
function voiceoverPlan(s, input, result) {
|
|
5059
7452
|
if (requireTrack(s, input.trackId).role !== "speech" || input.media.kind !== "voiceover-recording") throw new Error("Voiceover requires a speech Track and a recording");
|
|
@@ -5154,7 +7547,7 @@ function voiceoverPlan(s, input, result) {
|
|
|
5154
7547
|
mutations.push(...placementMutations(s, result.clipId, input.placement));
|
|
5155
7548
|
uniqueIds(result.captionClipIds);
|
|
5156
7549
|
const retained = new Set(result.captionClipIds);
|
|
5157
|
-
const oldCaptions =
|
|
7550
|
+
const oldCaptions = existing ? captionsShowing(s, existing.source) : [];
|
|
5158
7551
|
mutations.push(...deleteClips(s, oldCaptions.filter((id) => !retained.has(id)), "cascade"));
|
|
5159
7552
|
const placement = mutations.find((m) => m.type === "patch-entity" && m.table === "clip-entities" && m.id === result.clipId && "anchor" in m.patch);
|
|
5160
7553
|
const anchor = placement?.type === "patch-entity" && placement.table === "clip-entities" ? placement.patch.anchor : void 0;
|
|
@@ -5225,6 +7618,33 @@ function voiceoverPlan(s, input, result) {
|
|
|
5225
7618
|
});
|
|
5226
7619
|
return mutations;
|
|
5227
7620
|
}
|
|
7621
|
+
/**
|
|
7622
|
+
* Caption Clips showing what a recording says (D183): their Caption selects the run of the segment
|
|
7623
|
+
* the PhoneticScript it was spoken from is written for, or any segment of the script it was read
|
|
7624
|
+
* from directly.
|
|
7625
|
+
*/
|
|
7626
|
+
function captionsShowing(s, recordingId) {
|
|
7627
|
+
const said = spokenScripts(s, recordingId).map(({ audioScriptId, phoneticScriptId }) => {
|
|
7628
|
+
if (phoneticScriptId === void 0) return { audioScriptId };
|
|
7629
|
+
const { segmentId } = s["phonetic-script-entities"][phoneticScriptId].phoneme;
|
|
7630
|
+
return {
|
|
7631
|
+
audioScriptId,
|
|
7632
|
+
segmentId,
|
|
7633
|
+
run: s.phoneticText[phoneticScriptId]
|
|
7634
|
+
};
|
|
7635
|
+
});
|
|
7636
|
+
return Object.entries(s["clip-entities"]).flatMap(([id, clip]) => {
|
|
7637
|
+
const selection = s["caption-entities"][clip.source]?.selection;
|
|
7638
|
+
const shown = s.captionText[clip.source];
|
|
7639
|
+
if (!selection) return [];
|
|
7640
|
+
return said.some((words) => {
|
|
7641
|
+
if (selection.audioScriptId !== words.audioScriptId) return false;
|
|
7642
|
+
if (!("segmentId" in words)) return true;
|
|
7643
|
+
if (selection.segmentId !== words.segmentId || !shown || !words.run) return false;
|
|
7644
|
+
return shown.start < words.run.end && words.run.start < shown.end;
|
|
7645
|
+
}) && requireTrack(s, trackOf(s, id)).role === "caption" ? [id] : [];
|
|
7646
|
+
});
|
|
7647
|
+
}
|
|
5228
7648
|
//#endregion
|
|
5229
7649
|
//#region src/dsl/dsl-editor.ts
|
|
5230
7650
|
/** Business edits write DSL facts in one audited transaction; layout is derived on read. */
|
|
@@ -5592,8 +8012,12 @@ var DslEditor = class {
|
|
|
5592
8012
|
* it; an unchanged one keeps its file. Recordings already spoken from it keep their derived-from.
|
|
5593
8013
|
*/
|
|
5594
8014
|
async setPhoneticScript(input, options) {
|
|
8015
|
+
this.dsl.assertWritable();
|
|
5595
8016
|
const id = input.phoneticScriptId ?? this.idFactory("phonetic-script");
|
|
5596
8017
|
const range = segmentRange(input);
|
|
8018
|
+
assertId(id);
|
|
8019
|
+
assertSegmentRange(range, requireSegment(this.dsl.getSnapshot(), range.audioScriptId, range.segmentId).text);
|
|
8020
|
+
assertPhonemeScript(input.phonemeScript);
|
|
5597
8021
|
const file = input.phonemeScript === void 0 ? {} : await this.fileFor(id, input.phonemeScript);
|
|
5598
8022
|
const exists = Object.hasOwn(this.dsl.getSnapshot()["phonetic-script-entities"], id);
|
|
5599
8023
|
const cleared = {
|
|
@@ -5743,132 +8167,6 @@ function createInitialMedeoDsl(options = {}) {
|
|
|
5743
8167
|
}
|
|
5744
8168
|
}
|
|
5745
8169
|
//#endregion
|
|
5746
|
-
//#region src/dsl/projection/audio-scripts.ts
|
|
5747
|
-
/**
|
|
5748
|
-
* The scripts a recording says. It follows derived-from from the recording; a PhoneticScript is a
|
|
5749
|
-
* variant, so its words are the base its phoneme names, and derived-from is not followed past it.
|
|
5750
|
-
* When nothing is found that way, a script read off the recording itself is what it says.
|
|
5751
|
-
*/
|
|
5752
|
-
function spokenScripts(snapshot, recordingId) {
|
|
5753
|
-
const relations = Object.values(snapshot["derived-from-relations"]);
|
|
5754
|
-
const found = /* @__PURE__ */ new Map();
|
|
5755
|
-
const seen = /* @__PURE__ */ new Set();
|
|
5756
|
-
const visit = (id, phoneticScriptId) => {
|
|
5757
|
-
if (seen.has(id)) return;
|
|
5758
|
-
seen.add(id);
|
|
5759
|
-
if (snapshot["audio-script-entities"][id]) {
|
|
5760
|
-
if (!found.has(id)) found.set(id, phoneticScriptId);
|
|
5761
|
-
return;
|
|
5762
|
-
}
|
|
5763
|
-
const phonetic = snapshot["phonetic-script-entities"][id];
|
|
5764
|
-
if (phonetic) {
|
|
5765
|
-
visit(phonetic.phoneme.audioScriptId, id);
|
|
5766
|
-
return;
|
|
5767
|
-
}
|
|
5768
|
-
for (const r of relations) if (r.entityId === id) visit(r.sourceId, phoneticScriptId);
|
|
5769
|
-
};
|
|
5770
|
-
visit(recordingId);
|
|
5771
|
-
if (!found.size) {
|
|
5772
|
-
for (const r of relations) if (r.sourceId === recordingId && snapshot["audio-script-entities"][r.entityId]) found.set(r.entityId, void 0);
|
|
5773
|
-
}
|
|
5774
|
-
return [...found].map(([audioScriptId, phoneticScriptId]) => phoneticScriptId === void 0 ? { audioScriptId } : {
|
|
5775
|
-
audioScriptId,
|
|
5776
|
-
phoneticScriptId
|
|
5777
|
-
});
|
|
5778
|
-
}
|
|
5779
|
-
/** Every AudioScript in the document with its PhoneticScripts, the Speech Clips saying it and the Caption Clips showing it. */
|
|
5780
|
-
function toAudioScriptViews(snapshot) {
|
|
5781
|
-
const views = /* @__PURE__ */ new Map();
|
|
5782
|
-
const segmentIndex = (scriptId, segmentId) => {
|
|
5783
|
-
const index = snapshot["audio-script-entities"][scriptId]?.segments.findIndex((s) => s.id === segmentId) ?? -1;
|
|
5784
|
-
return index < 0 ? void 0 : index;
|
|
5785
|
-
};
|
|
5786
|
-
for (const [id, script] of Object.entries(snapshot["audio-script-entities"])) views.set(id, {
|
|
5787
|
-
id,
|
|
5788
|
-
segments: script.segments.map((segment) => {
|
|
5789
|
-
const rich = snapshot.richText[segment.id];
|
|
5790
|
-
const language = rich ? markedLanguage(langsFromDelta(rich.delta)) : void 0;
|
|
5791
|
-
return language === void 0 ? {
|
|
5792
|
-
id: segment.id,
|
|
5793
|
-
text: segment.text
|
|
5794
|
-
} : {
|
|
5795
|
-
id: segment.id,
|
|
5796
|
-
text: segment.text,
|
|
5797
|
-
language
|
|
5798
|
-
};
|
|
5799
|
-
}),
|
|
5800
|
-
sourceMediaIds: [],
|
|
5801
|
-
phoneticScripts: [],
|
|
5802
|
-
speeches: [],
|
|
5803
|
-
captions: []
|
|
5804
|
-
});
|
|
5805
|
-
for (const r of Object.values(snapshot["derived-from-relations"])) if (snapshot["media-entities"][r.sourceId]) views.get(r.entityId)?.sourceMediaIds.push(r.sourceId);
|
|
5806
|
-
for (const [id, phonetic] of Object.entries(snapshot["phonetic-script-entities"])) {
|
|
5807
|
-
const { audioScriptId, segmentId, phonemeScript } = phonetic.phoneme;
|
|
5808
|
-
const { digest } = phonetic;
|
|
5809
|
-
const segment = segmentIndex(audioScriptId, segmentId);
|
|
5810
|
-
const text = snapshot.phoneticText[id]?.text;
|
|
5811
|
-
views.get(audioScriptId)?.phoneticScripts.push({
|
|
5812
|
-
id,
|
|
5813
|
-
...segment === void 0 ? {} : { segment },
|
|
5814
|
-
...text === void 0 ? {} : { text },
|
|
5815
|
-
...phonemeScript === void 0 ? {} : { phonemeScript },
|
|
5816
|
-
...digest === void 0 ? {} : { digest }
|
|
5817
|
-
});
|
|
5818
|
-
}
|
|
5819
|
-
const timbres = Object.values(snapshot["voice-timbre-relations"]);
|
|
5820
|
-
for (const track of resolveDslLayout(snapshot).tracks) for (const { clipId } of track.clips) {
|
|
5821
|
-
const source = snapshot["clip-entities"][clipId]?.source;
|
|
5822
|
-
if (!source) continue;
|
|
5823
|
-
if (snapshot["media-entities"][source]?.kind === "voiceover-recording") {
|
|
5824
|
-
const voices = timbres.filter((r) => r.recordingId === source).flatMap((r) => {
|
|
5825
|
-
const voice = snapshot["voice-entities"][r.voiceId];
|
|
5826
|
-
if (!voice) return [];
|
|
5827
|
-
return [voice.name === void 0 ? {
|
|
5828
|
-
voiceId: r.voiceId,
|
|
5829
|
-
key: voice.key
|
|
5830
|
-
} : {
|
|
5831
|
-
voiceId: r.voiceId,
|
|
5832
|
-
key: voice.key,
|
|
5833
|
-
name: voice.name
|
|
5834
|
-
}];
|
|
5835
|
-
});
|
|
5836
|
-
for (const spoken of spokenScripts(snapshot, source)) {
|
|
5837
|
-
const applied = appliedSpeech(snapshot, source, spoken);
|
|
5838
|
-
views.get(spoken.audioScriptId)?.speeches.push({
|
|
5839
|
-
clipId,
|
|
5840
|
-
recordingId: source,
|
|
5841
|
-
...spoken.phoneticScriptId === void 0 ? {} : { phoneticScriptId: spoken.phoneticScriptId },
|
|
5842
|
-
voices,
|
|
5843
|
-
...applied === void 0 ? {} : { applied }
|
|
5844
|
-
});
|
|
5845
|
-
}
|
|
5846
|
-
continue;
|
|
5847
|
-
}
|
|
5848
|
-
const caption = snapshot["caption-entities"][source];
|
|
5849
|
-
if (!caption?.selection || caption.text !== void 0 || !snapshot.captionText[source]) continue;
|
|
5850
|
-
const { audioScriptId, segmentId } = caption.selection;
|
|
5851
|
-
const segment = segmentIndex(audioScriptId, segmentId);
|
|
5852
|
-
if (segment !== void 0) views.get(audioScriptId)?.captions.push({
|
|
5853
|
-
clipId,
|
|
5854
|
-
captionId: source,
|
|
5855
|
-
segment
|
|
5856
|
-
});
|
|
5857
|
-
}
|
|
5858
|
-
return [...views.values()];
|
|
5859
|
-
}
|
|
5860
|
-
/**
|
|
5861
|
-
* Whether a recording still matches what it was synthesized from: the digest its derived-from
|
|
5862
|
-
* records against the source's file digest now. An AudioScript has no file, so a recording
|
|
5863
|
-
* derived from a script directly is not judged.
|
|
5864
|
-
*/
|
|
5865
|
-
function appliedSpeech(snapshot, recordingId, spoken) {
|
|
5866
|
-
const sourceId = spoken.phoneticScriptId ?? spoken.audioScriptId;
|
|
5867
|
-
const recorded = Object.values(snapshot["derived-from-relations"]).find((r) => r.entityId === recordingId && r.sourceId === sourceId)?.sourceDigest;
|
|
5868
|
-
const current = spoken.phoneticScriptId === void 0 ? void 0 : snapshot["phonetic-script-entities"][sourceId]?.digest;
|
|
5869
|
-
return recorded === void 0 || current === void 0 ? void 0 : recorded === current;
|
|
5870
|
-
}
|
|
5871
|
-
//#endregion
|
|
5872
8170
|
//#region src/dsl/projection/caption-style.ts
|
|
5873
8171
|
const fields = {
|
|
5874
8172
|
fontSize: "font_size",
|
|
@@ -5905,6 +8203,9 @@ function fromDraftStyle(style) {
|
|
|
5905
8203
|
};
|
|
5906
8204
|
}
|
|
5907
8205
|
//#endregion
|
|
8206
|
+
//#region src/dsl/projection/video-draft-types.ts
|
|
8207
|
+
const DEFAULT_UNIT_TIME_MS = 33.333;
|
|
8208
|
+
//#endregion
|
|
5908
8209
|
//#region src/dsl/projection/to-video-draft.ts
|
|
5909
8210
|
/** A content read view, not an archive of authoring identities, rich-text marks or provenance. */
|
|
5910
8211
|
function toVideoDraft(snapshot) {
|
|
@@ -5940,7 +8241,8 @@ function toVideoDraft(snapshot) {
|
|
|
5940
8241
|
} }
|
|
5941
8242
|
} };
|
|
5942
8243
|
} else if (track.role === "speech") {
|
|
5943
|
-
if (!source || source.kind !== "
|
|
8244
|
+
if (!source || source.kind !== "audio" && source.kind !== "voiceover-recording") fail("Speech playback requires an Audio or VoiceoverRecording");
|
|
8245
|
+
if (source.system !== "memota" && !(source.kind === "voiceover-recording" && source.system === "memota-speech")) fail("Speech playback requires a Memota audio file");
|
|
5944
8246
|
requireWholeAudio(source, clip, fail);
|
|
5945
8247
|
if (clip.fillTimeline) fail("Legacy Speech cannot express timeline fill");
|
|
5946
8248
|
const voices = Object.values(snapshot["voice-timbre-relations"]).filter((r) => r.recordingId === sourceId).flatMap((r) => snapshot["voice-entities"][r.voiceId] ? [snapshot["voice-entities"][r.voiceId]] : []);
|
|
@@ -5957,7 +8259,7 @@ function toVideoDraft(snapshot) {
|
|
|
5957
8259
|
audio_storage_key: source.storageKey,
|
|
5958
8260
|
origin_speech_id: source.key,
|
|
5959
8261
|
volume: clip.volume,
|
|
5960
|
-
audio_script: spokenText(snapshot, sourceId),
|
|
8262
|
+
audio_script: source.kind === "voiceover-recording" ? spokenText(snapshot, sourceId) : "",
|
|
5961
8263
|
voice: voice ? {
|
|
5962
8264
|
id: voice.key,
|
|
5963
8265
|
name: voice.name
|
|
@@ -6392,4 +8694,4 @@ function importedScript(id, text, mintSegment) {
|
|
|
6392
8694
|
};
|
|
6393
8695
|
}
|
|
6394
8696
|
//#endregion
|
|
6395
|
-
export { DslEditor as a,
|
|
8697
|
+
export { DslProjectionError as _, DslEditor as a, resourceId as b, UnsupportedDocumentFormatError as c, documentFormat as d, medeoDslLoroShape as f, resolveDslLayout as g, timeAnchorCodec as h, createInitialMedeoDsl as i, assertDslDocument as l, isInvalidAtomic as m, toVideoDraft as n, toAudioScriptViews as o, captionContentCodec as p, DEFAULT_UNIT_TIME_MS as r, MedeoDsl as s, fromVideoDraft as t, assertPhoneticBindingFormat as u, defaultIdFactory as v, tupleHash as x, relationId as y };
|