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.
- package/dist/browser/cjs/document-model.js +2 -2
- package/dist/browser/cjs/document.js +2 -2
- package/dist/browser/cjs/index.js +2 -2
- package/dist/{node/cjs/internal/index-DxD7Ha1j.js → browser/cjs/internal/index-BImZF-Wk.js} +2 -2
- package/dist/browser/cjs/internal/index-BImZF-Wk.js.map +1 -0
- package/dist/browser/cjs/internal/{index-DdOJi6Ua.js → index-vlbA6Asd.js} +2 -2
- package/dist/browser/cjs/internal/{index-DdOJi6Ua.js.map → index-vlbA6Asd.js.map} +1 -1
- package/dist/browser/cjs/internal/{object-CRlmL8Nt.js → object-9Wvjprnm.js} +151 -124
- package/dist/browser/cjs/internal/object-9Wvjprnm.js.map +1 -0
- package/dist/browser/es/document-model.js +2 -2
- package/dist/browser/es/document.js +2 -2
- package/dist/browser/es/index.js +2 -2
- package/dist/browser/es/internal/{index-C1HKYMZA.js → index-CuhiYn-8.js} +2 -2
- package/dist/browser/es/internal/{index-C1HKYMZA.js.map → index-CuhiYn-8.js.map} +1 -1
- package/dist/browser/es/internal/{index-BsGRYydP.js → index-Drp90r05.js} +2 -2
- package/dist/browser/es/internal/index-Drp90r05.js.map +1 -0
- package/dist/browser/es/internal/{object-B562qKhQ.js → object-CyAog_F_.js} +151 -124
- package/dist/browser/es/internal/object-CyAog_F_.js.map +1 -0
- package/dist/browser/src/document/utils/document-helpers.d.ts +1 -0
- package/dist/node/cjs/document-model.js +2 -2
- package/dist/node/cjs/document.js +2 -2
- package/dist/node/cjs/index.js +2 -2
- package/dist/{browser/cjs/internal/index-pAtJQrtD.js → node/cjs/internal/index-CJIU8iX-.js} +2 -2
- package/dist/node/cjs/internal/index-CJIU8iX-.js.map +1 -0
- package/dist/node/cjs/internal/{index-BWc0K1Dk.js → index-DWeC8dwW.js} +2 -2
- package/dist/node/cjs/internal/{index-BWc0K1Dk.js.map → index-DWeC8dwW.js.map} +1 -1
- package/dist/node/cjs/internal/{object-_zq9luMS.js → object-op6YzhH1.js} +152 -125
- package/dist/node/cjs/internal/object-op6YzhH1.js.map +1 -0
- package/dist/node/es/document-model.js +2 -2
- package/dist/node/es/document.js +2 -2
- package/dist/node/es/index.js +2 -2
- package/dist/node/es/internal/{index-D8WTToX1.js → index-BNAE-_Hg.js} +2 -2
- package/dist/node/es/internal/{index-D8WTToX1.js.map → index-BNAE-_Hg.js.map} +1 -1
- package/dist/node/es/internal/{index-DkkBVgni.js → index-B_7z_4EV.js} +2 -2
- package/dist/node/es/internal/index-B_7z_4EV.js.map +1 -0
- package/dist/node/es/internal/{object-D3jjs7tr.js → object-BuK9PFjs.js} +152 -125
- package/dist/node/es/internal/object-BuK9PFjs.js.map +1 -0
- package/dist/node/src/document/utils/document-helpers.d.ts +1 -0
- package/package.json +1 -1
- package/dist/browser/cjs/internal/index-pAtJQrtD.js.map +0 -1
- package/dist/browser/cjs/internal/object-CRlmL8Nt.js.map +0 -1
- package/dist/browser/es/internal/index-BsGRYydP.js.map +0 -1
- package/dist/browser/es/internal/object-B562qKhQ.js.map +0 -1
- package/dist/node/cjs/internal/index-DxD7Ha1j.js.map +0 -1
- package/dist/node/cjs/internal/object-_zq9luMS.js.map +0 -1
- package/dist/node/es/internal/index-DkkBVgni.js.map +0 -1
- 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-
|
|
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-BuK9PFjs.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-
|
|
50
|
+
//# sourceMappingURL=index-B_7z_4EV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-B_7z_4EV.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
|
-
|
|
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;
|
|
@@ -1509,6 +1388,22 @@ function filterDuplicatedOperations(targetOperations, sourceOperations) {
|
|
|
1509
1388
|
return true;
|
|
1510
1389
|
});
|
|
1511
1390
|
}
|
|
1391
|
+
function filterDocumentOperationsResultingState(documentOperations) {
|
|
1392
|
+
if (!documentOperations) {
|
|
1393
|
+
return {};
|
|
1394
|
+
}
|
|
1395
|
+
const entries = Object.entries(documentOperations);
|
|
1396
|
+
return entries.reduce(
|
|
1397
|
+
(acc, [scope, operations]) => ({
|
|
1398
|
+
...acc,
|
|
1399
|
+
[scope]: operations.map((op) => {
|
|
1400
|
+
const { resultingState, ...restProps } = op;
|
|
1401
|
+
return restProps;
|
|
1402
|
+
})
|
|
1403
|
+
}),
|
|
1404
|
+
{}
|
|
1405
|
+
);
|
|
1406
|
+
}
|
|
1512
1407
|
const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
1513
1408
|
__proto__: null,
|
|
1514
1409
|
IntegrityIssueSubType,
|
|
@@ -1517,6 +1412,7 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
|
|
|
1517
1412
|
attachBranch,
|
|
1518
1413
|
checkCleanedOperationsIntegrity,
|
|
1519
1414
|
checkOperationsIntegrity,
|
|
1415
|
+
filterDocumentOperationsResultingState,
|
|
1520
1416
|
filterDuplicatedOperations,
|
|
1521
1417
|
garbageCollect,
|
|
1522
1418
|
garbageCollectDocumentOperations,
|
|
@@ -1533,6 +1429,137 @@ const documentHelpers = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.def
|
|
|
1533
1429
|
sortOperations: sortOperations$1,
|
|
1534
1430
|
split
|
|
1535
1431
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
1432
|
+
const createZip = async (document) => {
|
|
1433
|
+
const zip = new JSZip();
|
|
1434
|
+
const { name, revision, documentType, created, lastModified } = document;
|
|
1435
|
+
const header = {
|
|
1436
|
+
name,
|
|
1437
|
+
revision,
|
|
1438
|
+
documentType,
|
|
1439
|
+
created,
|
|
1440
|
+
lastModified
|
|
1441
|
+
};
|
|
1442
|
+
zip.file("header.json", JSON.stringify(header, null, 2));
|
|
1443
|
+
zip.file(
|
|
1444
|
+
"state.json",
|
|
1445
|
+
JSON.stringify(document.initialState || {}, null, 2)
|
|
1446
|
+
);
|
|
1447
|
+
zip.file(
|
|
1448
|
+
"operations.json",
|
|
1449
|
+
JSON.stringify(
|
|
1450
|
+
filterDocumentOperationsResultingState(document.operations),
|
|
1451
|
+
null,
|
|
1452
|
+
2
|
|
1453
|
+
)
|
|
1454
|
+
);
|
|
1455
|
+
if (document.attachments) {
|
|
1456
|
+
const attachments = Object.keys(document.attachments);
|
|
1457
|
+
attachments.forEach((key) => {
|
|
1458
|
+
const { data, ...attributes } = document.attachments[key];
|
|
1459
|
+
zip.file(key, data, {
|
|
1460
|
+
base64: true,
|
|
1461
|
+
createFolders: true,
|
|
1462
|
+
comment: JSON.stringify(attributes)
|
|
1463
|
+
});
|
|
1464
|
+
});
|
|
1465
|
+
}
|
|
1466
|
+
return zip;
|
|
1467
|
+
};
|
|
1468
|
+
const saveToFile = async (document, path, extension, name) => {
|
|
1469
|
+
const zip = await createZip(document);
|
|
1470
|
+
const file = await zip.generateAsync({
|
|
1471
|
+
type: "uint8array",
|
|
1472
|
+
streamFiles: true
|
|
1473
|
+
});
|
|
1474
|
+
const fileName = name ?? document.name;
|
|
1475
|
+
const fileExtension = `.${extension}.zip`;
|
|
1476
|
+
return writeFile(
|
|
1477
|
+
path,
|
|
1478
|
+
fileName.endsWith(fileExtension) ? fileName : `${fileName}${fileExtension}`,
|
|
1479
|
+
file
|
|
1480
|
+
);
|
|
1481
|
+
};
|
|
1482
|
+
const saveToFileHandle = async (document, input) => {
|
|
1483
|
+
const zip = await createZip(document);
|
|
1484
|
+
const blob = await zip.generateAsync({ type: "blob" });
|
|
1485
|
+
const writable = await input.createWritable();
|
|
1486
|
+
await writable.write(blob);
|
|
1487
|
+
await writable.close();
|
|
1488
|
+
};
|
|
1489
|
+
const loadFromFile = async (path, reducer, options) => {
|
|
1490
|
+
const file = readFile(path);
|
|
1491
|
+
return loadFromInput(file, reducer, options);
|
|
1492
|
+
};
|
|
1493
|
+
const loadFromInput = async (input, reducer, options) => {
|
|
1494
|
+
const zip = new JSZip();
|
|
1495
|
+
await zip.loadAsync(input);
|
|
1496
|
+
return loadFromZip(zip, reducer, options);
|
|
1497
|
+
};
|
|
1498
|
+
async function loadFromZip(zip, reducer, options) {
|
|
1499
|
+
const initialStateZip = zip.file("state.json");
|
|
1500
|
+
if (!initialStateZip) {
|
|
1501
|
+
throw new Error("Initial state not found");
|
|
1502
|
+
}
|
|
1503
|
+
const initialStateStr = await initialStateZip.async("string");
|
|
1504
|
+
const initialState = JSON.parse(initialStateStr);
|
|
1505
|
+
const headerZip = zip.file("header.json");
|
|
1506
|
+
let header = void 0;
|
|
1507
|
+
if (headerZip) {
|
|
1508
|
+
header = JSON.parse(await headerZip.async("string"));
|
|
1509
|
+
}
|
|
1510
|
+
const operationsZip = zip.file("operations.json");
|
|
1511
|
+
if (!operationsZip) {
|
|
1512
|
+
throw new Error("Operations history not found");
|
|
1513
|
+
}
|
|
1514
|
+
const operations = JSON.parse(
|
|
1515
|
+
await operationsZip.async("string")
|
|
1516
|
+
);
|
|
1517
|
+
const clearedOperations = garbageCollectDocumentOperations(operations);
|
|
1518
|
+
const operationsError = validateOperations(clearedOperations);
|
|
1519
|
+
if (operationsError.length) {
|
|
1520
|
+
const errorMessages = operationsError.map((err) => err.message);
|
|
1521
|
+
throw new Error(errorMessages.join("\n"));
|
|
1522
|
+
}
|
|
1523
|
+
let result = replayDocument(
|
|
1524
|
+
initialState,
|
|
1525
|
+
clearedOperations,
|
|
1526
|
+
reducer,
|
|
1527
|
+
void 0,
|
|
1528
|
+
header,
|
|
1529
|
+
{},
|
|
1530
|
+
options
|
|
1531
|
+
);
|
|
1532
|
+
if (header) {
|
|
1533
|
+
result = {
|
|
1534
|
+
...result,
|
|
1535
|
+
...header
|
|
1536
|
+
};
|
|
1537
|
+
}
|
|
1538
|
+
return result;
|
|
1539
|
+
}
|
|
1540
|
+
function getFileAttributes(file) {
|
|
1541
|
+
const extension = file.replace(/^.*\./, "") || void 0;
|
|
1542
|
+
const fileName = file.replace(/^.*[/\\]/, "") || void 0;
|
|
1543
|
+
return { extension, fileName };
|
|
1544
|
+
}
|
|
1545
|
+
async function getRemoteFile(url) {
|
|
1546
|
+
const { buffer, mimeType = "application/octet-stream" } = await fetchFile(url);
|
|
1547
|
+
const attributes = getFileAttributes(url);
|
|
1548
|
+
const data = buffer.toString("base64");
|
|
1549
|
+
return {
|
|
1550
|
+
data,
|
|
1551
|
+
hash: hash(data),
|
|
1552
|
+
mimeType,
|
|
1553
|
+
...attributes
|
|
1554
|
+
};
|
|
1555
|
+
}
|
|
1556
|
+
async function getLocalFile(path) {
|
|
1557
|
+
const buffer = await getFile(path);
|
|
1558
|
+
const mimeType = mime.getType(path) || "application/octet-stream";
|
|
1559
|
+
const attributes = getFileAttributes(path);
|
|
1560
|
+
const data = buffer.toString("base64");
|
|
1561
|
+
return { data, hash: hash(data), mimeType, ...attributes };
|
|
1562
|
+
}
|
|
1536
1563
|
function getNextRevision(document, action) {
|
|
1537
1564
|
let latestOperation;
|
|
1538
1565
|
if ("index" in action) {
|
|
@@ -2333,4 +2360,4 @@ export {
|
|
|
2333
2360
|
readOnly as y,
|
|
2334
2361
|
replayDocument as z
|
|
2335
2362
|
};
|
|
2336
|
-
//# sourceMappingURL=object-
|
|
2363
|
+
//# sourceMappingURL=object-BuK9PFjs.js.map
|