@portabletext/editor 2.14.2 → 2.14.3

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
@@ -58,53 +58,58 @@ function keepObjectEquality(object, keyMap) {
58
58
  function toSlateValue(value, {
59
59
  schemaTypes
60
60
  }, keyMap = {}) {
61
- return value && Array.isArray(value) ? value.map((block) => {
62
- const {
63
- _type,
64
- _key,
65
- ...rest
66
- } = block;
67
- if (block && block._type === schemaTypes.block.name) {
68
- const textBlock = block;
69
- let hasInlines = !1;
70
- const hasMissingStyle = typeof textBlock.style > "u", hasMissingMarkDefs = typeof textBlock.markDefs > "u", hasMissingChildren = typeof textBlock.children > "u", children = (textBlock.children || []).map((child) => {
71
- const {
72
- _type: cType,
73
- _key: cKey,
74
- ...cRest
75
- } = child;
76
- return cType !== "span" ? (hasInlines = !0, keepObjectEquality({
77
- _type: cType,
78
- _key: cKey,
79
- children: [{
80
- _key: VOID_CHILD_KEY,
81
- _type: "span",
82
- text: "",
83
- marks: []
84
- }],
85
- value: cRest,
86
- __inline: !0
87
- }, keyMap)) : child;
88
- });
89
- return !hasMissingStyle && !hasMissingMarkDefs && !hasMissingChildren && !hasInlines && Element$1.isElement(block) ? block : (hasMissingStyle && (rest.style = schemaTypes.styles[0].name), keepObjectEquality({
90
- _type,
91
- _key,
92
- ...rest,
93
- children
94
- }, keyMap));
95
- }
96
- return keepObjectEquality({
61
+ return value && Array.isArray(value) ? value.map((block) => toSlateBlock(block, {
62
+ schemaTypes
63
+ }, keyMap)) : [];
64
+ }
65
+ function toSlateBlock(block, {
66
+ schemaTypes
67
+ }, keyMap = {}) {
68
+ const {
69
+ _type,
70
+ _key,
71
+ ...rest
72
+ } = block;
73
+ if (block && block._type === schemaTypes.block.name) {
74
+ const textBlock = block;
75
+ let hasInlines = !1;
76
+ const hasMissingStyle = typeof textBlock.style > "u", hasMissingMarkDefs = typeof textBlock.markDefs > "u", hasMissingChildren = typeof textBlock.children > "u", children = (textBlock.children || []).map((child) => {
77
+ const {
78
+ _type: cType,
79
+ _key: cKey,
80
+ ...cRest
81
+ } = child;
82
+ return cType !== "span" ? (hasInlines = !0, keepObjectEquality({
83
+ _type: cType,
84
+ _key: cKey,
85
+ children: [{
86
+ _key: VOID_CHILD_KEY,
87
+ _type: "span",
88
+ text: "",
89
+ marks: []
90
+ }],
91
+ value: cRest,
92
+ __inline: !0
93
+ }, keyMap)) : child;
94
+ });
95
+ return !hasMissingStyle && !hasMissingMarkDefs && !hasMissingChildren && !hasInlines && Element$1.isElement(block) ? block : (hasMissingStyle && (rest.style = schemaTypes.styles[0].name), keepObjectEquality({
97
96
  _type,
98
97
  _key,
99
- children: [{
100
- _key: VOID_CHILD_KEY,
101
- _type: "span",
102
- text: "",
103
- marks: []
104
- }],
105
- value: rest
106
- }, keyMap);
107
- }) : [];
98
+ ...rest,
99
+ children
100
+ }, keyMap));
101
+ }
102
+ return keepObjectEquality({
103
+ _type,
104
+ _key,
105
+ children: [{
106
+ _key: VOID_CHILD_KEY,
107
+ _type: "span",
108
+ text: "",
109
+ marks: []
110
+ }],
111
+ value: rest
112
+ }, keyMap);
108
113
  }
109
114
  function fromSlateValue(value, textBlockType, keyMap = {}) {
110
115
  return value.map((block) => {
@@ -11308,7 +11313,9 @@ function validateValue(value, types, keyGenerator) {
11308
11313
  }
11309
11314
  }, !0;
11310
11315
  }
11311
- const allUsedMarks = uniq(flatten(textBlock.children.filter((cld) => cld._type === types.span.name).map((cld) => cld.marks || [])));
11316
+ const allUsedMarks = uniq(flatten(textBlock.children.filter((child) => isSpan({
11317
+ schema: types
11318
+ }, child)).map((cld) => cld.marks || [])));
11312
11319
  if (Array.isArray(blk.markDefs) && blk.markDefs.length > 0) {
11313
11320
  const unusedMarkDefs = uniq(blk.markDefs.map((def) => def._key).filter((key) => !allUsedMarks.includes(key)));
11314
11321
  if (unusedMarkDefs.length > 0)
@@ -11733,75 +11740,69 @@ async function updateValue({
11733
11740
  });
11734
11741
  });
11735
11742
  });
