@portabletext/editor 1.12.3 → 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 -192
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +361 -192
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +361 -192
- package/lib/index.mjs.map +1 -1
- package/package.json +5 -5
- 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 +1 -48
- package/src/index.ts +4 -0
- package/src/utils/is-hotkey.test.ts +97 -46
- package/src/utils/is-hotkey.ts +1 -1
package/lib/index.mjs
CHANGED
|
@@ -25,11 +25,123 @@ import get from "lodash/get.js";
|
|
|
25
25
|
import isUndefined from "lodash/isUndefined.js";
|
|
26
26
|
import omitBy from "lodash/omitBy.js";
|
|
27
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
|
+
}
|
|
28
140
|
function defineBehavior(behavior) {
|
|
29
141
|
return behavior;
|
|
30
142
|
}
|
|
31
143
|
function selectionIsCollapsed(context) {
|
|
32
|
-
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;
|
|
33
145
|
}
|
|
34
146
|
function getFocusBlock(context) {
|
|
35
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;
|
|
@@ -145,11 +257,42 @@ function isEmptyTextBlock(block) {
|
|
|
145
257
|
function getTextBlockText(block) {
|
|
146
258
|
return block.children.map((child) => child.text ?? "").join("");
|
|
147
259
|
}
|
|
148
|
-
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 = {
|
|
149
289
|
on: "insert break",
|
|
150
290
|
guard: ({
|
|
151
291
|
context
|
|
152
|
-
}) =>
|
|
292
|
+
}) => {
|
|
293
|
+
const focusBlockObject = getFocusBlockObject(context);
|
|
294
|
+
return selectionIsCollapsed(context) && focusBlockObject !== void 0;
|
|
295
|
+
},
|
|
153
296
|
actions: [() => [{
|
|
154
297
|
type: "insert text block",
|
|
155
298
|
placement: "after"
|
|
@@ -215,6 +358,8 @@ const breakingBlockObject = {
|
|
|
215
358
|
}
|
|
216
359
|
}]]
|
|
217
360
|
}, coreBlockObjectBehaviors = {
|
|
361
|
+
arrowDownOnLonelyBlockObject,
|
|
362
|
+
arrowUpOnLonelyBlockObject,
|
|
218
363
|
breakingBlockObject,
|
|
219
364
|
deletingEmptyTextBlockAfterBlockObject,
|
|
220
365
|
deletingEmptyTextBlockBeforeBlockObject
|
|
@@ -298,7 +443,7 @@ const breakingBlockObject = {
|
|
|
298
443
|
text: `
|
|
299
444
|
`
|
|
300
445
|
}]]
|
|
301
|
-
}, 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 = {
|
|
302
447
|
softReturn,
|
|
303
448
|
decorators: coreDecoratorBehaviors,
|
|
304
449
|
blockObjects: coreBlockObjectBehaviors,
|
|
@@ -311,7 +456,7 @@ function createLinkBehaviors(config) {
|
|
|
311
456
|
context,
|
|
312
457
|
event
|
|
313
458
|
}) => {
|
|
314
|
-
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?.({
|
|
315
460
|
url,
|
|
316
461
|
schema: context.schema
|
|
317
462
|
}) : void 0;
|
|
@@ -334,7 +479,7 @@ function createLinkBehaviors(config) {
|
|
|
334
479
|
const focusSpan = getFocusSpan(context), selectionCollapsed = selectionIsCollapsed(context);
|
|
335
480
|
if (!focusSpan || !selectionCollapsed)
|
|
336
481
|
return !1;
|
|
337
|
-
const text = event.
|
|
482
|
+
const text = event.data.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
|
|
338
483
|
url,
|
|
339
484
|
schema: context.schema
|
|
340
485
|
}) : void 0;
|
|
@@ -610,7 +755,7 @@ function createMarkdownBehaviors(config) {
|
|
|
610
755
|
context,
|
|
611
756
|
event
|
|
612
757
|
}) => {
|
|
613
|
-
const text = event.
|
|
758
|
+
const text = event.data.getData("text/plain"), hrRegExp = /^(---)$|(___)$|(\*\*\*)$/gm, hrCharacters = text.match(hrRegExp)?.[0], hrObject = config.horizontalRuleObject?.({
|
|
614
759
|
schema: context.schema
|
|
615
760
|
}), focusBlock = getFocusBlock(context);
|
|
616
761
|
return !hrCharacters || !hrObject || !focusBlock ? !1 : {
|
|
@@ -806,6 +951,53 @@ function createMarkdownBehaviors(config) {
|
|
|
806
951
|
};
|
|
807
952
|
return [automaticBlockquoteOnSpace, automaticHeadingOnSpace, automaticHr, automaticHrOnPaste, clearStyleOnBackspace, automaticListOnSpace];
|
|
808
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
|
+
}
|
|
809
1001
|
function getPortableTextMemberSchemaTypes(portableTextType) {
|
|
810
1002
|
if (!portableTextType)
|
|
811
1003
|
throw new Error("Parameter 'portabletextType' missing (required)");
|
|
@@ -5437,43 +5629,31 @@ const addAnnotationActionImplementation = ({
|
|
|
5437
5629
|
at: editor.selection.focus,
|
|
5438
5630
|
match: (n) => editor.isTextSpan(n),
|
|
5439
5631
|
voids: !1
|
|
5440
|
-
}))[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);
|
|
5441
5633
|
if (editor.isTextBlock(focusBlock)) {
|
|
5442
|
-
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, {
|
|
5443
5638
|
path: [...focusBlockPath, 0],
|
|
5444
5639
|
offset: 0
|
|
5445
5640
|
});
|
|
5446
|
-
if (
|
|
5641
|
+
if (atTheEndOfBlock && Range.isCollapsed(editor.selection)) {
|
|
5447
5642
|
Editor.insertNode(editor, editor.pteCreateTextBlock({
|
|
5448
|
-
decorators:
|
|
5643
|
+
decorators: [],
|
|
5449
5644
|
listItem: focusBlock.listItem,
|
|
5450
5645
|
level: focusBlock.level
|
|
5451
5646
|
}));
|
|
5452
|
-
const [nextBlockPath] = Path.next(focusBlockPath);
|
|
5453
|
-
Transforms.select(editor, {
|
|
5454
|
-
anchor: {
|
|
5455
|
-
path: [nextBlockPath, 0],
|
|
5456
|
-
offset: 0
|
|
5457
|
-
},
|
|
5458
|
-
focus: {
|
|
5459
|
-
path: [nextBlockPath, 0],
|
|
5460
|
-
offset: 0
|
|
5461
|
-
}
|
|
5462
|
-
});
|
|
5463
5647
|
return;
|
|
5464
5648
|
}
|
|
5465
|
-
|
|
5466
|
-
path: [...focusBlockPath, focusBlock.children.length - 1],
|
|
5467
|
-
offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
|
|
5468
|
-
});
|
|
5469
|
-
if (atTheEndOfBlock && Range.isCollapsed(editor.selection)) {
|
|
5649
|
+
if (atTheStartOfBlock && Range.isCollapsed(editor.selection)) {
|
|
5470
5650
|
Editor.insertNode(editor, editor.pteCreateTextBlock({
|
|
5471
|
-
decorators: [],
|
|
5651
|
+
decorators: focusAnnotations.length === 0 ? focusDecorators : [],
|
|
5472
5652
|
listItem: focusBlock.listItem,
|
|
5473
5653
|
level: focusBlock.level
|
|
5474
5654
|
}));
|
|
5475
5655
|
const [nextBlockPath] = Path.next(focusBlockPath);
|
|
5476
|
-
Transforms.
|
|
5656
|
+
Transforms.select(editor, {
|
|
5477
5657
|
anchor: {
|
|
5478
5658
|
path: [nextBlockPath, 0],
|
|
5479
5659
|
offset: 0
|
|
@@ -5485,7 +5665,8 @@ const addAnnotationActionImplementation = ({
|
|
|
5485
5665
|
});
|
|
5486
5666
|
return;
|
|
5487
5667
|
}
|
|
5488
|
-
|
|
5668
|
+
const selectionAcrossBlocks = anchorBlockPath[0] !== focusBlockPath[0];
|
|
5669
|
+
if (!atTheStartOfBlock && !atTheEndOfBlock && !selectionAcrossBlocks) {
|
|
5489
5670
|
Editor.withoutNormalizing(editor, () => {
|
|
5490
5671
|
if (!editor.selection)
|
|
5491
5672
|
return;
|
|
@@ -5534,6 +5715,9 @@ const addAnnotationActionImplementation = ({
|
|
|
5534
5715
|
return;
|
|
5535
5716
|
}
|
|
5536
5717
|
}
|
|
5718
|
+
Transforms.splitNodes(editor, {
|
|
5719
|
+
always: !0
|
|
5720
|
+
});
|
|
5537
5721
|
}, insertSoftBreakActionImplementation = ({
|
|
5538
5722
|
context,
|
|
5539
5723
|
action
|
|
@@ -5634,6 +5818,8 @@ const addAnnotationActionImplementation = ({
|
|
|
5634
5818
|
});
|
|
5635
5819
|
}
|
|
5636
5820
|
},
|
|
5821
|
+
copy: () => {
|
|
5822
|
+
},
|
|
5637
5823
|
"delete backward": ({
|
|
5638
5824
|
action
|
|
5639
5825
|
}) => {
|
|
@@ -5733,10 +5919,48 @@ const addAnnotationActionImplementation = ({
|
|
|
5733
5919
|
}) => {
|
|
5734
5920
|
action.effect();
|
|
5735
5921
|
},
|
|
5736
|
-
|
|
5922
|
+
"key.down": () => {
|
|
5923
|
+
},
|
|
5924
|
+
"key.up": () => {
|
|
5925
|
+
},
|
|
5926
|
+
"move block": ({
|
|
5737
5927
|
action
|
|
5738
5928
|
}) => {
|
|
5739
|
-
|
|
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: () => {
|
|
5740
5964
|
},
|
|
5741
5965
|
select: ({
|
|
5742
5966
|
action
|
|
@@ -5744,6 +5968,31 @@ const addAnnotationActionImplementation = ({
|
|
|
5744
5968
|
const newSelection = toSlateRange(action.selection, action.editor);
|
|
5745
5969
|
newSelection ? Transforms.select(action.editor, newSelection) : Transforms.deselect(action.editor);
|
|
5746
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
|
+
},
|
|
5747
5996
|
reselect: ({
|
|
5748
5997
|
action
|
|
5749
5998
|
}) => {
|
|
@@ -5795,6 +6044,13 @@ function performAction({
|
|
|
5795
6044
|
});
|
|
5796
6045
|
break;
|
|
5797
6046
|
}
|
|
6047
|
+
case "move block": {
|
|
6048
|
+
behaviorActionImplementations["move block"]({
|
|
6049
|
+
context,
|
|
6050
|
+
action
|
|
6051
|
+
});
|
|
6052
|
+
break;
|
|
6053
|
+
}
|
|
5798
6054
|
case "set block": {
|
|
5799
6055
|
behaviorActionImplementations["set block"]({
|
|
5800
6056
|
context,
|
|
@@ -5823,6 +6079,20 @@ function performAction({
|
|
|
5823
6079
|
});
|
|
5824
6080
|
break;
|
|
5825
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
|
+
}
|
|
5826
6096
|
case "reselect": {
|
|
5827
6097
|
behaviorActionImplementations.reselect({
|
|
5828
6098
|
context,
|
|
@@ -5863,6 +6133,8 @@ function performDefaultAction({
|
|
|
5863
6133
|
});
|
|
5864
6134
|
break;
|
|
5865
6135
|
}
|
|
6136
|
+
case "copy":
|
|
6137
|
+
break;
|
|
5866
6138
|
case "decorator.add": {
|
|
5867
6139
|
behaviorActionImplementations["decorator.add"]({
|
|
5868
6140
|
context,
|
|
@@ -5926,11 +6198,6 @@ function performDefaultAction({
|
|
|
5926
6198
|
});
|
|
5927
6199
|
break;
|
|
5928
6200
|
}
|
|
5929
|
-
default:
|
|
5930
|
-
behaviorActionImplementations.paste({
|
|
5931
|
-
context,
|
|
5932
|
-
action
|
|
5933
|
-
});
|
|
5934
6201
|
}
|
|
5935
6202
|
}
|
|
5936
6203
|
const editorMachine = setup({
|
|
@@ -6007,10 +6274,10 @@ const editorMachine = setup({
|
|
|
6007
6274
|
};
|
|
6008
6275
|
let behaviorOverwritten = !1;
|
|
6009
6276
|
for (const eventBehavior of eventBehaviors) {
|
|
6010
|
-
const shouldRun = eventBehavior.guard
|
|
6277
|
+
const shouldRun = eventBehavior.guard === void 0 || eventBehavior.guard({
|
|
6011
6278
|
context: behaviorContext,
|
|
6012
6279
|
event: event.behaviorEvent
|
|
6013
|
-
})
|
|
6280
|
+
});
|
|
6014
6281
|
if (!shouldRun)
|
|
6015
6282
|
continue;
|
|
6016
6283
|
const actionIntendSets = eventBehavior.actions.map((actionSet) => actionSet({
|
|
@@ -6023,8 +6290,10 @@ const editorMachine = setup({
|
|
|
6023
6290
|
editor: event.editor,
|
|
6024
6291
|
actionIntends
|
|
6025
6292
|
});
|
|
6026
|
-
if (behaviorOverwritten)
|
|
6293
|
+
if (behaviorOverwritten) {
|
|
6294
|
+
event.nativeEvent?.preventDefault();
|
|
6027
6295
|
break;
|
|
6296
|
+
}
|
|
6028
6297
|
}
|
|
6029
6298
|
behaviorOverwritten || enqueue.raise({
|
|
6030
6299
|
type: "behavior action intends",
|
|
@@ -6623,118 +6892,6 @@ const debug$3 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
|
|
|
6623
6892
|
return useMemo(() => /* @__PURE__ */ jsx("span", { ...attributes, ref: spanRef, children: content }, leaf._key), [leaf, attributes, content]);
|
|
6624
6893
|
};
|
|
6625
6894
|
Leaf.displayName = "Leaf";
|
|
6626
|
-
const IS_MAC = typeof window < "u" && /Mac|iPod|iPhone|iPad/.test(window.navigator.userAgent), modifiers = {
|
|
6627
|
-
alt: "altKey",
|
|
6628
|
-
control: "ctrlKey",
|
|
6629
|
-
meta: "metaKey",
|
|
6630
|
-
shift: "shiftKey"
|
|
6631
|
-
}, aliases = {
|
|
6632
|
-
add: "+",
|
|
6633
|
-
break: "pause",
|
|
6634
|
-
cmd: "meta",
|
|
6635
|
-
command: "meta",
|
|
6636
|
-
ctl: "control",
|
|
6637
|
-
ctrl: "control",
|
|
6638
|
-
del: "delete",
|
|
6639
|
-
down: "arrowdown",
|
|
6640
|
-
esc: "escape",
|
|
6641
|
-
ins: "insert",
|
|
6642
|
-
left: "arrowleft",
|
|
6643
|
-
mod: IS_MAC ? "meta" : "control",
|
|
6644
|
-
opt: "alt",
|
|
6645
|
-
option: "alt",
|
|
6646
|
-
return: "enter",
|
|
6647
|
-
right: "arrowright",
|
|
6648
|
-
space: " ",
|
|
6649
|
-
spacebar: " ",
|
|
6650
|
-
up: "arrowup",
|
|
6651
|
-
win: "meta",
|
|
6652
|
-
windows: "meta"
|
|
6653
|
-
}, keyCodes = {
|
|
6654
|
-
backspace: 8,
|
|
6655
|
-
tab: 9,
|
|
6656
|
-
enter: 13,
|
|
6657
|
-
shift: 16,
|
|
6658
|
-
control: 17,
|
|
6659
|
-
alt: 18,
|
|
6660
|
-
pause: 19,
|
|
6661
|
-
capslock: 20,
|
|
6662
|
-
escape: 27,
|
|
6663
|
-
" ": 32,
|
|
6664
|
-
pageup: 33,
|
|
6665
|
-
pagedown: 34,
|
|
6666
|
-
end: 35,
|
|
6667
|
-
home: 36,
|
|
6668
|
-
arrowleft: 37,
|
|
6669
|
-
arrowup: 38,
|
|
6670
|
-
arrowright: 39,
|
|
6671
|
-
arrowdown: 40,
|
|
6672
|
-
insert: 45,
|
|
6673
|
-
delete: 46,
|
|
6674
|
-
meta: 91,
|
|
6675
|
-
numlock: 144,
|
|
6676
|
-
scrolllock: 145,
|
|
6677
|
-
";": 186,
|
|
6678
|
-
"=": 187,
|
|
6679
|
-
",": 188,
|
|
6680
|
-
"-": 189,
|
|
6681
|
-
".": 190,
|
|
6682
|
-
"/": 191,
|
|
6683
|
-
"`": 192,
|
|
6684
|
-
"[": 219,
|
|
6685
|
-
"\\": 220,
|
|
6686
|
-
"]": 221,
|
|
6687
|
-
"'": 222,
|
|
6688
|
-
f1: 112,
|
|
6689
|
-
f2: 113,
|
|
6690
|
-
f3: 114,
|
|
6691
|
-
f4: 115,
|
|
6692
|
-
f5: 116,
|
|
6693
|
-
f6: 117,
|
|
6694
|
-
f7: 118,
|
|
6695
|
-
f8: 119,
|
|
6696
|
-
f9: 120,
|
|
6697
|
-
f10: 121,
|
|
6698
|
-
f11: 122,
|
|
6699
|
-
f12: 123,
|
|
6700
|
-
f13: 124,
|
|
6701
|
-
f14: 125,
|
|
6702
|
-
f15: 126,
|
|
6703
|
-
f16: 127,
|
|
6704
|
-
f17: 128,
|
|
6705
|
-
f18: 129,
|
|
6706
|
-
f19: 130,
|
|
6707
|
-
f20: 131
|
|
6708
|
-
};
|
|
6709
|
-
function isHotkey(hotkey, event) {
|
|
6710
|
-
return compareHotkey(parseHotkey(hotkey), event);
|
|
6711
|
-
}
|
|
6712
|
-
function parseHotkey(hotkey) {
|
|
6713
|
-
const parsedHotkey = {
|
|
6714
|
-
altKey: !1,
|
|
6715
|
-
ctrlKey: !1,
|
|
6716
|
-
metaKey: !1,
|
|
6717
|
-
shiftKey: !1
|
|
6718
|
-
}, hotkeySegments = hotkey.replace("++", "+add").split("+");
|
|
6719
|
-
for (const rawHotkeySegment of hotkeySegments) {
|
|
6720
|
-
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];
|
|
6721
|
-
if (hotkeySegment.length > 1 && modifier === void 0 && alias === void 0 && code === void 0)
|
|
6722
|
-
throw new TypeError(`Unknown modifier: "${hotkeySegment}"`);
|
|
6723
|
-
(hotkeySegments.length === 1 || modifier === void 0) && (parsedHotkey.key = keyName, parsedHotkey.keyCode = toKeyCode(hotkeySegment)), modifier !== void 0 && (parsedHotkey[modifier] = optional ? null : !0);
|
|
6724
|
-
}
|
|
6725
|
-
return parsedHotkey;
|
|
6726
|
-
}
|
|
6727
|
-
function compareHotkey(parsedHotkey, event) {
|
|
6728
|
-
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;
|
|
6729
|
-
}
|
|
6730
|
-
function toKeyCode(name) {
|
|
6731
|
-
const keyName = toKeyName(name);
|
|
6732
|
-
return keyCodes[keyName] ?? keyName.toUpperCase().charCodeAt(0);
|
|
6733
|
-
}
|
|
6734
|
-
function toKeyName(name) {
|
|
6735
|
-
const keyName = name.toLowerCase();
|
|
6736
|
-
return aliases[keyName] ?? keyName;
|
|
6737
|
-
}
|
|
6738
6895
|
const debug$2 = debugWithName("plugin:withHotKeys"), DEFAULT_HOTKEYS = {
|
|
6739
6896
|
marks: {
|
|
6740
6897
|
"mod+b": "strong",
|
|
@@ -6782,38 +6939,7 @@ function createWithHotkeys(editorActor, portableTextEditor, hotkeysFromOptions)
|
|
|
6782
6939
|
}
|
|
6783
6940
|
}
|
|
6784
6941
|
});
|
|
6785
|
-
const isEnter = isHotkey("enter", event.nativeEvent), isTab = isHotkey("tab", event.nativeEvent), isShiftEnter = isHotkey("shift+enter", event.nativeEvent), isShiftTab = isHotkey("shift+tab", event.nativeEvent)
|
|
6786
|
-
if (isArrowDown && editor.selection) {
|
|
6787
|
-
const focusBlock = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
6788
|
-
if (focusBlock && Editor.isVoid(editor, focusBlock)) {
|
|
6789
|
-
const nextPath = Path.next(editor.selection.focus.path.slice(0, 1));
|
|
6790
|
-
if (!Node.has(editor, nextPath)) {
|
|
6791
|
-
Transforms.insertNodes(editor, editor.pteCreateTextBlock({
|
|
6792
|
-
decorators: []
|
|
6793
|
-
}), {
|
|
6794
|
-
at: nextPath
|
|
6795
|
-
}), Transforms.select(editor, {
|
|
6796
|
-
path: [...nextPath, 0],
|
|
6797
|
-
offset: 0
|
|
6798
|
-
}), editor.onChange();
|
|
6799
|
-
return;
|
|
6800
|
-
}
|
|
6801
|
-
}
|
|
6802
|
-
}
|
|
6803
|
-
if (isArrowUp && editor.selection) {
|
|
6804
|
-
const isFirstBlock = editor.selection.focus.path[0] === 0, focusBlock = Node.descendant(editor, editor.selection.focus.path.slice(0, 1));
|
|
6805
|
-
if (isFirstBlock && focusBlock && Editor.isVoid(editor, focusBlock)) {
|
|
6806
|
-
Transforms.insertNodes(editor, editor.pteCreateTextBlock({
|
|
6807
|
-
decorators: []
|
|
6808
|
-
}), {
|
|
6809
|
-
at: [0]
|
|
6810
|
-
}), Transforms.select(editor, {
|
|
6811
|
-
path: [0, 0],
|
|
6812
|
-
offset: 0
|
|
6813
|
-
}), editor.onChange();
|
|
6814
|
-
return;
|
|
6815
|
-
}
|
|
6816
|
-
}
|
|
6942
|
+
const isEnter = isHotkey("enter", event.nativeEvent), isTab = isHotkey("tab", event.nativeEvent), isShiftEnter = isHotkey("shift+enter", event.nativeEvent), isShiftTab = isHotkey("shift+tab", event.nativeEvent);
|
|
6817
6943
|
if ((isTab || isShiftTab) && editor.selection) {
|
|
6818
6944
|
const [focusChild] = Editor.node(editor, editor.selection.focus, {
|
|
6819
6945
|
depth: 2
|
|
@@ -7160,8 +7286,16 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7160
7286
|
return () => teardown();
|
|
7161
7287
|
}, [slateEditor, syncRangeDecorations]);
|
|
7162
7288
|
const handleCopy = useCallback((event) => {
|
|
7163
|
-
onCopy
|
|
7164
|
-
|
|
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) => {
|
|
7165
7299
|
const value_0 = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(value_0, slateEditor.selection, schemaTypes)?.focus.path || [], onPasteResult = onPaste?.({
|
|
7166
7300
|
event: event_0,
|
|
7167
7301
|
value: value_0,
|
|
@@ -7178,14 +7312,15 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7178
7312
|
editorActor.send({
|
|
7179
7313
|
type: "done loading"
|
|
7180
7314
|
});
|
|
7181
|
-
})) : event_0.nativeEvent.clipboardData &&
|
|
7315
|
+
})) : event_0.nativeEvent.clipboardData && editorActor.send({
|
|
7182
7316
|
type: "behavior event",
|
|
7183
7317
|
behaviorEvent: {
|
|
7184
7318
|
type: "paste",
|
|
7185
|
-
|
|
7319
|
+
data: event_0.nativeEvent.clipboardData
|
|
7186
7320
|
},
|
|
7187
|
-
editor: slateEditor
|
|
7188
|
-
|
|
7321
|
+
editor: slateEditor,
|
|
7322
|
+
nativeEvent: event_0
|
|
7323
|
+
}), debug("No result from custom paste handler, pasting normally");
|
|
7189
7324
|
}, [editorActor, onPaste, portableTextEditor, schemaTypes, slateEditor]), handleOnFocus = useCallback((event_1) => {
|
|
7190
7325
|
if (onFocus && onFocus(event_1), !event_1.isDefaultPrevented()) {
|
|
7191
7326
|
const selection = PortableTextEditor.getSelection(portableTextEditor);
|
|
@@ -7246,8 +7381,40 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7246
7381
|
}
|
|
7247
7382
|
}, [validateSelection, editableElement]);
|
|
7248
7383
|
const handleKeyDown = useCallback((event_5) => {
|
|
7249
|
-
props.onKeyDown && props.onKeyDown(event_5), event_5.isDefaultPrevented() || slateEditor.pteWithHotKeys(event_5)
|
|
7250
|
-
|
|
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(() => {
|
|
7251
7418
|
if (scrollSelectionIntoView !== void 0)
|
|
7252
7419
|
return scrollSelectionIntoView === null ? noop : (_editor, domRange) => {
|
|
7253
7420
|
scrollSelectionIntoView(portableTextEditor, domRange);
|
|
@@ -7294,6 +7461,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
|
|
|
7294
7461
|
onDOMBeforeInput: handleOnBeforeInput,
|
|
7295
7462
|
onFocus: handleOnFocus,
|
|
7296
7463
|
onKeyDown: handleKeyDown,
|
|
7464
|
+
onKeyUp: handleKeyUp,
|
|
7297
7465
|
onPaste: handlePaste,
|
|
7298
7466
|
readOnly,
|
|
7299
7467
|
renderPlaceholder: void 0,
|
|
@@ -7358,6 +7526,7 @@ export {
|
|
|
7358
7526
|
PortableTextEditor,
|
|
7359
7527
|
coreBehavior,
|
|
7360
7528
|
coreBehaviors,
|
|
7529
|
+
createCodeEditorBehaviors,
|
|
7361
7530
|
createLinkBehaviors,
|
|
7362
7531
|
createMarkdownBehaviors,
|
|
7363
7532
|
defineBehavior,
|