@portabletext/editor 1.0.2 → 1.0.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/README.md +9 -2
- package/lib/index.d.mts +22 -97
- package/lib/index.d.ts +22 -97
- package/lib/index.esm.js +42 -58
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +42 -58
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +42 -58
- package/lib/index.mjs.map +1 -1
- package/package.json +38 -20
- package/src/editor/Editable.tsx +32 -38
- package/src/editor/components/Synchronizer.tsx +1 -1
- package/src/editor/plugins/__tests__/createWithInsertData.test.tsx +166 -0
- package/src/editor/plugins/createWithInsertData.ts +34 -1
- package/src/editor/plugins/createWithPatches.ts +2 -2
- package/src/editor/plugins/createWithUndoRedo.ts +1 -1
- package/src/index.ts +1 -2
- package/src/types/editor.ts +4 -3
- package/src/utils/__tests__/dmpToOperations.test.ts +1 -1
- package/src/utils/applyPatch.ts +8 -8
- package/src/utils/operationToPatches.ts +2 -2
- package/src/utils/validateValue.ts +1 -1
- package/src/patch/PatchEvent.ts +0 -33
- package/src/patch/applyPatch.ts +0 -29
- package/src/patch/array.ts +0 -89
- package/src/patch/arrayInsert.ts +0 -27
- package/src/patch/object.ts +0 -39
- package/src/patch/patches.ts +0 -53
- package/src/patch/primitive.ts +0 -43
- package/src/patch/string.ts +0 -51
- package/src/types/patch.ts +0 -65
- package/src/utils/patches.ts +0 -36
package/lib/index.mjs
CHANGED
|
@@ -2306,16 +2306,16 @@ function apply$1(value, patch) {
|
|
|
2306
2306
|
return OPERATIONS$1[patch.type](value, patch.value);
|
|
2307
2307
|
}
|
|
2308
2308
|
const OPERATIONS = {
|
|
2309
|
-
replace(
|
|
2309
|
+
replace(_currentValue, nextValue) {
|
|
2310
2310
|
return nextValue;
|
|
2311
2311
|
},
|
|
2312
|
-
set(
|
|
2312
|
+
set(_currentValue, nextValue) {
|
|
2313
2313
|
return nextValue;
|
|
2314
2314
|
},
|
|
2315
2315
|
setIfMissing(currentValue, nextValue) {
|
|
2316
2316
|
return currentValue === void 0 ? nextValue : currentValue;
|
|
2317
2317
|
},
|
|
2318
|
-
unset(
|
|
2318
|
+
unset(_currentValue, _nextValue) {
|
|
2319
2319
|
},
|
|
2320
2320
|
diffMatchPatch(currentValue, nextValue) {
|
|
2321
2321
|
const [result] = apply$4(parse(nextValue), currentValue, {
|
|
@@ -3772,7 +3772,8 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3772
3772
|
editor,
|
|
3773
3773
|
toSlateValue(parsed, { schemaTypes }),
|
|
3774
3774
|
keyGenerator,
|
|
3775
|
-
spanTypeName
|
|
3775
|
+
spanTypeName,
|
|
3776
|
+
schemaTypes
|
|
3776
3777
|
), validation = validateValue(parsed, schemaTypes, keyGenerator);
|
|
3777
3778
|
if (!validation.valid && !((_a = validation.resolution) != null && _a.autoResolve)) {
|
|
3778
3779
|
const errorDescription = `${(_b = validation.resolution) == null ? void 0 : _b.description}`;
|
|
@@ -3853,21 +3854,32 @@ const entityMap = {
|
|
|
3853
3854
|
function escapeHtml(str) {
|
|
3854
3855
|
return String(str).replace(/[&<>"'`=/]/g, (s) => entityMap[s]);
|
|
3855
3856
|
}
|
|
3856
|
-
function _regenerateKeys(editor, fragment, keyGenerator, spanTypeName) {
|
|
3857
|
+
function _regenerateKeys(editor, fragment, keyGenerator, spanTypeName, editorTypes) {
|
|
3857
3858
|
return fragment.map((node) => {
|
|
3858
3859
|
const newNode = { ...node };
|
|
3859
|
-
editor.isTextBlock(newNode)
|
|
3860
|
-
const
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3860
|
+
if (editor.isTextBlock(newNode)) {
|
|
3861
|
+
const annotations = editorTypes.annotations.map((t) => t.name);
|
|
3862
|
+
if (annotations.length === 0) {
|
|
3863
|
+
const { markDefs, ...NewNodeNoDefs } = newNode;
|
|
3864
|
+
return { ...NewNodeNoDefs, _key: keyGenerator() };
|
|
3865
|
+
}
|
|
3866
|
+
if ((newNode.markDefs || []).some((def) => !annotations.includes(def._type))) {
|
|
3867
|
+
const allowedAnnotations = (newNode.markDefs || []).filter((def) => annotations.includes(def._type));
|
|
3868
|
+
return { ...newNode, markDefs: allowedAnnotations, _key: keyGenerator() };
|
|
3869
|
+
}
|
|
3870
|
+
newNode.markDefs = (newNode.markDefs || []).map((def) => {
|
|
3871
|
+
const oldKey = def._key, newKey = keyGenerator();
|
|
3872
|
+
return newNode.children = newNode.children.map(
|
|
3873
|
+
(child) => child._type === spanTypeName && editor.isTextSpan(child) ? {
|
|
3874
|
+
...child,
|
|
3875
|
+
marks: child.marks && child.marks.includes(oldKey) ? (
|
|
3876
|
+
// eslint-disable-next-line max-nested-callbacks
|
|
3877
|
+
[...child.marks].filter((mark) => mark !== oldKey).concat(newKey)
|
|
3878
|
+
) : child.marks
|
|
3879
|
+
} : child
|
|
3880
|
+
), { ...def, _key: newKey };
|
|
3881
|
+
});
|
|
3882
|
+
}
|
|
3871
3883
|
const nodeWithNewKeys = { ...newNode, _key: keyGenerator() };
|
|
3872
3884
|
return editor.isTextBlock(nodeWithNewKeys) && (nodeWithNewKeys.children = nodeWithNewKeys.children.map((child) => ({
|
|
3873
3885
|
...child,
|
|
@@ -4720,35 +4732,20 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4720
4732
|
[onCopy]
|
|
4721
4733
|
), handlePaste = useCallback(
|
|
4722
4734
|
(event) => {
|
|
4723
|
-
if (event.preventDefault(),
|
|
4724
|
-
|
|
4725
|
-
|
|
4726
|
-
|
|
4727
|
-
|
|
4728
|
-
new Promise((resolve) => {
|
|
4729
|
-
const value = PortableTextEditor.getValue(portableTextEditor), ptRange = toPortableTextRange(value, slateEditor.selection, schemaTypes), path = (ptRange == null ? void 0 : ptRange.focus.path) || [];
|
|
4730
|
-
resolve(
|
|
4731
|
-
onPaste({
|
|
4732
|
-
event,
|
|
4733
|
-
value,
|
|
4734
|
-
path,
|
|
4735
|
-
schemaTypes
|
|
4736
|
-
})
|
|
4737
|
-
);
|
|
4738
|
-
}).then((result) => {
|
|
4739
|
-
if (debug("Custom paste function from client resolved", result), change$.next({ type: "loading", isLoading: !0 }), !result || !result.insert) {
|
|
4740
|
-
debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData);
|
|
4741
|
-
return;
|
|
4742
|
-
}
|
|
4743
|
-
if (result && result.insert) {
|
|
4744
|
-
slateEditor.insertFragment(
|
|
4745
|
-
toSlateValue(result.insert, { schemaTypes })
|
|
4746
|
-
), change$.next({ type: "loading", isLoading: !1 });
|
|
4747
|
-
return;
|
|
4748
|
-
}
|
|
4749
|
-
console.warn("Your onPaste function returned something unexpected:", result);
|
|
4750
|
-
}).catch((error) => (change$.next({ type: "loading", isLoading: !1 }), console.error(error), error));
|
|
4735
|
+
if (event.preventDefault(), !slateEditor.selection)
|
|
4736
|
+
return;
|
|
4737
|
+
if (!onPaste) {
|
|
4738
|
+
debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
|
|
4739
|
+
return;
|
|
4751
4740
|
}
|
|
4741
|
+
const value = PortableTextEditor.getValue(portableTextEditor), ptRange = toPortableTextRange(value, slateEditor.selection, schemaTypes), path = (ptRange == null ? void 0 : ptRange.focus.path) || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
|
|
4742
|
+
onPasteResult === void 0 ? (debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData)) : (change$.next({ type: "loading", isLoading: !0 }), Promise.resolve(onPasteResult).then((result) => {
|
|
4743
|
+
debug("Custom paste function from client resolved", result), result && result.insert ? slateEditor.insertFragment(
|
|
4744
|
+
toSlateValue(result.insert, { schemaTypes })
|
|
4745
|
+
) : console.warn("Your onPaste function returned something unexpected:", result);
|
|
4746
|
+
}).catch((error) => (console.error(error), error)).finally(() => {
|
|
4747
|
+
change$.next({ type: "loading", isLoading: !1 });
|
|
4748
|
+
}));
|
|
4752
4749
|
},
|
|
4753
4750
|
[change$, onPaste, portableTextEditor, schemaTypes, slateEditor]
|
|
4754
4751
|
), handleOnFocus = useCallback(
|
|
@@ -4873,22 +4870,9 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4873
4870
|
}
|
|
4874
4871
|
) : null;
|
|
4875
4872
|
});
|
|
4876
|
-
function compactPatches(patches) {
|
|
4877
|
-
const lastPatch = patches.slice(-1)[0];
|
|
4878
|
-
if (lastPatch && lastPatch.type === "unset" && lastPatch.path.length === 0)
|
|
4879
|
-
return [lastPatch];
|
|
4880
|
-
let finalPatches = patches;
|
|
4881
|
-
return finalPatches = finalPatches.filter((patch, index) => {
|
|
4882
|
-
if (!patch)
|
|
4883
|
-
return !1;
|
|
4884
|
-
const nextPatch = finalPatches[index + 1];
|
|
4885
|
-
return !(nextPatch && nextPatch.type === "set" && patch.type === "set" && isEqual(patch.path, nextPatch.path));
|
|
4886
|
-
}), finalPatches.length !== patches.length ? finalPatches : patches;
|
|
4887
|
-
}
|
|
4888
4873
|
export {
|
|
4889
4874
|
PortableTextEditable,
|
|
4890
4875
|
PortableTextEditor,
|
|
4891
|
-
compactPatches,
|
|
4892
4876
|
defaultKeyGenerator as keyGenerator,
|
|
4893
4877
|
usePortableTextEditor,
|
|
4894
4878
|
usePortableTextEditorSelection
|