]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/services/keyboard-handling.ts
Lexical: Updated tests for node changes
[bookstack] / resources / js / wysiwyg / services / keyboard-handling.ts
index 65a8e42547daade99564d2cc27585a49050b4c54..6a1345fac6d5596cfdf92c19919d8bf4fc9bd57c 100644 (file)
@@ -1,18 +1,21 @@
 import {EditorUiContext} from "../ui/framework/core";
 import {
+    $createParagraphNode,
+    $getSelection,
     $isDecoratorNode,
     COMMAND_PRIORITY_LOW,
     KEY_BACKSPACE_COMMAND,
     KEY_DELETE_COMMAND,
-    KEY_ENTER_COMMAND,
+    KEY_ENTER_COMMAND, KEY_TAB_COMMAND,
     LexicalEditor,
     LexicalNode
 } from "lexical";
-import {$isImageNode} from "../nodes/image";
-import {$isMediaNode} from "../nodes/media";
+import {$isImageNode} from "@lexical/rich-text/LexicalImageNode";
+import {$isMediaNode} from "@lexical/rich-text/LexicalMediaNode";
 import {getLastSelection} from "../utils/selection";
 import {$getNearestNodeBlockParent} from "../utils/nodes";
-import {$createCustomParagraphNode} from "../nodes/custom-paragraph";
+import {$setInsetForSelection} from "../utils/lists";
+import {$isListItemNode} from "@lexical/list";
 
 function isSingleSelectedNode(nodes: LexicalNode[]): boolean {
     if (nodes.length === 1) {
@@ -42,7 +45,7 @@ function insertAfterSingleSelectedNode(editor: LexicalEditor, event: KeyboardEve
         if (nearestBlock) {
             requestAnimationFrame(() => {
                 editor.update(() => {
-                    const newParagraph = $createCustomParagraphNode();
+                    const newParagraph = $createParagraphNode();
                     nearestBlock.insertAfter(newParagraph);
                     newParagraph.select();
                 });
@@ -55,6 +58,21 @@ function insertAfterSingleSelectedNode(editor: LexicalEditor, event: KeyboardEve
     return false;
 }
 
+function handleInsetOnTab(editor: LexicalEditor, event: KeyboardEvent|null): boolean {
+    const change = event?.shiftKey ? -40 : 40;
+    const selection = $getSelection();
+    const nodes = selection?.getNodes() || [];
+    if (nodes.length > 1 || (nodes.length === 1 && $isListItemNode(nodes[0].getParent()))) {
+        editor.update(() => {
+            $setInsetForSelection(editor, change);
+        });
+        event?.preventDefault();
+        return true;
+    }
+
+    return false;
+}
+
 export function registerKeyboardHandling(context: EditorUiContext): () => void {
     const unregisterBackspace = context.editor.registerCommand(KEY_BACKSPACE_COMMAND, (): boolean => {
         deleteSingleSelectedNode(context.editor);
@@ -70,9 +88,14 @@ export function registerKeyboardHandling(context: EditorUiContext): () => void {
         return insertAfterSingleSelectedNode(context.editor, event);
     }, COMMAND_PRIORITY_LOW);
 
+    const unregisterTab = context.editor.registerCommand(KEY_TAB_COMMAND, (event): boolean => {
+        return handleInsetOnTab(context.editor, event);
+    }, COMMAND_PRIORITY_LOW);
+
     return () => {
         unregisterBackspace();
         unregisterDelete();
         unregisterEnter();
+        unregisterTab();
     };
 }
\ No newline at end of file