@portabletext/editor 2.21.2 → 2.21.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/index.js CHANGED
@@ -24,7 +24,7 @@ import { toHTML } from "@portabletext/to-html";
24
24
  import { Schema } from "@sanity/schema";
25
25
  import flatten from "lodash/flatten.js";
26
26
  import omit from "lodash/omit.js";
27
- import { applyAll, unset, insert, set, setIfMissing, diffMatchPatch as diffMatchPatch$1 } from "@portabletext/patches";
27
+ import { applyAll, unset, insert, setIfMissing, set, diffMatchPatch as diffMatchPatch$1 } from "@portabletext/patches";
28
28
  import { createDraft, finishDraft } from "immer";
29
29
  import { createKeyboardShortcut, code, underline, italic, bold, undo, redo } from "@portabletext/keyboard-shortcuts";
30
30
  import { sliceTextBlock } from "./_chunks-es/util.slice-text-block.js";
@@ -5661,6 +5661,32 @@ function setPatch(editor, patch) {
5661
5661
  if (!block)
5662
5662
  return !1;
5663
5663
  const isTextBlock2 = editor.isTextBlock(block.node);
5664
+ if (patch.path.length === 1) {
5665
+ const updatedBlock = applyAll(block.node, [{
5666
+ ...patch,
5667
+ path: patch.path.slice(1)
5668
+ }]);
5669
+ if (editor.isTextBlock(block.node) && Element$1.isElement(updatedBlock)) {
5670
+ Transforms.setNodes(editor, updatedBlock, {
5671
+ at: [block.index]
5672
+ });
5673
+ const previousSelection = editor.selection;
5674
+ for (const [_, childPath] of Editor.nodes(editor, {
5675
+ at: [block.index],
5676
+ reverse: !0,
5677
+ mode: "lowest"
5678
+ }))
5679
+ Transforms.removeNodes(editor, {
5680
+ at: childPath
5681
+ });
5682
+ return Transforms.insertNodes(editor, updatedBlock.children, {
5683
+ at: [block.index, 0]
5684
+ }), previousSelection && (Transforms.setSelection(editor, previousSelection), Transforms.select(editor, previousSelection)), !0;
5685
+ } else
5686
+ return Transforms.setNodes(editor, updatedBlock, {
5687
+ at: [block.index]
5688
+ }), !0;
5689
+ }
5664
5690
  if (isTextBlock2 && patch.path[1] !== "children") {
5665
5691
  const updatedBlock = applyAll(block.node, [{
5666
5692
  ...patch,
@@ -6048,12 +6074,14 @@ function insertNodePatch(schema, children, operation, beforeValue) {
6048
6074
  ...operation.node
6049
6075
  };
6050
6076
  !node._type && Text.isText(node) && (node._type = "span", node.marks = []);
6051
- const child = fromSlateValue([{
6077
+ const setIfMissingPatch = setIfMissing([], [{
6078
+ _key: block._key
6079
+ }, "children"]), child = fromSlateValue([{
6052
6080
  _key: "bogus",
6053
6081
  _type: schema.block.name,
6054
6082
  children: [node]
6055
6083
  }], schema.block.name)[0].children[0];
6056
- return [insert([child], position, [{
6084
+ return [setIfMissingPatch, insert([child], position, [{
6057
6085
  _key: block._key
6058
6086
  }, "children", block.children.length <= 1 || !block.children[operation.path[1] - 1] ? 0 : {
6059
6087
  _key: block.children[operation.path[1] - 1]._key
@@ -6095,7 +6123,9 @@ function splitNodePatch(schema, children, operation, beforeValue) {
6095
6123
  ...splitBlock,
6096
6124
  children: splitBlock.children.slice(operation.path[1] + 1, operation.path[1] + 2)
6097
6125
  }], schema.block.name)[0].children;
6098
- patches.push(insert(targetSpans, "after", [{
6126
+ patches.push(setIfMissing([], [{
6127
+ _key: splitBlock._key
6128
+ }, "children"])), patches.push(insert(targetSpans, "after", [{
6099
6129
  _key: splitBlock._key
6100
6130
  }, "children", {
6101
6131
  _key: splitSpan._key
@@ -6184,7 +6214,9 @@ function moveNodePatch(schema, beforeValue, operation) {
6184
6214
  _key: block._key
6185
6215
  }, "children", {
6186
6216
  _key: child._key
6187
- }])), patches.push(insert([childToInsert], position, [{
6217
+ }])), patches.push(setIfMissing([], [{
6218
+ _key: targetBlock._key
6219
+ }, "children"])), patches.push(insert([childToInsert], position, [{
6188
6220
  _key: targetBlock._key
6189
6221
  }, "children", {
6190
6222
  _key: targetChild._key
@@ -12018,7 +12050,7 @@ function syncBlock({
12018
12050
  type: "patch",
12019
12051
  patch
12020
12052
  });
12021
- })), validation.valid || validation.resolution?.autoResolve ? (oldBlock._key === block._key ? (debug$2.enabled && debug$2("Updating block", oldBlock, block), Editor.withoutNormalizing(slateEditor, () => {
12053
+ })), validation.valid || validation.resolution?.autoResolve ? (oldBlock._key === block._key && oldBlock._type === block._type ? (debug$2.enabled && debug$2("Updating block", oldBlock, block), Editor.withoutNormalizing(slateEditor, () => {
12022
12054
  withRemoteChanges(slateEditor, () => {
12023
12055
  withoutPatching(slateEditor, () => {
12024
12056
  updateBlock({
@@ -12090,7 +12122,7 @@ function updateBlock({
12090
12122
  }), slateBlock.children.forEach((currentBlockChild, currentBlockChildIndex) => {
12091
12123
  const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual(currentBlockChild, oldBlockChild), isTextChanged = !isEqual(currentBlockChild.text, oldBlockChild?.text), path = [index, currentBlockChildIndex];
12092
12124
  if (isChildChanged)
12093
- if (currentBlockChild._key === oldBlockChild?._key) {
12125
+ if (currentBlockChild._key === oldBlockChild?._key && currentBlockChild._type === oldBlockChild?._type) {
12094
12126
  debug$2("Updating changed child", currentBlockChild, oldBlockChild), Transforms.setNodes(slateEditor, currentBlockChild, {
12095
12127
  at: path
12096
12128
  });