@portabletext/editor 2.15.0 → 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 +108 -24
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +106 -24
- package/lib/index.js.map +1 -1
- package/lib/utils/index.d.cts +2 -2
- package/package.json +9 -9
- 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/operations/behavior.operation.insert.child.ts +47 -21
- 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
|
});
|
|
@@ -4961,8 +4959,24 @@ const insertChildOperationImplementation = ({
|
|
|
4961
4959
|
_key,
|
|
4962
4960
|
_type,
|
|
4963
4961
|
...rest
|
|
4964
|
-
} = inlineObject
|
|
4965
|
-
|
|
4962
|
+
} = inlineObject, [focusSpan] = getFocusSpan({
|
|
4963
|
+
editor: operation.editor
|
|
4964
|
+
});
|
|
4965
|
+
focusSpan ? slate.Transforms.insertNodes(operation.editor, {
|
|
4966
|
+
_key,
|
|
4967
|
+
_type,
|
|
4968
|
+
children: [{
|
|
4969
|
+
_key: VOID_CHILD_KEY,
|
|
4970
|
+
_type: "span",
|
|
4971
|
+
text: "",
|
|
4972
|
+
marks: []
|
|
4973
|
+
}],
|
|
4974
|
+
value: rest,
|
|
4975
|
+
__inline: !0
|
|
4976
|
+
}, {
|
|
4977
|
+
at: focus,
|
|
4978
|
+
select: !0
|
|
4979
|
+
}) : slate.Transforms.insertNodes(operation.editor, {
|
|
4966
4980
|
_key,
|
|
4967
4981
|
_type,
|
|
4968
4982
|
children: [{
|
|
@@ -5521,6 +5535,37 @@ function createWithObjectKeys(editorActor) {
|
|
|
5521
5535
|
apply2(operation);
|
|
5522
5536
|
}, editor.normalizeNode = (entry) => {
|
|
5523
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
|
+
}
|
|
5524
5569
|
if (slate.Element.isElement(node) && node._type === editorActor.getSnapshot().context.schema.block.name) {
|
|
5525
5570
|
if (!node._key) {
|
|
5526
5571
|
withNormalizeNode(editor, () => {
|
|
@@ -5532,17 +5577,32 @@ function createWithObjectKeys(editorActor) {
|
|
|
5532
5577
|
});
|
|
5533
5578
|
return;
|
|
5534
5579
|
}
|
|
5535
|
-
|
|
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
|
+
}
|
|
5536
5593
|
if (!child._key) {
|
|
5537
|
-
|
|
5594
|
+
const _key = editorActor.getSnapshot().context.keyGenerator();
|
|
5595
|
+
childKeys.add(_key), withNormalizeNode(editor, () => {
|
|
5538
5596
|
slate.Transforms.setNodes(editor, {
|
|
5539
|
-
_key
|
|
5597
|
+
_key
|
|
5540
5598
|
}, {
|
|
5541
5599
|
at: childPath
|
|
5542
5600
|
});
|
|
5543
5601
|
});
|
|
5544
5602
|
return;
|
|
5545
5603
|
}
|
|
5604
|
+
childKeys.add(child._key);
|
|
5605
|
+
}
|
|
5546
5606
|
}
|
|
5547
5607
|
withNormalizeNode(editor, () => {
|
|
5548
5608
|
normalizeNode(entry);
|
|
@@ -5639,8 +5699,15 @@ function setPatch(editor, patch) {
|
|
|
5639
5699
|
if (!block)
|
|
5640
5700
|
return !1;
|
|
5641
5701
|
const isTextBlock = editor.isTextBlock(block.node);
|
|
5642
|
-
if (isTextBlock && patch.path
|
|
5643
|
-
|
|
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
|
+
}
|
|
5644
5711
|
const child = findBlockChild(block, patch.path);
|
|
5645
5712
|
if (isTextBlock && child) {
|
|
5646
5713
|
if (slate.Text.isText(child.node))
|
|
@@ -5914,17 +5981,34 @@ function removeTextPatch(schema$12, children, operation, beforeValue) {
|
|
|
5914
5981
|
return patch.value ? [patch] : [];
|
|
5915
5982
|
}
|
|
5916
5983
|
function setNodePatch(schema$12, children, operation) {
|
|
5917
|
-
|
|
5918
|
-
|
|
5919
|
-
|
|
5920
|
-
|
|
5921
|
-
|
|
5922
|
-
|
|
5923
|
-
|
|
5924
|
-
},
|
|
5925
|
-
|
|
5926
|
-
|
|
5927
|
-
|
|
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
|
+
}
|
|
5928
6012
|
} else if (operation.path.length === 2) {
|
|
5929
6013
|
const block = children[operation.path[0]];
|
|
5930
6014
|
if (schema.isTextBlock({
|