@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/_chunks-dts/behavior.types.action.d.cts +1 -1
- package/lib/index.cjs +138 -125
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +138 -125
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.ts +3 -3
- package/package.json +1 -1
- package/src/editor/sync-machine.ts +23 -29
- package/src/internal-utils/validateValue.ts +3 -3
- package/src/internal-utils/values.ts +80 -70
|
@@ -1697,7 +1697,7 @@ declare const editorMachine: xstate229.StateMachine<{
|
|
|
1697
1697
|
}, xstate229.AnyEventObject>;
|
|
1698
1698
|
}) => {
|
|
1699
1699
|
behaviors: Set<{
|
|
1700
|
-
behavior: Behavior<"*" | "split" | `custom.${string}` | "annotation.add" | "annotation.remove" | "block.set" | "block.unset" | "child.set" | "child.unset" | "decorator.add" | "decorator.remove" | "delete" | "history.redo" | "history.undo" | "insert.block" | "insert.child" | "insert.text" | "move.backward" | "move.block" | "move.forward" | "select" | "annotation.set" | "annotation.toggle" | "decorator.toggle" | "delete.backward" | "delete.block" | "delete.child" | "delete.forward" | "delete.text" | "deserialize" | "deserialize.data" | "deserialization.success" | "deserialization.failure" | "insert.blocks" | "insert.break" | "insert.inline object" | "insert.soft break" | "insert.span" | "list item.add" | "list item.remove" | "list item.toggle" | "move.block down" | "move.block up" | "select.block" | "select.previous block" | "select.next block" | "serialize" | "serialize.data" | "serialization.success" | "serialization.failure" | "style.add" | "style.remove" | "style.toggle" | "clipboard.copy" | "clipboard.cut" | "clipboard.paste" | "drag.dragstart" | "drag.drag" | "drag.dragend" | "drag.dragenter" | "drag.dragover" | "drag.dragleave" | "drag.drop" | "input.*" | "keyboard.keydown" | "keyboard.keyup" | "mouse.click" | "history.*" | "split.*" | "delete.*" | "select.*" | "deserialize.*" | "serialize.*" | "annotation.*" | "block.*" | "child.*" | "decorator.*" | "insert.*" | "move.*" | "deserialization.*" | "list item.*" | "serialization.*" | "
|
|
1700
|
+
behavior: Behavior<"*" | "split" | `custom.${string}` | "annotation.add" | "annotation.remove" | "block.set" | "block.unset" | "child.set" | "child.unset" | "decorator.add" | "decorator.remove" | "delete" | "history.redo" | "history.undo" | "insert.block" | "insert.child" | "insert.text" | "move.backward" | "move.block" | "move.forward" | "select" | "annotation.set" | "annotation.toggle" | "decorator.toggle" | "delete.backward" | "delete.block" | "delete.child" | "delete.forward" | "delete.text" | "deserialize" | "deserialize.data" | "deserialization.success" | "deserialization.failure" | "insert.blocks" | "insert.break" | "insert.inline object" | "insert.soft break" | "insert.span" | "list item.add" | "list item.remove" | "list item.toggle" | "move.block down" | "move.block up" | "select.block" | "select.previous block" | "select.next block" | "serialize" | "serialize.data" | "serialization.success" | "serialization.failure" | "style.add" | "style.remove" | "style.toggle" | "clipboard.copy" | "clipboard.cut" | "clipboard.paste" | "drag.dragstart" | "drag.drag" | "drag.dragend" | "drag.dragenter" | "drag.dragover" | "drag.dragleave" | "drag.drop" | "input.*" | "keyboard.keydown" | "keyboard.keyup" | "mouse.click" | "style.*" | "history.*" | "split.*" | "delete.*" | "select.*" | "deserialize.*" | "serialize.*" | "annotation.*" | "block.*" | "child.*" | "decorator.*" | "insert.*" | "move.*" | "deserialization.*" | "list item.*" | "serialization.*" | "clipboard.*" | "drag.*" | "keyboard.*" | "mouse.*", true, {
|
|
1701
1701
|
type: StrictExtract<"split" | "annotation.add" | "annotation.remove" | "block.set" | "block.unset" | "child.set" | "child.unset" | "decorator.add" | "decorator.remove" | "delete" | "history.redo" | "history.undo" | "insert.block" | "insert.child" | "insert.text" | "move.backward" | "move.block" | "move.forward" | "select" | "annotation.set" | "annotation.toggle" | "decorator.toggle" | "delete.backward" | "delete.block" | "delete.child" | "delete.forward" | "delete.text" | "deserialize" | "deserialize.data" | "deserialization.success" | "deserialization.failure" | "insert.blocks" | "insert.break" | "insert.inline object" | "insert.soft break" | "insert.span" | "list item.add" | "list item.remove" | "list item.toggle" | "move.block down" | "move.block up" | "select.block" | "select.previous block" | "select.next block" | "serialize" | "serialize.data" | "serialization.success" | "serialization.failure" | "style.add" | "style.remove" | "style.toggle", "annotation.add">;
|
|
1702
1702
|
annotation: {
|
|
1703
1703
|
name: string;
|
package/lib/index.cjs
CHANGED
|
@@ -29,53 +29,58 @@ function keepObjectEquality(object, keyMap) {
|
|
|
29
29
|
function toSlateValue(value, {
|
|
30
30
|
schemaTypes
|
|
31
31
|
}, keyMap = {}) {
|
|
32
|
-
return value && Array.isArray(value) ? value.map((block) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return keepObjectEquality({
|
|
32
|
+
return value && Array.isArray(value) ? value.map((block) => toSlateBlock(block, {
|
|
33
|
+
schemaTypes
|
|
34
|
+
}, keyMap)) : [];
|
|
35
|
+
}
|
|
36
|
+
function toSlateBlock(block, {
|
|
37
|
+
schemaTypes
|
|
38
|
+
}, keyMap = {}) {
|
|
39
|
+
const {
|
|
40
|
+
_type,
|
|
41
|
+
_key,
|
|
42
|
+
...rest
|
|
43
|
+
} = block;
|
|
44
|
+
if (block && block._type === schemaTypes.block.name) {
|
|
45
|
+
const textBlock = block;
|
|
46
|
+
let hasInlines = !1;
|
|
47
|
+
const hasMissingStyle = typeof textBlock.style > "u", hasMissingMarkDefs = typeof textBlock.markDefs > "u", hasMissingChildren = typeof textBlock.children > "u", children = (textBlock.children || []).map((child) => {
|
|
48
|
+
const {
|
|
49
|
+
_type: cType,
|
|
50
|
+
_key: cKey,
|
|
51
|
+
...cRest
|
|
52
|
+
} = child;
|
|
53
|
+
return cType !== "span" ? (hasInlines = !0, keepObjectEquality({
|
|
54
|
+
_type: cType,
|
|
55
|
+
_key: cKey,
|
|
56
|
+
children: [{
|
|
57
|
+
_key: VOID_CHILD_KEY,
|
|
58
|
+
_type: "span",
|
|
59
|
+
text: "",
|
|
60
|
+
marks: []
|
|
61
|
+
}],
|
|
62
|
+
value: cRest,
|
|
63
|
+
__inline: !0
|
|
64
|
+
}, keyMap)) : child;
|
|
65
|
+
});
|
|
66
|
+
return !hasMissingStyle && !hasMissingMarkDefs && !hasMissingChildren && !hasInlines && slate.Element.isElement(block) ? block : (hasMissingStyle && (rest.style = schemaTypes.styles[0].name), keepObjectEquality({
|
|
68
67
|
_type,
|
|
69
68
|
_key,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
...rest,
|
|
70
|
+
children
|
|
71
|
+
}, keyMap));
|
|
72
|
+
}
|
|
73
|
+
return keepObjectEquality({
|
|
74
|
+
_type,
|
|
75
|
+
_key,
|
|
76
|
+
children: [{
|
|
77
|
+
_key: VOID_CHILD_KEY,
|
|
78
|
+
_type: "span",
|
|
79
|
+
text: "",
|
|
80
|
+
marks: []
|
|
81
|
+
}],
|
|
82
|
+
value: rest
|
|
83
|
+
}, keyMap);
|
|
79
84
|
}
|
|
80
85
|
function fromSlateValue(value, textBlockType, keyMap = {}) {
|
|
81
86
|
return value.map((block) => {
|
|
@@ -11279,7 +11284,9 @@ function validateValue(value, types, keyGenerator) {
|
|
|
11279
11284
|
}
|
|
11280
11285
|
}, !0;
|
|
11281
11286
|
}
|
|
11282
|
-
const allUsedMarks = uniq__default.default(flatten__default.default(textBlock.children.filter((
|
|
11287
|
+
const allUsedMarks = uniq__default.default(flatten__default.default(textBlock.children.filter((child) => schema.isSpan({
|
|
11288
|
+
schema: types
|
|
11289
|
+
}, child)).map((cld) => cld.marks || [])));
|
|
11283
11290
|
if (Array.isArray(blk.markDefs) && blk.markDefs.length > 0) {
|
|
11284
11291
|
const unusedMarkDefs = uniq__default.default(blk.markDefs.map((def) => def._key).filter((key) => !allUsedMarks.includes(key)));
|
|
11285
11292
|
if (unusedMarkDefs.length > 0)
|
|
@@ -11704,75 +11711,69 @@ async function updateValue({
|
|
|
11704
11711
|
});
|
|
11705
11712
|
});
|
|
11706
11713
|
});
|
|
11707
|
-
}), isChanged = !0), value && value.length > 0) {
|
|
11708
|
-
|
|
11709
|
-
schemaTypes: context.schema
|
|
11710
|
-
});
|
|
11711
|
-
streamBlocks ? await new Promise((resolve) => {
|
|
11712
|
-
slate.Editor.withoutNormalizing(slateEditor, () => {
|
|
11713
|
-
withRemoteChanges(slateEditor, () => {
|
|
11714
|
-
withoutPatching(slateEditor, () => {
|
|
11715
|
-
if (doneSyncing) {
|
|
11716
|
-
resolve();
|
|
11717
|
-
return;
|
|
11718
|
-
}
|
|
11719
|
-
isChanged = removeExtraBlocks({
|
|
11720
|
-
slateEditor,
|
|
11721
|
-
slateValueFromProps
|
|
11722
|
-
}), (async () => {
|
|
11723
|
-
for await (const [currentBlock, currentBlockIndex] of getStreamedBlocks({
|
|
11724
|
-
slateValue: slateValueFromProps
|
|
11725
|
-
})) {
|
|
11726
|
-
const {
|
|
11727
|
-
blockChanged,
|
|
11728
|
-
blockValid
|
|
11729
|
-
} = syncBlock({
|
|
11730
|
-
context,
|
|
11731
|
-
sendBack,
|
|
11732
|
-
block: currentBlock,
|
|
11733
|
-
index: currentBlockIndex,
|
|
11734
|
-
slateEditor,
|
|
11735
|
-
value
|
|
11736
|
-
});
|
|
11737
|
-
if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !isValid)
|
|
11738
|
-
break;
|
|
11739
|
-
}
|
|
11740
|
-
resolve();
|
|
11741
|
-
})();
|
|
11742
|
-
});
|
|
11743
|
-
});
|
|
11744
|
-
});
|
|
11745
|
-
}) : slate.Editor.withoutNormalizing(slateEditor, () => {
|
|
11714
|
+
}), isChanged = !0), value && value.length > 0 && (streamBlocks ? await new Promise((resolve) => {
|
|
11715
|
+
slate.Editor.withoutNormalizing(slateEditor, () => {
|
|
11746
11716
|
withRemoteChanges(slateEditor, () => {
|
|
11747
11717
|
withoutPatching(slateEditor, () => {
|
|
11748
|
-
if (doneSyncing)
|
|
11718
|
+
if (doneSyncing) {
|
|
11719
|
+
resolve();
|
|
11749
11720
|
return;
|
|
11721
|
+
}
|
|
11750
11722
|
isChanged = removeExtraBlocks({
|
|
11751
11723
|
slateEditor,
|
|
11752
|
-
|
|
11753
|
-
})
|
|
11754
|
-
|
|
11755
|
-
for (const currentBlock of slateValueFromProps) {
|
|
11756
|
-
const {
|
|
11757
|
-
blockChanged,
|
|
11758
|
-
blockValid
|
|
11759
|
-
} = syncBlock({
|
|
11760
|
-
context,
|
|
11761
|
-
sendBack,
|
|
11762
|
-
block: currentBlock,
|
|
11763
|
-
index,
|
|
11764
|
-
slateEditor,
|
|
11724
|
+
value
|
|
11725
|
+
}), (async () => {
|
|
11726
|
+
for await (const [currentBlock, currentBlockIndex] of getStreamedBlocks({
|
|
11765
11727
|
value
|
|
11766
|
-
})
|
|
11767
|
-
|
|
11768
|
-
|
|
11769
|
-
|
|
11770
|
-
|
|
11728
|
+
})) {
|
|
11729
|
+
const {
|
|
11730
|
+
blockChanged,
|
|
11731
|
+
blockValid
|
|
11732
|
+
} = syncBlock({
|
|
11733
|
+
context,
|
|
11734
|
+
sendBack,
|
|
11735
|
+
block: currentBlock,
|
|
11736
|
+
index: currentBlockIndex,
|
|
11737
|
+
slateEditor,
|
|
11738
|
+
value
|
|
11739
|
+
});
|
|
11740
|
+
if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !isValid)
|
|
11741
|
+
break;
|
|
11742
|
+
}
|
|
11743
|
+
resolve();
|
|
11744
|
+
})();
|
|
11771
11745
|
});
|
|
11772
11746
|
});
|
|
11773
11747
|
});
|
|
11774
|
-
}
|
|
11775
|
-
|
|
11748
|
+
}) : slate.Editor.withoutNormalizing(slateEditor, () => {
|
|
11749
|
+
withRemoteChanges(slateEditor, () => {
|
|
11750
|
+
withoutPatching(slateEditor, () => {
|
|
11751
|
+
if (doneSyncing)
|
|
11752
|
+
return;
|
|
11753
|
+
isChanged = removeExtraBlocks({
|
|
11754
|
+
slateEditor,
|
|
11755
|
+
value
|
|
11756
|
+
});
|
|
11757
|
+
let index = 0;
|
|
11758
|
+
for (const currentBlock of value) {
|
|
11759
|
+
const {
|
|
11760
|
+
blockChanged,
|
|
11761
|
+
blockValid
|
|
11762
|
+
} = syncBlock({
|
|
11763
|
+
context,
|
|
11764
|
+
sendBack,
|
|
11765
|
+
block: currentBlock,
|
|
11766
|
+
index,
|
|
11767
|
+
slateEditor,
|
|
11768
|
+
value
|
|
11769
|
+
});
|
|
11770
|
+
if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !blockValid)
|
|
11771
|
+
break;
|
|
11772
|
+
index++;
|
|
11773
|
+
}
|
|
11774
|
+
});
|
|
11775
|
+
});
|
|
11776
|
+
})), !isValid) {
|
|
11776
11777
|
debug$2("Invalid value, returning"), doneSyncing = !0, sendBack({
|
|
11777
11778
|
type: "done syncing",
|
|
11778
11779
|
value
|
|
@@ -11816,12 +11817,12 @@ async function updateValue({
|
|
|
11816
11817
|
}
|
|
11817
11818
|
function removeExtraBlocks({
|
|
11818
11819
|
slateEditor,
|
|
11819
|
-
|
|
11820
|
+
value
|
|
11820
11821
|
}) {
|
|
11821
11822
|
let isChanged = !1;
|
|
11822
11823
|
const childrenLength = slateEditor.children.length;
|
|
11823
|
-
if (
|
|
11824
|
-
for (let i = childrenLength - 1; i >
|
|
11824
|
+
if (value.length < childrenLength) {
|
|
11825
|
+
for (let i = childrenLength - 1; i > value.length - 1; i--)
|
|
11825
11826
|
slate.Transforms.removeNodes(slateEditor, {
|
|
11826
11827
|
at: [i]
|
|
11827
11828
|
});
|
|
@@ -11830,10 +11831,10 @@ function removeExtraBlocks({
|
|
|
11830
11831
|
return isChanged;
|
|
11831
11832
|
}
|
|
11832
11833
|
async function* getStreamedBlocks({
|
|
11833
|
-
|
|
11834
|
+
value
|
|
11834
11835
|
}) {
|
|
11835
11836
|
let index = 0;
|
|
11836
|
-
for await (const block of
|
|
11837
|
+
for await (const block of value)
|
|
11837
11838
|
index % 10 === 0 && await new Promise((resolve) => setTimeout(resolve, 0)), yield [block, index], index++;
|
|
11838
11839
|
}
|
|
11839
11840
|
function syncBlock({
|
|
@@ -11851,26 +11852,38 @@ function syncBlock({
|
|
|
11851
11852
|
withoutPatching(slateEditor, () => {
|
|
11852
11853
|
if (hasChanges && blockValid) {
|
|
11853
11854
|
const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
|
|
11854
|
-
!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) => {
|
|
11855
|
+
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) => {
|
|
11855
11856
|
sendBack({
|
|
11856
11857
|
type: "patch",
|
|
11857
11858
|
patch
|
|
11858
11859
|
});
|
|
11859
|
-
})), validation.valid || validation.resolution?.autoResolve
|
|
11860
|
-
|
|
11861
|
-
|
|
11862
|
-
|
|
11863
|
-
|
|
11860
|
+
})), validation.valid || validation.resolution?.autoResolve) {
|
|
11861
|
+
const slateBlock = toSlateBlock(currentBlock, {
|
|
11862
|
+
schemaTypes: context.schema
|
|
11863
|
+
});
|
|
11864
|
+
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;
|
|
11865
|
+
} else
|
|
11866
|
+
sendBack({
|
|
11867
|
+
type: "invalid value",
|
|
11868
|
+
resolution: validation.resolution,
|
|
11869
|
+
value
|
|
11870
|
+
}), blockValid = !1;
|
|
11864
11871
|
}
|
|
11865
11872
|
if (!oldBlock && blockValid) {
|
|
11866
|
-
const
|
|
11867
|
-
debug$2.enabled && debug$2("Validating and inserting new block in the end of the value", currentBlock), validation.valid || validation.resolution?.autoResolve
|
|
11868
|
-
|
|
11869
|
-
|
|
11870
|
-
|
|
11871
|
-
|
|
11872
|
-
|
|
11873
|
-
|
|
11873
|
+
const validation = validateValue([currentBlock], context.schema, context.keyGenerator);
|
|
11874
|
+
if (debug$2.enabled && debug$2("Validating and inserting new block in the end of the value", currentBlock), validation.valid || validation.resolution?.autoResolve) {
|
|
11875
|
+
const slateBlock = toSlateBlock(currentBlock, {
|
|
11876
|
+
schemaTypes: context.schema
|
|
11877
|
+
});
|
|
11878
|
+
slate.Transforms.insertNodes(slateEditor, slateBlock, {
|
|
11879
|
+
at: [currentBlockIndex]
|
|
11880
|
+
});
|
|
11881
|
+
} else
|
|
11882
|
+
debug$2("Invalid", validation), sendBack({
|
|
11883
|
+
type: "invalid value",
|
|
11884
|
+
resolution: validation.resolution,
|
|
11885
|
+
value
|
|
11886
|
+
}), blockValid = !1;
|
|
11874
11887
|
}
|
|
11875
11888
|
});
|
|
11876
11889
|
});
|