@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.mjs
CHANGED
|
@@ -24,13 +24,124 @@ import { setup, assign, assertEvent, emit, enqueueActions, createActor } from "x
|
|
|
24
24
|
import get from "lodash/get.js";
|
|
25
25
|
import isUndefined from "lodash/isUndefined.js";
|
|
26
26
|
import omitBy from "lodash/omitBy.js";
|
|
27
|
-
import { isHotkey } from "is-hotkey-esm";
|
|
28
27
|
import { htmlToBlocks, normalizeBlock } from "@sanity/block-tools";
|
|
28
|
+
const IS_MAC = typeof window < "u" && /Mac|iPod|iPhone|iPad/.test(window.navigator.userAgent), modifiers = {
|
|
29
|
+
alt: "altKey",
|
|
30
|
+
control: "ctrlKey",
|
|
31
|
+
meta: "metaKey",
|
|
32
|
+
shift: "shiftKey"
|
|
33
|
+
}, aliases = {
|
|
34
|
+
add: "+",
|
|
35
|
+
break: "pause",
|
|
36
|
+
cmd: "meta",
|
|
37
|
+
command: "meta",
|
|
38
|
+
ctl: "control",
|
|
39
|
+
ctrl: "control",
|
|
40
|
+
del: "delete",
|
|
41
|
+
down: "arrowdown",
|
|
42
|
+
esc: "escape",
|
|
43
|
+
ins: "insert",
|
|
44
|
+
left: "arrowleft",
|
|
45
|
+
mod: IS_MAC ? "meta" : "control",
|
|
46
|
+
opt: "alt",
|
|
47
|
+
option: "alt",
|
|
48
|
+
return: "enter",
|
|
49
|
+
right: "arrowright",
|
|
50
|
+
space: " ",
|
|
51
|
+
spacebar: " ",
|
|
52
|
+
up: "arrowup",
|
|
53
|
+
win: "meta",
|
|
54
|
+
windows: "meta"
|
|
55
|
+
}, keyCodes = {
|
|
56
|
+
backspace: 8,
|
|
57
|
+
tab: 9,
|
|
58
|
+
enter: 13,
|
|
59
|
+
shift: 16,
|
|
60
|
+
control: 17,
|
|
61
|
+
alt: 18,
|
|
62
|
+
pause: 19,
|
|
63
|
+
capslock: 20,
|
|
64
|
+
escape: 27,
|
|
65
|
+
" ": 32,
|
|
66
|
+
pageup: 33,
|
|
67
|
+
pagedown: 34,
|
|
68
|
+
end: 35,
|
|
69
|
+
home: 36,
|
|
70
|
+
arrowleft: 37,
|
|
71
|
+
arrowup: 38,
|
|
72
|
+
arrowright: 39,
|
|
73
|
+
arrowdown: 40,
|
|
74
|
+
insert: 45,
|
|
75
|
+
delete: 46,
|
|
76
|
+
meta: 91,
|
|
77
|
+
numlock: 144,
|
|
78
|
+
scrolllock: 145,
|
|
79
|
+
";": 186,
|
|
80
|
+
"=": 187,
|
|
81
|
+
",": 188,
|
|
82
|
+
"-": 189,
|
|
83
|
+
".": 190,
|
|
84
|
+
"/": 191,
|
|
85
|
+
"`": 192,
|
|
86
|
+
"[": 219,
|
|
87
|
+
"\\": 220,
|
|
88
|
+
"]": 221,
|
|
89
|
+
"'": 222,
|
|
90
|
+
f1: 112,
|
|
91
|
+
f2: 113,
|
|
92
|
+
f3: 114,
|
|
93
|
+
f4: 115,
|
|
94
|
+
f5: 116,
|
|
95
|
+
f6: 117,
|
|
96
|
+
f7: 118,
|
|
97
|
+
f8: 119,
|
|
98
|
+
f9: 120,
|
|
99
|
+
f10: 121,
|
|
100
|
+
f11: 122,
|
|
101
|
+
f12: 123,
|
|
102
|
+
f13: 124,
|
|
103
|
+
f14: 125,
|
|
104
|
+
f15: 126,
|
|
105
|
+
f16: 127,
|
|
106
|
+
f17: 128,
|
|
107
|
+
f18: 129,
|
|
108
|
+
f19: 130,
|
|
109
|
+
f20: 131
|
|
110
|
+
};
|
|
111
|
+
function isHotkey(hotkey, event) {
|
|
112
|
+
return compareHotkey(parseHotkey(hotkey), event);
|
|
113
|
+
}
|
|
114
|
+
function parseHotkey(hotkey) {
|
|
115
|
+
const parsedHotkey = {
|
|
116
|
+
altKey: !1,
|
|
117
|
+
ctrlKey: !1,
|
|
118
|
+
metaKey: !1,
|
|
119
|
+
shiftKey: !1
|
|
120
|
+
}, hotkeySegments = hotkey.replace("++", "+add").split("+");
|
|
121
|
+
for (const rawHotkeySegment of hotkeySegments) {
|
|
122
|
+
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];
|
|
123
|
+
if (hotkeySegment.length > 1 && modifier === void 0 && alias === void 0 && code === void 0)
|
|
124
|
+
throw new TypeError(`Unknown modifier: "${hotkeySegment}"`);
|
|
125
|
+
(hotkeySegments.length === 1 || modifier === void 0) && (parsedHotkey.key = keyName, parsedHotkey.keyCode = toKeyCode(hotkeySegment)), modifier !== void 0 && (parsedHotkey[modifier] = optional ? null : !0);
|
|
126
|
+
}
|
|
127
|
+
return parsedHotkey;
|
|
128
|
+
}
|
|
129
|
+
function compareHotkey(parsedHotkey, event) {
|
|
130
|
+
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;
|
|
131
|
+
}
|
|
132
|
+
function toKeyCode(name) {
|
|
133
|
+
const keyName = toKeyName(name);
|
|
134
|
+
return keyCodes[keyName] ?? keyName.toUpperCase().charCodeAt(0);
|
|
135
|
+
}
|
|
136
|
+
function toKeyName(name) {
|
|
137
|
+
const keyName = name.toLowerCase();
|
|
138
|
+
return aliases[keyName] ?? keyName;
|
|
139
|
+
}
|
|
29
140
|
function defineBehavior(behavior) {
|
|
30
141
|
return behavior;
|
|
31
142
|
}
|
|
32
143
|
function selectionIsCollapsed(context) {
|
|
33
|
-
return context.selection?.anchor.path
|
|
144
|
+
return JSON.stringify(context.selection?.anchor.path) === JSON.stringify(context.selection?.focus.path) && context.selection?.anchor.offset === context.selection?.focus.offset;
|
|
34
145
|
}
|
|
35
146
|
function getFocusBlock(context) {
|
|
36
147
|
const key = context.selection && 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;
|
|
@@ -146,11 +257,42 @@ function isEmptyTextBlock(block) {
|
|
|
146
257
|
function getTextBlockText(block) {
|
|
147
258
|
return block.children.map((child) => child.text ?? "").join("");
|
|
148
259
|
}
|
|
149
|
-
const
|
|
260
|
+
const arrowDownOnLonelyBlockObject = {
|
|
261
|
+
on: "key.down",
|
|
262
|
+
guard: ({
|
|
263
|
+
context,
|
|
264
|
+
event
|
|
265
|
+
}) => {
|
|
266
|
+
const isArrowDown = isHotkey("ArrowDown", event.keyboardEvent), focusBlockObject = getFocusBlockObject(context), nextBlock = getNextBlock(context);
|
|
267
|
+
return isArrowDown && focusBlockObject && !nextBlock;
|
|
268
|
+
},
|
|
269
|
+
actions: [() => [{
|
|
270
|
+
type: "insert text block",
|
|
271
|
+
placement: "after"
|
|
272
|
+
}]]
|
|
273
|
+
}, arrowUpOnLonelyBlockObject = {
|
|
274
|
+
on: "key.down",
|
|
275
|
+
guard: ({
|
|
276
|
+
context,
|
|
277
|
+
event
|
|
278
|
+
}) => {
|
|
279
|
+
const isArrowUp = isHotkey("ArrowUp", event.keyboardEvent), focusBlockObject = getFocusBlockObject(context), previousBlock = getPreviousBlock(context);
|
|
280
|
+
return isArrowUp && focusBlockObject && !previousBlock;
|
|
281
|
+
},
|
|
282
|
+
actions: [() => [{
|
|
283
|
+
type: "insert text block",
|
|
284
|
+
placement: "before"
|
|
285
|
+
}, {
|
|
286
|
+
type: "select previous block"
|
|
287
|
+
}]]
|
|
288
|
+
}, breakingBlockObject = {
|
|
150
289
|
on: "insert break",
|
|
151
290
|
guard: ({
|
|
152
291
|
context
|
|
153
|
-
}) =>
|
|
292
|
+
}) => {
|
|
293
|
+
const focusBlockObject = getFocusBlockObject(context);
|
|
294
|
+
return selectionIsCollapsed(context) && focusBlockObject !== void 0;
|
|
295
|
+
},
|
|
154
296
|
actions: [() => [{
|
|
155
297
|
type: "insert text block",
|
|
156
298
|
placement: "after"
|
|
@@ -216,6 +358,8 @@ const breakingBlockObject = {
|
|
|
216
358
|
}
|
|
217
359
|
}]]
|
|
218
360
|
}, coreBlockObjectBehaviors = {
|
|
361
|
+
arrowDownOnLonelyBlockObject,
|
|
362
|
+
arrowUpOnLonelyBlockObject,
|
|
219
363
|
breakingBlockObject,
|
|
220
364
|
deletingEmptyTextBlockAfterBlockObject,
|
|
221
365
|
deletingEmptyTextBlockBeforeBlockObject
|
|
@@ -299,7 +443,7 @@ const breakingBlockObject = {
|
|
|
299
443
|
text: `
|
|
300
444
|
`
|
|
301
445
|
}]]
|
|
302
|
-
}, coreBehaviors = [softReturn, coreDecoratorBehaviors.decoratorAdd, coreDecoratorBehaviors.decoratorRemove, coreDecoratorBehaviors.decoratorToggle, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace], coreBehavior = {
|
|
446
|
+
}, coreBehaviors = [softReturn, coreDecoratorBehaviors.decoratorAdd, coreDecoratorBehaviors.decoratorRemove, coreDecoratorBehaviors.decoratorToggle, coreBlockObjectBehaviors.arrowDownOnLonelyBlockObject, coreBlockObjectBehaviors.arrowUpOnLonelyBlockObject, coreBlockObjectBehaviors.breakingBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockAfterBlockObject, coreBlockObjectBehaviors.deletingEmptyTextBlockBeforeBlockObject, coreListBehaviors.clearListOnBackspace, coreListBehaviors.unindentListOnBackspace], coreBehavior = {
|
|
303
447
|
softReturn,
|
|
304
448
|
decorators: coreDecoratorBehaviors,
|
|
305
449
|
blockObjects: coreBlockObjectBehaviors,
|
|
@@ -312,7 +456,7 @@ function createLinkBehaviors(config) {
|
|
|
312
456
|
context,
|
|
313
457
|
event
|
|
314
458
|
}) => {
|
|
315
|
-
const selectionCollapsed = selectionIsCollapsed(context), text = event.
|
|
459
|
+
const selectionCollapsed = selectionIsCollapsed(context), text = event.data.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
|
|
316
460
|
url,
|
|
317
461
|
schema: context.schema
|
|
318
462
|
}) : void 0;
|
|
@@ -335,7 +479,7 @@ function createLinkBehaviors(config) {
|
|
|
335
479
|
const focusSpan = getFocusSpan(context), selectionCollapsed = selectionIsCollapsed(context);
|
|
336
480
|
if (!focusSpan || !selectionCollapsed)
|
|
337
481
|
return !1;
|
|
338
|
-
const text = event.
|
|
482
|
+
const text = event.data.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
|
|
339
483
|
url,
|
|
340
484
|
schema: context.schema
|
|
341
485
|
}) : void 0;
|
|
@@ -611,7 +755,7 @@ function createMarkdownBehaviors(config) {
|
|
|
611
755
|
context,
|
|
612
756
|
event
|
|
613
757
|
}) => {
|
|
614
|
-
const text = event.
|
|
758
|
+
const text = event.data.getData("text/plain"), hrRegExp = /^(---)$|(___)$|(\*\*\*)$/gm, hrCharacters = text.match(hrRegExp)?.[0], hrObject = config.horizontalRuleObject?.({
|
|
615
759
|
schema: context.schema
|
|
616
760
|
}), focusBlock = getFocusBlock(context);
|
|
617
761
|
return !hrCharacters || !hrObject || !focusBlock ? !1 : {
|
|
@@ -807,6 +951,53 @@ function createMarkdownBehaviors(config) {
|
|
|
807
951
|
};
|
|
808
952
|
return [automaticBlockquoteOnSpace, automaticHeadingOnSpace, automaticHr, automaticHrOnPaste, clearStyleOnBackspace, automaticListOnSpace];
|
|
809
953
|
}
|
|
954
|
+
function createCodeEditorBehaviors(config) {
|
|
955
|
+
return [{
|
|
956
|
+
on: "key.down",
|
|
957
|
+
guard: ({
|
|
958
|
+
context,
|
|
959
|
+
event
|
|
960
|
+
}) => {
|
|
961
|
+
if (!isHotkey(config.moveBlockUpShortcut, event.keyboardEvent) || !selectionIsCollapsed(context))
|
|
962
|
+
return !1;
|
|
963
|
+
const focusBlock = getFocusBlock(context), previousBlock = getPreviousBlock(context);
|
|
964
|
+
return focusBlock && previousBlock ? {
|
|
965
|
+
focusBlock,
|
|
966
|
+
previousBlock
|
|
967
|
+
} : !1;
|
|
968
|
+
},
|
|
969
|
+
actions: [(_, {
|
|
970
|
+
focusBlock,
|
|
971
|
+
previousBlock
|
|
972
|
+
}) => [{
|
|
973
|
+
type: "move block",
|
|
974
|
+
blockPath: focusBlock.path,
|
|
975
|
+
to: previousBlock.path
|
|
976
|
+
}]]
|
|
977
|
+
}, {
|
|
978
|
+
on: "key.down",
|
|
979
|
+
guard: ({
|
|
980
|
+
context,
|
|
981
|
+
event
|
|
982
|
+
}) => {
|
|
983
|
+
if (!isHotkey(config.moveBlockDownShortcut, event.keyboardEvent) || !selectionIsCollapsed(context))
|
|
984
|
+
return !1;
|
|
985
|
+
const focusBlock = getFocusBlock(context), nextBlock = getNextBlock(context);
|
|
986
|
+
return focusBlock && nextBlock ? {
|
|
987
|
+
focusBlock,
|
|
988
|
+
nextBlock
|
|
989
|
+
} : !1;
|
|
990
|
+
},
|
|
991
|
+
actions: [(_, {
|
|
992
|
+
focusBlock,
|
|
993
|
+
nextBlock
|
|
994
|
+
}) => [{
|
|
995
|
+
type: "move block",
|
|
996
|
+
blockPath: focusBlock.path,
|
|
997
|
+
to: nextBlock.path
|
|
998
|
+
}]]
|
|
999
|
+
}];
|
|
1000
|
+
}
|
|
810
1001
|
function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
811
1002
|
if (!portableTextType)
|
|
812
1003
|
throw new Error("Parameter 'portabletextType' missing (required)");
|
|
@@ -5438,43 +5629,31 @@ const addAnnotationActionImplementation = ({
|
|
|
5438
5629
|
at: editor.selection.focus,
|
|
5439
5630
|
match: (n) => editor.isTextSpan(n),
|
|
5440
5631
|
voids: !1
|
|
5441
|
-
}))[0] ?? [void 0], focusDecorators = focusSpan
|
|
5632
|
+
}))[0] ?? [void 0], focusDecorators = focusSpan?.marks?.filter((mark) => schema.decorators.some((decorator) => decorator.value === mark)) ?? [], focusAnnotations = focusSpan?.marks?.filter((mark) => !schema.decorators.some((decorator) => decorator.value === mark)) ?? [], anchorBlockPath = editor.selection.anchor.path.slice(0, 1), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
|
|
5442
5633
|
if (editor.isTextBlock(focusBlock)) {
|
|
5443
|
-
const [start, end] = Range.edges(editor.selection),
|
|
5634
|
+
const [start, end] = Range.edges(editor.selection), lastFocusBlockChild = focusBlock.children[focusBlock.children.length - 1], atTheEndOfBlock = isEqual(start, {
|
|
5635
|
+
path: [...focusBlockPath, focusBlock.children.length - 1],
|
|
5636
|
+
offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
|
|
5637
|
+
}), atTheStartOfBlock = isEqual(end, {
|
|
5444
5638
|
path: [...focusBlockPath, 0],
|
|
5445
5639
|
offset: 0
|
|
5446
5640
|
});
|
|
5447
|
-
if (
|
|
5641
|
+
if (atTheEndOfBlock && Range.isCollapsed(editor.selection)) {
|
|
5448
5642
|
Editor.insertNode(editor, editor.pteCreateTextBlock({
|
|
5449
|
-
decorators:
|
|
5643
|
+
decorators: [],
|
|
5450
5644
|
listItem: focusBlock.listItem,
|
|
5451
5645
|
level: focusBlock.level
|
|
5452
5646
|
}));
|
|
5453
|
-
const [nextBlockPath] = Path.next(focusBlockPath);
|
|
5454
|
-
Transforms.select(editor, {
|
|
5455
|
-
anchor: {
|
|
5456
|
-
path: [nextBlockPath, 0],
|
|
5457
|
-
offset: 0
|
|
5458
|
-
},
|
|
5459
|
-
focus: {
|
|
5460
|
-
path: [nextBlockPath, 0],
|
|
5461
|
-
offset: 0
|
|
5462
|
-
}
|
|
5463
|
-
});
|
|
5464
5647
|
return;
|
|
5465
5648
|
}
|
|
5466
|
-
|
|
5467
|
-
path: [...focusBlockPath, focusBlock.children.length - 1],
|
|
5468
|
-
offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
|
|
5469
|
-
});
|
|
5470
|
-
if (atTheEndOfBlock && Range.isCollapsed(editor.selection)) {
|
|
5649
|
+
if (atTheStartOfBlock && Range.isCollapsed(editor.selection)) {
|
|
5471
5650
|
Editor.insertNode(editor, editor.pteCreateTextBlock({
|
|
5472
|
-
decorators: [],
|
|
5651
|
+
decorators: focusAnnotations.length === 0 ? focusDecorators : [],
|
|
5473
5652
|
listItem: focusBlock.listItem,
|
|
5474
5653
|
level: focusBlock.level
|
|
5475
5654
|
}));
|
|
5476
5655
|
const [nextBlockPath] = Path.next(focusBlockPath);
|
|
5477
|
-
Transforms.
|
|
5656
|
+
Transforms.select(editor, {
|
|
5478
5657
|
anchor: {
|
|
5479
5658
|
path: [nextBlockPath, 0],
|
|
5480
5659
|
offset: 0
|
|
@@ -5486,7 +5665,8 @@ const addAnnotationActionImplementation = ({
|
|
|
5486
5665
|
});
|
|
5487
5666
|
return;
|
|
5488
5667
|
}
|
|
5489
|
-
|
|
5668
|
+
const selectionAcrossBlocks = anchorBlockPath[0] !== focusBlockPath[0];
|
|
5669
|
+
if (!atTheStartOfBlock && !atTheEndOfBlock && !selectionAcrossBlocks) {
|
|
5490
5670
|
Editor.withoutNormalizing(editor, () => {
|
|
5491
5671
|
if (!editor.selection)
|
|
5492
5672
|
return;
|
|
@@ -5535,6 +5715,9 @@ const addAnnotationActionImplementation = ({
|
|
|
5535
5715
|
return;
|
|
5536
5716
|
}
|
|
5537
5717
|
}
|
|
5718
|
+
Transforms.splitNodes(editor, {
|
|
5719
|
+
always: !0
|
|
5720
|
+
});
|
|
5538
5721
|
}, insertSoftBreakActionImplementation = ({
|
|
5539
5722
|
context,
|
|
5540
5723
|
action
|
|
@@ -5635,6 +5818,8 @@ const addAnnotationActionImplementation = ({
|
|
|
5635
5818
|
});
|
|
5636
5819
|
}
|
|
5637
5820
|
},
|
|
5821
|
+
copy: () => {
|
|
5822
|
+
},
|
|
5638
5823
|
"delete backward": ({
|
|
5639
5824
|
action
|
|
5640
5825
|
}) => {
|
|
@@ -5734,10 +5919,48 @@ const addAnnotationActionImplementation = ({
|
|
|
5734
5919
|
}) => {
|
|
5735
5920
|
action.effect();
|
|
5736
5921
|
},
|
|
5737
|
-
|
|
5922
|
+
"key.down": () => {
|
|
5923
|
+
},
|
|
5924
|
+
"key.up": () => {
|
|
5925
|
+
},
|
|
5926
|
+
"move block": ({
|
|
5738
5927
|
action
|
|
5739
5928
|
}) => {
|
|
5740
|
-
|
|
5929
|
+
const location = toSlateRange({
|
|
5930
|
+
anchor: {
|
|
5931
|
+
path: action.blockPath,
|
|
5932
|
+
offset: 0
|
|
5933
|
+
},
|
|
5934
|
+
focus: {
|
|
5935
|
+
path: action.blockPath,
|
|
5936
|
+
offset: 0
|
|
5937
|
+
}
|
|
5938
|
+
}, action.editor);
|
|
5939
|
+
if (!location) {
|
|
5940
|
+
console.error("Unable to find Slate range from selection points");
|
|
5941
|
+
return;
|
|
5942
|
+
}
|
|
5943
|
+
const newLocation = toSlateRange({
|
|
5944
|
+
anchor: {
|
|
5945
|
+
path: action.to,
|
|
5946
|
+
offset: 0
|
|
5947
|
+
},
|
|
5948
|
+
focus: {
|
|
5949
|
+
path: action.to,
|
|
5950
|
+
offset: 0
|
|
5951
|
+
}
|
|
5952
|
+
}, action.editor);
|
|
5953
|
+
if (!newLocation) {
|
|
5954
|
+
console.error("Unable to find Slate range from selection points");
|
|
5955
|
+
return;
|
|
5956
|
+
}
|
|
5957
|
+
Transforms.moveNodes(action.editor, {
|
|
5958
|
+
at: location,
|
|
5959
|
+
to: newLocation.anchor.path.slice(0, 1),
|
|
5960
|
+
mode: "highest"
|
|
5961
|
+
});
|
|
5962
|
+
},
|
|
5963
|
+
paste: () => {
|
|
5741
5964
|
},
|
|
5742
5965
|
select: ({
|
|
5743
5966
|
action
|
|
@@ -5745,6 +5968,31 @@ const addAnnotationActionImplementation = ({
|
|
|
5745
5968
|
const newSelection = toSlateRange(action.selection, action.editor);
|
|
5746
5969
|
newSelection ? Transforms.select(action.editor, newSelection) : Transforms.deselect(action.editor);
|
|
5747
5970
|
},
|
|
5971
|
+
"select previous block": ({
|
|
5972
|
+
action
|
|
5973
|
+
}) => {
|
|
5974
|
+
if (!action.editor.selection) {
|
|
5975
|
+
console.error("Unable to select previous block without a selection");
|
|
5976
|
+
return;
|
|
5977
|
+
}
|
|
5978
|
+
const blockPath = action.editor.selection.focus.path.slice(0, 1);
|
|
5979
|
+
if (!Path.hasPrevious(blockPath)) {
|
|
5980
|
+
console.error("There's no previous block to select");
|
|
5981
|
+
return;
|
|
5982
|
+
}
|
|
5983
|
+
const previousBlockPath = Path.previous(blockPath);
|
|
5984
|
+
Transforms.select(action.editor, previousBlockPath);
|
|
5985
|
+
},
|
|
5986
|
+
"select next block": ({
|
|
5987
|
+
action
|
|
5988
|
+
}) => {
|
|
5989
|
+
if (!action.editor.selection) {
|
|
5990
|
+
console.error("Unable to select next block without a selection");
|
|
5991
|
+
return;
|
|
5992
|
+
}
|
|
5993
|
+
const nextBlockPath = [action.editor.selection.focus.path.slice(0, 1)[0] + 1];
|
|
5994
|
+
Transforms.select(action.editor, nextBlockPath);
|
|
5995
|
+
},
|
|
5748
5996
|
reselect: ({
|
|
5749
5997
|
action
|
|
5750
5998
|
}) => {
|
|
@@ -5796,6 +6044,13 @@ function performAction({
|
|
|
5796
6044
|
});
|
|
5797
6045
|
break;
|
|
5798
6046
|
}
|
|
6047
|
+
case "move block": {
|
|
6048
|
+
behaviorActionImplementations["move block"]({
|
|
6049
|
+
context,
|
|
6050
|
+
action
|
|
6051
|
+
});
|
|
6052
|
+
break;
|
|
6053
|
+
}
|
|
5799
6054
|
case "set block": {
|
|
5800
6055
|
behaviorActionImplementations["set block"]({
|
|
5801
6056
|
context,
|
|
@@ -5824,6 +6079,20 @@ function performAction({
|
|
|
5824
6079
|
});
|
|
5825
6080
|
break;
|
|
5826
6081
|
}
|
|
6082
|
+
case "select previous block": {
|
|
6083
|
+
behaviorActionImplementations["select previous block"]({
|
|
6084
|
+
context,
|
|
6085
|
+
action
|
|
6086
|
+
});
|
|
6087
|
+
break;
|
|
6088
|
+
}
|
|
6089
|
+
case "select next block": {
|
|
6090
|
+
behaviorActionImplementations["select next block"]({
|
|
6091
|
+
context,
|
|
6092
|
+
action
|
|
6093
|
+
});
|
|
6094
|
+
break;
|
|
6095
|
+
}
|
|
5827
6096
|
case "reselect": {
|
|
5828
6097
|
behaviorActionImplementations.reselect({
|
|
5829
6098
|
context,
|
|
@@ -5864,6 +6133,8 @@ function performDefaultAction({
|
|
|
5864
6133
|
});
|
|
5865
6134
|
break;
|
|
5866
6135
|
}
|
|
6136
|
+
case "copy":
|
|
6137
|
+
break;
|
|
5867
6138
|
case "decorator.add": {
|
|
5868
6139
|
behaviorActionImplementations["decorator.add"]({
|
|
5869
6140
|
context,
|
|
@@ -5927,11 +6198,6 @@ function performDefaultAction({
|
|
|
5927
6198
|
});
|
|
5928
6199
|
break;
|
|
5929
6200
|
}
|
|
5930
|
-
default:
|
|
5931
|
-
behaviorActionImplementations.paste({
|
|
5932
|
-
context,
|
|
5933
|
-
action
|
|
5934
|
-
});
|
|
5935
6201
|
}
|
|
5936
6202
|
}
|
|
5937
6203
|
const editorMachine = setup({
|
|
@@ -6008,10 +6274,10 @@ const editorMachine = setup({
|
|
|
6008
6274
|
};
|
|
6009
6275
|
let behaviorOverwritten = !1;
|
|
6010
6276
|
for (const eventBehavior of eventBehaviors) {
|
|
6011
|
-
const shouldRun = eventBehavior.guard
|
|
6277
|
+
const shouldRun = eventBehavior.guard === void 0 || eventBehavior.guard({
|
|
6012
6278
|
context: behaviorContext,
|
|
6013
6279
|
event: event.behaviorEvent
|
|
6014
|
-
})
|
|
6280
|
+
});
|
|
6015
6281
|
if (!shouldRun)
|
|
6016
6282
|
continue;
|
|
6017
6283
|
const actionIntendSets = eventBehavior.actions.map((actionSet) => actionSet({
|
|
@@ -6024,8 +6290,10 @@ const editorMachine = setup({
|
|
|
6024
6290
|
editor: event.editor,
|
|
6025
6291
|
actionIntends
|
|
6026
6292
|
});
|
|
6027
|
-
if (behaviorOverwritten)
|
|
6293
|
+
if (behaviorOverwritten) {
|
|
6294
|
+
event.nativeEvent?.preventDefault();
|
|
6028
6295
|
break;
|
|
6296
|
+
}
|
|
6029
6297
|
}
|
|
6030
6298
|
behaviorOverwritten || enqueue.raise({
|
|
6031
6299
|
type: "behavior action intends",
|
|
@@ -6671,38 +6939,7 @@ function createWithHotkeys(editorActor, portableTextEditor, hotkeysFromOptions)
|
|
|
6671
6939
|
}
|
|
6672
6940
|
}
|
|
6673
6941
|
});
|
|
6674
|
-
const isEnter = isHotkey("enter", event.nativeEvent), isTab = isHotkey("tab", event.nativeEvent), isShiftEnter = isHotkey("shift+enter", event.nativeEvent), isShiftTab = isHotkey("shift+tab", event.nativeEvent)
|
|
6675
|
-
if (isArrowDown && editor.selection) {
|
|
6676
|
-
const focusBlock = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
6677
|
-
if (focusBlock && Editor.isVoid(editor, focusBlock)) {
|
|
6678
|
-
const nextPath = Path.next(editor.selection.focus.path.slice(0, 1));
|
|
6679
|
-
if (!Node.has(editor, nextPath)) {
|
|
6680
|
-
Transforms.insertNodes(editor, editor.pteCreateTextBlock({
|
|
6681
|
-
decorators: []
|
|
6682
|
-
}), {
|
|
6683
|
-
at: nextPath
|
|
6684
|
-
}), Transforms.select(editor, {
|
|
6685
|
-
path: [...nextPath, 0],
|
|
6686
|
-
offset: 0
|
|
6687
|
-
}), editor.onChange();
|
|
6688
|
-
return;
|
|
6689
|
-
}
|
|
6690
|
-
}
|
|
6691
|
-
}
|
|
6692
|
-
if (isArrowUp && editor.selection) {
|
|
6693
|
-
const isFirstBlock = editor.selection.focus.path[0] === 0, focusBlock = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
6694
|
-
if (isFirstBlock && focusBlock && Editor.isVoid(editor, focusBlock)) {
|
|
6695
|
-
Transforms.insertNodes(editor, editor.pteCreateTextBlock({
|
|
6696
|
-
decorators: []
|
|
6697
|
-
}), {
|
|
6698
|
-
at: [0]
|
|
6699
|
-
}), Transforms.select(editor, {
|
|
6700
|
-
path: [0, 0],
|
|
6701
|
-
offset: 0
|
|
6702
|
-
}), editor.onChange();
|
|
6703
|
-
return;
|
|
6704
|
-
}
|
|
6705
|
-
}
|
|
6942
|
+
const isEnter = isHotkey("enter", event.nativeEvent), isTab = isHotkey("tab", event.nativeEvent), isShiftEnter = isHotkey("shift+enter", event.nativeEvent), isShiftTab = isHotkey("shift+tab", event.nativeEvent);
|
|
6706
6943
|
if ((isTab || isShiftTab) && editor.selection) {
|
|
6707
6944
|
const [focusChild] = Editor.node(editor, editor.selection.focus, {
|
|
6708
6945
|
depth: 2
|
|
@@ -7049,8 +7286,16 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7049
7286
|
return () => teardown();
|
|
7050
7287
|
}, [slateEditor, syncRangeDecorations]);
|
|
7051
7288
|
const handleCopy = useCallback((event) => {
|
|
7052
|
-
onCopy
|
|
7053
|
-
|
|
7289
|
+
onCopy ? onCopy(event) !== void 0 && event.preventDefault() : event.nativeEvent.clipboardData && editorActor.send({
|
|
7290
|
+
type: "behavior event",
|
|
7291
|
+
behaviorEvent: {
|
|
7292
|
+
type: "copy",
|
|
7293
|
+
data: event.nativeEvent.clipboardData
|
|
7294
|
+
},
|
|
7295
|
+
editor: slateEditor,
|
|
7296
|
+
nativeEvent: event
|
|
7297
|
+
});
|
|
7298
|
+
}, [onCopy, editorActor, slateEditor]), handlePaste = useCallback((event_0) => {
|
|
7054
7299
|
const value_0 = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(value_0, slateEditor.selection, schemaTypes)?.focus.path || [], onPasteResult = onPaste?.({
|
|
7055
7300
|
event: event_0,
|
|
7056
7301
|
value: value_0,
|
|
@@ -7067,14 +7312,15 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7067
7312
|
editorActor.send({
|
|
7068
7313
|
type: "done loading"
|
|
7069
7314
|
});
|
|
7070
|
-
})) : event_0.nativeEvent.clipboardData &&
|
|
7315
|
+
})) : event_0.nativeEvent.clipboardData && editorActor.send({
|
|
7071
7316
|
type: "behavior event",
|
|
7072
7317
|
behaviorEvent: {
|
|
7073
7318
|
type: "paste",
|
|
7074
|
-
|
|
7319
|
+
data: event_0.nativeEvent.clipboardData
|
|
7075
7320
|
},
|
|
7076
|
-
editor: slateEditor
|
|
7077
|
-
|
|
7321
|
+
editor: slateEditor,
|
|
7322
|
+
nativeEvent: event_0
|
|
7323
|
+
}), debug("No result from custom paste handler, pasting normally");
|
|
7078
7324
|
}, [editorActor, onPaste, portableTextEditor, schemaTypes, slateEditor]), handleOnFocus = useCallback((event_1) => {
|
|
7079
7325
|
if (onFocus && onFocus(event_1), !event_1.isDefaultPrevented()) {
|
|
7080
7326
|
const selection = PortableTextEditor.getSelection(portableTextEditor);
|
|
@@ -7135,8 +7381,40 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7135
7381
|
}
|
|
7136
7382
|
}, [validateSelection, editableElement]);
|
|
7137
7383
|
const handleKeyDown = useCallback((event_5) => {
|
|
7138
|
-
props.onKeyDown && props.onKeyDown(event_5), event_5.isDefaultPrevented() || slateEditor.pteWithHotKeys(event_5)
|
|
7139
|
-
|
|
7384
|
+
props.onKeyDown && props.onKeyDown(event_5), event_5.isDefaultPrevented() || slateEditor.pteWithHotKeys(event_5), event_5.isDefaultPrevented() || editorActor.send({
|
|
7385
|
+
type: "behavior event",
|
|
7386
|
+
behaviorEvent: {
|
|
7387
|
+
type: "key.down",
|
|
7388
|
+
keyboardEvent: {
|
|
7389
|
+
key: event_5.key,
|
|
7390
|
+
code: event_5.code,
|
|
7391
|
+
altKey: event_5.altKey,
|
|
7392
|
+
ctrlKey: event_5.ctrlKey,
|
|
7393
|
+
metaKey: event_5.metaKey,
|
|
7394
|
+
shiftKey: event_5.shiftKey
|
|
7395
|
+
}
|
|
7396
|
+
},
|
|
7397
|
+
editor: slateEditor,
|
|
7398
|
+
nativeEvent: event_5
|
|
7399
|
+
});
|
|
7400
|
+
}, [props, editorActor, slateEditor]), handleKeyUp = useCallback((event_6) => {
|
|
7401
|
+
props.onKeyUp && props.onKeyUp(event_6), event_6.isDefaultPrevented() || editorActor.send({
|
|
7402
|
+
type: "behavior event",
|
|
7403
|
+
behaviorEvent: {
|
|
7404
|
+
type: "key.up",
|
|
7405
|
+
keyboardEvent: {
|
|
7406
|
+
key: event_6.key,
|
|
7407
|
+
code: event_6.code,
|
|
7408
|
+
altKey: event_6.altKey,
|
|
7409
|
+
ctrlKey: event_6.ctrlKey,
|
|
7410
|
+
metaKey: event_6.metaKey,
|
|
7411
|
+
shiftKey: event_6.shiftKey
|
|
7412
|
+
}
|
|
7413
|
+
},
|
|
7414
|
+
editor: slateEditor,
|
|
7415
|
+
nativeEvent: event_6
|
|
7416
|
+
});
|
|
7417
|
+
}, [props, editorActor, slateEditor]), scrollSelectionIntoViewToSlate = useMemo(() => {
|
|
7140
7418
|
if (scrollSelectionIntoView !== void 0)
|
|
7141
7419
|
return scrollSelectionIntoView === null ? noop : (_editor, domRange) => {
|
|
7142
7420
|
scrollSelectionIntoView(portableTextEditor, domRange);
|
|
@@ -7183,6 +7461,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7183
7461
|
onDOMBeforeInput: handleOnBeforeInput,
|
|
7184
7462
|
onFocus: handleOnFocus,
|
|
7185
7463
|
onKeyDown: handleKeyDown,
|
|
7464
|
+
onKeyUp: handleKeyUp,
|
|
7186
7465
|
onPaste: handlePaste,
|
|
7187
7466
|
readOnly,
|
|
7188
7467
|
renderPlaceholder: void 0,
|
|
@@ -7247,6 +7526,7 @@ export {
|
|
|
7247
7526
|
PortableTextEditor,
|
|
7248
7527
|
coreBehavior,
|
|
7249
7528
|
coreBehaviors,
|
|
7529
|
+
createCodeEditorBehaviors,
|
|
7250
7530
|
createLinkBehaviors,
|
|
7251
7531
|
createMarkdownBehaviors,
|
|
7252
7532
|
defineBehavior,
|