document-model 1.4.0 → 1.6.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 (47) 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/{node/cjs/internal/index-DxD7Ha1j.js → browser/cjs/internal/index-BImZF-Wk.js} +2 -2
  5. package/dist/browser/cjs/internal/index-BImZF-Wk.js.map +1 -0
  6. package/dist/browser/cjs/internal/{index-DdOJi6Ua.js → index-vlbA6Asd.js} +2 -2
  7. package/dist/browser/cjs/internal/{index-DdOJi6Ua.js.map → index-vlbA6Asd.js.map} +1 -1
  8. package/dist/browser/cjs/internal/{object-CRlmL8Nt.js → object-9Wvjprnm.js} +151 -124
  9. package/dist/browser/cjs/internal/object-9Wvjprnm.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-CuhiYn-8.js} +2 -2
  14. package/dist/browser/es/internal/{index-C1HKYMZA.js.map → index-CuhiYn-8.js.map} +1 -1
  15. package/dist/browser/es/internal/{index-BsGRYydP.js → index-Drp90r05.js} +2 -2
  16. package/dist/browser/es/internal/index-Drp90r05.js.map +1 -0
  17. package/dist/browser/es/internal/{object-B562qKhQ.js → object-CyAog_F_.js} +151 -124
  18. package/dist/browser/es/internal/object-CyAog_F_.js.map +1 -0
  19. package/dist/browser/src/document/utils/document-helpers.d.ts +1 -0
  20. package/dist/node/cjs/document-model.js +2 -2
  21. package/dist/node/cjs/document.js +2 -2
  22. package/dist/node/cjs/index.js +2 -2
  23. package/dist/{browser/cjs/internal/index-pAtJQrtD.js → node/cjs/internal/index-CJIU8iX-.js} +2 -2
  24. package/dist/node/cjs/internal/index-CJIU8iX-.js.map +1 -0
  25. package/dist/node/cjs/internal/{index-BWc0K1Dk.js → index-DWeC8dwW.js} +2 -2
  26. package/dist/node/cjs/internal/{index-BWc0K1Dk.js.map → index-DWeC8dwW.js.map} +1 -1
  27. package/dist/node/cjs/internal/{object-_zq9luMS.js → object-op6YzhH1.js} +152 -125
  28. package/dist/node/cjs/internal/object-op6YzhH1.js.map +1 -0
  29. package/dist/node/es/document-model.js +2 -2
  30. package/dist/node/es/document.js +2 -2
  31. package/dist/node/es/index.js +2 -2
  32. package/dist/node/es/internal/{index-D8WTToX1.js → index-BNAE-_Hg.js} +2 -2
  33. package/dist/node/es/internal/{index-D8WTToX1.js.map → index-BNAE-_Hg.js.map} +1 -1
  34. package/dist/node/es/internal/{index-DkkBVgni.js → index-B_7z_4EV.js} +2 -2
  35. package/dist/node/es/internal/index-B_7z_4EV.js.map +1 -0
  36. package/dist/node/es/internal/{object-D3jjs7tr.js → object-BuK9PFjs.js} +152 -125
  37. package/dist/node/es/internal/object-BuK9PFjs.js.map +1 -0
  38. package/dist/node/src/document/utils/document-helpers.d.ts +1 -0
  39. package/package.json +1 -1
  40. package/dist/browser/cjs/internal/index-pAtJQrtD.js.map +0 -1
  41. package/dist/browser/cjs/internal/object-CRlmL8Nt.js.map +0 -1
  42. package/dist/browser/es/internal/index-BsGRYydP.js.map +0 -1
  43. package/dist/browser/es/internal/object-B562qKhQ.js.map +0 -1
  44. package/dist/node/cjs/internal/index-DxD7Ha1j.js.map +0 -1
  45. package/dist/node/cjs/internal/object-_zq9luMS.js.map +0 -1
  46. package/dist/node/es/internal/index-DkkBVgni.js.map +0 -1
  47. 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-CyAog_F_.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-Drp90r05.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-Drp90r05.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;
@@ -3507,6 +3387,22 @@ function filterDuplicatedOperations(targetOperations, sourceOperations) {
3507
3387
  return true;
3508
3388
  });
3509
3389
  }
3390
+ function filterDocumentOperationsResultingState(documentOperations) {
3391
+ if (!documentOperations) {
3392
+ return {};
3393
+ }
3394
+ const entries = Object.entries(documentOperations);
3395
+ return entries.reduce(
3396
+ (acc, [scope, operations]) => ({
3397
+ ...acc,
3398
+ [scope]: operations.map((op) => {
3399
+ const { resultingState, ...restProps } = op;
3400
+ return restProps;
3401
+ })
3402
+ }),
3403
+ {}
3404
+ );
3405
+ }
3510
3406
  const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
3511
3407
  __proto__: null,
3512
3408
  IntegrityIssueSubType,
@@ -3515,6 +3411,7 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
3515
3411
  attachBranch,
3516
3412
  checkCleanedOperationsIntegrity,
3517
3413
  checkOperationsIntegrity,
3414
+ filterDocumentOperationsResultingState,
3518
3415
  filterDuplicatedOperations,
3519
3416
  garbageCollect,
3520
3417
  garbageCollectDocumentOperations,
@@ -3531,6 +3428,136 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
3531
3428
  sortOperations: sortOperations$1,
3532
3429
  split
3533
3430
  }, Symbol.toStringTag, { value: "Module" }));
