1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-30 04:23:11 +03:00

Lexical: Custom list nesting support

Added list nesting support to allow li > ul style nesting which lexical
didn't do by default.
Adds tab handling for inset/outset controls.
Will be a range of edge-case bugs to squash during testing.
This commit is contained in:
Dan Brown
2024-09-13 15:50:42 +01:00
parent 5083188ed8
commit 662110c269
7 changed files with 263 additions and 26 deletions

View File

@ -10,7 +10,7 @@ import {
ElementFormatType,
ElementNode, LexicalEditor,
LexicalNode,
TextFormatType
TextFormatType, TextNode
} from "lexical";
import {$findMatchingParent, $getNearestBlockElementAncestorOrThrow} from "@lexical/utils";
import {LexicalElementNodeCreator, LexicalNodeMatcher} from "../nodes";
@ -106,6 +106,57 @@ export function $selectSingleNode(node: LexicalNode) {
$setSelection(nodeSelection);
}
function getFirstTextNodeInNodes(nodes: LexicalNode[]): TextNode|null {
for (const node of nodes) {
if ($isTextNode(node)) {
return node;
}
if ($isElementNode(node)) {
const children = node.getChildren();
const textNode = getFirstTextNodeInNodes(children);
if (textNode !== null) {
return textNode;
}
}
}
return null;
}
function getLastTextNodeInNodes(nodes: LexicalNode[]): TextNode|null {
const revNodes = [...nodes].reverse();
for (const node of revNodes) {
if ($isTextNode(node)) {
return node;
}
if ($isElementNode(node)) {
const children = [...node.getChildren()].reverse();
const textNode = getLastTextNodeInNodes(children);
if (textNode !== null) {
return textNode;
}
}
}
return null;
}
export function $selectNodes(nodes: LexicalNode[]) {
if (nodes.length === 0) {
return;
}
const selection = $createRangeSelection();
const firstText = getFirstTextNodeInNodes(nodes);
const lastText = getLastTextNodeInNodes(nodes);
if (firstText && lastText) {
selection.setTextNodeRange(firstText, 0, lastText, lastText.getTextContentSize() || 0)
$setSelection(selection);
}
}
export function $toggleSelection(editor: LexicalEditor) {
const lastSelection = getLastSelection(editor);