11736
- }), isChanged = !0), value && value.length > 0) {
11737
- const slateValueFromProps = toSlateValue(value, {
11738
- schemaTypes: context.schema
11739
- });
11740
- streamBlocks ? await new Promise((resolve) => {
11741
- Editor.withoutNormalizing(slateEditor, () => {
11742
- withRemoteChanges(slateEditor, () => {
11743
- withoutPatching(slateEditor, () => {
11744
- if (doneSyncing) {
11745
- resolve();
11746
- return;
11747
- }
11748
- isChanged = removeExtraBlocks({
11749
- slateEditor,
11750
- slateValueFromProps
11751
- }), (async () => {
11752
- for await (const [currentBlock, currentBlockIndex] of getStreamedBlocks({
11753
- slateValue: slateValueFromProps
11754
- })) {
11755
- const {
11756
- blockChanged,
11757
- blockValid
11758
- } = syncBlock({
11759
- context,
11760
- sendBack,
11761
- block: currentBlock,
11762
- index: currentBlockIndex,
11763
- slateEditor,
11764
- value
11765
- });
11766
- if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !isValid)
11767
- break;
11768
- }
11769
- resolve();
11770
- })();
11771
- });
11772
- });
11773
- });
11774
- }) : Editor.withoutNormalizing(slateEditor, () => {
11743
+ }), isChanged = !0), value && value.length > 0 && (streamBlocks ? await new Promise((resolve) => {
11744
+ Editor.withoutNormalizing(slateEditor, () => {
11775
11745
  withRemoteChanges(slateEditor, () => {
11776
11746
  withoutPatching(slateEditor, () => {
11777
- if (doneSyncing)
11747
+ if (doneSyncing) {
11748
+ resolve();
11778
11749
  return;
11750
+ }
11779
11751
  isChanged = removeExtraBlocks({
11780
11752
  slateEditor,
11781
- slateValueFromProps
11782
- });
11783
- let index = 0;
11784
- for (const currentBlock of slateValueFromProps) {
11785
- const {
11786
- blockChanged,
11787
- blockValid
11788
- } = syncBlock({
11789
- context,
11790
- sendBack,
11791
- block: currentBlock,
11792
- index,
11793
- slateEditor,
11753
+ value
11754
+ }), (async () => {
11755
+ for await (const [currentBlock, currentBlockIndex] of getStreamedBlocks({
11794
11756
  value
11795
- });
11796
- if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !blockValid)
11797
- break;
11798
- index++;
11799
- }
11757
+ })) {
11758
+ const {
11759
+ blockChanged,
11760
+ blockValid
11761
+ } = syncBlock({
11762
+ context,
11763
+ sendBack,
11764
+ block: currentBlock,
11765
+ index: currentBlockIndex,
11766
+ slateEditor,
11767
+ value
11768
+ });
11769
+ if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !isValid)
11770
+ break;
11771
+ }
11772
+ resolve();
11773
+ })();
11800
11774
  });
11801
11775
  });
11802
11776
  });
