@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.js CHANGED
@@ -28,8 +28,6 @@ import { applyAll, unset, insert, set, setIfMissing, diffMatchPatch as diffMatch
28
28
  import { blockOffsetsToSelection } from "./_chunks-es/util.child-selection-point-to-block-offset.js";
29
29
  import { selectionPointToBlockOffset, sliceTextBlock } from "./_chunks-es/util.slice-text-block.js";
30
30
  import get from "lodash/get.js";
31
- import isUndefined from "lodash/isUndefined.js";
32
- import omitBy from "lodash/omitBy.js";
33
31
  import { createDraft, finishDraft } from "immer";
34
32
  import { createKeyboardShortcut, code, underline, italic, bold, undo, redo } from "@portabletext/keyboard-shortcuts";
35
33
  import isPlainObject from "lodash/isPlainObject.js";
@@ -4275,11 +4273,9 @@ const addAnnotationOperationImplementation = ({
4275
4273
  });
4276
4274
  if (!parsedBlock)
4277
4275
  throw new Error(`Unable to update block at ${JSON.stringify(operation.at)}`);
4278
- const slateBlock = toSlateValue([parsedBlock], {
4276
+ const slateBlock = toSlateBlock(parsedBlock, {
4279
4277
  schemaTypes: context.schema
4280
- })?.at(0);
4281
- if (!slateBlock)
4282
- throw new Error("Unable to convert block to Slate value");
4278
+ });
4283
4279
  Transforms.setNodes(operation.editor, slateBlock, {
4284
4280
  at: [blockIndex]
4285
4281
  });
@@ -5566,6 +5562,37 @@ function createWithObjectKeys(editorActor) {
5566
5562
  apply2(operation);
5567
5563
  }, editor.normalizeNode = (entry) => {
5568
5564
  const [node, path] = entry;
5565
+ if (Element$1.isElement(node)) {
5566
+ const [parent] = Editor.parent(editor, path);
5567
+ if (parent && Editor.isEditor(parent)) {
5568
+ const blockKeys = /* @__PURE__ */ new Set();
5569
+ for (const sibling of parent.children) {
5570
+ if (sibling._key && blockKeys.has(sibling._key)) {
5571
+ const _key = editorActor.getSnapshot().context.keyGenerator();
5572
+ blockKeys.add(_key), withNormalizeNode(editor, () => {
5573
+ Transforms.setNodes(editor, {
5574
+ _key
5575
+ }, {
5576
+ at: path
5577
+ });
5578
+ });
5579
+ return;
5580
+ }
5581
+ if (!sibling._key) {
5582
+ const _key = editorActor.getSnapshot().context.keyGenerator();
5583
+ blockKeys.add(_key), withNormalizeNode(editor, () => {
5584
+ Transforms.setNodes(editor, {
5585
+ _key
5586
+ }, {
5587
+ at: path
5588
+ });
5589
+ });
5590
+ return;
5591
+ }
5592
+ blockKeys.add(sibling._key);
5593
+ }
5594
+ }
5595
+ }
5569
5596
  if (Element$1.isElement(node) && node._type === editorActor.getSnapshot().context.schema.block.name) {
5570
5597
  if (!node._key) {
5571
5598
  withNormalizeNode(editor, () => {
@@ -5577,17 +5604,32 @@ function createWithObjectKeys(editorActor) {
5577
5604
  });
5578
5605
  return;
5579
5606
  }
5580
- for (const [child, childPath] of Node.children(editor, path))
5607
+ const childKeys = /* @__PURE__ */ new Set();
5608
+ for (const [child, childPath] of Node.children(editor, path)) {
5609
+ if (child._key && childKeys.has(child._key)) {
5610
+ const _key = editorActor.getSnapshot().context.keyGenerator();
5611
+ childKeys.add(_key), withNormalizeNode(editor, () => {
5612
+ Transforms.setNodes(editor, {
5613
+ _key
5614
+ }, {
5615
+ at: childPath
5616
+ });
5617
+ });
5618
+ return;
5619
+ }
5581
5620
  if (!child._key) {
5582
- withNormalizeNode(editor, () => {
5621
+ const _key = editorActor.getSnapshot().context.keyGenerator();
5622
+ childKeys.add(_key), withNormalizeNode(editor, () => {
5583
5623
  Transforms.setNodes(editor, {
5584
- _key: editorActor.getSnapshot().context.keyGenerator()
5624
+ _key
5585
5625
  }, {
5586
5626
  at: childPath
5587
5627
  });
5588
5628
  });
5589
5629
  return;
5590
5630
  }
5631
+ childKeys.add(child._key);
5632
+ }
5591
5633
  }
5592
5634
  withNormalizeNode(editor, () => {
5593
5635
  normalizeNode(entry);
@@ -5684,8 +5726,15 @@ function setPatch(editor, patch) {
5684
5726
  if (!block)
5685
5727
  return !1;
5686
5728
  const isTextBlock2 = editor.isTextBlock(block.node);
5687
- if (isTextBlock2 && patch.path.length > 1 && patch.path[1] !== "children")
5688
- return !1;
5729
+ if (isTextBlock2 && patch.path[1] !== "children") {
5730
+ const updatedBlock = applyAll(block.node, [{
5731
+ ...patch,
5732
+ path: patch.path.slice(1)
5733
+ }]);
5734
+ return Transforms.setNodes(editor, updatedBlock, {
5735
+ at: [block.index]
5736
+ }), !0;
5737
+ }
5689
5738
  const child = findBlockChild(block, patch.path);
5690
5739
  if (isTextBlock2 && child) {
5691
5740
  if (Text.isText(child.node))
@@ -5959,17 +6008,34 @@ function removeTextPatch(schema, children, operation, beforeValue) {
5959
6008
  return patch.value ? [patch] : [];
5960
6009
  }
5961
6010
  function setNodePatch(schema, children, operation) {
5962
- if (operation.path.length === 1) {
5963
- const block = children[operation.path[0]];
5964
- if (typeof block._key != "string")
5965
- throw new Error("Expected block to have a _key");
5966
- const setNode = omitBy({
5967
- ...children[operation.path[0]],
5968
- ...operation.newProperties
5969
- }, isUndefined);
5970
- return [set(fromSlateValue([setNode], schema.block.name)[0], [{
5971
- _key: block._key
5972
- }])];
6011
+ const blockIndex = operation.path.at(0);
6012
+ if (blockIndex !== void 0 && operation.path.length === 1) {
6013
+ const block = children.at(blockIndex);
6014
+ if (!block)
6015
+ return console.error("Could not find block at index", blockIndex), [];
6016
+ if (isTextBlock({
6017
+ schema
6018
+ }, block)) {
6019
+ const patches = [];
6020
+ for (const key of Object.keys(operation.newProperties)) {
6021
+ const value = operation.newProperties[key];
6022
+ key === "_key" ? patches.push(set(value, [blockIndex, "_key"])) : patches.push(set(value, [{
6023
+ _key: block._key
6024
+ }, key]));
6025
+ }
6026
+ return patches;
6027
+ } else {
6028
+ const patches = [], _key = operation.newProperties._key;
6029
+ _key !== void 0 && patches.push(set(_key, [blockIndex, "_key"]));
6030
+ const properties = "value" in operation.newProperties && typeof operation.newProperties.value == "object" ? operation.newProperties.value : {}, keys = Object.keys(properties);
6031
+ for (const key of keys) {
6032
+ const value = properties[key];
6033
+ patches.push(set(value, [{
6034
+ _key: block._key
6035
+ }, key]));
6036
+ }
6037
+ return patches;
6038
+ }
5973
6039
  } else if (operation.path.length === 2) {
5974
6040
  const block = children[operation.path[0]];
5975
6041
  if (isTextBlock({