@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.js
CHANGED
|
@@ -169,8 +169,7 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
|
|
|
169
169
|
});
|
|
170
170
|
}
|
|
171
171
|
function isEqualToEmptyEditor(children, schemaTypes) {
|
|
172
|
-
|
|
173
|
-
return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && slate.Element.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 && slate.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 === "";
|
|
172
|
+
return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && slate.Element.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 && slate.Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
|
|
174
173
|
}
|
|
175
174
|
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__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx("pre", { children: JSON.stringify(props.value, null, 2) }) }), DefaultBlockObject = styledComponents.styled.div`
|
|
176
175
|
user-select: none;
|
|
@@ -639,13 +638,12 @@ function DefaultAnnotation(props) {
|
|
|
639
638
|
return /* @__PURE__ */ jsxRuntime.jsx("span", { style: { color: "blue" }, onClick: handleClick, children: props.children });
|
|
640
639
|
}
|
|
641
640
|
function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
642
|
-
var _a, _b, _c;
|
|
643
641
|
if (!portableTextType)
|
|
644
642
|
throw new Error("Parameter 'portabletextType' missing (required)");
|
|
645
|
-
const blockType =
|
|
643
|
+
const blockType = portableTextType.of?.find(findBlockType);
|
|
646
644
|
if (!blockType)
|
|
647
645
|
throw new Error("Block type is not defined in this schema (required)");
|
|
648
|
-
const childrenField =
|
|
646
|
+
const childrenField = blockType.fields?.find((field) => field.name === "children");
|
|
649
647
|
if (!childrenField)
|
|
650
648
|
throw new Error("Children field for block type found in schema (required)");
|
|
651
649
|
const ofType = childrenField.type.of;
|
|
@@ -654,7 +652,7 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
|
654
652
|
const spanType = ofType.find((memberType) => memberType.name === "span");
|
|
655
653
|
if (!spanType)
|
|
656
654
|
throw new Error("Span type not found in schema (required)");
|
|
657
|
-
const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes =
|
|
655
|
+
const inlineObjectTypes = ofType.filter((memberType) => memberType.name !== "span") || [], blockObjectTypes = portableTextType.of?.filter((field) => field.name !== blockType.name) || [];
|
|
658
656
|
return {
|
|
659
657
|
styles: resolveEnabledStyles(blockType),
|
|
660
658
|
decorators: resolveEnabledDecorators(spanType),
|
|
@@ -668,11 +666,10 @@ function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
|
668
666
|
};
|
|
669
667
|
}
|
|
670
668
|
function resolveEnabledStyles(blockType) {
|
|
671
|
-
|
|
672
|
-
const styleField = (_a = blockType.fields) == null ? void 0 : _a.find((btField) => btField.name === "style");
|
|
669
|
+
const styleField = blockType.fields?.find((btField) => btField.name === "style");
|
|
673
670
|
if (!styleField)
|
|
674
671
|
throw new Error("A field with name 'style' is not defined in the block type (required).");
|
|
675
|
-
const textStyles =
|
|
672
|
+
const textStyles = styleField.type.options?.list && styleField.type.options.list?.filter((style) => style.value);
|
|
676
673
|
if (!textStyles || textStyles.length === 0)
|
|
677
674
|
throw new Error(
|
|
678
675
|
"The style fields need at least one style defined. I.e: {title: 'Normal', value: 'normal'}."
|
|
@@ -683,11 +680,10 @@ function resolveEnabledDecorators(spanType) {
|
|
|
683
680
|
return spanType.decorators;
|
|
684
681
|
}
|
|
685
682
|
function resolveEnabledListItems(blockType) {
|
|
686
|
-
|
|
687
|
-
const listField = (_a = blockType.fields) == null ? void 0 : _a.find((btField) => btField.name === "listItem");
|
|
683
|
+
const listField = blockType.fields?.find((btField) => btField.name === "listItem");
|
|
688
684
|
if (!listField)
|
|
689
685
|
throw new Error("A field with name 'listItem' is not defined in the block type (required).");
|
|
690
|
-
const listItems =
|
|
686
|
+
const listItems = listField.type.options?.list && listField.type.options.list.filter((list) => list.value);
|
|
691
687
|
if (!listItems)
|
|
692
688
|
throw new Error("The list field need at least to be an empty array");
|
|
693
689
|
return listItems;
|
|
@@ -765,7 +761,7 @@ function createOperationToPatches(types2) {
|
|
|
765
761
|
function insertNodePatch(editor, operation, beforeValue) {
|
|
766
762
|
const block = beforeValue[operation.path[0]], isTextBlock = editor.isTextBlock(block);
|
|
767
763
|
if (operation.path.length === 1) {
|
|
768
|
-
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block
|
|
764
|
+
const position = operation.path[0] === 0 ? "before" : "after", beforeBlock = beforeValue[operation.path[0] - 1], targetKey = operation.path[0] === 0 ? block?._key : beforeBlock?._key;
|
|
769
765
|
return targetKey ? [
|
|
770
766
|
patches.insert([fromSlateValue([operation.node], textBlockName)[0]], position, [
|
|
771
767
|
{ _key: targetKey }
|
|
@@ -867,7 +863,7 @@ function createOperationToPatches(types2) {
|
|
|
867
863
|
function mergeNodePatch(editor, operation, beforeValue) {
|
|
868
864
|
const patches$1 = [], block = beforeValue[operation.path[0]], targetBlock = editor.children[operation.path[0]];
|
|
869
865
|
if (operation.path.length === 1)
|
|
870
|
-
if (block
|
|
866
|
+
if (block?._key) {
|
|
871
867
|
const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
|
|
872
868
|
patches$1.push(patches.set(newBlock, [{ _key: newBlock._key }])), patches$1.push(patches.unset([{ _key: block._key }]));
|
|
873
869
|
} else
|
|
@@ -965,7 +961,6 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
965
961
|
}
|
|
966
962
|
},
|
|
967
963
|
insertChild: (type, value) => {
|
|
968
|
-
var _a;
|
|
969
964
|
if (!editor.selection)
|
|
970
965
|
throw new Error("The editor has no selection");
|
|
971
966
|
const [focusBlock] = Array.from(
|
|
@@ -997,14 +992,13 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
997
992
|
return isSpanNode && focusNode._type !== types$1.span.name && (debug$j("Inserting span child next to inline object child, moving selection + 1"), editor.move({ distance: 1, unit: "character" })), slate.Transforms.insertNodes(editor, child, {
|
|
998
993
|
select: !0,
|
|
999
994
|
at: editor.selection
|
|
1000
|
-
}), editor.onChange(),
|
|
995
|
+
}), editor.onChange(), toPortableTextRange(
|
|
1001
996
|
fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1002
997
|
editor.selection,
|
|
1003
998
|
types$1
|
|
1004
|
-
)
|
|
999
|
+
)?.focus.path || [];
|
|
1005
1000
|
},
|
|
1006
1001
|
insertBlock: (type, value) => {
|
|
1007
|
-
var _a, _b, _c;
|
|
1008
1002
|
const block = toSlateValue(
|
|
1009
1003
|
[
|
|
1010
1004
|
{
|
|
@@ -1023,11 +1017,11 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1023
1017
|
reverse: !0
|
|
1024
1018
|
})
|
|
1025
1019
|
)[0];
|
|
1026
|
-
return slate.Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: lastBlock[1] }), editor.onChange(),
|
|
1020
|
+
return slate.Editor.insertNode(editor, block), lastBlock && isEqualToEmptyEditor([lastBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: lastBlock[1] }), editor.onChange(), toPortableTextRange(
|
|
1027
1021
|
fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1028
1022
|
editor.selection,
|
|
1029
1023
|
types$1
|
|
1030
|
-
)
|
|
1024
|
+
)?.focus.path ?? [];
|
|
1031
1025
|
}
|
|
1032
1026
|
const focusBlock = Array.from(
|
|
1033
1027
|
slate.Editor.nodes(editor, {
|
|
@@ -1035,11 +1029,11 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1035
1029
|
match: (n) => n._type === types$1.block.name
|
|
1036
1030
|
})
|
|
1037
1031
|
)[0];
|
|
1038
|
-
return slate.Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: focusBlock[1] }), editor.onChange(),
|
|
1032
|
+
return slate.Editor.insertNode(editor, block), focusBlock && isEqualToEmptyEditor([focusBlock[0]], types$1) && slate.Transforms.removeNodes(editor, { at: focusBlock[1] }), editor.onChange(), toPortableTextRange(
|
|
1039
1033
|
fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
|
|
1040
1034
|
editor.selection,
|
|
1041
1035
|
types$1
|
|
1042
|
-
)
|
|
1036
|
+
)?.focus.path || [];
|
|
1043
1037
|
},
|
|
1044
1038
|
hasBlockStyle: (style) => {
|
|
1045
1039
|
try {
|
|
@@ -1095,7 +1089,6 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1095
1089
|
return node;
|
|
1096
1090
|
},
|
|
1097
1091
|
activeAnnotations: () => {
|
|
1098
|
-
var _a;
|
|
1099
1092
|
if (!editor.selection || editor.selection.focus.path.length < 2)
|
|
1100
1093
|
return [];
|
|
1101
1094
|
try {
|
|
@@ -1105,9 +1098,9 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1105
1098
|
});
|
|
1106
1099
|
for (const [span, path] of spans) {
|
|
1107
1100
|
const [block] = slate.Editor.node(editor, path, { depth: 1 });
|
|
1108
|
-
editor.isTextBlock(block) &&
|
|
1101
|
+
editor.isTextBlock(block) && block.markDefs?.forEach((def) => {
|
|
1109
1102
|
slate.Text.isText(span) && span.marks && Array.isArray(span.marks) && span.marks.includes(def._key) && activeAnnotations.push(def);
|
|
1110
|
-
})
|
|
1103
|
+
});
|
|
1111
1104
|
}
|
|
1112
1105
|
return activeAnnotations;
|
|
1113
1106
|
} catch {
|
|
@@ -1125,27 +1118,16 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1125
1118
|
})
|
|
1126
1119
|
];
|
|
1127
1120
|
if (spans.length === 0 || spans.some(
|
|
1128
|
-
([span]) =>
|
|
1129
|
-
var _a;
|
|
1130
|
-
return !types.isPortableTextSpan(span) || !span.marks || ((_a = span.marks) == null ? void 0 : _a.length) === 0;
|
|
1131
|
-
}
|
|
1121
|
+
([span]) => !types.isPortableTextSpan(span) || !span.marks || span.marks?.length === 0
|
|
1132
1122
|
))
|
|
1133
1123
|
return !1;
|
|
1134
1124
|
const selectionMarkDefs = spans.reduce((accMarkDefs, [, path]) => {
|
|
1135
1125
|
const [block] = slate.Editor.node(editor, path, { depth: 1 });
|
|
1136
1126
|
return editor.isTextBlock(block) && block.markDefs ? [...accMarkDefs, ...block.markDefs] : accMarkDefs;
|
|
1137
1127
|
}, []);
|
|
1138
|
-
return spans.every(([span]) =>
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
const spanMarkDefs = (_a = span.marks) == null ? void 0 : _a.map(
|
|
1142
|
-
(markKey) => {
|
|
1143
|
-
var _a2;
|
|
1144
|
-
return (_a2 = selectionMarkDefs.find((def) => (def == null ? void 0 : def._key) === markKey)) == null ? void 0 : _a2._type;
|
|
1145
|
-
}
|
|
1146
|
-
);
|
|
1147
|
-
return spanMarkDefs == null ? void 0 : spanMarkDefs.includes(annotationType);
|
|
1148
|
-
});
|
|
1128
|
+
return spans.every(([span]) => types.isPortableTextSpan(span) ? span.marks?.map(
|
|
1129
|
+
(markKey) => selectionMarkDefs.find((def) => def?._key === markKey)?._type
|
|
1130
|
+
)?.includes(annotationType) : !1);
|
|
1149
1131
|
} catch {
|
|
1150
1132
|
return !1;
|
|
1151
1133
|
}
|
|
@@ -1205,7 +1187,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1205
1187
|
if (!(range && range.anchor.path.length > 0 && range.focus.path.length > 0))
|
|
1206
1188
|
throw new Error("Invalid range");
|
|
1207
1189
|
if (range) {
|
|
1208
|
-
if (!
|
|
1190
|
+
if (!options?.mode || options?.mode === "selected") {
|
|
1209
1191
|
debug$j("Deleting content in selection"), slate.Transforms.delete(editor, {
|
|
1210
1192
|
at: range,
|
|
1211
1193
|
hanging: !0,
|
|
@@ -1213,11 +1195,11 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
|
|
|
1213
1195
|
}), editor.onChange();
|
|
1214
1196
|
return;
|
|
1215
1197
|
}
|
|
1216
|
-
|
|
1198
|
+
options?.mode === "blocks" && (debug$j("Deleting blocks touched by selection"), slate.Transforms.removeNodes(editor, {
|
|
1217
1199
|
at: range,
|
|
1218
1200
|
voids: !0,
|
|
1219
1201
|
match: (node) => editor.isTextBlock(node) || !editor.isTextBlock(node) && slate.Element.isElement(node)
|
|
1220
|
-
})),
|
|
1202
|
+
})), options?.mode === "children" && (debug$j("Deleting children touched by selection"), slate.Transforms.removeNodes(editor, {
|
|
1221
1203
|
at: range,
|
|
1222
1204
|
voids: !0,
|
|
1223
1205
|
match: (node) => node._type === types$1.span.name || // Text children
|
|
@@ -1316,6 +1298,10 @@ function withPreserveKeys(editor, fn) {
|
|
|
1316
1298
|
const prev = isPreservingKeys(editor);
|
|
1317
1299
|
PRESERVE_KEYS.set(editor, !0), fn(), PRESERVE_KEYS.set(editor, prev);
|
|
1318
1300
|
}
|
|
1301
|
+
function withoutPreserveKeys(editor, fn) {
|
|
1302
|
+
const prev = isPreservingKeys(editor);
|
|
1303
|
+
PRESERVE_KEYS.set(editor, !1), fn(), PRESERVE_KEYS.set(editor, prev);
|
|
1304
|
+
}
|
|
1319
1305
|
function isPreservingKeys(editor) {
|
|
1320
1306
|
return PRESERVE_KEYS.get(editor);
|
|
1321
1307
|
}
|
|
@@ -1326,17 +1312,27 @@ function createWithObjectKeys(schemaTypes, keyGenerator) {
|
|
|
1326
1312
|
return editor.apply = (operation) => {
|
|
1327
1313
|
if (operation.type === "split_node") {
|
|
1328
1314
|
const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.properties);
|
|
1329
|
-
|
|
1330
|
-
...operation
|
|
1331
|
-
|
|
1332
|
-
|
|
1315
|
+
apply2({
|
|
1316
|
+
...operation,
|
|
1317
|
+
properties: {
|
|
1318
|
+
...operation.properties,
|
|
1319
|
+
...withNewKey ? { _key: keyGenerator() } : {}
|
|
1320
|
+
}
|
|
1321
|
+
});
|
|
1322
|
+
return;
|
|
1333
1323
|
}
|
|
1334
1324
|
if (operation.type === "insert_node") {
|
|
1335
1325
|
const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.node);
|
|
1336
|
-
slate.Editor.isEditor(operation.node)
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1326
|
+
if (!slate.Editor.isEditor(operation.node)) {
|
|
1327
|
+
apply2({
|
|
1328
|
+
...operation,
|
|
1329
|
+
node: {
|
|
1330
|
+
...operation.node,
|
|
1331
|
+
...withNewKey ? { _key: keyGenerator() } : {}
|
|
1332
|
+
}
|
|
1333
|
+
});
|
|
1334
|
+
return;
|
|
1335
|
+
}
|
|
1340
1336
|
}
|
|
1341
1337
|
apply2(operation);
|
|
1342
1338
|
}, editor.normalizeNode = (entry) => {
|
|
@@ -2224,7 +2220,7 @@ function findBlockAndChildFromPath(editor, path) {
|
|
|
2224
2220
|
const isMatch = isKeyedSegment(path[2]) ? node._key === path[2]._key : index === path[2];
|
|
2225
2221
|
return isMatch && (childIndex = index), isMatch;
|
|
2226
2222
|
});
|
|
2227
|
-
return child ? { block, child, blockPath, childPath: blockPath
|
|
2223
|
+
return child ? { block, child, blockPath, childPath: blockPath?.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
|
|
2228
2224
|
}
|
|
2229
2225
|
function withRemoteChanges(editor, fn) {
|
|
2230
2226
|
const prev = isChangingRemotely(editor) || !1;
|
|
@@ -2313,11 +2309,12 @@ function createWithUndoRedo(options) {
|
|
|
2313
2309
|
)
|
|
2314
2310
|
);
|
|
2315
2311
|
});
|
|
2312
|
+
const reversedOperations = transformedOperations.map(slate.Operation.inverse).reverse();
|
|
2316
2313
|
try {
|
|
2317
2314
|
slate.Editor.withoutNormalizing(editor, () => {
|
|
2318
2315
|
withPreserveKeys(editor, () => {
|
|
2319
2316
|
withoutSaving(editor, () => {
|
|
2320
|
-
|
|
2317
|
+
reversedOperations.forEach((op) => {
|
|
2321
2318
|
editor.apply(op);
|
|
2322
2319
|
});
|
|
2323
2320
|
});
|
|
@@ -2390,14 +2387,13 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
|
|
|
2390
2387
|
if (patch.type === "diffMatchPatch") {
|
|
2391
2388
|
const operationTargetBlock = findOperationTargetBlock(editor, transformedOperation);
|
|
2392
2389
|
return !operationTargetBlock || !isEqual__default.default({ _key: operationTargetBlock._key }, patch.path[0]) ? [transformedOperation] : (parse(patch.value).forEach((diffPatch) => {
|
|
2393
|
-
var _a, _b, _c, _d;
|
|
2394
2390
|
let adjustOffsetBy = 0, changedOffset = diffPatch.utf8Start1;
|
|
2395
2391
|
const { diffs } = diffPatch;
|
|
2396
2392
|
if (diffs.forEach((diff2, index) => {
|
|
2397
2393
|
const [diffType, text] = diff2;
|
|
2398
2394
|
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));
|
|
2399
2395
|
}), 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") {
|
|
2400
|
-
const currentFocus =
|
|
2396
|
+
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;
|
|
2401
2397
|
(currentFocus && currentAnchor || newFocus && newAnchor) && ([currentFocus, currentAnchor, newFocus, newAnchor].forEach((point) => {
|
|
2402
2398
|
point && changedOffset < point.offset && (point.offset += adjustOffsetBy);
|
|
2403
2399
|
}), currentFocus && currentAnchor && (transformedOperation.properties = {
|
|
@@ -2413,14 +2409,13 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
|
|
|
2413
2409
|
return [transformedOperation];
|
|
2414
2410
|
}
|
|
2415
2411
|
function adjustBlockPath(operation, level, blockIndex) {
|
|
2416
|
-
var _a, _b, _c, _d;
|
|
2417
2412
|
const transformedOperation = { ...operation };
|
|
2418
2413
|
if (blockIndex >= 0 && transformedOperation.type !== "set_selection" && Array.isArray(transformedOperation.path) && transformedOperation.path[0] >= blockIndex + level && transformedOperation.path[0] + level > -1) {
|
|
2419
2414
|
const newPath = [transformedOperation.path[0] + level, ...transformedOperation.path.slice(1)];
|
|
2420
2415
|
transformedOperation.path = newPath;
|
|
2421
2416
|
}
|
|
2422
2417
|
if (transformedOperation.type === "set_selection") {
|
|
2423
|
-
const currentFocus =
|
|
2418
|
+
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;
|
|
2424
2419
|
(currentFocus && currentAnchor || newFocus && newAnchor) && ([currentFocus, currentAnchor, newFocus, newAnchor].forEach((point) => {
|
|
2425
2420
|
point && point.path[0] >= blockIndex + level && point.path[0] + level > -1 && (point.path = [point.path[0] + level, ...point.path.slice(1)]);
|
|
2426
2421
|
}), currentFocus && currentAnchor && (transformedOperation.properties = {
|
|
@@ -2718,6 +2713,21 @@ function createWithPortableTextLists(types2) {
|
|
|
2718
2713
|
}, editor;
|
|
2719
2714
|
};
|
|
2720
2715
|
}
|
|
2716
|
+
function isPortableTextSpan(node) {
|
|
2717
|
+
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"));
|
|
2718
|
+
}
|
|
2719
|
+
function isPortableTextBlock(node) {
|
|
2720
|
+
return (
|
|
2721
|
+
// A block doesn't _have_ to be named 'block' - to differentiate between
|
|
2722
|
+
// allowed child types and marks, one might name them differently
|
|
2723
|
+
typeof node._type == "string" && // Toolkit-types like nested spans are @-prefixed
|
|
2724
|
+
node._type[0] !== "@" && // `markDefs` isn't _required_ per say, but if it's there, it needs to be an array
|
|
2725
|
+
(!("markDefs" in node) || !node.markDefs || Array.isArray(node.markDefs) && // Every mark definition needs to have an `_key` to be mappable in child spans
|
|
2726
|
+
node.markDefs.every((def) => typeof def._key == "string")) && // `children` is required and needs to be an array
|
|
2727
|
+
"children" in node && Array.isArray(node.children) && // All children are objects with `_type` (usually spans, but can contain other stuff)
|
|
2728
|
+
node.children.every((child) => typeof child == "object" && "_type" in child)
|
|
2729
|
+
);
|
|
2730
|
+
}
|
|
2721
2731
|
const debug$c = debugWithName("plugin:withPortableTextMarkModel");
|
|
2722
2732
|
function createWithPortableTextMarkModel(types2, change$) {
|
|
2723
2733
|
return function(editor) {
|
|
@@ -2745,10 +2755,7 @@ function createWithPortableTextMarkModel(types2, change$) {
|
|
|
2745
2755
|
);
|
|
2746
2756
|
if (annotationMarks.length > 0) {
|
|
2747
2757
|
const [block] = slate.Editor.node(editor, slate.Path.parent(path)), orphanedMarks = editor.isTextBlock(block) && annotationMarks.filter(
|
|
2748
|
-
(mark) =>
|
|
2749
|
-
var _a;
|
|
2750
|
-
return !((_a = block.markDefs) != null && _a.find((def) => def._key === mark));
|
|
2751
|
-
}
|
|
2758
|
+
(mark) => !block.markDefs?.find((def) => def._key === mark)
|
|
2752
2759
|
) || [];
|
|
2753
2760
|
orphanedMarks.length > 0 && (debug$c("Removing orphaned .marks from span node"), slate.Transforms.setNodes(
|
|
2754
2761
|
editor,
|
|
@@ -2800,10 +2807,9 @@ function createWithPortableTextMarkModel(types2, change$) {
|
|
|
2800
2807
|
), editor.onChange());
|
|
2801
2808
|
}
|
|
2802
2809
|
}, editor.apply = (op) => {
|
|
2803
|
-
var _a, _b;
|
|
2804
2810
|
if (op.type === "insert_text") {
|
|
2805
2811
|
const { selection } = editor;
|
|
2806
|
-
if (selection && slate.Range.isCollapsed(selection) &&
|
|
2812
|
+
if (selection && slate.Range.isCollapsed(selection) && slate.Editor.marks(editor)?.marks?.some((mark) => !decorators.includes(mark))) {
|
|
2807
2813
|
const [node] = Array.from(
|
|
2808
2814
|
slate.Editor.nodes(editor, {
|
|
2809
2815
|
mode: "lowest",
|
|
@@ -2829,6 +2835,32 @@ function createWithPortableTextMarkModel(types2, change$) {
|
|
|
2829
2835
|
}
|
|
2830
2836
|
}
|
|
2831
2837
|
}
|
|
2838
|
+
if (op.type === "remove_text") {
|
|
2839
|
+
const node = Array.from(
|
|
2840
|
+
slate.Editor.nodes(editor, {
|
|
2841
|
+
mode: "lowest",
|
|
2842
|
+
at: { path: op.path, offset: op.offset },
|
|
2843
|
+
match: (n) => n._type === types2.span.name,
|
|
2844
|
+
voids: !1
|
|
2845
|
+
})
|
|
2846
|
+
)[0][0], block = slate.Editor.node(editor, slate.Path.parent(op.path))[0];
|
|
2847
|
+
if (node && isPortableTextSpan(node) && block && isPortableTextBlock(block)) {
|
|
2848
|
+
const markDefs = block.markDefs ?? [], nodeHasAnnotations = (node.marks ?? []).some(
|
|
2849
|
+
(mark) => markDefs.find((markDef) => markDef._key === mark)
|
|
2850
|
+
), deletingPartOfTheNode = op.offset !== 0, deletingFromTheEnd = op.offset + op.text.length === node.text.length;
|
|
2851
|
+
if (nodeHasAnnotations && deletingPartOfTheNode && deletingFromTheEnd) {
|
|
2852
|
+
slate.Editor.withoutNormalizing(editor, () => {
|
|
2853
|
+
withoutPreserveKeys(editor, () => {
|
|
2854
|
+
slate.Transforms.splitNodes(editor, {
|
|
2855
|
+
match: slate.Text.isText,
|
|
2856
|
+
at: { path: op.path, offset: op.offset }
|
|
2857
|
+
});
|
|
2858
|
+
}), slate.Transforms.removeNodes(editor, { at: slate.Path.next(op.path) });
|
|
2859
|
+
}), editor.onChange();
|
|
2860
|
+
return;
|
|
2861
|
+
}
|
|
2862
|
+
}
|
|
2863
|
+
}
|
|
2832
2864
|
apply2(op);
|
|
2833
2865
|
}, editor.addMark = (mark) => {
|
|
2834
2866
|
if (editor.selection) {
|
|
@@ -2837,10 +2869,7 @@ function createWithPortableTextMarkModel(types2, change$) {
|
|
|
2837
2869
|
const splitTextNodes = [
|
|
2838
2870
|
...slate.Editor.nodes(editor, { at: editor.selection, match: slate.Text.isText })
|
|
2839
2871
|
];
|
|
2840
|
-
if (splitTextNodes.every((node) =>
|
|
2841
|
-
var _a;
|
|
2842
|
-
return (_a = node[0].marks) == null ? void 0 : _a.includes(mark);
|
|
2843
|
-
}))
|
|
2872
|
+
if (splitTextNodes.every((node) => node[0].marks?.includes(mark)))
|
|
2844
2873
|
return editor.removeMark(mark), editor;
|
|
2845
2874
|
slate.Editor.withoutNormalizing(editor, () => {
|
|
2846
2875
|
splitTextNodes.forEach(([node, path]) => {
|
|
@@ -2909,9 +2938,8 @@ function createWithPortableTextMarkModel(types2, change$) {
|
|
|
2909
2938
|
slate.Editor.nodes(editor, { match: slate.Text.isText, at: editor.selection })
|
|
2910
2939
|
);
|
|
2911
2940
|
return slate.Range.isExpanded(editor.selection) ? selectedNodes.every((n) => {
|
|
2912
|
-
var _a;
|
|
2913
2941
|
const [node] = n;
|
|
2914
|
-
return
|
|
2942
|
+
return node.marks?.includes(mark);
|
|
2915
2943
|
}) : ({
|
|
2916
2944
|
...slate.Editor.marks(editor) || {}
|
|
2917
2945
|
}.marks || []).includes(mark);
|
|
@@ -3044,7 +3072,6 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3044
3072
|
const reservedHotkeys = ["enter", "tab", "shift", "delete", "end"], activeHotkeys = hotkeysFromOptions || DEFAULT_HOTKEYS;
|
|
3045
3073
|
return function(editor) {
|
|
3046
3074
|
return editor.pteWithHotKeys = (event) => {
|
|
3047
|
-
var _a, _b, _c, _d, _e, _f;
|
|
3048
3075
|
Object.keys(activeHotkeys).forEach((cat) => {
|
|
3049
3076
|
if (cat === "marks")
|
|
3050
3077
|
for (const hotkey in activeHotkeys[cat]) {
|
|
@@ -3091,7 +3118,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3091
3118
|
}
|
|
3092
3119
|
}
|
|
3093
3120
|
if (isBackspace && editor.selection && editor.selection.focus.path[0] === 0 && slate.Range.isCollapsed(editor.selection)) {
|
|
3094
|
-
const focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1)), nextPath = slate.Path.next(editor.selection.focus.path.slice(0, 1)), nextBlock = slate.Node.has(editor, nextPath), isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 &&
|
|
3121
|
+
const focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1)), nextPath = slate.Path.next(editor.selection.focus.path.slice(0, 1)), nextBlock = slate.Node.has(editor, nextPath), isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
|
|
3095
3122
|
if (nextBlock && isTextBlock && isEmptyFocusBlock) {
|
|
3096
3123
|
event.preventDefault(), event.stopPropagation(), slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), editor.onChange();
|
|
3097
3124
|
return;
|
|
@@ -3101,7 +3128,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3101
3128
|
const prevPath = slate.Path.previous(editor.selection.focus.path.slice(0, 1)), prevBlock = slate.Node.descendant(editor, prevPath), focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
3102
3129
|
if (prevBlock && focusBlock && slate.Editor.isVoid(editor, prevBlock) && editor.selection.focus.offset === 0) {
|
|
3103
3130
|
debug$8("Preventing deleting void block above"), event.preventDefault(), event.stopPropagation();
|
|
3104
|
-
const isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 &&
|
|
3131
|
+
const isTextBlock = types.isPortableTextTextBlock(focusBlock), isEmptyFocusBlock = isTextBlock && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
|
|
3105
3132
|
if (!isTextBlock || isEmptyFocusBlock) {
|
|
3106
3133
|
slate.Transforms.removeNodes(editor, { match: (n) => n === focusBlock }), slate.Transforms.select(editor, prevPath), editor.onChange();
|
|
3107
3134
|
return;
|
|
@@ -3117,7 +3144,7 @@ function createWithHotkeys(types$1, portableTextEditor, hotkeysFromOptions) {
|
|
|
3117
3144
|
const nextBlock = slate.Node.descendant(
|
|
3118
3145
|
editor,
|
|
3119
3146
|
slate.Path.next(editor.selection.focus.path.slice(0, 1))
|
|
3120
|
-
), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath), isEmptyFocusBlock = types.isPortableTextTextBlock(focusBlock) && focusBlock.children.length === 1 &&
|
|
3147
|
+
), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath), isEmptyFocusBlock = types.isPortableTextTextBlock(focusBlock) && focusBlock.children.length === 1 && focusBlock.children?.[0]?.text === "";
|
|
3121
3148
|
if (nextBlock && focusBlock && !slate.Editor.isVoid(editor, focusBlock) && slate.Editor.isVoid(editor, nextBlock) && isEmptyFocusBlock) {
|
|
3122
3149
|
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();
|
|
3123
3150
|
return;
|
|
@@ -3422,13 +3449,7 @@ function validateValue(value, types$1, keyGenerator) {
|
|
|
3422
3449
|
const debug$7 = debugWithName("plugin:withInsertData");
|
|
3423
3450
|
function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
3424
3451
|
return function(editor) {
|
|
3425
|
-
const blockTypeName = schemaTypes.block.name, spanTypeName = schemaTypes.span.name, whitespaceOnPasteMode = schemaTypes.block.options.unstable_whitespaceOnPasteMode, toPlainText = (blocks) => blocks.map((block) => {
|
|
3426
|
-
var _a;
|
|
3427
|
-
return editor.isTextBlock(block) ? block.children.map((child) => {
|
|
3428
|
-
var _a2;
|
|
3429
|
-
return child._type === spanTypeName ? child.text : `[${((_a2 = schemaTypes.inlineObjects.find((t) => t.name === child._type)) == null ? void 0 : _a2.title) || "Object"}]`;
|
|
3430
|
-
}).join("") : `[${((_a = schemaTypes.blockObjects.find((t) => t.name === block._type)) == null ? void 0 : _a.title) || "Object"}]`;
|
|
3431
|
-
}).join(`
|
|
3452
|
+
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(`
|
|
3432
3453
|
|
|
3433
3454
|
`);
|
|
3434
3455
|
return editor.setFragmentData = (data, originEvent) => {
|
|
@@ -3460,7 +3481,6 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3460
3481
|
const fragment = editor.getFragment(), portableText = fromSlateValue(fragment, blockTypeName), asJSON = JSON.stringify(portableText), asPlainText = toPlainText(portableText);
|
|
3461
3482
|
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);
|
|
3462
3483
|
}, editor.insertPortableTextData = (data) => {
|
|
3463
|
-
var _a, _b;
|
|
3464
3484
|
if (!editor.selection)
|
|
3465
3485
|
return !1;
|
|
3466
3486
|
const pText = data.getData("application/x-portable-text"), origin = data.getData("application/x-portable-text-event-origin");
|
|
@@ -3474,8 +3494,8 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3474
3494
|
spanTypeName,
|
|
3475
3495
|
schemaTypes
|
|
3476
3496
|
), validation = validateValue(parsed, schemaTypes, keyGenerator);
|
|
3477
|
-
if (!validation.valid && !
|
|
3478
|
-
const errorDescription = `${
|
|
3497
|
+
if (!validation.valid && !validation.resolution?.autoResolve) {
|
|
3498
|
+
const errorDescription = `${validation.resolution?.description}`;
|
|
3479
3499
|
return change$.next({
|
|
3480
3500
|
type: "error",
|
|
3481
3501
|
level: "warning",
|
|
@@ -3489,7 +3509,6 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3489
3509
|
}
|
|
3490
3510
|
return !1;
|
|
3491
3511
|
}, editor.insertTextOrHTMLData = (data) => {
|
|
3492
|
-
var _a;
|
|
3493
3512
|
if (!editor.selection)
|
|
3494
3513
|
return debug$7("No selection, not inserting"), !1;
|
|
3495
3514
|
change$.next({ type: "loading", isLoading: !0 });
|
|
@@ -3515,7 +3534,7 @@ function createWithInsertData(change$, schemaTypes, keyGenerator) {
|
|
|
3515
3534
|
const validation = validateValue(portableText, schemaTypes, keyGenerator);
|
|
3516
3535
|
if (!validation.valid) {
|
|
3517
3536
|
const errorDescription = `Could not validate the resulting portable text to insert.
|
|
3518
|
-
${
|
|
3537
|
+
${validation.resolution?.description}
|
|
3519
3538
|
Try to insert as plain text (shift-paste) instead.`;
|
|
3520
3539
|
return change$.next({
|
|
3521
3540
|
type: "error",
|
|
@@ -3780,15 +3799,14 @@ function useSyncValue(props) {
|
|
|
3780
3799
|
isChanged = !0;
|
|
3781
3800
|
}
|
|
3782
3801
|
slateValueFromProps.forEach((currentBlock, currentBlockIndex) => {
|
|
3783
|
-
var _a, _b, _c, _d, _e;
|
|
3784
3802
|
const oldBlock = slateEditor.children[currentBlockIndex];
|
|
3785
3803
|
if (oldBlock && !isEqual__default.default(currentBlock, oldBlock) && isValid) {
|
|
3786
3804
|
const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, schemaTypes, keyGenerator);
|
|
3787
|
-
!validation.valid &&
|
|
3788
|
-
`${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${
|
|
3805
|
+
!validation.valid && validation.resolution?.autoResolve && validation.resolution?.patches.length > 0 && !readOnly && previousValue.current && previousValue.current !== value && (console.warn(
|
|
3806
|
+
`${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${validation.resolution?.description}`
|
|
3789
3807
|
), validation.resolution.patches.forEach((patch) => {
|
|
3790
3808
|
change$.next({ type: "patch", patch });
|
|
3791
|
-
})), validation.valid ||
|
|
3809
|
+
})), 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({
|
|
3792
3810
|
type: "invalidValue",
|
|
3793
3811
|
resolution: validation.resolution,
|
|
3794
3812
|
value
|
|
@@ -3799,7 +3817,7 @@ function useSyncValue(props) {
|
|
|
3799
3817
|
debug$5.enabled && debug$5(
|
|
3800
3818
|
"Validating and inserting new block in the end of the value",
|
|
3801
3819
|
currentBlock
|
|
3802
|
-
), validation.valid ||
|
|
3820
|
+
), validation.valid || validation.resolution?.autoResolve ? withPreserveKeys(slateEditor, () => {
|
|
3803
3821
|
slate.Transforms.insertNodes(slateEditor, currentBlock, {
|
|
3804
3822
|
at: [currentBlockIndex]
|
|
3805
3823
|
});
|
|
@@ -3869,9 +3887,9 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
|
|
|
3869
3887
|
}));
|
|
3870
3888
|
}
|
|
3871
3889
|
), currentBlock.children.forEach((currentBlockChild, currentBlockChildIndex) => {
|
|
3872
|
-
const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual__default.default(currentBlockChild, oldBlockChild), isTextChanged = !isEqual__default.default(currentBlockChild.text, oldBlockChild
|
|
3890
|
+
const oldBlockChild = oldBlock.children[currentBlockChildIndex], isChildChanged = !isEqual__default.default(currentBlockChild, oldBlockChild), isTextChanged = !isEqual__default.default(currentBlockChild.text, oldBlockChild?.text), path = [currentBlockIndex, currentBlockChildIndex];
|
|
3873
3891
|
if (isChildChanged)
|
|
3874
|
-
if (currentBlockChild._key ===
|
|
3892
|
+
if (currentBlockChild._key === oldBlockChild?._key) {
|
|
3875
3893
|
debug$5("Updating changed child", currentBlockChild, oldBlockChild), slate.Transforms.setNodes(slateEditor, currentBlockChild, {
|
|
3876
3894
|
at: path
|
|
3877
3895
|
});
|
|
@@ -3987,16 +4005,22 @@ function PortableTextEditorSelectionProvider(props) {
|
|
|
3987
4005
|
};
|
|
3988
4006
|
}, [change$]), /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children });
|
|
3989
4007
|
}
|
|
3990
|
-
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);
|
|
3991
4008
|
const debug$2 = debugWithName("component:PortableTextEditor");
|
|
3992
4009
|
class PortableTextEditor extends react.Component {
|
|
4010
|
+
/**
|
|
4011
|
+
* An observable of all the editor changes.
|
|
4012
|
+
*/
|
|
4013
|
+
change$ = new rxjs.Subject();
|
|
4014
|
+
/**
|
|
4015
|
+
* A lookup table for all the relevant schema types for this portable text type.
|
|
4016
|
+
*/
|
|
4017
|
+
schemaTypes;
|
|
4018
|
+
/**
|
|
4019
|
+
* The editor API (currently implemented with Slate).
|
|
4020
|
+
*/
|
|
4021
|
+
editable;
|
|
3993
4022
|
constructor(props) {
|
|
3994
|
-
if (super(props),
|
|
3995
|
-
this.editable = { ...this.editable, ...editable };
|
|
3996
|
-
}), __publicField(this, "getValue", () => {
|
|
3997
|
-
if (this.editable)
|
|
3998
|
-
return this.editable.getValue();
|
|
3999
|
-
}), !props.schemaType)
|
|
4023
|
+
if (super(props), !props.schemaType)
|
|
4000
4024
|
throw new Error('PortableTextEditor: missing "schemaType" property');
|
|
4001
4025
|
props.incomingPatches$ && console.warn("The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"), this.change$.next({ type: "loading", isLoading: !0 }), this.schemaTypes = getPortableTextMemberSchemaTypes(
|
|
4002
4026
|
props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)
|
|
@@ -4007,6 +4031,13 @@ class PortableTextEditor extends react.Component {
|
|
|
4007
4031
|
this.props.schemaType.hasOwnProperty("jsonType") ? this.props.schemaType : compileType(this.props.schemaType)
|
|
4008
4032
|
)), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this);
|
|
4009
4033
|
}
|
|
4034
|
+
setEditable = (editable) => {
|
|
4035
|
+
this.editable = { ...this.editable, ...editable };
|
|
4036
|
+
};
|
|
4037
|
+
getValue = () => {
|
|
4038
|
+
if (this.editable)
|
|
4039
|
+
return this.editable.getValue();
|
|
4040
|
+
};
|
|
4010
4041
|
render() {
|
|
4011
4042
|
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;
|
|
4012
4043
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
@@ -4032,99 +4063,59 @@ class PortableTextEditor extends react.Component {
|
|
|
4032
4063
|
}
|
|
4033
4064
|
);
|
|
4034
4065
|
}
|
|
4066
|
+
// Static API methods
|
|
4067
|
+
static activeAnnotations = (editor) => editor && editor.editable ? editor.editable.activeAnnotations() : [];
|
|
4068
|
+
static isAnnotationActive = (editor, annotationType) => editor && editor.editable ? editor.editable.isAnnotationActive(annotationType) : !1;
|
|
4069
|
+
static addAnnotation = (editor, type, value) => editor.editable?.addAnnotation(type, value);
|
|
4070
|
+
static blur = (editor) => {
|
|
4071
|
+
debug$2("Host blurred"), editor.editable?.blur();
|
|
4072
|
+
};
|
|
4073
|
+
static delete = (editor, selection, options) => editor.editable?.delete(selection, options);
|
|
4074
|
+
static findDOMNode = (editor, element) => editor.editable?.findDOMNode(element);
|
|
4075
|
+
static findByPath = (editor, path) => editor.editable?.findByPath(path) || [];
|
|
4076
|
+
static focus = (editor) => {
|
|
4077
|
+
debug$2("Host requesting focus"), editor.editable?.focus();
|
|
4078
|
+
};
|
|
4079
|
+
static focusBlock = (editor) => editor.editable?.focusBlock();
|
|
4080
|
+
static focusChild = (editor) => editor.editable?.focusChild();
|
|
4081
|
+
static getSelection = (editor) => editor.editable ? editor.editable.getSelection() : null;
|
|
4082
|
+
static getValue = (editor) => editor.editable?.getValue();
|
|
4083
|
+
static hasBlockStyle = (editor, blockStyle) => editor.editable?.hasBlockStyle(blockStyle);
|
|
4084
|
+
static hasListStyle = (editor, listStyle) => editor.editable?.hasListStyle(listStyle);
|
|
4085
|
+
static isCollapsedSelection = (editor) => editor.editable?.isCollapsedSelection();
|
|
4086
|
+
static isExpandedSelection = (editor) => editor.editable?.isExpandedSelection();
|
|
4087
|
+
static isMarkActive = (editor, mark) => editor.editable?.isMarkActive(mark);
|
|
4088
|
+
static insertChild = (editor, type, value) => (debug$2("Host inserting child"), editor.editable?.insertChild(type, value));
|
|
4089
|
+
static insertBlock = (editor, type, value) => editor.editable?.insertBlock(type, value);
|
|
4090
|
+
static insertBreak = (editor) => editor.editable?.insertBreak();
|
|
4091
|
+
static isVoid = (editor, element) => editor.editable?.isVoid(element);
|
|
4092
|
+
static isObjectPath = (editor, path) => {
|
|
4093
|
+
if (!path || !Array.isArray(path)) return !1;
|
|
4094
|
+
const isChildObjectEditPath = path.length > 3 && path[1] === "children";
|
|
4095
|
+
return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
|
|
4096
|
+
};
|
|
4097
|
+
static marks = (editor) => editor.editable?.marks();
|
|
4098
|
+
static select = (editor, selection) => {
|
|
4099
|
+
debug$2("Host setting selection", selection), editor.editable?.select(selection);
|
|
4100
|
+
};
|
|
4101
|
+
static removeAnnotation = (editor, type) => editor.editable?.removeAnnotation(type);
|
|
4102
|
+
static toggleBlockStyle = (editor, blockStyle) => (debug$2("Host is toggling block style"), editor.editable?.toggleBlockStyle(blockStyle));
|
|
4103
|
+
static toggleList = (editor, listStyle) => editor.editable?.toggleList(listStyle);
|
|
4104
|
+
static toggleMark = (editor, mark) => {
|
|
4105
|
+
debug$2("Host toggling mark", mark), editor.editable?.toggleMark(mark);
|
|
4106
|
+
};
|
|
4107
|
+
static getFragment = (editor) => (debug$2("Host getting fragment"), editor.editable?.getFragment());
|
|
4108
|
+
static undo = (editor) => {
|
|
4109
|
+
debug$2("Host undoing"), editor.editable?.undo();
|
|
4110
|
+
};
|
|
4111
|
+
static redo = (editor) => {
|
|
4112
|
+
debug$2("Host redoing"), editor.editable?.redo();
|
|
4113
|
+
};
|
|
4114
|
+
static isSelectionsOverlapping = (editor, selectionA, selectionB) => editor.editable?.isSelectionsOverlapping(selectionA, selectionB);
|
|
4035
4115
|
}
|
|
4036
|
-
__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) => {
|
|
4037
|
-
var _a;
|
|
4038
|
-
return (_a = editor.editable) == null ? void 0 : _a.addAnnotation(type, value);
|
|
4039
|
-
}), __publicField(PortableTextEditor, "blur", (editor) => {
|
|
4040
|
-
var _a;
|
|
4041
|
-
debug$2("Host blurred"), (_a = editor.editable) == null || _a.blur();
|
|
4042
|
-
}), __publicField(PortableTextEditor, "delete", (editor, selection, options) => {
|
|
4043
|
-
var _a;
|
|
4044
|
-
return (_a = editor.editable) == null ? void 0 : _a.delete(selection, options);
|
|
4045
|
-
}), __publicField(PortableTextEditor, "findDOMNode", (editor, element) => {
|
|
4046
|
-
var _a;
|
|
4047
|
-
return (_a = editor.editable) == null ? void 0 : _a.findDOMNode(element);
|
|
4048
|
-
}), __publicField(PortableTextEditor, "findByPath", (editor, path) => {
|
|
4049
|
-
var _a;
|
|
4050
|
-
return ((_a = editor.editable) == null ? void 0 : _a.findByPath(path)) || [];
|
|
4051
|
-
}), __publicField(PortableTextEditor, "focus", (editor) => {
|
|
4052
|
-
var _a;
|
|
4053
|
-
debug$2("Host requesting focus"), (_a = editor.editable) == null || _a.focus();
|
|
4054
|
-
}), __publicField(PortableTextEditor, "focusBlock", (editor) => {
|
|
4055
|
-
var _a;
|
|
4056
|
-
return (_a = editor.editable) == null ? void 0 : _a.focusBlock();
|
|
4057
|
-
}), __publicField(PortableTextEditor, "focusChild", (editor) => {
|
|
4058
|
-
var _a;
|
|
4059
|
-
return (_a = editor.editable) == null ? void 0 : _a.focusChild();
|
|
4060
|
-
}), __publicField(PortableTextEditor, "getSelection", (editor) => editor.editable ? editor.editable.getSelection() : null), __publicField(PortableTextEditor, "getValue", (editor) => {
|
|
4061
|
-
var _a;
|
|
4062
|
-
return (_a = editor.editable) == null ? void 0 : _a.getValue();
|
|
4063
|
-
}), __publicField(PortableTextEditor, "hasBlockStyle", (editor, blockStyle) => {
|
|
4064
|
-
var _a;
|
|
4065
|
-
return (_a = editor.editable) == null ? void 0 : _a.hasBlockStyle(blockStyle);
|
|
4066
|
-
}), __publicField(PortableTextEditor, "hasListStyle", (editor, listStyle) => {
|
|
4067
|
-
var _a;
|
|
4068
|
-
return (_a = editor.editable) == null ? void 0 : _a.hasListStyle(listStyle);
|
|
4069
|
-
}), __publicField(PortableTextEditor, "isCollapsedSelection", (editor) => {
|
|
4070
|
-
var _a;
|
|
4071
|
-
return (_a = editor.editable) == null ? void 0 : _a.isCollapsedSelection();
|
|
4072
|
-
}), __publicField(PortableTextEditor, "isExpandedSelection", (editor) => {
|
|
4073
|
-
var _a;
|
|
4074
|
-
return (_a = editor.editable) == null ? void 0 : _a.isExpandedSelection();
|
|
4075
|
-
}), __publicField(PortableTextEditor, "isMarkActive", (editor, mark) => {
|
|
4076
|
-
var _a;
|
|
4077
|
-
return (_a = editor.editable) == null ? void 0 : _a.isMarkActive(mark);
|
|
4078
|
-
}), __publicField(PortableTextEditor, "insertChild", (editor, type, value) => {
|
|
4079
|
-
var _a;
|
|
4080
|
-
return debug$2("Host inserting child"), (_a = editor.editable) == null ? void 0 : _a.insertChild(type, value);
|
|
4081
|
-
}), __publicField(PortableTextEditor, "insertBlock", (editor, type, value) => {
|
|
4082
|
-
var _a;
|
|
4083
|
-
return (_a = editor.editable) == null ? void 0 : _a.insertBlock(type, value);
|
|
4084
|
-
}), __publicField(PortableTextEditor, "insertBreak", (editor) => {
|
|
4085
|
-
var _a;
|
|
4086
|
-
return (_a = editor.editable) == null ? void 0 : _a.insertBreak();
|
|
4087
|
-
}), __publicField(PortableTextEditor, "isVoid", (editor, element) => {
|
|
4088
|
-
var _a;
|
|
4089
|
-
return (_a = editor.editable) == null ? void 0 : _a.isVoid(element);
|
|
4090
|
-
}), __publicField(PortableTextEditor, "isObjectPath", (editor, path) => {
|
|
4091
|
-
if (!path || !Array.isArray(path)) return !1;
|
|
4092
|
-
const isChildObjectEditPath = path.length > 3 && path[1] === "children";
|
|
4093
|
-
return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
|
|
4094
|
-
}), __publicField(PortableTextEditor, "marks", (editor) => {
|
|
4095
|
-
var _a;
|
|
4096
|
-
return (_a = editor.editable) == null ? void 0 : _a.marks();
|
|
4097
|
-
}), __publicField(PortableTextEditor, "select", (editor, selection) => {
|
|
4098
|
-
var _a;
|
|
4099
|
-
debug$2("Host setting selection", selection), (_a = editor.editable) == null || _a.select(selection);
|
|
4100
|
-
}), __publicField(PortableTextEditor, "removeAnnotation", (editor, type) => {
|
|
4101
|
-
var _a;
|
|
4102
|
-
return (_a = editor.editable) == null ? void 0 : _a.removeAnnotation(type);
|
|
4103
|
-
}), __publicField(PortableTextEditor, "toggleBlockStyle", (editor, blockStyle) => {
|
|
4104
|
-
var _a;
|
|
4105
|
-
return debug$2("Host is toggling block style"), (_a = editor.editable) == null ? void 0 : _a.toggleBlockStyle(blockStyle);
|
|
4106
|
-
}), __publicField(PortableTextEditor, "toggleList", (editor, listStyle) => {
|
|
4107
|
-
var _a;
|
|
4108
|
-
return (_a = editor.editable) == null ? void 0 : _a.toggleList(listStyle);
|
|
4109
|
-
}), __publicField(PortableTextEditor, "toggleMark", (editor, mark) => {
|
|
4110
|
-
var _a;
|
|
4111
|
-
debug$2("Host toggling mark", mark), (_a = editor.editable) == null || _a.toggleMark(mark);
|
|
4112
|
-
}), __publicField(PortableTextEditor, "getFragment", (editor) => {
|
|
4113
|
-
var _a;
|
|
4114
|
-
return debug$2("Host getting fragment"), (_a = editor.editable) == null ? void 0 : _a.getFragment();
|
|
4115
|
-
}), __publicField(PortableTextEditor, "undo", (editor) => {
|
|
4116
|
-
var _a;
|
|
4117
|
-
debug$2("Host undoing"), (_a = editor.editable) == null || _a.undo();
|
|
4118
|
-
}), __publicField(PortableTextEditor, "redo", (editor) => {
|
|
4119
|
-
var _a;
|
|
4120
|
-
debug$2("Host redoing"), (_a = editor.editable) == null || _a.redo();
|
|
4121
|
-
}), __publicField(PortableTextEditor, "isSelectionsOverlapping", (editor, selectionA, selectionB) => {
|
|
4122
|
-
var _a;
|
|
4123
|
-
return (_a = editor.editable) == null ? void 0 : _a.isSelectionsOverlapping(selectionA, selectionB);
|
|
4124
|
-
});
|
|
4125
4116
|
const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (props) => {
|
|
4126
4117
|
const { attributes, children, leaf, schemaTypes, renderChild, renderDecorator, renderAnnotation } = props, spanRef = react.useRef(null), portableTextEditor = usePortableTextEditor(), blockSelected = slateReact.useSelected(), [focused, setFocused] = react.useState(!1), [selected, setSelected] = react.useState(!1), block = children.props.parent, path = react.useMemo(
|
|
4127
|
-
() => block ? [{ _key: block
|
|
4118
|
+
() => block ? [{ _key: block?._key }, "children", { _key: leaf._key }] : [],
|
|
4128
4119
|
[block, leaf._key]
|
|
4129
4120
|
), decoratorValues = react.useMemo(
|
|
4130
4121
|
() => schemaTypes.decorators.map((dec) => dec.value),
|
|
@@ -4134,10 +4125,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4134
4125
|
[decoratorValues, leaf.marks]
|
|
4135
4126
|
), annotationMarks = Array.isArray(leaf.marks) ? leaf.marks : EMPTY_MARKS, annotations = react.useMemo(
|
|
4136
4127
|
() => annotationMarks.map(
|
|
4137
|
-
(mark) =>
|
|
4138
|
-
var _a;
|
|
4139
|
-
return !decoratorValues.includes(mark) && ((_a = block == null ? void 0 : block.markDefs) == null ? void 0 : _a.find((def) => def._key === mark));
|
|
4140
|
-
}
|
|
4128
|
+
(mark) => !decoratorValues.includes(mark) && block?.markDefs?.find((def) => def._key === mark)
|
|
4141
4129
|
).filter(Boolean),
|
|
4142
4130
|
[annotationMarks, block, decoratorValues]
|
|
4143
4131
|
), shouldTrackSelectionAndFocus = annotations.length > 0 && blockSelected;
|
|
@@ -4448,7 +4436,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4448
4436
|
debug("Pasting normally"), slateEditor.insertData(event.clipboardData);
|
|
4449
4437
|
return;
|
|
4450
4438
|
}
|
|
4451
|
-
const value = PortableTextEditor.getValue(portableTextEditor),
|
|
4439
|
+
const value = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(value, slateEditor.selection, schemaTypes)?.focus.path || [], onPasteResult = onPaste({ event, value, path, schemaTypes });
|
|
4452
4440
|
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) => {
|
|
4453
4441
|
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(
|
|
4454
4442
|
toSlateValue(result.insert, { schemaTypes })
|
|
@@ -4504,7 +4492,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
4504
4492
|
const existingDOMRange = domSelection.getRangeAt(0);
|
|
4505
4493
|
try {
|
|
4506
4494
|
const newDOMRange = slateReact.ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
|
|
4507
|
-
(newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection
|
|
4495
|
+
(newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
|
|
4508
4496
|
} catch {
|
|
4509
4497
|
debug("Could not resolve selection, selecting top document"), slate.Transforms.deselect(slateEditor), slateEditor.children.length > 0 && slate.Transforms.select(slateEditor, [0, 0]), slateEditor.onChange();
|
|
4510
4498
|
}
|