1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-12-23 23:02:08 +03:00

Lexical: Added better selection display for collapisble blocks

This commit is contained in:
Dan Brown
2025-08-27 12:51:36 +01:00
parent 849bc4d6c3
commit 519acaf324
3 changed files with 55 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ import {modals} from "./ui/defaults/modals";
import {CodeBlockDecorator} from "./ui/decorators/code-block";
import {DiagramDecorator} from "./ui/decorators/diagram";
import {registerMouseHandling} from "./services/mouse-handling";
import {registerSelectionHandling} from "./services/selection-handling";
const theme = {
text: {
@@ -53,6 +54,7 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
registerShortcuts(context),
registerKeyboardHandling(context),
registerMouseHandling(context),
registerSelectionHandling(context),
registerTableResizer(editor, context.scrollDOM),
registerTableSelectionHandler(editor),
registerTaskListHandler(editor, context.editorDOM),

View File

@@ -0,0 +1,49 @@
import {EditorUiContext} from "../ui/framework/core";
import {
$getSelection,
COMMAND_PRIORITY_LOW,
SELECTION_CHANGE_COMMAND
} from "lexical";
import {$isDetailsNode} from "@lexical/rich-text/LexicalDetailsNode";
const trackedDomNodes = new Set<HTMLElement>();
/**
* Set a selection indicator on nodes which require it.
* @param context
*/
function setSelectionIndicator(context: EditorUiContext): boolean {
for (const domNode of trackedDomNodes) {
domNode.classList.remove('selected');
trackedDomNodes.delete(domNode);
}
const selection = $getSelection();
const nodes = selection?.getNodes() || [];
if (nodes.length === 1) {
if ($isDetailsNode(nodes[0])) {
const domEl = context.editor.getElementByKey(nodes[0].getKey());
if (domEl) {
domEl.classList.add('selected');
trackedDomNodes.add(domEl);
}
}
}
return false;
}
export function registerSelectionHandling(context: EditorUiContext): () => void {
const unregisterSelectionChange = context.editor.registerCommand(SELECTION_CHANGE_COMMAND, (): boolean => {
setSelectionIndicator(context);
return false;
}, COMMAND_PRIORITY_LOW);
return () => {
unregisterSelectionChange();
};
}