-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCommandMenuPlugin.tsx
More file actions
59 lines (51 loc) · 2.03 KB
/
CommandMenuPlugin.tsx
File metadata and controls
59 lines (51 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
import { mergeRegister } from "@lexical/utils";
import { COMMAND_PRIORITY_NORMAL, DROP_COMMAND, KEY_DOWN_COMMAND, PASTE_COMMAND } from "lexical";
import { useEffect } from "react";
import { LoggerBasic } from "shared";
/**
* This plugin prevents the backslash or forward slash key from being typed, or pasted or dragged.
* @returns `null`. This plugin has no DOM presence.
*/
export function CommandMenuPlugin({ logger }: { logger?: LoggerBasic }): null {
const [editor] = useLexicalComposerContext();
useEffect(() => {
return mergeRegister(
// When the backslash or forward slash key is typed.
editor.registerCommand(
KEY_DOWN_COMMAND,
(event: KeyboardEvent) => {
if (event.key !== "\\" && event.key !== "/") return false;
event.preventDefault();
return true;
},
COMMAND_PRIORITY_NORMAL,
),
// When the backslash or forward slash character is pasted into the editor.
editor.registerCommand(
PASTE_COMMAND,
(event: ClipboardEvent) => {
const text = event.clipboardData?.getData("text/plain");
if (!text || (!text.includes("\\") && !text.includes("/"))) return false;
logger?.info("CommandMenuPlugin: paste containing backslash or forward slash ignored.");
event.preventDefault();
return true;
},
COMMAND_PRIORITY_NORMAL,
),
// When the backslash or forward slash character is dragged into the editor.
editor.registerCommand(
DROP_COMMAND,
(event: DragEvent) => {
const text = event.dataTransfer?.getData("text/plain");
if (!text || (!text.includes("\\") && !text.includes("/"))) return false;
logger?.info("CommandMenuPlugin: drag containing backslash or forward slash ignored.");
event.preventDefault();
return true;
},
COMMAND_PRIORITY_NORMAL,
),
);
}, [editor, logger]);
return null;
}