@portabletext/editor 1.50.2 → 1.50.4
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/behaviors/index.d.cts +1 -0
- package/lib/behaviors/index.d.ts +1 -0
- package/lib/index.cjs +577 -286
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +15 -2
- package/lib/index.d.ts +15 -2
- package/lib/index.js +584 -292
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.cts +7 -0
- package/lib/plugins/index.d.ts +7 -0
- package/lib/selectors/index.d.cts +1 -0
- package/lib/selectors/index.d.ts +1 -0
- package/lib/utils/index.d.cts +1 -0
- package/lib/utils/index.d.ts +1 -0
- package/package.json +14 -13
- package/src/editor/PortableTextEditor.tsx +22 -22
- package/src/editor/create-slate-editor.tsx +9 -1
- package/src/editor/editor-selector.ts +1 -5
- package/src/editor/editor-snapshot.ts +1 -3
- package/src/editor/plugins/createWithPatches.ts +37 -75
- package/src/editor/plugins/slate-plugin.update-value.ts +30 -0
- package/src/editor/plugins/with-plugins.ts +8 -4
- package/src/editor/relay-machine.ts +9 -0
- package/src/internal-utils/apply-operation-to-portable-text.test.ts +175 -0
- package/src/internal-utils/apply-operation-to-portable-text.ts +435 -0
- package/src/internal-utils/create-placeholder-block.ts +20 -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/internal-utils/portable-text-node.ts +209 -0
- package/src/types/editor.ts +8 -2
- package/src/internal-utils/__tests__/patchToOperations.test.ts +0 -312
- package/src/internal-utils/operationToPatches.ts +0 -489
- package/src/internal-utils/slate-children-to-blocks.ts +0 -49
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"), immer = require("immer"), 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,226 +1348,18 @@ function compileType(rawType) {
|
|
|
1348
1348
|
types: [rawType]
|
|
1349
1349
|
}).get(rawType.name);
|
|
1350
1350
|
}
|
|
1351
|
-
|
|
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
|
-
}
|
|
1351
|
+
function createPlaceholderBlock(context) {
|
|
1562
1352
|
return {
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1353
|
+
_type: context.schema.block.name,
|
|
1354
|
+
_key: context.keyGenerator(),
|
|
1355
|
+
style: context.schema.styles[0].name ?? "normal",
|
|
1356
|
+
markDefs: [],
|
|
1357
|
+
children: [{
|
|
1358
|
+
_type: context.schema.span.name,
|
|
1359
|
+
_key: context.keyGenerator(),
|
|
1360
|
+
text: "",
|
|
1361
|
+
marks: []
|
|
1362
|
+
}]
|
|
1571
1363
|
};
|
|
1572
1364
|
}
|
|
1573
1365
|
const insertTextOperationImplementation = ({
|
|
@@ -4411,6 +4203,254 @@ function findBlockAndChildFromPath(editor, path) {
|
|
|
4411
4203
|
childPath: void 0
|
|
4412
4204
|
};
|
|
4413
4205
|
}
|
|
4206
|
+
function insertTextPatch(schema2, children, operation, beforeValue) {
|
|
4207
|
+
const block = util_sliceBlocks.isTextBlock({
|
|
4208
|
+
schema: schema2
|
|
4209
|
+
}, children[operation.path[0]]) && children[operation.path[0]];
|
|
4210
|
+
if (!block)
|
|
4211
|
+
throw new Error("Could not find block");
|
|
4212
|
+
const textChild = util_sliceBlocks.isTextBlock({
|
|
4213
|
+
schema: schema2
|
|
4214
|
+
}, block) && util_sliceBlocks.isSpan$1({
|
|
4215
|
+
schema: schema2
|
|
4216
|
+
}, block.children[operation.path[1]]) && block.children[operation.path[1]];
|
|
4217
|
+
if (!textChild)
|
|
4218
|
+
throw new Error("Could not find child");
|
|
4219
|
+
const path = [{
|
|
4220
|
+
_key: block._key
|
|
4221
|
+
}, "children", {
|
|
4222
|
+
_key: textChild._key
|
|
4223
|
+
}, "text"], prevBlock = beforeValue[operation.path[0]], prevChild = util_sliceBlocks.isTextBlock({
|
|
4224
|
+
schema: schema2
|
|
4225
|
+
}, prevBlock) && prevBlock.children[operation.path[1]], prevText = util_sliceBlocks.isSpan$1({
|
|
4226
|
+
schema: schema2
|
|
4227
|
+
}, prevChild) ? prevChild.text : "", patch = patches.diffMatchPatch(prevText, textChild.text, path);
|
|
4228
|
+
return patch.value.length ? [patch] : [];
|
|
4229
|
+
}
|
|
4230
|
+
function removeTextPatch(schema2, children, operation, beforeValue) {
|
|
4231
|
+
const block = children[operation.path[0]];
|
|
4232
|
+
if (!block)
|
|
4233
|
+
throw new Error("Could not find block");
|
|
4234
|
+
const child = util_sliceBlocks.isTextBlock({
|
|
4235
|
+
schema: schema2
|
|
4236
|
+
}, block) && block.children[operation.path[1]] || void 0, textChild = util_sliceBlocks.isSpan$1({
|
|
4237
|
+
schema: schema2
|
|
4238
|
+
}, child) ? child : void 0;
|
|
4239
|
+
if (child && !textChild)
|
|
4240
|
+
throw new Error("Expected span");
|
|
4241
|
+
if (!textChild)
|
|
4242
|
+
throw new Error("Could not find child");
|
|
4243
|
+
const path = [{
|
|
4244
|
+
_key: block._key
|
|
4245
|
+
}, "children", {
|
|
4246
|
+
_key: textChild._key
|
|
4247
|
+
}, "text"], beforeBlock = beforeValue[operation.path[0]], prevTextChild = util_sliceBlocks.isTextBlock({
|
|
4248
|
+
schema: schema2
|
|
4249
|
+
}, beforeBlock) && beforeBlock.children[operation.path[1]], prevText = util_sliceBlocks.isSpan$1({
|
|
4250
|
+
schema: schema2
|
|
4251
|
+
}, prevTextChild) && prevTextChild.text, patch = patches.diffMatchPatch(prevText || "", textChild.text, path);
|
|
4252
|
+
return patch.value ? [patch] : [];
|
|
4253
|
+
}
|
|
4254
|
+
function setNodePatch(schema2, children, operation) {
|
|
4255
|
+
if (operation.path.length === 1) {
|
|
4256
|
+
const block = children[operation.path[0]];
|
|
4257
|
+
if (typeof block._key != "string")
|
|
4258
|
+
throw new Error("Expected block to have a _key");
|
|
4259
|
+
const setNode = omitBy__default.default({
|
|
4260
|
+
...children[operation.path[0]],
|
|
4261
|
+
...operation.newProperties
|
|
4262
|
+
}, isUndefined__default.default);
|
|
4263
|
+
return [patches.set(fromSlateValue([setNode], schema2.block.name)[0], [{
|
|
4264
|
+
_key: block._key
|
|
4265
|
+
}])];
|
|
4266
|
+
} else if (operation.path.length === 2) {
|
|
4267
|
+
const block = children[operation.path[0]];
|
|
4268
|
+
if (util_sliceBlocks.isTextBlock({
|
|
4269
|
+
schema: schema2
|
|
4270
|
+
}, block)) {
|
|
4271
|
+
const child = block.children[operation.path[1]];
|
|
4272
|
+
if (child) {
|
|
4273
|
+
const blockKey = block._key, childKey = child._key, patches$1 = [], keys = Object.keys(operation.newProperties);
|
|
4274
|
+
return keys.forEach((keyName) => {
|
|
4275
|
+
if (keys.length === 1 && keyName === "_key") {
|
|
4276
|
+
const val = get__default.default(operation.newProperties, keyName);
|
|
4277
|
+
patches$1.push(patches.set(val, [{
|
|
4278
|
+
_key: blockKey
|
|
4279
|
+
}, "children", block.children.indexOf(child), keyName]));
|
|
4280
|
+
} else {
|
|
4281
|
+
const val = get__default.default(operation.newProperties, keyName);
|
|
4282
|
+
patches$1.push(patches.set(val, [{
|
|
4283
|
+
_key: blockKey
|
|
4284
|
+
}, "children", {
|
|
4285
|
+
_key: childKey
|
|
4286
|
+
}, keyName]));
|
|
4287
|
+
}
|
|
4288
|
+
}), patches$1;
|
|
4289
|
+
}
|
|
4290
|
+
throw new Error("Could not find a valid child");
|
|
4291
|
+
}
|
|
4292
|
+
throw new Error("Could not find a valid block");
|
|
4293
|
+
} else
|
|
4294
|
+
throw new Error(`Unexpected path encountered: ${JSON.stringify(operation.path)}`);
|
|
4295
|
+
}
|
|
4296
|
+
function insertNodePatch(schema2, children, operation, beforeValue) {
|
|
4297
|
+
const block = beforeValue[operation.path[0]];
|
|
4298
|
+
if (operation.path.length === 1) {
|
|
4299
|
+
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
|
|
4300
|
+
return targetKey ? [patches.insert([fromSlateValue([operation.node], schema2.block.name)[0]], position, [{
|
|
4301
|
+
_key: targetKey
|
|
4302
|
+
}])] : [patches.setIfMissing(beforeValue, []), patches.insert([fromSlateValue([operation.node], schema2.block.name)[0]], "before", [operation.path[0]])];
|
|
4303
|
+
} else if (util_sliceBlocks.isTextBlock({
|
|
4304
|
+
schema: schema2
|
|
4305
|
+
}, block) && operation.path.length === 2 && children[operation.path[0]]) {
|
|
4306
|
+
const position = block.children.length === 0 || !block.children[operation.path[1] - 1] ? "before" : "after", node = {
|
|
4307
|
+
...operation.node
|
|
4308
|
+
};
|
|
4309
|
+
!node._type && slate.Text.isText(node) && (node._type = "span", node.marks = []);
|
|
4310
|
+
const child = fromSlateValue([{
|
|
4311
|
+
_key: "bogus",
|
|
4312
|
+
_type: schema2.block.name,
|
|
4313
|
+
children: [node]
|
|
4314
|
+
}], schema2.block.name)[0].children[0];
|
|
4315
|
+
return [patches.insert([child], position, [{
|
|
4316
|
+
_key: block._key
|
|
4317
|
+
}, "children", block.children.length <= 1 || !block.children[operation.path[1] - 1] ? 0 : {
|
|
4318
|
+
_key: block.children[operation.path[1] - 1]._key
|
|
4319
|
+
}])];
|
|
4320
|
+
}
|
|
4321
|
+
return [];
|
|
4322
|
+
}
|
|
4323
|
+
function splitNodePatch(schema2, children, operation, beforeValue) {
|
|
4324
|
+
const patches$1 = [], splitBlock = children[operation.path[0]];
|
|
4325
|
+
if (!util_sliceBlocks.isTextBlock({
|
|
4326
|
+
schema: schema2
|
|
4327
|
+
}, splitBlock))
|
|
4328
|
+
throw new Error(`Block with path ${JSON.stringify(operation.path[0])} is not a text block and can't be split`);
|
|
4329
|
+
if (operation.path.length === 1) {
|
|
4330
|
+
const oldBlock = beforeValue[operation.path[0]];
|
|
4331
|
+
if (util_sliceBlocks.isTextBlock({
|
|
4332
|
+
schema: schema2
|
|
4333
|
+
}, oldBlock)) {
|
|
4334
|
+
const targetValue = fromSlateValue([children[operation.path[0] + 1]], schema2.block.name)[0];
|
|
4335
|
+
targetValue && (patches$1.push(patches.insert([targetValue], "after", [{
|
|
4336
|
+
_key: splitBlock._key
|
|
4337
|
+
}])), oldBlock.children.slice(operation.position).forEach((span) => {
|
|
4338
|
+
const path = [{
|
|
4339
|
+
_key: oldBlock._key
|
|
4340
|
+
}, "children", {
|
|
4341
|
+
_key: span._key
|
|
4342
|
+
}];
|
|
4343
|
+
patches$1.push(patches.unset(path));
|
|
4344
|
+
}));
|
|
4345
|
+
}
|
|
4346
|
+
return patches$1;
|
|
4347
|
+
}
|
|
4348
|
+
if (operation.path.length === 2) {
|
|
4349
|
+
const splitSpan = splitBlock.children[operation.path[1]];
|
|
4350
|
+
if (util_sliceBlocks.isSpan$1({
|
|
4351
|
+
schema: schema2
|
|
4352
|
+
}, splitSpan)) {
|
|
4353
|
+
const targetSpans = fromSlateValue([{
|
|
4354
|
+
...splitBlock,
|
|
4355
|
+
children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
|
|
4356
|
+
}], schema2.block.name)[0].children;
|
|
4357
|
+
patches$1.push(patches.insert(targetSpans, "after", [{
|
|
4358
|
+
_key: splitBlock._key
|
|
4359
|
+
}, "children", {
|
|
4360
|
+
_key: splitSpan._key
|
|
4361
|
+
}])), patches$1.push(patches.set(splitSpan.text, [{
|
|
4362
|
+
_key: splitBlock._key
|
|
4363
|
+
}, "children", {
|
|
4364
|
+
_key: splitSpan._key
|
|
4365
|
+
}, "text"]));
|
|
4366
|
+
}
|
|
4367
|
+
return patches$1;
|
|
4368
|
+
}
|
|
4369
|
+
return patches$1;
|
|
4370
|
+
}
|
|
4371
|
+
function removeNodePatch(schema2, beforeValue, operation) {
|
|
4372
|
+
const block = beforeValue[operation.path[0]];
|
|
4373
|
+
if (operation.path.length === 1) {
|
|
4374
|
+
if (block && block._key)
|
|
4375
|
+
return [patches.unset([{
|
|
4376
|
+
_key: block._key
|
|
4377
|
+
}])];
|
|
4378
|
+
throw new Error("Block not found");
|
|
4379
|
+
} else if (util_sliceBlocks.isTextBlock({
|
|
4380
|
+
schema: schema2
|
|
4381
|
+
}, block) && operation.path.length === 2) {
|
|
4382
|
+
const spanToRemove = block.children[operation.path[1]];
|
|
4383
|
+
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([{
|
|
4384
|
+
_key: block._key
|
|
4385
|
+
}, "children", {
|
|
4386
|
+
_key: spanToRemove._key
|
|
4387
|
+
}])] : [];
|
|
4388
|
+
} else
|
|
4389
|
+
return [];
|
|
4390
|
+
}
|
|
4391
|
+
function mergeNodePatch(schema2, children, operation, beforeValue) {
|
|
4392
|
+
const patches$1 = [], block = beforeValue[operation.path[0]], updatedBlock = children[operation.path[0]];
|
|
4393
|
+
if (operation.path.length === 1)
|
|
4394
|
+
if (block?._key) {
|
|
4395
|
+
const newBlock = fromSlateValue([children[operation.path[0] - 1]], schema2.block.name)[0];
|
|
4396
|
+
patches$1.push(patches.set(newBlock, [{
|
|
4397
|
+
_key: newBlock._key
|
|
4398
|
+
}])), patches$1.push(patches.unset([{
|
|
4399
|
+
_key: block._key
|
|
4400
|
+
}]));
|
|
4401
|
+
} else
|
|
4402
|
+
throw new Error("Target key not found!");
|
|
4403
|
+
else if (util_sliceBlocks.isTextBlock({
|
|
4404
|
+
schema: schema2
|
|
4405
|
+
}, block) && util_sliceBlocks.isTextBlock({
|
|
4406
|
+
schema: schema2
|
|
4407
|
+
}, updatedBlock) && operation.path.length === 2) {
|
|
4408
|
+
const updatedSpan = updatedBlock.children[operation.path[1] - 1] && util_sliceBlocks.isSpan$1({
|
|
4409
|
+
schema: schema2
|
|
4410
|
+
}, updatedBlock.children[operation.path[1] - 1]) ? updatedBlock.children[operation.path[1] - 1] : void 0, removedSpan = block.children[operation.path[1]] && util_sliceBlocks.isSpan$1({
|
|
4411
|
+
schema: schema2
|
|
4412
|
+
}, block.children[operation.path[1]]) ? block.children[operation.path[1]] : void 0;
|
|
4413
|
+
updatedSpan && (block.children.filter((span) => span._key === updatedSpan._key).length === 1 ? patches$1.push(patches.set(updatedSpan.text, [{
|
|
4414
|
+
_key: block._key
|
|
4415
|
+
}, "children", {
|
|
4416
|
+
_key: updatedSpan._key
|
|
4417
|
+
}, "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([{
|
|
4418
|
+
_key: block._key
|
|
4419
|
+
}, "children", {
|
|
4420
|
+
_key: removedSpan._key
|
|
4421
|
+
}])) : console.warn(`Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`, JSON.stringify(block, null, 2)));
|
|
4422
|
+
}
|
|
4423
|
+
return patches$1;
|
|
4424
|
+
}
|
|
4425
|
+
function moveNodePatch(schema2, beforeValue, operation) {
|
|
4426
|
+
const patches$1 = [], block = beforeValue[operation.path[0]], targetBlock = beforeValue[operation.newPath[0]];
|
|
4427
|
+
if (!targetBlock)
|
|
4428
|
+
return patches$1;
|
|
4429
|
+
if (operation.path.length === 1) {
|
|
4430
|
+
const position = operation.path[0] > operation.newPath[0] ? "before" : "after";
|
|
4431
|
+
patches$1.push(patches.unset([{
|
|
4432
|
+
_key: block._key
|
|
4433
|
+
}])), patches$1.push(patches.insert([fromSlateValue([block], schema2.block.name)[0]], position, [{
|
|
4434
|
+
_key: targetBlock._key
|
|
4435
|
+
}]));
|
|
4436
|
+
} else if (operation.path.length === 2 && util_sliceBlocks.isTextBlock({
|
|
4437
|
+
schema: schema2
|
|
4438
|
+
}, block) && util_sliceBlocks.isTextBlock({
|
|
4439
|
+
schema: schema2
|
|
4440
|
+
}, targetBlock)) {
|
|
4441
|
+
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]];
|
|
4442
|
+
patches$1.push(patches.unset([{
|
|
4443
|
+
_key: block._key
|
|
4444
|
+
}, "children", {
|
|
4445
|
+
_key: child._key
|
|
4446
|
+
}])), patches$1.push(patches.insert([childToInsert], position, [{
|
|
4447
|
+
_key: targetBlock._key
|
|
4448
|
+
}, "children", {
|
|
4449
|
+
_key: targetChild._key
|
|
4450
|
+
}]));
|
|
4451
|
+
}
|
|
4452
|
+
return patches$1;
|
|
4453
|
+
}
|
|
4414
4454
|
const PATCHING = /* @__PURE__ */ new WeakMap();
|
|
4415
4455
|
function withoutPatching(editor, fn) {
|
|
4416
4456
|
const prev = isPatching(editor);
|
|
@@ -4423,7 +4463,6 @@ const debug$e = debugWithName("plugin:withPatches");
|
|
|
4423
4463
|
function createWithPatches({
|
|
4424
4464
|
editorActor,
|
|
4425
4465
|
relayActor,
|
|
4426
|
-
patchFunctions,
|
|
4427
4466
|
subscriptions
|
|
4428
4467
|
}) {
|
|
4429
4468
|
let previousChildren;
|
|
@@ -4478,28 +4517,28 @@ function createWithPatches({
|
|
|
4478
4517
|
return editor;
|
|
4479
4518
|
switch (editorWasEmpty && !editorIsEmpty && operation.type !== "set_selection" && patches$1.push(patches.insert(previousChildren, "before", [0])), operation.type) {
|
|
4480
4519
|
case "insert_text":
|
|
4481
|
-
patches$1 = [...patches$1, ...
|
|
4520
|
+
patches$1 = [...patches$1, ...insertTextPatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4482
4521
|
break;
|
|
4483
4522
|
case "remove_text":
|
|
4484
|
-
patches$1 = [...patches$1, ...
|
|
4523
|
+
patches$1 = [...patches$1, ...removeTextPatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4485
4524
|
break;
|
|
4486
4525
|
case "remove_node":
|
|
4487
|
-
patches$1 = [...patches$1, ...
|
|
4526
|
+
patches$1 = [...patches$1, ...removeNodePatch(editorActor.getSnapshot().context.schema, previousChildren, operation)];
|
|
4488
4527
|
break;
|
|
4489
4528
|
case "split_node":
|
|
4490
|
-
patches$1 = [...patches$1, ...
|
|
4529
|
+
patches$1 = [...patches$1, ...splitNodePatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4491
4530
|
break;
|
|
4492
4531
|
case "insert_node":
|
|
4493
|
-
patches$1 = [...patches$1, ...
|
|
4532
|
+
patches$1 = [...patches$1, ...insertNodePatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4494
4533
|
break;
|
|
4495
4534
|
case "set_node":
|
|
4496
|
-
patches$1 = [...patches$1, ...
|
|
4535
|
+
patches$1 = [...patches$1, ...setNodePatch(editorActor.getSnapshot().context.schema, editor.children, operation)];
|
|
4497
4536
|
break;
|
|
4498
4537
|
case "merge_node":
|
|
4499
|
-
patches$1 = [...patches$1, ...
|
|
4538
|
+
patches$1 = [...patches$1, ...mergeNodePatch(editorActor.getSnapshot().context.schema, editor.children, operation, previousChildren)];
|
|
4500
4539
|
break;
|
|
4501
4540
|
case "move_node":
|
|
4502
|
-
patches$1 = [...patches$1, ...
|
|
4541
|
+
patches$1 = [...patches$1, ...moveNodePatch(editorActor.getSnapshot().context.schema, previousChildren, operation)];
|
|
4503
4542
|
break;
|
|
4504
4543
|
}
|
|
4505
4544
|
if (!editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(operation.type) && (patches$1 = [...patches$1, patches.unset([])], relayActor.send({
|
|
@@ -4691,16 +4730,298 @@ function createWithUtils({
|
|
|
4691
4730
|
})[0], editor;
|
|
4692
4731
|
};
|
|
4693
4732
|
}
|
|
4733
|
+
function isEditorNode(node) {
|
|
4734
|
+
return typeof node == "object" && node !== null ? !("_type" in node) && "children" in node && Array.isArray(node.children) : !1;
|
|
4735
|
+
}
|
|
4736
|
+
function isTextBlockNode(context, node) {
|
|
4737
|
+
return util_sliceBlocks.isTypedObject(node) && node._type === context.schema.block.name;
|
|
4738
|
+
}
|
|
4739
|
+
function isSpanNode(context, node) {
|
|
4740
|
+
return typeof node != "object" || node === null || "children" in node ? !1 : "_type" in node ? node._type === context.schema.span.name : "text" in node;
|
|
4741
|
+
}
|
|
4742
|
+
function isPartialSpanNode(node) {
|
|
4743
|
+
return typeof node == "object" && node !== null && "text" in node && typeof node.text == "string";
|
|
4744
|
+
}
|
|
4745
|
+
function isObjectNode(context, node) {
|
|
4746
|
+
return !isEditorNode(node) && !isTextBlockNode(context, node) && !isSpanNode(context, node) && !isPartialSpanNode(node);
|
|
4747
|
+
}
|
|
4748
|
+
function getBlock(root, path) {
|
|
4749
|
+
const index = path.at(0);
|
|
4750
|
+
if (!(index === void 0 || path.length !== 1))
|
|
4751
|
+
return root.children.at(index);
|
|
4752
|
+
}
|
|
4753
|
+
function getNode(context, root, path) {
|
|
4754
|
+
if (path.length === 0)
|
|
4755
|
+
return root;
|
|
4756
|
+
if (path.length === 1)
|
|
4757
|
+
return getBlock(root, path);
|
|
4758
|
+
if (path.length === 2) {
|
|
4759
|
+
const block = getBlock(root, path.slice(0, 1));
|
|
4760
|
+
return !block || !isTextBlockNode(context, block) ? void 0 : block.children.at(path[1]) || void 0;
|
|
4761
|
+
}
|
|
4762
|
+
}
|
|
4763
|
+
function getSpan(context, root, path) {
|
|
4764
|
+
const node = getNode(context, root, path);
|
|
4765
|
+
if (node && isSpanNode(context, node))
|
|
4766
|
+
return node;
|
|
4767
|
+
}
|
|
4768
|
+
function getParent(context, root, path) {
|
|
4769
|
+
if (path.length === 0)
|
|
4770
|
+
return;
|
|
4771
|
+
const parentPath = path.slice(0, -1);
|
|
4772
|
+
if (parentPath.length === 0)
|
|
4773
|
+
return root;
|
|
4774
|
+
const blockIndex = parentPath.at(0);
|
|
4775
|
+
if (blockIndex === void 0 || parentPath.length !== 1)
|
|
4776
|
+
return;
|
|
4777
|
+
const block = root.children.at(blockIndex);
|
|
4778
|
+
if (block && isTextBlockNode(context, block))
|
|
4779
|
+
return block;
|
|
4780
|
+
}
|
|
4781
|
+
function applyOperationToPortableText(context, value, operation) {
|
|
4782
|
+
const draft = immer.createDraft({
|
|
4783
|
+
children: value
|
|
4784
|
+
});
|
|
4785
|
+
try {
|
|
4786
|
+
applyOperationToPortableTextDraft(context, draft, operation);
|
|
4787
|
+
} catch (e) {
|
|
4788
|
+
console.error(e);
|
|
4789
|
+
}
|
|
4790
|
+
return immer.finishDraft(draft).children;
|
|
4791
|
+
}
|
|
4792
|
+
function applyOperationToPortableTextDraft(context, root, operation) {
|
|
4793
|
+
switch (operation.type) {
|
|
4794
|
+
case "insert_node": {
|
|
4795
|
+
const {
|
|
4796
|
+
path,
|
|
4797
|
+
node: insertedNode
|
|
4798
|
+
} = operation, parent = getParent(context, root, path), index = path[path.length - 1];
|
|
4799
|
+
if (!parent || index > parent.children.length)
|
|
4800
|
+
break;
|
|
4801
|
+
if (path.length === 1) {
|
|
4802
|
+
if (isTextBlockNode(context, insertedNode)) {
|
|
4803
|
+
parent.children.splice(index, 0, {
|
|
4804
|
+
...insertedNode,
|
|
4805
|
+
children: insertedNode.children.map((child) => "__inline" in child ? {
|
|
4806
|
+
_key: child._key,
|
|
4807
|
+
_type: child._type,
|
|
4808
|
+
..."value" in child && typeof child.value == "object" ? child.value : {}
|
|
4809
|
+
} : child)
|
|
4810
|
+
});
|
|
4811
|
+
break;
|
|
4812
|
+
}
|
|
4813
|
+
if (slate.Element.isElement(insertedNode) && !("__inline" in insertedNode)) {
|
|
4814
|
+
parent.children.splice(index, 0, {
|
|
4815
|
+
_key: insertedNode._key,
|
|
4816
|
+
_type: insertedNode._type,
|
|
4817
|
+
..."value" in insertedNode && typeof insertedNode.value == "object" ? insertedNode.value : {}
|
|
4818
|
+
});
|
|
4819
|
+
break;
|
|
4820
|
+
}
|
|
4821
|
+
}
|
|
4822
|
+
if (path.length === 2) {
|
|
4823
|
+
if (!isTextBlockNode(context, parent))
|
|
4824
|
+
break;
|
|
4825
|
+
if (isPartialSpanNode(insertedNode)) {
|
|
4826
|
+
parent.children.splice(index, 0, insertedNode);
|
|
4827
|
+
break;
|
|
4828
|
+
}
|
|
4829
|
+
if ("__inline" in insertedNode) {
|
|
4830
|
+
parent.children.splice(index, 0, {
|
|
4831
|
+
_key: insertedNode._key,
|
|
4832
|
+
_type: insertedNode._type,
|
|
4833
|
+
..."value" in insertedNode && typeof insertedNode.value == "object" ? insertedNode.value : {}
|
|
4834
|
+
});
|
|
4835
|
+
break;
|
|
4836
|
+
}
|
|
4837
|
+
}
|
|
4838
|
+
break;
|
|
4839
|
+
}
|
|
4840
|
+
case "insert_text": {
|
|
4841
|
+
const {
|
|
4842
|
+
path,
|
|
4843
|
+
offset,
|
|
4844
|
+
text
|
|
4845
|
+
} = operation;
|
|
4846
|
+
if (text.length === 0) break;
|
|
4847
|
+
const span = getSpan(context, root, path);
|
|
4848
|
+
if (!span)
|
|
4849
|
+
break;
|
|
4850
|
+
const before = span.text.slice(0, offset), after = span.text.slice(offset);
|
|
4851
|
+
span.text = before + text + after;
|
|
4852
|
+
break;
|
|
4853
|
+
}
|
|
4854
|
+
case "merge_node": {
|
|
4855
|
+
const {
|
|
4856
|
+
path
|
|
4857
|
+
} = operation, node = getNode(context, root, path), prevPath = slate.Path.previous(path), prev = getNode(context, root, prevPath), parent = getParent(context, root, path);
|
|
4858
|
+
if (!node || !prev || !parent)
|
|
4859
|
+
break;
|
|
4860
|
+
const index = path[path.length - 1];
|
|
4861
|
+
if (isPartialSpanNode(node) && isPartialSpanNode(prev))
|
|
4862
|
+
prev.text += node.text;
|
|
4863
|
+
else if (isTextBlockNode(context, node) && isTextBlockNode(context, prev))
|
|
4864
|
+
prev.children.push(...node.children);
|
|
4865
|
+
else
|
|
4866
|
+
break;
|
|
4867
|
+
parent.children.splice(index, 1);
|
|
4868
|
+
break;
|
|
4869
|
+
}
|
|
4870
|
+
case "move_node": {
|
|
4871
|
+
const {
|
|
4872
|
+
path,
|
|
4873
|
+
newPath
|
|
4874
|
+
} = operation;
|
|
4875
|
+
if (slate.Path.isAncestor(path, newPath))
|
|
4876
|
+
break;
|
|
4877
|
+
const node = getNode(context, root, path), parent = getParent(context, root, path), index = path[path.length - 1];
|
|
4878
|
+
if (!node || !parent)
|
|
4879
|
+
break;
|
|
4880
|
+
parent.children.splice(index, 1);
|
|
4881
|
+
const truePath = slate.Path.transform(path, operation), newParent = getNode(context, root, slate.Path.parent(truePath)), newIndex = truePath[truePath.length - 1];
|
|
4882
|
+
if (!newParent || !("children" in newParent) || !Array.isArray(newParent.children))
|
|
4883
|
+
break;
|
|
4884
|
+
newParent.children.splice(newIndex, 0, node);
|
|
4885
|
+
break;
|
|
4886
|
+
}
|
|
4887
|
+
case "remove_node": {
|
|
4888
|
+
const {
|
|
4889
|
+
path
|
|
4890
|
+
} = operation, index = path[path.length - 1];
|
|
4891
|
+
getParent(context, root, path)?.children.splice(index, 1);
|
|
4892
|
+
break;
|
|
4893
|
+
}
|
|
4894
|
+
case "remove_text": {
|
|
4895
|
+
const {
|
|
4896
|
+
path,
|
|
4897
|
+
offset,
|
|
4898
|
+
text
|
|
4899
|
+
} = operation;
|
|
4900
|
+
if (text.length === 0)
|
|
4901
|
+
break;
|
|
4902
|
+
const span = getSpan(context, root, path);
|
|
4903
|
+
if (!span)
|
|
4904
|
+
break;
|
|
4905
|
+
const before = span.text.slice(0, offset), after = span.text.slice(offset + text.length);
|
|
4906
|
+
span.text = before + after;
|
|
4907
|
+
break;
|
|
4908
|
+
}
|
|
4909
|
+
case "set_node": {
|
|
4910
|
+
const {
|
|
4911
|
+
path,
|
|
4912
|
+
properties,
|
|
4913
|
+
newProperties
|
|
4914
|
+
} = operation, node = getNode(context, root, path);
|
|
4915
|
+
if (!node || isEditorNode(node))
|
|
4916
|
+
break;
|
|
4917
|
+
if (isObjectNode(context, node)) {
|
|
4918
|
+
const valueBefore = "value" in properties && typeof properties.value == "object" ? properties.value : {}, valueAfter = "value" in newProperties && typeof newProperties.value == "object" ? newProperties.value : {};
|
|
4919
|
+
for (const key in newProperties) {
|
|
4920
|
+
if (key === "value")
|
|
4921
|
+
continue;
|
|
4922
|
+
const value = newProperties[key];
|
|
4923
|
+
value == null ? delete node[key] : node[key] = value;
|
|
4924
|
+
}
|
|
4925
|
+
for (const key in properties)
|
|
4926
|
+
key !== "value" && (newProperties.hasOwnProperty(key) || delete node[key]);
|
|
4927
|
+
for (const key in valueAfter) {
|
|
4928
|
+
const value = valueAfter[key];
|
|
4929
|
+
value == null ? delete node[key] : node[key] = value;
|
|
4930
|
+
}
|
|
4931
|
+
for (const key in valueBefore)
|
|
4932
|
+
valueAfter.hasOwnProperty(key) || delete node[key];
|
|
4933
|
+
break;
|
|
4934
|
+
}
|
|
4935
|
+
if (isTextBlockNode(context, node)) {
|
|
4936
|
+
for (const key in newProperties) {
|
|
4937
|
+
if (key === "children" || key === "text")
|
|
4938
|
+
break;
|
|
4939
|
+
const value = newProperties[key];
|
|
4940
|
+
value == null ? delete node[key] : node[key] = value;
|
|
4941
|
+
}
|
|
4942
|
+
for (const key in properties)
|
|
4943
|
+
newProperties.hasOwnProperty(key) || delete node[key];
|
|
4944
|
+
break;
|
|
4945
|
+
}
|
|
4946
|
+
if (isPartialSpanNode(node)) {
|
|
4947
|
+
for (const key in newProperties) {
|
|
4948
|
+
if (key === "text")
|
|
4949
|
+
break;
|
|
4950
|
+
const value = newProperties[key];
|
|
4951
|
+
value == null ? delete node[key] : node[key] = value;
|
|
4952
|
+
}
|
|
4953
|
+
for (const key in properties)
|
|
4954
|
+
newProperties.hasOwnProperty(key) || delete node[key];
|
|
4955
|
+
break;
|
|
4956
|
+
}
|
|
4957
|
+
break;
|
|
4958
|
+
}
|
|
4959
|
+
case "split_node": {
|
|
4960
|
+
const {
|
|
4961
|
+
path,
|
|
4962
|
+
position,
|
|
4963
|
+
properties
|
|
4964
|
+
} = operation;
|
|
4965
|
+
if (path.length === 0)
|
|
4966
|
+
break;
|
|
4967
|
+
const parent = getParent(context, root, path), index = path[path.length - 1];
|
|
4968
|
+
if (!parent)
|
|
4969
|
+
break;
|
|
4970
|
+
if (isEditorNode(parent)) {
|
|
4971
|
+
const block = getBlock(root, path);
|
|
4972
|
+
if (!block || !isTextBlockNode(context, block))
|
|
4973
|
+
break;
|
|
4974
|
+
const before = block.children.slice(0, position), after = block.children.slice(position);
|
|
4975
|
+
block.children = before;
|
|
4976
|
+
const newTextBlockNode = {
|
|
4977
|
+
...properties,
|
|
4978
|
+
children: after,
|
|
4979
|
+
_type: context.schema.block.name
|
|
4980
|
+
};
|
|
4981
|
+
parent.children.splice(index + 1, 0, newTextBlockNode);
|
|
4982
|
+
break;
|
|
4983
|
+
}
|
|
4984
|
+
if (isTextBlockNode(context, parent)) {
|
|
4985
|
+
const node = getNode(context, root, path);
|
|
4986
|
+
if (!node || !isSpanNode(context, node))
|
|
4987
|
+
break;
|
|
4988
|
+
const before = node.text.slice(0, position), after = node.text.slice(position);
|
|
4989
|
+
node.text = before;
|
|
4990
|
+
const newSpanNode = {
|
|
4991
|
+
...properties,
|
|
4992
|
+
text: after
|
|
4993
|
+
};
|
|
4994
|
+
parent.children.splice(index + 1, 0, newSpanNode);
|
|
4995
|
+
}
|
|
4996
|
+
break;
|
|
4997
|
+
}
|
|
4998
|
+
}
|
|
4999
|
+
return root;
|
|
5000
|
+
}
|
|
5001
|
+
function pluginUpdateValue(context, editor) {
|
|
5002
|
+
const {
|
|
5003
|
+
apply: apply2
|
|
5004
|
+
} = editor;
|
|
5005
|
+
return editor.apply = (operation) => {
|
|
5006
|
+
if (operation.type === "set_selection") {
|
|
5007
|
+
apply2(operation);
|
|
5008
|
+
return;
|
|
5009
|
+
}
|
|
5010
|
+
editor.value = applyOperationToPortableText({
|
|
5011
|
+
keyGenerator: context.keyGenerator,
|
|
5012
|
+
schema: context.schema
|
|
5013
|
+
}, editor.value, operation), apply2(operation);
|
|
5014
|
+
}, editor;
|
|
5015
|
+
}
|
|
4694
5016
|
const withPlugins = (editor, options) => {
|
|
4695
5017
|
const e = editor, {
|
|
4696
5018
|
editorActor,
|
|
4697
5019
|
relayActor
|
|
4698
|
-
} = options,
|
|
5020
|
+
} = options, withObjectKeys = createWithObjectKeys(editorActor), withSchemaTypes = createWithSchemaTypes({
|
|
4699
5021
|
editorActor
|
|
4700
5022
|
}), withPatches = createWithPatches({
|
|
4701
5023
|
editorActor,
|
|
4702
5024
|
relayActor,
|
|
4703
|
-
patchFunctions: operationToPatches,
|
|
4704
5025
|
subscriptions: options.subscriptions
|
|
4705
5026
|
}), withMaxBlocks = createWithMaxBlocks(editorActor), withUndoRedo = createWithUndoRedo({
|
|
4706
5027
|
editorActor,
|
|
@@ -4708,7 +5029,7 @@ const withPlugins = (editor, options) => {
|
|
|
4708
5029
|
}), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
|
|
4709
5030
|
editorActor
|
|
4710
5031
|
}), withPortableTextSelections = createWithPortableTextSelections(editorActor);
|
|
4711
|
-
return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
|
|
5032
|
+
return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(pluginUpdateValue(editorActor.getSnapshot().context, e))))))))))));
|
|
4712
5033
|
}, debug$a = debugWithName("setup");
|
|
4713
5034
|
function createSlateEditor(config) {
|
|
4714
5035
|
debug$a("Creating new Slate editor instance");
|
|
@@ -4717,10 +5038,10 @@ function createSlateEditor(config) {
|
|
|
4717
5038
|
relayActor: config.relayActor,
|
|
4718
5039
|
subscriptions: config.subscriptions
|
|
4719
5040
|
});
|
|
4720
|
-
KEY_TO_VALUE_ELEMENT.set(instance, {}), KEY_TO_SLATE_ELEMENT.set(instance, {});
|
|
4721
|
-
const initialValue =
|
|
4722
|
-
|
|
4723
|
-
})
|
|
5041
|
+
KEY_TO_VALUE_ELEMENT.set(instance, {}), KEY_TO_SLATE_ELEMENT.set(instance, {}), instance.value = [createPlaceholderBlock(config.editorActor.getSnapshot().context)];
|
|
5042
|
+
const initialValue = toSlateValue(instance.value, {
|
|
5043
|
+
schemaTypes: config.editorActor.getSnapshot().context.schema
|
|
5044
|
+
});
|
|
4724
5045
|
return {
|
|
4725
5046
|
instance,
|
|
4726
5047
|
initialValue
|
|
@@ -6512,34 +6833,6 @@ function sortByPriority(items) {
|
|
|
6512
6833
|
result.includes(item) || result.push(item);
|
|
6513
6834
|
return [...result, ...itemsWithoutPriority];
|
|
6514
6835
|
}
|
|
6515
|
-
function slateChildrenToBlocks(schema2, value) {
|
|
6516
|
-
const blocks = new Array(value.length);
|
|
6517
|
-
for (let blockIndex = 0; blockIndex < value.length; blockIndex++) {
|
|
6518
|
-
const descendant = value[blockIndex];
|
|
6519
|
-
if (descendant._type !== schema2.block.name) {
|
|
6520
|
-
blocks[blockIndex] = {
|
|
6521
|
-
_key: descendant._key,
|
|
6522
|
-
_type: descendant._type,
|
|
6523
|
-
..."value" in descendant && typeof descendant.value == "object" ? descendant.value : {}
|
|
6524
|
-
};
|
|
6525
|
-
continue;
|
|
6526
|
-
}
|
|
6527
|
-
const children = "children" in descendant ? descendant.children : [], processedChildren = new Array(children.length);
|
|
6528
|
-
for (let childIndex = 0; childIndex < children.length; childIndex++) {
|
|
6529
|
-
const child = children[childIndex];
|
|
6530
|
-
processedChildren[childIndex] = child._type === schema2.span.name ? child : {
|
|
6531
|
-
_key: child._key,
|
|
6532
|
-
_type: child._type,
|
|
6533
|
-
..."value" in child && typeof child.value == "object" ? child.value : {}
|
|
6534
|
-
};
|
|
6535
|
-
}
|
|
6536
|
-
blocks[blockIndex] = {
|
|
6537
|
-
...descendant,
|
|
6538
|
-
children: processedChildren
|
|
6539
|
-
};
|
|
6540
|
-
}
|
|
6541
|
-
return blocks;
|
|
6542
|
-
}
|
|
6543
6836
|
function getActiveDecorators({
|
|
6544
6837
|
schema: schema2,
|
|
6545
6838
|
slateEditorInstance
|
|
@@ -6558,7 +6851,7 @@ function createEditorSnapshot({
|
|
|
6558
6851
|
hasTag,
|
|
6559
6852
|
internalDrag
|
|
6560
6853
|
}) {
|
|
6561
|
-
const
|
|
6854
|
+
const selection = editor.selection ? slateRangeToSelection({
|
|
6562
6855
|
schema: schema2,
|
|
6563
6856
|
editor,
|
|
6564
6857
|
range: editor.selection
|
|
@@ -6574,7 +6867,7 @@ function createEditorSnapshot({
|
|
|
6574
6867
|
readOnly,
|
|
6575
6868
|
schema: schema2,
|
|
6576
6869
|
selection,
|
|
6577
|
-
value
|
|
6870
|
+
value: editor.value
|
|
6578
6871
|
},
|
|
6579
6872
|
beta: {
|
|
6580
6873
|
hasTag,
|
|
@@ -7290,7 +7583,7 @@ function getEditorSnapshot({
|
|
|
7290
7583
|
}),
|
|
7291
7584
|
schema: editorActorSnapshot.context.schema,
|
|
7292
7585
|
selection: editorActorSnapshot.context.selection,
|
|
7293
|
-
value:
|
|
7586
|
+
value: slateEditorInstance.value
|
|
7294
7587
|
},
|
|
7295
7588
|
beta: {
|
|
7296
7589
|
hasTag: (tag) => editorActorSnapshot.hasTag(tag),
|
|
@@ -7677,8 +7970,8 @@ function createEditableAPI(editor, editorActor) {
|
|
|
7677
7970
|
}]
|
|
7678
7971
|
}], {
|
|
7679
7972
|
schemaTypes: editorActor.getSnapshot().context.schema
|
|
7680
|
-
})[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2),
|
|
7681
|
-
return
|
|
7973
|
+
})[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2), isSpanNode2 = child._type === types2.span.name, focusNode = slate.Node.get(editor, focusChildPath);
|
|
7974
|
+
return isSpanNode2 && focusNode._type !== types2.span.name && (debug$6("Inserting span child next to inline object child, moving selection + 1"), editor.move({
|
|
7682
7975
|
distance: 1,
|
|
7683
7976
|
unit: "character"
|
|
7684
7977
|
})), slate.Transforms.insertNodes(editor, child, {
|
|
@@ -8718,8 +9011,8 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
|
|
|
8718
9011
|
debug$5("Updating changed child", currentBlockChild, oldBlockChild), slate.Transforms.setNodes(slateEditor, currentBlockChild, {
|
|
8719
9012
|
at: path
|
|
8720
9013
|
});
|
|
8721
|
-
const
|
|
8722
|
-
|
|
9014
|
+
const isSpanNode2 = slate.Text.isText(currentBlockChild) && currentBlockChild._type === "span" && slate.Text.isText(oldBlockChild) && oldBlockChild._type === "span";
|
|
9015
|
+
isSpanNode2 && isTextChanged ? (oldBlockChild.text.length > 0 && slate.Transforms.delete(slateEditor, {
|
|
8723
9016
|
at: {
|
|
8724
9017
|
focus: {
|
|
8725
9018
|
path,
|
|
@@ -8732,7 +9025,7 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
|
|
|
8732
9025
|
}
|
|
8733
9026
|
}), slate.Transforms.insertText(slateEditor, currentBlockChild.text, {
|
|
8734
9027
|
at: path
|
|
8735
|
-
}), slateEditor.onChange()) :
|
|
9028
|
+
}), slateEditor.onChange()) : isSpanNode2 || (debug$5("Updating changed inline object child", currentBlockChild), slate.Transforms.setNodes(slateEditor, {
|
|
8736
9029
|
_key: VOID_CHILD_KEY
|
|
8737
9030
|
}, {
|
|
8738
9031
|
at: [...path, 0],
|
|
@@ -9102,6 +9395,7 @@ class PortableTextEditor extends React.Component {
|
|
|
9102
9395
|
/*
|
|
9103
9396
|
* The editor API (currently implemented with Slate).
|
|
9104
9397
|
*/
|
|
9398
|
+
subscriptions = [];
|
|
9105
9399
|
unsubscribers = [];
|
|
9106
9400
|
constructor(props) {
|
|
9107
9401
|
if (super(props), props.editor)
|
|
@@ -9118,23 +9412,20 @@ class PortableTextEditor extends React.Component {
|
|
|
9118
9412
|
readOnly: props.readOnly,
|
|
9119
9413
|
schema: props.schemaType
|
|
9120
9414
|
});
|
|
9121
|
-
this.
|
|
9122
|
-
const subscription = actors.relayActor.on("*", (event) => {
|
|
9123
|
-
const change = eventToChange(event);
|
|
9124
|
-
change && (props.onChange(change), this.change$.next(change));
|
|
9125
|
-
});
|
|
9126
|
-
return () => {
|
|
9127
|
-
subscription.unsubscribe();
|
|
9128
|
-
};
|
|
9129
|
-
})());
|
|
9130
|
-
for (const subscription of subscriptions)
|
|
9131
|
-
this.unsubscribers.push(subscription());
|
|
9132
|
-
this.actors = actors, this.editor = editor, this.schemaTypes = actors.editorActor.getSnapshot().context.getLegacySchema();
|
|
9415
|
+
this.subscriptions = subscriptions, this.actors = actors, this.editor = editor, this.schemaTypes = actors.editorActor.getSnapshot().context.getLegacySchema();
|
|
9133
9416
|
}
|
|
9134
9417
|
this.editable = this.editor._internal.editable;
|
|
9135
9418
|
}
|
|
9136
9419
|
componentDidMount() {
|
|
9137
|
-
|
|
9420
|
+
if (!this.actors)
|
|
9421
|
+
return;
|
|
9422
|
+
for (const subscription of this.subscriptions)
|
|
9423
|
+
this.unsubscribers.push(subscription());
|
|
9424
|
+
const relayActorSubscription = this.actors.relayActor.on("*", (event) => {
|
|
9425
|
+
const change = eventToChange(event);
|
|
9426
|
+
change && (this.props.editor || this.props.onChange(change), this.change$.next(change));
|
|
9427
|
+
});
|
|
9428
|
+
this.unsubscribers.push(relayActorSubscription.unsubscribe), this.actors.editorActor.start(), this.actors.mutationActor.start(), this.actors.relayActor.start(), this.actors.syncActor.start();
|
|
9138
9429
|
}
|
|
9139
9430
|
componentDidUpdate(prevProps) {
|
|
9140
9431
|
!this.props.editor && !prevProps.editor && this.props.schemaType !== prevProps.schemaType && console.warn("Updating schema type is no longer supported"), !this.props.editor && !prevProps.editor && (this.props.readOnly !== prevProps.readOnly && this.editor._internal.editorActor.send({
|