3431
+ const createZip = async (document) => {
3432
+ const zip = new JSZip();
3433
+ const { name, revision, documentType, created, lastModified } = document;
3434
+ const header = {
3435
+ name,
3436
+ revision,
3437
+ documentType,
3438
+ created,
3439
+ lastModified
3440
+ };
3441
+ zip.file("header.json", JSON.stringify(header, null, 2));
3442
+ zip.file(
3443
+ "state.json",
3444
+ JSON.stringify(document.initialState || {}, null, 2)
3445
+ );
3446
+ zip.file(
3447
+ "operations.json",
3448
+ JSON.stringify(
3449
+ filterDocumentOperationsResultingState(document.operations),
3450
+ null,
3451
+ 2
3452
+ )
3453
+ );
3454
+ if (document.attachments) {
3455
+ const attachments = Object.keys(document.attachments);
3456
+ attachments.forEach((key) => {
3457
+ const { data, ...attributes } = document.attachments[key];
3458
+ zip.file(key, data, {
3459
+ base64: true,
3460
+ createFolders: true,
3461
+ comment: JSON.stringify(attributes)
3462
+ });
3463
+ });
3464
+ }
3465
+ return zip;
3466
+ };
3467
+ const saveToFile = async (document, path, extension, name) => {
3468
+ const zip = await createZip(document);
3469
+ await zip.generateAsync({
3470
+ type: "uint8array",
3471
+ streamFiles: true
3472
+ });
3473
+ const fileName = name ?? document.name;
3474
+ const fileExtension = `.${extension}.zip`;
3475
+ return writeFile(
3476
+ path,
3477
+ fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`
3478
+ );
3479
+ };
3480
+ const saveToFileHandle = async (document, input) => {
3481
+ const zip = await createZip(document);
3482
+ const blob = await zip.generateAsync({ type: "blob" });
3483
+ const writable = await input.createWritable();
3484
+ await writable.write(blob);
3485
+ await writable.close();
3486
+ };
3487
+ const loadFromFile = async (path, reducer, options) => {
3488
+ const file = readFile();
3489
+ return loadFromInput(file, reducer, options);
3490
+ };
3491
+ const loadFromInput = async (input, reducer, options) => {
3492
+ const zip = new JSZip();
3493
+ await zip.loadAsync(input);
3494
+ return loadFromZip(zip, reducer, options);
3495
+ };
3496
+ async function loadFromZip(zip, reducer, options) {
3497
+ const initialStateZip = zip.file("state.json");
3498
+ if (!initialStateZip) {
3499
+ throw new Error("Initial state not found");
3500
+ }
3501
+ const initialStateStr = await initialStateZip.async("string");
3502
+ const initialState = JSON.parse(initialStateStr);
3503
+ const headerZip = zip.file("header.json");
3504
+ let header = void 0;
3505
+ if (headerZip) {
3506
+ header = JSON.parse(await headerZip.async("string"));
3507
+ }
3508
+ const operationsZip = zip.file("operations.json");
3509
+ if (!operationsZip) {
3510
+ throw new Error("Operations history not found");
3511
+ }
3512
+ const operations = JSON.parse(
3513
+ await operationsZip.async("string")
3514
+ );
3515
+ const clearedOperations = garbageCollectDocumentOperations(operations);
3516
+ const operationsError = validateOperations(clearedOperations);
3517
+ if (operationsError.length) {
3518
+ const errorMessages = operationsError.map((err) => err.message);
3519
+ throw new Error(errorMessages.join("\n"));
3520
+ }
3521
+ let result = replayDocument(
3522
+ initialState,
3523
+ clearedOperations,
3524
+ reducer,
3525
+ void 0,
3526
+ header,
3527
+ {},
3528
+ options
3529
+ );
3530
+ if (header) {
3531
+ result = {
3532
+ ...result,
3533
+ ...header
3534
+ };
3535
+ }
3536
+ return result;
3537
+ }
3538
+ function getFileAttributes(file) {
3539
+ const extension = file.replace(/^.*\./, "") || void 0;
3540
+ const fileName = file.replace(/^.*[/\\]/, "") || void 0;
3541
+ return { extension, fileName };
3542
+ }
3543
+ async function getRemoteFile(url) {
3544
+ const { buffer: buffer2, mimeType = "application/octet-stream" } = await fetchFile();
3545
+ const attributes = getFileAttributes(url);
3546
+ const data = buffer2.toString("base64");
3547
+ return {
3548
+ data,
3549
+ hash: hash(data),
3550
+ mimeType,
3551
+ ...attributes
3552
+ };
3553
+ }
3554
+ async function getLocalFile(path) {
3555
+ const buffer2 = await getFile();
3556
+ const mimeType = mime.getType(path) || "application/octet-stream";
3557
+ const attributes = getFileAttributes(path);
3558
+ const data = buffer2.toString("base64");
3559
+ return { data, hash: hash(data), mimeType, ...attributes };
3560
+ }
3534
3561
  function getNextRevision(document, action) {
3535
3562
  let latestOperation;
3536
3563
  if ("index" in action) {
@@ -4331,4 +4358,4 @@ export {
4331
4358
  readOnly as y,
4332
4359
  replayDocument as z
4333
4360
  };
4334
- //# sourceMappingURL=object-B562qKhQ.js.map
4361
+ //# sourceMappingURL=object-CyAog_F_.js.map