@portabletext/editor 1.12.2 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.mts +234 -14
- package/lib/index.d.ts +234 -14
- package/lib/index.esm.js +361 -81
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +364 -83
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +361 -81
- package/lib/index.mjs.map +1 -1
- package/package.json +10 -7
- package/src/editor/Editable.tsx +60 -5
- package/src/editor/behavior/behavior.action.insert-break.ts +21 -24
- package/src/editor/behavior/behavior.actions.ts +125 -3
- package/src/editor/behavior/behavior.code-editor.ts +86 -0
- package/src/editor/behavior/behavior.core.block-objects.ts +34 -1
- package/src/editor/behavior/behavior.core.ts +2 -0
- package/src/editor/behavior/behavior.links.ts +2 -2
- package/src/editor/behavior/behavior.markdown.ts +1 -1
- package/src/editor/behavior/behavior.types.ts +37 -2
- package/src/editor/behavior/behavior.utils.ts +2 -2
- package/src/editor/editor-machine.ts +5 -2
- package/src/editor/plugins/createWithHotKeys.ts +2 -49
- package/src/index.ts +4 -0
- package/src/utils/is-hotkey.test.ts +112 -0
- package/src/utils/is-hotkey.ts +209 -0
package/lib/index.js
CHANGED
|
@@ -1,15 +1,127 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var types = require("@sanity/types"), schema = require("@sanity/schema"), startCase = require("lodash.startcase"), jsxRuntime = require("react/jsx-runtime"), react = require("@xstate/react"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), React = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), reactCompilerRuntime = require("react-compiler-runtime"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), useEffectEvent = require("use-effect-event"), throttle = require("lodash/throttle.js"), debounce = require("lodash/debounce.js"), patches = require("@portabletext/patches"), flatten = require("lodash/flatten.js"), isPlainObject = require("lodash/isPlainObject.js"), getRandomValues = require("get-random-values-esm"), xstate = require("xstate"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"),
|
|
3
|
+
var types = require("@sanity/types"), schema = require("@sanity/schema"), startCase = require("lodash.startcase"), jsxRuntime = require("react/jsx-runtime"), react = require("@xstate/react"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), React = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), reactCompilerRuntime = require("react-compiler-runtime"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), useEffectEvent = require("use-effect-event"), throttle = require("lodash/throttle.js"), debounce = require("lodash/debounce.js"), patches = require("@portabletext/patches"), flatten = require("lodash/flatten.js"), isPlainObject = require("lodash/isPlainObject.js"), getRandomValues = require("get-random-values-esm"), xstate = require("xstate"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), blockTools = require("@sanity/block-tools");
|
|
4
4
|
function _interopDefaultCompat(e) {
|
|
5
5
|
return e && typeof e == "object" && "default" in e ? e : { default: e };
|
|
6
6
|
}
|
|
7
7
|
var startCase__default = /* @__PURE__ */ _interopDefaultCompat(startCase), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), React__default = /* @__PURE__ */ _interopDefaultCompat(React), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$m), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), throttle__default = /* @__PURE__ */ _interopDefaultCompat(throttle), debounce__default = /* @__PURE__ */ _interopDefaultCompat(debounce), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject), getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy);
|
|
8
|
+
const IS_MAC = typeof window < "u" && /Mac|iPod|iPhone|iPad/.test(window.navigator.userAgent), modifiers = {
|
|
9
|
+
alt: "altKey",
|
|
10
|
+
control: "ctrlKey",
|
|
11
|
+
meta: "metaKey",
|
|
12
|
+
shift: "shiftKey"
|
|
13
|
+
}, aliases = {
|
|
14
|
+
add: "+",
|
|
15
|
+
break: "pause",
|
|
16
|
+
cmd: "meta",
|
|
17
|
+
command: "meta",
|
|
18
|
+
ctl: "control",
|
|
19
|
+
ctrl: "control",
|
|
20
|
+
del: "delete",
|
|
21
|
+
down: "arrowdown",
|
|
22
|
+
esc: "escape",
|
|
23
|
+
ins: "insert",
|
|
24
|
+
left: "arrowleft",
|
|
25
|
+
mod: IS_MAC ? "meta" : "control",
|
|
26
|
+
opt: "alt",
|
|
27
|
+
option: "alt",
|
|
28
|
+
return: "enter",
|
|
29
|
+
right: "arrowright",
|
|
30
|
+
space: " ",
|
|
31
|
+
spacebar: " ",
|
|
32
|
+
up: "arrowup",
|
|
33
|
+
win: "meta",
|
|
34
|
+
windows: "meta"
|
|
35
|
+
}, keyCodes = {
|
|
36
|
+
backspace: 8,
|
|
37
|
+
tab: 9,
|
|
38
|
+
enter: 13,
|
|
39
|
+
shift: 16,
|
|
40
|
+
control: 17,
|
|
41
|
+
alt: 18,
|
|
42
|
+
pause: 19,
|
|
43
|
+
capslock: 20,
|
|
44
|
+
escape: 27,
|
|
45
|
+
" ": 32,
|
|
46
|
+
pageup: 33,
|
|
47
|
+
pagedown: 34,
|
|
48
|
+
end: 35,
|
|
49
|
+
home: 36,
|
|
50
|
+
arrowleft: 37,
|
|
51
|
+
arrowup: 38,
|
|
52
|
+
arrowright: 39,
|
|
53
|
+
arrowdown: 40,
|
|
54
|
+
insert: 45,
|
|
55
|
+
delete: 46,
|
|
56
|
+
meta: 91,
|
|
57
|
+
numlock: 144,
|
|
58
|
+
scrolllock: 145,
|
|
59
|
+
";": 186,
|
|
60
|
+
"=": 187,
|
|
61
|
+
",": 188,
|
|
62
|
+
"-": 189,
|
|
63
|
+
".": 190,
|
|
64
|
+
"/": 191,
|
|
65
|
+
"`": 192,
|
|
66
|
+
"[": 219,
|
|
67
|
+
"\\": 220,
|
|
68
|
+
"]": 221,
|
|
69
|
+
"'": 222,
|
|
70
|
+
f1: 112,
|
|
71
|
+
f2: 113,
|
|
72
|
+
f3: 114,
|
|
73
|
+
f4: 115,
|
|
74
|
+
f5: 116,
|
|
75
|
+
f6: 117,
|
|
76
|
+
f7: 118,
|
|
77
|
+
f8: 119,
|
|
78
|
+
f9: 120,
|
|
79
|
+
f10: 121,
|
|
80
|
+
f11: 122,
|
|
81
|
+
f12: 123,
|
|
82
|
+
f13: 124,
|
|
83
|
+
f14: 125,
|
|
84
|
+
f15: 126,
|
|
85
|
+
f16: 127,
|
|
86
|
+
f17: 128,
|
|
87
|
+
f18: 129,
|
|
88
|
+
f19: 130,
|
|
89
|
+
f20: 131
|
|
90
|
+
};
|
|
91
|
+
function isHotkey(hotkey, event) {
|
|
92
|
+
return compareHotkey(parseHotkey(hotkey), event);
|
|
93
|
+
}
|
|
94
|
+
function parseHotkey(hotkey) {
|
|
95
|
+
const parsedHotkey = {
|
|
96
|
+
altKey: !1,
|
|
97
|
+
ctrlKey: !1,
|
|
98
|
+
metaKey: !1,
|
|
99
|
+
shiftKey: !1
|
|
100
|
+
}, hotkeySegments = hotkey.replace("++", "+add").split("+");
|
|
101
|
+
for (const rawHotkeySegment of hotkeySegments) {
|
|
102
|
+
const optional = rawHotkeySegment.endsWith("?") && rawHotkeySegment.length > 1, hotkeySegment = optional ? rawHotkeySegment.slice(0, -1) : rawHotkeySegment, keyName = toKeyName(hotkeySegment), modifier = modifiers[keyName], alias = aliases[hotkeySegment], code = keyCodes[keyName];
|
|
103
|
+
if (hotkeySegment.length > 1 && modifier === void 0 && alias === void 0 && code === void 0)
|
|
104
|
+
throw new TypeError(`Unknown modifier: "${hotkeySegment}"`);
|
|
105
|
+
(hotkeySegments.length === 1 || modifier === void 0) && (parsedHotkey.key = keyName, parsedHotkey.keyCode = toKeyCode(hotkeySegment)), modifier !== void 0 && (parsedHotkey[modifier] = optional ? null : !0);
|
|
106
|
+
}
|
|
107
|
+
return parsedHotkey;
|
|
108
|
+
}
|
|
109
|
+
function compareHotkey(parsedHotkey, event) {
|
|
110
|
+
return (parsedHotkey.altKey == null || parsedHotkey.altKey === event.altKey) && (parsedHotkey.ctrlKey == null || parsedHotkey.ctrlKey === event.ctrlKey) && (parsedHotkey.metaKey == null || parsedHotkey.metaKey === event.metaKey) && (parsedHotkey.shiftKey == null || parsedHotkey.shiftKey === event.shiftKey) ? parsedHotkey.keyCode !== void 0 && event.keyCode !== void 0 ? parsedHotkey.keyCode === 91 && event.keyCode === 93 ? !0 : parsedHotkey.keyCode === event.keyCode : parsedHotkey.keyCode === event.keyCode || parsedHotkey.key === event.key.toLowerCase() : !1;
|
|
111
|
+
}
|
|
112
|
+
function toKeyCode(name) {
|
|
113
|
+
const keyName = toKeyName(name);
|
|
114
|
+
return keyCodes[keyName] ?? keyName.toUpperCase().charCodeAt(0);
|
|
115
|
+
}
|
|
116
|
+
function toKeyName(name) {
|
|
117
|
+
const keyName = name.toLowerCase();
|
|
118
|
+
return aliases[keyName] ?? keyName;
|
|
119
|
+
}
|
|
8
120
|
function defineBehavior(behavior) {
|
|
9
121
|
return behavior;
|
|
10
122
|
}
|
|
11
123
|
function selectionIsCollapsed(context) {
|
|
12
|
-
return context.selection?.anchor.path
|
|
124
|
+
return JSON.stringify(context.selection?.anchor.path) === JSON.stringify(context.selection?.focus.path) && context.selection?.anchor.offset === context.selection?.focus.offset;
|
|
13
125
|
}
|
|
14
126
|
function getFocusBlock(context) {
|
|
15
127
|
const key = context.selection && types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
|
|
@@ -125,11 +237,42 @@ function isEmptyTextBlock(block) {
|
|
|
125
237
|
function getTextBlockText(block) {
|
|
126
238
|
return block.children.map((child) => child.text ?? "").join("");
|
|
127
239
|
}
|
|
128
|
-
const
|
|
240
|
+
const arrowDownOnLonelyBlockObject = {
|
|
241
|
+
on: "key.down",
|
|
242
|
+
guard: ({
|
|
243
|
+
context,
|
|
244
|
+
event
|
|
245
|
+
}) => {
|
|
246
|
+
const isArrowDown = isHotkey("ArrowDown", event.keyboardEvent), focusBlockObject = getFocusBlockObject(context), nextBlock = getNextBlock(context);
|
|
247
|
+
return isArrowDown && focusBlockObject && !nextBlock;
|
|
248
|
+
},
|
|
249
|
+
actions: [() => [{
|
|
250
|
+
type: "insert text block",
|
|
251
|
+
placement: "after"
|
|
252
|
+
}]]
|
|
253
|
+
}, arrowUpOnLonelyBlockObject = {
|
|
254
|
+
on: "key.down",
|
|
255
|
+
guard: ({
|
|
256
|
+
context,
|
|
257
|
+
event
|
|
258
|
+
}) => {
|
|
259
|
+
const isArrowUp = isHotkey("ArrowUp", event.keyboardEvent), focusBlockObject = getFocusBlockObject(context), previousBlock = getPreviousBlock(context);
|
|
260
|
+
return isArrowUp && focusBlockObject && !previousBlock;
|
|
261
|
+
},
|
|
262
|
+
actions: [() => [{
|
|
263
|
+
type: "insert text block",
|
|
264
|
+
placement: "before"
|
|
265
|
+
}, {
|
|
266
|
+
type: "select previous block"
|
|
267
|
+
}]]
|
|
268
|
+
}, breakingBlockObject = {
|
|
129
269
|
on: "insert break",
|
|
130
270
|
guard: ({
|
|
131
271
|
context
|
|
132
|
-
}) =>
|
|
272
|
+
}) => {
|
|
273
|
+
const focusBlockObject = getFocusBlockObject(context);
|
|
274
|
+
return selectionIsCollapsed(context) && focusBlockObject !== void 0;
|
|
275
|
+
},
|
|
133
276
|
actions: [() => [{
|
|
134
277
|
type: "insert text block",
|
|
135
278
|
placement: "after"
|
|
@@ -195,6 +338,8 @@ const breakingBlockObject = {
|
|
|
195
338
|
}
|
|
196
339
|
}]]
|
|
197
340
|
}, coreBlockObjectBehaviors = {
|
|
341
|
+
arrowDownOnLonelyBlockObject,
|
|
342
|
+
arrowUpOnLonelyBlockObject,
|
|
198
343
|
breakingBlockObject,
|
|
199
344
|
deletingEmptyTextBlockAfterBlockObject,
|
|
200
345
|
deletingEmptyTextBlockBeforeBlockObject
|
|
@@ -278,7 +423,7 @@ const breakingBlockObject = {
|
|
|
278
423
|
text: `
|
|
279
424
|
`
|
|
280
425
|
}]]
|
|
281
|
-
}, coreBehaviors = [softReturn, coreDecoratorBehaviors.decoratorAdd, coreDecoratorBehaviors.decoratorRemove, coreDecoratorBehaviors.decoratorToggle, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace], coreBehavior = {
|
|
426
|
+
}, coreBehaviors = [softReturn, coreDecoratorBehaviors.decoratorAdd, coreDecoratorBehaviors.decoratorRemove, coreDecoratorBehaviors.decoratorToggle, coreBlockObjectBehaviors.arrowDownOnLonelyBlockObject, coreBlockObjectBehaviors.arrowUpOnLonelyBlockObject, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace], coreBehavior = {
|
|
282
427
|
softReturn,
|
|
283
428
|
decorators: coreDecoratorBehaviors,
|
|
284
429
|
blockObjects: coreBlockObjectBehaviors,
|
|
@@ -291,7 +436,7 @@ function createLinkBehaviors(config) {
|
|
|
291
436
|
context,
|
|
292
437
|
event
|
|
293
438
|
}) => {
|
|
294
|
-
const selectionCollapsed = selectionIsCollapsed(context), text = event.
|
|
439
|
+
const selectionCollapsed = selectionIsCollapsed(context), text = event.data.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
|
|
295
440
|
url,
|
|
296
441
|
schema: context.schema
|
|
297
442
|
}) : void 0;
|
|
@@ -314,7 +459,7 @@ function createLinkBehaviors(config) {
|
|
|
314
459
|
const focusSpan = getFocusSpan(context), selectionCollapsed = selectionIsCollapsed(context);
|
|
315
460
|
if (!focusSpan || !selectionCollapsed)
|
|
316
461
|
return !1;
|
|
317
|
-
const text = event.
|
|
462
|
+
const text = event.data.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
|
|
318
463
|
url,
|
|
319
464
|
schema: context.schema
|
|
320
465
|
}) : void 0;
|
|
@@ -590,7 +735,7 @@ function createMarkdownBehaviors(config) {
|
|
|
590
735
|
context,
|
|
591
736
|
event
|
|
592
737
|
}) => {
|
|
593
|
-
const text = event.
|
|
738
|
+
const text = event.data.getData("text/plain"), hrRegExp = /^(---)$|(___)$|(\*\*\*)$/gm, hrCharacters = text.match(hrRegExp)?.[0], hrObject = config.horizontalRuleObject?.({
|
|
594
739
|
schema: context.schema
|
|
595
740
|
}), focusBlock = getFocusBlock(context);
|
|
596
741
|
return !hrCharacters || !hrObject || !focusBlock ? !1 : {
|
|
@@ -786,6 +931,53 @@ function createMarkdownBehaviors(config) {
|
|
|
786
931
|
};
|
|
787
932
|
return [automaticBlockquoteOnSpace, automaticHeadingOnSpace, automaticHr, automaticHrOnPaste, clearStyleOnBackspace, automaticListOnSpace];
|
|
788
933
|
}
|
|
934
|
+
function createCodeEditorBehaviors(config) {
|
|
935
|
+
return [{
|
|
936
|
+
on: "key.down",
|
|
937
|
+
guard: ({
|
|
938
|
+
context,
|
|
939
|
+
event
|
|
940
|
+
}) => {
|
|
941
|
+
if (!isHotkey(config.moveBlockUpShortcut, event.keyboardEvent) || !selectionIsCollapsed(context))
|
|
942
|
+
return !1;
|
|
943
|
+
const focusBlock = getFocusBlock(context), previousBlock = getPreviousBlock(context);
|
|
944
|
+
return focusBlock && previousBlock ? {
|
|
945
|
+
focusBlock,
|
|
946
|
+
previousBlock
|
|
947
|
+
} : !1;
|
|
948
|
+
},
|
|
949
|
+
actions: [(_, {
|
|
950
|
+
focusBlock,
|
|
951
|
+
previousBlock
|
|
952
|
+
}) => [{
|
|
953
|
+
type: "move block",
|
|
954
|
+
blockPath: focusBlock.path,
|
|
955
|
+
to: previousBlock.path
|
|
956
|
+
}]]
|
|
957
|
+
}, {
|
|
958
|
+
on: "key.down",
|
|
959
|
+
guard: ({
|
|
960
|
+
context,
|
|
961
|
+
event
|
|
962
|
+
}) => {
|
|
963
|
+
if (!isHotkey(config.moveBlockDownShortcut, event.keyboardEvent) || !selectionIsCollapsed(context))
|
|
964
|
+
return !1;
|
|
965
|
+
const focusBlock = getFocusBlock(context), nextBlock = getNextBlock(context);
|
|
966
|
+
return focusBlock && nextBlock ? {
|
|
967
|
+
focusBlock,
|
|
968
|
+
nextBlock
|
|
969
|
+
} : !1;
|
|
970
|
+
},
|
|
971
|
+
actions: [(_, {
|
|
972
|
+
focusBlock,
|
|
973
|
+
nextBlock
|
|
974
|
+
}) => [{
|
|
975
|
+
type: "move block",
|
|
976
|
+
blockPath: focusBlock.path,
|
|
977
|
+
to: nextBlock.path
|
|
978
|
+
}]]
|
|
979
|
+
}];
|
|
980
|
+
}
|
|
789
981
|
function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
790
982
|
if (!portableTextType)
|
|
791
983
|
throw new Error("Parameter 'portabletextType' missing (required)");
|
|
@@ -5417,43 +5609,31 @@ const addAnnotationActionImplementation = ({
|
|
|
5417
5609
|
at: editor.selection.focus,
|
|
5418
5610
|
match: (n) => editor.isTextSpan(n),
|
|
5419
5611
|
voids: !1
|
|
5420
|
-
}))[0] ?? [void 0], focusDecorators = focusSpan
|
|
5612
|
+
}))[0] ?? [void 0], focusDecorators = focusSpan?.marks?.filter((mark) => schema2.decorators.some((decorator) => decorator.value === mark)) ?? [], focusAnnotations = focusSpan?.marks?.filter((mark) => !schema2.decorators.some((decorator) => decorator.value === mark)) ?? [], anchorBlockPath = editor.selection.anchor.path.slice(0, 1), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
|
|
5421
5613
|
if (editor.isTextBlock(focusBlock)) {
|
|
5422
|
-
const [start, end] = slate.Range.edges(editor.selection),
|
|
5614
|
+
const [start, end] = slate.Range.edges(editor.selection), lastFocusBlockChild = focusBlock.children[focusBlock.children.length - 1], atTheEndOfBlock = isEqual__default.default(start, {
|
|
5615
|
+
path: [...focusBlockPath, focusBlock.children.length - 1],
|
|
5616
|
+
offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
|
|
5617
|
+
}), atTheStartOfBlock = isEqual__default.default(end, {
|
|
5423
5618
|
path: [...focusBlockPath, 0],
|
|
5424
5619
|
offset: 0
|
|
5425
5620
|
});
|
|
5426
|
-
if (
|
|
5621
|
+
if (atTheEndOfBlock && slate.Range.isCollapsed(editor.selection)) {
|
|
5427
5622
|
slate.Editor.insertNode(editor, editor.pteCreateTextBlock({
|
|
5428
|
-
decorators:
|
|
5623
|
+
decorators: [],
|
|
5429
5624
|
listItem: focusBlock.listItem,
|
|
5430
5625
|
level: focusBlock.level
|
|
5431
5626
|
}));
|
|
5432
|
-
const [nextBlockPath] = slate.Path.next(focusBlockPath);
|
|
5433
|
-
slate.Transforms.select(editor, {
|
|
5434
|
-
anchor: {
|
|
5435
|
-
path: [nextBlockPath, 0],
|
|
5436
|
-
offset: 0
|
|
5437
|
-
},
|
|
5438
|
-
focus: {
|
|
5439
|
-
path: [nextBlockPath, 0],
|
|
5440
|
-
offset: 0
|
|
5441
|
-
}
|
|
5442
|
-
});
|
|
5443
5627
|
return;
|
|
5444
5628
|
}
|
|
5445
|
-
|
|
5446
|
-
path: [...focusBlockPath, focusBlock.children.length - 1],
|
|
5447
|
-
offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
|
|
5448
|
-
});
|
|
5449
|
-
if (atTheEndOfBlock && slate.Range.isCollapsed(editor.selection)) {
|
|
5629
|
+
if (atTheStartOfBlock && slate.Range.isCollapsed(editor.selection)) {
|
|
5450
5630
|
slate.Editor.insertNode(editor, editor.pteCreateTextBlock({
|
|
5451
|
-
decorators: [],
|
|
5631
|
+
decorators: focusAnnotations.length === 0 ? focusDecorators : [],
|
|
5452
5632
|
listItem: focusBlock.listItem,
|
|
5453
5633
|
level: focusBlock.level
|
|
5454
5634
|
}));
|
|
5455
5635
|
const [nextBlockPath] = slate.Path.next(focusBlockPath);
|
|
5456
|
-
slate.Transforms.
|
|
5636
|
+
slate.Transforms.select(editor, {
|
|
5457
5637
|
anchor: {
|
|
5458
5638
|
path: [nextBlockPath, 0],
|
|
5459
5639
|
offset: 0
|
|
@@ -5465,7 +5645,8 @@ const addAnnotationActionImplementation = ({
|
|
|
5465
5645
|
});
|
|
5466
5646
|
return;
|
|
5467
5647
|
}
|
|
5468
|
-
|
|
5648
|
+
const selectionAcrossBlocks = anchorBlockPath[0] !== focusBlockPath[0];
|
|
5649
|
+
if (!atTheStartOfBlock && !atTheEndOfBlock && !selectionAcrossBlocks) {
|
|
5469
5650
|
slate.Editor.withoutNormalizing(editor, () => {
|
|
5470
5651
|
if (!editor.selection)
|
|
5471
5652
|
return;
|
|
@@ -5514,6 +5695,9 @@ const addAnnotationActionImplementation = ({
|
|
|
5514
5695
|
return;
|
|
5515
5696
|
}
|
|
5516
5697
|
}
|
|
5698
|
+
slate.Transforms.splitNodes(editor, {
|
|
5699
|
+
always: !0
|
|
5700
|
+
});
|
|
5517
5701
|
}, insertSoftBreakActionImplementation = ({
|
|
5518
5702
|
context,
|
|
5519
5703
|
action
|
|
@@ -5614,6 +5798,8 @@ const addAnnotationActionImplementation = ({
|
|
|
5614
5798
|
});
|
|
5615
5799
|
}
|
|
5616
5800
|
},
|
|
5801
|
+
copy: () => {
|
|
5802
|
+
},
|
|
5617
5803
|
"delete backward": ({
|
|
5618
5804
|
action
|
|
5619
5805
|
}) => {
|
|
@@ -5713,10 +5899,48 @@ const addAnnotationActionImplementation = ({
|
|
|
5713
5899
|
}) => {
|
|
5714
5900
|
action.effect();
|
|
5715
5901
|
},
|
|
5716
|
-
|
|
5902
|
+
"key.down": () => {
|
|
5903
|
+
},
|
|
5904
|
+
"key.up": () => {
|
|
5905
|
+
},
|
|
5906
|
+
"move block": ({
|
|
5717
5907
|
action
|
|
5718
5908
|
}) => {
|
|
5719
|
-
|
|
5909
|
+
const location = toSlateRange({
|
|
5910
|
+
anchor: {
|
|
5911
|
+
path: action.blockPath,
|
|
5912
|
+
offset: 0
|
|
5913
|
+
},
|
|
5914
|
+
focus: {
|
|
5915
|
+
path: action.blockPath,
|
|
5916
|
+
offset: 0
|
|
5917
|
+
}
|
|
5918
|
+
}, action.editor);
|
|
5919
|
+
if (!location) {
|
|
5920
|
+
console.error("Unable to find Slate range from selection points");
|
|
5921
|
+
return;
|
|
5922
|
+
}
|
|
5923
|
+
const newLocation = toSlateRange({
|
|
5924
|
+
anchor: {
|
|
5925
|
+
path: action.to,
|
|
5926
|
+
offset: 0
|
|
5927
|
+
},
|
|
5928
|
+
focus: {
|
|
5929
|
+
path: action.to,
|
|
5930
|
+
offset: 0
|
|
5931
|
+
}
|
|
5932
|
+
}, action.editor);
|
|
5933
|
+
if (!newLocation) {
|
|
5934
|
+
console.error("Unable to find Slate range from selection points");
|
|
5935
|
+
return;
|
|
5936
|
+
}
|
|
5937
|
+
slate.Transforms.moveNodes(action.editor, {
|
|
5938
|
+
at: location,
|
|
5939
|
+
to: newLocation.anchor.path.slice(0, 1),
|
|
5940
|
+
mode: "highest"
|
|
5941
|
+
});
|
|
5942
|
+
},
|
|
5943
|
+
paste: () => {
|
|
5720
5944
|
},
|
|
5721
5945
|
select: ({
|
|
5722
5946
|
action
|
|
@@ -5724,6 +5948,31 @@ const addAnnotationActionImplementation = ({
|
|
|
5724
5948
|
const newSelection = toSlateRange(action.selection, action.editor);
|
|
5725
5949
|
newSelection ? slate.Transforms.select(action.editor, newSelection) : slate.Transforms.deselect(action.editor);
|
|
5726
5950
|
},
|
|
5951
|
+
"select previous block": ({
|
|
5952
|
+
action
|
|
5953
|
+
}) => {
|
|
5954
|
+
if (!action.editor.selection) {
|
|
5955
|
+
console.error("Unable to select previous block without a selection");
|
|
5956
|
+
return;
|
|
5957
|
+
}
|
|
5958
|
+
const blockPath = action.editor.selection.focus.path.slice(0, 1);
|
|
5959
|
+
if (!slate.Path.hasPrevious(blockPath)) {
|
|
5960
|
+
console.error("There's no previous block to select");
|
|
5961
|
+
return;
|
|
5962
|
+
}
|
|
5963
|
+
const previousBlockPath = slate.Path.previous(blockPath);
|
|
5964
|
+
slate.Transforms.select(action.editor, previousBlockPath);
|
|
5965
|
+
},
|
|
5966
|
+
"select next block": ({
|
|
5967
|
+
action
|
|
5968
|
+
}) => {
|
|
5969
|
+
if (!action.editor.selection) {
|
|
5970
|
+
console.error("Unable to select next block without a selection");
|
|
5971
|
+
return;
|
|
5972
|
+
}
|
|
5973
|
+
const nextBlockPath = [action.editor.selection.focus.path.slice(0, 1)[0] + 1];
|
|
5974
|
+
slate.Transforms.select(action.editor, nextBlockPath);
|
|
5975
|
+
},
|
|
5727
5976
|
reselect: ({
|
|
5728
5977
|
action
|
|
5729
5978
|
}) => {
|
|
@@ -5775,6 +6024,13 @@ function performAction({
|
|
|
5775
6024
|
});
|
|
5776
6025
|
break;
|
|
5777
6026
|
}
|
|
6027
|
+
case "move block": {
|
|
6028
|
+
behaviorActionImplementations["move block"]({
|
|
6029
|
+
context,
|
|
6030
|
+
action
|
|
6031
|
+
});
|
|
6032
|
+
break;
|
|
6033
|
+
}
|
|
5778
6034
|
case "set block": {
|
|
5779
6035
|
behaviorActionImplementations["set block"]({
|
|
5780
6036
|
context,
|
|
@@ -5803,6 +6059,20 @@ function performAction({
|
|
|
5803
6059
|
});
|
|
5804
6060
|
break;
|
|
5805
6061
|
}
|
|
6062
|
+
case "select previous block": {
|
|
6063
|
+
behaviorActionImplementations["select previous block"]({
|
|
6064
|
+
context,
|
|
6065
|
+
action
|
|
6066
|
+
});
|
|
6067
|
+
break;
|
|
6068
|
+
}
|
|
6069
|
+
case "select next block": {
|
|
6070
|
+
behaviorActionImplementations["select next block"]({
|
|
6071
|
+
context,
|
|
6072
|
+
action
|
|
6073
|
+
});
|
|
6074
|
+
break;
|
|
6075
|
+
}
|
|
5806
6076
|
case "reselect": {
|
|
5807
6077
|
behaviorActionImplementations.reselect({
|
|
5808
6078
|
context,
|
|
@@ -5843,6 +6113,8 @@ function performDefaultAction({
|
|
|
5843
6113
|
});
|
|
5844
6114
|
break;
|
|
5845
6115
|
}
|
|
6116
|
+
case "copy":
|
|
6117
|
+
break;
|
|
5846
6118
|
case "decorator.add": {
|
|
5847
6119
|
behaviorActionImplementations["decorator.add"]({
|
|
5848
6120
|
context,
|
|
@@ -5906,11 +6178,6 @@ function performDefaultAction({
|
|
|
5906
6178
|
});
|
|
5907
6179
|
break;
|
|
5908
6180
|
}
|
|
5909
|
-
default:
|
|
5910
|
-
behaviorActionImplementations.paste({
|
|
5911
|
-
context,
|
|
5912
|
-
action
|
|
5913
|
-
});
|
|
5914
6181
|
}
|
|
5915
6182
|
}
|
|
5916
6183
|
const editorMachine = xstate.setup({
|
|
@@ -5987,10 +6254,10 @@ const editorMachine = xstate.setup({
|
|
|
5987
6254
|
};
|
|
5988
6255
|
let behaviorOverwritten = !1;
|
|
5989
6256
|
for (const eventBehavior of eventBehaviors) {
|
|
5990
|
-
const shouldRun = eventBehavior.guard
|
|
6257
|
+
const shouldRun = eventBehavior.guard === void 0 || eventBehavior.guard({
|
|
5991
6258
|
context: behaviorContext,
|
|
5992
6259
|
event: event.behaviorEvent
|
|
5993
|
-
})
|
|
6260
|
+
});
|
|
5994
6261
|
if (!shouldRun)
|
|
5995
6262
|
continue;
|
|
5996
6263
|
const actionIntendSets = eventBehavior.actions.map((actionSet) => actionSet({
|
|
@@ -6003,8 +6270,10 @@ const editorMachine = xstate.setup({
|
|
|
6003
6270
|
editor: event.editor,
|
|
6004
6271
|
actionIntends
|
|
6005
6272
|
});
|
|
6006
|
-
if (behaviorOverwritten)
|
|
6273
|
+
if (behaviorOverwritten) {
|
|
6274
|
+
event.nativeEvent?.preventDefault();
|
|
6007
6275
|
break;
|
|
6276
|
+
}
|
|
6008
6277
|
}
|
|
6009
6278
|
behaviorOverwritten || enqueue.raise({
|
|
6010
6279
|
type: "behavior action intends",
|
|
@@ -6621,7 +6890,7 @@ function createWithHotkeys(editorActor, portableTextEditor, hotkeysFromOptions)
|
|
|
6621
6890
|
for (const hotkey in activeHotkeys[cat]) {
|
|
6622
6891
|
if (reservedHotkeys.includes(hotkey))
|
|
6623
6892
|
throw new Error(`The hotkey ${hotkey} is reserved!`);
|
|
6624
|
-
if (
|
|
6893
|
+
if (isHotkey(hotkey, event.nativeEvent)) {
|
|
6625
6894
|
event.preventDefault();
|
|
6626
6895
|
const possibleMark = activeHotkeys[cat];
|
|
6627
6896
|
if (possibleMark) {
|
|
@@ -6641,7 +6910,7 @@ function createWithHotkeys(editorActor, portableTextEditor, hotkeysFromOptions)
|
|
|
6641
6910
|
for (const hotkey in activeHotkeys[cat]) {
|
|
6642
6911
|
if (reservedHotkeys.includes(hotkey))
|
|
6643
6912
|
throw new Error(`The hotkey ${hotkey} is reserved!`);
|
|
6644
|
-
if (
|
|
6913
|
+
if (isHotkey(hotkey, event.nativeEvent)) {
|
|
6645
6914
|
const possibleCommand = activeHotkeys[cat];
|
|
6646
6915
|
if (possibleCommand) {
|
|
6647
6916
|
const command = possibleCommand[hotkey];
|
|
@@ -6650,38 +6919,7 @@ function createWithHotkeys(editorActor, portableTextEditor, hotkeysFromOptions)
|
|
|
6650
6919
|
}
|
|
6651
6920
|
}
|
|
6652
6921
|
});
|
|
6653
|
-
const isEnter =
|
|
6654
|
-
if (isArrowDown && editor.selection) {
|
|
6655
|
-
const focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
6656
|
-
if (focusBlock && slate.Editor.isVoid(editor, focusBlock)) {
|
|
6657
|
-
const nextPath = slate.Path.next(editor.selection.focus.path.slice(0, 1));
|
|
6658
|
-
if (!slate.Node.has(editor, nextPath)) {
|
|
6659
|
-
slate.Transforms.insertNodes(editor, editor.pteCreateTextBlock({
|
|
6660
|
-
decorators: []
|
|
6661
|
-
}), {
|
|
6662
|
-
at: nextPath
|
|
6663
|
-
}), slate.Transforms.select(editor, {
|
|
6664
|
-
path: [...nextPath, 0],
|
|
6665
|
-
offset: 0
|
|
6666
|
-
}), editor.onChange();
|
|
6667
|
-
return;
|
|
6668
|
-
}
|
|
6669
|
-
}
|
|
6670
|
-
}
|
|
6671
|
-
if (isArrowUp && editor.selection) {
|
|
6672
|
-
const isFirstBlock = editor.selection.focus.path[0] === 0, focusBlock = slate.Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
6673
|
-
if (isFirstBlock && focusBlock && slate.Editor.isVoid(editor, focusBlock)) {
|
|
6674
|
-
slate.Transforms.insertNodes(editor, editor.pteCreateTextBlock({
|
|
6675
|
-
decorators: []
|
|
6676
|
-
}), {
|
|
6677
|
-
at: [0]
|
|
6678
|
-
}), slate.Transforms.select(editor, {
|
|
6679
|
-
path: [0, 0],
|
|
6680
|
-
offset: 0
|
|
6681
|
-
}), editor.onChange();
|
|
6682
|
-
return;
|
|
6683
|
-
}
|
|
6684
|
-
}
|
|
6922
|
+
const isEnter = isHotkey("enter", event.nativeEvent), isTab = isHotkey("tab", event.nativeEvent), isShiftEnter = isHotkey("shift+enter", event.nativeEvent), isShiftTab = isHotkey("shift+tab", event.nativeEvent);
|
|
6685
6923
|
if ((isTab || isShiftTab) && editor.selection) {
|
|
6686
6924
|
const [focusChild] = slate.Editor.node(editor, editor.selection.focus, {
|
|
6687
6925
|
depth: 2
|
|
@@ -7028,8 +7266,16 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7028
7266
|
return () => teardown();
|
|
7029
7267
|
}, [slateEditor, syncRangeDecorations]);
|
|
7030
7268
|
const handleCopy = React.useCallback((event) => {
|
|
7031
|
-
onCopy
|
|
7032
|
-
|
|
7269
|
+
onCopy ? onCopy(event) !== void 0 && event.preventDefault() : event.nativeEvent.clipboardData && editorActor.send({
|
|
7270
|
+
type: "behavior event",
|
|
7271
|
+
behaviorEvent: {
|
|
7272
|
+
type: "copy",
|
|
7273
|
+
data: event.nativeEvent.clipboardData
|
|
7274
|
+
},
|
|
7275
|
+
editor: slateEditor,
|
|
7276
|
+
nativeEvent: event
|
|
7277
|
+
});
|
|
7278
|
+
}, [onCopy, editorActor, slateEditor]), handlePaste = React.useCallback((event_0) => {
|
|
7033
7279
|
const value_0 = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(value_0, slateEditor.selection, schemaTypes)?.focus.path || [], onPasteResult = onPaste?.({
|
|
7034
7280
|
event: event_0,
|
|
7035
7281
|
value: value_0,
|
|
@@ -7046,14 +7292,15 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7046
7292
|
editorActor.send({
|
|
7047
7293
|
type: "done loading"
|
|
7048
7294
|
});
|
|
7049
|
-
})) : event_0.nativeEvent.clipboardData &&
|
|
7295
|
+
})) : event_0.nativeEvent.clipboardData && editorActor.send({
|
|
7050
7296
|
type: "behavior event",
|
|
7051
7297
|
behaviorEvent: {
|
|
7052
7298
|
type: "paste",
|
|
7053
|
-
|
|
7299
|
+
data: event_0.nativeEvent.clipboardData
|
|
7054
7300
|
},
|
|
7055
|
-
editor: slateEditor
|
|
7056
|
-
|
|
7301
|
+
editor: slateEditor,
|
|
7302
|
+
nativeEvent: event_0
|
|
7303
|
+
}), debug("No result from custom paste handler, pasting normally");
|
|
7057
7304
|
}, [editorActor, onPaste, portableTextEditor, schemaTypes, slateEditor]), handleOnFocus = React.useCallback((event_1) => {
|
|
7058
7305
|
if (onFocus && onFocus(event_1), !event_1.isDefaultPrevented()) {
|
|
7059
7306
|
const selection = PortableTextEditor.getSelection(portableTextEditor);
|
|
@@ -7114,8 +7361,40 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7114
7361
|
}
|
|
7115
7362
|
}, [validateSelection, editableElement]);
|
|
7116
7363
|
const handleKeyDown = React.useCallback((event_5) => {
|
|
7117
|
-
props.onKeyDown && props.onKeyDown(event_5), event_5.isDefaultPrevented() || slateEditor.pteWithHotKeys(event_5)
|
|
7118
|
-
|
|
7364
|
+
props.onKeyDown && props.onKeyDown(event_5), event_5.isDefaultPrevented() || slateEditor.pteWithHotKeys(event_5), event_5.isDefaultPrevented() || editorActor.send({
|
|
7365
|
+
type: "behavior event",
|
|
7366
|
+
behaviorEvent: {
|
|
7367
|
+
type: "key.down",
|
|
7368
|
+
keyboardEvent: {
|
|
7369
|
+
key: event_5.key,
|
|
7370
|
+
code: event_5.code,
|
|
7371
|
+
altKey: event_5.altKey,
|
|
7372
|
+
ctrlKey: event_5.ctrlKey,
|
|
7373
|
+
metaKey: event_5.metaKey,
|
|
7374
|
+
shiftKey: event_5.shiftKey
|
|
7375
|
+
}
|
|
7376
|
+
},
|
|
7377
|
+
editor: slateEditor,
|
|
7378
|
+
nativeEvent: event_5
|
|
7379
|
+
});
|
|
7380
|
+
}, [props, editorActor, slateEditor]), handleKeyUp = React.useCallback((event_6) => {
|
|
7381
|
+
props.onKeyUp && props.onKeyUp(event_6), event_6.isDefaultPrevented() || editorActor.send({
|
|
7382
|
+
type: "behavior event",
|
|
7383
|
+
behaviorEvent: {
|
|
7384
|
+
type: "key.up",
|
|
7385
|
+
keyboardEvent: {
|
|
7386
|
+
key: event_6.key,
|
|
7387
|
+
code: event_6.code,
|
|
7388
|
+
altKey: event_6.altKey,
|
|
7389
|
+
ctrlKey: event_6.ctrlKey,
|
|
7390
|
+
metaKey: event_6.metaKey,
|
|
7391
|
+
shiftKey: event_6.shiftKey
|
|
7392
|
+
}
|
|
7393
|
+
},
|
|
7394
|
+
editor: slateEditor,
|
|
7395
|
+
nativeEvent: event_6
|
|
7396
|
+
});
|
|
7397
|
+
}, [props, editorActor, slateEditor]), scrollSelectionIntoViewToSlate = React.useMemo(() => {
|
|
7119
7398
|
if (scrollSelectionIntoView !== void 0)
|
|
7120
7399
|
return scrollSelectionIntoView === null ? noop__default.default : (_editor, domRange) => {
|
|
7121
7400
|
scrollSelectionIntoView(portableTextEditor, domRange);
|
|
@@ -7162,6 +7441,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7162
7441
|
onDOMBeforeInput: handleOnBeforeInput,
|
|
7163
7442
|
onFocus: handleOnFocus,
|
|
7164
7443
|
onKeyDown: handleKeyDown,
|
|
7444
|
+
onKeyUp: handleKeyUp,
|
|
7165
7445
|
onPaste: handlePaste,
|
|
7166
7446
|
readOnly,
|
|
7167
7447
|
renderPlaceholder: void 0,
|
|
@@ -7225,6 +7505,7 @@ exports.PortableTextEditable = PortableTextEditable;
|
|
|
7225
7505
|
exports.PortableTextEditor = PortableTextEditor;
|
|
7226
7506
|
exports.coreBehavior = coreBehavior;
|
|
7227
7507
|
exports.coreBehaviors = coreBehaviors;
|
|
7508
|
+
exports.createCodeEditorBehaviors = createCodeEditorBehaviors;
|
|
7228
7509
|
exports.createLinkBehaviors = createLinkBehaviors;
|
|
7229
7510
|
exports.createMarkdownBehaviors = createMarkdownBehaviors;
|
|
7230
7511
|
exports.defineBehavior = defineBehavior;
|