document-model 1.4.0 → 1.5.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.
Files changed (44) hide show
  1. package/dist/browser/cjs/document-model.js +2 -2
  2. package/dist/browser/cjs/document.js +2 -2
  3. package/dist/browser/cjs/index.js +2 -2
  4. package/dist/browser/cjs/internal/{index-DdOJi6Ua.js → index-BoiSedJ2.js} +2 -2
  5. package/dist/browser/cjs/internal/{index-DdOJi6Ua.js.map → index-BoiSedJ2.js.map} +1 -1
  6. package/dist/{node/cjs/internal/index-DxD7Ha1j.js → browser/cjs/internal/index-frw-qprx.js} +2 -2
  7. package/dist/browser/cjs/internal/index-frw-qprx.js.map +1 -0
  8. package/dist/browser/cjs/internal/{object-CRlmL8Nt.js → object-DldjPOTT.js} +127 -124
  9. package/dist/browser/cjs/internal/object-DldjPOTT.js.map +1 -0
  10. package/dist/browser/es/document-model.js +2 -2
  11. package/dist/browser/es/document.js +2 -2
  12. package/dist/browser/es/index.js +2 -2
  13. package/dist/browser/es/internal/{index-C1HKYMZA.js → index-B7idSyw8.js} +2 -2
  14. package/dist/browser/es/internal/{index-C1HKYMZA.js.map → index-B7idSyw8.js.map} +1 -1
  15. package/dist/browser/es/internal/{index-BsGRYydP.js → index-D2kKzCXD.js} +2 -2
  16. package/dist/browser/es/internal/index-D2kKzCXD.js.map +1 -0
  17. package/dist/browser/es/internal/{object-B562qKhQ.js → object-I6vKsK3r.js} +127 -124
  18. package/dist/browser/{cjs/internal/object-CRlmL8Nt.js.map → es/internal/object-I6vKsK3r.js.map} +1 -1
  19. package/dist/node/cjs/document-model.js +2 -2
  20. package/dist/node/cjs/document.js +2 -2
  21. package/dist/node/cjs/index.js +2 -2
  22. package/dist/{browser/cjs/internal/index-pAtJQrtD.js → node/cjs/internal/index-RX6RcsNg.js} +2 -2
  23. package/dist/node/cjs/internal/index-RX6RcsNg.js.map +1 -0
  24. package/dist/node/cjs/internal/{index-BWc0K1Dk.js → index-WVYoLyly.js} +2 -2
  25. package/dist/node/cjs/internal/{index-BWc0K1Dk.js.map → index-WVYoLyly.js.map} +1 -1
  26. package/dist/node/cjs/internal/{object-_zq9luMS.js → object-7GplcYr4.js} +128 -125
  27. package/dist/node/cjs/internal/object-7GplcYr4.js.map +1 -0
  28. package/dist/node/es/document-model.js +2 -2
  29. package/dist/node/es/document.js +2 -2
  30. package/dist/node/es/index.js +2 -2
  31. package/dist/node/es/internal/{index-D8WTToX1.js → index-BFRJ-0Rp.js} +2 -2
  32. package/dist/node/es/internal/{index-D8WTToX1.js.map → index-BFRJ-0Rp.js.map} +1 -1
  33. package/dist/node/es/internal/{index-DkkBVgni.js → index-NWZKlhKu.js} +2 -2
  34. package/dist/node/es/internal/index-NWZKlhKu.js.map +1 -0
  35. package/dist/node/es/internal/{object-D3jjs7tr.js → object-Cu2Dz-iF.js} +128 -125
  36. package/dist/node/es/internal/object-Cu2Dz-iF.js.map +1 -0
  37. package/package.json +1 -1
  38. package/dist/browser/cjs/internal/index-pAtJQrtD.js.map +0 -1
  39. package/dist/browser/es/internal/index-BsGRYydP.js.map +0 -1
  40. package/dist/browser/es/internal/object-B562qKhQ.js.map +0 -1
  41. package/dist/node/cjs/internal/index-DxD7Ha1j.js.map +0 -1
  42. package/dist/node/cjs/internal/object-_zq9luMS.js.map +0 -1
  43. package/dist/node/es/internal/index-DkkBVgni.js.map +0 -1
  44. package/dist/node/es/internal/object-D3jjs7tr.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import { j as calculateSkipsLeft, f as createAction, b as createDocument, a as createExtendedState, c as createReducer, k as createUnsafeReducer, m as createZip, n as documentHelpers, o as getLocalFile, p as getRemoteFile, q as hashDocument, r as hashKey, i as isBaseAction, t as isNoopOperation, u as isSameDocument, v as isUndoRedo, l as loadFromFile, e as loadFromInput, w as mapSkippedOperations, x as parseResultingState, y as readOnly, z as replayDocument, A as replayOperations, s as saveToFile, d as saveToFileHandle, C as sortMappedOperations, D as sortOperations, E as validateOperations, B as BaseDocument, h as BaseActions, g as applyMixins, F as baseReducer, G as mutableBaseReducer, H as processUndoRedo, I as updateDocument, J as updateHeader, K as zod } from "./object-B562qKhQ.js";
