@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.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
  });
@@ -4990,8 +4986,24 @@ const insertChildOperationImplementation = ({
4990
4986
  _key,
4991
4987
  _type,
4992
4988
  ...rest
4993
- } = inlineObject;
4994
- Transforms.insertNodes(operation.editor, {
4989
+ } = inlineObject, [focusSpan] = getFocusSpan({
4990
+ editor: operation.editor
4991
+ });
4992
+ focusSpan ? Transforms.insertNodes(operation.editor, {
4993
+ _key,
4994
+ _type,
4995
+ children: [{
4996
+ _key: VOID_CHILD_KEY,
4997
+ _type: "span",
4998
+ text: "",
4999
+ marks: []
5000
+ }],
5001
+ value: rest,
5002
+ __inline: !0
5003
+ }, {
5004
+ at: focus,
5005
+ select: !0
5006
+ }) : Transforms.insertNodes(operation.editor, {
4995
5007
  _key,
4996
5008
  _type,
4997
5009
  children: [{
@@ -5550,6 +5562,37 @@ function createWithObjectKeys(editorActor) {
5550
5562
  apply2(operation);
5551
5563
  }, editor.normalizeNode = (entry) => {
5552
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
+ }
5553
5596
  if (Element$1.isElement(node) && node._type === editorActor.getSnapshot().context.schema.block.name) {
5554
5597
  if (!node._key) {
5555
5598
  withNormalizeNode(editor, () => {
@@ -5561,17 +5604,32 @@ function createWithObjectKeys(editorActor) {
5561
5604
  });
5562
5605
  return;
5563
5606
  }
5564
- 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
+ }
5565
5620
  if (!child._key) {
5566
- withNormalizeNode(editor, () => {
5621
+ const _key = editorActor.getSnapshot().context.keyGenerator();
5622
+ childKeys.add(_key), withNormalizeNode(editor, () => {
5567
5623
  Transforms.setNodes(editor, {
5568
- _key: editorActor.getSnapshot().context.keyGenerator()
5624
+ _key
5569
5625
  }, {
5570
5626
  at: childPath
5571
5627
  });
5572
5628
  });
5573
5629
  return;
5574
5630
  }
5631
+ childKeys.add(child._key);
5632
+ }
5575
5633
  }
5576
5634
  withNormalizeNode(editor, () => {
5577
5635
  normalizeNode(entry);
@@ -5668,8 +5726,15 @@ function setPatch(editor, patch) {
5668
5726
  if (!block)
5669
5727
  return !1;
5670
5728
  const isTextBlock2 = editor.isTextBlock(block.node);
5671
- if (isTextBlock2 && patch.path.length > 1 && patch.path[1] !== "children")
5672
- 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
+ }
5673
5738
  const child = findBlockChild(block, patch.path);
5674
5739
  if (isTextBlock2 && child) {
5675
5740
  if (Text.isText(child.node))
@@ -5943,17 +6008,34 @@ function removeTextPatch(schema, children, operation, beforeValue) {
5943
6008
  return patch.value ? [patch] : [];
5944
6009
  }
5945
6010
  function setNodePatch(schema, children, operation) {
5946
- if (operation.path.length === 1) {
5947
- const block = children[operation.path[0]];
5948
- if (typeof block._key != "string")
5949
- throw new Error("Expected block to have a _key");
5950
- const setNode = omitBy({
5951
- ...children[operation.path[0]],
5952
- ...operation.newProperties
5953
- }, isUndefined);
5954
- return [set(fromSlateValue([setNode], schema.block.name)[0], [{
5955
- _key: block._key
5956
- }])];
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
+ }
5957
6039
  } else if (operation.path.length === 2) {
5958
6040
  const block = children[operation.path[0]];
5959
6041
  if (isTextBlock({