@portabletext/editor 2.21.3 → 2.21.5
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/lib/index.js +19 -13
- package/lib/index.js.map +1 -1
- package/package.json +3 -3
- package/src/editor/sync-machine.ts +9 -5
- package/src/internal-utils/apply-operation-to-portable-text.test.ts +91 -0
- package/src/internal-utils/apply-operation-to-portable-text.ts +6 -3
- package/src/internal-utils/operation-to-patches.test.ts +20 -0
- package/src/internal-utils/operation-to-patches.ts +8 -0
- package/src/internal-utils/portable-text-node.test.ts +132 -0
- package/src/internal-utils/portable-text-node.ts +18 -8
package/lib/index.js
CHANGED
|
@@ -24,7 +24,7 @@ import { toHTML } from "@portabletext/to-html";
|
|
|
24
24
|
import { Schema } from "@sanity/schema";
|
|
25
25
|
import flatten from "lodash/flatten.js";
|
|
26
26
|
import omit from "lodash/omit.js";
|
|
27
|
-
import { applyAll, unset, insert,
|
|
27
|
+
import { applyAll, unset, insert, setIfMissing, set, diffMatchPatch as diffMatchPatch$1 } from "@portabletext/patches";
|
|
28
28
|
import { createDraft, finishDraft } from "immer";
|
|
29
29
|
import { createKeyboardShortcut, code, underline, italic, bold, undo, redo } from "@portabletext/keyboard-shortcuts";
|
|
30
30
|
import { sliceTextBlock } from "./_chunks-es/util.slice-text-block.js";
|
|
@@ -6074,12 +6074,14 @@ function insertNodePatch(schema, children, operation, beforeValue) {
|
|
|
6074
6074
|
...operation.node
|
|
6075
6075
|
};
|
|
6076
6076
|
!node._type && Text.isText(node) && (node._type = "span", node.marks = []);
|
|
6077
|
-
const
|
|
6077
|
+
const setIfMissingPatch = setIfMissing([], [{
|
|
6078
|
+
_key: block._key
|
|
6079
|
+
}, "children"]), child = fromSlateValue([{
|
|
6078
6080
|
_key: "bogus",
|
|
6079
6081
|
_type: schema.block.name,
|
|
6080
6082
|
children: [node]
|
|
6081
6083
|
}], schema.block.name)[0].children[0];
|
|
6082
|
-
return [insert([child], position, [{
|
|
6084
|
+
return [setIfMissingPatch, insert([child], position, [{
|
|
6083
6085
|
_key: block._key
|
|
6084
6086
|
}, "children", block.children.length <= 1 || !block.children[operation.path[1] - 1] ? 0 : {
|
|
6085
6087
|
_key: block.children[operation.path[1] - 1]._key
|
|
@@ -6121,7 +6123,9 @@ function splitNodePatch(schema, children, operation, beforeValue) {
|
|
|
6121
6123
|
...splitBlock,
|
|
6122
6124
|
children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
|
|
6123
6125
|
}], schema.block.name)[0].children;
|
|
6124
|
-
patches.push(
|
|
6126
|
+
patches.push(setIfMissing([], [{
|
|
6127
|
+
_key: splitBlock._key
|
|
6128
|
+
}, "children"])), patches.push(insert(targetSpans, "after", [{
|
|
6125
6129
|
_key: splitBlock._key
|
|
6126
6130
|
}, "children", {
|
|
6127
6131
|
_key: splitSpan._key
|
|
@@ -6210,7 +6214,9 @@ function moveNodePatch(schema, beforeValue, operation) {
|
|
|
6210
6214
|
_key: block._key
|
|
6211
6215
|
}, "children", {
|
|
6212
6216
|
_key: child._key
|
|
6213
|
-
}])), patches.push(
|
|
6217
|
+
}])), patches.push(setIfMissing([], [{
|
|
6218
|
+
_key: targetBlock._key
|
|
6219
|
+
}, "children"])), patches.push(insert([childToInsert], position, [{
|
|
6214
6220
|
_key: targetBlock._key
|
|
6215
6221
|
}, "children", {
|
|
6216
6222
|
_key: targetChild._key
|
|
@@ -6474,11 +6480,11 @@ function isTextBlockNode(context, node) {
|
|
|
6474
6480
|
function isSpanNode(context, node) {
|
|
6475
6481
|
return typeof node != "object" || node === null || "children" in node ? !1 : "_type" in node ? node._type === context.schema.span.name : "text" in node;
|
|
6476
6482
|
}
|
|
6477
|
-
function isPartialSpanNode(node) {
|
|
6478
|
-
return typeof node
|
|
6483
|
+
function isPartialSpanNode(context, node) {
|
|
6484
|
+
return !(typeof node != "object" || node === null || !("text" in node) || typeof node.text != "string" || "_type" in node && node._type !== context.schema.span.name);
|
|
6479
6485
|
}
|
|
6480
6486
|
function isObjectNode(context, node) {
|
|
6481
|
-
return !isEditorNode(node) && !isTextBlockNode(context, node) && !isSpanNode(context, node) && !isPartialSpanNode(node);
|
|
6487
|
+
return !isEditorNode(node) && !isTextBlockNode(context, node) && !isSpanNode(context, node) && !isPartialSpanNode(context, node);
|
|
6482
6488
|
}
|
|
6483
6489
|
function getBlock(root, path) {
|
|
6484
6490
|
const index = path.at(0);
|
|
@@ -6557,7 +6563,7 @@ function applyOperationToPortableTextDraft(context, root, operation) {
|
|
|
6557
6563
|
if (path.length === 2) {
|
|
6558
6564
|
if (!isTextBlockNode(context, parent))
|
|
6559
6565
|
break;
|
|
6560
|
-
if (isPartialSpanNode(insertedNode)) {
|
|
6566
|
+
if (isPartialSpanNode(context, insertedNode)) {
|
|
6561
6567
|
parent.children.splice(index, 0, insertedNode);
|
|
6562
6568
|
break;
|
|
6563
6569
|
}
|
|
@@ -6593,7 +6599,7 @@ function applyOperationToPortableTextDraft(context, root, operation) {
|
|
|
6593
6599
|
if (!node || !prev || !parent)
|
|
6594
6600
|
break;
|
|
6595
6601
|
const index = path[path.length - 1];
|
|
6596
|
-
if (isPartialSpanNode(node) && isPartialSpanNode(prev))
|
|
6602
|
+
if (isPartialSpanNode(context, node) && isPartialSpanNode(context, prev))
|
|
6597
6603
|
prev.text += node.text;
|
|
6598
6604
|
else if (isTextBlockNode(context, node) && isTextBlockNode(context, prev))
|
|
6599
6605
|
prev.children.push(...node.children);
|
|
@@ -6678,7 +6684,7 @@ function applyOperationToPortableTextDraft(context, root, operation) {
|
|
|
6678
6684
|
newProperties.hasOwnProperty(key) || delete node[key];
|
|
6679
6685
|
break;
|
|
6680
6686
|
}
|
|
6681
|
-
if (isPartialSpanNode(node)) {
|
|
6687
|
+
if (isPartialSpanNode(context, node)) {
|
|
6682
6688
|
for (const key in newProperties) {
|
|
6683
6689
|
if (key === "text")
|
|
6684
6690
|
break;
|
|
@@ -12044,7 +12050,7 @@ function syncBlock({
|
|
|
12044
12050
|
type: "patch",
|
|
12045
12051
|
patch
|
|
12046
12052
|
});
|
|
12047
|
-
})), validation.valid || validation.resolution?.autoResolve ? (oldBlock._key === block._key ? (debug$2.enabled && debug$2("Updating block", oldBlock, block), Editor.withoutNormalizing(slateEditor, () => {
|
|
12053
|
+
})), validation.valid || validation.resolution?.autoResolve ? (oldBlock._key === block._key && oldBlock._type === block._type ? (debug$2.enabled && debug$2("Updating block", oldBlock, block), Editor.withoutNormalizing(slateEditor, () => {
|
|
12048
12054
|
withRemoteChanges(slateEditor, () => {
|
|
12049
12055
|
withoutPatching(slateEditor, () => {
|
|
12050
12056
|
updateBlock({
|
|
@@ -12116,7 +12122,7 @@ function updateBlock({
|
|
|
12116
12122
|
}), slateBlock.children.forEach((currentBlockChild, currentBlockChildIndex) => {
|
|
12117
12123
|
const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual(currentBlockChild, oldBlockChild), isTextChanged = !isEqual(currentBlockChild.text, oldBlockChild?.text), path = [index, currentBlockChildIndex];
|
|
12118
12124
|
if (isChildChanged)
|
|
12119
|
-
if (currentBlockChild._key === oldBlockChild?._key) {
|
|
12125
|
+
if (currentBlockChild._key === oldBlockChild?._key && currentBlockChild._type === oldBlockChild?._type) {
|
|
12120
12126
|
debug$2("Updating changed child", currentBlockChild, oldBlockChild), Transforms.setNodes(slateEditor, currentBlockChild, {
|
|
12121
12127
|
at: path
|
|
12122
12128
|
});
|