@portabletext/editor 2.15.1 → 2.15.2
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 +90 -22
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +88 -22
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
- package/src/editor/plugins/createWithObjectKeys.ts +61 -6
- package/src/internal-utils/applyPatch.ts +13 -3
- package/src/internal-utils/operation-to-patches.ts +49 -14
- package/src/internal-utils/text-selection.test.ts +13 -0
- package/src/internal-utils/text-selection.ts +1 -0
- package/src/operations/behavior.operation.block.set.ts +3 -7
- package/src/test/vitest/step-context.ts +2 -0
- package/src/test/vitest/step-definitions.tsx +62 -18
- package/src/test/vitest/test-editor.tsx +94 -0
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"), 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$g = require("debug"), slateDom = require("slate-dom"), util_isEmptyTextBlock = require("./_chunks-cjs/util.is-empty-text-block.cjs"), util_getTextBlockText = require("./_chunks-cjs/util.get-text-block-text.cjs"), isEqual = require("lodash/isEqual.js"), schema = require("@portabletext/schema"), selector_isAtTheStartOfBlock = require("./_chunks-cjs/selector.is-at-the-start-of-block.cjs"), selector_getSelectionText = require("./_chunks-cjs/selector.get-selection-text.cjs"), behaviors_index = require("./behaviors/index.cjs"), uniq = require("lodash/uniq.js"), xstate = require("xstate"), sanityBridge = require("@portabletext/sanity-bridge"), blockTools = require("@portabletext/block-tools"), toHtml = require("@portabletext/to-html"), schema$1 = require("@sanity/schema"), flatten = require("lodash/flatten.js"), omit = require("lodash/omit.js"), patches = require("@portabletext/patches"), util_childSelectionPointToBlockOffset = require("./_chunks-cjs/util.child-selection-point-to-block-offset.cjs"), util_sliceTextBlock = require("./_chunks-cjs/util.slice-text-block.cjs"), get = require("lodash/get.js"),
|
|
3
|
+
var reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), 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$g = require("debug"), slateDom = require("slate-dom"), util_isEmptyTextBlock = require("./_chunks-cjs/util.is-empty-text-block.cjs"), util_getTextBlockText = require("./_chunks-cjs/util.get-text-block-text.cjs"), isEqual = require("lodash/isEqual.js"), schema = require("@portabletext/schema"), selector_isAtTheStartOfBlock = require("./_chunks-cjs/selector.is-at-the-start-of-block.cjs"), selector_getSelectionText = require("./_chunks-cjs/selector.get-selection-text.cjs"), behaviors_index = require("./behaviors/index.cjs"), uniq = require("lodash/uniq.js"), xstate = require("xstate"), sanityBridge = require("@portabletext/sanity-bridge"), blockTools = require("@portabletext/block-tools"), toHtml = require("@portabletext/to-html"), schema$1 = require("@sanity/schema"), flatten = require("lodash/flatten.js"), omit = require("lodash/omit.js"), patches = require("@portabletext/patches"), util_childSelectionPointToBlockOffset = require("./_chunks-cjs/util.child-selection-point-to-block-offset.cjs"), util_sliceTextBlock = require("./_chunks-cjs/util.slice-text-block.cjs"), get = require("lodash/get.js"), immer = require("immer"), keyboardShortcuts = require("@portabletext/keyboard-shortcuts"), isPlainObject = require("lodash/isPlainObject.js"), rxjs = require("rxjs");
|
|
4
4
|
function _interopDefaultCompat(e) {
|
|
5
5
|
return e && typeof e == "object" && "default" in e ? e : { default: e };
|
|
6
6
|
}
|
|
7
|
-
var noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$g), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), omit__default = /* @__PURE__ */ _interopDefaultCompat(omit), get__default = /* @__PURE__ */ _interopDefaultCompat(get),
|
|
7
|
+
var noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$g), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), omit__default = /* @__PURE__ */ _interopDefaultCompat(omit), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject);
|
|
8
8
|
function EditorEventListener(props) {
|
|
9
9
|
const $ = reactCompilerRuntime.c(4), editor = useEditor.useEditor();
|
|
10
10
|
let t0, t1;
|
|
@@ -4246,11 +4246,9 @@ const addAnnotationOperationImplementation = ({
|
|
|
4246
4246
|
});
|
|
4247
4247
|
if (!parsedBlock)
|
|
4248
4248
|
throw new Error(`Unable to update block at ${JSON.stringify(operation.at)}`);
|
|
4249
|
-
const slateBlock =
|
|
4249
|
+
const slateBlock = toSlateBlock(parsedBlock, {
|
|
4250
4250
|
schemaTypes: context.schema
|
|
4251
|
-
})
|
|
4252
|
-
if (!slateBlock)
|
|
4253
|
-
throw new Error("Unable to convert block to Slate value");
|
|
4251
|
+
});
|
|
4254
4252
|
slate.Transforms.setNodes(operation.editor, slateBlock, {
|
|
4255
4253
|
at: [blockIndex]
|
|
4256
4254
|
});
|
|
@@ -5537,6 +5535,37 @@ function createWithObjectKeys(editorActor) {
|
|
|
5537
5535
|
apply2(operation);
|
|
5538
5536
|
}, editor.normalizeNode = (entry) => {
|
|
5539
5537
|
const [node, path] = entry;
|
|
5538
|
+
if (slate.Element.isElement(node)) {
|
|
5539
|
+
const [parent] = slate.Editor.parent(editor, path);
|
|
5540
|
+
if (parent && slate.Editor.isEditor(parent)) {
|
|
5541
|
+
const blockKeys = /* @__PURE__ */ new Set();
|
|
5542
|
+
for (const sibling of parent.children) {
|
|
5543
|
+
if (sibling._key && blockKeys.has(sibling._key)) {
|
|
5544
|
+
const _key = editorActor.getSnapshot().context.keyGenerator();
|
|
5545
|
+
blockKeys.add(_key), withNormalizeNode(editor, () => {
|
|
5546
|
+
slate.Transforms.setNodes(editor, {
|
|
5547
|
+
_key
|
|
5548
|
+
}, {
|
|
5549
|
+
at: path
|
|
5550
|
+
});
|
|
5551
|
+
});
|
|
5552
|
+
return;
|
|
5553
|
+
}
|
|
5554
|
+
if (!sibling._key) {
|
|
5555
|
+
const _key = editorActor.getSnapshot().context.keyGenerator();
|
|
5556
|
+
blockKeys.add(_key), withNormalizeNode(editor, () => {
|
|
5557
|
+
slate.Transforms.setNodes(editor, {
|
|
5558
|
+
_key
|
|
5559
|
+
}, {
|
|
5560
|
+
at: path
|
|
5561
|
+
});
|
|
5562
|
+
});
|
|
5563
|
+
return;
|
|
5564
|
+
}
|
|
5565
|
+
blockKeys.add(sibling._key);
|
|
5566
|
+
}
|
|
5567
|
+
}
|
|
5568
|
+
}
|
|
5540
5569
|
if (slate.Element.isElement(node) && node._type === editorActor.getSnapshot().context.schema.block.name) {
|
|
5541
5570
|
if (!node._key) {
|
|
5542
5571
|
withNormalizeNode(editor, () => {
|
|
@@ -5548,17 +5577,32 @@ function createWithObjectKeys(editorActor) {
|
|
|
5548
5577
|
});
|
|
5549
5578
|
return;
|
|
5550
5579
|
}
|
|
5551
|
-
|
|
5580
|
+
const childKeys = /* @__PURE__ */ new Set();
|
|
5581
|
+
for (const [child, childPath] of slate.Node.children(editor, path)) {
|
|
5582
|
+
if (child._key && childKeys.has(child._key)) {
|
|
5583
|
+
const _key = editorActor.getSnapshot().context.keyGenerator();
|
|
5584
|
+
childKeys.add(_key), withNormalizeNode(editor, () => {
|
|
5585
|
+
slate.Transforms.setNodes(editor, {
|
|
5586
|
+
_key
|
|
5587
|
+
}, {
|
|
5588
|
+
at: childPath
|
|
5589
|
+
});
|
|
5590
|
+
});
|
|
5591
|
+
return;
|
|
5592
|
+
}
|
|
5552
5593
|
if (!child._key) {
|
|
5553
|
-
|
|
5594
|
+
const _key = editorActor.getSnapshot().context.keyGenerator();
|
|
5595
|
+
childKeys.add(_key), withNormalizeNode(editor, () => {
|
|
5554
5596
|
slate.Transforms.setNodes(editor, {
|
|
5555
|
-
_key
|
|
5597
|
+
_key
|
|
5556
5598
|
}, {
|
|
5557
5599
|
at: childPath
|
|
5558
5600
|
});
|
|
5559
5601
|
});
|
|
5560
5602
|
return;
|
|
5561
5603
|
}
|
|
5604
|
+
childKeys.add(child._key);
|
|
5605
|
+
}
|
|
5562
5606
|
}
|
|
5563
5607
|
withNormalizeNode(editor, () => {
|
|
5564
5608
|
normalizeNode(entry);
|
|
@@ -5655,8 +5699,15 @@ function setPatch(editor, patch) {
|
|
|
5655
5699
|
if (!block)
|
|
5656
5700
|
return !1;
|
|
5657
5701
|
const isTextBlock = editor.isTextBlock(block.node);
|
|
5658
|
-
if (isTextBlock && patch.path
|
|
5659
|
-
|
|
5702
|
+
if (isTextBlock && patch.path[1] !== "children") {
|
|
5703
|
+
const updatedBlock = patches.applyAll(block.node, [{
|
|
5704
|
+
...patch,
|
|
5705
|
+
path: patch.path.slice(1)
|
|
5706
|
+
}]);
|
|
5707
|
+
return slate.Transforms.setNodes(editor, updatedBlock, {
|
|
5708
|
+
at: [block.index]
|
|
5709
|
+
}), !0;
|
|
5710
|
+
}
|
|
5660
5711
|
const child = findBlockChild(block, patch.path);
|
|
5661
5712
|
if (isTextBlock && child) {
|
|
5662
5713
|
if (slate.Text.isText(child.node))
|
|
@@ -5930,17 +5981,34 @@ function removeTextPatch(schema$12, children, operation, beforeValue) {
|
|
|
5930
5981
|
return patch.value ? [patch] : [];
|
|
5931
5982
|
}
|
|
5932
5983
|
function setNodePatch(schema$12, children, operation) {
|
|
5933
|
-
|
|
5934
|
-
|
|
5935
|
-
|
|
5936
|
-
|
|
5937
|
-
|
|
5938
|
-
|
|
5939
|
-
|
|
5940
|
-
},
|
|
5941
|
-
|
|
5942
|
-
|
|
5943
|
-
|
|
5984
|
+
const blockIndex = operation.path.at(0);
|
|
5985
|
+
if (blockIndex !== void 0 && operation.path.length === 1) {
|
|
5986
|
+
const block = children.at(blockIndex);
|
|
5987
|
+
if (!block)
|
|
5988
|
+
return console.error("Could not find block at index", blockIndex), [];
|
|
5989
|
+
if (schema.isTextBlock({
|
|
5990
|
+
schema: schema$12
|
|
5991
|
+
}, block)) {
|
|
5992
|
+
const patches$1 = [];
|
|
5993
|
+
for (const key of Object.keys(operation.newProperties)) {
|
|
5994
|
+
const value = operation.newProperties[key];
|
|
5995
|
+
key === "_key" ? patches$1.push(patches.set(value, [blockIndex, "_key"])) : patches$1.push(patches.set(value, [{
|
|
5996
|
+
_key: block._key
|
|
5997
|
+
}, key]));
|
|
5998
|
+
}
|
|
5999
|
+
return patches$1;
|
|
6000
|
+
} else {
|
|
6001
|
+
const patches$1 = [], _key = operation.newProperties._key;
|
|
6002
|
+
_key !== void 0 && patches$1.push(patches.set(_key, [blockIndex, "_key"]));
|
|
6003
|
+
const properties = "value" in operation.newProperties && typeof operation.newProperties.value == "object" ? operation.newProperties.value : {}, keys = Object.keys(properties);
|
|
6004
|
+
for (const key of keys) {
|
|
6005
|
+
const value = properties[key];
|
|
6006
|
+
patches$1.push(patches.set(value, [{
|
|
6007
|
+
_key: block._key
|
|
6008
|
+
}, key]));
|
|
6009
|
+
}
|
|
6010
|
+
return patches$1;
|
|
6011
|
+
}
|
|
5944
6012
|
} else if (operation.path.length === 2) {
|
|
5945
6013
|
const block = children[operation.path[0]];
|
|
5946
6014
|
if (schema.isTextBlock({
|