@portabletext/editor 1.0.9 → 1.0.11
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.d.mts +17 -5
- package/lib/index.d.ts +17 -5
- package/lib/index.esm.js +25 -12
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +25 -12
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +25 -12
- package/lib/index.mjs.map +1 -1
- package/package.json +19 -20
- package/src/editor/PortableTextEditor.tsx +1 -6
- package/src/editor/__tests__/insert-block.test.tsx +229 -0
- package/src/editor/hooks/usePortableTextEditor.ts +1 -0
- package/src/editor/hooks/usePortableTextEditorKeyGenerator.ts +3 -0
- package/src/editor/hooks/usePortableTextEditorSelection.tsx +1 -0
- package/src/editor/plugins/createWithEditableAPI.ts +41 -10
- package/src/editor/plugins/createWithHotKeys.ts +5 -1
- package/src/types/editor.ts +7 -0
- package/src/types/options.ts +6 -0
- package/src/utils/bufferUntil.ts +0 -15
package/lib/index.js
CHANGED
|
@@ -1004,9 +1004,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1004
1004
|
)) == null ? void 0 : _a.focus.path) || [];
|
|
1005
1005
|
},
|
|
1006
1006
|
insertBlock: (type, value) => {
|
|
1007
|
-
var _a;
|
|
1008
|
-
if (!editor.selection)
|
|
1009
|
-
throw new Error("The editor has no selection");
|
|
1007
|
+
var _a, _b, _c;
|
|
1010
1008
|
const block = toSlateValue(
|
|
1011
1009
|
[
|
|
1012
1010
|
{
|
|
@@ -1016,17 +1014,32 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1016
1014
|
}
|
|
1017
1015
|
],
|
|
1018
1016
|
portableTextEditor
|
|
1019
|
-
)[0]
|
|
1017
|
+
)[0];
|
|
1018
|
+
if (!editor.selection) {
|
|
1019
|
+
const lastBlock = Array.from(
|
|
1020
|
+
slate.Editor.nodes(editor, {
|
|
1021
|
+
match: (n) => !slate.Editor.isEditor(n),
|
|
1022
|
+
at: [],
|
|
1023
|
+
reverse: !0
|
|
1024
|
+
})
|
|
1025
|
+
)[0];
|
|
1026
|
+
return slate.Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: lastBlock[1] }), editor.onChange(), (_b = (_a = toPortableTextRange(
|
|
1027
|
+
fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1028
|
+
editor.selection,
|
|
1029
|
+
types$1
|
|
1030
|
+
)) == null ? void 0 : _a.focus.path) != null ? _b : [];
|
|
1031
|
+
}
|
|
1032
|
+
const focusBlock = Array.from(
|
|
1020
1033
|
slate.Editor.nodes(editor, {
|
|
1021
1034
|
at: editor.selection.focus.path.slice(0, 1),
|
|
1022
1035
|
match: (n) => n._type === types$1.block.name
|
|
1023
1036
|
})
|
|
1024
|
-
)[0]
|
|
1025
|
-
return focusBlock && isEqualToEmptyEditor([focusBlock], types$1) && slate.Transforms.removeNodes(editor, { at:
|
|
1037
|
+
)[0];
|
|
1038
|
+
return slate.Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: focusBlock[1] }), editor.onChange(), ((_c = toPortableTextRange(
|
|
1026
1039
|
fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1027
1040
|
editor.selection,
|
|
1028
1041
|
types$1
|
|
1029
|
-
)) == null ? void 0 :
|
|
1042
|
+
)) == null ? void 0 : _c.focus.path) || [];
|
|
1030
1043
|
},
|
|
1031
1044
|
hasBlockStyle: (style) => {
|
|
1032
1045
|
try {
|
|
@@ -1111,7 +1124,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1111
1124
|
match: (node) => slate.Text.isText(node)
|
|
1112
1125
|
})
|
|
1113
1126
|
];
|
|
1114
|
-
if (spans.some(
|
|
1127
|
+
if (spans.length === 0 || spans.some(
|
|
1115
1128
|
([span]) => {
|
|
1116
1129
|
var _a;
|
|
1117
1130
|
return !types.isPortableTextSpan(span) || !span.marks || ((_a = span.marks) == null ? void 0 : _a.length) === 0;
|
|
@@ -3031,7 +3044,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3031
3044
|
const reservedHotkeys = ["enter", "tab", "shift", "delete", "end"], activeHotkeys = hotkeysFromOptions || DEFAULT_HOTKEYS;
|
|
3032
3045
|
return function(editor) {
|
|
3033
3046
|
return editor.pteWithHotKeys = (event) => {
|
|
3034
|
-
var _a, _b, _c, _d;
|
|
3047
|
+
var _a, _b, _c, _d, _e, _f;
|
|
3035
3048
|
Object.keys(activeHotkeys).forEach((cat) => {
|
|
3036
3049
|
if (cat === "marks")
|
|
3037
3050
|
for (const hotkey in activeHotkeys[cat]) {
|
|
@@ -3104,8 +3117,8 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3104
3117
|
const nextBlock = slate.Node.descendant(
|
|
3105
3118
|
editor,
|
|
3106
3119
|
slate.Path.next(editor.selection.focus.path.slice(0, 1))
|
|
3107
|
-
), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
|
|
3108
|
-
if (nextBlock && focusBlock && !slate.Editor.isVoid(editor, focusBlock) && slate.Editor.isVoid(editor, nextBlock)) {
|
|
3120
|
+
), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath), isEmptyFocusBlock = types.isPortableTextTextBlock(focusBlock) && focusBlock.children.length === 1 && ((_f = (_e = focusBlock.children) == null ? void 0 : _e[0]) == null ? void 0 : _f.text) === "";
|
|
3121
|
+
if (nextBlock && focusBlock && !slate.Editor.isVoid(editor, focusBlock) && slate.Editor.isVoid(editor, nextBlock) && isEmptyFocusBlock) {
|
|
3109
3122
|
debug$8("Preventing deleting void block below"), event.preventDefault(), event.stopPropagation(), slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), slate.Transforms.select(editor, focusBlockPath), editor.onChange();
|
|
3110
3123
|
return;
|
|
3111
3124
|
}
|
|
@@ -3984,7 +3997,7 @@ class PortableTextEditor extends react.Component {
|
|
|
3984
3997
|
if (this.editable)
|
|
3985
3998
|
return this.editable.getValue();
|
|
3986
3999
|
}), !props.schemaType)
|
|
3987
|
-
throw new Error('PortableTextEditor: missing "
|
|
4000
|
+
throw new Error('PortableTextEditor: missing "schemaType" property');
|
|
3988
4001
|
props.incomingPatches$ && console.warn("The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"), this.change$.next({ type: "loading", isLoading: !0 }), this.schemaTypes = getPortableTextMemberSchemaTypes(
|
|
3989
4002
|
props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
|
|
3990
4003
|
);
|