@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
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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((
|
|
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
|
-
|
|
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
|
-
|
|
11782
|
-
})
|
|
11783
|
-
|
|
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
|
-
|
|
11797
|
-
|
|
11798
|
-
|
|
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
|
-
|
|
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
|
-
|
|
11849
|
+
value
|
|
11849
11850
|
}) {
|
|
11850
11851
|
let isChanged = !1;
|
|
11851
11852
|
const childrenLength = slateEditor.children.length;
|
|
11852
|
-
if (
|
|
11853
|
-
for (let i = childrenLength - 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
|
-
|
|
11863
|
+
value
|
|
11863
11864
|
}) {
|
|
11864
11865
|
let index = 0;
|
|
11865
|
-
for await (const block of
|
|
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
|
|
11889
|
-
|
|
11890
|
-
|
|
11891
|
-
|
|
11892
|
-
|
|
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
|
|
11896
|
-
debug$2.enabled && debug$2("Validating and inserting new block in the end of the value", currentBlock), validation.valid || validation.resolution?.autoResolve
|
|
11897
|
-
|
|
11898
|
-
|
|
11899
|
-
|
|
11900
|
-
|
|
11901
|
-
|
|
11902
|
-
|
|
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
|
});
|