11803
- }
11804
- if (!isValid) {
11777
+ }) : Editor.withoutNormalizing(slateEditor, () => {
11778
+ withRemoteChanges(slateEditor, () => {
11779
+ withoutPatching(slateEditor, () => {
11780
+ if (doneSyncing)
11781
+ return;
11782
+ isChanged = removeExtraBlocks({
11783
+ slateEditor,
11784
+ value
11785
+ });
11786
+ let index = 0;
11787
+ for (const currentBlock of value) {
11788
+ const {
11789
+ blockChanged,
11790
+ blockValid
11791
+ } = syncBlock({
11792
+ context,
11793
+ sendBack,
11794
+ block: currentBlock,
11795
+ index,
11796
+ slateEditor,
11797
+ value
11798
+ });
11799
+ if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !blockValid)
11800
+ break;
11801
+ index++;
11802
+ }
11803
+ });
11804
+ });
11805
+ })), !isValid) {
11805
11806
  debug$2("Invalid value, returning"), doneSyncing = !0, sendBack({
11806
11807
  type: "done syncing",
11807
11808
  value
@@ -11845,12 +11846,12 @@ async function updateValue({
11845
11846
  }
11846
11847
  function removeExtraBlocks({
11847
11848
  slateEditor,
11848
- slateValueFromProps
11849
+ value
11849
11850
  }) {
11850
11851
  let isChanged = !1;
11851
11852
  const childrenLength = slateEditor.children.length;
11852
- if (slateValueFromProps.length < childrenLength) {
11853
- for (let i = childrenLength - 1; i > slateValueFromProps.length - 1; i--)
11853
+ if (value.length < childrenLength) {
11854
+ for (let i = childrenLength - 1; i > value.length - 1; i--)
11854
11855
  Transforms.removeNodes(slateEditor, {
11855
11856
  at: [i]
11856
11857
  });
@@ -11859,10 +11860,10 @@ function removeExtraBlocks({
11859
11860
  return isChanged;
11860
11861
  }
11861
11862
  async function* getStreamedBlocks({
11862
- slateValue
11863
+ value
11863
11864
  }) {
11864
11865
  let index = 0;
11865
- for await (const block of slateValue)
11866
+ for await (const block of value)
11866
11867
  index % 10 === 0 && await new Promise((resolve) => setTimeout(resolve, 0)), yield [block, index], index++;
11867
11868
  }
11868
11869
  function syncBlock({
@@ -11880,26 +11881,38 @@ function syncBlock({
11880
11881
  withoutPatching(slateEditor, () => {
11881
11882
  if (hasChanges && blockValid) {
11882
11883
  const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
11883
- !validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !context.readOnly && context.previousValue && context.previousValue !== value && (console.warn(`${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`), validation.resolution.patches.forEach((patch) => {
11884
+ if (!validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !context.readOnly && context.previousValue && context.previousValue !== value && (console.warn(`${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`), validation.resolution.patches.forEach((patch) => {
11884
11885
  sendBack({
11885
11886
  type: "patch",
11886
11887
  patch
11887
11888
  });
11888
- })), validation.valid || validation.resolution?.autoResolve ? (oldBlock._key === currentBlock._key ? (debug$2.enabled && debug$2("Updating block", oldBlock, currentBlock), _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex)) : (debug$2.enabled && debug$2("Replacing block", oldBlock, currentBlock), _replaceBlock(slateEditor, currentBlock, currentBlockIndex)), blockChanged = !0) : (sendBack({
11889
- type: "invalid value",
11890
- resolution: validation.resolution,
11891
- value
11892
- }), blockValid = !1);
11889
+ })), validation.valid || validation.resolution?.autoResolve) {
11890
+ const slateBlock = toSlateBlock(currentBlock, {
11891
+ schemaTypes: context.schema
11892
+ });
11893
+ oldBlock._key === currentBlock._key ? (debug$2.enabled && debug$2("Updating block", oldBlock, currentBlock), _updateBlock(slateEditor, slateBlock, oldBlock, currentBlockIndex)) : (debug$2.enabled && debug$2("Replacing block", oldBlock, currentBlock), _replaceBlock(slateEditor, slateBlock, currentBlockIndex)), blockChanged = !0;
11894
+ } else
11895
+ sendBack({
11896
+ type: "invalid value",
11897
+ resolution: validation.resolution,
11898
+ value
11899
+ }), blockValid = !1;
11893
11900
  }
11894
11901
  if (!oldBlock && blockValid) {
11895
- const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
11896
- debug$2.enabled && debug$2("Validating and inserting new block in the end of the value", currentBlock), validation.valid || validation.resolution?.autoResolve ? Transforms.insertNodes(slateEditor, currentBlock, {
11897
- at: [currentBlockIndex]
11898
- }) : (debug$2("Invalid", validation), sendBack({
11899
- type: "invalid value",
11900
- resolution: validation.resolution,
11901
- value
11902
- }), blockValid = !1);
11902
+ const validation = validateValue([currentBlock], context.schema, context.keyGenerator);
11903
+ if (debug$2.enabled && debug$2("Validating and inserting new block in the end of the value", currentBlock), validation.valid || validation.resolution?.autoResolve) {
11904
+ const slateBlock = toSlateBlock(currentBlock, {
11905
+ schemaTypes: context.schema
11906
+ });
11907
+ Transforms.insertNodes(slateEditor, slateBlock, {
11908
+ at: [currentBlockIndex]
11909
+ });
11910
+ } else
11911
+ debug$2("Invalid", validation), sendBack({
11912
+ type: "invalid value",
11913
+ resolution: validation.resolution,
11914
+ value
11915
+ }), blockValid = !1;
11903
11916
  }
11904
11917
  });
11905
11918
  });