@portabletext/editor 1.0.11 → 1.0.12
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.esm.js +187 -199
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +183 -195
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +187 -199
- package/lib/index.mjs.map +1 -1
- package/package.json +19 -17
- package/src/editor/__tests__/PortableTextEditor.test.tsx +15 -1
- package/src/editor/__tests__/handleClick.test.tsx +40 -20
- package/src/editor/__tests__/utils.ts +14 -15
- package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +62 -20
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +19 -0
- package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +259 -134
- package/src/editor/plugins/__tests__/withHotkeys.test.tsx +1 -1
- package/src/editor/plugins/__tests__/withInsertBreak.test.tsx +18 -2
- package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +176 -154
- package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +17 -0
- package/src/editor/plugins/createWithObjectKeys.ts +23 -8
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +51 -1
- package/src/editor/plugins/createWithUndoRedo.ts +5 -7
- package/src/utils/__tests__/values.test.ts +1 -0
- package/src/utils/withPreserveKeys.ts +7 -0
package/lib/index.esm.js
CHANGED
|
@@ -5,7 +5,7 @@ import { useRef, useState, useMemo, useEffect, useCallback, createContext, useCo
|
|
|
5
5
|
import { Editor, Element as Element$1, Range, Point, Text, Path, Transforms, Node, Operation, createEditor } from "slate";
|
|
6
6
|
import { useSlateStatic, ReactEditor, useSelected, withReact, Slate, useSlate, Editable } from "slate-react";
|
|
7
7
|
import debug$m from "debug";
|
|
8
|
-
import { isKeySegment, isPortableTextSpan, isPortableTextTextBlock, isPortableTextListBlock } from "@sanity/types";
|
|
8
|
+
import { isKeySegment, isPortableTextSpan as isPortableTextSpan$1, isPortableTextTextBlock, isPortableTextListBlock } from "@sanity/types";
|
|
9
9
|
import { styled } from "styled-components";
|
|
10
10
|
import uniq from "lodash/uniq.js";
|
|
11
11
|
import { Subject } from "rxjs";
|
|
@@ -185,8 +185,7 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
|
|
|
185
185
|
});
|
|
186
186
|
}
|
|
187
187
|
function isEqualToEmptyEditor(children, schemaTypes) {
|
|
188
|
-
|
|
189
|
-
return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && Element$1.isElement(children[0]) && children[0]._type === schemaTypes.block.name && "style" in children[0] && children[0].style === schemaTypes.styles[0].value && !("listItem" in children[0]) && Array.isArray(children[0].children) && children[0].children.length === 1 && Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !((_a = children[0].children[0].marks) != null && _a.join("")) && children[0].children[0].text === "";
|
|
188
|
+
return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && Element$1.isElement(children[0]) && children[0]._type === schemaTypes.block.name && "style" in children[0] && children[0].style === schemaTypes.styles[0].value && !("listItem" in children[0]) && Array.isArray(children[0].children) && children[0].children.length === 1 && Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
|
|
190
189
|
}
|
|
191
190
|
const IS_PROCESSING_REMOTE_CHANGES = /* @__PURE__ */ new WeakMap(), IS_PROCESSING_LOCAL_CHANGES = /* @__PURE__ */ new WeakMap(), IS_DRAGGING = /* @__PURE__ */ new WeakMap(), IS_DRAGGING_BLOCK_ELEMENT = /* @__PURE__ */ new WeakMap(), IS_DRAGGING_ELEMENT_TARGET = /* @__PURE__ */ new WeakMap(), IS_DRAGGING_BLOCK_TARGET_POSITION = /* @__PURE__ */ new WeakMap(), KEY_TO_SLATE_ELEMENT = /* @__PURE__ */ new WeakMap(), KEY_TO_VALUE_ELEMENT = /* @__PURE__ */ new WeakMap(), SLATE_TO_PORTABLE_TEXT_RANGE = /* @__PURE__ */ new WeakMap(), DefaultObject = (props) => /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx("pre", { children: JSON.stringify(props.value, null, 2) }) }), DefaultBlockObject = styled.div`
|
|
192
191
|
user-select: none;
|
|
@@ -655,13 +654,12 @@ function DefaultAnnotation(props) {
|
|
|
655
654
|
return /* @__PURE__ */ jsx("span", { style: { color: "blue" }, onClick: handleClick, children: props.children });
|
|
656
655
|
}
|
|
657
656
|
function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
658
|
-
var _a, _b, _c;
|
|
659
657
|
if (!portableTextType)
|
|
660
658
|
throw new Error("Parameter 'portabletextType' missing (required)");
|
|
661
|
-
const blockType =
|
|
659
|
+
const blockType = portableTextType.of?.find(findBlockType);
|
|
662
660
|
if (!blockType)
|
|
663
661
|
throw new Error("Block type is not defined in this schema (required)");
|
|
664
|
-
const childrenField =
|
|
662
|
+
const childrenField = blockType.fields?.find((field) => field.name === "children");
|
|
665
663
|
if (!childrenField)
|
|
666
664
|
throw new Error("Children field for block type found in schema (required)");
|
|
667
665
|
const ofType = childrenField.type.of;
|
|
@@ -670,7 +668,7 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
|
670
668
|
const spanType = ofType.find((memberType) => memberType.name === "span");
|
|
671
669
|
if (!spanType)
|
|
672
670
|
throw new Error("Span type not found in schema (required)");
|
|
673
|
-
const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes =
|
|
671
|
+
const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
|
|
674
672
|
return {
|
|
675
673
|
styles: resolveEnabledStyles(blockType),
|
|
676
674
|
decorators: resolveEnabledDecorators(spanType),
|
|
@@ -684,11 +682,10 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
|
684
682
|
};
|
|
685
683
|
}
|
|
686
684
|
function resolveEnabledStyles(blockType) {
|
|
687
|
-
|
|
688
|
-
const styleField = (_a = blockType.fields) == null ? void 0 : _a.find((btField) => btField.name === "style");
|
|
685
|
+
const styleField = blockType.fields?.find((btField) => btField.name === "style");
|
|
689
686
|
if (!styleField)
|
|
690
687
|
throw new Error("A field with name 'style' is not defined in the block type (required).");
|
|
691
|
-
const textStyles =
|
|
688
|
+
const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
|
|
692
689
|
if (!textStyles || textStyles.length === 0)
|
|
693
690
|
throw new Error(
|
|
694
691
|
"The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}."
|
|
@@ -699,11 +696,10 @@ function resolveEnabledDecorators(spanType) {
|
|
|
699
696
|
return spanType.decorators;
|
|
700
697
|
}
|
|
701
698
|
function resolveEnabledListItems(blockType) {
|
|
702
|
-
|
|
703
|
-
const listField = (_a = blockType.fields) == null ? void 0 : _a.find((btField) => btField.name === "listItem");
|
|
699
|
+
const listField = blockType.fields?.find((btField) => btField.name === "listItem");
|
|
704
700
|
if (!listField)
|
|
705
701
|
throw new Error("A field with name 'listItem' is not defined in the block type (required).");
|
|
706
|
-
const listItems =
|
|
702
|
+
const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
|
|
707
703
|
if (!listItems)
|
|
708
704
|
throw new Error("The list field need at least to be an empty array");
|
|
709
705
|
return listItems;
|
|
@@ -781,7 +777,7 @@ function createOperationToPatches(types) {
|
|
|
781
777
|
function insertNodePatch(editor, operation, beforeValue) {
|
|
782
778
|
const block = beforeValue[operation.path[0]], isTextBlock = editor.isTextBlock(block);
|
|
783
779
|
if (operation.path.length === 1) {
|
|
784
|
-
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block
|
|
780
|
+
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
|
|
785
781
|
return targetKey ? [
|
|
786
782
|
insert([fromSlateValue([operation.node], textBlockName)[0]], position, [
|
|
787
783
|
{ _key: targetKey }
|
|
@@ -883,7 +879,7 @@ function createOperationToPatches(types) {
|
|
|
883
879
|
function mergeNodePatch(editor, operation, beforeValue) {
|
|
884
880
|
const patches = [], block = beforeValue[operation.path[0]], targetBlock = editor.children[operation.path[0]];
|
|
885
881
|
if (operation.path.length === 1)
|
|
886
|
-
if (block
|
|
882
|
+
if (block?._key) {
|
|
887
883
|
const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
|
|
888
884
|
patches.push(set(newBlock, [{ _key: newBlock._key }])), patches.push(unset([{ _key: block._key }]));
|
|
889
885
|
} else
|
|
@@ -981,7 +977,6 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
981
977
|
}
|
|
982
978
|
},
|
|
983
979
|
insertChild: (type, value) => {
|
|
984
|
-
var _a;
|
|
985
980
|
if (!editor.selection)
|
|
986
981
|
throw new Error("The editor has no selection");
|
|
987
982
|
const [focusBlock] = Array.from(
|
|
@@ -1013,14 +1008,13 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1013
1008
|
return isSpanNode && focusNode._type !== types.span.name && (debug$j("Inserting span child next to inline object child, moving selection + 1"), editor.move({ distance: 1, unit: "character" })), Transforms.insertNodes(editor, child, {
|
|
1014
1009
|
select: !0,
|
|
1015
1010
|
at: editor.selection
|
|
1016
|
-
}), editor.onChange(),
|
|
1011
|
+
}), editor.onChange(), toPortableTextRange(
|
|
1017
1012
|
fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1018
1013
|
editor.selection,
|
|
1019
1014
|
types
|
|
1020
|
-
)
|
|
1015
|
+
)?.focus.path || [];
|
|
1021
1016
|
},
|
|
1022
1017
|
insertBlock: (type, value) => {
|
|
1023
|
-
var _a, _b, _c;
|
|
1024
1018
|
const block = toSlateValue(
|
|
1025
1019
|
[
|
|
1026
1020
|
{
|
|
@@ -1039,11 +1033,11 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1039
1033
|
reverse: !0
|
|
1040
1034
|
})
|
|
1041
1035
|
)[0];
|
|
1042
|
-
return Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types) && Transforms.removeNodes(editor, { at: lastBlock[1] }), editor.onChange(),
|
|
1036
|
+
return Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types) && Transforms.removeNodes(editor, { at: lastBlock[1] }), editor.onChange(), toPortableTextRange(
|
|
1043
1037
|
fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1044
1038
|
editor.selection,
|
|
1045
1039
|
types
|
|
1046
|
-
)
|
|
1040
|
+
)?.focus.path ?? [];
|
|
1047
1041
|
}
|
|
1048
1042
|
const focusBlock = Array.from(
|
|
1049
1043
|
Editor.nodes(editor, {
|
|
@@ -1051,11 +1045,11 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1051
1045
|
match: (n) => n._type === types.block.name
|
|
1052
1046
|
})
|
|
1053
1047
|
)[0];
|
|
1054
|
-
return Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types) && Transforms.removeNodes(editor, { at: focusBlock[1] }), editor.onChange(),
|
|
1048
|
+
return Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types) && Transforms.removeNodes(editor, { at: focusBlock[1] }), editor.onChange(), toPortableTextRange(
|
|
1055
1049
|
fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1056
1050
|
editor.selection,
|
|
1057
1051
|
types
|
|
1058
|
-
)
|
|
1052
|
+
)?.focus.path || [];
|
|
1059
1053
|
},
|
|
1060
1054
|
hasBlockStyle: (style) => {
|
|
1061
1055
|
try {
|
|
@@ -1111,7 +1105,6 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1111
1105
|
return node;
|
|
1112
1106
|
},
|
|
1113
1107
|
activeAnnotations: () => {
|
|
1114
|
-
var _a;
|
|
1115
1108
|
if (!editor.selection || editor.selection.focus.path.length < 2)
|
|
1116
1109
|
return [];
|
|
1117
1110
|
try {
|
|
@@ -1121,9 +1114,9 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1121
1114
|
});
|
|
1122
1115
|
for (const [span, path] of spans) {
|
|
1123
1116
|
const [block] = Editor.node(editor, path, { depth: 1 });
|
|
1124
|
-
editor.isTextBlock(block) &&
|
|
1117
|
+
editor.isTextBlock(block) && block.markDefs?.forEach((def) => {
|
|
1125
1118
|
Text.isText(span) && span.marks && Array.isArray(span.marks) && span.marks.includes(def._key) && activeAnnotations.push(def);
|
|
1126
|
-
})
|
|
1119
|
+
});
|
|
1127
1120
|
}
|
|
1128
1121
|
return activeAnnotations;
|
|
1129
1122
|
} catch {
|
|
@@ -1141,27 +1134,16 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1141
1134
|
})
|
|
1142
1135
|
];
|
|
1143
1136
|
if (spans.length === 0 || spans.some(
|
|
1144
|
-
([span]) =>
|
|
1145
|
-
var _a;
|
|
1146
|
-
return !isPortableTextSpan(span) || !span.marks || ((_a = span.marks) == null ? void 0 : _a.length) === 0;
|
|
1147
|
-
}
|
|
1137
|
+
([span]) => !isPortableTextSpan$1(span) || !span.marks || span.marks?.length === 0
|
|
1148
1138
|
))
|
|
1149
1139
|
return !1;
|
|
1150
1140
|
const selectionMarkDefs = spans.reduce((accMarkDefs, [, path]) => {
|
|
1151
1141
|
const [block] = Editor.node(editor, path, { depth: 1 });
|
|
1152
1142
|
return editor.isTextBlock(block) && block.markDefs ? [...accMarkDefs, ...block.markDefs] : accMarkDefs;
|
|
1153
1143
|
}, []);
|
|
1154
|
-
return spans.every(([span]) =>
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
const spanMarkDefs = (_a = span.marks) == null ? void 0 : _a.map(
|
|
1158
|
-
(markKey) => {
|
|
1159
|
-
var _a2;
|
|
1160
|
-
return (_a2 = selectionMarkDefs.find((def) => (def == null ? void 0 : def._key) === markKey)) == null ? void 0 : _a2._type;
|
|
1161
|
-
}
|
|
1162
|
-
);
|
|
1163
|
-
return spanMarkDefs == null ? void 0 : spanMarkDefs.includes(annotationType);
|
|
1164
|
-
});
|
|
1144
|
+
return spans.every(([span]) => isPortableTextSpan$1(span) ? span.marks?.map(
|
|
1145
|
+
(markKey) => selectionMarkDefs.find((def) => def?._key === markKey)?._type
|
|
1146
|
+
)?.includes(annotationType) : !1);
|
|
1165
1147
|
} catch {
|
|
1166
1148
|
return !1;
|
|
1167
1149
|
}
|
|
@@ -1221,7 +1203,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1221
1203
|
if (!(range && range.anchor.path.length > 0 && range.focus.path.length > 0))
|
|
1222
1204
|
throw new Error("Invalid range");
|
|
1223
1205
|
if (range) {
|
|
1224
|
-
if (!
|
|
1206
|
+
if (!options?.mode || options?.mode === "selected") {
|
|
1225
1207
|
debug$j("Deleting content in selection"), Transforms.delete(editor, {
|
|
1226
1208
|
at: range,
|
|
1227
1209
|
hanging: !0,
|
|
@@ -1229,11 +1211,11 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
|
|
|
1229
1211
|
}), editor.onChange();
|
|
1230
1212
|
return;
|
|
1231
1213
|
}
|
|
1232
|
-
|
|
1214
|
+
options?.mode === "blocks" && (debug$j("Deleting blocks touched by selection"), Transforms.removeNodes(editor, {
|
|
1233
1215
|
at: range,
|
|
1234
1216
|
voids: !0,
|
|
1235
1217
|
match: (node) => editor.isTextBlock(node) || !editor.isTextBlock(node) && Element$1.isElement(node)
|
|
1236
|
-
})),
|
|
1218
|
+
})), options?.mode === "children" && (debug$j("Deleting children touched by selection"), Transforms.removeNodes(editor, {
|
|
1237
1219
|
at: range,
|
|
1238
1220
|
voids: !0,
|
|
1239
1221
|
match: (node) => node._type === types.span.name || // Text children
|
|
@@ -1332,6 +1314,10 @@ function withPreserveKeys(editor, fn) {
|
|
|
1332
1314
|
const prev = isPreservingKeys(editor);
|
|
1333
1315
|
PRESERVE_KEYS.set(editor, !0), fn(), PRESERVE_KEYS.set(editor, prev);
|
|
1334
1316
|
}
|
|
1317
|
+
function withoutPreserveKeys(editor, fn) {
|
|
1318
|
+
const prev = isPreservingKeys(editor);
|
|
1319
|
+
PRESERVE_KEYS.set(editor, !1), fn(), PRESERVE_KEYS.set(editor, prev);
|
|
1320
|
+
}
|
|
1335
1321
|
function isPreservingKeys(editor) {
|
|
1336
1322
|
return PRESERVE_KEYS.get(editor);
|
|
1337
1323
|
}
|
|
@@ -1342,17 +1328,27 @@ function createWithObjectKeys(schemaTypes, keyGenerator) {
|
|
|
1342
1328
|
return editor.apply = (operation) => {
|
|
1343
1329
|
if (operation.type === "split_node") {
|
|
1344
1330
|
const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.properties);
|
|
1345
|
-
|
|
1346
|
-
...operation
|
|
1347
|
-
|
|
1348
|
-
|
|
1331
|
+
apply2({
|
|
1332
|
+
...operation,
|
|
1333
|
+
properties: {
|
|
1334
|
+
...operation.properties,
|
|
1335
|
+
...withNewKey ? { _key: keyGenerator() } : {}
|
|
1336
|
+
}
|
|
1337
|
+
});
|
|
1338
|
+
return;
|
|
1349
1339
|
}
|
|
1350
1340
|
if (operation.type === "insert_node") {
|
|
1351
1341
|
const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.node);
|
|
1352
|
-
Editor.isEditor(operation.node)
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1342
|
+
if (!Editor.isEditor(operation.node)) {
|
|
1343
|
+
apply2({
|
|
1344
|
+
...operation,
|
|
1345
|
+
node: {
|
|
1346
|
+
...operation.node,
|
|
1347
|
+
...withNewKey ? { _key: keyGenerator() } : {}
|
|
1348
|
+
}
|
|
1349
|
+
});
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1356
1352
|
}
|
|
1357
1353
|
apply2(operation);
|
|
1358
1354
|
}, editor.normalizeNode = (entry) => {
|
|
@@ -2240,7 +2236,7 @@ function findBlockAndChildFromPath(editor, path) {
|
|
|
2240
2236
|
const isMatch = isKeyedSegment(path[2]) ? node._key === path[2]._key : index === path[2];
|
|
2241
2237
|
return isMatch && (childIndex = index), isMatch;
|
|
2242
2238
|
});
|
|
2243
|
-
return child ? { block, child, blockPath, childPath: blockPath
|
|
2239
|
+
return child ? { block, child, blockPath, childPath: blockPath?.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
|
|
2244
2240
|
}
|
|
2245
2241
|
function withRemoteChanges(editor, fn) {
|
|
2246
2242
|
const prev = isChangingRemotely(editor) || !1;
|
|
@@ -2329,11 +2325,12 @@ function createWithUndoRedo(options) {
|
|
|
2329
2325
|
)
|
|
2330
2326
|
);
|
|
2331
2327
|
});
|
|
2328
|
+
const reversedOperations = transformedOperations.map(Operation.inverse).reverse();
|
|
2332
2329
|
try {
|
|
2333
2330
|
Editor.withoutNormalizing(editor, () => {
|
|
2334
2331
|
withPreserveKeys(editor, () => {
|
|
2335
2332
|
withoutSaving(editor, () => {
|
|
2336
|
-
|
|
2333
|
+
reversedOperations.forEach((op) => {
|
|
2337
2334
|
editor.apply(op);
|
|
2338
2335
|
});
|
|
2339
2336
|
});
|
|
@@ -2406,14 +2403,13 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
|
|
|
2406
2403
|
if (patch.type === "diffMatchPatch") {
|
|
2407
2404
|
const operationTargetBlock = findOperationTargetBlock(editor, transformedOperation);
|
|
2408
2405
|
return !operationTargetBlock || !isEqual({ _key: operationTargetBlock._key }, patch.path[0]) ? [transformedOperation] : (parse(patch.value).forEach((diffPatch) => {
|
|
2409
|
-
var _a, _b, _c, _d;
|
|
2410
2406
|
let adjustOffsetBy = 0, changedOffset = diffPatch.utf8Start1;
|
|
2411
2407
|
const { diffs } = diffPatch;
|
|
2412
2408
|
if (diffs.forEach((diff2, index) => {
|
|
2413
2409
|
const [diffType, text] = diff2;
|
|
2414
2410
|
diffType === DIFF_INSERT ? (adjustOffsetBy += text.length, changedOffset += text.length) : diffType === DIFF_DELETE ? (adjustOffsetBy -= text.length, changedOffset -= text.length) : diffType === DIFF_EQUAL && (diffs.slice(index).every(([dType]) => dType === DIFF_EQUAL) || (changedOffset += text.length));
|
|
2415
2411
|
}), transformedOperation.type === "insert_text" && changedOffset < transformedOperation.offset && (transformedOperation.offset += adjustOffsetBy), transformedOperation.type === "remove_text" && changedOffset <= transformedOperation.offset - transformedOperation.text.length && (transformedOperation.offset += adjustOffsetBy), transformedOperation.type === "set_selection") {
|
|
2416
|
-
const currentFocus =
|
|
2412
|
+
const currentFocus = transformedOperation.properties?.focus ? { ...transformedOperation.properties.focus } : void 0, currentAnchor = transformedOperation?.properties?.anchor ? { ...transformedOperation.properties.anchor } : void 0, newFocus = transformedOperation?.newProperties?.focus ? { ...transformedOperation.newProperties.focus } : void 0, newAnchor = transformedOperation?.newProperties?.anchor ? { ...transformedOperation.newProperties.anchor } : void 0;
|
|
2417
2413
|
(currentFocus && currentAnchor || newFocus && newAnchor) && ([currentFocus, currentAnchor, newFocus, newAnchor].forEach((point) => {
|
|
2418
2414
|
point && changedOffset < point.offset && (point.offset += adjustOffsetBy);
|
|
2419
2415
|
}), currentFocus && currentAnchor && (transformedOperation.properties = {
|
|
@@ -2429,14 +2425,13 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
|
|
|
2429
2425
|
return [transformedOperation];
|
|
2430
2426
|
}
|
|
2431
2427
|
function adjustBlockPath(operation, level, blockIndex) {
|
|
2432
|
-
var _a, _b, _c, _d;
|
|
2433
2428
|
const transformedOperation = { ...operation };
|
|
2434
2429
|
if (blockIndex >= 0 && transformedOperation.type !== "set_selection" && Array.isArray(transformedOperation.path) && transformedOperation.path[0] >= blockIndex + level && transformedOperation.path[0] + level > -1) {
|
|
2435
2430
|
const newPath = [transformedOperation.path[0] + level, ...transformedOperation.path.slice(1)];
|
|
2436
2431
|
transformedOperation.path = newPath;
|
|
2437
2432
|
}
|
|
2438
2433
|
if (transformedOperation.type === "set_selection") {
|
|
2439
|
-
const currentFocus =
|
|
2434
|
+
const currentFocus = transformedOperation.properties?.focus ? { ...transformedOperation.properties.focus } : void 0, currentAnchor = transformedOperation?.properties?.anchor ? { ...transformedOperation.properties.anchor } : void 0, newFocus = transformedOperation?.newProperties?.focus ? { ...transformedOperation.newProperties.focus } : void 0, newAnchor = transformedOperation?.newProperties?.anchor ? { ...transformedOperation.newProperties.anchor } : void 0;
|
|
2440
2435
|
(currentFocus && currentAnchor || newFocus && newAnchor) && ([currentFocus, currentAnchor, newFocus, newAnchor].forEach((point) => {
|
|
2441
2436
|
point && point.path[0] >= blockIndex + level && point.path[0] + level > -1 && (point.path = [point.path[0] + level, ...point.path.slice(1)]);
|
|
2442
2437
|
}), currentFocus && currentAnchor && (transformedOperation.properties = {
|
|
@@ -2734,6 +2729,21 @@ function createWithPortableTextLists(types) {
|
|
|
2734
2729
|
}, editor;
|
|
2735
2730
|
};
|
|
2736
2731
|
}
|
|
2732
|
+
function isPortableTextSpan(node) {
|
|
2733
|
+
return node._type === "span" && "text" in node && typeof node.text == "string" && (typeof node.marks > "u" || Array.isArray(node.marks) && node.marks.every((mark) => typeof mark == "string"));
|
|
2734
|
+
}
|
|
2735
|
+
function isPortableTextBlock(node) {
|
|
2736
|
+
return (
|
|
2737
|
+
// A block doesn't _have_ to be named 'block' - to differentiate between
|
|
2738
|
+
// allowed child types and marks, one might name them differently
|
|
2739
|
+
typeof node._type == "string" && // Toolkit-types like nested spans are @-prefixed
|
|
2740
|
+
node._type[0] !== "@" && // `markDefs` isn't _required_ per say, but if it's there, it needs to be an array
|
|
2741
|
+
(!("markDefs" in node) || !node.markDefs || Array.isArray(node.markDefs) && // Every mark definition needs to have an `_key` to be mappable in child spans
|
|
2742
|
+
node.markDefs.every((def) => typeof def._key == "string")) && // `children` is required and needs to be an array
|
|
2743
|
+
"children" in node && Array.isArray(node.children) && // All children are objects with `_type` (usually spans, but can contain other stuff)
|
|
2744
|
+
node.children.every((child) => typeof child == "object" && "_type" in child)
|
|
2745
|
+
);
|
|
2746
|
+
}
|
|
2737
2747
|
const debug$c = debugWithName("plugin:withPortableTextMarkModel");
|
|
2738
2748
|
function createWithPortableTextMarkModel(types, change$) {
|
|
2739
2749
|
return function(editor) {
|
|
@@ -2761,10 +2771,7 @@ function createWithPortableTextMarkModel(types, change$) {
|
|
|
2761
2771
|
);
|
|
2762
2772
|
if (annotationMarks.length > 0) {
|
|
2763
2773
|
const [block] = Editor.node(editor, Path.parent(path)), orphanedMarks = editor.isTextBlock(block) && annotationMarks.filter(
|
|
2764
|
-
(mark) =>
|
|
2765
|
-
var _a;
|
|
2766
|
-
return !((_a = block.markDefs) != null && _a.find((def) => def._key === mark));
|
|
2767
|
-
}
|
|
2774
|
+
(mark) => !block.markDefs?.find((def) => def._key === mark)
|
|
2768
2775
|
) || [];
|
|
2769
2776
|
orphanedMarks.length > 0 && (debug$c("Removing orphaned .marks from span node"), Transforms.setNodes(
|
|
2770
2777
|
editor,
|
|
@@ -2816,10 +2823,9 @@ function createWithPortableTextMarkModel(types, change$) {
|
|
|
2816
2823
|
), editor.onChange());
|
|
2817
2824
|
}
|
|
2818
2825
|
}, editor.apply = (op) => {
|
|
2819
|
-
var _a, _b;
|
|
2820
2826
|
if (op.type === "insert_text") {
|
|
2821
2827
|
const { selection } = editor;
|
|
2822
|
-
if (selection && Range.isCollapsed(selection) &&
|
|
2828
|
+
if (selection && Range.isCollapsed(selection) && Editor.marks(editor)?.marks?.some((mark) => !decorators.includes(mark))) {
|
|
2823
2829
|
const [node] = Array.from(
|
|
2824
2830
|
Editor.nodes(editor, {
|
|
2825
2831
|
mode: "lowest",
|
|
@@ -2845,6 +2851,32 @@ function createWithPortableTextMarkModel(types, change$) {
|
|
|
2845
2851
|
}
|
|
2846
2852
|
}
|
|
2847
2853
|
}
|
|
2854
|
+
if (op.type === "remove_text") {
|
|
2855
|
+
const node = Array.from(
|
|
2856
|
+
Editor.nodes(editor, {
|
|
2857
|
+
mode: "lowest",
|
|
2858
|
+
at: { path: op.path, offset: op.offset },
|
|
2859
|
+
match: (n) => n._type === types.span.name,
|
|
2860
|
+
voids: !1
|
|
2861
|
+
})
|
|
2862
|
+
)[0][0], block = Editor.node(editor, Path.parent(op.path))[0];
|
|
2863
|
+
if (node && isPortableTextSpan(node) && block && isPortableTextBlock(block)) {
|
|
2864
|
+
const markDefs = block.markDefs ?? [], nodeHasAnnotations = (node.marks ?? []).some(
|
|
2865
|
+
(mark) => markDefs.find((markDef) => markDef._key === mark)
|
|
2866
|
+
), deletingPartOfTheNode = op.offset !== 0, deletingFromTheEnd = op.offset + op.text.length === node.text.length;
|
|
2867
|
+
if (nodeHasAnnotations && deletingPartOfTheNode && deletingFromTheEnd) {
|
|
2868
|
+
Editor.withoutNormalizing(editor, () => {
|
|
2869
|
+
withoutPreserveKeys(editor, () => {
|
|
2870
|
+
Transforms.splitNodes(editor, {
|
|
2871
|
+
match: Text.isText,
|
|
2872
|
+
at: { path: op.path, offset: op.offset }
|
|
2873
|
+
});
|
|
2874
|
+
}), Transforms.removeNodes(editor, { at: Path.next(op.path) });
|
|
2875
|
+
}), editor.onChange();
|
|
2876
|
+
return;
|
|
2877
|
+
}
|
|
2878
|
+
}
|
|
2879
|
+
}
|
|
2848
2880
|
apply2(op);
|
|
2849
2881
|
}, editor.addMark = (mark) => {
|
|
2850
2882
|
if (editor.selection) {
|
|
@@ -2853,10 +2885,7 @@ function createWithPortableTextMarkModel(types, change$) {
|
|
|
2853
2885
|
const splitTextNodes = [
|
|
2854
2886
|
...Editor.nodes(editor, { at: editor.selection, match: Text.isText })
|
|
2855
2887
|
];
|
|
2856
|
-
if (splitTextNodes.every((node) =>
|
|
2857
|
-
var _a;
|
|
2858
|
-
return (_a = node[0].marks) == null ? void 0 : _a.includes(mark);
|
|
2859
|
-
}))
|
|
2888
|
+
if (splitTextNodes.every((node) => node[0].marks?.includes(mark)))
|
|
2860
2889
|
return editor.removeMark(mark), editor;
|
|
2861
2890
|
Editor.withoutNormalizing(editor, () => {
|
|
2862
2891
|
splitTextNodes.forEach(([node, path]) => {
|
|
@@ -2925,9 +2954,8 @@ function createWithPortableTextMarkModel(types, change$) {
|
|
|
2925
2954
|
Editor.nodes(editor, { match: Text.isText, at: editor.selection })
|
|
2926
2955
|
);
|
|
2927
2956
|
return Range.isExpanded(editor.selection) ? selectedNodes.every((n) => {
|
|
2928
|
-
var _a;
|
|
2929
2957
|
const [node] = n;
|
|
2930
|
-
return
|
|
2958
|
+
return node.marks?.includes(mark);
|
|
2931
2959
|
}) : ({
|
|
2932
2960
|
...Editor.marks(editor) || {}
|
|
2933
2961
|
}.marks || []).includes(mark);
|
|
@@ -2987,7 +3015,7 @@ function createWithSchemaTypes({
|
|
|
2987
3015
|
keyGenerator
|
|
2988
3016
|
}) {
|
|
2989
3017
|
return function(editor) {
|
|
2990
|
-
editor.isTextBlock = (value) => isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => isPortableTextSpan(value) && value._type == schemaTypes.span.name, editor.isListBlock = (value) => isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
|
|
3018
|
+
editor.isTextBlock = (value) => isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => isPortableTextSpan$1(value) && value._type == schemaTypes.span.name, editor.isListBlock = (value) => isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
|
|
2991
3019
|
const { normalizeNode } = editor;
|
|
2992
3020
|
return editor.normalizeNode = (entry) => {
|
|
2993
3021
|
const [node, path] = entry;
|
|
@@ -3060,7 +3088,6 @@ function createWithHotkeys(types, portableTextEditor, hotkeysFromOptions) {
|
|
|
3060
3088
|
const reservedHotkeys = ["enter", "tab", "shift", "delete", "end"], activeHotkeys = hotkeysFromOptions || DEFAULT_HOTKEYS;
|
|
3061
3089
|
return function(editor) {
|
|
3062
3090
|
return editor.pteWithHotKeys = (event) => {
|
|
3063
|
-
var _a, _b, _c, _d, _e, _f;
|
|
3064
3091
|
Object.keys(activeHotkeys).forEach((cat) => {
|
|
3065
3092
|
if (cat === "marks")
|
|
3066
3093
|
for (const hotkey in activeHotkeys[cat]) {
|
|
@@ -3107,7 +3134,7 @@ function createWithHotkeys(types, portableTextEditor, hotkeysFromOptions) {
|
|
|
3107
3134
|
}
|
|
3108
3135
|
}
|
|
3109
3136
|
if (isBackspace && editor.selection && editor.selection.focus.path[0] === 0 && Range.isCollapsed(editor.selection)) {
|
|
3110
|
-
const focusBlock = Node.descendant(editor, editor.selection.focus.path.slice(0, 1)), nextPath = Path.next(editor.selection.focus.path.slice(0, 1)), nextBlock = Node.has(editor, nextPath), isTextBlock = isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 &&
|
|
3137
|
+
const focusBlock = Node.descendant(editor, editor.selection.focus.path.slice(0, 1)), nextPath = Path.next(editor.selection.focus.path.slice(0, 1)), nextBlock = Node.has(editor, nextPath), isTextBlock = isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
|
|
3111
3138
|
if (nextBlock && isTextBlock && isEmptyFocusBlock) {
|
|
3112
3139
|
event.preventDefault(), event.stopPropagation(), Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), editor.onChange();
|
|
3113
3140
|
return;
|
|
@@ -3117,7 +3144,7 @@ function createWithHotkeys(types, portableTextEditor, hotkeysFromOptions) {
|
|
|
3117
3144
|
const prevPath = Path.previous(editor.selection.focus.path.slice(0, 1)), prevBlock = Node.descendant(editor, prevPath), focusBlock = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
3118
3145
|
if (prevBlock && focusBlock && Editor.isVoid(editor, prevBlock) && editor.selection.focus.offset === 0) {
|
|
3119
3146
|
debug$8("Preventing deleting void block above"), event.preventDefault(), event.stopPropagation();
|
|
3120
|
-
const isTextBlock = isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 &&
|
|
3147
|
+
const isTextBlock = isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
|
|
3121
3148
|
if (!isTextBlock || isEmptyFocusBlock) {
|
|
3122
3149
|
Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), Transforms.select(editor, prevPath), editor.onChange();
|
|
3123
3150
|
return;
|
|
@@ -3133,17 +3160,17 @@ function createWithHotkeys(types, portableTextEditor, hotkeysFromOptions) {
|
|
|
3133
3160
|
const nextBlock = Node.descendant(
|
|
3134
3161
|
editor,
|
|
3135
3162
|
Path.next(editor.selection.focus.path.slice(0, 1))
|
|
3136
|
-
), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath), isEmptyFocusBlock = isPortableTextTextBlock(focusBlock) && focusBlock.children.length === 1 &&
|
|
3163
|
+
), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath), isEmptyFocusBlock = isPortableTextTextBlock(focusBlock) && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
|
|
3137
3164
|
if (nextBlock && focusBlock && !Editor.isVoid(editor, focusBlock) && Editor.isVoid(editor, nextBlock) && isEmptyFocusBlock) {
|
|
3138
3165
|
debug$8("Preventing deleting void block below"), event.preventDefault(), event.stopPropagation(), Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), Transforms.select(editor, focusBlockPath), editor.onChange();
|
|
3139
3166
|
return;
|
|
3140
3167
|
}
|
|
3141
3168
|
}
|
|
3142
3169
|
if ((isTab || isShiftTab) && editor.selection) {
|
|
3143
|
-
const [focusChild] = Editor.node(editor, editor.selection.focus, { depth: 2 }), [focusBlock] = isPortableTextSpan(focusChild) ? Editor.node(editor, editor.selection.focus, { depth: 1 }) : [], hasAnnotationFocus = focusChild && isPortableTextTextBlock(focusBlock) && isPortableTextSpan(focusChild) && (focusChild.marks || []).filter(
|
|
3170
|
+
const [focusChild] = Editor.node(editor, editor.selection.focus, { depth: 2 }), [focusBlock] = isPortableTextSpan$1(focusChild) ? Editor.node(editor, editor.selection.focus, { depth: 1 }) : [], hasAnnotationFocus = focusChild && isPortableTextTextBlock(focusBlock) && isPortableTextSpan$1(focusChild) && (focusChild.marks || []).filter(
|
|
3144
3171
|
(m) => (focusBlock.markDefs || []).map((def) => def._key).includes(m)
|
|
3145
3172
|
).length > 0, [start] = Range.edges(editor.selection), atStartOfNode = Editor.isStart(editor, start, start.path);
|
|
3146
|
-
focusChild && isPortableTextSpan(focusChild) && (!hasAnnotationFocus || atStartOfNode) && editor.pteIncrementBlockLevels(isShiftTab) && event.preventDefault();
|
|
3173
|
+
focusChild && isPortableTextSpan$1(focusChild) && (!hasAnnotationFocus || atStartOfNode) && editor.pteIncrementBlockLevels(isShiftTab) && event.preventDefault();
|
|
3147
3174
|
}
|
|
3148
3175
|
if (isEnter && !isShiftEnter && editor.selection) {
|
|
3149
3176
|
const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
|
|
@@ -3438,13 +3465,7 @@ function validateValue(value, types, keyGenerator) {
|
|
|
3438
3465
|
const debug$7 = debugWithName("plugin:withInsertData");
|
|
3439
3466
|
function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
3440
3467
|
return function(editor) {
|
|
3441
|
-
const blockTypeName = schemaTypes.block.name, spanTypeName = schemaTypes.span.name, whitespaceOnPasteMode = schemaTypes.block.options.unstable_whitespaceOnPasteMode, toPlainText = (blocks) => blocks.map((block) => {
|
|
3442
|
-
var _a;
|
|
3443
|
-
return editor.isTextBlock(block) ? block.children.map((child) => {
|
|
3444
|
-
var _a2;
|
|
3445
|
-
return child._type === spanTypeName ? child.text : `[${((_a2 = schemaTypes.inlineObjects.find((t) => t.name === child._type)) == null ? void 0 : _a2.title) || "Object"}]`;
|
|
3446
|
-
}).join("") : `[${((_a = schemaTypes.blockObjects.find((t) => t.name === block._type)) == null ? void 0 : _a.title) || "Object"}]`;
|
|
3447
|
-
}).join(`
|
|
3468
|
+
const blockTypeName = schemaTypes.block.name, spanTypeName = schemaTypes.span.name, whitespaceOnPasteMode = schemaTypes.block.options.unstable_whitespaceOnPasteMode, toPlainText = (blocks) => blocks.map((block) => editor.isTextBlock(block) ? block.children.map((child) => child._type === spanTypeName ? child.text : `[${schemaTypes.inlineObjects.find((t) => t.name === child._type)?.title || "Object"}]`).join("") : `[${schemaTypes.blockObjects.find((t) => t.name === block._type)?.title || "Object"}]`).join(`
|
|
3448
3469
|
|
|
3449
3470
|
`);
|
|
3450
3471
|
return editor.setFragmentData = (data, originEvent) => {
|
|
@@ -3476,7 +3497,6 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3476
3497
|
const fragment = editor.getFragment(), portableText = fromSlateValue(fragment, blockTypeName), asJSON = JSON.stringify(portableText), asPlainText = toPlainText(portableText);
|
|
3477
3498
|
data.clearData(), data.setData("text/plain", asPlainText), data.setData("text/html", asHTML), data.setData("application/json", asJSON), data.setData("application/x-portable-text", asJSON), debug$7("text", asPlainText), data.setData("application/x-portable-text-event-origin", originEvent || "external"), debug$7("Set fragment data", asJSON, asHTML);
|
|
3478
3499
|
}, editor.insertPortableTextData = (data) => {
|
|
3479
|
-
var _a, _b;
|
|
3480
3500
|
if (!editor.selection)
|
|
3481
3501
|
return !1;
|
|
3482
3502
|
const pText = data.getData("application/x-portable-text"), origin = data.getData("application/x-portable-text-event-origin");
|
|
@@ -3490,8 +3510,8 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3490
3510
|
spanTypeName,
|
|
3491
3511
|
schemaTypes
|
|
3492
3512
|
), validation = validateValue(parsed, schemaTypes, keyGenerator);
|
|
3493
|
-
if (!validation.valid && !
|
|
3494
|
-
const errorDescription = `${
|
|
3513
|
+
if (!validation.valid && !validation.resolution?.autoResolve) {
|
|
3514
|
+
const errorDescription = `${validation.resolution?.description}`;
|
|
3495
3515
|
return change$.next({
|
|
3496
3516
|
type: "error",
|
|
3497
3517
|
level: "warning",
|
|
@@ -3505,7 +3525,6 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3505
3525
|
}
|
|
3506
3526
|
return !1;
|
|
3507
3527
|
}, editor.insertTextOrHTMLData = (data) => {
|
|
3508
|
-
var _a;
|
|
3509
3528
|
if (!editor.selection)
|
|
3510
3529
|
return debug$7("No selection, not inserting"), !1;
|
|
3511
3530
|
change$.next({ type: "loading", isLoading: !0 });
|
|
@@ -3531,7 +3550,7 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3531
3550
|
const validation = validateValue(portableText, schemaTypes, keyGenerator);
|
|
3532
3551
|
if (!validation.valid) {
|
|
3533
3552
|
const errorDescription = `Could not validate the resulting portable text to insert.
|
|
3534
|
-
${
|
|
3553
|
+
${validation.resolution?.description}
|
|
3535
3554
|
Try to insert as plain text (shift-paste) instead.`;
|
|
3536
3555
|
return change$.next({
|
|
3537
3556
|
type: "error",
|
|
@@ -3796,15 +3815,14 @@ function useSyncValue(props) {
|
|
|
3796
3815
|
isChanged = !0;
|
|
3797
3816
|
}
|
|
3798
3817
|
slateValueFromProps.forEach((currentBlock, currentBlockIndex) => {
|
|
3799
|
-
var _a, _b, _c, _d, _e;
|
|
3800
3818
|
const oldBlock = slateEditor.children[currentBlockIndex];
|
|
3801
3819
|
if (oldBlock && !isEqual(currentBlock, oldBlock) && isValid) {
|
|
3802
3820
|
const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, schemaTypes, keyGenerator);
|
|
3803
|
-
!validation.valid &&
|
|
3804
|
-
`${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${
|
|
3821
|
+
!validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
|
|
3822
|
+
`${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`
|
|
3805
3823
|
), validation.resolution.patches.forEach((patch) => {
|
|
3806
3824
|
change$.next({ type: "patch", patch });
|
|
3807
|
-
})), validation.valid ||
|
|
3825
|
+
})), validation.valid || validation.resolution?.autoResolve ? (oldBlock._key === currentBlock._key ? (debug$5.enabled && debug$5("Updating block", oldBlock, currentBlock), _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex)) : (debug$5.enabled && debug$5("Replacing block", oldBlock, currentBlock), _replaceBlock(slateEditor, currentBlock, currentBlockIndex)), isChanged = !0) : (change$.next({
|
|
3808
3826
|
type: "invalidValue",
|
|
3809
3827
|
resolution: validation.resolution,
|
|
3810
3828
|
value
|
|
@@ -3815,7 +3833,7 @@ function useSyncValue(props) {
|
|
|
3815
3833
|
debug$5.enabled && debug$5(
|
|
3816
3834
|
"Validating and inserting new block in the end of the value",
|
|
3817
3835
|
currentBlock
|
|
3818
|
-
), validation.valid ||
|
|
3836
|
+
), validation.valid || validation.resolution?.autoResolve ? withPreserveKeys(slateEditor, () => {
|
|
3819
3837
|
Transforms.insertNodes(slateEditor, currentBlock, {
|
|
3820
3838
|
at: [currentBlockIndex]
|
|
3821
3839
|
});
|
|
@@ -3885,9 +3903,9 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
|
|
|
3885
3903
|
}));
|
|
3886
3904
|
}
|
|
3887
3905
|
), currentBlock.children.forEach((currentBlockChild, currentBlockChildIndex) => {
|
|
3888
|
-
const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual(currentBlockChild, oldBlockChild), isTextChanged = !isEqual(currentBlockChild.text, oldBlockChild
|
|
3906
|
+
const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual(currentBlockChild, oldBlockChild), isTextChanged = !isEqual(currentBlockChild.text, oldBlockChild?.text), path = [currentBlockIndex, currentBlockChildIndex];
|
|
3889
3907
|
if (isChildChanged)
|
|
3890
|
-
if (currentBlockChild._key ===
|
|
3908
|
+
if (currentBlockChild._key === oldBlockChild?._key) {
|
|
3891
3909
|
debug$5("Updating changed child", currentBlockChild, oldBlockChild), Transforms.setNodes(slateEditor, currentBlockChild, {
|
|
3892
3910
|
at: path
|
|
3893
3911
|
});
|
|
@@ -4003,16 +4021,22 @@ function PortableTextEditorSelectionProvider(props) {
|
|
|
4003
4021
|
};
|
|
4004
4022
|
}, [change$]), /* @__PURE__ */ jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children });
|
|
4005
4023
|
}
|
|
4006
|
-
var __defProp = Object.defineProperty, __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __publicField = (obj, key, value) => __defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value);
|
|
4007
4024
|
const debug$2 = debugWithName("component:PortableTextEditor");
|
|
4008
4025
|
class PortableTextEditor extends Component {
|
|
4026
|
+
/**
|
|
4027
|
+
* An observable of all the editor changes.
|
|
4028
|
+
*/
|
|
4029
|
+
change$ = new Subject();
|
|
4030
|
+
/**
|
|
4031
|
+
* A lookup table for all the relevant schema types for this portable text type.
|
|
4032
|
+
*/
|
|
4033
|
+
schemaTypes;
|
|
4034
|
+
/**
|
|
4035
|
+
* The editor API (currently implemented with Slate).
|
|
4036
|
+
*/
|
|
4037
|
+
editable;
|
|
4009
4038
|
constructor(props) {
|
|
4010
|
-
if (super(props),
|
|
4011
|
-
this.editable = { ...this.editable, ...editable };
|
|
4012
|
-
}), __publicField(this, "getValue", () => {
|
|
4013
|
-
if (this.editable)
|
|
4014
|
-
return this.editable.getValue();
|
|
4015
|
-
}), !props.schemaType)
|
|
4039
|
+
if (super(props), !props.schemaType)
|
|
4016
4040
|
throw new Error('PortableTextEditor: missing "schemaType" property');
|
|
4017
4041
|
props.incomingPatches$ && console.warn("The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"), this.change$.next({ type: "loading", isLoading: !0 }), this.schemaTypes = getPortableTextMemberSchemaTypes(
|
|
4018
4042
|
props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
|
|
@@ -4023,6 +4047,13 @@ class PortableTextEditor extends Component {
|
|
|
4023
4047
|
this.props.schemaType.hasOwnProperty("jsonType") ? this.props.schemaType : compileType(this.props.schemaType)
|
|
4024
4048
|
)), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this);
|
|
4025
4049
|
}
|
|
4050
|
+
setEditable = (editable) => {
|
|
4051
|
+
this.editable = { ...this.editable, ...editable };
|
|
4052
|
+
};
|
|
4053
|
+
getValue = () => {
|
|
4054
|
+
if (this.editable)
|
|
4055
|
+
return this.editable.getValue();
|
|
4056
|
+
};
|
|
4026
4057
|
render() {
|
|
4027
4058
|
const { onChange, value, children, patches$, incomingPatches$ } = this.props, { change$ } = this, _patches$ = incomingPatches$ || patches$, maxBlocks = typeof this.props.maxBlocks > "u" ? void 0 : parseInt(this.props.maxBlocks.toString(), 10) || void 0, readOnly = !!this.props.readOnly, keyGenerator = this.props.keyGenerator || defaultKeyGenerator;
|
|
4028
4059
|
return /* @__PURE__ */ jsx(
|
|
@@ -4048,99 +4079,59 @@ class PortableTextEditor extends Component {
|
|
|
4048
4079
|
}
|
|
4049
4080
|
);
|
|
4050
4081
|
}
|
|
4082
|
+
// Static API methods
|
|
4083
|
+
static activeAnnotations = (editor) => editor && editor.editable ? editor.editable.activeAnnotations() : [];
|
|
4084
|
+
static isAnnotationActive = (editor, annotationType) => editor && editor.editable ? editor.editable.isAnnotationActive(annotationType) : !1;
|
|
4085
|
+
static addAnnotation = (editor, type, value) => editor.editable?.addAnnotation(type, value);
|
|
4086
|
+
static blur = (editor) => {
|
|
4087
|
+
debug$2("Host blurred"), editor.editable?.blur();
|
|
4088
|
+
};
|
|
4089
|
+
static delete = (editor, selection, options) => editor.editable?.delete(selection, options);
|
|
4090
|
+
static findDOMNode = (editor, element) => editor.editable?.findDOMNode(element);
|
|
4091
|
+
static findByPath = (editor, path) => editor.editable?.findByPath(path) || [];
|
|
4092
|
+
static focus = (editor) => {
|
|
4093
|
+
debug$2("Host requesting focus"), editor.editable?.focus();
|
|
4094
|
+
};
|
|
4095
|
+
static focusBlock = (editor) => editor.editable?.focusBlock();
|
|
4096
|
+
static focusChild = (editor) => editor.editable?.focusChild();
|
|
4097
|
+
static getSelection = (editor) => editor.editable ? editor.editable.getSelection() : null;
|
|
4098
|
+
static getValue = (editor) => editor.editable?.getValue();
|
|
4099
|
+
static hasBlockStyle = (editor, blockStyle) => editor.editable?.hasBlockStyle(blockStyle);
|
|
4100
|
+
static hasListStyle = (editor, listStyle) => editor.editable?.hasListStyle(listStyle);
|
|
4101
|
+
static isCollapsedSelection = (editor) => editor.editable?.isCollapsedSelection();
|
|
4102
|
+
static isExpandedSelection = (editor) => editor.editable?.isExpandedSelection();
|
|
4103
|
+
static isMarkActive = (editor, mark) => editor.editable?.isMarkActive(mark);
|
|
4104
|
+
static insertChild = (editor, type, value) => (debug$2("Host inserting child"), editor.editable?.insertChild(type, value));
|
|
4105
|
+
static insertBlock = (editor, type, value) => editor.editable?.insertBlock(type, value);
|
|
4106
|
+
static insertBreak = (editor) => editor.editable?.insertBreak();
|
|
4107
|
+
static isVoid = (editor, element) => editor.editable?.isVoid(element);
|
|
4108
|
+
static isObjectPath = (editor, path) => {
|
|
4109
|
+
if (!path || !Array.isArray(path)) return !1;
|
|
4110
|
+
const isChildObjectEditPath = path.length > 3 && path[1] === "children";
|
|
4111
|
+
return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
|
|
4112
|
+
};
|
|
4113
|
+
static marks = (editor) => editor.editable?.marks();
|
|
4114
|
+
static select = (editor, selection) => {
|
|
4115
|
+
debug$2("Host setting selection", selection), editor.editable?.select(selection);
|
|
4116
|
+
};
|
|
4117
|
+
static removeAnnotation = (editor, type) => editor.editable?.removeAnnotation(type);
|
|
4118
|
+
static toggleBlockStyle = (editor, blockStyle) => (debug$2("Host is toggling block style"), editor.editable?.toggleBlockStyle(blockStyle));
|
|
4119
|
+
static toggleList = (editor, listStyle) => editor.editable?.toggleList(listStyle);
|
|
4120
|
+
static toggleMark = (editor, mark) => {
|
|
4121
|
+
debug$2("Host toggling mark", mark), editor.editable?.toggleMark(mark);
|
|
4122
|
+
};
|
|
4123
|
+
static getFragment = (editor) => (debug$2("Host getting fragment"), editor.editable?.getFragment());
|
|
4124
|
+
static undo = (editor) => {
|
|
4125
|
+
debug$2("Host undoing"), editor.editable?.undo();
|
|
4126
|
+
};
|
|
4127
|
+
static redo = (editor) => {
|
|
4128
|
+
debug$2("Host redoing"), editor.editable?.redo();
|
|
4129
|
+
};
|
|
4130
|
+
static isSelectionsOverlapping = (editor, selectionA, selectionB) => editor.editable?.isSelectionsOverlapping(selectionA, selectionB);
|
|
4051
4131
|
}
|
|
4052
|
-
__publicField(PortableTextEditor, "activeAnnotations", (editor) => editor && editor.editable ? editor.editable.activeAnnotations() : []), __publicField(PortableTextEditor, "isAnnotationActive", (editor, annotationType) => editor && editor.editable ? editor.editable.isAnnotationActive(annotationType) : !1), __publicField(PortableTextEditor, "addAnnotation", (editor, type, value) => {
|
|
4053
|
-
var _a;
|
|
4054
|
-
return (_a = editor.editable) == null ? void 0 : _a.addAnnotation(type, value);
|
|
4055
|
-
}), __publicField(PortableTextEditor, "blur", (editor) => {
|
|
4056
|
-
var _a;
|
|
4057
|
-
debug$2("Host blurred"), (_a = editor.editable) == null || _a.blur();
|
|
4058
|
-
}), __publicField(PortableTextEditor, "delete", (editor, selection, options) => {
|
|
4059
|
-
var _a;
|
|
4060
|
-
return (_a = editor.editable) == null ? void 0 : _a.delete(selection, options);
|
|
4061
|
-
}), __publicField(PortableTextEditor, "findDOMNode", (editor, element) => {
|
|
4062
|
-
var _a;
|
|
4063
|
-
return (_a = editor.editable) == null ? void 0 : _a.findDOMNode(element);
|
|
4064
|
-
}), __publicField(PortableTextEditor, "findByPath", (editor, path) => {
|
|
4065
|
-
var _a;
|
|
4066
|
-
return ((_a = editor.editable) == null ? void 0 : _a.findByPath(path)) || [];
|
|
4067
|
-
}), __publicField(PortableTextEditor, "focus", (editor) => {
|
|
4068
|
-
var _a;
|
|
4069
|
-
debug$2("Host requesting focus"), (_a = editor.editable) == null || _a.focus();
|
|
4070
|
-
}), __publicField(PortableTextEditor, "focusBlock", (editor) => {
|
|
4071
|
-
var _a;
|
|
4072
|
-
return (_a = editor.editable) == null ? void 0 : _a.focusBlock();
|
|
4073
|
-
}), __publicField(PortableTextEditor, "focusChild", (editor) => {
|
|
4074
|
-
var _a;
|
|
4075
|
-
return (_a = editor.editable) == null ? void 0 : _a.focusChild();
|
|
4076
|
-
}), __publicField(PortableTextEditor, "getSelection", (editor) => editor.editable ? editor.editable.getSelection() : null), __publicField(PortableTextEditor, "getValue", (editor) => {
|
|
4077
|
-
var _a;
|
|
4078
|
-
return (_a = editor.editable) == null ? void 0 : _a.getValue();
|
|
4079
|
-
}), __publicField(PortableTextEditor, "hasBlockStyle", (editor, blockStyle) => {
|
|
4080
|
-
var _a;
|
|
4081
|
-
return (_a = editor.editable) == null ? void 0 : _a.hasBlockStyle(blockStyle);
|
|
4082
|
-
}), __publicField(PortableTextEditor, "hasListStyle", (editor, listStyle) => {
|
|
4083
|
-
var _a;
|
|
4084
|
-
return (_a = editor.editable) == null ? void 0 : _a.hasListStyle(listStyle);
|
|
4085
|
-
}), __publicField(PortableTextEditor, "isCollapsedSelection", (editor) => {
|
|
4086
|
-
var _a;
|
|
4087
|
-
return (_a = editor.editable) == null ? void 0 : _a.isCollapsedSelection();
|
|
4088
|
-
}), __publicField(PortableTextEditor, "isExpandedSelection", (editor) => {
|
|
4089
|
-
var _a;
|
|
4090
|
-
return (_a = editor.editable) == null ? void 0 : _a.isExpandedSelection();
|
|
4091
|
-
}), __publicField(PortableTextEditor, "isMarkActive", (editor, mark) => {
|
|
4092
|
-
var _a;
|
|
4093
|
-
return (_a = editor.editable) == null ? void 0 : _a.isMarkActive(mark);
|
|
4094
|
-
}), __publicField(PortableTextEditor, "insertChild", (editor, type, value) => {
|
|
4095
|
-
var _a;
|
|
4096
|
-
return debug$2("Host inserting child"), (_a = editor.editable) == null ? void 0 : _a.insertChild(type, value);
|
|
4097
|
-
}), __publicField(PortableTextEditor, "insertBlock", (editor, type, value) => {
|
|
4098
|
-
var _a;
|
|
4099
|
-
return (_a = editor.editable) == null ? void 0 : _a.insertBlock(type, value);
|
|
4100
|
-
}), __publicField(PortableTextEditor, "insertBreak", (editor) => {
|
|
4101
|
-
var _a;
|
|
4102
|
-
return (_a = editor.editable) == null ? void 0 : _a.insertBreak();
|
|
4103
|
-
}), __publicField(PortableTextEditor, "isVoid", (editor, element) => {
|
|
4104
|
-
var _a;
|
|
4105
|
-
return (_a = editor.editable) == null ? void 0 : _a.isVoid(element);
|
|
4106
|
-
}), __publicField(PortableTextEditor, "isObjectPath", (editor, path) => {
|
|
4107
|
-
if (!path || !Array.isArray(path)) return !1;
|
|
4108
|
-
const isChildObjectEditPath = path.length > 3 && path[1] === "children";
|
|
4109
|
-
return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
|
|
4110
|
-
}), __publicField(PortableTextEditor, "marks", (editor) => {
|
|
4111
|
-
var _a;
|
|
4112
|
-
return (_a = editor.editable) == null ? void 0 : _a.marks();
|
|
4113
|
-
}), __publicField(PortableTextEditor, "select", (editor, selection) => {
|
|
4114
|
-
var _a;
|
|
4115
|
-
debug$2("Host setting selection", selection), (_a = editor.editable) == null || _a.select(selection);
|
|
4116
|
-
}), __publicField(PortableTextEditor, "removeAnnotation", (editor, type) => {
|
|
4117
|
-
var _a;
|
|
4118
|
-
return (_a = editor.editable) == null ? void 0 : _a.removeAnnotation(type);
|
|
4119
|
-
}), __publicField(PortableTextEditor, "toggleBlockStyle", (editor, blockStyle) => {
|
|
4120
|
-
var _a;
|
|
4121
|
-
return debug$2("Host is toggling block style"), (_a = editor.editable) == null ? void 0 : _a.toggleBlockStyle(blockStyle);
|
|
4122
|
-
}), __publicField(PortableTextEditor, "toggleList", (editor, listStyle) => {
|
|
4123
|
-
var _a;
|
|
4124
|
-
return (_a = editor.editable) == null ? void 0 : _a.toggleList(listStyle);
|
|
4125
|
-
}), __publicField(PortableTextEditor, "toggleMark", (editor, mark) => {
|
|
4126
|
-
var _a;
|
|
4127
|
-
debug$2("Host toggling mark", mark), (_a = editor.editable) == null || _a.toggleMark(mark);
|
|
4128
|
-
}), __publicField(PortableTextEditor, "getFragment", (editor) => {
|
|
4129
|
-
var _a;
|
|
4130
|
-
return debug$2("Host getting fragment"), (_a = editor.editable) == null ? void 0 : _a.getFragment();
|
|
4131
|
-
}), __publicField(PortableTextEditor, "undo", (editor) => {
|
|
4132
|
-
var _a;
|
|
4133
|
-
debug$2("Host undoing"), (_a = editor.editable) == null || _a.undo();
|
|
4134
|
-
}), __publicField(PortableTextEditor, "redo", (editor) => {
|
|
4135
|
-
var _a;
|
|
4136
|
-
debug$2("Host redoing"), (_a = editor.editable) == null || _a.redo();
|
|
4137
|
-
}), __publicField(PortableTextEditor, "isSelectionsOverlapping", (editor, selectionA, selectionB) => {
|
|
4138
|
-
var _a;
|
|
4139
|
-
return (_a = editor.editable) == null ? void 0 : _a.isSelectionsOverlapping(selectionA, selectionB);
|
|
4140
|
-
});
|
|
4141
4132
|
const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (props) => {
|
|
4142
4133
|
const { attributes, children, leaf, schemaTypes, renderChild, renderDecorator, renderAnnotation } = props, spanRef = useRef(null), portableTextEditor = usePortableTextEditor(), blockSelected = useSelected(), [focused, setFocused] = useState(!1), [selected, setSelected] = useState(!1), block = children.props.parent, path = useMemo(
|
|
4143
|
-
() => block ? [{ _key: block
|
|
4134
|
+
() => block ? [{ _key: block?._key }, "children", { _key: leaf._key }] : [],
|
|
4144
4135
|
[block, leaf._key]
|
|
4145
4136
|
), decoratorValues = useMemo(
|
|
4146
4137
|
() => schemaTypes.decorators.map((dec) => dec.value),
|
|
@@ -4150,10 +4141,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4150
4141
|
[decoratorValues, leaf.marks]
|
|
4151
4142
|
), annotationMarks = Array.isArray(leaf.marks) ? leaf.marks : EMPTY_MARKS, annotations = useMemo(
|
|
4152
4143
|
() => annotationMarks.map(
|
|
4153
|
-
(mark) =>
|
|
4154
|
-
var _a;
|
|
4155
|
-
return !decoratorValues.includes(mark) && ((_a = block == null ? void 0 : block.markDefs) == null ? void 0 : _a.find((def) => def._key === mark));
|
|
4156
|
-
}
|
|
4144
|
+
(mark) => !decoratorValues.includes(mark) && block?.markDefs?.find((def) => def._key === mark)
|
|
4157
4145
|
).filter(Boolean),
|
|
4158
4146
|
[annotationMarks, block, decoratorValues]
|
|
4159
4147
|
), shouldTrackSelectionAndFocus = annotations.length > 0 && blockSelected;
|
|
@@ -4464,7 +4452,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4464
4452
|
debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
|
|
4465
4453
|
return;
|
|
4466
4454
|
}
|
|
4467
|
-
const value = PortableTextEditor.getValue(portableTextEditor),
|
|
4455
|
+
const value = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(value, slateEditor.selection, schemaTypes)?.focus.path || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
|
|
4468
4456
|
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) => {
|
|
4469
4457
|
debug("Custom paste function from client resolved", result), !result || !result.insert ? (debug("No result from custom paste handler, pasting normally"), slateEditor.insertData(event.clipboardData)) : result.insert ? slateEditor.insertFragment(
|
|
4470
4458
|
toSlateValue(result.insert, { schemaTypes })
|
|
@@ -4520,7 +4508,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4520
4508
|
const existingDOMRange = domSelection.getRangeAt(0);
|
|
4521
4509
|
try {
|
|
4522
4510
|
const newDOMRange = ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
|
|
4523
|
-
(newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection
|
|
4511
|
+
(newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
|
|
4524
4512
|
} catch {
|
|
4525
4513
|
debug("Could not resolve selection, selecting top document"), Transforms.deselect(slateEditor), slateEditor.children.length > 0 && Transforms.select(slateEditor, [0, 0]), slateEditor.onChange();
|
|
4526
4514
|
}
|