|
1 | 1 | import { TextDirection } from "./text-direction.model"; |
2 | 2 | import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; |
3 | | -import { $findMatchingParent } from "@lexical/utils"; |
4 | | -import { |
5 | | - $getSelection, |
6 | | - $isRangeSelection, |
7 | | - COMMAND_PRIORITY_HIGH, |
8 | | - KEY_DOWN_COMMAND, |
9 | | - LexicalEditor, |
10 | | -} from "lexical"; |
| 3 | +import { LexicalEditor } from "lexical"; |
11 | 4 | import { useEffect } from "react"; |
12 | | -import { getNodeElementTagName } from "shared"; |
13 | 5 |
|
14 | 6 | export function TextDirectionPlugin({ textDirection }: { textDirection: TextDirection }): null { |
15 | 7 | const [editor] = useLexicalComposerContext(); |
16 | 8 | useTextDirection(editor, textDirection); |
17 | | - useArrowKeys(editor); |
18 | 9 | return null; |
19 | 10 | } |
20 | 11 |
|
21 | 12 | function useTextDirection(editor: LexicalEditor, textDirection: TextDirection) { |
22 | 13 | useEffect(() => { |
23 | | - function updateTextDirection() { |
24 | | - const rootElement = editor.getRootElement(); |
25 | | - if (!rootElement || textDirection === "auto") return; |
26 | | - |
27 | | - rootElement.dir = textDirection; |
28 | | - const placeholderClassName = editor._config.theme.placeholder; |
29 | | - const placeholderElement = document.getElementsByClassName( |
30 | | - placeholderClassName, |
31 | | - )[0] as HTMLElement; |
32 | | - if (placeholderElement) placeholderElement.dir = textDirection; |
33 | | - } |
34 | | - |
35 | | - updateTextDirection(); |
| 14 | + updateTextDirection(editor, textDirection); |
36 | 15 | return editor.registerUpdateListener(({ dirtyElements }) => { |
37 | | - if (dirtyElements.size > 0) updateTextDirection(); |
| 16 | + if (dirtyElements.size > 0) updateTextDirection(editor, textDirection); |
38 | 17 | }); |
39 | 18 | }, [editor, textDirection]); |
40 | 19 | } |
41 | 20 |
|
42 | | -/** |
43 | | - * Change the direction of the left and right arrow keys if the immediate text direction is |
44 | | - * different than the property setting. |
45 | | - * @param editor - The LexicalEditor instance used to access the DOM. |
46 | | - */ |
47 | | -function useArrowKeys(editor: LexicalEditor) { |
48 | | - useEffect(() => { |
49 | | - const $handleKeyDown = (event: KeyboardEvent): boolean => { |
50 | | - if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return false; |
51 | | - |
52 | | - const selection = $getSelection(); |
53 | | - if (!$isRangeSelection(selection)) return false; |
54 | | - |
55 | | - // Find the closest paragraph element |
56 | | - const anchorNode = selection.anchor.getNode(); |
57 | | - const paragraphNode = $findMatchingParent( |
58 | | - anchorNode, |
59 | | - (node) => getNodeElementTagName(node, editor) === "p", |
60 | | - ); |
61 | | - if (!paragraphNode) return false; |
62 | | - |
63 | | - // Get the DOM element corresponding to the paragraph node |
64 | | - const paragraphElement = editor.getElementByKey(paragraphNode.getKey()); |
65 | | - if (!paragraphElement) return false; |
66 | | - |
67 | | - // Check if directions are different |
68 | | - const inputDiv = paragraphElement.parentElement; |
69 | | - const paragraphDir = paragraphElement.dir || "ltr"; |
70 | | - const inputDir = (inputDiv?.dir ?? "") || "ltr"; |
71 | | - if (!inputDiv || paragraphDir === inputDir) return false; |
72 | | - |
73 | | - // Move in the opposite direction |
74 | | - const isBackward = |
75 | | - (inputDiv.dir === "rtl" && event.key === "ArrowLeft") || |
76 | | - (inputDiv.dir === "ltr" && event.key === "ArrowRight"); |
77 | | - selection.modify("move", isBackward, "character"); |
| 21 | +function updateTextDirection(editor: LexicalEditor, textDirection: TextDirection) { |
| 22 | + if (textDirection === "auto") return; |
78 | 23 |
|
79 | | - event.preventDefault(); |
80 | | - return true; |
81 | | - }; |
| 24 | + const rootElement = editor.getRootElement(); |
| 25 | + if (rootElement) rootElement.dir = textDirection; |
82 | 26 |
|
83 | | - return editor.registerCommand(KEY_DOWN_COMMAND, $handleKeyDown, COMMAND_PRIORITY_HIGH); |
84 | | - }, [editor]); |
| 27 | + const placeholderClassName = editor._config.theme.placeholder; |
| 28 | + const placeholderElement = document.getElementsByClassName( |
| 29 | + placeholderClassName, |
| 30 | + )[0] as HTMLElement; |
| 31 | + if (placeholderElement) placeholderElement.dir = textDirection; |
85 | 32 | } |
0 commit comments