1
+ import { j as calculateSkipsLeft, f as createAction, b as createDocument, a as createExtendedState, c as createReducer, k as createUnsafeReducer, m as createZip, n as documentHelpers, o as getLocalFile, p as getRemoteFile, q as hashDocument, r as hashKey, i as isBaseAction, t as isNoopOperation, u as isSameDocument, v as isUndoRedo, l as loadFromFile, e as loadFromInput, w as mapSkippedOperations, x as parseResultingState, y as readOnly, z as replayDocument, A as replayOperations, s as saveToFile, d as saveToFileHandle, C as sortMappedOperations, D as sortOperations, E as validateOperations, B as BaseDocument, h as BaseActions, g as applyMixins, F as baseReducer, G as mutableBaseReducer, H as processUndoRedo, I as updateDocument, J as updateHeader, K as zod } from "./object-I6vKsK3r.js";
2
2
  const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
3
3
  __proto__: null,
4
4
  calculateSkipsLeft,
@@ -47,4 +47,4 @@ export {
47
47
  Document as D,
48
48
  index as i
49
49
  };
50
- //# sourceMappingURL=index-BsGRYydP.js.map
50
+ //# sourceMappingURL=index-D2kKzCXD.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-D2kKzCXD.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -3059,8 +3059,10 @@ function validateOperations(operations) {
3059
3059
  const scopes = Object.keys(operations);
