@dabble/patches 0.5.14 → 0.5.16

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.
@@ -24,9 +24,7 @@ const text = {
24
24
  doc = new Delta().insert("\n");
25
25
  }
26
26
  doc = doc.compose(delta);
27
- if (hasInvalidOps(doc)) {
28
- return "Invalid text delta provided for this text document";
29
- }
27
+ doc = fixBadDeltaDoc(doc);
30
28
  return replace.apply(state, path, doc);
31
29
  },
32
30
  transform(state, thisOp, otherOps) {
@@ -49,8 +47,29 @@ const text = {
49
47
  return new Delta(delta1).compose(new Delta(delta2));
50
48
  }
51
49
  };
52
- function hasInvalidOps(doc) {
53
- return doc.ops.some((op) => typeof op.insert !== "string" && (typeof op.insert !== "object" || op.insert === null));
50
+ function fixBadDeltaDoc(delta) {
51
+ while (delta.ops.length && delta.ops[delta.ops.length - 1].insert === void 0) {
52
+ delta.ops.pop();
53
+ }
54
+ const endsWithNewline = delta.ops.length > 0 && typeof delta.ops[delta.ops.length - 1].insert === "string" && delta.ops[delta.ops.length - 1].insert.endsWith("\n");
55
+ if (!endsWithNewline) {
56
+ delta.push({ insert: "\n" });
57
+ }
58
+ const hasNonInsertOps = delta.ops.some((op) => op.insert === void 0);
59
+ if (!hasNonInsertOps) {
60
+ return delta;
61
+ }
62
+ const newDelta = new Delta();
63
+ for (const op of delta.ops) {
64
+ if (op.insert !== void 0) {
65
+ newDelta.push(op);
66
+ } else if (op.retain) {
67
+ const insertOp = { insert: "".padStart(op.retain) };
68
+ if (op.attributes) insertOp.attributes = op.attributes;
69
+ newDelta.push(insertOp);
70
+ }
71
+ }
72
+ return newDelta;
54
73
  }
55
74
  export {
56
75
  text
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dabble/patches",
3
- "version": "0.5.14",
3
+ "version": "0.5.16",
4
4
  "description": "Immutable JSON Patch implementation based on RFC 6902 supporting operational transformation and last-writer-wins",
5
5
  "author": "Jacob Wright <jacwright@gmail.com>",
6
6
  "bugs": {