@portabletext/editor 1.0.2 → 1.0.3
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/README.md +9 -2
- package/lib/index.d.mts +22 -97
- package/lib/index.d.ts +22 -97
- package/lib/index.esm.js +1065 -1377
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +1128 -1430
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +1065 -1377
- package/lib/index.mjs.map +1 -1
- package/package.json +40 -22
- package/src/editor/Editable.tsx +32 -38
- package/src/editor/components/Synchronizer.tsx +1 -1
- package/src/editor/plugins/__tests__/createWithInsertData.test.tsx +166 -0
- package/src/editor/plugins/createWithInsertData.ts +34 -1
- package/src/editor/plugins/createWithPatches.ts +2 -2
- package/src/editor/plugins/createWithUndoRedo.ts +1 -1
- package/src/index.ts +1 -2
- package/src/types/editor.ts +4 -3
- package/src/utils/__tests__/dmpToOperations.test.ts +1 -1
- package/src/utils/applyPatch.ts +8 -8
- package/src/utils/operationToPatches.ts +2 -2
- package/src/utils/validateValue.ts +1 -1
- package/src/patch/PatchEvent.ts +0 -33
- package/src/patch/applyPatch.ts +0 -29
- package/src/patch/array.ts +0 -89
- package/src/patch/arrayInsert.ts +0 -27
- package/src/patch/object.ts +0 -39
- package/src/patch/patches.ts +0 -53
- package/src/patch/primitive.ts +0 -43
- package/src/patch/string.ts +0 -51
- package/src/types/patch.ts +0 -65
- package/src/utils/patches.ts +0 -36
package/lib/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$l = require("debug"), types = require("@sanity/types"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), schema = require("@sanity/schema"),
|
|
3
|
+
var jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$l = require("debug"), types$1 = require("@sanity/types"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), schema = require("@sanity/schema"), patchEvent = require("@portabletext/patches/patch-event"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), apply$1 = require("@portabletext/patches/apply"), flatten = require("lodash/flatten.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), content = require("@sanity/util/content"), debounce = require("lodash/debounce.js"), types = require("@portabletext/patches/types");
|
|
4
4
|
function _interopDefaultCompat(e) {
|
|
5
5
|
return e && typeof e == "object" && "default" in e ? e : { default: e };
|
|
6
6
|
}
|
|
7
|
-
var isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$l), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy),
|
|
7
|
+
var isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$l), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject), throttle__default = /* @__PURE__ */ _interopDefaultCompat(throttle), debounce__default = /* @__PURE__ */ _interopDefaultCompat(debounce);
|
|
8
8
|
const rootName = "sanity-pte:";
|
|
9
9
|
debug__default.default(rootName);
|
|
10
10
|
function debugWithName(name) {
|
|
@@ -31,7 +31,7 @@ function createArrayedPath(point, editor) {
|
|
|
31
31
|
const [block, blockPath] = Array.from(
|
|
32
32
|
slate.Editor.nodes(editor, {
|
|
33
33
|
at: [],
|
|
34
|
-
match: (n) => types.isKeySegment(point.path[0]) && n._key === point.path[0]._key
|
|
34
|
+
match: (n) => types$1.isKeySegment(point.path[0]) && n._key === point.path[0]._key
|
|
35
35
|
})
|
|
36
36
|
)[0] || [void 0, void 0];
|
|
37
37
|
if (!block || !slate.Element.isElement(block))
|
|
@@ -701,1062 +701,240 @@ function compileType(rawType) {
|
|
|
701
701
|
types: [rawType]
|
|
702
702
|
}).get(rawType.name);
|
|
703
703
|
}
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
function
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
const text1Length = text1.length, text2Length = text2.length;
|
|
719
|
-
if (text1Length === 0 || text2Length === 0)
|
|
720
|
-
return 0;
|
|
721
|
-
text1Length > text2Length ? text1 = text1.substring(text1Length - text2Length) : text1Length < text2Length && (text2 = text2.substring(0, text1Length));
|
|
722
|
-
const textLength = Math.min(text1Length, text2Length);
|
|
723
|
-
if (text1 === text2)
|
|
724
|
-
return textLength;
|
|
725
|
-
let best = 0, length = 1;
|
|
726
|
-
for (let found = 0; found !== -1; ) {
|
|
727
|
-
const pattern = text1.substring(textLength - length);
|
|
728
|
-
if (found = text2.indexOf(pattern), found === -1)
|
|
729
|
-
return best;
|
|
730
|
-
length += found, (found === 0 || text1.substring(textLength - length) === text2.substring(0, length)) && (best = length, length++);
|
|
731
|
-
}
|
|
732
|
-
return best;
|
|
733
|
-
}
|
|
734
|
-
function getCommonPrefix(text1, text2) {
|
|
735
|
-
if (!text1 || !text2 || text1[0] !== text2[0])
|
|
736
|
-
return 0;
|
|
737
|
-
let pointerMin = 0, pointerMax = Math.min(text1.length, text2.length), pointerMid = pointerMax, pointerStart = 0;
|
|
738
|
-
for (; pointerMin < pointerMid; )
|
|
739
|
-
text1.substring(pointerStart, pointerMid) === text2.substring(pointerStart, pointerMid) ? (pointerMin = pointerMid, pointerStart = pointerMin) : pointerMax = pointerMid, pointerMid = Math.floor((pointerMax - pointerMin) / 2 + pointerMin);
|
|
740
|
-
return pointerMid;
|
|
741
|
-
}
|
|
742
|
-
function getCommonSuffix(text1, text2) {
|
|
743
|
-
if (!text1 || !text2 || text1[text1.length - 1] !== text2[text2.length - 1])
|
|
744
|
-
return 0;
|
|
745
|
-
let pointerMin = 0, pointerMax = Math.min(text1.length, text2.length), pointerMid = pointerMax, pointerEnd = 0;
|
|
746
|
-
for (; pointerMin < pointerMid; )
|
|
747
|
-
text1.substring(text1.length - pointerMid, text1.length - pointerEnd) === text2.substring(text2.length - pointerMid, text2.length - pointerEnd) ? (pointerMin = pointerMid, pointerEnd = pointerMin) : pointerMax = pointerMid, pointerMid = Math.floor((pointerMax - pointerMin) / 2 + pointerMin);
|
|
748
|
-
return pointerMid;
|
|
749
|
-
}
|
|
750
|
-
function cleanupSemantic(rawDiffs) {
|
|
751
|
-
let diffs = rawDiffs.map((diff2) => cloneDiff(diff2)), hasChanges = !1;
|
|
752
|
-
const equalities = [];
|
|
753
|
-
let equalitiesLength = 0, lastEquality = null, pointer = 0, lengthInsertions1 = 0, lengthDeletions1 = 0, lengthInsertions2 = 0, lengthDeletions2 = 0;
|
|
754
|
-
for (; pointer < diffs.length; )
|
|
755
|
-
diffs[pointer][0] === DIFF_EQUAL ? (equalities[equalitiesLength++] = pointer, lengthInsertions1 = lengthInsertions2, lengthDeletions1 = lengthDeletions2, lengthInsertions2 = 0, lengthDeletions2 = 0, lastEquality = diffs[pointer][1]) : (diffs[pointer][0] === DIFF_INSERT ? lengthInsertions2 += diffs[pointer][1].length : lengthDeletions2 += diffs[pointer][1].length, lastEquality && lastEquality.length <= Math.max(lengthInsertions1, lengthDeletions1) && lastEquality.length <= Math.max(lengthInsertions2, lengthDeletions2) && (diffs.splice(equalities[equalitiesLength - 1], 0, [DIFF_DELETE, lastEquality]), diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT, equalitiesLength--, equalitiesLength--, pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1, lengthInsertions1 = 0, lengthDeletions1 = 0, lengthInsertions2 = 0, lengthDeletions2 = 0, lastEquality = null, hasChanges = !0)), pointer++;
|
|
756
|
-
for (hasChanges && (diffs = cleanupMerge(diffs)), diffs = cleanupSemanticLossless(diffs), pointer = 1; pointer < diffs.length; ) {
|
|
757
|
-
if (diffs[pointer - 1][0] === DIFF_DELETE && diffs[pointer][0] === DIFF_INSERT) {
|
|
758
|
-
const deletion = diffs[pointer - 1][1], insertion = diffs[pointer][1], overlapLength1 = getCommonOverlap(deletion, insertion), overlapLength2 = getCommonOverlap(insertion, deletion);
|
|
759
|
-
overlapLength1 >= overlapLength2 ? (overlapLength1 >= deletion.length / 2 || overlapLength1 >= insertion.length / 2) && (diffs.splice(pointer, 0, [DIFF_EQUAL, insertion.substring(0, overlapLength1)]), diffs[pointer - 1][1] = deletion.substring(0, deletion.length - overlapLength1), diffs[pointer + 1][1] = insertion.substring(overlapLength1), pointer++) : (overlapLength2 >= deletion.length / 2 || overlapLength2 >= insertion.length / 2) && (diffs.splice(pointer, 0, [DIFF_EQUAL, deletion.substring(0, overlapLength2)]), diffs[pointer - 1][0] = DIFF_INSERT, diffs[pointer - 1][1] = insertion.substring(0, insertion.length - overlapLength2), diffs[pointer + 1][0] = DIFF_DELETE, diffs[pointer + 1][1] = deletion.substring(overlapLength2), pointer++), pointer++;
|
|
760
|
-
}
|
|
761
|
-
pointer++;
|
|
704
|
+
const debug$j = debugWithName("operationToPatches");
|
|
705
|
+
debug$j.enabled = !1;
|
|
706
|
+
function createOperationToPatches(types2) {
|
|
707
|
+
const textBlockName = types2.block.name;
|
|
708
|
+
function insertTextPatch(editor, operation, beforeValue) {
|
|
709
|
+
debug$j.enabled && debug$j("Operation", JSON.stringify(operation, null, 2));
|
|
710
|
+
const block = editor.isTextBlock(editor.children[operation.path[0]]) && editor.children[operation.path[0]];
|
|
711
|
+
if (!block)
|
|
712
|
+
throw new Error("Could not find block");
|
|
713
|
+
const textChild = editor.isTextBlock(block) && editor.isTextSpan(block.children[operation.path[1]]) && block.children[operation.path[1]];
|
|
714
|
+
if (!textChild)
|
|
715
|
+
throw new Error("Could not find child");
|
|
716
|
+
const path = [{ _key: block._key }, "children", { _key: textChild._key }, "text"], prevBlock = beforeValue[operation.path[0]], prevChild = editor.isTextBlock(prevBlock) && prevBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevChild) ? prevChild.text : "", patch = patchEvent.diffMatchPatch(prevText, textChild.text, path);
|
|
717
|
+
return patch.value.length ? [patch] : [];
|
|
762
718
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
719
|
+
function removeTextPatch(editor, operation, beforeValue) {
|
|
720
|
+
const block = editor && editor.children[operation.path[0]];
|
|
721
|
+
if (!block)
|
|
722
|
+
throw new Error("Could not find block");
|
|
723
|
+
const child = editor.isTextBlock(block) && block.children[operation.path[1]] || void 0, textChild = editor.isTextSpan(child) ? child : void 0;
|
|
724
|
+
if (child && !textChild)
|
|
725
|
+
throw new Error("Expected span");
|
|
726
|
+
if (!textChild)
|
|
727
|
+
throw new Error("Could not find child");
|
|
728
|
+
const path = [{ _key: block._key }, "children", { _key: textChild._key }, "text"], beforeBlock = beforeValue[operation.path[0]], prevTextChild = editor.isTextBlock(beforeBlock) && beforeBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevTextChild) && prevTextChild.text, patch = patchEvent.diffMatchPatch(prevText || "", textChild.text, path);
|
|
729
|
+
return patch.value ? [patch] : [];
|
|
773
730
|
}
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
731
|
+
function setNodePatch(editor, operation) {
|
|
732
|
+
if (operation.path.length === 1) {
|
|
733
|
+
const block = editor.children[operation.path[0]];
|
|
734
|
+
if (typeof block._key != "string")
|
|
735
|
+
throw new Error("Expected block to have a _key");
|
|
736
|
+
const setNode = omitBy__default.default(
|
|
737
|
+
{ ...editor.children[operation.path[0]], ...operation.newProperties },
|
|
738
|
+
isUndefined__default.default
|
|
739
|
+
);
|
|
740
|
+
return [patchEvent.set(fromSlateValue([setNode], textBlockName)[0], [{ _key: block._key }])];
|
|
741
|
+
} else if (operation.path.length === 2) {
|
|
742
|
+
const block = editor.children[operation.path[0]];
|
|
743
|
+
if (editor.isTextBlock(block)) {
|
|
744
|
+
const child = block.children[operation.path[1]];
|
|
745
|
+
if (child) {
|
|
746
|
+
const blockKey = block._key, childKey = child._key, patches = [], keys = Object.keys(operation.newProperties);
|
|
747
|
+
return keys.forEach((keyName) => {
|
|
748
|
+
if (keys.length === 1 && keyName === "_key") {
|
|
749
|
+
const val = get__default.default(operation.newProperties, keyName);
|
|
750
|
+
patches.push(
|
|
751
|
+
patchEvent.set(val, [{ _key: blockKey }, "children", block.children.indexOf(child), keyName])
|
|
752
|
+
);
|
|
753
|
+
} else {
|
|
754
|
+
const val = get__default.default(operation.newProperties, keyName);
|
|
755
|
+
patches.push(patchEvent.set(val, [{ _key: blockKey }, "children", { _key: childKey }, keyName]));
|
|
756
|
+
}
|
|
757
|
+
}), patches;
|
|
758
|
+
}
|
|
759
|
+
throw new Error("Could not find a valid child");
|
|
788
760
|
}
|
|
789
|
-
|
|
790
|
-
}
|
|
791
|
-
|
|
761
|
+
throw new Error("Could not find a valid block");
|
|
762
|
+
} else
|
|
763
|
+
throw new Error(`Unexpected path encountered: ${JSON.stringify(operation.path)}`);
|
|
792
764
|
}
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
765
|
+
function insertNodePatch(editor, operation, beforeValue) {
|
|
766
|
+
const block = beforeValue[operation.path[0]], isTextBlock = editor.isTextBlock(block);
|
|
767
|
+
if (operation.path.length === 1) {
|
|
768
|
+
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block == null ? void 0 : block._key : beforeBlock == null ? void 0 : beforeBlock._key;
|
|
769
|
+
return targetKey ? [
|
|
770
|
+
patchEvent.insert([fromSlateValue([operation.node], textBlockName)[0]], position, [
|
|
771
|
+
{ _key: targetKey }
|
|
772
|
+
])
|
|
773
|
+
] : [
|
|
774
|
+
patchEvent.setIfMissing(beforeValue, []),
|
|
775
|
+
patchEvent.insert([fromSlateValue([operation.node], textBlockName)[0]], "before", [
|
|
776
|
+
operation.path[0]
|
|
777
|
+
])
|
|
778
|
+
];
|
|
779
|
+
} else if (isTextBlock && operation.path.length === 2 && editor.children[operation.path[0]]) {
|
|
780
|
+
const position = block.children.length === 0 || !block.children[operation.path[1] - 1] ? "before" : "after", node = { ...operation.node };
|
|
781
|
+
!node._type && slate.Text.isText(node) && (node._type = "span", node.marks = []);
|
|
782
|
+
const child = fromSlateValue(
|
|
783
|
+
[
|
|
784
|
+
{
|
|
785
|
+
_key: "bogus",
|
|
786
|
+
_type: textBlockName,
|
|
787
|
+
children: [node]
|
|
788
|
+
}
|
|
789
|
+
],
|
|
790
|
+
textBlockName
|
|
791
|
+
)[0].children[0];
|
|
792
|
+
return [
|
|
793
|
+
patchEvent.insert([child], position, [
|
|
794
|
+
{ _key: block._key },
|
|
795
|
+
"children",
|
|
796
|
+
block.children.length <= 1 || !block.children[operation.path[1] - 1] ? 0 : { _key: block.children[operation.path[1] - 1]._key }
|
|
797
|
+
])
|
|
798
|
+
];
|
|
812
799
|
}
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
}
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
for (let x = 0; x < vLength; x++)
|
|
835
|
-
v1[x] = -1, v2[x] = -1;
|
|
836
|
-
v1[vOffset + 1] = 0, v2[vOffset + 1] = 0;
|
|
837
|
-
const delta = text1Length - text2Length, front = delta % 2 !== 0;
|
|
838
|
-
let k1start = 0, k1end = 0, k2start = 0, k2end = 0;
|
|
839
|
-
for (let d = 0; d < maxD && !(Date.now() > deadline); d++) {
|
|
840
|
-
for (let k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
|
|
841
|
-
const k1Offset = vOffset + k1;
|
|
842
|
-
let x1;
|
|
843
|
-
k1 === -d || k1 !== d && v1[k1Offset - 1] < v1[k1Offset + 1] ? x1 = v1[k1Offset + 1] : x1 = v1[k1Offset - 1] + 1;
|
|
844
|
-
let y1 = x1 - k1;
|
|
845
|
-
for (; x1 < text1Length && y1 < text2Length && text1.charAt(x1) === text2.charAt(y1); )
|
|
846
|
-
x1++, y1++;
|
|
847
|
-
if (v1[k1Offset] = x1, x1 > text1Length)
|
|
848
|
-
k1end += 2;
|
|
849
|
-
else if (y1 > text2Length)
|
|
850
|
-
k1start += 2;
|
|
851
|
-
else if (front) {
|
|
852
|
-
const k2Offset = vOffset + delta - k1;
|
|
853
|
-
if (k2Offset >= 0 && k2Offset < vLength && v2[k2Offset] !== -1) {
|
|
854
|
-
const x2 = text1Length - v2[k2Offset];
|
|
855
|
-
if (x1 >= x2)
|
|
856
|
-
return bisectSplit(text1, text2, x1, y1, deadline);
|
|
857
|
-
}
|
|
800
|
+
return debug$j("Something was inserted into a void block. Not producing editor patches."), [];
|
|
801
|
+
}
|
|
802
|
+
function splitNodePatch(editor, operation, beforeValue) {
|
|
803
|
+
const patches = [], splitBlock = editor.children[operation.path[0]];
|
|
804
|
+
if (!editor.isTextBlock(splitBlock))
|
|
805
|
+
throw new Error(
|
|
806
|
+
`Block with path ${JSON.stringify(
|
|
807
|
+
operation.path[0]
|
|
808
|
+
)} is not a text block and can't be split`
|
|
809
|
+
);
|
|
810
|
+
if (operation.path.length === 1) {
|
|
811
|
+
const oldBlock = beforeValue[operation.path[0]];
|
|
812
|
+
if (editor.isTextBlock(oldBlock)) {
|
|
813
|
+
const targetValue = fromSlateValue(
|
|
814
|
+
[editor.children[operation.path[0] + 1]],
|
|
815
|
+
textBlockName
|
|
816
|
+
)[0];
|
|
817
|
+
targetValue && (patches.push(patchEvent.insert([targetValue], "after", [{ _key: splitBlock._key }])), oldBlock.children.slice(operation.position).forEach((span) => {
|
|
818
|
+
const path = [{ _key: oldBlock._key }, "children", { _key: span._key }];
|
|
819
|
+
patches.push(patchEvent.unset(path));
|
|
820
|
+
}));
|
|
858
821
|
}
|
|
822
|
+
return patches;
|
|
859
823
|
}
|
|
860
|
-
|
|
861
|
-
const
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
824
|
+
if (operation.path.length === 2) {
|
|
825
|
+
const splitSpan = splitBlock.children[operation.path[1]];
|
|
826
|
+
if (editor.isTextSpan(splitSpan)) {
|
|
827
|
+
const targetSpans = fromSlateValue(
|
|
828
|
+
[
|
|
829
|
+
{
|
|
830
|
+
...splitBlock,
|
|
831
|
+
children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
|
|
832
|
+
}
|
|
833
|
+
],
|
|
834
|
+
textBlockName
|
|
835
|
+
)[0].children;
|
|
836
|
+
patches.push(
|
|
837
|
+
patchEvent.insert(targetSpans, "after", [
|
|
838
|
+
{ _key: splitBlock._key },
|
|
839
|
+
"children",
|
|
840
|
+
{ _key: splitSpan._key }
|
|
841
|
+
])
|
|
842
|
+
), patches.push(
|
|
843
|
+
patchEvent.set(splitSpan.text, [
|
|
844
|
+
{ _key: splitBlock._key },
|
|
845
|
+
"children",
|
|
846
|
+
{ _key: splitSpan._key },
|
|
847
|
+
"text"
|
|
848
|
+
])
|
|
849
|
+
);
|
|
878
850
|
}
|
|
851
|
+
return patches;
|
|
879
852
|
}
|
|
853
|
+
return patches;
|
|
880
854
|
}
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
}
|
|
893
|
-
function findHalfMatch(text1, text2) {
|
|
894
|
-
if ((arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1) <= 0)
|
|
895
|
-
return null;
|
|
896
|
-
const longText = text1.length > text2.length ? text1 : text2, shortText = text1.length > text2.length ? text2 : text1;
|
|
897
|
-
if (longText.length < 4 || shortText.length * 2 < longText.length)
|
|
898
|
-
return null;
|
|
899
|
-
const halfMatch1 = halfMatchI(longText, shortText, Math.ceil(longText.length / 4)), halfMatch2 = halfMatchI(longText, shortText, Math.ceil(longText.length / 2));
|
|
900
|
-
let halfMatch;
|
|
901
|
-
if (halfMatch1 && halfMatch2)
|
|
902
|
-
halfMatch = halfMatch1[4].length > halfMatch2[4].length ? halfMatch1 : halfMatch2;
|
|
903
|
-
else {
|
|
904
|
-
if (!halfMatch1 && !halfMatch2)
|
|
905
|
-
return null;
|
|
906
|
-
halfMatch2 ? halfMatch1 || (halfMatch = halfMatch2) : halfMatch = halfMatch1;
|
|
907
|
-
}
|
|
908
|
-
if (!halfMatch)
|
|
909
|
-
throw new Error("Unable to find a half match.");
|
|
910
|
-
let text1A, text1B, text2A, text2B;
|
|
911
|
-
text1.length > text2.length ? (text1A = halfMatch[0], text1B = halfMatch[1], text2A = halfMatch[2], text2B = halfMatch[3]) : (text2A = halfMatch[0], text2B = halfMatch[1], text1A = halfMatch[2], text1B = halfMatch[3]);
|
|
912
|
-
const midCommon = halfMatch[4];
|
|
913
|
-
return [text1A, text1B, text2A, text2B, midCommon];
|
|
914
|
-
}
|
|
915
|
-
function halfMatchI(longText, shortText, i) {
|
|
916
|
-
const seed = longText.slice(i, i + Math.floor(longText.length / 4));
|
|
917
|
-
let j = -1, bestCommon = "", bestLongTextA, bestLongTextB, bestShortTextA, bestShortTextB;
|
|
918
|
-
for (; (j = shortText.indexOf(seed, j + 1)) !== -1; ) {
|
|
919
|
-
const prefixLength = getCommonPrefix(longText.slice(i), shortText.slice(j)), suffixLength = getCommonSuffix(longText.slice(0, i), shortText.slice(0, j));
|
|
920
|
-
bestCommon.length < suffixLength + prefixLength && (bestCommon = shortText.slice(j - suffixLength, j) + shortText.slice(j, j + prefixLength), bestLongTextA = longText.slice(0, i - suffixLength), bestLongTextB = longText.slice(i + prefixLength), bestShortTextA = shortText.slice(0, j - suffixLength), bestShortTextB = shortText.slice(j + prefixLength));
|
|
855
|
+
function removeNodePatch(editor, operation, beforeValue) {
|
|
856
|
+
const block = beforeValue[operation.path[0]];
|
|
857
|
+
if (operation.path.length === 1) {
|
|
858
|
+
if (block && block._key)
|
|
859
|
+
return [patchEvent.unset([{ _key: block._key }])];
|
|
860
|
+
throw new Error("Block not found");
|
|
861
|
+
} else if (editor.isTextBlock(block) && operation.path.length === 2) {
|
|
862
|
+
const spanToRemove = editor.isTextBlock(block) && block.children && block.children[operation.path[1]];
|
|
863
|
+
return spanToRemove ? [patchEvent.unset([{ _key: block._key }, "children", { _key: spanToRemove._key }])] : (debug$j("Span not found in editor trying to remove node"), []);
|
|
864
|
+
} else
|
|
865
|
+
return debug$j("Not creating patch inside object block"), [];
|
|
921
866
|
}
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
867
|
+
function mergeNodePatch(editor, operation, beforeValue) {
|
|
868
|
+
const patches = [], block = beforeValue[operation.path[0]], targetBlock = editor.children[operation.path[0]];
|
|
869
|
+
if (operation.path.length === 1)
|
|
870
|
+
if (block != null && block._key) {
|
|
871
|
+
const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
|
|
872
|
+
patches.push(patchEvent.set(newBlock, [{ _key: newBlock._key }])), patches.push(patchEvent.unset([{ _key: block._key }]));
|
|
873
|
+
} else
|
|
874
|
+
throw new Error("Target key not found!");
|
|
875
|
+
else if (operation.path.length === 2 && editor.isTextBlock(targetBlock)) {
|
|
876
|
+
const mergedSpan = editor.isTextBlock(block) && block.children[operation.path[1]] || void 0, targetSpan = targetBlock.children[operation.path[1] - 1];
|
|
877
|
+
editor.isTextSpan(targetSpan) && (patches.push(
|
|
878
|
+
patchEvent.set(targetSpan.text, [{ _key: block._key }, "children", { _key: targetSpan._key }, "text"])
|
|
879
|
+
), mergedSpan && patches.push(patchEvent.unset([{ _key: block._key }, "children", { _key: mergedSpan._key }])));
|
|
880
|
+
} else
|
|
881
|
+
debug$j("Void nodes can't be merged, not creating any patches");
|
|
882
|
+
return patches;
|
|
930
883
|
}
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
884
|
+
function moveNodePatch(editor, operation, beforeValue) {
|
|
885
|
+
const patches = [], block = beforeValue[operation.path[0]], targetBlock = beforeValue[operation.newPath[0]];
|
|
886
|
+
if (operation.path.length === 1) {
|
|
887
|
+
const position = operation.path[0] > operation.newPath[0] ? "before" : "after";
|
|
888
|
+
patches.push(patchEvent.unset([{ _key: block._key }])), patches.push(
|
|
889
|
+
patchEvent.insert([fromSlateValue([block], textBlockName)[0]], position, [{ _key: targetBlock._key }])
|
|
890
|
+
);
|
|
891
|
+
} else if (operation.path.length === 2 && editor.isTextBlock(block) && editor.isTextBlock(targetBlock)) {
|
|
892
|
+
const child = block.children[operation.path[1]], targetChild = targetBlock.children[operation.newPath[1]], position = operation.newPath[1] === targetBlock.children.length ? "after" : "before", childToInsert = fromSlateValue([block], textBlockName)[0].children[operation.path[1]];
|
|
893
|
+
patches.push(patchEvent.unset([{ _key: block._key }, "children", { _key: child._key }])), patches.push(
|
|
894
|
+
patchEvent.insert([childToInsert], position, [
|
|
895
|
+
{ _key: targetBlock._key },
|
|
896
|
+
"children",
|
|
897
|
+
{ _key: targetChild._key }
|
|
898
|
+
])
|
|
899
|
+
);
|
|
942
900
|
}
|
|
943
|
-
return
|
|
901
|
+
return patches;
|
|
944
902
|
}
|
|
945
|
-
let maxLines = 4e4;
|
|
946
|
-
const chars1 = diffLinesToMunge(textA);
|
|
947
|
-
maxLines = 65535;
|
|
948
|
-
const chars2 = diffLinesToMunge(textB);
|
|
949
903
|
return {
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
904
|
+
insertNodePatch,
|
|
905
|
+
insertTextPatch,
|
|
906
|
+
mergeNodePatch,
|
|
907
|
+
moveNodePatch,
|
|
908
|
+
removeNodePatch,
|
|
909
|
+
removeTextPatch,
|
|
910
|
+
setNodePatch,
|
|
911
|
+
splitNodePatch
|
|
953
912
|
};
|
|
954
913
|
}
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
deadline: opts.deadline
|
|
980
|
-
});
|
|
981
|
-
for (let j = aa.length - 1; j >= 0; j--)
|
|
982
|
-
diffs.splice(pointer, 0, aa[j]);
|
|
983
|
-
pointer += aa.length;
|
|
984
|
-
}
|
|
985
|
-
countInsert = 0, countDelete = 0, textDelete = "", textInsert = "";
|
|
986
|
-
break;
|
|
987
|
-
default:
|
|
988
|
-
throw new Error("Unknown diff operation.");
|
|
989
|
-
}
|
|
990
|
-
pointer++;
|
|
991
|
-
}
|
|
992
|
-
return diffs.pop(), diffs;
|
|
993
|
-
}
|
|
994
|
-
function computeDiff(text1, text2, opts) {
|
|
995
|
-
let diffs;
|
|
996
|
-
if (!text1)
|
|
997
|
-
return [[DIFF_INSERT, text2]];
|
|
998
|
-
if (!text2)
|
|
999
|
-
return [[DIFF_DELETE, text1]];
|
|
1000
|
-
const longtext = text1.length > text2.length ? text1 : text2, shorttext = text1.length > text2.length ? text2 : text1, i = longtext.indexOf(shorttext);
|
|
1001
|
-
if (i !== -1)
|
|
1002
|
-
return diffs = [[DIFF_INSERT, longtext.substring(0, i)], [DIFF_EQUAL, shorttext], [DIFF_INSERT, longtext.substring(i + shorttext.length)]], text1.length > text2.length && (diffs[0][0] = DIFF_DELETE, diffs[2][0] = DIFF_DELETE), diffs;
|
|
1003
|
-
if (shorttext.length === 1)
|
|
1004
|
-
return [[DIFF_DELETE, text1], [DIFF_INSERT, text2]];
|
|
1005
|
-
const halfMatch = findHalfMatch(text1, text2);
|
|
1006
|
-
if (halfMatch) {
|
|
1007
|
-
const text1A = halfMatch[0], text1B = halfMatch[1], text2A = halfMatch[2], text2B = halfMatch[3], midCommon = halfMatch[4], diffsA = doDiff(text1A, text2A, opts), diffsB = doDiff(text1B, text2B, opts);
|
|
1008
|
-
return diffsA.concat([[DIFF_EQUAL, midCommon]], diffsB);
|
|
1009
|
-
}
|
|
1010
|
-
return opts.checkLines && text1.length > 100 && text2.length > 100 ? doLineModeDiff(text1, text2, opts) : bisect(text1, text2, opts.deadline);
|
|
1011
|
-
}
|
|
1012
|
-
const DIFF_DELETE = -1, DIFF_INSERT = 1, DIFF_EQUAL = 0;
|
|
1013
|
-
function diff(textA, textB, opts) {
|
|
1014
|
-
if (textA === null || textB === null)
|
|
1015
|
-
throw new Error("Null input. (diff)");
|
|
1016
|
-
const diffs = doDiff(textA, textB, createInternalOpts(opts || {}));
|
|
1017
|
-
return adjustDiffForSurrogatePairs(diffs), diffs;
|
|
1018
|
-
}
|
|
1019
|
-
function doDiff(textA, textB, options) {
|
|
1020
|
-
let text1 = textA, text2 = textB;
|
|
1021
|
-
if (text1 === text2)
|
|
1022
|
-
return text1 ? [[DIFF_EQUAL, text1]] : [];
|
|
1023
|
-
let commonlength = getCommonPrefix(text1, text2);
|
|
1024
|
-
const commonprefix = text1.substring(0, commonlength);
|
|
1025
|
-
text1 = text1.substring(commonlength), text2 = text2.substring(commonlength), commonlength = getCommonSuffix(text1, text2);
|
|
1026
|
-
const commonsuffix = text1.substring(text1.length - commonlength);
|
|
1027
|
-
text1 = text1.substring(0, text1.length - commonlength), text2 = text2.substring(0, text2.length - commonlength);
|
|
1028
|
-
let diffs = computeDiff(text1, text2, options);
|
|
1029
|
-
return commonprefix && diffs.unshift([DIFF_EQUAL, commonprefix]), commonsuffix && diffs.push([DIFF_EQUAL, commonsuffix]), diffs = cleanupMerge(diffs), diffs;
|
|
1030
|
-
}
|
|
1031
|
-
function createDeadLine(timeout) {
|
|
1032
|
-
let t = 1;
|
|
1033
|
-
return typeof timeout < "u" && (t = timeout <= 0 ? Number.MAX_VALUE : timeout), Date.now() + t * 1e3;
|
|
1034
|
-
}
|
|
1035
|
-
function createInternalOpts(opts) {
|
|
1036
|
-
return {
|
|
1037
|
-
checkLines: !0,
|
|
1038
|
-
deadline: createDeadLine(opts.timeout || 1),
|
|
1039
|
-
...opts
|
|
1040
|
-
};
|
|
1041
|
-
}
|
|
1042
|
-
function combineChar(data, char, dir) {
|
|
1043
|
-
return dir === 1 ? data + char : char + data;
|
|
1044
|
-
}
|
|
1045
|
-
function splitChar(data, dir) {
|
|
1046
|
-
return dir === 1 ? [data.substring(0, data.length - 1), data[data.length - 1]] : [data.substring(1), data[0]];
|
|
1047
|
-
}
|
|
1048
|
-
function hasSharedChar(diffs, i, j, dir) {
|
|
1049
|
-
return dir === 1 ? diffs[i][1][diffs[i][1].length - 1] === diffs[j][1][diffs[j][1].length - 1] : diffs[i][1][0] === diffs[j][1][0];
|
|
1050
|
-
}
|
|
1051
|
-
function deisolateChar(diffs, i, dir) {
|
|
1052
|
-
const inv = dir === 1 ? -1 : 1;
|
|
1053
|
-
let insertIdx = null, deleteIdx = null, j = i + dir;
|
|
1054
|
-
for (; j >= 0 && j < diffs.length && (insertIdx === null || deleteIdx === null); j += dir) {
|
|
1055
|
-
const [op, text2] = diffs[j];
|
|
1056
|
-
if (text2.length !== 0) {
|
|
1057
|
-
if (op === DIFF_INSERT) {
|
|
1058
|
-
insertIdx === null && (insertIdx = j);
|
|
1059
|
-
continue;
|
|
1060
|
-
} else if (op === DIFF_DELETE) {
|
|
1061
|
-
deleteIdx === null && (deleteIdx = j);
|
|
1062
|
-
continue;
|
|
1063
|
-
} else if (op === DIFF_EQUAL) {
|
|
1064
|
-
if (insertIdx === null && deleteIdx === null) {
|
|
1065
|
-
const [rest, char2] = splitChar(diffs[i][1], dir);
|
|
1066
|
-
diffs[i][1] = rest, diffs[j][1] = combineChar(diffs[j][1], char2, inv);
|
|
1067
|
-
return;
|
|
1068
|
-
}
|
|
1069
|
-
break;
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
1073
|
-
if (insertIdx !== null && deleteIdx !== null && hasSharedChar(diffs, insertIdx, deleteIdx, dir)) {
|
|
1074
|
-
const [insertText, insertChar] = splitChar(diffs[insertIdx][1], inv), [deleteText] = splitChar(diffs[deleteIdx][1], inv);
|
|
1075
|
-
diffs[insertIdx][1] = insertText, diffs[deleteIdx][1] = deleteText, diffs[i][1] = combineChar(diffs[i][1], insertChar, dir);
|
|
1076
|
-
return;
|
|
1077
|
-
}
|
|
1078
|
-
const [text, char] = splitChar(diffs[i][1], dir);
|
|
1079
|
-
diffs[i][1] = text, insertIdx === null ? (diffs.splice(j, 0, [DIFF_INSERT, char]), deleteIdx !== null && deleteIdx >= j && deleteIdx++) : diffs[insertIdx][1] = combineChar(diffs[insertIdx][1], char, inv), deleteIdx === null ? diffs.splice(j, 0, [DIFF_DELETE, char]) : diffs[deleteIdx][1] = combineChar(diffs[deleteIdx][1], char, inv);
|
|
1080
|
-
}
|
|
1081
|
-
function adjustDiffForSurrogatePairs(diffs) {
|
|
1082
|
-
for (let i = 0; i < diffs.length; i++) {
|
|
1083
|
-
const [diffType, diffText] = diffs[i];
|
|
1084
|
-
if (diffText.length === 0) continue;
|
|
1085
|
-
const firstChar = diffText[0], lastChar = diffText[diffText.length - 1];
|
|
1086
|
-
isHighSurrogate(lastChar) && diffType === DIFF_EQUAL && deisolateChar(diffs, i, 1), isLowSurrogate(firstChar) && diffType === DIFF_EQUAL && deisolateChar(diffs, i, -1);
|
|
1087
|
-
}
|
|
1088
|
-
for (let i = 0; i < diffs.length; i++)
|
|
1089
|
-
diffs[i][1].length === 0 && diffs.splice(i, 1);
|
|
1090
|
-
}
|
|
1091
|
-
const DEFAULT_OPTIONS = {
|
|
1092
|
-
/**
|
|
1093
|
-
* At what point is no match declared (0.0 = perfection, 1.0 = very loose).
|
|
1094
|
-
*/
|
|
1095
|
-
threshold: 0.5,
|
|
1096
|
-
/**
|
|
1097
|
-
* How far to search for a match (0 = exact location, 1000+ = broad match).
|
|
1098
|
-
* A match this many characters away from the expected location will add
|
|
1099
|
-
* 1.0 to the score (0.0 is a perfect match).
|
|
1100
|
-
*/
|
|
1101
|
-
distance: 1e3
|
|
1102
|
-
};
|
|
1103
|
-
function applyDefaults(options) {
|
|
1104
|
-
return {
|
|
1105
|
-
...DEFAULT_OPTIONS,
|
|
1106
|
-
...options
|
|
1107
|
-
};
|
|
1108
|
-
}
|
|
1109
|
-
const MAX_BITS$1 = 32;
|
|
1110
|
-
function bitap(text, pattern, loc) {
|
|
1111
|
-
let opts = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {};
|
|
1112
|
-
if (pattern.length > MAX_BITS$1)
|
|
1113
|
-
throw new Error("Pattern too long for this browser.");
|
|
1114
|
-
const options = applyDefaults(opts), s = getAlphabetFromPattern(pattern);
|
|
1115
|
-
function getBitapScore(e, x) {
|
|
1116
|
-
const accuracy = e / pattern.length, proximity = Math.abs(loc - x);
|
|
1117
|
-
return options.distance ? accuracy + proximity / options.distance : proximity ? 1 : accuracy;
|
|
1118
|
-
}
|
|
1119
|
-
let scoreThreshold = options.threshold, bestLoc = text.indexOf(pattern, loc);
|
|
1120
|
-
bestLoc !== -1 && (scoreThreshold = Math.min(getBitapScore(0, bestLoc), scoreThreshold), bestLoc = text.lastIndexOf(pattern, loc + pattern.length), bestLoc !== -1 && (scoreThreshold = Math.min(getBitapScore(0, bestLoc), scoreThreshold)));
|
|
1121
|
-
const matchmask = 1 << pattern.length - 1;
|
|
1122
|
-
bestLoc = -1;
|
|
1123
|
-
let binMin, binMid, binMax = pattern.length + text.length, lastRd = [];
|
|
1124
|
-
for (let d = 0; d < pattern.length; d++) {
|
|
1125
|
-
for (binMin = 0, binMid = binMax; binMin < binMid; )
|
|
1126
|
-
getBitapScore(d, loc + binMid) <= scoreThreshold ? binMin = binMid : binMax = binMid, binMid = Math.floor((binMax - binMin) / 2 + binMin);
|
|
1127
|
-
binMax = binMid;
|
|
1128
|
-
let start = Math.max(1, loc - binMid + 1);
|
|
1129
|
-
const finish = Math.min(loc + binMid, text.length) + pattern.length, rd = new Array(finish + 2);
|
|
1130
|
-
rd[finish + 1] = (1 << d) - 1;
|
|
1131
|
-
for (let j = finish; j >= start; j--) {
|
|
1132
|
-
const charMatch = s[text.charAt(j - 1)];
|
|
1133
|
-
if (d === 0 ? rd[j] = (rd[j + 1] << 1 | 1) & charMatch : rd[j] = (rd[j + 1] << 1 | 1) & charMatch | ((lastRd[j + 1] | lastRd[j]) << 1 | 1) | lastRd[j + 1], rd[j] & matchmask) {
|
|
1134
|
-
const score = getBitapScore(d, j - 1);
|
|
1135
|
-
if (score <= scoreThreshold)
|
|
1136
|
-
if (scoreThreshold = score, bestLoc = j - 1, bestLoc > loc)
|
|
1137
|
-
start = Math.max(1, 2 * loc - bestLoc);
|
|
1138
|
-
else
|
|
1139
|
-
break;
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
if (getBitapScore(d + 1, loc) > scoreThreshold)
|
|
1143
|
-
break;
|
|
1144
|
-
lastRd = rd;
|
|
1145
|
-
}
|
|
1146
|
-
return bestLoc;
|
|
1147
|
-
}
|
|
1148
|
-
function getAlphabetFromPattern(pattern) {
|
|
1149
|
-
const s = {};
|
|
1150
|
-
for (let i = 0; i < pattern.length; i++)
|
|
1151
|
-
s[pattern.charAt(i)] = 0;
|
|
1152
|
-
for (let i = 0; i < pattern.length; i++)
|
|
1153
|
-
s[pattern.charAt(i)] |= 1 << pattern.length - i - 1;
|
|
1154
|
-
return s;
|
|
1155
|
-
}
|
|
1156
|
-
function match(text, pattern, searchLocation) {
|
|
1157
|
-
if (text === null || pattern === null || searchLocation === null)
|
|
1158
|
-
throw new Error("Null input. (match())");
|
|
1159
|
-
const loc = Math.max(0, Math.min(searchLocation, text.length));
|
|
1160
|
-
if (text === pattern)
|
|
1161
|
-
return 0;
|
|
1162
|
-
if (text.length) {
|
|
1163
|
-
if (text.substring(loc, loc + pattern.length) === pattern)
|
|
1164
|
-
return loc;
|
|
1165
|
-
} else return -1;
|
|
1166
|
-
return bitap(text, pattern, loc);
|
|
1167
|
-
}
|
|
1168
|
-
function createPatchObject(start1, start2) {
|
|
1169
|
-
return {
|
|
1170
|
-
diffs: [],
|
|
1171
|
-
start1,
|
|
1172
|
-
start2,
|
|
1173
|
-
utf8Start1: start1,
|
|
1174
|
-
utf8Start2: start2,
|
|
1175
|
-
length1: 0,
|
|
1176
|
-
length2: 0,
|
|
1177
|
-
utf8Length1: 0,
|
|
1178
|
-
utf8Length2: 0
|
|
1179
|
-
};
|
|
1180
|
-
}
|
|
1181
|
-
function diffText1(diffs) {
|
|
1182
|
-
const text = [];
|
|
1183
|
-
for (let x = 0; x < diffs.length; x++)
|
|
1184
|
-
diffs[x][0] !== DIFF_INSERT && (text[x] = diffs[x][1]);
|
|
1185
|
-
return text.join("");
|
|
1186
|
-
}
|
|
1187
|
-
function diffText2(diffs) {
|
|
1188
|
-
const text = [];
|
|
1189
|
-
for (let x = 0; x < diffs.length; x++)
|
|
1190
|
-
diffs[x][0] !== DIFF_DELETE && (text[x] = diffs[x][1]);
|
|
1191
|
-
return text.join("");
|
|
1192
|
-
}
|
|
1193
|
-
function countUtf8Bytes(str) {
|
|
1194
|
-
let bytes = 0;
|
|
1195
|
-
for (let i = 0; i < str.length; i++) {
|
|
1196
|
-
const codePoint = str.codePointAt(i);
|
|
1197
|
-
if (typeof codePoint > "u")
|
|
1198
|
-
throw new Error("Failed to get codepoint");
|
|
1199
|
-
bytes += utf8len(codePoint);
|
|
1200
|
-
}
|
|
1201
|
-
return bytes;
|
|
1202
|
-
}
|
|
1203
|
-
function adjustIndiciesToUcs2(patches, base) {
|
|
1204
|
-
let options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}, byteOffset = 0, idx = 0;
|
|
1205
|
-
function advanceTo(target) {
|
|
1206
|
-
for (; byteOffset < target; ) {
|
|
1207
|
-
const codePoint = base.codePointAt(idx);
|
|
1208
|
-
if (typeof codePoint > "u")
|
|
1209
|
-
return idx;
|
|
1210
|
-
byteOffset += utf8len(codePoint), codePoint > 65535 ? idx += 2 : idx += 1;
|
|
1211
|
-
}
|
|
1212
|
-
if (!options.allowExceedingIndices && byteOffset !== target)
|
|
1213
|
-
throw new Error("Failed to determine byte offset");
|
|
1214
|
-
return idx;
|
|
1215
|
-
}
|
|
1216
|
-
const adjusted = [];
|
|
1217
|
-
for (const patch of patches)
|
|
1218
|
-
adjusted.push({
|
|
1219
|
-
diffs: patch.diffs.map((diff2) => cloneDiff(diff2)),
|
|
1220
|
-
start1: advanceTo(patch.start1),
|
|
1221
|
-
start2: advanceTo(patch.start2),
|
|
1222
|
-
utf8Start1: patch.utf8Start1,
|
|
1223
|
-
utf8Start2: patch.utf8Start2,
|
|
1224
|
-
length1: patch.length1,
|
|
1225
|
-
length2: patch.length2,
|
|
1226
|
-
utf8Length1: patch.utf8Length1,
|
|
1227
|
-
utf8Length2: patch.utf8Length2
|
|
1228
|
-
});
|
|
1229
|
-
return adjusted;
|
|
1230
|
-
}
|
|
1231
|
-
function utf8len(codePoint) {
|
|
1232
|
-
return codePoint <= 127 ? 1 : codePoint <= 2047 ? 2 : codePoint <= 65535 ? 3 : 4;
|
|
1233
|
-
}
|
|
1234
|
-
const MAX_BITS = 32, DEFAULT_MARGIN = 4, DEFAULT_OPTS = {
|
|
1235
|
-
margin: 4
|
|
1236
|
-
};
|
|
1237
|
-
function getDefaultOpts() {
|
|
1238
|
-
let opts = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
1239
|
-
return {
|
|
1240
|
-
...DEFAULT_OPTS,
|
|
1241
|
-
...opts
|
|
1242
|
-
};
|
|
1243
|
-
}
|
|
1244
|
-
function make(a, b, options) {
|
|
1245
|
-
if (typeof a == "string" && typeof b == "string") {
|
|
1246
|
-
let diffs = diff(a, b, {
|
|
1247
|
-
checkLines: !0
|
|
1248
|
-
});
|
|
1249
|
-
return diffs.length > 2 && (diffs = cleanupSemantic(diffs), diffs = cleanupEfficiency(diffs)), _make(a, diffs, getDefaultOpts(options));
|
|
1250
|
-
}
|
|
1251
|
-
if (a && Array.isArray(a) && typeof b > "u")
|
|
1252
|
-
return _make(diffText1(a), a, getDefaultOpts(options));
|
|
1253
|
-
if (typeof a == "string" && b && Array.isArray(b))
|
|
1254
|
-
return _make(a, b, getDefaultOpts(options));
|
|
1255
|
-
throw new Error("Unknown call format to make()");
|
|
1256
|
-
}
|
|
1257
|
-
function _make(textA, diffs, options) {
|
|
1258
|
-
if (diffs.length === 0)
|
|
1259
|
-
return [];
|
|
1260
|
-
const patches = [];
|
|
1261
|
-
let patch = createPatchObject(0, 0), patchDiffLength = 0, charCount1 = 0, charCount2 = 0, utf8Count1 = 0, utf8Count2 = 0, prepatchText = textA, postpatchText = textA;
|
|
1262
|
-
for (let x = 0; x < diffs.length; x++) {
|
|
1263
|
-
const currentDiff = diffs[x], [diffType, diffText] = currentDiff, diffTextLength = diffText.length, diffByteLength = countUtf8Bytes(diffText);
|
|
1264
|
-
switch (!patchDiffLength && diffType !== DIFF_EQUAL && (patch.start1 = charCount1, patch.start2 = charCount2, patch.utf8Start1 = utf8Count1, patch.utf8Start2 = utf8Count2), diffType) {
|
|
1265
|
-
case DIFF_INSERT:
|
|
1266
|
-
patch.diffs[patchDiffLength++] = currentDiff, patch.length2 += diffTextLength, patch.utf8Length2 += diffByteLength, postpatchText = postpatchText.substring(0, charCount2) + diffText + postpatchText.substring(charCount2);
|
|
1267
|
-
break;
|
|
1268
|
-
case DIFF_DELETE:
|
|
1269
|
-
patch.length1 += diffTextLength, patch.utf8Length1 += diffByteLength, patch.diffs[patchDiffLength++] = currentDiff, postpatchText = postpatchText.substring(0, charCount2) + postpatchText.substring(charCount2 + diffTextLength);
|
|
1270
|
-
break;
|
|
1271
|
-
case DIFF_EQUAL:
|
|
1272
|
-
diffTextLength <= 2 * options.margin && patchDiffLength && diffs.length !== x + 1 ? (patch.diffs[patchDiffLength++] = currentDiff, patch.length1 += diffTextLength, patch.length2 += diffTextLength, patch.utf8Length1 += diffByteLength, patch.utf8Length2 += diffByteLength) : diffTextLength >= 2 * options.margin && patchDiffLength && (addContext(patch, prepatchText, options), patches.push(patch), patch = createPatchObject(-1, -1), patchDiffLength = 0, prepatchText = postpatchText, charCount1 = charCount2, utf8Count1 = utf8Count2);
|
|
1273
|
-
break;
|
|
1274
|
-
default:
|
|
1275
|
-
throw new Error("Unknown diff type");
|
|
1276
|
-
}
|
|
1277
|
-
diffType !== DIFF_INSERT && (charCount1 += diffTextLength, utf8Count1 += diffByteLength), diffType !== DIFF_DELETE && (charCount2 += diffTextLength, utf8Count2 += diffByteLength);
|
|
1278
|
-
}
|
|
1279
|
-
return patchDiffLength && (addContext(patch, prepatchText, options), patches.push(patch)), patches;
|
|
1280
|
-
}
|
|
1281
|
-
function addContext(patch, text, opts) {
|
|
1282
|
-
if (text.length === 0)
|
|
1283
|
-
return;
|
|
1284
|
-
let pattern = text.substring(patch.start2, patch.start2 + patch.length1), padding = 0;
|
|
1285
|
-
for (; text.indexOf(pattern) !== text.lastIndexOf(pattern) && pattern.length < MAX_BITS - opts.margin - opts.margin; )
|
|
1286
|
-
padding += opts.margin, pattern = text.substring(patch.start2 - padding, patch.start2 + patch.length1 + padding);
|
|
1287
|
-
padding += opts.margin;
|
|
1288
|
-
let prefixStart = patch.start2 - padding;
|
|
1289
|
-
prefixStart >= 1 && isLowSurrogate(text[prefixStart]) && prefixStart--;
|
|
1290
|
-
const prefix = text.substring(prefixStart, patch.start2);
|
|
1291
|
-
prefix && patch.diffs.unshift([DIFF_EQUAL, prefix]);
|
|
1292
|
-
const prefixLength = prefix.length, prefixUtf8Length = countUtf8Bytes(prefix);
|
|
1293
|
-
let suffixEnd = patch.start2 + patch.length1 + padding;
|
|
1294
|
-
suffixEnd < text.length && isLowSurrogate(text[suffixEnd]) && suffixEnd++;
|
|
1295
|
-
const suffix = text.substring(patch.start2 + patch.length1, suffixEnd);
|
|
1296
|
-
suffix && patch.diffs.push([DIFF_EQUAL, suffix]);
|
|
1297
|
-
const suffixLength = suffix.length, suffixUtf8Length = countUtf8Bytes(suffix);
|
|
1298
|
-
patch.start1 -= prefixLength, patch.start2 -= prefixLength, patch.utf8Start1 -= prefixUtf8Length, patch.utf8Start2 -= prefixUtf8Length, patch.length1 += prefixLength + suffixLength, patch.length2 += prefixLength + suffixLength, patch.utf8Length1 += prefixUtf8Length + suffixUtf8Length, patch.utf8Length2 += prefixUtf8Length + suffixUtf8Length;
|
|
1299
|
-
}
|
|
1300
|
-
function levenshtein(diffs) {
|
|
1301
|
-
let leven = 0, insertions = 0, deletions = 0;
|
|
1302
|
-
for (let x = 0; x < diffs.length; x++) {
|
|
1303
|
-
const op = diffs[x][0], data = diffs[x][1];
|
|
1304
|
-
switch (op) {
|
|
1305
|
-
case DIFF_INSERT:
|
|
1306
|
-
insertions += data.length;
|
|
1307
|
-
break;
|
|
1308
|
-
case DIFF_DELETE:
|
|
1309
|
-
deletions += data.length;
|
|
1310
|
-
break;
|
|
1311
|
-
case DIFF_EQUAL:
|
|
1312
|
-
leven += Math.max(insertions, deletions), insertions = 0, deletions = 0;
|
|
1313
|
-
break;
|
|
1314
|
-
default:
|
|
1315
|
-
throw new Error("Unknown diff operation.");
|
|
1316
|
-
}
|
|
1317
|
-
}
|
|
1318
|
-
return leven += Math.max(insertions, deletions), leven;
|
|
1319
|
-
}
|
|
1320
|
-
function xIndex(diffs, loc) {
|
|
1321
|
-
let chars1 = 0, chars2 = 0, lastChars1 = 0, lastChars2 = 0, x;
|
|
1322
|
-
for (x = 0; x < diffs.length && (diffs[x][0] !== DIFF_INSERT && (chars1 += diffs[x][1].length), diffs[x][0] !== DIFF_DELETE && (chars2 += diffs[x][1].length), !(chars1 > loc)); x++)
|
|
1323
|
-
lastChars1 = chars1, lastChars2 = chars2;
|
|
1324
|
-
return diffs.length !== x && diffs[x][0] === DIFF_DELETE ? lastChars2 : lastChars2 + (loc - lastChars1);
|
|
1325
|
-
}
|
|
1326
|
-
function addPadding(patches) {
|
|
1327
|
-
const paddingLength = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : DEFAULT_MARGIN;
|
|
1328
|
-
let nullPadding = "";
|
|
1329
|
-
for (let x = 1; x <= paddingLength; x++)
|
|
1330
|
-
nullPadding += String.fromCharCode(x);
|
|
1331
|
-
for (const p of patches)
|
|
1332
|
-
p.start1 += paddingLength, p.start2 += paddingLength, p.utf8Start1 += paddingLength, p.utf8Start2 += paddingLength;
|
|
1333
|
-
let patch = patches[0], diffs = patch.diffs;
|
|
1334
|
-
if (diffs.length === 0 || diffs[0][0] !== DIFF_EQUAL)
|
|
1335
|
-
diffs.unshift([DIFF_EQUAL, nullPadding]), patch.start1 -= paddingLength, patch.start2 -= paddingLength, patch.utf8Start1 -= paddingLength, patch.utf8Start2 -= paddingLength, patch.length1 += paddingLength, patch.length2 += paddingLength, patch.utf8Length1 += paddingLength, patch.utf8Length2 += paddingLength;
|
|
1336
|
-
else if (paddingLength > diffs[0][1].length) {
|
|
1337
|
-
const firstDiffLength = diffs[0][1].length, extraLength = paddingLength - firstDiffLength;
|
|
1338
|
-
diffs[0][1] = nullPadding.substring(firstDiffLength) + diffs[0][1], patch.start1 -= extraLength, patch.start2 -= extraLength, patch.utf8Start1 -= extraLength, patch.utf8Start2 -= extraLength, patch.length1 += extraLength, patch.length2 += extraLength, patch.utf8Length1 += extraLength, patch.utf8Length2 += extraLength;
|
|
1339
|
-
}
|
|
1340
|
-
if (patch = patches[patches.length - 1], diffs = patch.diffs, diffs.length === 0 || diffs[diffs.length - 1][0] !== DIFF_EQUAL)
|
|
1341
|
-
diffs.push([DIFF_EQUAL, nullPadding]), patch.length1 += paddingLength, patch.length2 += paddingLength, patch.utf8Length1 += paddingLength, patch.utf8Length2 += paddingLength;
|
|
1342
|
-
else if (paddingLength > diffs[diffs.length - 1][1].length) {
|
|
1343
|
-
const extraLength = paddingLength - diffs[diffs.length - 1][1].length;
|
|
1344
|
-
diffs[diffs.length - 1][1] += nullPadding.substring(0, extraLength), patch.length1 += extraLength, patch.length2 += extraLength, patch.utf8Length1 += extraLength, patch.utf8Length2 += extraLength;
|
|
1345
|
-
}
|
|
1346
|
-
return nullPadding;
|
|
1347
|
-
}
|
|
1348
|
-
function splitMax(patches) {
|
|
1349
|
-
let margin = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : DEFAULT_MARGIN;
|
|
1350
|
-
const patchSize = MAX_BITS;
|
|
1351
|
-
for (let x = 0; x < patches.length; x++) {
|
|
1352
|
-
if (patches[x].length1 <= patchSize)
|
|
1353
|
-
continue;
|
|
1354
|
-
const bigpatch = patches[x];
|
|
1355
|
-
patches.splice(x--, 1);
|
|
1356
|
-
let start1 = bigpatch.start1, start2 = bigpatch.start2, preContext = "";
|
|
1357
|
-
for (; bigpatch.diffs.length !== 0; ) {
|
|
1358
|
-
const patch = createPatchObject(start1 - preContext.length, start2 - preContext.length);
|
|
1359
|
-
let empty = !0;
|
|
1360
|
-
if (preContext !== "") {
|
|
1361
|
-
const precontextByteCount = countUtf8Bytes(preContext);
|
|
1362
|
-
patch.length1 = preContext.length, patch.utf8Length1 = precontextByteCount, patch.length2 = preContext.length, patch.utf8Length2 = precontextByteCount, patch.diffs.push([DIFF_EQUAL, preContext]);
|
|
1363
|
-
}
|
|
1364
|
-
for (; bigpatch.diffs.length !== 0 && patch.length1 < patchSize - margin; ) {
|
|
1365
|
-
const diffType = bigpatch.diffs[0][0];
|
|
1366
|
-
let diffText = bigpatch.diffs[0][1], diffTextByteCount = countUtf8Bytes(diffText);
|
|
1367
|
-
if (diffType === DIFF_INSERT) {
|
|
1368
|
-
patch.length2 += diffText.length, patch.utf8Length2 += diffTextByteCount, start2 += diffText.length;
|
|
1369
|
-
const diff2 = bigpatch.diffs.shift();
|
|
1370
|
-
diff2 && patch.diffs.push(diff2), empty = !1;
|
|
1371
|
-
} else diffType === DIFF_DELETE && patch.diffs.length === 1 && patch.diffs[0][0] === DIFF_EQUAL && diffText.length > 2 * patchSize ? (patch.length1 += diffText.length, patch.utf8Length1 += diffTextByteCount, start1 += diffText.length, empty = !1, patch.diffs.push([diffType, diffText]), bigpatch.diffs.shift()) : (diffText = diffText.substring(0, patchSize - patch.length1 - margin), diffTextByteCount = countUtf8Bytes(diffText), patch.length1 += diffText.length, patch.utf8Length1 += diffTextByteCount, start1 += diffText.length, diffType === DIFF_EQUAL ? (patch.length2 += diffText.length, patch.utf8Length2 += diffTextByteCount, start2 += diffText.length) : empty = !1, patch.diffs.push([diffType, diffText]), diffText === bigpatch.diffs[0][1] ? bigpatch.diffs.shift() : bigpatch.diffs[0][1] = bigpatch.diffs[0][1].substring(diffText.length));
|
|
1372
|
-
}
|
|
1373
|
-
preContext = diffText2(patch.diffs), preContext = preContext.substring(preContext.length - margin);
|
|
1374
|
-
const postContext = diffText1(bigpatch.diffs).substring(0, margin), postContextByteCount = countUtf8Bytes(postContext);
|
|
1375
|
-
postContext !== "" && (patch.length1 += postContext.length, patch.length2 += postContext.length, patch.utf8Length1 += postContextByteCount, patch.utf8Length2 += postContextByteCount, patch.diffs.length !== 0 && patch.diffs[patch.diffs.length - 1][0] === DIFF_EQUAL ? patch.diffs[patch.diffs.length - 1][1] += postContext : patch.diffs.push([DIFF_EQUAL, postContext])), empty || patches.splice(++x, 0, patch);
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
|
-
function apply$4(patches, originalText) {
|
|
1380
|
-
let opts = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
1381
|
-
if (typeof patches == "string")
|
|
1382
|
-
throw new Error("Patches must be an array - pass the patch to `parsePatch()` first");
|
|
1383
|
-
let text = originalText;
|
|
1384
|
-
if (patches.length === 0)
|
|
1385
|
-
return [text, []];
|
|
1386
|
-
const parsed = adjustIndiciesToUcs2(patches, text, {
|
|
1387
|
-
allowExceedingIndices: opts.allowExceedingIndices
|
|
1388
|
-
}), margin = opts.margin || DEFAULT_MARGIN, deleteThreshold = opts.deleteThreshold || 0.4, nullPadding = addPadding(parsed, margin);
|
|
1389
|
-
text = nullPadding + text + nullPadding, splitMax(parsed, margin);
|
|
1390
|
-
let delta = 0;
|
|
1391
|
-
const results = [];
|
|
1392
|
-
for (let x = 0; x < parsed.length; x++) {
|
|
1393
|
-
const expectedLoc = parsed[x].start2 + delta, text1 = diffText1(parsed[x].diffs);
|
|
1394
|
-
let startLoc, endLoc = -1;
|
|
1395
|
-
if (text1.length > MAX_BITS ? (startLoc = match(text, text1.substring(0, MAX_BITS), expectedLoc), startLoc !== -1 && (endLoc = match(text, text1.substring(text1.length - MAX_BITS), expectedLoc + text1.length - MAX_BITS), (endLoc === -1 || startLoc >= endLoc) && (startLoc = -1))) : startLoc = match(text, text1, expectedLoc), startLoc === -1)
|
|
1396
|
-
results[x] = !1, delta -= parsed[x].length2 - parsed[x].length1;
|
|
1397
|
-
else {
|
|
1398
|
-
results[x] = !0, delta = startLoc - expectedLoc;
|
|
1399
|
-
let text2;
|
|
1400
|
-
if (endLoc === -1 ? text2 = text.substring(startLoc, startLoc + text1.length) : text2 = text.substring(startLoc, endLoc + MAX_BITS), text1 === text2)
|
|
1401
|
-
text = text.substring(0, startLoc) + diffText2(parsed[x].diffs) + text.substring(startLoc + text1.length);
|
|
1402
|
-
else {
|
|
1403
|
-
let diffs = diff(text1, text2, {
|
|
1404
|
-
checkLines: !1
|
|
1405
|
-
});
|
|
1406
|
-
if (text1.length > MAX_BITS && levenshtein(diffs) / text1.length > deleteThreshold)
|
|
1407
|
-
results[x] = !1;
|
|
1408
|
-
else {
|
|
1409
|
-
diffs = cleanupSemanticLossless(diffs);
|
|
1410
|
-
let index1 = 0, index2 = 0;
|
|
1411
|
-
for (let y = 0; y < parsed[x].diffs.length; y++) {
|
|
1412
|
-
const mod = parsed[x].diffs[y];
|
|
1413
|
-
mod[0] !== DIFF_EQUAL && (index2 = xIndex(diffs, index1)), mod[0] === DIFF_INSERT ? text = text.substring(0, startLoc + index2) + mod[1] + text.substring(startLoc + index2) : mod[0] === DIFF_DELETE && (text = text.substring(0, startLoc + index2) + text.substring(startLoc + xIndex(diffs, index1 + mod[1].length))), mod[0] !== DIFF_DELETE && (index1 += mod[1].length);
|
|
1414
|
-
}
|
|
1415
|
-
}
|
|
1416
|
-
}
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
return text = text.substring(nullPadding.length, text.length - nullPadding.length), [text, results];
|
|
1420
|
-
}
|
|
1421
|
-
function stringify(patches) {
|
|
1422
|
-
return patches.map(stringifyPatch).join("");
|
|
1423
|
-
}
|
|
1424
|
-
function stringifyPatch(patch) {
|
|
1425
|
-
const {
|
|
1426
|
-
utf8Length1,
|
|
1427
|
-
utf8Length2,
|
|
1428
|
-
utf8Start1,
|
|
1429
|
-
utf8Start2,
|
|
1430
|
-
diffs
|
|
1431
|
-
} = patch;
|
|
1432
|
-
let coords1;
|
|
1433
|
-
utf8Length1 === 0 ? coords1 = "".concat(utf8Start1, ",0") : utf8Length1 === 1 ? coords1 = "".concat(utf8Start1 + 1) : coords1 = "".concat(utf8Start1 + 1, ",").concat(utf8Length1);
|
|
1434
|
-
let coords2;
|
|
1435
|
-
utf8Length2 === 0 ? coords2 = "".concat(utf8Start2, ",0") : utf8Length2 === 1 ? coords2 = "".concat(utf8Start2 + 1) : coords2 = "".concat(utf8Start2 + 1, ",").concat(utf8Length2);
|
|
1436
|
-
const text = ["@@ -".concat(coords1, " +").concat(coords2, ` @@
|
|
1437
|
-
`)];
|
|
1438
|
-
let op;
|
|
1439
|
-
for (let x = 0; x < diffs.length; x++) {
|
|
1440
|
-
switch (diffs[x][0]) {
|
|
1441
|
-
case DIFF_INSERT:
|
|
1442
|
-
op = "+";
|
|
1443
|
-
break;
|
|
1444
|
-
case DIFF_DELETE:
|
|
1445
|
-
op = "-";
|
|
1446
|
-
break;
|
|
1447
|
-
case DIFF_EQUAL:
|
|
1448
|
-
op = " ";
|
|
1449
|
-
break;
|
|
1450
|
-
default:
|
|
1451
|
-
throw new Error("Unknown patch operation.");
|
|
1452
|
-
}
|
|
1453
|
-
text[x + 1] = "".concat(op + encodeURI(diffs[x][1]), `
|
|
1454
|
-
`);
|
|
1455
|
-
}
|
|
1456
|
-
return text.join("").replace(/%20/g, " ");
|
|
1457
|
-
}
|
|
1458
|
-
const patchHeader = /^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$/;
|
|
1459
|
-
function parse(textline) {
|
|
1460
|
-
if (!textline)
|
|
1461
|
-
return [];
|
|
1462
|
-
const patches = [], lines = textline.split(`
|
|
1463
|
-
`);
|
|
1464
|
-
let textPointer = 0;
|
|
1465
|
-
for (; textPointer < lines.length; ) {
|
|
1466
|
-
const m = lines[textPointer].match(patchHeader);
|
|
1467
|
-
if (!m)
|
|
1468
|
-
throw new Error("Invalid patch string: ".concat(lines[textPointer]));
|
|
1469
|
-
const patch = createPatchObject(toInt(m[1]), toInt(m[3]));
|
|
1470
|
-
for (patches.push(patch), m[2] === "" ? (patch.start1--, patch.utf8Start1--, patch.length1 = 1, patch.utf8Length1 = 1) : m[2] === "0" ? (patch.length1 = 0, patch.utf8Length1 = 0) : (patch.start1--, patch.utf8Start1--, patch.utf8Length1 = toInt(m[2]), patch.length1 = patch.utf8Length1), m[4] === "" ? (patch.start2--, patch.utf8Start2--, patch.length2 = 1, patch.utf8Length2 = 1) : m[4] === "0" ? (patch.length2 = 0, patch.utf8Length2 = 0) : (patch.start2--, patch.utf8Start2--, patch.utf8Length2 = toInt(m[4]), patch.length2 = patch.utf8Length2), textPointer++; textPointer < lines.length; ) {
|
|
1471
|
-
const currentLine = lines[textPointer], sign = currentLine.charAt(0);
|
|
1472
|
-
if (sign === "@")
|
|
1473
|
-
break;
|
|
1474
|
-
if (sign === "") {
|
|
1475
|
-
textPointer++;
|
|
1476
|
-
continue;
|
|
1477
|
-
}
|
|
1478
|
-
let line;
|
|
1479
|
-
try {
|
|
1480
|
-
line = decodeURI(currentLine.slice(1));
|
|
1481
|
-
} catch {
|
|
1482
|
-
throw new Error("Illegal escape in parse: ".concat(currentLine));
|
|
1483
|
-
}
|
|
1484
|
-
const utf8Diff = countUtf8Bytes(line) - line.length;
|
|
1485
|
-
if (sign === "-")
|
|
1486
|
-
patch.diffs.push([DIFF_DELETE, line]), patch.length1 -= utf8Diff;
|
|
1487
|
-
else if (sign === "+")
|
|
1488
|
-
patch.diffs.push([DIFF_INSERT, line]), patch.length2 -= utf8Diff;
|
|
1489
|
-
else if (sign === " ")
|
|
1490
|
-
patch.diffs.push([DIFF_EQUAL, line]), patch.length1 -= utf8Diff, patch.length2 -= utf8Diff;
|
|
1491
|
-
else
|
|
1492
|
-
throw new Error('Invalid patch mode "'.concat(sign, '" in: ').concat(line));
|
|
1493
|
-
textPointer++;
|
|
1494
|
-
}
|
|
1495
|
-
}
|
|
1496
|
-
return patches;
|
|
1497
|
-
}
|
|
1498
|
-
function toInt(num) {
|
|
1499
|
-
return parseInt(num, 10);
|
|
1500
|
-
}
|
|
1501
|
-
function setIfMissing(value, path = []) {
|
|
1502
|
-
return {
|
|
1503
|
-
type: "setIfMissing",
|
|
1504
|
-
path,
|
|
1505
|
-
value
|
|
1506
|
-
};
|
|
1507
|
-
}
|
|
1508
|
-
function diffMatchPatch$1(currentValue, nextValue, path = []) {
|
|
1509
|
-
const patches = make(currentValue, nextValue), patch = stringify(patches);
|
|
1510
|
-
return { type: "diffMatchPatch", path, value: patch };
|
|
1511
|
-
}
|
|
1512
|
-
function insert$1(items, position, path = []) {
|
|
1513
|
-
return {
|
|
1514
|
-
type: "insert",
|
|
1515
|
-
path,
|
|
1516
|
-
position,
|
|
1517
|
-
items
|
|
1518
|
-
};
|
|
1519
|
-
}
|
|
1520
|
-
function set(value, path = []) {
|
|
1521
|
-
return { type: "set", path, value };
|
|
1522
|
-
}
|
|
1523
|
-
function unset(path = []) {
|
|
1524
|
-
return { type: "unset", path };
|
|
1525
|
-
}
|
|
1526
|
-
const debug$j = debugWithName("operationToPatches");
|
|
1527
|
-
debug$j.enabled = !1;
|
|
1528
|
-
function createOperationToPatches(types2) {
|
|
1529
|
-
const textBlockName = types2.block.name;
|
|
1530
|
-
function insertTextPatch(editor, operation, beforeValue) {
|
|
1531
|
-
debug$j.enabled && debug$j("Operation", JSON.stringify(operation, null, 2));
|
|
1532
|
-
const block = editor.isTextBlock(editor.children[operation.path[0]]) && editor.children[operation.path[0]];
|
|
1533
|
-
if (!block)
|
|
1534
|
-
throw new Error("Could not find block");
|
|
1535
|
-
const textChild = editor.isTextBlock(block) && editor.isTextSpan(block.children[operation.path[1]]) && block.children[operation.path[1]];
|
|
1536
|
-
if (!textChild)
|
|
1537
|
-
throw new Error("Could not find child");
|
|
1538
|
-
const path = [{ _key: block._key }, "children", { _key: textChild._key }, "text"], prevBlock = beforeValue[operation.path[0]], prevChild = editor.isTextBlock(prevBlock) && prevBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevChild) ? prevChild.text : "", patch = diffMatchPatch$1(prevText, textChild.text, path);
|
|
1539
|
-
return patch.value.length ? [patch] : [];
|
|
1540
|
-
}
|
|
1541
|
-
function removeTextPatch(editor, operation, beforeValue) {
|
|
1542
|
-
const block = editor && editor.children[operation.path[0]];
|
|
1543
|
-
if (!block)
|
|
1544
|
-
throw new Error("Could not find block");
|
|
1545
|
-
const child = editor.isTextBlock(block) && block.children[operation.path[1]] || void 0, textChild = editor.isTextSpan(child) ? child : void 0;
|
|
1546
|
-
if (child && !textChild)
|
|
1547
|
-
throw new Error("Expected span");
|
|
1548
|
-
if (!textChild)
|
|
1549
|
-
throw new Error("Could not find child");
|
|
1550
|
-
const path = [{ _key: block._key }, "children", { _key: textChild._key }, "text"], beforeBlock = beforeValue[operation.path[0]], prevTextChild = editor.isTextBlock(beforeBlock) && beforeBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevTextChild) && prevTextChild.text, patch = diffMatchPatch$1(prevText || "", textChild.text, path);
|
|
1551
|
-
return patch.value ? [patch] : [];
|
|
1552
|
-
}
|
|
1553
|
-
function setNodePatch(editor, operation) {
|
|
1554
|
-
if (operation.path.length === 1) {
|
|
1555
|
-
const block = editor.children[operation.path[0]];
|
|
1556
|
-
if (typeof block._key != "string")
|
|
1557
|
-
throw new Error("Expected block to have a _key");
|
|
1558
|
-
const setNode = omitBy__default.default(
|
|
1559
|
-
{ ...editor.children[operation.path[0]], ...operation.newProperties },
|
|
1560
|
-
isUndefined__default.default
|
|
1561
|
-
);
|
|
1562
|
-
return [set(fromSlateValue([setNode], textBlockName)[0], [{ _key: block._key }])];
|
|
1563
|
-
} else if (operation.path.length === 2) {
|
|
1564
|
-
const block = editor.children[operation.path[0]];
|
|
1565
|
-
if (editor.isTextBlock(block)) {
|
|
1566
|
-
const child = block.children[operation.path[1]];
|
|
1567
|
-
if (child) {
|
|
1568
|
-
const blockKey = block._key, childKey = child._key, patches = [], keys = Object.keys(operation.newProperties);
|
|
1569
|
-
return keys.forEach((keyName) => {
|
|
1570
|
-
if (keys.length === 1 && keyName === "_key") {
|
|
1571
|
-
const val = get__default.default(operation.newProperties, keyName);
|
|
1572
|
-
patches.push(
|
|
1573
|
-
set(val, [{ _key: blockKey }, "children", block.children.indexOf(child), keyName])
|
|
1574
|
-
);
|
|
1575
|
-
} else {
|
|
1576
|
-
const val = get__default.default(operation.newProperties, keyName);
|
|
1577
|
-
patches.push(set(val, [{ _key: blockKey }, "children", { _key: childKey }, keyName]));
|
|
1578
|
-
}
|
|
1579
|
-
}), patches;
|
|
1580
|
-
}
|
|
1581
|
-
throw new Error("Could not find a valid child");
|
|
1582
|
-
}
|
|
1583
|
-
throw new Error("Could not find a valid block");
|
|
1584
|
-
} else
|
|
1585
|
-
throw new Error(`Unexpected path encountered: ${JSON.stringify(operation.path)}`);
|
|
1586
|
-
}
|
|
1587
|
-
function insertNodePatch(editor, operation, beforeValue) {
|
|
1588
|
-
const block = beforeValue[operation.path[0]], isTextBlock = editor.isTextBlock(block);
|
|
1589
|
-
if (operation.path.length === 1) {
|
|
1590
|
-
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block == null ? void 0 : block._key : beforeBlock == null ? void 0 : beforeBlock._key;
|
|
1591
|
-
return targetKey ? [
|
|
1592
|
-
insert$1([fromSlateValue([operation.node], textBlockName)[0]], position, [
|
|
1593
|
-
{ _key: targetKey }
|
|
1594
|
-
])
|
|
1595
|
-
] : [
|
|
1596
|
-
setIfMissing(beforeValue, []),
|
|
1597
|
-
insert$1([fromSlateValue([operation.node], textBlockName)[0]], "before", [
|
|
1598
|
-
operation.path[0]
|
|
1599
|
-
])
|
|
1600
|
-
];
|
|
1601
|
-
} else if (isTextBlock && operation.path.length === 2 && editor.children[operation.path[0]]) {
|
|
1602
|
-
const position = block.children.length === 0 || !block.children[operation.path[1] - 1] ? "before" : "after", node = { ...operation.node };
|
|
1603
|
-
!node._type && slate.Text.isText(node) && (node._type = "span", node.marks = []);
|
|
1604
|
-
const child = fromSlateValue(
|
|
1605
|
-
[
|
|
1606
|
-
{
|
|
1607
|
-
_key: "bogus",
|
|
1608
|
-
_type: textBlockName,
|
|
1609
|
-
children: [node]
|
|
1610
|
-
}
|
|
1611
|
-
],
|
|
1612
|
-
textBlockName
|
|
1613
|
-
)[0].children[0];
|
|
1614
|
-
return [
|
|
1615
|
-
insert$1([child], position, [
|
|
1616
|
-
{ _key: block._key },
|
|
1617
|
-
"children",
|
|
1618
|
-
block.children.length <= 1 || !block.children[operation.path[1] - 1] ? 0 : { _key: block.children[operation.path[1] - 1]._key }
|
|
1619
|
-
])
|
|
1620
|
-
];
|
|
1621
|
-
}
|
|
1622
|
-
return debug$j("Something was inserted into a void block. Not producing editor patches."), [];
|
|
1623
|
-
}
|
|
1624
|
-
function splitNodePatch(editor, operation, beforeValue) {
|
|
1625
|
-
const patches = [], splitBlock = editor.children[operation.path[0]];
|
|
1626
|
-
if (!editor.isTextBlock(splitBlock))
|
|
1627
|
-
throw new Error(
|
|
1628
|
-
`Block with path ${JSON.stringify(
|
|
1629
|
-
operation.path[0]
|
|
1630
|
-
)} is not a text block and can't be split`
|
|
1631
|
-
);
|
|
1632
|
-
if (operation.path.length === 1) {
|
|
1633
|
-
const oldBlock = beforeValue[operation.path[0]];
|
|
1634
|
-
if (editor.isTextBlock(oldBlock)) {
|
|
1635
|
-
const targetValue = fromSlateValue(
|
|
1636
|
-
[editor.children[operation.path[0] + 1]],
|
|
1637
|
-
textBlockName
|
|
1638
|
-
)[0];
|
|
1639
|
-
targetValue && (patches.push(insert$1([targetValue], "after", [{ _key: splitBlock._key }])), oldBlock.children.slice(operation.position).forEach((span) => {
|
|
1640
|
-
const path = [{ _key: oldBlock._key }, "children", { _key: span._key }];
|
|
1641
|
-
patches.push(unset(path));
|
|
1642
|
-
}));
|
|
1643
|
-
}
|
|
1644
|
-
return patches;
|
|
1645
|
-
}
|
|
1646
|
-
if (operation.path.length === 2) {
|
|
1647
|
-
const splitSpan = splitBlock.children[operation.path[1]];
|
|
1648
|
-
if (editor.isTextSpan(splitSpan)) {
|
|
1649
|
-
const targetSpans = fromSlateValue(
|
|
1650
|
-
[
|
|
1651
|
-
{
|
|
1652
|
-
...splitBlock,
|
|
1653
|
-
children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
|
|
1654
|
-
}
|
|
1655
|
-
],
|
|
1656
|
-
textBlockName
|
|
1657
|
-
)[0].children;
|
|
1658
|
-
patches.push(
|
|
1659
|
-
insert$1(targetSpans, "after", [
|
|
1660
|
-
{ _key: splitBlock._key },
|
|
1661
|
-
"children",
|
|
1662
|
-
{ _key: splitSpan._key }
|
|
1663
|
-
])
|
|
1664
|
-
), patches.push(
|
|
1665
|
-
set(splitSpan.text, [
|
|
1666
|
-
{ _key: splitBlock._key },
|
|
1667
|
-
"children",
|
|
1668
|
-
{ _key: splitSpan._key },
|
|
1669
|
-
"text"
|
|
1670
|
-
])
|
|
1671
|
-
);
|
|
1672
|
-
}
|
|
1673
|
-
return patches;
|
|
1674
|
-
}
|
|
1675
|
-
return patches;
|
|
1676
|
-
}
|
|
1677
|
-
function removeNodePatch(editor, operation, beforeValue) {
|
|
1678
|
-
const block = beforeValue[operation.path[0]];
|
|
1679
|
-
if (operation.path.length === 1) {
|
|
1680
|
-
if (block && block._key)
|
|
1681
|
-
return [unset([{ _key: block._key }])];
|
|
1682
|
-
throw new Error("Block not found");
|
|
1683
|
-
} else if (editor.isTextBlock(block) && operation.path.length === 2) {
|
|
1684
|
-
const spanToRemove = editor.isTextBlock(block) && block.children && block.children[operation.path[1]];
|
|
1685
|
-
return spanToRemove ? [unset([{ _key: block._key }, "children", { _key: spanToRemove._key }])] : (debug$j("Span not found in editor trying to remove node"), []);
|
|
1686
|
-
} else
|
|
1687
|
-
return debug$j("Not creating patch inside object block"), [];
|
|
1688
|
-
}
|
|
1689
|
-
function mergeNodePatch(editor, operation, beforeValue) {
|
|
1690
|
-
const patches = [], block = beforeValue[operation.path[0]], targetBlock = editor.children[operation.path[0]];
|
|
1691
|
-
if (operation.path.length === 1)
|
|
1692
|
-
if (block != null && block._key) {
|
|
1693
|
-
const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
|
|
1694
|
-
patches.push(set(newBlock, [{ _key: newBlock._key }])), patches.push(unset([{ _key: block._key }]));
|
|
1695
|
-
} else
|
|
1696
|
-
throw new Error("Target key not found!");
|
|
1697
|
-
else if (operation.path.length === 2 && editor.isTextBlock(targetBlock)) {
|
|
1698
|
-
const mergedSpan = editor.isTextBlock(block) && block.children[operation.path[1]] || void 0, targetSpan = targetBlock.children[operation.path[1] - 1];
|
|
1699
|
-
editor.isTextSpan(targetSpan) && (patches.push(
|
|
1700
|
-
set(targetSpan.text, [{ _key: block._key }, "children", { _key: targetSpan._key }, "text"])
|
|
1701
|
-
), mergedSpan && patches.push(unset([{ _key: block._key }, "children", { _key: mergedSpan._key }])));
|
|
1702
|
-
} else
|
|
1703
|
-
debug$j("Void nodes can't be merged, not creating any patches");
|
|
1704
|
-
return patches;
|
|
1705
|
-
}
|
|
1706
|
-
function moveNodePatch(editor, operation, beforeValue) {
|
|
1707
|
-
const patches = [], block = beforeValue[operation.path[0]], targetBlock = beforeValue[operation.newPath[0]];
|
|
1708
|
-
if (operation.path.length === 1) {
|
|
1709
|
-
const position = operation.path[0] > operation.newPath[0] ? "before" : "after";
|
|
1710
|
-
patches.push(unset([{ _key: block._key }])), patches.push(
|
|
1711
|
-
insert$1([fromSlateValue([block], textBlockName)[0]], position, [{ _key: targetBlock._key }])
|
|
1712
|
-
);
|
|
1713
|
-
} else if (operation.path.length === 2 && editor.isTextBlock(block) && editor.isTextBlock(targetBlock)) {
|
|
1714
|
-
const child = block.children[operation.path[1]], targetChild = targetBlock.children[operation.newPath[1]], position = operation.newPath[1] === targetBlock.children.length ? "after" : "before", childToInsert = fromSlateValue([block], textBlockName)[0].children[operation.path[1]];
|
|
1715
|
-
patches.push(unset([{ _key: block._key }, "children", { _key: child._key }])), patches.push(
|
|
1716
|
-
insert$1([childToInsert], position, [
|
|
1717
|
-
{ _key: targetBlock._key },
|
|
1718
|
-
"children",
|
|
1719
|
-
{ _key: targetChild._key }
|
|
1720
|
-
])
|
|
1721
|
-
);
|
|
1722
|
-
}
|
|
1723
|
-
return patches;
|
|
1724
|
-
}
|
|
1725
|
-
return {
|
|
1726
|
-
insertNodePatch,
|
|
1727
|
-
insertTextPatch,
|
|
1728
|
-
mergeNodePatch,
|
|
1729
|
-
moveNodePatch,
|
|
1730
|
-
removeNodePatch,
|
|
1731
|
-
removeTextPatch,
|
|
1732
|
-
setNodePatch,
|
|
1733
|
-
splitNodePatch
|
|
1734
|
-
};
|
|
1735
|
-
}
|
|
1736
|
-
const debug$i = debugWithName("API:editable");
|
|
1737
|
-
function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
1738
|
-
return function(editor) {
|
|
1739
|
-
return portableTextEditor.setEditable({
|
|
1740
|
-
focus: () => {
|
|
1741
|
-
slateReact.ReactEditor.focus(editor);
|
|
1742
|
-
},
|
|
1743
|
-
blur: () => {
|
|
1744
|
-
slateReact.ReactEditor.blur(editor);
|
|
1745
|
-
},
|
|
1746
|
-
toggleMark: (mark) => {
|
|
1747
|
-
editor.pteToggleMark(mark);
|
|
1748
|
-
},
|
|
1749
|
-
toggleList: (listStyle) => {
|
|
1750
|
-
editor.pteToggleListItem(listStyle);
|
|
1751
|
-
},
|
|
1752
|
-
toggleBlockStyle: (blockStyle) => {
|
|
1753
|
-
editor.pteToggleBlockStyle(blockStyle);
|
|
1754
|
-
},
|
|
1755
|
-
isMarkActive: (mark) => {
|
|
1756
|
-
try {
|
|
1757
|
-
return editor.pteIsMarkActive(mark);
|
|
1758
|
-
} catch (err) {
|
|
1759
|
-
return console.warn(err), !1;
|
|
914
|
+
const debug$i = debugWithName("API:editable");
|
|
915
|
+
function createWithEditableAPI(portableTextEditor, types2, keyGenerator) {
|
|
916
|
+
return function(editor) {
|
|
917
|
+
return portableTextEditor.setEditable({
|
|
918
|
+
focus: () => {
|
|
919
|
+
slateReact.ReactEditor.focus(editor);
|
|
920
|
+
},
|
|
921
|
+
blur: () => {
|
|
922
|
+
slateReact.ReactEditor.blur(editor);
|
|
923
|
+
},
|
|
924
|
+
toggleMark: (mark) => {
|
|
925
|
+
editor.pteToggleMark(mark);
|
|
926
|
+
},
|
|
927
|
+
toggleList: (listStyle) => {
|
|
928
|
+
editor.pteToggleListItem(listStyle);
|
|
929
|
+
},
|
|
930
|
+
toggleBlockStyle: (blockStyle) => {
|
|
931
|
+
editor.pteToggleBlockStyle(blockStyle);
|
|
932
|
+
},
|
|
933
|
+
isMarkActive: (mark) => {
|
|
934
|
+
try {
|
|
935
|
+
return editor.pteIsMarkActive(mark);
|
|
936
|
+
} catch (err) {
|
|
937
|
+
return console.warn(err), !1;
|
|
1760
938
|
}
|
|
1761
939
|
},
|
|
1762
940
|
marks: () => ({
|
|
@@ -1772,7 +950,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1772
950
|
if (editor.selection) {
|
|
1773
951
|
const block = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
1774
952
|
if (block)
|
|
1775
|
-
return fromSlateValue([block],
|
|
953
|
+
return fromSlateValue([block], types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0];
|
|
1776
954
|
}
|
|
1777
955
|
},
|
|
1778
956
|
focusChild: () => {
|
|
@@ -1781,7 +959,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1781
959
|
if (block && editor.isTextBlock(block))
|
|
1782
960
|
return fromSlateValue(
|
|
1783
961
|
[block],
|
|
1784
|
-
|
|
962
|
+
types2.block.name,
|
|
1785
963
|
KEY_TO_VALUE_ELEMENT.get(editor)
|
|
1786
964
|
)[0].children[editor.selection.focus.path[1]];
|
|
1787
965
|
}
|
|
@@ -1793,18 +971,18 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1793
971
|
const [focusBlock] = Array.from(
|
|
1794
972
|
slate.Editor.nodes(editor, {
|
|
1795
973
|
at: editor.selection.focus.path.slice(0, 1),
|
|
1796
|
-
match: (n) => n._type ===
|
|
974
|
+
match: (n) => n._type === types2.block.name
|
|
1797
975
|
})
|
|
1798
976
|
)[0] || [void 0];
|
|
1799
977
|
if (!focusBlock)
|
|
1800
978
|
throw new Error("No focused text block");
|
|
1801
|
-
if (type.name !==
|
|
979
|
+
if (type.name !== types2.span.name && !types2.inlineObjects.some((t) => t.name === type.name))
|
|
1802
980
|
throw new Error("This type cannot be inserted as a child to a text block");
|
|
1803
981
|
const child = toSlateValue(
|
|
1804
982
|
[
|
|
1805
983
|
{
|
|
1806
984
|
_key: keyGenerator(),
|
|
1807
|
-
_type:
|
|
985
|
+
_type: types2.block.name,
|
|
1808
986
|
children: [
|
|
1809
987
|
{
|
|
1810
988
|
_key: keyGenerator(),
|
|
@@ -1815,14 +993,14 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1815
993
|
}
|
|
1816
994
|
],
|
|
1817
995
|
portableTextEditor
|
|
1818
|
-
)[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2), isSpanNode = child._type ===
|
|
1819
|
-
return isSpanNode && focusNode._type !==
|
|
996
|
+
)[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2), isSpanNode = child._type === types2.span.name, focusNode = slate.Node.get(editor, focusChildPath);
|
|
997
|
+
return isSpanNode && focusNode._type !== types2.span.name && (debug$i("Inserting span child next to inline object child, moving selection + 1"), editor.move({ distance: 1, unit: "character" })), slate.Transforms.insertNodes(editor, child, {
|
|
1820
998
|
select: !0,
|
|
1821
999
|
at: editor.selection
|
|
1822
1000
|
}), editor.onChange(), ((_a = toPortableTextRange(
|
|
1823
|
-
fromSlateValue(editor.children,
|
|
1001
|
+
fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1824
1002
|
editor.selection,
|
|
1825
|
-
|
|
1003
|
+
types2
|
|
1826
1004
|
)) == null ? void 0 : _a.focus.path) || [];
|
|
1827
1005
|
},
|
|
1828
1006
|
insertBlock: (type, value) => {
|
|
@@ -1841,13 +1019,13 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1841
1019
|
)[0], [focusBlock] = Array.from(
|
|
1842
1020
|
slate.Editor.nodes(editor, {
|
|
1843
1021
|
at: editor.selection.focus.path.slice(0, 1),
|
|
1844
|
-
match: (n) => n._type ===
|
|
1022
|
+
match: (n) => n._type === types2.block.name
|
|
1845
1023
|
})
|
|
1846
1024
|
)[0] || [void 0];
|
|
1847
|
-
return focusBlock && isEqualToEmptyEditor([focusBlock],
|
|
1848
|
-
fromSlateValue(editor.children,
|
|
1025
|
+
return focusBlock && isEqualToEmptyEditor([focusBlock], types2) && slate.Transforms.removeNodes(editor, { at: editor.selection }), slate.Editor.insertNode(editor, block), editor.onChange(), ((_a = toPortableTextRange(
|
|
1026
|
+
fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1849
1027
|
editor.selection,
|
|
1850
|
-
|
|
1028
|
+
types2
|
|
1851
1029
|
)) == null ? void 0 : _a.focus.path) || [];
|
|
1852
1030
|
},
|
|
1853
1031
|
hasBlockStyle: (style) => {
|
|
@@ -1864,7 +1042,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1864
1042
|
return !1;
|
|
1865
1043
|
}
|
|
1866
1044
|
},
|
|
1867
|
-
isVoid: (element) => ![
|
|
1045
|
+
isVoid: (element) => ![types2.block.name, types2.span.name].includes(element._type),
|
|
1868
1046
|
findByPath: (path) => {
|
|
1869
1047
|
const slatePath = toSlateRange(
|
|
1870
1048
|
{ focus: { path, offset: 0 }, anchor: { path, offset: 0 } },
|
|
@@ -1874,10 +1052,10 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1874
1052
|
const [block, blockPath] = slate.Editor.node(editor, slatePath.focus.path.slice(0, 1));
|
|
1875
1053
|
if (block && blockPath && typeof block._key == "string") {
|
|
1876
1054
|
if (path.length === 1 && slatePath.focus.path.length === 1)
|
|
1877
|
-
return [fromSlateValue([block],
|
|
1055
|
+
return [fromSlateValue([block], types2.block.name)[0], [{ _key: block._key }]];
|
|
1878
1056
|
const ptBlock = fromSlateValue(
|
|
1879
1057
|
[block],
|
|
1880
|
-
|
|
1058
|
+
types2.block.name,
|
|
1881
1059
|
KEY_TO_VALUE_ELEMENT.get(editor)
|
|
1882
1060
|
)[0];
|
|
1883
1061
|
if (editor.isTextBlock(ptBlock)) {
|
|
@@ -1936,7 +1114,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1936
1114
|
if (spans.some(
|
|
1937
1115
|
([span]) => {
|
|
1938
1116
|
var _a;
|
|
1939
|
-
return !types.isPortableTextSpan(span) || !span.marks || ((_a = span.marks) == null ? void 0 : _a.length) === 0;
|
|
1117
|
+
return !types$1.isPortableTextSpan(span) || !span.marks || ((_a = span.marks) == null ? void 0 : _a.length) === 0;
|
|
1940
1118
|
}
|
|
1941
1119
|
))
|
|
1942
1120
|
return !1;
|
|
@@ -1946,7 +1124,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1946
1124
|
}, []);
|
|
1947
1125
|
return spans.every(([span]) => {
|
|
1948
1126
|
var _a;
|
|
1949
|
-
if (!types.isPortableTextSpan(span)) return !1;
|
|
1127
|
+
if (!types$1.isPortableTextSpan(span)) return !1;
|
|
1950
1128
|
const spanMarkDefs = (_a = span.marks) == null ? void 0 : _a.map(
|
|
1951
1129
|
(markKey) => {
|
|
1952
1130
|
var _a2;
|
|
@@ -1986,7 +1164,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1986
1164
|
},
|
|
1987
1165
|
{
|
|
1988
1166
|
at: editor.selection,
|
|
1989
|
-
match: (n) => n._type ===
|
|
1167
|
+
match: (n) => n._type === types2.span.name
|
|
1990
1168
|
}
|
|
1991
1169
|
), editor.onChange(), editor.selection && slate.Transforms.insertNodes(
|
|
1992
1170
|
editor,
|
|
@@ -1996,9 +1174,9 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1996
1174
|
}
|
|
1997
1175
|
);
|
|
1998
1176
|
const newPortableTextEditorSelection = toPortableTextRange(
|
|
1999
|
-
fromSlateValue(editor.children,
|
|
1177
|
+
fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
2000
1178
|
editor.selection,
|
|
2001
|
-
|
|
1179
|
+
types2
|
|
2002
1180
|
);
|
|
2003
1181
|
newPortableTextEditorSelection && (returnValue = {
|
|
2004
1182
|
spanPath: newPortableTextEditorSelection.focus.path,
|
|
@@ -2029,305 +1207,833 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
2029
1207
|
})), (options == null ? void 0 : options.mode) === "children" && (debug$i("Deleting children touched by selection"), slate.Transforms.removeNodes(editor, {
|
|
2030
1208
|
at: range,
|
|
2031
1209
|
voids: !0,
|
|
2032
|
-
match: (node) => node._type ===
|
|
1210
|
+
match: (node) => node._type === types2.span.name || // Text children
|
|
2033
1211
|
!editor.isTextBlock(node) && slate.Element.isElement(node)
|
|
2034
1212
|
})), editor.children.length === 0 && (editor.children = [editor.pteCreateEmptyBlock()]), editor.onChange();
|
|
2035
1213
|
}
|
|
2036
1214
|
}
|
|
2037
|
-
},
|
|
2038
|
-
removeAnnotation: (type) => {
|
|
2039
|
-
let { selection } = editor;
|
|
2040
|
-
if (debug$i("Removing annotation", type), selection) {
|
|
2041
|
-
if (slate.Range.isCollapsed(selection)) {
|
|
2042
|
-
const [node, nodePath] = slate.Editor.node(editor, selection, { depth: 2 });
|
|
2043
|
-
slate.Text.isText(node) && node.marks && typeof node.text == "string" && (slate.Transforms.select(editor, nodePath), selection = editor.selection);
|
|
2044
|
-
}
|
|
2045
|
-
slate.Editor.withoutNormalizing(editor, () => {
|
|
2046
|
-
if (selection && slate.Range.isExpanded(selection)) {
|
|
2047
|
-
if (selection = editor.selection, !selection)
|
|
2048
|
-
return;
|
|
2049
|
-
[
|
|
2050
|
-
...slate.Editor.nodes(editor, {
|
|
2051
|
-
at: selection,
|
|
2052
|
-
match: (node) => editor.isTextBlock(node) && Array.isArray(node.markDefs) && node.markDefs.some((def) => def._type === type.name)
|
|
2053
|
-
})
|
|
2054
|
-
].forEach(([block]) => {
|
|
2055
|
-
editor.isTextBlock(block) && Array.isArray(block.markDefs) && block.markDefs.filter((def) => def._type === type.name).forEach((def) => {
|
|
2056
|
-
slate.Editor.removeMark(editor, def._key);
|
|
2057
|
-
});
|
|
2058
|
-
});
|
|
2059
|
-
}
|
|
2060
|
-
}), slate.Editor.normalize(editor), editor.onChange();
|
|
1215
|
+
},
|
|
1216
|
+
removeAnnotation: (type) => {
|
|
1217
|
+
let { selection } = editor;
|
|
1218
|
+
if (debug$i("Removing annotation", type), selection) {
|
|
1219
|
+
if (slate.Range.isCollapsed(selection)) {
|
|
1220
|
+
const [node, nodePath] = slate.Editor.node(editor, selection, { depth: 2 });
|
|
1221
|
+
slate.Text.isText(node) && node.marks && typeof node.text == "string" && (slate.Transforms.select(editor, nodePath), selection = editor.selection);
|
|
1222
|
+
}
|
|
1223
|
+
slate.Editor.withoutNormalizing(editor, () => {
|
|
1224
|
+
if (selection && slate.Range.isExpanded(selection)) {
|
|
1225
|
+
if (selection = editor.selection, !selection)
|
|
1226
|
+
return;
|
|
1227
|
+
[
|
|
1228
|
+
...slate.Editor.nodes(editor, {
|
|
1229
|
+
at: selection,
|
|
1230
|
+
match: (node) => editor.isTextBlock(node) && Array.isArray(node.markDefs) && node.markDefs.some((def) => def._type === type.name)
|
|
1231
|
+
})
|
|
1232
|
+
].forEach(([block]) => {
|
|
1233
|
+
editor.isTextBlock(block) && Array.isArray(block.markDefs) && block.markDefs.filter((def) => def._type === type.name).forEach((def) => {
|
|
1234
|
+
slate.Editor.removeMark(editor, def._key);
|
|
1235
|
+
});
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
}), slate.Editor.normalize(editor), editor.onChange();
|
|
1239
|
+
}
|
|
1240
|
+
},
|
|
1241
|
+
getSelection: () => {
|
|
1242
|
+
let ptRange = null;
|
|
1243
|
+
if (editor.selection) {
|
|
1244
|
+
const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
|
|
1245
|
+
if (existing)
|
|
1246
|
+
return existing;
|
|
1247
|
+
ptRange = toPortableTextRange(
|
|
1248
|
+
fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1249
|
+
editor.selection,
|
|
1250
|
+
types2
|
|
1251
|
+
), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
|
|
1252
|
+
}
|
|
1253
|
+
return ptRange;
|
|
1254
|
+
},
|
|
1255
|
+
getValue: () => fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1256
|
+
isCollapsedSelection: () => !!editor.selection && slate.Range.isCollapsed(editor.selection),
|
|
1257
|
+
isExpandedSelection: () => !!editor.selection && slate.Range.isExpanded(editor.selection),
|
|
1258
|
+
insertBreak: () => {
|
|
1259
|
+
editor.insertBreak(), editor.onChange();
|
|
1260
|
+
},
|
|
1261
|
+
getFragment: () => fromSlateValue(editor.getFragment(), types2.block.name),
|
|
1262
|
+
isSelectionsOverlapping: (selectionA, selectionB) => {
|
|
1263
|
+
const rangeA = toSlateRange(selectionA, editor), rangeB = toSlateRange(selectionB, editor);
|
|
1264
|
+
return slate.Range.isRange(rangeA) && slate.Range.isRange(rangeB) && slate.Range.includes(rangeA, rangeB);
|
|
1265
|
+
}
|
|
1266
|
+
}), editor;
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
function createWithInsertBreak(types2) {
|
|
1270
|
+
return function(editor) {
|
|
1271
|
+
const { insertBreak } = editor;
|
|
1272
|
+
return editor.insertBreak = () => {
|
|
1273
|
+
if (editor.selection) {
|
|
1274
|
+
const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
|
|
1275
|
+
if (editor.isTextBlock(focusBlock)) {
|
|
1276
|
+
const [, end] = slate.Range.edges(editor.selection), isEndAtStartOfNode = slate.Editor.isStart(editor, end, end.path), isEmptyTextBlock = focusBlock && isEqualToEmptyEditor([focusBlock], types2);
|
|
1277
|
+
if (isEndAtStartOfNode && !isEmptyTextBlock) {
|
|
1278
|
+
slate.Editor.insertNode(editor, editor.pteCreateEmptyBlock());
|
|
1279
|
+
const [nextBlockPath] = slate.Path.next(focusBlockPath);
|
|
1280
|
+
slate.Transforms.select(editor, {
|
|
1281
|
+
anchor: { path: [nextBlockPath, 0], offset: 0 },
|
|
1282
|
+
focus: { path: [nextBlockPath, 0], offset: 0 }
|
|
1283
|
+
}), editor.onChange();
|
|
1284
|
+
return;
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
insertBreak();
|
|
1289
|
+
}, editor;
|
|
1290
|
+
};
|
|
1291
|
+
}
|
|
1292
|
+
function createWithMaxBlocks(maxBlocks) {
|
|
1293
|
+
return function(editor) {
|
|
1294
|
+
const { apply: apply2 } = editor;
|
|
1295
|
+
return editor.apply = (operation) => {
|
|
1296
|
+
const rows = maxBlocks;
|
|
1297
|
+
rows > 0 && editor.children.length >= rows && (operation.type === "insert_node" || operation.type === "split_node") && operation.path.length === 1 || apply2(operation);
|
|
1298
|
+
}, editor;
|
|
1299
|
+
};
|
|
1300
|
+
}
|
|
1301
|
+
const PRESERVE_KEYS = /* @__PURE__ */ new WeakMap();
|
|
1302
|
+
function withPreserveKeys(editor, fn) {
|
|
1303
|
+
const prev = isPreservingKeys(editor);
|
|
1304
|
+
PRESERVE_KEYS.set(editor, !0), fn(), PRESERVE_KEYS.set(editor, prev);
|
|
1305
|
+
}
|
|
1306
|
+
function isPreservingKeys(editor) {
|
|
1307
|
+
return PRESERVE_KEYS.get(editor);
|
|
1308
|
+
}
|
|
1309
|
+
function createWithObjectKeys(schemaTypes, keyGenerator) {
|
|
1310
|
+
return function(editor) {
|
|
1311
|
+
PRESERVE_KEYS.set(editor, !1);
|
|
1312
|
+
const { apply: apply2, normalizeNode } = editor;
|
|
1313
|
+
return editor.apply = (operation) => {
|
|
1314
|
+
if (operation.type === "split_node") {
|
|
1315
|
+
const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.properties);
|
|
1316
|
+
operation.properties = {
|
|
1317
|
+
...operation.properties,
|
|
1318
|
+
...withNewKey ? { _key: keyGenerator() } : {}
|
|
1319
|
+
};
|
|
1320
|
+
}
|
|
1321
|
+
if (operation.type === "insert_node") {
|
|
1322
|
+
const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.node);
|
|
1323
|
+
slate.Editor.isEditor(operation.node) || (operation.node = {
|
|
1324
|
+
...operation.node,
|
|
1325
|
+
...withNewKey ? { _key: keyGenerator() } : {}
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
apply2(operation);
|
|
1329
|
+
}, editor.normalizeNode = (entry) => {
|
|
1330
|
+
const [node, path] = entry;
|
|
1331
|
+
if (slate.Element.isElement(node) && node._type === schemaTypes.block.name) {
|
|
1332
|
+
node._key || slate.Transforms.setNodes(editor, { _key: keyGenerator() }, { at: path });
|
|
1333
|
+
for (const [child, childPath] of slate.Node.children(editor, path))
|
|
1334
|
+
if (!child._key) {
|
|
1335
|
+
slate.Transforms.setNodes(editor, { _key: keyGenerator() }, { at: childPath });
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
normalizeNode(entry);
|
|
1340
|
+
}, editor;
|
|
1341
|
+
};
|
|
1342
|
+
}
|
|
1343
|
+
function isHighSurrogate(char) {
|
|
1344
|
+
const charCode = char.charCodeAt(0);
|
|
1345
|
+
return charCode >= 55296 && charCode <= 56319;
|
|
1346
|
+
}
|
|
1347
|
+
function isLowSurrogate(char) {
|
|
1348
|
+
const charCode = char.charCodeAt(0);
|
|
1349
|
+
return charCode >= 56320 && charCode <= 57343;
|
|
1350
|
+
}
|
|
1351
|
+
function cloneDiff(diff2) {
|
|
1352
|
+
const [type, patch] = diff2;
|
|
1353
|
+
return [type, patch];
|
|
1354
|
+
}
|
|
1355
|
+
function getCommonOverlap(textA, textB) {
|
|
1356
|
+
let text1 = textA, text2 = textB;
|
|
1357
|
+
const text1Length = text1.length, text2Length = text2.length;
|
|
1358
|
+
if (text1Length === 0 || text2Length === 0)
|
|
1359
|
+
return 0;
|
|
1360
|
+
text1Length > text2Length ? text1 = text1.substring(text1Length - text2Length) : text1Length < text2Length && (text2 = text2.substring(0, text1Length));
|
|
1361
|
+
const textLength = Math.min(text1Length, text2Length);
|
|
1362
|
+
if (text1 === text2)
|
|
1363
|
+
return textLength;
|
|
1364
|
+
let best = 0, length = 1;
|
|
1365
|
+
for (let found = 0; found !== -1; ) {
|
|
1366
|
+
const pattern = text1.substring(textLength - length);
|
|
1367
|
+
if (found = text2.indexOf(pattern), found === -1)
|
|
1368
|
+
return best;
|
|
1369
|
+
length += found, (found === 0 || text1.substring(textLength - length) === text2.substring(0, length)) && (best = length, length++);
|
|
1370
|
+
}
|
|
1371
|
+
return best;
|
|
1372
|
+
}
|
|
1373
|
+
function getCommonPrefix(text1, text2) {
|
|
1374
|
+
if (!text1 || !text2 || text1[0] !== text2[0])
|
|
1375
|
+
return 0;
|
|
1376
|
+
let pointerMin = 0, pointerMax = Math.min(text1.length, text2.length), pointerMid = pointerMax, pointerStart = 0;
|
|
1377
|
+
for (; pointerMin < pointerMid; )
|
|
1378
|
+
text1.substring(pointerStart, pointerMid) === text2.substring(pointerStart, pointerMid) ? (pointerMin = pointerMid, pointerStart = pointerMin) : pointerMax = pointerMid, pointerMid = Math.floor((pointerMax - pointerMin) / 2 + pointerMin);
|
|
1379
|
+
return pointerMid;
|
|
1380
|
+
}
|
|
1381
|
+
function getCommonSuffix(text1, text2) {
|
|
1382
|
+
if (!text1 || !text2 || text1[text1.length - 1] !== text2[text2.length - 1])
|
|
1383
|
+
return 0;
|
|
1384
|
+
let pointerMin = 0, pointerMax = Math.min(text1.length, text2.length), pointerMid = pointerMax, pointerEnd = 0;
|
|
1385
|
+
for (; pointerMin < pointerMid; )
|
|
1386
|
+
text1.substring(text1.length - pointerMid, text1.length - pointerEnd) === text2.substring(text2.length - pointerMid, text2.length - pointerEnd) ? (pointerMin = pointerMid, pointerEnd = pointerMin) : pointerMax = pointerMid, pointerMid = Math.floor((pointerMax - pointerMin) / 2 + pointerMin);
|
|
1387
|
+
return pointerMid;
|
|
1388
|
+
}
|
|
1389
|
+
function cleanupSemantic(rawDiffs) {
|
|
1390
|
+
let diffs = rawDiffs.map((diff2) => cloneDiff(diff2)), hasChanges = !1;
|
|
1391
|
+
const equalities = [];
|
|
1392
|
+
let equalitiesLength = 0, lastEquality = null, pointer = 0, lengthInsertions1 = 0, lengthDeletions1 = 0, lengthInsertions2 = 0, lengthDeletions2 = 0;
|
|
1393
|
+
for (; pointer < diffs.length; )
|
|
1394
|
+
diffs[pointer][0] === DIFF_EQUAL ? (equalities[equalitiesLength++] = pointer, lengthInsertions1 = lengthInsertions2, lengthDeletions1 = lengthDeletions2, lengthInsertions2 = 0, lengthDeletions2 = 0, lastEquality = diffs[pointer][1]) : (diffs[pointer][0] === DIFF_INSERT ? lengthInsertions2 += diffs[pointer][1].length : lengthDeletions2 += diffs[pointer][1].length, lastEquality && lastEquality.length <= Math.max(lengthInsertions1, lengthDeletions1) && lastEquality.length <= Math.max(lengthInsertions2, lengthDeletions2) && (diffs.splice(equalities[equalitiesLength - 1], 0, [DIFF_DELETE, lastEquality]), diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT, equalitiesLength--, equalitiesLength--, pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1, lengthInsertions1 = 0, lengthDeletions1 = 0, lengthInsertions2 = 0, lengthDeletions2 = 0, lastEquality = null, hasChanges = !0)), pointer++;
|
|
1395
|
+
for (hasChanges && (diffs = cleanupMerge(diffs)), diffs = cleanupSemanticLossless(diffs), pointer = 1; pointer < diffs.length; ) {
|
|
1396
|
+
if (diffs[pointer - 1][0] === DIFF_DELETE && diffs[pointer][0] === DIFF_INSERT) {
|
|
1397
|
+
const deletion = diffs[pointer - 1][1], insertion = diffs[pointer][1], overlapLength1 = getCommonOverlap(deletion, insertion), overlapLength2 = getCommonOverlap(insertion, deletion);
|
|
1398
|
+
overlapLength1 >= overlapLength2 ? (overlapLength1 >= deletion.length / 2 || overlapLength1 >= insertion.length / 2) && (diffs.splice(pointer, 0, [DIFF_EQUAL, insertion.substring(0, overlapLength1)]), diffs[pointer - 1][1] = deletion.substring(0, deletion.length - overlapLength1), diffs[pointer + 1][1] = insertion.substring(overlapLength1), pointer++) : (overlapLength2 >= deletion.length / 2 || overlapLength2 >= insertion.length / 2) && (diffs.splice(pointer, 0, [DIFF_EQUAL, deletion.substring(0, overlapLength2)]), diffs[pointer - 1][0] = DIFF_INSERT, diffs[pointer - 1][1] = insertion.substring(0, insertion.length - overlapLength2), diffs[pointer + 1][0] = DIFF_DELETE, diffs[pointer + 1][1] = deletion.substring(overlapLength2), pointer++), pointer++;
|
|
1399
|
+
}
|
|
1400
|
+
pointer++;
|
|
1401
|
+
}
|
|
1402
|
+
return diffs;
|
|
1403
|
+
}
|
|
1404
|
+
const nonAlphaNumericRegex = /[^a-zA-Z0-9]/, whitespaceRegex = /\s/, linebreakRegex = /[\r\n]/, blanklineEndRegex = /\n\r?\n$/, blanklineStartRegex = /^\r?\n\r?\n/;
|
|
1405
|
+
function cleanupSemanticLossless(rawDiffs) {
|
|
1406
|
+
const diffs = rawDiffs.map((diff2) => cloneDiff(diff2));
|
|
1407
|
+
function diffCleanupSemanticScore(one, two) {
|
|
1408
|
+
if (!one || !two)
|
|
1409
|
+
return 6;
|
|
1410
|
+
const char1 = one.charAt(one.length - 1), char2 = two.charAt(0), nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex), nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex), whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex), whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex), lineBreak1 = whitespace1 && char1.match(linebreakRegex), lineBreak2 = whitespace2 && char2.match(linebreakRegex), blankLine1 = lineBreak1 && one.match(blanklineEndRegex), blankLine2 = lineBreak2 && two.match(blanklineStartRegex);
|
|
1411
|
+
return blankLine1 || blankLine2 ? 5 : lineBreak1 || lineBreak2 ? 4 : nonAlphaNumeric1 && !whitespace1 && whitespace2 ? 3 : whitespace1 || whitespace2 ? 2 : nonAlphaNumeric1 || nonAlphaNumeric2 ? 1 : 0;
|
|
1412
|
+
}
|
|
1413
|
+
let pointer = 1;
|
|
1414
|
+
for (; pointer < diffs.length - 1; ) {
|
|
1415
|
+
if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
|
|
1416
|
+
let equality1 = diffs[pointer - 1][1], edit = diffs[pointer][1], equality2 = diffs[pointer + 1][1];
|
|
1417
|
+
const commonOffset = getCommonSuffix(equality1, edit);
|
|
1418
|
+
if (commonOffset) {
|
|
1419
|
+
const commonString = edit.substring(edit.length - commonOffset);
|
|
1420
|
+
equality1 = equality1.substring(0, equality1.length - commonOffset), edit = commonString + edit.substring(0, edit.length - commonOffset), equality2 = commonString + equality2;
|
|
1421
|
+
}
|
|
1422
|
+
let bestEquality1 = equality1, bestEdit = edit, bestEquality2 = equality2, bestScore = diffCleanupSemanticScore(equality1, edit) + diffCleanupSemanticScore(edit, equality2);
|
|
1423
|
+
for (; edit.charAt(0) === equality2.charAt(0); ) {
|
|
1424
|
+
equality1 += edit.charAt(0), edit = edit.substring(1) + equality2.charAt(0), equality2 = equality2.substring(1);
|
|
1425
|
+
const score = diffCleanupSemanticScore(equality1, edit) + diffCleanupSemanticScore(edit, equality2);
|
|
1426
|
+
score >= bestScore && (bestScore = score, bestEquality1 = equality1, bestEdit = edit, bestEquality2 = equality2);
|
|
1427
|
+
}
|
|
1428
|
+
diffs[pointer - 1][1] !== bestEquality1 && (bestEquality1 ? diffs[pointer - 1][1] = bestEquality1 : (diffs.splice(pointer - 1, 1), pointer--), diffs[pointer][1] = bestEdit, bestEquality2 ? diffs[pointer + 1][1] = bestEquality2 : (diffs.splice(pointer + 1, 1), pointer--));
|
|
1429
|
+
}
|
|
1430
|
+
pointer++;
|
|
1431
|
+
}
|
|
1432
|
+
return diffs;
|
|
1433
|
+
}
|
|
1434
|
+
function cleanupMerge(rawDiffs) {
|
|
1435
|
+
let diffs = rawDiffs.map((diff2) => cloneDiff(diff2));
|
|
1436
|
+
diffs.push([DIFF_EQUAL, ""]);
|
|
1437
|
+
let pointer = 0, countDelete = 0, countInsert = 0, textDelete = "", textInsert = "", commonlength;
|
|
1438
|
+
for (; pointer < diffs.length; )
|
|
1439
|
+
switch (diffs[pointer][0]) {
|
|
1440
|
+
case DIFF_INSERT:
|
|
1441
|
+
countInsert++, textInsert += diffs[pointer][1], pointer++;
|
|
1442
|
+
break;
|
|
1443
|
+
case DIFF_DELETE:
|
|
1444
|
+
countDelete++, textDelete += diffs[pointer][1], pointer++;
|
|
1445
|
+
break;
|
|
1446
|
+
case DIFF_EQUAL:
|
|
1447
|
+
countDelete + countInsert > 1 ? (countDelete !== 0 && countInsert !== 0 && (commonlength = getCommonPrefix(textInsert, textDelete), commonlength !== 0 && (pointer - countDelete - countInsert > 0 && diffs[pointer - countDelete - countInsert - 1][0] === DIFF_EQUAL ? diffs[pointer - countDelete - countInsert - 1][1] += textInsert.substring(0, commonlength) : (diffs.splice(0, 0, [DIFF_EQUAL, textInsert.substring(0, commonlength)]), pointer++), textInsert = textInsert.substring(commonlength), textDelete = textDelete.substring(commonlength)), commonlength = getCommonSuffix(textInsert, textDelete), commonlength !== 0 && (diffs[pointer][1] = textInsert.substring(textInsert.length - commonlength) + diffs[pointer][1], textInsert = textInsert.substring(0, textInsert.length - commonlength), textDelete = textDelete.substring(0, textDelete.length - commonlength))), pointer -= countDelete + countInsert, diffs.splice(pointer, countDelete + countInsert), textDelete.length && (diffs.splice(pointer, 0, [DIFF_DELETE, textDelete]), pointer++), textInsert.length && (diffs.splice(pointer, 0, [DIFF_INSERT, textInsert]), pointer++), pointer++) : pointer !== 0 && diffs[pointer - 1][0] === DIFF_EQUAL ? (diffs[pointer - 1][1] += diffs[pointer][1], diffs.splice(pointer, 1)) : pointer++, countInsert = 0, countDelete = 0, textDelete = "", textInsert = "";
|
|
1448
|
+
break;
|
|
1449
|
+
default:
|
|
1450
|
+
throw new Error("Unknown diff operation");
|
|
1451
|
+
}
|
|
1452
|
+
diffs[diffs.length - 1][1] === "" && diffs.pop();
|
|
1453
|
+
let hasChanges = !1;
|
|
1454
|
+
for (pointer = 1; pointer < diffs.length - 1; )
|
|
1455
|
+
diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL && (diffs[pointer][1].substring(diffs[pointer][1].length - diffs[pointer - 1][1].length) === diffs[pointer - 1][1] ? (diffs[pointer][1] = diffs[pointer - 1][1] + diffs[pointer][1].substring(0, diffs[pointer][1].length - diffs[pointer - 1][1].length), diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1], diffs.splice(pointer - 1, 1), hasChanges = !0) : diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) === diffs[pointer + 1][1] && (diffs[pointer - 1][1] += diffs[pointer + 1][1], diffs[pointer][1] = diffs[pointer][1].substring(diffs[pointer + 1][1].length) + diffs[pointer + 1][1], diffs.splice(pointer + 1, 1), hasChanges = !0)), pointer++;
|
|
1456
|
+
return hasChanges && (diffs = cleanupMerge(diffs)), diffs;
|
|
1457
|
+
}
|
|
1458
|
+
function trueCount() {
|
|
1459
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++)
|
|
1460
|
+
args[_key] = arguments[_key];
|
|
1461
|
+
return args.reduce((n, bool) => n + (bool ? 1 : 0), 0);
|
|
1462
|
+
}
|
|
1463
|
+
function cleanupEfficiency(rawDiffs) {
|
|
1464
|
+
let editCost = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 4, diffs = rawDiffs.map((diff2) => cloneDiff(diff2)), hasChanges = !1;
|
|
1465
|
+
const equalities = [];
|
|
1466
|
+
let equalitiesLength = 0, lastEquality = null, pointer = 0, preIns = !1, preDel = !1, postIns = !1, postDel = !1;
|
|
1467
|
+
for (; pointer < diffs.length; )
|
|
1468
|
+
diffs[pointer][0] === DIFF_EQUAL ? (diffs[pointer][1].length < editCost && (postIns || postDel) ? (equalities[equalitiesLength++] = pointer, preIns = postIns, preDel = postDel, lastEquality = diffs[pointer][1]) : (equalitiesLength = 0, lastEquality = null), postIns = !1, postDel = !1) : (diffs[pointer][0] === DIFF_DELETE ? postDel = !0 : postIns = !0, lastEquality && (preIns && preDel && postIns && postDel || lastEquality.length < editCost / 2 && trueCount(preIns, preDel, postIns, postDel) === 3) && (diffs.splice(equalities[equalitiesLength - 1], 0, [DIFF_DELETE, lastEquality]), diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT, equalitiesLength--, lastEquality = null, preIns && preDel ? (postIns = !0, postDel = !0, equalitiesLength = 0) : (equalitiesLength--, pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1, postIns = !1, postDel = !1), hasChanges = !0)), pointer++;
|
|
1469
|
+
return hasChanges && (diffs = cleanupMerge(diffs)), diffs;
|
|
1470
|
+
}
|
|
1471
|
+
function bisect(text1, text2, deadline) {
|
|
1472
|
+
const text1Length = text1.length, text2Length = text2.length, maxD = Math.ceil((text1Length + text2Length) / 2), vOffset = maxD, vLength = 2 * maxD, v1 = new Array(vLength), v2 = new Array(vLength);
|
|
1473
|
+
for (let x = 0; x < vLength; x++)
|
|
1474
|
+
v1[x] = -1, v2[x] = -1;
|
|
1475
|
+
v1[vOffset + 1] = 0, v2[vOffset + 1] = 0;
|
|
1476
|
+
const delta = text1Length - text2Length, front = delta % 2 !== 0;
|
|
1477
|
+
let k1start = 0, k1end = 0, k2start = 0, k2end = 0;
|
|
1478
|
+
for (let d = 0; d < maxD && !(Date.now() > deadline); d++) {
|
|
1479
|
+
for (let k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
|
|
1480
|
+
const k1Offset = vOffset + k1;
|
|
1481
|
+
let x1;
|
|
1482
|
+
k1 === -d || k1 !== d && v1[k1Offset - 1] < v1[k1Offset + 1] ? x1 = v1[k1Offset + 1] : x1 = v1[k1Offset - 1] + 1;
|
|
1483
|
+
let y1 = x1 - k1;
|
|
1484
|
+
for (; x1 < text1Length && y1 < text2Length && text1.charAt(x1) === text2.charAt(y1); )
|
|
1485
|
+
x1++, y1++;
|
|
1486
|
+
if (v1[k1Offset] = x1, x1 > text1Length)
|
|
1487
|
+
k1end += 2;
|
|
1488
|
+
else if (y1 > text2Length)
|
|
1489
|
+
k1start += 2;
|
|
1490
|
+
else if (front) {
|
|
1491
|
+
const k2Offset = vOffset + delta - k1;
|
|
1492
|
+
if (k2Offset >= 0 && k2Offset < vLength && v2[k2Offset] !== -1) {
|
|
1493
|
+
const x2 = text1Length - v2[k2Offset];
|
|
1494
|
+
if (x1 >= x2)
|
|
1495
|
+
return bisectSplit(text1, text2, x1, y1, deadline);
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
for (let k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
|
|
1500
|
+
const k2Offset = vOffset + k2;
|
|
1501
|
+
let x2;
|
|
1502
|
+
k2 === -d || k2 !== d && v2[k2Offset - 1] < v2[k2Offset + 1] ? x2 = v2[k2Offset + 1] : x2 = v2[k2Offset - 1] + 1;
|
|
1503
|
+
let y2 = x2 - k2;
|
|
1504
|
+
for (; x2 < text1Length && y2 < text2Length && text1.charAt(text1Length - x2 - 1) === text2.charAt(text2Length - y2 - 1); )
|
|
1505
|
+
x2++, y2++;
|
|
1506
|
+
if (v2[k2Offset] = x2, x2 > text1Length)
|
|
1507
|
+
k2end += 2;
|
|
1508
|
+
else if (y2 > text2Length)
|
|
1509
|
+
k2start += 2;
|
|
1510
|
+
else if (!front) {
|
|
1511
|
+
const k1Offset = vOffset + delta - k2;
|
|
1512
|
+
if (k1Offset >= 0 && k1Offset < vLength && v1[k1Offset] !== -1) {
|
|
1513
|
+
const x1 = v1[k1Offset], y1 = vOffset + x1 - k1Offset;
|
|
1514
|
+
if (x2 = text1Length - x2, x1 >= x2)
|
|
1515
|
+
return bisectSplit(text1, text2, x1, y1, deadline);
|
|
2061
1516
|
}
|
|
2062
|
-
}
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
return [[DIFF_DELETE, text1], [DIFF_INSERT, text2]];
|
|
1521
|
+
}
|
|
1522
|
+
function bisectSplit(text1, text2, x, y, deadline) {
|
|
1523
|
+
const text1a = text1.substring(0, x), text2a = text2.substring(0, y), text1b = text1.substring(x), text2b = text2.substring(y), diffs = doDiff(text1a, text2a, {
|
|
1524
|
+
checkLines: !1,
|
|
1525
|
+
deadline
|
|
1526
|
+
}), diffsb = doDiff(text1b, text2b, {
|
|
1527
|
+
checkLines: !1,
|
|
1528
|
+
deadline
|
|
1529
|
+
});
|
|
1530
|
+
return diffs.concat(diffsb);
|
|
1531
|
+
}
|
|
1532
|
+
function findHalfMatch(text1, text2) {
|
|
1533
|
+
if ((arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1) <= 0)
|
|
1534
|
+
return null;
|
|
1535
|
+
const longText = text1.length > text2.length ? text1 : text2, shortText = text1.length > text2.length ? text2 : text1;
|
|
1536
|
+
if (longText.length < 4 || shortText.length * 2 < longText.length)
|
|
1537
|
+
return null;
|
|
1538
|
+
const halfMatch1 = halfMatchI(longText, shortText, Math.ceil(longText.length / 4)), halfMatch2 = halfMatchI(longText, shortText, Math.ceil(longText.length / 2));
|
|
1539
|
+
let halfMatch;
|
|
1540
|
+
if (halfMatch1 && halfMatch2)
|
|
1541
|
+
halfMatch = halfMatch1[4].length > halfMatch2[4].length ? halfMatch1 : halfMatch2;
|
|
1542
|
+
else {
|
|
1543
|
+
if (!halfMatch1 && !halfMatch2)
|
|
1544
|
+
return null;
|
|
1545
|
+
halfMatch2 ? halfMatch1 || (halfMatch = halfMatch2) : halfMatch = halfMatch1;
|
|
1546
|
+
}
|
|
1547
|
+
if (!halfMatch)
|
|
1548
|
+
throw new Error("Unable to find a half match.");
|
|
1549
|
+
let text1A, text1B, text2A, text2B;
|
|
1550
|
+
text1.length > text2.length ? (text1A = halfMatch[0], text1B = halfMatch[1], text2A = halfMatch[2], text2B = halfMatch[3]) : (text2A = halfMatch[0], text2B = halfMatch[1], text1A = halfMatch[2], text1B = halfMatch[3]);
|
|
1551
|
+
const midCommon = halfMatch[4];
|
|
1552
|
+
return [text1A, text1B, text2A, text2B, midCommon];
|
|
1553
|
+
}
|
|
1554
|
+
function halfMatchI(longText, shortText, i) {
|
|
1555
|
+
const seed = longText.slice(i, i + Math.floor(longText.length / 4));
|
|
1556
|
+
let j = -1, bestCommon = "", bestLongTextA, bestLongTextB, bestShortTextA, bestShortTextB;
|
|
1557
|
+
for (; (j = shortText.indexOf(seed, j + 1)) !== -1; ) {
|
|
1558
|
+
const prefixLength = getCommonPrefix(longText.slice(i), shortText.slice(j)), suffixLength = getCommonSuffix(longText.slice(0, i), shortText.slice(0, j));
|
|
1559
|
+
bestCommon.length < suffixLength + prefixLength && (bestCommon = shortText.slice(j - suffixLength, j) + shortText.slice(j, j + prefixLength), bestLongTextA = longText.slice(0, i - suffixLength), bestLongTextB = longText.slice(i + prefixLength), bestShortTextA = shortText.slice(0, j - suffixLength), bestShortTextB = shortText.slice(j + prefixLength));
|
|
1560
|
+
}
|
|
1561
|
+
return bestCommon.length * 2 >= longText.length ? [bestLongTextA || "", bestLongTextB || "", bestShortTextA || "", bestShortTextB || "", bestCommon || ""] : null;
|
|
1562
|
+
}
|
|
1563
|
+
function charsToLines(diffs, lineArray) {
|
|
1564
|
+
for (let x = 0; x < diffs.length; x++) {
|
|
1565
|
+
const chars = diffs[x][1], text = [];
|
|
1566
|
+
for (let y = 0; y < chars.length; y++)
|
|
1567
|
+
text[y] = lineArray[chars.charCodeAt(y)];
|
|
1568
|
+
diffs[x][1] = text.join("");
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
function linesToChars(textA, textB) {
|
|
1572
|
+
const lineArray = [], lineHash = {};
|
|
1573
|
+
lineArray[0] = "";
|
|
1574
|
+
function diffLinesToMunge(text) {
|
|
1575
|
+
let chars = "", lineStart = 0, lineEnd = -1, lineArrayLength = lineArray.length;
|
|
1576
|
+
for (; lineEnd < text.length - 1; ) {
|
|
1577
|
+
lineEnd = text.indexOf(`
|
|
1578
|
+
`, lineStart), lineEnd === -1 && (lineEnd = text.length - 1);
|
|
1579
|
+
let line = text.slice(lineStart, lineEnd + 1);
|
|
1580
|
+
(lineHash.hasOwnProperty ? lineHash.hasOwnProperty(line) : lineHash[line] !== void 0) ? chars += String.fromCharCode(lineHash[line]) : (lineArrayLength === maxLines && (line = text.slice(lineStart), lineEnd = text.length), chars += String.fromCharCode(lineArrayLength), lineHash[line] = lineArrayLength, lineArray[lineArrayLength++] = line), lineStart = lineEnd + 1;
|
|
1581
|
+
}
|
|
1582
|
+
return chars;
|
|
1583
|
+
}
|
|
1584
|
+
let maxLines = 4e4;
|
|
1585
|
+
const chars1 = diffLinesToMunge(textA);
|
|
1586
|
+
maxLines = 65535;
|
|
1587
|
+
const chars2 = diffLinesToMunge(textB);
|
|
1588
|
+
return {
|
|
1589
|
+
chars1,
|
|
1590
|
+
chars2,
|
|
1591
|
+
lineArray
|
|
1592
|
+
};
|
|
1593
|
+
}
|
|
1594
|
+
function doLineModeDiff(textA, textB, opts) {
|
|
1595
|
+
let text1 = textA, text2 = textB;
|
|
1596
|
+
const a = linesToChars(text1, text2);
|
|
1597
|
+
text1 = a.chars1, text2 = a.chars2;
|
|
1598
|
+
const linearray = a.lineArray;
|
|
1599
|
+
let diffs = doDiff(text1, text2, {
|
|
1600
|
+
checkLines: !1,
|
|
1601
|
+
deadline: opts.deadline
|
|
1602
|
+
});
|
|
1603
|
+
charsToLines(diffs, linearray), diffs = cleanupSemantic(diffs), diffs.push([DIFF_EQUAL, ""]);
|
|
1604
|
+
let pointer = 0, countDelete = 0, countInsert = 0, textDelete = "", textInsert = "";
|
|
1605
|
+
for (; pointer < diffs.length; ) {
|
|
1606
|
+
switch (diffs[pointer][0]) {
|
|
1607
|
+
case DIFF_INSERT:
|
|
1608
|
+
countInsert++, textInsert += diffs[pointer][1];
|
|
1609
|
+
break;
|
|
1610
|
+
case DIFF_DELETE:
|
|
1611
|
+
countDelete++, textDelete += diffs[pointer][1];
|
|
1612
|
+
break;
|
|
1613
|
+
case DIFF_EQUAL:
|
|
1614
|
+
if (countDelete >= 1 && countInsert >= 1) {
|
|
1615
|
+
diffs.splice(pointer - countDelete - countInsert, countDelete + countInsert), pointer = pointer - countDelete - countInsert;
|
|
1616
|
+
const aa = doDiff(textDelete, textInsert, {
|
|
1617
|
+
checkLines: !1,
|
|
1618
|
+
deadline: opts.deadline
|
|
1619
|
+
});
|
|
1620
|
+
for (let j = aa.length - 1; j >= 0; j--)
|
|
1621
|
+
diffs.splice(pointer, 0, aa[j]);
|
|
1622
|
+
pointer += aa.length;
|
|
2074
1623
|
}
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
1624
|
+
countInsert = 0, countDelete = 0, textDelete = "", textInsert = "";
|
|
1625
|
+
break;
|
|
1626
|
+
default:
|
|
1627
|
+
throw new Error("Unknown diff operation.");
|
|
1628
|
+
}
|
|
1629
|
+
pointer++;
|
|
1630
|
+
}
|
|
1631
|
+
return diffs.pop(), diffs;
|
|
1632
|
+
}
|
|
1633
|
+
function computeDiff(text1, text2, opts) {
|
|
1634
|
+
let diffs;
|
|
1635
|
+
if (!text1)
|
|
1636
|
+
return [[DIFF_INSERT, text2]];
|
|
1637
|
+
if (!text2)
|
|
1638
|
+
return [[DIFF_DELETE, text1]];
|
|
1639
|
+
const longtext = text1.length > text2.length ? text1 : text2, shorttext = text1.length > text2.length ? text2 : text1, i = longtext.indexOf(shorttext);
|
|
1640
|
+
if (i !== -1)
|
|
1641
|
+
return diffs = [[DIFF_INSERT, longtext.substring(0, i)], [DIFF_EQUAL, shorttext], [DIFF_INSERT, longtext.substring(i + shorttext.length)]], text1.length > text2.length && (diffs[0][0] = DIFF_DELETE, diffs[2][0] = DIFF_DELETE), diffs;
|
|
1642
|
+
if (shorttext.length === 1)
|
|
1643
|
+
return [[DIFF_DELETE, text1], [DIFF_INSERT, text2]];
|
|
1644
|
+
const halfMatch = findHalfMatch(text1, text2);
|
|
1645
|
+
if (halfMatch) {
|
|
1646
|
+
const text1A = halfMatch[0], text1B = halfMatch[1], text2A = halfMatch[2], text2B = halfMatch[3], midCommon = halfMatch[4], diffsA = doDiff(text1A, text2A, opts), diffsB = doDiff(text1B, text2B, opts);
|
|
1647
|
+
return diffsA.concat([[DIFF_EQUAL, midCommon]], diffsB);
|
|
1648
|
+
}
|
|
1649
|
+
return opts.checkLines && text1.length > 100 && text2.length > 100 ? doLineModeDiff(text1, text2, opts) : bisect(text1, text2, opts.deadline);
|
|
1650
|
+
}
|
|
1651
|
+
const DIFF_DELETE = -1, DIFF_INSERT = 1, DIFF_EQUAL = 0;
|
|
1652
|
+
function diff(textA, textB, opts) {
|
|
1653
|
+
if (textA === null || textB === null)
|
|
1654
|
+
throw new Error("Null input. (diff)");
|
|
1655
|
+
const diffs = doDiff(textA, textB, createInternalOpts(opts || {}));
|
|
1656
|
+
return adjustDiffForSurrogatePairs(diffs), diffs;
|
|
1657
|
+
}
|
|
1658
|
+
function doDiff(textA, textB, options) {
|
|
1659
|
+
let text1 = textA, text2 = textB;
|
|
1660
|
+
if (text1 === text2)
|
|
1661
|
+
return text1 ? [[DIFF_EQUAL, text1]] : [];
|
|
1662
|
+
let commonlength = getCommonPrefix(text1, text2);
|
|
1663
|
+
const commonprefix = text1.substring(0, commonlength);
|
|
1664
|
+
text1 = text1.substring(commonlength), text2 = text2.substring(commonlength), commonlength = getCommonSuffix(text1, text2);
|
|
1665
|
+
const commonsuffix = text1.substring(text1.length - commonlength);
|
|
1666
|
+
text1 = text1.substring(0, text1.length - commonlength), text2 = text2.substring(0, text2.length - commonlength);
|
|
1667
|
+
let diffs = computeDiff(text1, text2, options);
|
|
1668
|
+
return commonprefix && diffs.unshift([DIFF_EQUAL, commonprefix]), commonsuffix && diffs.push([DIFF_EQUAL, commonsuffix]), diffs = cleanupMerge(diffs), diffs;
|
|
1669
|
+
}
|
|
1670
|
+
function createDeadLine(timeout) {
|
|
1671
|
+
let t = 1;
|
|
1672
|
+
return typeof timeout < "u" && (t = timeout <= 0 ? Number.MAX_VALUE : timeout), Date.now() + t * 1e3;
|
|
1673
|
+
}
|
|
1674
|
+
function createInternalOpts(opts) {
|
|
1675
|
+
return {
|
|
1676
|
+
checkLines: !0,
|
|
1677
|
+
deadline: createDeadLine(opts.timeout || 1),
|
|
1678
|
+
...opts
|
|
2089
1679
|
};
|
|
2090
1680
|
}
|
|
2091
|
-
function
|
|
2092
|
-
return
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
1681
|
+
function combineChar(data, char, dir) {
|
|
1682
|
+
return dir === 1 ? data + char : char + data;
|
|
1683
|
+
}
|
|
1684
|
+
function splitChar(data, dir) {
|
|
1685
|
+
return dir === 1 ? [data.substring(0, data.length - 1), data[data.length - 1]] : [data.substring(1), data[0]];
|
|
1686
|
+
}
|
|
1687
|
+
function hasSharedChar(diffs, i, j, dir) {
|
|
1688
|
+
return dir === 1 ? diffs[i][1][diffs[i][1].length - 1] === diffs[j][1][diffs[j][1].length - 1] : diffs[i][1][0] === diffs[j][1][0];
|
|
1689
|
+
}
|
|
1690
|
+
function deisolateChar(diffs, i, dir) {
|
|
1691
|
+
const inv = dir === 1 ? -1 : 1;
|
|
1692
|
+
let insertIdx = null, deleteIdx = null, j = i + dir;
|
|
1693
|
+
for (; j >= 0 && j < diffs.length && (insertIdx === null || deleteIdx === null); j += dir) {
|
|
1694
|
+
const [op, text2] = diffs[j];
|
|
1695
|
+
if (text2.length !== 0) {
|
|
1696
|
+
if (op === DIFF_INSERT) {
|
|
1697
|
+
insertIdx === null && (insertIdx = j);
|
|
1698
|
+
continue;
|
|
1699
|
+
} else if (op === DIFF_DELETE) {
|
|
1700
|
+
deleteIdx === null && (deleteIdx = j);
|
|
1701
|
+
continue;
|
|
1702
|
+
} else if (op === DIFF_EQUAL) {
|
|
1703
|
+
if (insertIdx === null && deleteIdx === null) {
|
|
1704
|
+
const [rest, char2] = splitChar(diffs[i][1], dir);
|
|
1705
|
+
diffs[i][1] = rest, diffs[j][1] = combineChar(diffs[j][1], char2, inv);
|
|
1706
|
+
return;
|
|
2108
1707
|
}
|
|
1708
|
+
break;
|
|
2109
1709
|
}
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
if (insertIdx !== null && deleteIdx !== null && hasSharedChar(diffs, insertIdx, deleteIdx, dir)) {
|
|
1713
|
+
const [insertText, insertChar] = splitChar(diffs[insertIdx][1], inv), [deleteText] = splitChar(diffs[deleteIdx][1], inv);
|
|
1714
|
+
diffs[insertIdx][1] = insertText, diffs[deleteIdx][1] = deleteText, diffs[i][1] = combineChar(diffs[i][1], insertChar, dir);
|
|
1715
|
+
return;
|
|
1716
|
+
}
|
|
1717
|
+
const [text, char] = splitChar(diffs[i][1], dir);
|
|
1718
|
+
diffs[i][1] = text, insertIdx === null ? (diffs.splice(j, 0, [DIFF_INSERT, char]), deleteIdx !== null && deleteIdx >= j && deleteIdx++) : diffs[insertIdx][1] = combineChar(diffs[insertIdx][1], char, inv), deleteIdx === null ? diffs.splice(j, 0, [DIFF_DELETE, char]) : diffs[deleteIdx][1] = combineChar(diffs[deleteIdx][1], char, inv);
|
|
2113
1719
|
}
|
|
2114
|
-
function
|
|
2115
|
-
|
|
2116
|
-
const
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
1720
|
+
function adjustDiffForSurrogatePairs(diffs) {
|
|
1721
|
+
for (let i = 0; i < diffs.length; i++) {
|
|
1722
|
+
const [diffType, diffText] = diffs[i];
|
|
1723
|
+
if (diffText.length === 0) continue;
|
|
1724
|
+
const firstChar = diffText[0], lastChar = diffText[diffText.length - 1];
|
|
1725
|
+
isHighSurrogate(lastChar) && diffType === DIFF_EQUAL && deisolateChar(diffs, i, 1), isLowSurrogate(firstChar) && diffType === DIFF_EQUAL && deisolateChar(diffs, i, -1);
|
|
1726
|
+
}
|
|
1727
|
+
for (let i = 0; i < diffs.length; i++)
|
|
1728
|
+
diffs[i][1].length === 0 && diffs.splice(i, 1);
|
|
1729
|
+
}
|
|
1730
|
+
const DEFAULT_OPTIONS = {
|
|
1731
|
+
/**
|
|
1732
|
+
* At what point is no match declared (0.0 = perfection, 1.0 = very loose).
|
|
1733
|
+
*/
|
|
1734
|
+
threshold: 0.5,
|
|
1735
|
+
/**
|
|
1736
|
+
* How far to search for a match (0 = exact location, 1000+ = broad match).
|
|
1737
|
+
* A match this many characters away from the expected location will add
|
|
1738
|
+
* 1.0 to the score (0.0 is a perfect match).
|
|
1739
|
+
*/
|
|
1740
|
+
distance: 1e3
|
|
1741
|
+
};
|
|
1742
|
+
function applyDefaults(options) {
|
|
1743
|
+
return {
|
|
1744
|
+
...DEFAULT_OPTIONS,
|
|
1745
|
+
...options
|
|
2121
1746
|
};
|
|
2122
1747
|
}
|
|
2123
|
-
const
|
|
2124
|
-
function
|
|
2125
|
-
|
|
2126
|
-
|
|
1748
|
+
const MAX_BITS$1 = 32;
|
|
1749
|
+
function bitap(text, pattern, loc) {
|
|
1750
|
+
let opts = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {};
|
|
1751
|
+
if (pattern.length > MAX_BITS$1)
|
|
1752
|
+
throw new Error("Pattern too long for this browser.");
|
|
1753
|
+
const options = applyDefaults(opts), s = getAlphabetFromPattern(pattern);
|
|
1754
|
+
function getBitapScore(e, x) {
|
|
1755
|
+
const accuracy = e / pattern.length, proximity = Math.abs(loc - x);
|
|
1756
|
+
return options.distance ? accuracy + proximity / options.distance : proximity ? 1 : accuracy;
|
|
1757
|
+
}
|
|
1758
|
+
let scoreThreshold = options.threshold, bestLoc = text.indexOf(pattern, loc);
|
|
1759
|
+
bestLoc !== -1 && (scoreThreshold = Math.min(getBitapScore(0, bestLoc), scoreThreshold), bestLoc = text.lastIndexOf(pattern, loc + pattern.length), bestLoc !== -1 && (scoreThreshold = Math.min(getBitapScore(0, bestLoc), scoreThreshold)));
|
|
1760
|
+
const matchmask = 1 << pattern.length - 1;
|
|
1761
|
+
bestLoc = -1;
|
|
1762
|
+
let binMin, binMid, binMax = pattern.length + text.length, lastRd = [];
|
|
1763
|
+
for (let d = 0; d < pattern.length; d++) {
|
|
1764
|
+
for (binMin = 0, binMid = binMax; binMin < binMid; )
|
|
1765
|
+
getBitapScore(d, loc + binMid) <= scoreThreshold ? binMin = binMid : binMax = binMid, binMid = Math.floor((binMax - binMin) / 2 + binMin);
|
|
1766
|
+
binMax = binMid;
|
|
1767
|
+
let start = Math.max(1, loc - binMid + 1);
|
|
1768
|
+
const finish = Math.min(loc + binMid, text.length) + pattern.length, rd = new Array(finish + 2);
|
|
1769
|
+
rd[finish + 1] = (1 << d) - 1;
|
|
1770
|
+
for (let j = finish; j >= start; j--) {
|
|
1771
|
+
const charMatch = s[text.charAt(j - 1)];
|
|
1772
|
+
if (d === 0 ? rd[j] = (rd[j + 1] << 1 | 1) & charMatch : rd[j] = (rd[j + 1] << 1 | 1) & charMatch | ((lastRd[j + 1] | lastRd[j]) << 1 | 1) | lastRd[j + 1], rd[j] & matchmask) {
|
|
1773
|
+
const score = getBitapScore(d, j - 1);
|
|
1774
|
+
if (score <= scoreThreshold)
|
|
1775
|
+
if (scoreThreshold = score, bestLoc = j - 1, bestLoc > loc)
|
|
1776
|
+
start = Math.max(1, 2 * loc - bestLoc);
|
|
1777
|
+
else
|
|
1778
|
+
break;
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
if (getBitapScore(d + 1, loc) > scoreThreshold)
|
|
1782
|
+
break;
|
|
1783
|
+
lastRd = rd;
|
|
1784
|
+
}
|
|
1785
|
+
return bestLoc;
|
|
2127
1786
|
}
|
|
2128
|
-
function
|
|
2129
|
-
|
|
1787
|
+
function getAlphabetFromPattern(pattern) {
|
|
1788
|
+
const s = {};
|
|
1789
|
+
for (let i = 0; i < pattern.length; i++)
|
|
1790
|
+
s[pattern.charAt(i)] = 0;
|
|
1791
|
+
for (let i = 0; i < pattern.length; i++)
|
|
1792
|
+
s[pattern.charAt(i)] |= 1 << pattern.length - i - 1;
|
|
1793
|
+
return s;
|
|
2130
1794
|
}
|
|
2131
|
-
function
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
node._key || slate.Transforms.setNodes(editor, { _key: keyGenerator() }, { at: path });
|
|
2155
|
-
for (const [child, childPath] of slate.Node.children(editor, path))
|
|
2156
|
-
if (!child._key) {
|
|
2157
|
-
slate.Transforms.setNodes(editor, { _key: keyGenerator() }, { at: childPath });
|
|
2158
|
-
return;
|
|
2159
|
-
}
|
|
2160
|
-
}
|
|
2161
|
-
normalizeNode(entry);
|
|
2162
|
-
}, editor;
|
|
1795
|
+
function match(text, pattern, searchLocation) {
|
|
1796
|
+
if (text === null || pattern === null || searchLocation === null)
|
|
1797
|
+
throw new Error("Null input. (match())");
|
|
1798
|
+
const loc = Math.max(0, Math.min(searchLocation, text.length));
|
|
1799
|
+
if (text === pattern)
|
|
1800
|
+
return 0;
|
|
1801
|
+
if (text.length) {
|
|
1802
|
+
if (text.substring(loc, loc + pattern.length) === pattern)
|
|
1803
|
+
return loc;
|
|
1804
|
+
} else return -1;
|
|
1805
|
+
return bitap(text, pattern, loc);
|
|
1806
|
+
}
|
|
1807
|
+
function createPatchObject(start1, start2) {
|
|
1808
|
+
return {
|
|
1809
|
+
diffs: [],
|
|
1810
|
+
start1,
|
|
1811
|
+
start2,
|
|
1812
|
+
utf8Start1: start1,
|
|
1813
|
+
utf8Start2: start2,
|
|
1814
|
+
length1: 0,
|
|
1815
|
+
length2: 0,
|
|
1816
|
+
utf8Length1: 0,
|
|
1817
|
+
utf8Length2: 0
|
|
2163
1818
|
};
|
|
2164
1819
|
}
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
if (array.length === 0)
|
|
2171
|
-
return items;
|
|
2172
|
-
const len = array.length, idx = Math.abs((len + index) % len) % len, normalizedIdx = position === "after" ? idx + 1 : idx, copy = array.slice();
|
|
2173
|
-
return copy.splice(normalizedIdx, 0, ...flatten(items)), copy;
|
|
2174
|
-
}
|
|
2175
|
-
function flatten(...values) {
|
|
2176
|
-
return values.reduce((prev, item) => prev.concat(item), []);
|
|
2177
|
-
}
|
|
2178
|
-
const hasOwn = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
|
|
2179
|
-
function move(arr, from, to) {
|
|
2180
|
-
const nextValue = arr.slice(), val = nextValue[from];
|
|
2181
|
-
return nextValue.splice(from, 1), nextValue.splice(to, 0, val), nextValue;
|
|
1820
|
+
function diffText1(diffs) {
|
|
1821
|
+
const text = [];
|
|
1822
|
+
for (let x = 0; x < diffs.length; x++)
|
|
1823
|
+
diffs[x][0] !== DIFF_INSERT && (text[x] = diffs[x][1]);
|
|
1824
|
+
return text.join("");
|
|
2182
1825
|
}
|
|
2183
|
-
function
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
return
|
|
1826
|
+
function diffText2(diffs) {
|
|
1827
|
+
const text = [];
|
|
1828
|
+
for (let x = 0; x < diffs.length; x++)
|
|
1829
|
+
diffs[x][0] !== DIFF_DELETE && (text[x] = diffs[x][1]);
|
|
1830
|
+
return text.join("");
|
|
2188
1831
|
}
|
|
2189
|
-
function
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
} else if (patch.type === "set") {
|
|
2197
|
-
if (!Array.isArray(patch.value))
|
|
2198
|
-
throw new Error("Cannot set value of an array to a non-array");
|
|
2199
|
-
return patch.value;
|
|
2200
|
-
} else {
|
|
2201
|
-
if (patch.type === "unset")
|
|
2202
|
-
return;
|
|
2203
|
-
if (patch.type === "move") {
|
|
2204
|
-
if (!patch.value || !hasOwn(patch.value, "from") || !hasOwn(patch.value, "to"))
|
|
2205
|
-
throw new Error(
|
|
2206
|
-
`Invalid value of 'move' patch. Expected a value with "from" and "to" indexes, instead got: ${JSON.stringify(
|
|
2207
|
-
patch.value
|
|
2208
|
-
)}`
|
|
2209
|
-
);
|
|
2210
|
-
return move(nextValue, patch.value.from, patch.value.to);
|
|
2211
|
-
}
|
|
2212
|
-
}
|
|
2213
|
-
throw new Error(`Invalid array operation: ${patch.type}`);
|
|
1832
|
+
function countUtf8Bytes(str) {
|
|
1833
|
+
let bytes = 0;
|
|
1834
|
+
for (let i = 0; i < str.length; i++) {
|
|
1835
|
+
const codePoint = str.codePointAt(i);
|
|
1836
|
+
if (typeof codePoint > "u")
|
|
1837
|
+
throw new Error("Failed to get codepoint");
|
|
1838
|
+
bytes += utf8len(codePoint);
|
|
2214
1839
|
}
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
return nextValue.splice(index, 1), nextValue;
|
|
1840
|
+
return bytes;
|
|
1841
|
+
}
|
|
1842
|
+
function adjustIndiciesToUcs2(patches, base) {
|
|
1843
|
+
let options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {}, byteOffset = 0, idx = 0;
|
|
1844
|
+
function advanceTo(target) {
|
|
1845
|
+
for (; byteOffset < target; ) {
|
|
1846
|
+
const codePoint = base.codePointAt(idx);
|
|
1847
|
+
if (typeof codePoint > "u")
|
|
1848
|
+
return idx;
|
|
1849
|
+
byteOffset += utf8len(codePoint), codePoint > 65535 ? idx += 2 : idx += 1;
|
|
2226
1850
|
}
|
|
1851
|
+
if (!options.allowExceedingIndices && byteOffset !== target)
|
|
1852
|
+
throw new Error("Failed to determine byte offset");
|
|
1853
|
+
return idx;
|
|
2227
1854
|
}
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
1855
|
+
const adjusted = [];
|
|
1856
|
+
for (const patch of patches)
|
|
1857
|
+
adjusted.push({
|
|
1858
|
+
diffs: patch.diffs.map((diff2) => cloneDiff(diff2)),
|
|
1859
|
+
start1: advanceTo(patch.start1),
|
|
1860
|
+
start2: advanceTo(patch.start2),
|
|
1861
|
+
utf8Start1: patch.utf8Start1,
|
|
1862
|
+
utf8Start2: patch.utf8Start2,
|
|
1863
|
+
length1: patch.length1,
|
|
1864
|
+
length2: patch.length2,
|
|
1865
|
+
utf8Length1: patch.utf8Length1,
|
|
1866
|
+
utf8Length2: patch.utf8Length2
|
|
1867
|
+
});
|
|
1868
|
+
return adjusted;
|
|
2232
1869
|
}
|
|
2233
|
-
function
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
1870
|
+
function utf8len(codePoint) {
|
|
1871
|
+
return codePoint <= 127 ? 1 : codePoint <= 2047 ? 2 : codePoint <= 65535 ? 3 : 4;
|
|
1872
|
+
}
|
|
1873
|
+
const MAX_BITS = 32, DEFAULT_MARGIN = 4;
|
|
1874
|
+
function levenshtein(diffs) {
|
|
1875
|
+
let leven = 0, insertions = 0, deletions = 0;
|
|
1876
|
+
for (let x = 0; x < diffs.length; x++) {
|
|
1877
|
+
const op = diffs[x][0], data = diffs[x][1];
|
|
1878
|
+
switch (op) {
|
|
1879
|
+
case DIFF_INSERT:
|
|
1880
|
+
insertions += data.length;
|
|
1881
|
+
break;
|
|
1882
|
+
case DIFF_DELETE:
|
|
1883
|
+
deletions += data.length;
|
|
1884
|
+
break;
|
|
1885
|
+
case DIFF_EQUAL:
|
|
1886
|
+
leven += Math.max(insertions, deletions), insertions = 0, deletions = 0;
|
|
1887
|
+
break;
|
|
1888
|
+
default:
|
|
1889
|
+
throw new Error("Unknown diff operation.");
|
|
2245
1890
|
}
|
|
2246
|
-
throw new Error(`Invalid object operation: ${patch.type}`);
|
|
2247
1891
|
}
|
|
2248
|
-
|
|
2249
|
-
if (typeof head != "string")
|
|
2250
|
-
throw new Error(`Expected field name to be a string, instad got: ${head}`);
|
|
2251
|
-
return tail.length === 0 && patch.type === "unset" ? omit__default.default(nextValue, head) : (nextValue[head] = _apply(nextValue[head], {
|
|
2252
|
-
...patch,
|
|
2253
|
-
path: tail
|
|
2254
|
-
}), nextValue);
|
|
1892
|
+
return leven += Math.max(insertions, deletions), leven;
|
|
2255
1893
|
}
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
1894
|
+
function xIndex(diffs, loc) {
|
|
1895
|
+
let chars1 = 0, chars2 = 0, lastChars1 = 0, lastChars2 = 0, x;
|
|
1896
|
+
for (x = 0; x < diffs.length && (diffs[x][0] !== DIFF_INSERT && (chars1 += diffs[x][1].length), diffs[x][0] !== DIFF_DELETE && (chars2 += diffs[x][1].length), !(chars1 > loc)); x++)
|
|
1897
|
+
lastChars1 = chars1, lastChars2 = chars2;
|
|
1898
|
+
return diffs.length !== x && diffs[x][0] === DIFF_DELETE ? lastChars2 : lastChars2 + (loc - lastChars1);
|
|
1899
|
+
}
|
|
1900
|
+
function addPadding(patches) {
|
|
1901
|
+
const paddingLength = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : DEFAULT_MARGIN;
|
|
1902
|
+
let nullPadding = "";
|
|
1903
|
+
for (let x = 1; x <= paddingLength; x++)
|
|
1904
|
+
nullPadding += String.fromCharCode(x);
|
|
1905
|
+
for (const p of patches)
|
|
1906
|
+
p.start1 += paddingLength, p.start2 += paddingLength, p.utf8Start1 += paddingLength, p.utf8Start2 += paddingLength;
|
|
1907
|
+
let patch = patches[0], diffs = patch.diffs;
|
|
1908
|
+
if (diffs.length === 0 || diffs[0][0] !== DIFF_EQUAL)
|
|
1909
|
+
diffs.unshift([DIFF_EQUAL, nullPadding]), patch.start1 -= paddingLength, patch.start2 -= paddingLength, patch.utf8Start1 -= paddingLength, patch.utf8Start2 -= paddingLength, patch.length1 += paddingLength, patch.length2 += paddingLength, patch.utf8Length1 += paddingLength, patch.utf8Length2 += paddingLength;
|
|
1910
|
+
else if (paddingLength > diffs[0][1].length) {
|
|
1911
|
+
const firstDiffLength = diffs[0][1].length, extraLength = paddingLength - firstDiffLength;
|
|
1912
|
+
diffs[0][1] = nullPadding.substring(firstDiffLength) + diffs[0][1], patch.start1 -= extraLength, patch.start2 -= extraLength, patch.utf8Start1 -= extraLength, patch.utf8Start2 -= extraLength, patch.length1 += extraLength, patch.length2 += extraLength, patch.utf8Length1 += extraLength, patch.utf8Length2 += extraLength;
|
|
2273
1913
|
}
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
if (
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
);
|
|
2282
|
-
if (patch.path.length > 0)
|
|
2283
|
-
throw new Error(
|
|
2284
|
-
`Cannot apply deep operations on primitive values. Received patch with type "${patch.type}" and path "${patch.path.map((path) => JSON.stringify(path)).join(".")} that targeted the value "${JSON.stringify(value)}"`
|
|
2285
|
-
);
|
|
2286
|
-
return OPERATIONS$1[patch.type](value, patch.value);
|
|
1914
|
+
if (patch = patches[patches.length - 1], diffs = patch.diffs, diffs.length === 0 || diffs[diffs.length - 1][0] !== DIFF_EQUAL)
|
|
1915
|
+
diffs.push([DIFF_EQUAL, nullPadding]), patch.length1 += paddingLength, patch.length2 += paddingLength, patch.utf8Length1 += paddingLength, patch.utf8Length2 += paddingLength;
|
|
1916
|
+
else if (paddingLength > diffs[diffs.length - 1][1].length) {
|
|
1917
|
+
const extraLength = paddingLength - diffs[diffs.length - 1][1].length;
|
|
1918
|
+
diffs[diffs.length - 1][1] += nullPadding.substring(0, extraLength), patch.length1 += extraLength, patch.length2 += extraLength, patch.utf8Length1 += extraLength, patch.utf8Length2 += extraLength;
|
|
1919
|
+
}
|
|
1920
|
+
return nullPadding;
|
|
2287
1921
|
}
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
1922
|
+
function splitMax(patches) {
|
|
1923
|
+
let margin = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : DEFAULT_MARGIN;
|
|
1924
|
+
const patchSize = MAX_BITS;
|
|
1925
|
+
for (let x = 0; x < patches.length; x++) {
|
|
1926
|
+
if (patches[x].length1 <= patchSize)
|
|
1927
|
+
continue;
|
|
1928
|
+
const bigpatch = patches[x];
|
|
1929
|
+
patches.splice(x--, 1);
|
|
1930
|
+
let start1 = bigpatch.start1, start2 = bigpatch.start2, preContext = "";
|
|
1931
|
+
for (; bigpatch.diffs.length !== 0; ) {
|
|
1932
|
+
const patch = createPatchObject(start1 - preContext.length, start2 - preContext.length);
|
|
1933
|
+
let empty = !0;
|
|
1934
|
+
if (preContext !== "") {
|
|
1935
|
+
const precontextByteCount = countUtf8Bytes(preContext);
|
|
1936
|
+
patch.length1 = preContext.length, patch.utf8Length1 = precontextByteCount, patch.length2 = preContext.length, patch.utf8Length2 = precontextByteCount, patch.diffs.push([DIFF_EQUAL, preContext]);
|
|
1937
|
+
}
|
|
1938
|
+
for (; bigpatch.diffs.length !== 0 && patch.length1 < patchSize - margin; ) {
|
|
1939
|
+
const diffType = bigpatch.diffs[0][0];
|
|
1940
|
+
let diffText = bigpatch.diffs[0][1], diffTextByteCount = countUtf8Bytes(diffText);
|
|
1941
|
+
if (diffType === DIFF_INSERT) {
|
|
1942
|
+
patch.length2 += diffText.length, patch.utf8Length2 += diffTextByteCount, start2 += diffText.length;
|
|
1943
|
+
const diff2 = bigpatch.diffs.shift();
|
|
1944
|
+
diff2 && patch.diffs.push(diff2), empty = !1;
|
|
1945
|
+
} else diffType === DIFF_DELETE && patch.diffs.length === 1 && patch.diffs[0][0] === DIFF_EQUAL && diffText.length > 2 * patchSize ? (patch.length1 += diffText.length, patch.utf8Length1 += diffTextByteCount, start1 += diffText.length, empty = !1, patch.diffs.push([diffType, diffText]), bigpatch.diffs.shift()) : (diffText = diffText.substring(0, patchSize - patch.length1 - margin), diffTextByteCount = countUtf8Bytes(diffText), patch.length1 += diffText.length, patch.utf8Length1 += diffTextByteCount, start1 += diffText.length, diffType === DIFF_EQUAL ? (patch.length2 += diffText.length, patch.utf8Length2 += diffTextByteCount, start2 += diffText.length) : empty = !1, patch.diffs.push([diffType, diffText]), diffText === bigpatch.diffs[0][1] ? bigpatch.diffs.shift() : bigpatch.diffs[0][1] = bigpatch.diffs[0][1].substring(diffText.length));
|
|
1946
|
+
}
|
|
1947
|
+
preContext = diffText2(patch.diffs), preContext = preContext.substring(preContext.length - margin);
|
|
1948
|
+
const postContext = diffText1(bigpatch.diffs).substring(0, margin), postContextByteCount = countUtf8Bytes(postContext);
|
|
1949
|
+
postContext !== "" && (patch.length1 += postContext.length, patch.length2 += postContext.length, patch.utf8Length1 += postContextByteCount, patch.utf8Length2 += postContextByteCount, patch.diffs.length !== 0 && patch.diffs[patch.diffs.length - 1][0] === DIFF_EQUAL ? patch.diffs[patch.diffs.length - 1][1] += postContext : patch.diffs.push([DIFF_EQUAL, postContext])), empty || patches.splice(++x, 0, patch);
|
|
1950
|
+
}
|
|
2305
1951
|
}
|
|
2306
|
-
}, SUPPORTED_PATCH_TYPES = Object.keys(OPERATIONS);
|
|
2307
|
-
function apply(value, patch) {
|
|
2308
|
-
if (!SUPPORTED_PATCH_TYPES.includes(patch.type))
|
|
2309
|
-
throw new Error(
|
|
2310
|
-
`Received patch of unsupported type: "${JSON.stringify(
|
|
2311
|
-
patch.type
|
|
2312
|
-
)}" for string. This is most likely a bug.`
|
|
2313
|
-
);
|
|
2314
|
-
if (patch.path.length > 0)
|
|
2315
|
-
throw new Error(
|
|
2316
|
-
`Cannot apply deep operations on string values. Received patch with type "${patch.type}" and path "${patch.path.join(".")} that targeted the value "${JSON.stringify(value)}"`
|
|
2317
|
-
);
|
|
2318
|
-
const func = OPERATIONS[patch.type];
|
|
2319
|
-
if (func)
|
|
2320
|
-
return func(value, patch.value);
|
|
2321
|
-
throw new Error("Unknown patch type");
|
|
2322
1952
|
}
|
|
2323
|
-
function
|
|
2324
|
-
|
|
1953
|
+
function apply(patches, originalText) {
|
|
1954
|
+
let opts = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
|
|
1955
|
+
if (typeof patches == "string")
|
|
1956
|
+
throw new Error("Patches must be an array - pass the patch to `parsePatch()` first");
|
|
1957
|
+
let text = originalText;
|
|
1958
|
+
if (patches.length === 0)
|
|
1959
|
+
return [text, []];
|
|
1960
|
+
const parsed = adjustIndiciesToUcs2(patches, text, {
|
|
1961
|
+
allowExceedingIndices: opts.allowExceedingIndices
|
|
1962
|
+
}), margin = opts.margin || DEFAULT_MARGIN, deleteThreshold = opts.deleteThreshold || 0.4, nullPadding = addPadding(parsed, margin);
|
|
1963
|
+
text = nullPadding + text + nullPadding, splitMax(parsed, margin);
|
|
1964
|
+
let delta = 0;
|
|
1965
|
+
const results = [];
|
|
1966
|
+
for (let x = 0; x < parsed.length; x++) {
|
|
1967
|
+
const expectedLoc = parsed[x].start2 + delta, text1 = diffText1(parsed[x].diffs);
|
|
1968
|
+
let startLoc, endLoc = -1;
|
|
1969
|
+
if (text1.length > MAX_BITS ? (startLoc = match(text, text1.substring(0, MAX_BITS), expectedLoc), startLoc !== -1 && (endLoc = match(text, text1.substring(text1.length - MAX_BITS), expectedLoc + text1.length - MAX_BITS), (endLoc === -1 || startLoc >= endLoc) && (startLoc = -1))) : startLoc = match(text, text1, expectedLoc), startLoc === -1)
|
|
1970
|
+
results[x] = !1, delta -= parsed[x].length2 - parsed[x].length1;
|
|
1971
|
+
else {
|
|
1972
|
+
results[x] = !0, delta = startLoc - expectedLoc;
|
|
1973
|
+
let text2;
|
|
1974
|
+
if (endLoc === -1 ? text2 = text.substring(startLoc, startLoc + text1.length) : text2 = text.substring(startLoc, endLoc + MAX_BITS), text1 === text2)
|
|
1975
|
+
text = text.substring(0, startLoc) + diffText2(parsed[x].diffs) + text.substring(startLoc + text1.length);
|
|
1976
|
+
else {
|
|
1977
|
+
let diffs = diff(text1, text2, {
|
|
1978
|
+
checkLines: !1
|
|
1979
|
+
});
|
|
1980
|
+
if (text1.length > MAX_BITS && levenshtein(diffs) / text1.length > deleteThreshold)
|
|
1981
|
+
results[x] = !1;
|
|
1982
|
+
else {
|
|
1983
|
+
diffs = cleanupSemanticLossless(diffs);
|
|
1984
|
+
let index1 = 0, index2 = 0;
|
|
1985
|
+
for (let y = 0; y < parsed[x].diffs.length; y++) {
|
|
1986
|
+
const mod = parsed[x].diffs[y];
|
|
1987
|
+
mod[0] !== DIFF_EQUAL && (index2 = xIndex(diffs, index1)), mod[0] === DIFF_INSERT ? text = text.substring(0, startLoc + index2) + mod[1] + text.substring(startLoc + index2) : mod[0] === DIFF_DELETE && (text = text.substring(0, startLoc + index2) + text.substring(startLoc + xIndex(diffs, index1 + mod[1].length))), mod[0] !== DIFF_DELETE && (index1 += mod[1].length);
|
|
1988
|
+
}
|
|
1989
|
+
}
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
1993
|
+
return text = text.substring(nullPadding.length, text.length - nullPadding.length), [text, results];
|
|
2325
1994
|
}
|
|
2326
|
-
|
|
2327
|
-
|
|
1995
|
+
const patchHeader = /^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$/;
|
|
1996
|
+
function parse(textline) {
|
|
1997
|
+
if (!textline)
|
|
1998
|
+
return [];
|
|
1999
|
+
const patches = [], lines = textline.split(`
|
|
2000
|
+
`);
|
|
2001
|
+
let textPointer = 0;
|
|
2002
|
+
for (; textPointer < lines.length; ) {
|
|
2003
|
+
const m = lines[textPointer].match(patchHeader);
|
|
2004
|
+
if (!m)
|
|
2005
|
+
throw new Error("Invalid patch string: ".concat(lines[textPointer]));
|
|
2006
|
+
const patch = createPatchObject(toInt(m[1]), toInt(m[3]));
|
|
2007
|
+
for (patches.push(patch), m[2] === "" ? (patch.start1--, patch.utf8Start1--, patch.length1 = 1, patch.utf8Length1 = 1) : m[2] === "0" ? (patch.length1 = 0, patch.utf8Length1 = 0) : (patch.start1--, patch.utf8Start1--, patch.utf8Length1 = toInt(m[2]), patch.length1 = patch.utf8Length1), m[4] === "" ? (patch.start2--, patch.utf8Start2--, patch.length2 = 1, patch.utf8Length2 = 1) : m[4] === "0" ? (patch.length2 = 0, patch.utf8Length2 = 0) : (patch.start2--, patch.utf8Start2--, patch.utf8Length2 = toInt(m[4]), patch.length2 = patch.utf8Length2), textPointer++; textPointer < lines.length; ) {
|
|
2008
|
+
const currentLine = lines[textPointer], sign = currentLine.charAt(0);
|
|
2009
|
+
if (sign === "@")
|
|
2010
|
+
break;
|
|
2011
|
+
if (sign === "") {
|
|
2012
|
+
textPointer++;
|
|
2013
|
+
continue;
|
|
2014
|
+
}
|
|
2015
|
+
let line;
|
|
2016
|
+
try {
|
|
2017
|
+
line = decodeURI(currentLine.slice(1));
|
|
2018
|
+
} catch {
|
|
2019
|
+
throw new Error("Illegal escape in parse: ".concat(currentLine));
|
|
2020
|
+
}
|
|
2021
|
+
const utf8Diff = countUtf8Bytes(line) - line.length;
|
|
2022
|
+
if (sign === "-")
|
|
2023
|
+
patch.diffs.push([DIFF_DELETE, line]), patch.length1 -= utf8Diff;
|
|
2024
|
+
else if (sign === "+")
|
|
2025
|
+
patch.diffs.push([DIFF_INSERT, line]), patch.length2 -= utf8Diff;
|
|
2026
|
+
else if (sign === " ")
|
|
2027
|
+
patch.diffs.push([DIFF_EQUAL, line]), patch.length1 -= utf8Diff, patch.length2 -= utf8Diff;
|
|
2028
|
+
else
|
|
2029
|
+
throw new Error('Invalid patch mode "'.concat(sign, '" in: ').concat(line));
|
|
2030
|
+
textPointer++;
|
|
2031
|
+
}
|
|
2032
|
+
}
|
|
2033
|
+
return patches;
|
|
2328
2034
|
}
|
|
2329
|
-
function
|
|
2330
|
-
return
|
|
2035
|
+
function toInt(num) {
|
|
2036
|
+
return parseInt(num, 10);
|
|
2331
2037
|
}
|
|
2332
2038
|
const debug$h = debugWithName("applyPatches"), debugVerbose$3 = debug$h.enabled && !0;
|
|
2333
2039
|
function createApplyPatch(schemaTypes) {
|
|
@@ -2368,7 +2074,7 @@ function diffMatchPatch(editor, patch) {
|
|
|
2368
2074
|
return debug$h("Child not found"), !1;
|
|
2369
2075
|
if (!(block && editor.isTextBlock(block) && patch.path.length === 4 && patch.path[1] === "children" && patch.path[3] === "text") || !slate.Text.isText(child))
|
|
2370
2076
|
return !1;
|
|
2371
|
-
const patches = parse(patch.value), [newValue] = apply
|
|
2077
|
+
const patches = parse(patch.value), [newValue] = apply(patches, child.text, { allowExceedingIndices: !0 }), diff$1 = cleanupEfficiency(diff(child.text, newValue), 5);
|
|
2372
2078
|
debugState(editor, "before");
|
|
2373
2079
|
let offset = 0;
|
|
2374
2080
|
for (const [op, text] of diff$1)
|
|
@@ -2457,7 +2163,7 @@ function setPatch(editor, patch) {
|
|
|
2457
2163
|
});
|
|
2458
2164
|
});
|
|
2459
2165
|
} else if (block && "value" in block) {
|
|
2460
|
-
const newVal = applyAll([block.value], [patch])[0];
|
|
2166
|
+
const newVal = apply$1.applyAll([block.value], [patch])[0];
|
|
2461
2167
|
return slate.Transforms.setNodes(editor, { ...block, value: newVal }, { at: blockPath }), !0;
|
|
2462
2168
|
}
|
|
2463
2169
|
return debugState(editor, "after"), !0;
|
|
@@ -2739,7 +2445,7 @@ function createWithPatches({
|
|
|
2739
2445
|
schemaTypes
|
|
2740
2446
|
}) {
|
|
2741
2447
|
let previousChildren;
|
|
2742
|
-
const
|
|
2448
|
+
const applyPatch = createApplyPatch(schemaTypes);
|
|
2743
2449
|
return function(editor) {
|
|
2744
2450
|
IS_PROCESSING_REMOTE_CHANGES.set(editor, !1), PATCHING.set(editor, !0), previousChildren = [...editor.children];
|
|
2745
2451
|
const { apply: apply2 } = editor;
|
|
@@ -2756,7 +2462,7 @@ function createWithPatches({
|
|
|
2756
2462
|
withoutSaving(editor, () => {
|
|
2757
2463
|
withPreserveKeys(editor, () => {
|
|
2758
2464
|
patches.forEach((patch) => {
|
|
2759
|
-
debug$f.enabled && debug$f(`Handling remote patch ${JSON.stringify(patch)}`), changed =
|
|
2465
|
+
debug$f.enabled && debug$f(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
|
|
2760
2466
|
});
|
|
2761
2467
|
});
|
|
2762
2468
|
});
|
|
@@ -2783,7 +2489,7 @@ function createWithPatches({
|
|
|
2783
2489
|
const editorIsEmpty = isEqualToEmptyEditor(editor.children, schemaTypes);
|
|
2784
2490
|
if (!isPatching(editor))
|
|
2785
2491
|
return editor;
|
|
2786
|
-
switch (editorWasEmpty && !editorIsEmpty && operation.type !== "set_selection" && patches.push(insert
|
|
2492
|
+
switch (editorWasEmpty && !editorIsEmpty && operation.type !== "set_selection" && patches.push(patchEvent.insert(previousChildren, "before", [0])), operation.type) {
|
|
2787
2493
|
case "insert_text":
|
|
2788
2494
|
patches = [
|
|
2789
2495
|
...patches,
|
|
@@ -2833,14 +2539,14 @@ function createWithPatches({
|
|
|
2833
2539
|
];
|
|
2834
2540
|
break;
|
|
2835
2541
|
}
|
|
2836
|
-
return !editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(operation.type) && (patches = [...patches, unset([])], change$.next({
|
|
2542
|
+
return !editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(operation.type) && (patches = [...patches, patchEvent.unset([])], change$.next({
|
|
2837
2543
|
type: "unset",
|
|
2838
2544
|
previousValue: fromSlateValue(
|
|
2839
2545
|
previousChildren,
|
|
2840
2546
|
schemaTypes.block.name,
|
|
2841
2547
|
KEY_TO_VALUE_ELEMENT.get(editor)
|
|
2842
2548
|
)
|
|
2843
|
-
})), editorWasEmpty && patches.length > 0 && (patches = [setIfMissing([], []), ...patches]), patches.length > 0 && patches.forEach((patch) => {
|
|
2549
|
+
})), editorWasEmpty && patches.length > 0 && (patches = [patchEvent.setIfMissing([], []), ...patches]), patches.length > 0 && patches.forEach((patch) => {
|
|
2844
2550
|
change$.next({
|
|
2845
2551
|
type: "patch",
|
|
2846
2552
|
patch: { ...patch, origin: "local" }
|
|
@@ -3252,7 +2958,7 @@ function createWithSchemaTypes({
|
|
|
3252
2958
|
keyGenerator
|
|
3253
2959
|
}) {
|
|
3254
2960
|
return function(editor) {
|
|
3255
|
-
editor.isTextBlock = (value) => types.isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => types.isPortableTextSpan(value) && value._type == schemaTypes.span.name, editor.isListBlock = (value) => types.isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
|
|
2961
|
+
editor.isTextBlock = (value) => types$1.isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => types$1.isPortableTextSpan(value) && value._type == schemaTypes.span.name, editor.isListBlock = (value) => types$1.isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
|
|
3256
2962
|
const { normalizeNode } = editor;
|
|
3257
2963
|
return editor.normalizeNode = (entry) => {
|
|
3258
2964
|
const [node, path] = entry;
|
|
@@ -3321,7 +3027,7 @@ const debug$7 = debugWithName("plugin:withHotKeys"), DEFAULT_HOTKEYS = {
|
|
|
3321
3027
|
},
|
|
3322
3028
|
custom: {}
|
|
3323
3029
|
};
|
|
3324
|
-
function createWithHotkeys(
|
|
3030
|
+
function createWithHotkeys(types2, portableTextEditor, hotkeysFromOptions) {
|
|
3325
3031
|
const reservedHotkeys = ["enter", "tab", "shift", "delete", "end"], activeHotkeys = hotkeysFromOptions || DEFAULT_HOTKEYS;
|
|
3326
3032
|
return function(editor) {
|
|
3327
3033
|
return editor.pteWithHotKeys = (event) => {
|
|
@@ -3372,7 +3078,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3372
3078
|
}
|
|
3373
3079
|
}
|
|
3374
3080
|
if (isBackspace && editor.selection && editor.selection.focus.path[0] === 0 && slate.Range.isCollapsed(editor.selection)) {
|
|
3375
|
-
const focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1)), nextPath = slate.Path.next(editor.selection.focus.path.slice(0, 1)), nextBlock = slate.Node.has(editor, nextPath), isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && ((_b = (_a = focusBlock.children) == null ? void 0 : _a[0]) == null ? void 0 : _b.text) === "";
|
|
3081
|
+
const focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1)), nextPath = slate.Path.next(editor.selection.focus.path.slice(0, 1)), nextBlock = slate.Node.has(editor, nextPath), isTextBlock = types$1.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && ((_b = (_a = focusBlock.children) == null ? void 0 : _a[0]) == null ? void 0 : _b.text) === "";
|
|
3376
3082
|
if (nextBlock && isTextBlock && isEmptyFocusBlock) {
|
|
3377
3083
|
event.preventDefault(), event.stopPropagation(), slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), editor.onChange();
|
|
3378
3084
|
return;
|
|
@@ -3382,7 +3088,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3382
3088
|
const prevPath = slate.Path.previous(editor.selection.focus.path.slice(0, 1)), prevBlock = slate.Node.descendant(editor, prevPath), focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
3383
3089
|
if (prevBlock && focusBlock && slate.Editor.isVoid(editor, prevBlock) && editor.selection.focus.offset === 0) {
|
|
3384
3090
|
debug$7("Preventing deleting void block above"), event.preventDefault(), event.stopPropagation();
|
|
3385
|
-
const isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && ((_d = (_c = focusBlock.children) == null ? void 0 : _c[0]) == null ? void 0 : _d.text) === "";
|
|
3091
|
+
const isTextBlock = types$1.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && ((_d = (_c = focusBlock.children) == null ? void 0 : _c[0]) == null ? void 0 : _d.text) === "";
|
|
3386
3092
|
if (!isTextBlock || isEmptyFocusBlock) {
|
|
3387
3093
|
slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), slate.Transforms.select(editor, prevPath), editor.onChange();
|
|
3388
3094
|
return;
|
|
@@ -3405,10 +3111,10 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3405
3111
|
}
|
|
3406
3112
|
}
|
|
3407
3113
|
if ((isTab || isShiftTab) && editor.selection) {
|
|
3408
|
-
const [focusChild] = slate.Editor.node(editor, editor.selection.focus, { depth: 2 }), [focusBlock] = types.isPortableTextSpan(focusChild) ? slate.Editor.node(editor, editor.selection.focus, { depth: 1 }) : [], hasAnnotationFocus = focusChild && types.isPortableTextTextBlock(focusBlock) && types.isPortableTextSpan(focusChild) && (focusChild.marks || []).filter(
|
|
3114
|
+
const [focusChild] = slate.Editor.node(editor, editor.selection.focus, { depth: 2 }), [focusBlock] = types$1.isPortableTextSpan(focusChild) ? slate.Editor.node(editor, editor.selection.focus, { depth: 1 }) : [], hasAnnotationFocus = focusChild && types$1.isPortableTextTextBlock(focusBlock) && types$1.isPortableTextSpan(focusChild) && (focusChild.marks || []).filter(
|
|
3409
3115
|
(m) => (focusBlock.markDefs || []).map((def) => def._key).includes(m)
|
|
3410
3116
|
).length > 0, [start] = slate.Range.edges(editor.selection), atStartOfNode = slate.Editor.isStart(editor, start, start.path);
|
|
3411
|
-
focusChild && types.isPortableTextSpan(focusChild) && (!hasAnnotationFocus || atStartOfNode) && editor.pteIncrementBlockLevels(isShiftTab) && event.preventDefault();
|
|
3117
|
+
focusChild && types$1.isPortableTextSpan(focusChild) && (!hasAnnotationFocus || atStartOfNode) && editor.pteIncrementBlockLevels(isShiftTab) && event.preventDefault();
|
|
3412
3118
|
}
|
|
3413
3119
|
if (isEnter && !isShiftEnter && editor.selection) {
|
|
3414
3120
|
const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
|
|
@@ -3416,7 +3122,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3416
3122
|
editor.pteEndList() && event.preventDefault();
|
|
3417
3123
|
return;
|
|
3418
3124
|
}
|
|
3419
|
-
if (editor.isTextBlock(focusBlock) && focusBlock.style && focusBlock.style !==
|
|
3125
|
+
if (editor.isTextBlock(focusBlock) && focusBlock.style && focusBlock.style !== types2.styles[0].value) {
|
|
3420
3126
|
const [, end] = slate.Range.edges(editor.selection);
|
|
3421
3127
|
if (slate.Editor.isEnd(editor, end, end.path)) {
|
|
3422
3128
|
slate.Editor.insertNode(editor, editor.pteCreateEmptyBlock()), event.preventDefault(), editor.onChange();
|
|
@@ -3442,13 +3148,13 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3442
3148
|
}, editor;
|
|
3443
3149
|
};
|
|
3444
3150
|
}
|
|
3445
|
-
function validateValue(value,
|
|
3151
|
+
function validateValue(value, types2, keyGenerator) {
|
|
3446
3152
|
let resolution = null, valid = !0;
|
|
3447
|
-
const validChildTypes = [
|
|
3153
|
+
const validChildTypes = [types2.span.name, ...types2.inlineObjects.map((t) => t.name)], validBlockTypes = [types2.block.name, ...types2.blockObjects.map((t) => t.name)];
|
|
3448
3154
|
return value === void 0 ? { valid: !0, resolution: null, value } : !Array.isArray(value) || value.length === 0 ? {
|
|
3449
3155
|
valid: !1,
|
|
3450
3156
|
resolution: {
|
|
3451
|
-
patches: [unset([])],
|
|
3157
|
+
patches: [patchEvent.unset([])],
|
|
3452
3158
|
description: "Editor value must be an array of Portable Text blocks, or undefined.",
|
|
3453
3159
|
action: "Unset the value",
|
|
3454
3160
|
item: value,
|
|
@@ -3461,7 +3167,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3461
3167
|
} : (value.some((blk, index) => {
|
|
3462
3168
|
if (!isPlainObject__default.default(blk))
|
|
3463
3169
|
return resolution = {
|
|
3464
|
-
patches: [unset([index])],
|
|
3170
|
+
patches: [patchEvent.unset([index])],
|
|
3465
3171
|
description: `Block must be an object, got ${String(blk)}`,
|
|
3466
3172
|
action: "Unset invalid item",
|
|
3467
3173
|
item: blk,
|
|
@@ -3473,7 +3179,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3473
3179
|
}, !0;
|
|
3474
3180
|
if (!blk._key || typeof blk._key != "string")
|
|
3475
3181
|
return resolution = {
|
|
3476
|
-
patches: [set({ ...blk, _key: keyGenerator() }, [index])],
|
|
3182
|
+
patches: [patchEvent.set({ ...blk, _key: keyGenerator() }, [index])],
|
|
3477
3183
|
description: `Block at index ${index} is missing required _key.`,
|
|
3478
3184
|
action: "Set the block with a random _key value",
|
|
3479
3185
|
item: blk,
|
|
@@ -3485,9 +3191,9 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3485
3191
|
}, !0;
|
|
3486
3192
|
if (!blk._type || !validBlockTypes.includes(blk._type)) {
|
|
3487
3193
|
if (blk._type === "block") {
|
|
3488
|
-
const currentBlockTypeName =
|
|
3194
|
+
const currentBlockTypeName = types2.block.name;
|
|
3489
3195
|
return resolution = {
|
|
3490
|
-
patches: [set({ ...blk, _type: currentBlockTypeName }, [{ _key: blk._key }])],
|
|
3196
|
+
patches: [patchEvent.set({ ...blk, _type: currentBlockTypeName }, [{ _key: blk._key }])],
|
|
3491
3197
|
description: `Block with _key '${blk._key}' has invalid type name '${blk._type}'. According to the schema, the block type name is '${currentBlockTypeName}'`,
|
|
3492
3198
|
action: `Use type '${currentBlockTypeName}'`,
|
|
3493
3199
|
item: blk,
|
|
@@ -3498,18 +3204,18 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3498
3204
|
}
|
|
3499
3205
|
}, !0;
|
|
3500
3206
|
}
|
|
3501
|
-
return !blk._type && types.isPortableTextTextBlock({ ...blk, _type:
|
|
3502
|
-
patches: [set({ ...blk, _type:
|
|
3503
|
-
description: `Block with _key '${blk._key}' is missing a type name. According to the schema, the block type name is '${
|
|
3504
|
-
action: `Use type '${
|
|
3207
|
+
return !blk._type && types$1.isPortableTextTextBlock({ ...blk, _type: types2.block.name }) ? (resolution = {
|
|
3208
|
+
patches: [patchEvent.set({ ...blk, _type: types2.block.name }, [{ _key: blk._key }])],
|
|
3209
|
+
description: `Block with _key '${blk._key}' is missing a type name. According to the schema, the block type name is '${types2.block.name}'`,
|
|
3210
|
+
action: `Use type '${types2.block.name}'`,
|
|
3505
3211
|
item: blk,
|
|
3506
3212
|
i18n: {
|
|
3507
3213
|
description: "inputs.portable-text.invalid-value.missing-block-type.description",
|
|
3508
3214
|
action: "inputs.portable-text.invalid-value.missing-block-type.action",
|
|
3509
|
-
values: { key: blk._key, expectedTypeName:
|
|
3215
|
+
values: { key: blk._key, expectedTypeName: types2.block.name }
|
|
3510
3216
|
}
|
|
3511
3217
|
}, !0) : blk._type ? (resolution = {
|
|
3512
|
-
patches: [unset([{ _key: blk._key }])],
|
|
3218
|
+
patches: [patchEvent.unset([{ _key: blk._key }])],
|
|
3513
3219
|
description: `Block with _key '${blk._key}' has invalid _type '${blk._type}'`,
|
|
3514
3220
|
action: "Remove the block",
|
|
3515
3221
|
item: blk,
|
|
@@ -3519,7 +3225,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3519
3225
|
values: { key: blk._key, typeName: blk._type }
|
|
3520
3226
|
}
|
|
3521
3227
|
}, !0) : (resolution = {
|
|
3522
|
-
patches: [unset([{ _key: blk._key }])],
|
|
3228
|
+
patches: [patchEvent.unset([{ _key: blk._key }])],
|
|
3523
3229
|
description: `Block with _key '${blk._key}' is missing an _type property`,
|
|
3524
3230
|
action: "Remove the block",
|
|
3525
3231
|
item: blk,
|
|
@@ -3530,11 +3236,11 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3530
3236
|
}
|
|
3531
3237
|
}, !0);
|
|
3532
3238
|
}
|
|
3533
|
-
if (blk._type ===
|
|
3239
|
+
if (blk._type === types2.block.name) {
|
|
3534
3240
|
const textBlock = blk;
|
|
3535
3241
|
if (textBlock.children && !Array.isArray(textBlock.children))
|
|
3536
3242
|
return resolution = {
|
|
3537
|
-
patches: [set({ children: [] }, [{ _key: textBlock._key }])],
|
|
3243
|
+
patches: [patchEvent.set({ children: [] }, [{ _key: textBlock._key }])],
|
|
3538
3244
|
description: `Text block with _key '${textBlock._key}' has a invalid required property 'children'.`,
|
|
3539
3245
|
action: "Reset the children property",
|
|
3540
3246
|
item: textBlock,
|
|
@@ -3546,7 +3252,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3546
3252
|
}, !0;
|
|
3547
3253
|
if (textBlock.children === void 0 || Array.isArray(textBlock.children) && textBlock.children.length === 0) {
|
|
3548
3254
|
const newSpan = {
|
|
3549
|
-
_type:
|
|
3255
|
+
_type: types2.span.name,
|
|
3550
3256
|
_key: keyGenerator(),
|
|
3551
3257
|
text: "",
|
|
3552
3258
|
marks: []
|
|
@@ -3554,8 +3260,8 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3554
3260
|
return resolution = {
|
|
3555
3261
|
autoResolve: !0,
|
|
3556
3262
|
patches: [
|
|
3557
|
-
setIfMissing([], [{ _key: blk._key }, "children"]),
|
|
3558
|
-
insert
|
|
3263
|
+
patchEvent.setIfMissing([], [{ _key: blk._key }, "children"]),
|
|
3264
|
+
patchEvent.insert([newSpan], "after", [{ _key: blk._key }, "children", 0])
|
|
3559
3265
|
],
|
|
3560
3266
|
description: `Children for text block with _key '${blk._key}' is empty.`,
|
|
3561
3267
|
action: "Insert an empty text",
|
|
@@ -3569,7 +3275,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3569
3275
|
}
|
|
3570
3276
|
if (blk.markDefs && !Array.isArray(blk.markDefs))
|
|
3571
3277
|
return resolution = {
|
|
3572
|
-
patches: [set({ ...textBlock, markDefs: EMPTY_MARKDEFS }, [{ _key: textBlock._key }])],
|
|
3278
|
+
patches: [patchEvent.set({ ...textBlock, markDefs: EMPTY_MARKDEFS }, [{ _key: textBlock._key }])],
|
|
3573
3279
|
description: "Block has invalid required property 'markDefs'.",
|
|
3574
3280
|
action: "Add empty markDefs array",
|
|
3575
3281
|
item: textBlock,
|
|
@@ -3581,7 +3287,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3581
3287
|
}, !0;
|
|
3582
3288
|
const allUsedMarks = uniq__default.default(
|
|
3583
3289
|
flatten__default.default(
|
|
3584
|
-
textBlock.children.filter((cld) => cld._type ===
|
|
3290
|
+
textBlock.children.filter((cld) => cld._type === types2.span.name).map((cld) => cld.marks || [])
|
|
3585
3291
|
)
|
|
3586
3292
|
);
|
|
3587
3293
|
if (Array.isArray(blk.markDefs) && blk.markDefs.length > 0) {
|
|
@@ -3592,7 +3298,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3592
3298
|
return resolution = {
|
|
3593
3299
|
autoResolve: !0,
|
|
3594
3300
|
patches: unusedMarkDefs.map(
|
|
3595
|
-
(markDefKey) => unset([{ _key: blk._key }, "markDefs", { _key: markDefKey }])
|
|
3301
|
+
(markDefKey) => patchEvent.unset([{ _key: blk._key }, "markDefs", { _key: markDefKey }])
|
|
3596
3302
|
),
|
|
3597
3303
|
description: `Block contains orphaned data (unused mark definitions): ${unusedMarkDefs.join(
|
|
3598
3304
|
", "
|
|
@@ -3607,19 +3313,19 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3607
3313
|
}, !0;
|
|
3608
3314
|
}
|
|
3609
3315
|
const orphanedMarks = allUsedMarks.filter(
|
|
3610
|
-
(mark) => !
|
|
3316
|
+
(mark) => !types2.decorators.map((dec) => dec.value).includes(mark)
|
|
3611
3317
|
).filter(
|
|
3612
3318
|
(mark) => textBlock.markDefs === void 0 || !textBlock.markDefs.find((def) => def._key === mark)
|
|
3613
3319
|
);
|
|
3614
3320
|
if (orphanedMarks.length > 0) {
|
|
3615
3321
|
const spanChildren = textBlock.children.filter(
|
|
3616
|
-
(cld) => cld._type ===
|
|
3322
|
+
(cld) => cld._type === types2.span.name && Array.isArray(cld.marks) && cld.marks.some((mark) => orphanedMarks.includes(mark))
|
|
3617
3323
|
);
|
|
3618
3324
|
if (spanChildren) {
|
|
3619
3325
|
const orphaned = orphanedMarks.join(", ");
|
|
3620
3326
|
return resolution = {
|
|
3621
3327
|
autoResolve: !0,
|
|
3622
|
-
patches: spanChildren.map((child) => set(
|
|
3328
|
+
patches: spanChildren.map((child) => patchEvent.set(
|
|
3623
3329
|
(child.marks || []).filter((cMrk) => !orphanedMarks.includes(cMrk)),
|
|
3624
3330
|
[{ _key: blk._key }, "children", { _key: child._key }, "marks"]
|
|
3625
3331
|
)),
|
|
@@ -3637,7 +3343,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3637
3343
|
textBlock.children.some((child, cIndex) => {
|
|
3638
3344
|
if (!isPlainObject__default.default(child))
|
|
3639
3345
|
return resolution = {
|
|
3640
|
-
patches: [unset([{ _key: blk._key }, "children", cIndex])],
|
|
3346
|
+
patches: [patchEvent.unset([{ _key: blk._key }, "children", cIndex])],
|
|
3641
3347
|
description: `Child at index '${cIndex}' in block with key '${blk._key}' is not an object.`,
|
|
3642
3348
|
action: "Remove the item",
|
|
3643
3349
|
item: blk,
|
|
@@ -3651,7 +3357,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3651
3357
|
const newChild = { ...child, _key: keyGenerator() };
|
|
3652
3358
|
return resolution = {
|
|
3653
3359
|
autoResolve: !0,
|
|
3654
|
-
patches: [set(newChild, [{ _key: blk._key }, "children", cIndex])],
|
|
3360
|
+
patches: [patchEvent.set(newChild, [{ _key: blk._key }, "children", cIndex])],
|
|
3655
3361
|
description: `Child at index ${cIndex} is missing required _key in block with _key ${blk._key}.`,
|
|
3656
3362
|
action: "Set a new random _key on the object",
|
|
3657
3363
|
item: blk,
|
|
@@ -3662,9 +3368,9 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3662
3368
|
}
|
|
3663
3369
|
}, !0;
|
|
3664
3370
|
}
|
|
3665
|
-
return child._type ? validChildTypes.includes(child._type) ? child._type ===
|
|
3371
|
+
return child._type ? validChildTypes.includes(child._type) ? child._type === types2.span.name && typeof child.text != "string" ? (resolution = {
|
|
3666
3372
|
patches: [
|
|
3667
|
-
set({ ...child, text: "" }, [{ _key: blk._key }, "children", { _key: child._key }])
|
|
3373
|
+
patchEvent.set({ ...child, text: "" }, [{ _key: blk._key }, "children", { _key: child._key }])
|
|
3668
3374
|
],
|
|
3669
3375
|
description: `Child with _key '${child._key}' in block with key '${blk._key}' has missing or invalid text property!`,
|
|
3670
3376
|
action: "Write an empty text property to the object",
|
|
@@ -3675,7 +3381,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3675
3381
|
values: { key: blk._key, childKey: child._key }
|
|
3676
3382
|
}
|
|
3677
3383
|
}, !0) : !1 : (resolution = {
|
|
3678
|
-
patches: [unset([{ _key: blk._key }, "children", { _key: child._key }])],
|
|
3384
|
+
patches: [patchEvent.unset([{ _key: blk._key }, "children", { _key: child._key }])],
|
|
3679
3385
|
description: `Child with _key '${child._key}' in block with key '${blk._key}' has invalid '_type' property (${child._type}).`,
|
|
3680
3386
|
action: "Remove the object",
|
|
3681
3387
|
item: blk,
|
|
@@ -3685,7 +3391,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3685
3391
|
values: { key: blk._key, childKey: child._key, childType: child._type }
|
|
3686
3392
|
}
|
|
3687
3393
|
}, !0) : (resolution = {
|
|
3688
|
-
patches: [unset([{ _key: blk._key }, "children", { _key: child._key }])],
|
|
3394
|
+
patches: [patchEvent.unset([{ _key: blk._key }, "children", { _key: child._key }])],
|
|
3689
3395
|
description: `Child with _key '${child._key}' in block with key '${blk._key}' is missing '_type' property.`,
|
|
3690
3396
|
action: "Remove the object",
|
|
3691
3397
|
item: blk,
|
|
@@ -3752,7 +3458,8 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3752
3458
|
editor,
|
|
3753
3459
|
toSlateValue(parsed, { schemaTypes }),
|
|
3754
3460
|
keyGenerator,
|
|
3755
|
-
spanTypeName
|
|
3461
|
+
spanTypeName,
|
|
3462
|
+
schemaTypes
|
|
3756
3463
|
), validation = validateValue(parsed, schemaTypes, keyGenerator);
|
|
3757
3464
|
if (!validation.valid && !((_a = validation.resolution) != null && _a.autoResolve)) {
|
|
3758
3465
|
const errorDescription = `${(_b = validation.resolution) == null ? void 0 : _b.description}`;
|
|
@@ -3833,21 +3540,32 @@ const entityMap = {
|
|
|
3833
3540
|
function escapeHtml(str) {
|
|
3834
3541
|
return String(str).replace(/[&<>"'`=/]/g, (s) => entityMap[s]);
|
|
3835
3542
|
}
|
|
3836
|
-
function _regenerateKeys(editor, fragment, keyGenerator, spanTypeName) {
|
|
3543
|
+
function _regenerateKeys(editor, fragment, keyGenerator, spanTypeName, editorTypes) {
|
|
3837
3544
|
return fragment.map((node) => {
|
|
3838
3545
|
const newNode = { ...node };
|
|
3839
|
-
editor.isTextBlock(newNode)
|
|
3840
|
-
const
|
|
3841
|
-
|
|
3842
|
-
|
|
3843
|
-
|
|
3844
|
-
|
|
3845
|
-
|
|
3846
|
-
|
|
3847
|
-
|
|
3848
|
-
|
|
3849
|
-
|
|
3850
|
-
|
|
3546
|
+
if (editor.isTextBlock(newNode)) {
|
|
3547
|
+
const annotations = editorTypes.annotations.map((t) => t.name);
|
|
3548
|
+
if (annotations.length === 0) {
|
|
3549
|
+
const { markDefs, ...NewNodeNoDefs } = newNode;
|
|
3550
|
+
return { ...NewNodeNoDefs, _key: keyGenerator() };
|
|
3551
|
+
}
|
|
3552
|
+
if ((newNode.markDefs || []).some((def) => !annotations.includes(def._type))) {
|
|
3553
|
+
const allowedAnnotations = (newNode.markDefs || []).filter((def) => annotations.includes(def._type));
|
|
3554
|
+
return { ...newNode, markDefs: allowedAnnotations, _key: keyGenerator() };
|
|
3555
|
+
}
|
|
3556
|
+
newNode.markDefs = (newNode.markDefs || []).map((def) => {
|
|
3557
|
+
const oldKey = def._key, newKey = keyGenerator();
|
|
3558
|
+
return newNode.children = newNode.children.map(
|
|
3559
|
+
(child) => child._type === spanTypeName && editor.isTextSpan(child) ? {
|
|
3560
|
+
...child,
|
|
3561
|
+
marks: child.marks && child.marks.includes(oldKey) ? (
|
|
3562
|
+
// eslint-disable-next-line max-nested-callbacks
|
|
3563
|
+
[...child.marks].filter((mark) => mark !== oldKey).concat(newKey)
|
|
3564
|
+
) : child.marks
|
|
3565
|
+
} : child
|
|
3566
|
+
), { ...def, _key: newKey };
|
|
3567
|
+
});
|
|
3568
|
+
}
|
|
3851
3569
|
const nodeWithNewKeys = { ...newNode, _key: keyGenerator() };
|
|
3852
3570
|
return editor.isTextBlock(nodeWithNewKeys) && (nodeWithNewKeys.children = nodeWithNewKeys.children.map((child) => ({
|
|
3853
3571
|
...child,
|
|
@@ -4700,35 +4418,20 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4700
4418
|
[onCopy]
|
|
4701
4419
|
), handlePaste = react.useCallback(
|
|
4702
4420
|
(event) => {
|
|
4703
|
-
if (event.preventDefault(),
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4708
|
-
new Promise((resolve) => {
|
|
4709
|
-
const value = PortableTextEditor.getValue(portableTextEditor), ptRange = toPortableTextRange(value, slateEditor.selection, schemaTypes), path = (ptRange == null ? void 0 : ptRange.focus.path) || [];
|
|
4710
|
-
resolve(
|
|
4711
|
-
onPaste({
|
|
4712
|
-
event,
|
|
4713
|
-
value,
|
|
4714
|
-
path,
|
|
4715
|
-
schemaTypes
|
|
4716
|
-
})
|
|
4717
|
-
);
|
|
4718
|
-
}).then((result) => {
|
|
4719
|
-
if (debug("Custom paste function from client resolved", result), change$.next({ type: "loading", isLoading: !0 }), !result || !result.insert) {
|
|
4720
|
-
debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData);
|
|
4721
|
-
return;
|
|
4722
|
-
}
|
|
4723
|
-
if (result && result.insert) {
|
|
4724
|
-
slateEditor.insertFragment(
|
|
4725
|
-
toSlateValue(result.insert, { schemaTypes })
|
|
4726
|
-
), change$.next({ type: "loading", isLoading: !1 });
|
|
4727
|
-
return;
|
|
4728
|
-
}
|
|
4729
|
-
console.warn("Your onPaste function returned something unexpected:", result);
|
|
4730
|
-
}).catch((error) => (change$.next({ type: "loading", isLoading: !1 }), console.error(error), error));
|
|
4421
|
+
if (event.preventDefault(), !slateEditor.selection)
|
|
4422
|
+
return;
|
|
4423
|
+
if (!onPaste) {
|
|
4424
|
+
debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
|
|
4425
|
+
return;
|
|
4731
4426
|
}
|
|
4427
|
+
const value = PortableTextEditor.getValue(portableTextEditor), ptRange = toPortableTextRange(value, slateEditor.selection, schemaTypes), path = (ptRange == null ? void 0 : ptRange.focus.path) || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
|
|
4428
|
+
onPasteResult === void 0 ? (debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData)) : (change$.next({ type: "loading", isLoading: !0 }), Promise.resolve(onPasteResult).then((result) => {
|
|
4429
|
+
debug("Custom paste function from client resolved", result), result && result.insert ? slateEditor.insertFragment(
|
|
4430
|
+
toSlateValue(result.insert, { schemaTypes })
|
|
4431
|
+
) : console.warn("Your onPaste function returned something unexpected:", result);
|
|
4432
|
+
}).catch((error) => (console.error(error), error)).finally(() => {
|
|
4433
|
+
change$.next({ type: "loading", isLoading: !1 });
|
|
4434
|
+
}));
|
|
4732
4435
|
},
|
|
4733
4436
|
[change$, onPaste, portableTextEditor, schemaTypes, slateEditor]
|
|
4734
4437
|
), handleOnFocus = react.useCallback(
|
|
@@ -4853,22 +4556,17 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4853
4556
|
}
|
|
4854
4557
|
) : null;
|
|
4855
4558
|
});
|
|
4856
|
-
function compactPatches(patches) {
|
|
4857
|
-
const lastPatch = patches.slice(-1)[0];
|
|
4858
|
-
if (lastPatch && lastPatch.type === "unset" && lastPatch.path.length === 0)
|
|
4859
|
-
return [lastPatch];
|
|
4860
|
-
let finalPatches = patches;
|
|
4861
|
-
return finalPatches = finalPatches.filter((patch, index) => {
|
|
4862
|
-
if (!patch)
|
|
4863
|
-
return !1;
|
|
4864
|
-
const nextPatch = finalPatches[index + 1];
|
|
4865
|
-
return !(nextPatch && nextPatch.type === "set" && patch.type === "set" && isEqual__default.default(patch.path, nextPatch.path));
|
|
4866
|
-
}), finalPatches.length !== patches.length ? finalPatches : patches;
|
|
4867
|
-
}
|
|
4868
4559
|
exports.PortableTextEditable = PortableTextEditable;
|
|
4869
4560
|
exports.PortableTextEditor = PortableTextEditor;
|
|
4870
|
-
exports.compactPatches = compactPatches;
|
|
4871
4561
|
exports.keyGenerator = defaultKeyGenerator;
|
|
4872
4562
|
exports.usePortableTextEditor = usePortableTextEditor;
|
|
4873
4563
|
exports.usePortableTextEditorSelection = usePortableTextEditorSelection;
|
|
4564
|
+
Object.keys(types).forEach(function(k) {
|
|
4565
|
+
k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k) && Object.defineProperty(exports, k, {
|
|
4566
|
+
enumerable: !0,
|
|
4567
|
+
get: function() {
|
|
4568
|
+
return types[k];
|
|
4569
|
+
}
|
|
4570
|
+
});
|
|
4571
|
+
});
|
|
4874
4572
|
//# sourceMappingURL=index.js.map
|