@portabletext/editor 1.50.2 → 1.50.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/lib/index.cjs +259 -235
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +13 -2
- package/lib/index.d.ts +13 -2
- package/lib/index.js +264 -240
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.cts +6 -0
- package/lib/plugins/index.d.ts +6 -0
- package/package.json +13 -13
- package/src/editor/plugins/createWithPatches.ts +37 -75
- package/src/editor/plugins/with-plugins.ts +0 -3
- package/src/editor/relay-machine.ts +9 -0
- package/src/internal-utils/{__tests__/operationToPatches.test.ts → operation-to-patches.test.ts} +44 -39
- package/src/internal-utils/operation-to-patches.ts +467 -0
- package/src/types/editor.ts +7 -2
- package/src/internal-utils/operationToPatches.ts +0 -489
package/lib/index.cjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), useEffectEvent = require("use-effect-event"), useEditor = require("./_chunks-cjs/use-editor.cjs"), jsxRuntime = require("react/jsx-runtime"), react = require("@xstate/react"), noop = require("lodash/noop.js"), slate = require("slate"), slateReact = require("slate-react"), debug$
|
|
3
|
+
var reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), useEffectEvent = require("use-effect-event"), useEditor = require("./_chunks-cjs/use-editor.cjs"), jsxRuntime = require("react/jsx-runtime"), react = require("@xstate/react"), noop = require("lodash/noop.js"), slate = require("slate"), slateReact = require("slate-react"), debug$i = require("debug"), util_isEqualSelectionPoints = require("./_chunks-cjs/util.is-equal-selection-points.cjs"), util_sliceBlocks = require("./_chunks-cjs/util.slice-blocks.cjs"), selector_isSelectionExpanded = require("./_chunks-cjs/selector.is-selection-expanded.cjs"), selector_isSelectingEntireBlocks = require("./_chunks-cjs/selector.is-selecting-entire-blocks.cjs"), slateDom = require("slate-dom"), util_selectionPointToBlockOffset = require("./_chunks-cjs/util.selection-point-to-block-offset.cjs"), isEqual = require("lodash/isEqual.js"), types = require("@sanity/types"), getRandomValues = require("get-random-values-esm"), behaviors_index = require("./behaviors/index.cjs"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), blockTools = require("@portabletext/block-tools"), toHtml = require("@portabletext/to-html"), schema = require("@sanity/schema"), flatten = require("lodash/flatten.js"), omit = require("lodash/omit.js"), util_childSelectionPointToBlockOffset = require("./_chunks-cjs/util.child-selection-point-to-block-offset.cjs"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), startCase = require("lodash.startcase"), isPlainObject = require("lodash/isPlainObject.js");
|
|
4
4
|
function _interopDefaultCompat(e) {
|
|
5
5
|
return e && typeof e == "object" && "default" in e ? e : { default: e };
|
|
6
6
|
}
|
|
7
|
-
var React__default = /* @__PURE__ */ _interopDefaultCompat(React), noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$
|
|
7
|
+
var React__default = /* @__PURE__ */ _interopDefaultCompat(React), noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$i), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), omit__default = /* @__PURE__ */ _interopDefaultCompat(omit), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy), startCase__default = /* @__PURE__ */ _interopDefaultCompat(startCase), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject);
|
|
8
8
|
function EditorEventListener(props) {
|
|
9
9
|
const $ = reactCompilerRuntime.c(5), editor = useEditor.useEditor(), on = useEffectEvent.useEffectEvent(props.on);
|
|
10
10
|
let t0;
|
|
@@ -1348,228 +1348,6 @@ function compileType(rawType) {
|
|
|
1348
1348
|
types: [rawType]
|
|
1349
1349
|
}).get(rawType.name);
|
|
1350
1350
|
}
|
|
1351
|
-
const debug$i = debugWithName("operationToPatches");
|
|
1352
|
-
function createOperationToPatches(editorActor) {
|
|
1353
|
-
const textBlockName = editorActor.getSnapshot().context.schema.block.name;
|
|
1354
|
-
function insertTextPatch(editor, operation, beforeValue) {
|
|
1355
|
-
debug$i.enabled && debug$i("Operation", JSON.stringify(operation, null, 2));
|
|
1356
|
-
const block = editor.isTextBlock(editor.children[operation.path[0]]) && editor.children[operation.path[0]];
|
|
1357
|
-
if (!block)
|
|
1358
|
-
throw new Error("Could not find block");
|
|
1359
|
-
const textChild = editor.isTextBlock(block) && editor.isTextSpan(block.children[operation.path[1]]) && block.children[operation.path[1]];
|
|
1360
|
-
if (!textChild)
|
|
1361
|
-
throw new Error("Could not find child");
|
|
1362
|
-
const path = [{
|
|
1363
|
-
_key: block._key
|
|
1364
|
-
}, "children", {
|
|
1365
|
-
_key: textChild._key
|
|
1366
|
-
}, "text"], prevBlock = beforeValue[operation.path[0]], prevChild = editor.isTextBlock(prevBlock) && prevBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevChild) ? prevChild.text : "", patch = patches.diffMatchPatch(prevText, textChild.text, path);
|
|
1367
|
-
return patch.value.length ? [patch] : [];
|
|
1368
|
-
}
|
|
1369
|
-
function removeTextPatch(editor, operation, beforeValue) {
|
|
1370
|
-
const block = editor && editor.children[operation.path[0]];
|
|
1371
|
-
if (!block)
|
|
1372
|
-
throw new Error("Could not find block");
|
|
1373
|
-
const child = editor.isTextBlock(block) && block.children[operation.path[1]] || void 0, textChild = editor.isTextSpan(child) ? child : void 0;
|
|
1374
|
-
if (child && !textChild)
|
|
1375
|
-
throw new Error("Expected span");
|
|
1376
|
-
if (!textChild)
|
|
1377
|
-
throw new Error("Could not find child");
|
|
1378
|
-
const path = [{
|
|
1379
|
-
_key: block._key
|
|
1380
|
-
}, "children", {
|
|
1381
|
-
_key: textChild._key
|
|
1382
|
-
}, "text"], beforeBlock = beforeValue[operation.path[0]], prevTextChild = editor.isTextBlock(beforeBlock) && beforeBlock.children[operation.path[1]], prevText = editor.isTextSpan(prevTextChild) && prevTextChild.text, patch = patches.diffMatchPatch(prevText || "", textChild.text, path);
|
|
1383
|
-
return patch.value ? [patch] : [];
|
|
1384
|
-
}
|
|
1385
|
-
function setNodePatch(editor, operation) {
|
|
1386
|
-
if (operation.path.length === 1) {
|
|
1387
|
-
const block = editor.children[operation.path[0]];
|
|
1388
|
-
if (typeof block._key != "string")
|
|
1389
|
-
throw new Error("Expected block to have a _key");
|
|
1390
|
-
const setNode = omitBy__default.default({
|
|
1391
|
-
...editor.children[operation.path[0]],
|
|
1392
|
-
...operation.newProperties
|
|
1393
|
-
}, isUndefined__default.default);
|
|
1394
|
-
return [patches.set(fromSlateValue([setNode], textBlockName)[0], [{
|
|
1395
|
-
_key: block._key
|
|
1396
|
-
}])];
|
|
1397
|
-
} else if (operation.path.length === 2) {
|
|
1398
|
-
const block = editor.children[operation.path[0]];
|
|
1399
|
-
if (editor.isTextBlock(block)) {
|
|
1400
|
-
const child = block.children[operation.path[1]];
|
|
1401
|
-
if (child) {
|
|
1402
|
-
const blockKey = block._key, childKey = child._key, patches$1 = [], keys = Object.keys(operation.newProperties);
|
|
1403
|
-
return keys.forEach((keyName) => {
|
|
1404
|
-
if (keys.length === 1 && keyName === "_key") {
|
|
1405
|
-
const val = get__default.default(operation.newProperties, keyName);
|
|
1406
|
-
patches$1.push(patches.set(val, [{
|
|
1407
|
-
_key: blockKey
|
|
1408
|
-
}, "children", block.children.indexOf(child), keyName]));
|
|
1409
|
-
} else {
|
|
1410
|
-
const val = get__default.default(operation.newProperties, keyName);
|
|
1411
|
-
patches$1.push(patches.set(val, [{
|
|
1412
|
-
_key: blockKey
|
|
1413
|
-
}, "children", {
|
|
1414
|
-
_key: childKey
|
|
1415
|
-
}, keyName]));
|
|
1416
|
-
}
|
|
1417
|
-
}), patches$1;
|
|
1418
|
-
}
|
|
1419
|
-
throw new Error("Could not find a valid child");
|
|
1420
|
-
}
|
|
1421
|
-
throw new Error("Could not find a valid block");
|
|
1422
|
-
} else
|
|
1423
|
-
throw new Error(`Unexpected path encountered: ${JSON.stringify(operation.path)}`);
|
|
1424
|
-
}
|
|
1425
|
-
function insertNodePatch(editor, operation, beforeValue) {
|
|
1426
|
-
const block = beforeValue[operation.path[0]], isTextBlock = editor.isTextBlock(block);
|
|
1427
|
-
if (operation.path.length === 1) {
|
|
1428
|
-
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
|
|
1429
|
-
return targetKey ? [patches.insert([fromSlateValue([operation.node], textBlockName)[0]], position, [{
|
|
1430
|
-
_key: targetKey
|
|
1431
|
-
}])] : [patches.setIfMissing(beforeValue, []), patches.insert([fromSlateValue([operation.node], textBlockName)[0]], "before", [operation.path[0]])];
|
|
1432
|
-
} else if (isTextBlock && operation.path.length === 2 && editor.children[operation.path[0]]) {
|
|
1433
|
-
const position = block.children.length === 0 || !block.children[operation.path[1] - 1] ? "before" : "after", node = {
|
|
1434
|
-
...operation.node
|
|
1435
|
-
};
|
|
1436
|
-
!node._type && slate.Text.isText(node) && (node._type = "span", node.marks = []);
|
|
1437
|
-
const child = fromSlateValue([{
|
|
1438
|
-
_key: "bogus",
|
|
1439
|
-
_type: textBlockName,
|
|
1440
|
-
children: [node]
|
|
1441
|
-
}], textBlockName)[0].children[0];
|
|
1442
|
-
return [patches.insert([child], position, [{
|
|
1443
|
-
_key: block._key
|
|
1444
|
-
}, "children", block.children.length <= 1 || !block.children[operation.path[1] - 1] ? 0 : {
|
|
1445
|
-
_key: block.children[operation.path[1] - 1]._key
|
|
1446
|
-
}])];
|
|
1447
|
-
}
|
|
1448
|
-
return debug$i("Something was inserted into a void block. Not producing editor patches."), [];
|
|
1449
|
-
}
|
|
1450
|
-
function splitNodePatch(editor, operation, beforeValue) {
|
|
1451
|
-
const patches$1 = [], splitBlock = editor.children[operation.path[0]];
|
|
1452
|
-
if (!editor.isTextBlock(splitBlock))
|
|
1453
|
-
throw new Error(`Block with path ${JSON.stringify(operation.path[0])} is not a text block and can't be split`);
|
|
1454
|
-
if (operation.path.length === 1) {
|
|
1455
|
-
const oldBlock = beforeValue[operation.path[0]];
|
|
1456
|
-
if (editor.isTextBlock(oldBlock)) {
|
|
1457
|
-
const targetValue = fromSlateValue([editor.children[operation.path[0] + 1]], textBlockName)[0];
|
|
1458
|
-
targetValue && (patches$1.push(patches.insert([targetValue], "after", [{
|
|
1459
|
-
_key: splitBlock._key
|
|
1460
|
-
}])), oldBlock.children.slice(operation.position).forEach((span) => {
|
|
1461
|
-
const path = [{
|
|
1462
|
-
_key: oldBlock._key
|
|
1463
|
-
}, "children", {
|
|
1464
|
-
_key: span._key
|
|
1465
|
-
}];
|
|
1466
|
-
patches$1.push(patches.unset(path));
|
|
1467
|
-
}));
|
|
1468
|
-
}
|
|
1469
|
-
return patches$1;
|
|
1470
|
-
}
|
|
1471
|
-
if (operation.path.length === 2) {
|
|
1472
|
-
const splitSpan = splitBlock.children[operation.path[1]];
|
|
1473
|
-
if (editor.isTextSpan(splitSpan)) {
|
|
1474
|
-
const targetSpans = fromSlateValue([{
|
|
1475
|
-
...splitBlock,
|
|
1476
|
-
children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
|
|
1477
|
-
}], textBlockName)[0].children;
|
|
1478
|
-
patches$1.push(patches.insert(targetSpans, "after", [{
|
|
1479
|
-
_key: splitBlock._key
|
|
1480
|
-
}, "children", {
|
|
1481
|
-
_key: splitSpan._key
|
|
1482
|
-
}])), patches$1.push(patches.set(splitSpan.text, [{
|
|
1483
|
-
_key: splitBlock._key
|
|
1484
|
-
}, "children", {
|
|
1485
|
-
_key: splitSpan._key
|
|
1486
|
-
}, "text"]));
|
|
1487
|
-
}
|
|
1488
|
-
return patches$1;
|
|
1489
|
-
}
|
|
1490
|
-
return patches$1;
|
|
1491
|
-
}
|
|
1492
|
-
function removeNodePatch(editor, operation, beforeValue) {
|
|
1493
|
-
const block = beforeValue[operation.path[0]];
|
|
1494
|
-
if (operation.path.length === 1) {
|
|
1495
|
-
if (block && block._key)
|
|
1496
|
-
return [patches.unset([{
|
|
1497
|
-
_key: block._key
|
|
1498
|
-
}])];
|
|
1499
|
-
throw new Error("Block not found");
|
|
1500
|
-
} else if (editor.isTextBlock(block) && operation.path.length === 2) {
|
|
1501
|
-
const spanToRemove = block.children[operation.path[1]];
|
|
1502
|
-
return spanToRemove ? block.children.filter((span) => span._key === operation.node._key).length > 1 ? (console.warn(`Multiple spans have \`_key\` ${operation.node._key}. It's ambiguous which one to remove.`, JSON.stringify(block, null, 2)), []) : [patches.unset([{
|
|
1503
|
-
_key: block._key
|
|
1504
|
-
}, "children", {
|
|
1505
|
-
_key: spanToRemove._key
|
|
1506
|
-
}])] : (debug$i("Span not found in editor trying to remove node"), []);
|
|
1507
|
-
} else
|
|
1508
|
-
return debug$i("Not creating patch inside object block"), [];
|
|
1509
|
-
}
|
|
1510
|
-
function mergeNodePatch(editor, operation, beforeValue) {
|
|
1511
|
-
const patches$1 = [], block = beforeValue[operation.path[0]], updatedBlock = editor.children[operation.path[0]];
|
|
1512
|
-
if (operation.path.length === 1)
|
|
1513
|
-
if (block?._key) {
|
|
1514
|
-
const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
|
|
1515
|
-
patches$1.push(patches.set(newBlock, [{
|
|
1516
|
-
_key: newBlock._key
|
|
1517
|
-
}])), patches$1.push(patches.unset([{
|
|
1518
|
-
_key: block._key
|
|
1519
|
-
}]));
|
|
1520
|
-
} else
|
|
1521
|
-
throw new Error("Target key not found!");
|
|
1522
|
-
else if (editor.isTextBlock(block) && editor.isTextBlock(updatedBlock) && operation.path.length === 2) {
|
|
1523
|
-
const updatedSpan = updatedBlock.children[operation.path[1] - 1] && editor.isTextSpan(updatedBlock.children[operation.path[1] - 1]) ? updatedBlock.children[operation.path[1] - 1] : void 0, removedSpan = block.children[operation.path[1]] && editor.isTextSpan(block.children[operation.path[1]]) ? block.children[operation.path[1]] : void 0;
|
|
1524
|
-
updatedSpan && (block.children.filter((span) => span._key === updatedSpan._key).length === 1 ? patches$1.push(patches.set(updatedSpan.text, [{
|
|
1525
|
-
_key: block._key
|
|
1526
|
-
}, "children", {
|
|
1527
|
-
_key: updatedSpan._key
|
|
1528
|
-
}, "text"])) : console.warn(`Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`, JSON.stringify(block, null, 2))), removedSpan && (block.children.filter((span) => span._key === removedSpan._key).length === 1 ? patches$1.push(patches.unset([{
|
|
1529
|
-
_key: block._key
|
|
1530
|
-
}, "children", {
|
|
1531
|
-
_key: removedSpan._key
|
|
1532
|
-
}])) : console.warn(`Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`, JSON.stringify(block, null, 2)));
|
|
1533
|
-
} else
|
|
1534
|
-
debug$i("Void nodes can't be merged, not creating any patches");
|
|
1535
|
-
return patches$1;
|
|
1536
|
-
}
|
|
1537
|
-
function moveNodePatch(editor, operation, beforeValue) {
|
|
1538
|
-
const patches$1 = [], block = beforeValue[operation.path[0]], targetBlock = beforeValue[operation.newPath[0]];
|
|
1539
|
-
if (!targetBlock)
|
|
1540
|
-
return patches$1;
|
|
1541
|
-
if (operation.path.length === 1) {
|
|
1542
|
-
const position = operation.path[0] > operation.newPath[0] ? "before" : "after";
|
|
1543
|
-
patches$1.push(patches.unset([{
|
|
1544
|
-
_key: block._key
|
|
1545
|
-
}])), patches$1.push(patches.insert([fromSlateValue([block], textBlockName)[0]], position, [{
|
|
1546
|
-
_key: targetBlock._key
|
|
1547
|
-
}]));
|
|
1548
|
-
} else if (operation.path.length === 2 && editor.isTextBlock(block) && editor.isTextBlock(targetBlock)) {
|
|
1549
|
-
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]];
|
|
1550
|
-
patches$1.push(patches.unset([{
|
|
1551
|
-
_key: block._key
|
|
1552
|
-
}, "children", {
|
|
1553
|
-
_key: child._key
|
|
1554
|
-
}])), patches$1.push(patches.insert([childToInsert], position, [{
|
|
1555
|
-
_key: targetBlock._key
|
|
1556
|
-
}, "children", {
|
|
1557
|
-
_key: targetChild._key
|
|
1558
|
-
}]));
|
|
1559
|
-
}
|
|
1560
|
-
return patches$1;
|
|
1561
|
-
}
|
|
1562
|
-
return {
|
|
1563
|
-
insertNodePatch,
|
|
1564
|
-
insertTextPatch,
|
|
1565
|
-
mergeNodePatch,
|
|
1566
|
-
moveNodePatch,
|
|
1567
|
-
removeNodePatch,
|
|
1568
|
-
removeTextPatch,
|
|
1569
|
-
setNodePatch,
|
|
1570
|
-
splitNodePatch
|
|
1571
|
-
};
|
|
1572
|
-
}
|
|
1573
1351
|
const insertTextOperationImplementation = ({
|
|
1574
1352
|
operation
|
|
1575
1353
|
}) => {
|
|
@@ -4411,6 +4189,254 @@ function findBlockAndChildFromPath(editor, path) {
|
|
|
4411
4189
|
childPath: void 0
|
|
4412
4190
|
};
|
|
4413
4191
|
}
|
|
4192
|
+
function insertTextPatch(schema2, children, operation, beforeValue) {
|
|
4193
|
+
const block = util_sliceBlocks.isTextBlock({
|
|
4194
|
+
schema: schema2
|
|
4195
|
+
}, children[operation.path[0]]) && children[operation.path[0]];
|
|
4196
|
+
if (!block)
|
|
4197
|
+
throw new Error("Could not find block");
|
|
4198
|
+
const textChild = util_sliceBlocks.isTextBlock({
|
|
4199
|
+
schema: schema2
|
|
4200
|
+
}, block) && util_sliceBlocks.isSpan$1({
|
|
4201
|
+
schema: schema2
|
|
4202
|
+
}, block.children[operation.path[1]]) && block.children[operation.path[1]];
|
|
4203
|
+
if (!textChild)
|
|
4204
|
+
throw new Error("Could not find child");
|
|
4205
|
+
const path = [{
|
|
4206
|
+
_key: block._key
|
|
4207
|
+
}, "children", {
|
|
4208
|
+
_key: textChild._key
|
|
4209
|
+
}, "text"], prevBlock = beforeValue[operation.path[0]], prevChild = util_sliceBlocks.isTextBlock({
|
|
4210
|
+
schema: schema2
|
|
4211
|
+
}, prevBlock) && prevBlock.children[operation.path[1]], prevText = util_sliceBlocks.isSpan$1({
|
|
4212
|
+
schema: schema2
|
|
4213
|
+
}, prevChild) ? prevChild.text : "", patch = patches.diffMatchPatch(prevText, textChild.text, path);
|
|
4214
|
+
return patch.value.length ? [patch] : [];
|
|
4215
|
+
}
|
|
4216
|
+
function removeTextPatch(schema2, children, operation, beforeValue) {
|
|
4217
|
+
const block = children[operation.path[0]];
|
|
4218
|
+
if (!block)
|
|
4219
|
+
throw new Error("Could not find block");
|
|
4220
|
+
const child = util_sliceBlocks.isTextBlock({
|
|
4221
|
+
schema: schema2
|
|
4222
|
+
}, block) && block.children[operation.path[1]] || void 0, textChild = util_sliceBlocks.isSpan$1({
|
|
4223
|
+
schema: schema2
|
|
4224
|
+
}, child) ? child : void 0;
|
|
4225
|
+
if (child && !textChild)
|
|
4226
|
+
throw new Error("Expected span");
|
|
4227
|
+
if (!textChild)
|
|
4228
|
+
throw new Error("Could not find child");
|
|
4229
|
+
const path = [{
|
|
4230
|
+
_key: block._key
|
|
4231
|
+
}, "children", {
|
|
4232
|
+
_key: textChild._key
|
|
4233
|
+
}, "text"], beforeBlock = beforeValue[operation.path[0]], prevTextChild = util_sliceBlocks.isTextBlock({
|
|
4234
|
+
schema: schema2
|
|
4235
|
+
}, beforeBlock) && beforeBlock.children[operation.path[1]], prevText = util_sliceBlocks.isSpan$1({
|
|
4236
|
+
schema: schema2
|
|
4237
|
+
}, prevTextChild) && prevTextChild.text, patch = patches.diffMatchPatch(prevText || "", textChild.text, path);
|
|
4238
|
+
return patch.value ? [patch] : [];
|
|
4239
|
+
}
|
|
4240
|
+
function setNodePatch(schema2, children, operation) {
|
|
4241
|
+
if (operation.path.length === 1) {
|
|
4242
|
+
const block = children[operation.path[0]];
|
|
4243
|
+
if (typeof block._key != "string")
|
|
4244
|
+
throw new Error("Expected block to have a _key");
|
|
4245
|
+
const setNode = omitBy__default.default({
|
|
4246
|
+
...children[operation.path[0]],
|
|
4247
|
+
...operation.newProperties
|
|
4248
|
+
}, isUndefined__default.default);
|
|
4249
|
+
return [patches.set(fromSlateValue([setNode], schema2.block.name)[0], [{
|
|
4250
|
+
_key: block._key
|
|
4251
|
+
}])];
|
|
4252
|
+
} else if (operation.path.length === 2) {
|
|
4253
|
+
const block = children[operation.path[0]];
|
|
4254
|
+
if (util_sliceBlocks.isTextBlock({
|
|
4255
|
+
schema: schema2
|
|
4256
|
+
}, block)) {
|
|
4257
|
+
const child = block.children[operation.path[1]];
|
|
4258
|
+
if (child) {
|
|
4259
|
+
const blockKey = block._key, childKey = child._key, patches$1 = [], keys = Object.keys(operation.newProperties);
|
|
4260
|
+
return keys.forEach((keyName) => {
|
|
4261
|
+
if (keys.length === 1 && keyName === "_key") {
|
|
4262
|
+
const val = get__default.default(operation.newProperties, keyName);
|
|
4263
|
+
patches$1.push(patches.set(val, [{
|
|
4264
|
+
_key: blockKey
|
|
4265
|
+
}, "children", block.children.indexOf(child), keyName]));
|
|
4266
|
+
} else {
|
|
4267
|
+
const val = get__default.default(operation.newProperties, keyName);
|
|
4268
|
+
patches$1.push(patches.set(val, [{
|
|
4269
|
+
_key: blockKey
|
|
4270
|
+
}, "children", {
|
|
4271
|
+
_key: childKey
|
|
4272
|
+
}, keyName]));
|
|
4273
|
+
}
|
|
4274
|
+
}), patches$1;
|
|
4275
|
+
}
|
|
4276
|
+
throw new Error("Could not find a valid child");
|
|
4277
|
+
}
|
|
4278
|
+
throw new Error("Could not find a valid block");
|
|
4279
|
+
} else
|
|
4280
|
+
throw new Error(`Unexpected path encountered: ${JSON.stringify(operation.path)}`);
|
|
4281
|
+
}
|
|
4282
|
+
function insertNodePatch(schema2, children, operation, beforeValue) {
|
|
4283
|
+
const block = beforeValue[operation.path[0]];
|
|
4284
|
+
if (operation.path.length === 1) {
|
|
4285
|
+
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
|
|
4286
|
+
return targetKey ? [patches.insert([fromSlateValue([operation.node], schema2.block.name)[0]], position, [{
|
|
4287
|
+
_key: targetKey
|
|
4288
|
+
}])] : [patches.setIfMissing(beforeValue, []), patches.insert([fromSlateValue([operation.node], schema2.block.name)[0]], "before", [operation.path[0]])];
|
|
4289
|
+
} else if (util_sliceBlocks.isTextBlock({
|
|
4290
|
+
schema: schema2
|
|
4291
|
+
}, block) && operation.path.length === 2 && children[operation.path[0]]) {
|
|
4292
|
+
const position = block.children.length === 0 || !block.children[operation.path[1] - 1] ? "before" : "after", node = {
|
|
4293
|
+
...operation.node
|
|
4294
|
+
};
|
|
4295
|
+
!node._type && slate.Text.isText(node) && (node._type = "span", node.marks = []);
|
|
4296
|
+
const child = fromSlateValue([{
|
|
4297
|
+
_key: "bogus",
|
|
4298
|
+
_type: schema2.block.name,
|
|
4299
|
+
children: [node]
|
|
4300
|
+
}], schema2.block.name)[0].children[0];
|
|
4301
|
+
return [patches.insert([child], position, [{
|
|
4302
|
+
_key: block._key
|
|
4303
|
+
}, "children", block.children.length <= 1 || !block.children[operation.path[1] - 1] ? 0 : {
|
|
4304
|
+
_key: block.children[operation.path[1] - 1]._key
|
|
4305
|
+
}])];
|
|
4306
|
+
}
|
|
4307
|
+
return [];
|
|
4308
|
+
}
|
|
4309
|
+
function splitNodePatch(schema2, children, operation, beforeValue) {
|
|
4310
|
+
const patches$1 = [], splitBlock = children[operation.path[0]];
|
|
4311
|
+
if (!util_sliceBlocks.isTextBlock({
|
|
4312
|
+
schema: schema2
|
|
4313
|
+
}, splitBlock))
|
|
4314
|
+
throw new Error(`Block with path ${JSON.stringify(operation.path[0])} is not a text block and can't be split`);
|
|
4315
|
+
if (operation.path.length === 1) {
|
|
4316
|
+
const oldBlock = beforeValue[operation.path[0]];
|
|
4317
|
+
if (util_sliceBlocks.isTextBlock({
|
|
4318
|
+
schema: schema2
|
|
4319
|
+
}, oldBlock)) {
|
|
4320
|
+
const targetValue = fromSlateValue([children[operation.path[0] + 1]], schema2.block.name)[0];
|
|
4321
|
+
targetValue && (patches$1.push(patches.insert([targetValue], "after", [{
|
|
4322
|
+
_key: splitBlock._key
|
|
4323
|
+
}])), oldBlock.children.slice(operation.position).forEach((span) => {
|
|
4324
|
+
const path = [{
|
|
4325
|
+
_key: oldBlock._key
|
|
4326
|
+
}, "children", {
|
|
4327
|
+
_key: span._key
|
|
4328
|
+
}];
|
|
4329
|
+
patches$1.push(patches.unset(path));
|
|
4330
|
+
}));
|
|
4331
|
+
}
|
|
4332
|
+
return patches$1;
|
|
4333
|
+
}
|
|
4334
|
+
if (operation.path.length === 2) {
|
|
4335
|
+
const splitSpan = splitBlock.children[operation.path[1]];
|
|
4336
|
+
if (util_sliceBlocks.isSpan$1({
|
|
4337
|
+
schema: schema2
|
|
4338
|
+
}, splitSpan)) {
|
|
4339
|
+
const targetSpans = fromSlateValue([{
|
|
4340
|
+
...splitBlock,
|
|
4341
|
+
children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
|
|
4342
|
+
}], schema2.block.name)[0].children;
|
|
4343
|
+
patches$1.push(patches.insert(targetSpans, "after", [{
|
|
4344
|
+
_key: splitBlock._key
|
|
4345
|
+
}, "children", {
|
|
4346
|
+
_key: splitSpan._key
|
|
4347
|
+
}])), patches$1.push(patches.set(splitSpan.text, [{
|
|
4348
|
+
_key: splitBlock._key
|
|
4349
|
+
}, "children", {
|
|
4350
|
+
_key: splitSpan._key
|
|
4351
|
+
}, "text"]));
|
|
4352
|
+
}
|
|
4353
|
+
return patches$1;
|
|
4354
|
+
}
|
|
4355
|
+
return patches$1;
|
|
4356
|
+
}
|
|
4357
|
+
function removeNodePatch(schema2, beforeValue, operation) {
|
|
4358
|
+
const block = beforeValue[operation.path[0]];
|
|
4359
|
+
if (operation.path.length === 1) {
|
|
4360
|
+
if (block && block._key)
|
|
4361
|
+
return [patches.unset([{
|
|
4362
|
+
_key: block._key
|
|
4363
|
+
}])];
|
|
4364
|
+
throw new Error("Block not found");
|
|
4365
|
+
} else if (util_sliceBlocks.isTextBlock({
|
|
4366
|
+
schema: schema2
|
|
4367
|
+
}, block) && operation.path.length === 2) {
|
|
4368
|
+
const spanToRemove = block.children[operation.path[1]];
|
|
4369
|
+
return spanToRemove ? block.children.filter((span) => span._key === operation.node._key).length > 1 ? (console.warn(`Multiple spans have \`_key\` ${operation.node._key}. It's ambiguous which one to remove.`, JSON.stringify(block, null, 2)), []) : [patches.unset([{
|
|
4370
|
+
_key: block._key
|
|
4371
|
+
}, "children", {
|
|
4372
|
+
_key: spanToRemove._key
|
|
4373
|
+
}])] : [];
|
|
4374
|
+
} else
|
|
4375
|
+
return [];
|
|
4376
|
+
}
|
|
4377
|
+
function mergeNodePatch(schema2, children, operation, beforeValue) {
|
|
4378
|
+
const patches$1 = [], block = beforeValue[operation.path[0]], updatedBlock = children[operation.path[0]];
|
|
4379
|
+
if (operation.path.length === 1)
|
|
4380
|
+
if (block?._key) {
|
|
4381
|
+
const newBlock = fromSlateValue([children[operation.path[0] - 1]], schema2.block.name)[0];
|
|
4382
|
+
patches$1.push(patches.set(newBlock, [{
|
|
4383
|
+
_key: newBlock._key
|
|
4384
|
+
}])), patches$1.push(patches.unset([{
|
|
4385
|
+
_key: block._key
|
|
4386
|
+
}]));
|
|
4387
|
+
} else
|
|
4388
|
+
throw new Error("Target key not found!");
|
|
4389
|
+
else if (util_sliceBlocks.isTextBlock({
|
|
4390
|
+
schema: schema2
|
|
4391
|
+
}, block) && util_sliceBlocks.isTextBlock({
|
|
4392
|
+
schema: schema2
|
|
4393
|
+
}, updatedBlock) && operation.path.length === 2) {
|
|
4394
|
+
const updatedSpan = updatedBlock.children[operation.path[1] - 1] && util_sliceBlocks.isSpan$1({
|
|
4395
|
+
schema: schema2
|
|
4396
|
+
}, updatedBlock.children[operation.path[1] - 1]) ? updatedBlock.children[operation.path[1] - 1] : void 0, removedSpan = block.children[operation.path[1]] && util_sliceBlocks.isSpan$1({
|
|
4397
|
+
schema: schema2
|
|
4398
|
+
}, block.children[operation.path[1]]) ? block.children[operation.path[1]] : void 0;
|
|
4399
|
+
updatedSpan && (block.children.filter((span) => span._key === updatedSpan._key).length === 1 ? patches$1.push(patches.set(updatedSpan.text, [{
|
|
4400
|
+
_key: block._key
|
|
4401
|
+
}, "children", {
|
|
4402
|
+
_key: updatedSpan._key
|
|
4403
|
+
}, "text"])) : console.warn(`Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`, JSON.stringify(block, null, 2))), removedSpan && (block.children.filter((span) => span._key === removedSpan._key).length === 1 ? patches$1.push(patches.unset([{
|
|
4404
|
+
_key: block._key
|
|
4405
|
+
}, "children", {
|
|
4406
|
+
_key: removedSpan._key
|
|
4407
|
+
}])) : console.warn(`Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`, JSON.stringify(block, null, 2)));
|
|
4408
|
+
}
|
|
4409
|
+
return patches$1;
|
|
4410
|
+
}
|
|
4411
|
+
function moveNodePatch(schema2, beforeValue, operation) {
|
|
4412
|
+
const patches$1 = [], block = beforeValue[operation.path[0]], targetBlock = beforeValue[operation.newPath[0]];
|
|
4413
|
+
if (!targetBlock)
|
|
4414
|
+
return patches$1;
|
|
4415
|
+
if (operation.path.length === 1) {
|
|
4416
|
+
const position = operation.path[0] > operation.newPath[0] ? "before" : "after";
|
|
4417
|
+
patches$1.push(patches.unset([{
|
|
4418
|
+
_key: block._key
|
|
4419
|
+
}])), patches$1.push(patches.insert([fromSlateValue([block], schema2.block.name)[0]], position, [{
|
|
4420
|
+
_key: targetBlock._key
|
|
4421
|
+
}]));
|
|
4422
|
+
} else if (operation.path.length === 2 && util_sliceBlocks.isTextBlock({
|
|
4423
|
+
schema: schema2
|
|
4424
|
+
}, block) && util_sliceBlocks.isTextBlock({
|
|
4425
|
+
schema: schema2
|
|
4426
|
+
}, targetBlock)) {
|
|
4427
|
+
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], schema2.block.name)[0].children[operation.path[1]];
|
|
4428
|
+
patches$1.push(patches.unset([{
|
|
4429
|
+
_key: block._key
|
|
4430
|
+
}, "children", {
|
|
4431
|
+
_key: child._key
|
|
4432
|
+
}])), patches$1.push(patches.insert([childToInsert], position, [{
|
|
4433
|
+
_key: targetBlock._key
|
|
4434
|
+
}, "children", {
|
|
4435
|
+
_key: targetChild._key
|
|
4436
|
+
}]));
|
|
4437
|
+
}
|
|
4438
|
+
return patches$1;
|
|
4439
|
+
}
|
|
4414
4440
|
const PATCHING = /* @__PURE__ */ new WeakMap();
|
|
4415
4441
|
function withoutPatching(editor, fn) {
|
|
4416
4442
|
const prev = isPatching(editor);
|
|
@@ -4423,7 +4449,6 @@ const debug$e = debugWithName("plugin:withPatches");
|
|
|
4423
4449
|
function createWithPatches({
|
|
4424
4450
|
editorActor,
|
|
4425
4451
|
relayActor,
|
|
4426
|
-
patchFunctions,
|
|
4427
4452
|
subscriptions
|
|
4428
4453
|
}) {
|
|
4429
4454
|
let previousChildren;
|
|
@@ -4478,28 +4503,28 @@ function createWithPatches({
|
|
|
4478
4503
|
return editor;
|
|
4479
4504
|
switch (editorWasEmpty && !editorIsEmpty && operation.type !== "set_selection" && patches$1.push(patches.insert(previousChildren, "before", [0])), operation.type) {
|
|
4480
4505
|
case "insert_text":
|
|
4481
|
-
patches$1 = [...patches$1, ...
|
|
4506
|
+
patches$1 = [...patches$1, ...insertTextPatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4482
4507
|
break;
|
|
4483
4508
|
case "remove_text":
|
|
4484
|
-
patches$1 = [...patches$1, ...
|
|
4509
|
+
patches$1 = [...patches$1, ...removeTextPatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4485
4510
|
break;
|
|
4486
4511
|
case "remove_node":
|
|
4487
|
-
patches$1 = [...patches$1, ...
|
|
4512
|
+
patches$1 = [...patches$1, ...removeNodePatch(editorActor.getSnapshot().context.schema, previousChildren, operation)];
|
|
4488
4513
|
break;
|
|
4489
4514
|
case "split_node":
|
|
4490
|
-
patches$1 = [...patches$1, ...
|
|
4515
|
+
patches$1 = [...patches$1, ...splitNodePatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4491
4516
|
break;
|
|
4492
4517
|
case "insert_node":
|
|
4493
|
-
patches$1 = [...patches$1, ...
|
|
4518
|
+
patches$1 = [...patches$1, ...insertNodePatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4494
4519
|
break;
|
|
4495
4520
|
case "set_node":
|
|
4496
|
-
patches$1 = [...patches$1, ...
|
|
4521
|
+
patches$1 = [...patches$1, ...setNodePatch(editorActor.getSnapshot().context.schema, editor.children, operation)];
|
|
4497
4522
|
break;
|
|
4498
4523
|
case "merge_node":
|
|
4499
|
-
patches$1 = [...patches$1, ...
|
|
4524
|
+
patches$1 = [...patches$1, ...mergeNodePatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4500
4525
|
break;
|
|
4501
4526
|
case "move_node":
|
|
4502
|
-
patches$1 = [...patches$1, ...
|
|
4527
|
+
patches$1 = [...patches$1, ...moveNodePatch(editorActor.getSnapshot().context.schema, previousChildren, operation)];
|
|
4503
4528
|
break;
|
|
4504
4529
|
}
|
|
4505
4530
|
if (!editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(operation.type) && (patches$1 = [...patches$1, patches.unset([])], relayActor.send({
|
|
@@ -4695,12 +4720,11 @@ const withPlugins = (editor, options) => {
|
|
|
4695
4720
|
const e = editor, {
|
|
4696
4721
|
editorActor,
|
|
4697
4722
|
relayActor
|
|
4698
|
-
} = options,
|
|
4723
|
+
} = options, withObjectKeys = createWithObjectKeys(editorActor), withSchemaTypes = createWithSchemaTypes({
|
|
4699
4724
|
editorActor
|
|
4700
4725
|
}), withPatches = createWithPatches({
|
|
4701
4726
|
editorActor,
|
|
4702
4727
|
relayActor,
|
|
4703
|
-
patchFunctions: operationToPatches,
|
|
4704
4728
|
subscriptions: options.subscriptions
|
|
4705
4729
|
}), withMaxBlocks = createWithMaxBlocks(editorActor), withUndoRedo = createWithUndoRedo({
|
|
4706
4730
|
editorActor,
|