3060
3060
  for (const scope of scopes) {
3061
3061
  const ops = operations[scope].sort((a, b) => a.index - b.index);
3062
+ let opIndex = -1;
3062
3063
  for (let i = 0; i < ops.length; i++) {
3063
- if (ops[i].index !== i) {
3064
+ opIndex = opIndex + 1 + ops[i].skip;
3065
+ if (ops[i].index !== opIndex) {
3064
3066
  errors.push({
3065
3067
  message: `Invalid operation index ${ops[i].index} at position ${i}`,
3066
3068
  details: {
@@ -3074,128 +3076,6 @@ function validateOperations(operations) {
3074
3076
  }
3075
3077
  return errors;
3076
3078
  }
3077
- const createZip = async (document) => {
3078
- const zip = new JSZip();
3079
- const { name, revision, documentType, created, lastModified } = document;
3080
- const header = {
3081
- name,
3082
- revision,
3083
- documentType,
3084
- created,
3085
- lastModified
3086
- };
3087
- zip.file("header.json", JSON.stringify(header, null, 2));
3088
- zip.file(
3089
- "state.json",
3090
- JSON.stringify(document.initialState || {}, null, 2)
3091
- );
3092
- zip.file("operations.json", JSON.stringify(document.operations, null, 2));
3093
- if (document.attachments) {
3094
- const attachments = Object.keys(document.attachments);
3095
- attachments.forEach((key) => {
3096
- const { data, ...attributes } = document.attachments[key];
3097
- zip.file(key, data, {
3098
- base64: true,
3099
- createFolders: true,
3100
- comment: JSON.stringify(attributes)
3101
- });
3102
- });
3103
- }
3104
- return zip;
3105
- };
3106
- const saveToFile = async (document, path, extension, name) => {
3107
- const zip = await createZip(document);
3108
- await zip.generateAsync({
3109
- type: "uint8array",
3110
- streamFiles: true
3111
- });
3112
- const fileName = name ?? document.name;
3113
- const fileExtension = `.${extension}.zip`;
3114
- return writeFile(
3115
- path,
3116
- fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`
3117
- );
3118
- };
3119
- const saveToFileHandle = async (document, input) => {
3120
- const zip = await createZip(document);
3121
- const blob = await zip.generateAsync({ type: "blob" });
3122
- const writable = await input.createWritable();
3123
- await writable.write(blob);
3124
- await writable.close();
3125
- };
3126
- const loadFromFile = async (path, reducer, options) => {
3127
- const file = readFile();
3128
- return loadFromInput(file, reducer, options);
3129
- };
3130
- const loadFromInput = async (input, reducer, options) => {
3131
- const zip = new JSZip();
3132
- await zip.loadAsync(input);
3133
- return loadFromZip(zip, reducer, options);
3134
- };
3135
- async function loadFromZip(zip, reducer, options) {
3136
- const initialStateZip = zip.file("state.json");
3137
- if (!initialStateZip) {
3138
- throw new Error("Initial state not found");
3139
- }
3140
- const initialStateStr = await initialStateZip.async("string");
3141
- const initialState = JSON.parse(initialStateStr);
3142
- const headerZip = zip.file("header.json");
3143
- let header = void 0;
3144
- if (headerZip) {
3145
- header = JSON.parse(await headerZip.async("string"));
3146
- }
3147
- const operationsZip = zip.file("operations.json");
3148
- if (!operationsZip) {
3149
- throw new Error("Operations history not found");
3150
- }
3151
- const operations = JSON.parse(
3152
- await operationsZip.async("string")
3153
- );
3154
- const operationsError = validateOperations(operations);
3155
- if (operationsError.length) {
3156
- const errorMessages = operationsError.map((err) => err.message);
3157
- throw new Error(errorMessages.join("\n"));
3158
- }
3159
- let result = replayDocument(
3160
- initialState,
3161
- operations,
3162
- reducer,
3163
- void 0,
3164
- header,
3165
- {},
3166
- options
3167
- );
3168
- if (header) {
3169
- result = {
3170
- ...result,
3171
- ...header
3172
- };
3173
- }
3174
- return result;
3175
- }
3176
- function getFileAttributes(file) {
3177
- const extension = file.replace(/^.*\./, "") || void 0;
3178
- const fileName = file.replace(/^.*[/\\]/, "") || void 0;
3179
- return { extension, fileName };
3180
- }
3181
- async function getRemoteFile(url) {
3182
- const { buffer: buffer2, mimeType = "application/octet-stream" } = await fetchFile();
3183
- const attributes = getFileAttributes(url);
3184
- const data = buffer2.toString("base64");
3185
- return {
3186
- data,
3187
- hash: hash(data),
3188
- mimeType,
3189
- ...attributes
3190
- };
3191
- }
3192
- async function getLocalFile(path) {
3193
- const buffer2 = await getFile();
3194
- const mimeType = mime.getType(path) || "application/octet-stream";
3195
- const attributes = getFileAttributes(path);
3196
- const data = buffer2.toString("base64");
3197
- return { data, hash: hash(data), mimeType, ...attributes };
3198
- }
3199
3079
  var IntegrityIssueType = /* @__PURE__ */ ((IntegrityIssueType2) => {
3200
3080
  IntegrityIssueType2["UNEXPECTED_INDEX"] = "UNEXPECTED_INDEX";
3201
3081
  return IntegrityIssueType2;
@@ -3531,6 +3411,129 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
3531
3411
  sortOperations: sortOperations$1,
3532
3412
  split
3533
3413
  }, Symbol.toStringTag, { value: "Module" }));
3414
+ const createZip = async (document) => {
3415
+ const zip = new JSZip();
3416
+ const { name, revision, documentType, created, lastModified } = document;
3417
+ const header = {
3418
+ name,
3419
+ revision,
3420
+ documentType,
3421
+ created,
3422
+ lastModified
3423
+ };
3424
+ zip.file("header.json", JSON.stringify(header, null, 2));
3425
+ zip.file(
3426
+ "state.json",
3427
+ JSON.stringify(document.initialState || {}, null, 2)
3428
+ );
3429
+ zip.file("operations.json", JSON.stringify(document.operations, null, 2));
3430
+ if (document.attachments) {
3431
+ const attachments = Object.keys(document.attachments);
3432
+ attachments.forEach((key) => {
3433
+ const { data, ...attributes } = document.attachments[key];
3434
+ zip.file(key, data, {
3435
+ base64: true,
3436
+ createFolders: true,
3437
+ comment: JSON.stringify(attributes)
3438
+ });
3439
+ });
3440
+ }
3441
+ return zip;
3442
+ };
3443
+ const saveToFile = async (document, path, extension, name) => {
3444
+ const zip = await createZip(document);
3445
+ await zip.generateAsync({
3446
+ type: "uint8array",
3447
+ streamFiles: true
3448
+ });
3449
+ const fileName = name ?? document.name;
3450
+ const fileExtension = `.${extension}.zip`;
3451
+ return writeFile(
3452
+ path,
3453
+ fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`
3454
+ );
3455
+ };
3456
+ const saveToFileHandle = async (document, input) => {
3457
+ const zip = await createZip(document);
3458
+ const blob = await zip.generateAsync({ type: "blob" });
3459
+ const writable = await input.createWritable();
3460
+ await writable.write(blob);
3461
+ await writable.close();
3462
+ };
3463
+ const loadFromFile = async (path, reducer, options) => {
3464
+ const file = readFile();
3465
+ return loadFromInput(file, reducer, options);
3466
+ };
3467
+ const loadFromInput = async (input, reducer, options) => {
3468
+ const zip = new JSZip();
3469
+ await zip.loadAsync(input);
3470
+ return loadFromZip(zip, reducer, options);
3471
+ };
3472
+ async function loadFromZip(zip, reducer, options) {
3473
+ const initialStateZip = zip.file("state.json");
3474
+ if (!initialStateZip) {
3475
+ throw new Error("Initial state not found");
3476
+ }
3477
+ const initialStateStr = await initialStateZip.async("string");
3478
+ const initialState = JSON.parse(initialStateStr);
3479
+ const headerZip = zip.file("header.json");
3480
+ let header = void 0;
3481
+ if (headerZip) {
3482
+ header = JSON.parse(await headerZip.async("string"));
3483
+ }
3484
+ const operationsZip = zip.file("operations.json");
3485
+ if (!operationsZip) {
3486
+ throw new Error("Operations history not found");
3487
+ }
3488
+ const operations = JSON.parse(
3489
+ await operationsZip.async("string")
3490
+ );
3491
+ const clearedOperations = garbageCollectDocumentOperations(operations);
3492
+ const operationsError = validateOperations(clearedOperations);
3493
+ if (operationsError.length) {
3494
+ const errorMessages = operationsError.map((err) => err.message);
3495
+ throw new Error(errorMessages.join("\n"));
3496
+ }
3497
+ let result = replayDocument(
3498
+ initialState,
3499
+ clearedOperations,
3500
+ reducer,
3501
+ void 0,
3502
+ header,
3503
+ {},
3504
+ options
3505
+ );
3506
+ if (header) {
3507
+ result = {
3508
+ ...result,
3509
+ ...header
3510
+ };
3511
+ }
3512
+ return result;
3513
+ }
3514
+ function getFileAttributes(file) {
3515
+ const extension = file.replace(/^.*\./, "") || void 0;
3516
+ const fileName = file.replace(/^.*[/\\]/, "") || void 0;
3517
+ return { extension, fileName };
3518
+ }
3519
+ async function getRemoteFile(url) {
3520
+ const { buffer: buffer2, mimeType = "application/octet-stream" } = await fetchFile();
3521
+ const attributes = getFileAttributes(url);
3522
+ const data = buffer2.toString("base64");
3523
+ return {
3524
+ data,
3525
+ hash: hash(data),
3526
+ mimeType,
3527
+ ...attributes
3528
+ };
3529
+ }
3530
+ async function getLocalFile(path) {
3531
+ const buffer2 = await getFile();
3532
+ const mimeType = mime.getType(path) || "application/octet-stream";
3533
+ const attributes = getFileAttributes(path);
3534
+ const data = buffer2.toString("base64");
3535
+ return { data, hash: hash(data), mimeType, ...attributes };
3536
+ }
3534
3537
  function getNextRevision(document, action) {
3535
3538
  let latestOperation;
3536
3539
  if ("index" in action) {
@@ -4331,4 +4334,4 @@ export {
4331
4334
  readOnly as y,
4332
4335
  replayDocument as z
4333
4336
  };
4334
- //# sourceMappingURL=object-B562qKhQ.js.map
4337
+ //# sourceMappingURL=object-I6vKsK3r.js.map