-
-
Notifications
You must be signed in to change notification settings - Fork 531
Expand file tree
/
Copy pathCode.tsx
More file actions
33 lines (31 loc) · 1.16 KB
/
Code.tsx
File metadata and controls
33 lines (31 loc) · 1.16 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
import { $setBlocksType } from '@lexical/selection'
import { $getSelection, $isRangeSelection, LexicalEditor } from 'lexical'
import { $createCodeNode } from '@lexical/code'
import { LexicalIconName } from '@/Components/Icon/LexicalIcons'
import { BlockPickerOption } from '../BlockPickerPlugin/BlockPickerOption'
export const CodeBlock = {
name: 'Code Block',
iconName: 'code' as LexicalIconName,
keywords: ['javascript', 'python', 'js', 'codeblock'],
onSelect: (editor: LexicalEditor) =>
editor.update(() => {
const selection = $getSelection()
if ($isRangeSelection(selection)) {
if (selection.isCollapsed()) {
$setBlocksType(selection, () => $createCodeNode('plain'))
} else {
const textContent = selection.getTextContent()
const codeNode = $createCodeNode('plain')
selection.insertNodes([codeNode])
selection.insertRawText(textContent)
}
}
}),
}
export function GetCodeBlockOption(editor: LexicalEditor) {
return new BlockPickerOption(CodeBlock.name, {
iconName: CodeBlock.iconName,
keywords: CodeBlock.keywords,
onSelect: () => CodeBlock.onSelect(editor),
})
}