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-D3jjs7tr.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-Cu2Dz-iF.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-DkkBVgni.js.map
50
+ //# sourceMappingURL=index-NWZKlhKu.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-NWZKlhKu.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1060,8 +1060,10 @@ function validateOperations(operations) {
1060
1060
  const scopes = Object.keys(operations);
1061
1061
  for (const scope of scopes) {
1062
1062
  const ops = operations[scope].sort((a, b) => a.index - b.index);
1063
+ let opIndex = -1;
1063
1064
  for (let i = 0; i < ops.length; i++) {
1064
- if (ops[i].index !== i) {
1065
+ opIndex = opIndex + 1 + ops[i].skip;
1066
+ if (ops[i].index !== opIndex) {
1065
1067
  errors.push({
1066
1068
  message: `Invalid operation index ${ops[i].index} at position ${i}`,
1067
1069
  details: {
@@ -1075,129 +1077,6 @@ function validateOperations(operations) {
1075
1077
  }
1076
1078
  return errors;
1077
1079
  }
1078
- const createZip = async (document) => {
1079
- const zip = new JSZip();
1080
- const { name, revision, documentType, created, lastModified } = document;
1081
- const header = {
1082
- name,
1083
- revision,
1084
- documentType,
1085
- created,
1086
- lastModified
1087
- };
1088
- zip.file("header.json", JSON.stringify(header, null, 2));
1089
- zip.file(
1090
- "state.json",
1091
- JSON.stringify(document.initialState || {}, null, 2)
1092
- );
1093
- zip.file("operations.json", JSON.stringify(document.operations, null, 2));
1094
- if (document.attachments) {
1095
- const attachments = Object.keys(document.attachments);
1096
- attachments.forEach((key) => {
1097
- const { data, ...attributes } = document.attachments[key];
1098
- zip.file(key, data, {
1099
- base64: true,
1100
- createFolders: true,
1101
- comment: JSON.stringify(attributes)
1102
- });
1103
- });
1104
- }
1105
- return zip;
1106
- };
1107
- const saveToFile = async (document, path, extension, name) => {
1108
- const zip = await createZip(document);
1109
- const file = await zip.generateAsync({
1110
- type: "uint8array",
1111
- streamFiles: true
1112
- });
1113
- const fileName = name ?? document.name;
1114
- const fileExtension = `.${extension}.zip`;
1115
- return writeFile(
1116
- path,
1117
- fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`,
1118
- file
1119
- );
1120
- };
1121
- const saveToFileHandle = async (document, input) => {
1122
- const zip = await createZip(document);
1123
- const blob = await zip.generateAsync({ type: "blob" });
1124
- const writable = await input.createWritable();
1125
- await writable.write(blob);
1126
- await writable.close();
1127
- };
1128
- const loadFromFile = async (path, reducer, options) => {
1129
- const file = readFile(path);
1130
- return loadFromInput(file, reducer, options);
1131
- };
1132
- const loadFromInput = async (input, reducer, options) => {
1133
- const zip = new JSZip();
1134
- await zip.loadAsync(input);
1135
- return loadFromZip(zip, reducer, options);
1136
- };
1137
- async function loadFromZip(zip, reducer, options) {
1138
- const initialStateZip = zip.file("state.json");
1139
- if (!initialStateZip) {
1140
- throw new Error("Initial state not found");
1141
- }
1142
- const initialStateStr = await initialStateZip.async("string");
1143
- const initialState = JSON.parse(initialStateStr);
1144
- const headerZip = zip.file("header.json");
1145
- let header = void 0;
1146
- if (headerZip) {
1147
- header = JSON.parse(await headerZip.async("string"));
1148
- }
1149
- const operationsZip = zip.file("operations.json");
1150
- if (!operationsZip) {
1151
- throw new Error("Operations history not found");
1152
- }
1153
- const operations = JSON.parse(
1154
- await operationsZip.async("string")
1155
- );
1156
- const operationsError = validateOperations(operations);
1157
- if (operationsError.length) {
1158
- const errorMessages = operationsError.map((err) => err.message);
1159
- throw new Error(errorMessages.join("\n"));
1160
- }
1161
- let result = replayDocument(
1162
- initialState,
1163
- operations,
1164
- reducer,
1165
- void 0,
1166
- header,
1167
- {},
1168
- options
1169
- );
1170
- if (header) {
1171
- result = {
1172
- ...result,
1173
- ...header
1174
- };
1175
- }
1176
- return result;
1177
- }
1178
- function getFileAttributes(file) {
1179
- const extension = file.replace(/^.*\./, "") || void 0;
1180
- const fileName = file.replace(/^.*[/\\]/, "") || void 0;
1181
- return { extension, fileName };
1182
- }
1183
- async function getRemoteFile(url) {
1184
- const { buffer, mimeType = "application/octet-stream" } = await fetchFile(url);
1185
- const attributes = getFileAttributes(url);
1186
- const data = buffer.toString("base64");
1187
- return {
1188
- data,
1189
- hash: hash(data),
1190
- mimeType,
1191
- ...attributes
1192
- };
1193
- }
1194
- async function getLocalFile(path) {
1195
- const buffer = await getFile(path);
1196
- const mimeType = mime.getType(path) || "application/octet-stream";
1197
- const attributes = getFileAttributes(path);
1198
- const data = buffer.toString("base64");
1199
- return { data, hash: hash(data), mimeType, ...attributes };
1200
- }
1201
1080
  var IntegrityIssueType = /* @__PURE__ */ ((IntegrityIssueType2) => {
1202
1081
  IntegrityIssueType2["UNEXPECTED_INDEX"] = "UNEXPECTED_INDEX";
1203
1082
  return IntegrityIssueType2;
@@ -1533,6 +1412,130 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
1533
1412
  sortOperations: sortOperations$1,
1534
1413
  split
1535
1414
  }, Symbol.toStringTag, { value: "Module" }));
1415
+ const createZip = async (document) => {
1416
+ const zip = new JSZip();
1417
+ const { name, revision, documentType, created, lastModified } = document;
1418
+ const header = {
1419
+ name,
1420
+ revision,
1421
+ documentType,
1422
+ created,
1423
+ lastModified
1424
+ };
1425
+ zip.file("header.json", JSON.stringify(header, null, 2));
1426
+ zip.file(
1427
+ "state.json",
1428
+ JSON.stringify(document.initialState || {}, null, 2)
1429
+ );
1430
+ zip.file("operations.json", JSON.stringify(document.operations, null, 2));
1431
+ if (document.attachments) {
1432
+ const attachments = Object.keys(document.attachments);
1433
+ attachments.forEach((key) => {
1434
+ const { data, ...attributes } = document.attachments[key];
1435
+ zip.file(key, data, {
1436
+ base64: true,
1437
+ createFolders: true,
1438
+ comment: JSON.stringify(attributes)
1439
+ });
1440
+ });
1441
+ }
1442
+ return zip;
1443
+ };
1444
+ const saveToFile = async (document, path, extension, name) => {
1445
+ const zip = await createZip(document);
1446
+ const file = await zip.generateAsync({
1447
+ type: "uint8array",
1448
+ streamFiles: true
1449
+ });
1450
+ const fileName = name ?? document.name;
1451
+ const fileExtension = `.${extension}.zip`;
1452
+ return writeFile(
1453
+ path,
1454
+ fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`,
1455
+ file
1456
+ );
1457
+ };
1458
+ const saveToFileHandle = async (document, input) => {
1459
+ const zip = await createZip(document);
1460
+ const blob = await zip.generateAsync({ type: "blob" });
1461
+ const writable = await input.createWritable();
1462
+ await writable.write(blob);
1463
+ await writable.close();
1464
+ };
1465
+ const loadFromFile = async (path, reducer, options) => {
1466
+ const file = readFile(path);
1467
+ return loadFromInput(file, reducer, options);
1468
+ };
1469
+ const loadFromInput = async (input, reducer, options) => {
1470
+ const zip = new JSZip();
1471
+ await zip.loadAsync(input);
1472
+ return loadFromZip(zip, reducer, options);
1473
+ };
1474
+ async function loadFromZip(zip, reducer, options) {
1475
+ const initialStateZip = zip.file("state.json");
1476
+ if (!initialStateZip) {
1477
+ throw new Error("Initial state not found");
1478
+ }
1479
+ const initialStateStr = await initialStateZip.async("string");
1480
+ const initialState = JSON.parse(initialStateStr);
1481
+ const headerZip = zip.file("header.json");
1482
+ let header = void 0;
1483
+ if (headerZip) {
1484
+ header = JSON.parse(await headerZip.async("string"));
1485
+ }
1486
+ const operationsZip = zip.file("operations.json");
1487
+ if (!operationsZip) {
1488
+ throw new Error("Operations history not found");
1489
+ }
1490
+ const operations = JSON.parse(
1491
+ await operationsZip.async("string")
1492
+ );
1493
+ const clearedOperations = garbageCollectDocumentOperations(operations);
1494
+ const operationsError = validateOperations(clearedOperations);
1495
+ if (operationsError.length) {
1496
+ const errorMessages = operationsError.map((err) => err.message);
1497
+ throw new Error(errorMessages.join("\n"));
1498
+ }
1499
+ let result = replayDocument(
1500
+ initialState,
1501
+ clearedOperations,
1502
+ reducer,
1503
+ void 0,
1504
+ header,
1505
+ {},
1506
+ options
1507
+ );
1508
+ if (header) {
1509
+ result = {
1510
+ ...result,
1511
+ ...header
1512
+ };
1513
+ }
1514
+ return result;
1515
+ }
1516
+ function getFileAttributes(file) {
1517
+ const extension = file.replace(/^.*\./, "") || void 0;
1518
+ const fileName = file.replace(/^.*[/\\]/, "") || void 0;
1519
+ return { extension, fileName };
1520
+ }
1521
+ async function getRemoteFile(url) {
1522
+ const { buffer, mimeType = "application/octet-stream" } = await fetchFile(url);
1523
+ const attributes = getFileAttributes(url);
1524
+ const data = buffer.toString("base64");
1525
+ return {
1526
+ data,
1527
+ hash: hash(data),
1528
+ mimeType,
1529
+ ...attributes
1530
+ };
1531
+ }
1532
+ async function getLocalFile(path) {
1533
+ const buffer = await getFile(path);
1534
+ const mimeType = mime.getType(path) || "application/octet-stream";
1535
+ const attributes = getFileAttributes(path);
1536
+ const data = buffer.toString("base64");
1537
+ return { data, hash: hash(data), mimeType, ...attributes };
1538
+ }
1536
1539
  function getNextRevision(document, action) {
1537
1540
  let latestOperation;
1538
1541
  if ("index" in action) {
@@ -2333,4 +2336,4 @@ export {
2333
2336
  readOnly as y,
2334
2337
  replayDocument as z
2335
2338
  };
2336
- //# sourceMappingURL=object-D3jjs7tr.js.map
2339
+ //# sourceMappingURL=object-Cu2Dz-iF.js.map