@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
98 lines • 3.89 kB
JavaScript
import { $createListNode, $isListItemNode, INSERT_ORDERED_LIST_COMMAND, INSERT_UNORDERED_LIST_COMMAND } from '@lexical/list';
import { mergeRegister } from '@lexical/utils';
import { $createParagraphNode, $getSelection, $isRangeSelection, $isRootNode, $isTextNode, COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_LOW, INDENT_CONTENT_COMMAND, INSERT_TAB_COMMAND, KEY_BACKSPACE_COMMAND, KEY_TAB_COMMAND, OUTDENT_CONTENT_COMMAND } from 'lexical';
import { HotkeyEnum } from "../../../types/hotkey";
import { $indentOverTab } from "../utils";
export function registerListCommands(editor, kernel, options) {
var _ref = options || {},
_ref$enableHotkey = _ref.enableHotkey,
enableHotkey = _ref$enableHotkey === void 0 ? true : _ref$enableHotkey;
return mergeRegister(
// Hotkey registrations
kernel.registerHotkey(HotkeyEnum.BulletList, function () {
return editor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND, undefined);
}, {
enabled: enableHotkey,
preventDefault: true,
stopImmediatePropagation: true
}), kernel.registerHotkey(HotkeyEnum.NumberList, function () {
return editor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND, undefined);
}, {
enabled: enableHotkey,
preventDefault: true,
stopImmediatePropagation: true
}),
// Tab key command for indentation
editor.registerCommand(KEY_TAB_COMMAND, function (event) {
var selection = $getSelection();
if (!$isRangeSelection(selection)) {
return false;
}
event.preventDefault();
var command = $indentOverTab(selection) ? event.shiftKey ? OUTDENT_CONTENT_COMMAND : INDENT_CONTENT_COMMAND : INSERT_TAB_COMMAND;
return editor.dispatchCommand(command, undefined);
}, COMMAND_PRIORITY_EDITOR),
// Backspace key command for list item handling
editor.registerCommand(KEY_BACKSPACE_COMMAND, function (event) {
var _listItemNode$getPare;
var selection = $getSelection();
if (!$isRangeSelection(selection) || !selection.isCollapsed()) {
return false;
}
var anchor = selection.anchor;
if (anchor.offset !== 0) {
return false;
}
var anchorNode = anchor.getNode();
var listItemNode;
if ($isListItemNode(anchorNode)) {
listItemNode = anchorNode;
} else if ($isTextNode(anchorNode)) {
// Do not handle non-leading text nodes
if (anchorNode.getPreviousSibling()) {
return false;
}
var parent = anchorNode.getParentOrThrow();
if (!$isListItemNode(parent)) {
return false;
}
listItemNode = parent;
}
if (!listItemNode || !$isRootNode((_listItemNode$getPare = listItemNode.getParent()) === null || _listItemNode$getPare === void 0 ? void 0 : _listItemNode$getPare.getParent())) {
return false;
}
var listNode = listItemNode.getParentOrThrow();
queueMicrotask(function () {
editor.update(function () {
// Add null check since listItemNode might be undefined in this closure
if (!listItemNode) return;
var newlistNode;
var isFirst = listItemNode.getPreviousSibling() === null;
if (isFirst) {
var _p = listItemNode.replace($createParagraphNode(), true);
_p.select(0, 0);
return;
}
var next = listItemNode.getNextSibling();
if (next) {
newlistNode = $createListNode(listNode.getListType(), listItemNode.getValue());
}
while (next && newlistNode) {
next.remove();
newlistNode.append(next);
next = next.getNextSibling();
}
var p = listItemNode.replace($createParagraphNode(), true);
p.remove();
listNode.insertAfter(p);
if (newlistNode) {
p.insertAfter(newlistNode);
}
p.select(0, 0);
});
});
event.stopImmediatePropagation();
event.preventDefault();
return true;
}, COMMAND_PRIORITY_LOW));
}