1 import {$isQuoteNode, HeadingNode, HeadingTagType} from "@lexical/rich-text";
2 import {$createTextNode, $getSelection, $insertNodes, LexicalEditor, LexicalNode} from "lexical";
4 $getBlockElementNodesInSelection,
6 $insertNewBlockNodeAtSelection, $selectionContainsNodeType,
7 $toggleSelectionBlockNodeType,
10 import {$createCustomHeadingNode, $isCustomHeadingNode} from "../nodes/custom-heading";
11 import {$createCustomParagraphNode, $isCustomParagraphNode} from "../nodes/custom-paragraph";
12 import {$createCustomQuoteNode} from "../nodes/custom-quote";
13 import {$createCodeBlockNode, $isCodeBlockNode, $openCodeEditorForNode, CodeBlockNode} from "../nodes/code-block";
14 import {$createCalloutNode, $isCalloutNode, CalloutCategory} from "../nodes/callout";
15 import {insertList, ListNode, ListType, removeList} from "@lexical/list";
16 import {$isCustomListNode} from "../nodes/custom-list";
17 import {$createLinkNode, $isLinkNode} from "@lexical/link";
19 const $isHeaderNodeOfTag = (node: LexicalNode | null | undefined, tag: HeadingTagType) => {
20 return $isCustomHeadingNode(node) && (node as HeadingNode).getTag() === tag;
23 export function toggleSelectionAsHeading(editor: LexicalEditor, tag: HeadingTagType) {
25 $toggleSelectionBlockNodeType(
26 (node) => $isHeaderNodeOfTag(node, tag),
27 () => $createCustomHeadingNode(tag),
32 export function toggleSelectionAsParagraph(editor: LexicalEditor) {
34 $toggleSelectionBlockNodeType($isCustomParagraphNode, $createCustomParagraphNode);
38 export function toggleSelectionAsBlockquote(editor: LexicalEditor) {
40 $toggleSelectionBlockNodeType($isQuoteNode, $createCustomQuoteNode);
44 export function toggleSelectionAsList(editor: LexicalEditor, type: ListType) {
45 editor.getEditorState().read(() => {
46 const selection = $getSelection();
47 const listSelected = $selectionContainsNodeType(selection, (node: LexicalNode | null | undefined): boolean => {
48 return $isCustomListNode(node) && (node as ListNode).getListType() === type;
54 insertList(editor, type);
59 export function formatCodeBlock(editor: LexicalEditor) {
60 editor.getEditorState().read(() => {
61 const selection = $getSelection();
62 const lastSelection = getLastSelection(editor);
63 const codeBlock = $getNodeFromSelection(lastSelection, $isCodeBlockNode) as (CodeBlockNode | null);
64 if (codeBlock === null) {
66 const codeBlock = $createCodeBlockNode();
67 codeBlock.setCode(selection?.getTextContent() || '');
68 $insertNewBlockNodeAtSelection(codeBlock, true);
69 $openCodeEditorForNode(editor, codeBlock);
70 codeBlock.selectStart();
73 $openCodeEditorForNode(editor, codeBlock);
78 export function cycleSelectionCalloutFormats(editor: LexicalEditor) {
80 const selection = $getSelection();
81 const blocks = $getBlockElementNodesInSelection(selection);
84 for (const block of blocks) {
85 if (!$isCalloutNode(block)) {
86 block.replace($createCalloutNode('info'), true);
95 const types: CalloutCategory[] = ['info', 'warning', 'danger', 'success'];
96 for (const block of blocks) {
97 if ($isCalloutNode(block)) {
98 const type = block.getCategory();
99 const typeIndex = types.indexOf(type);
100 const newIndex = (typeIndex + 1) % types.length;
101 const newType = types[newIndex];
102 block.setCategory(newType);
108 export function insertOrUpdateLink(editor: LexicalEditor, linkDetails: {text: string, title: string, target: string, url: string}) {
109 editor.update(() => {
110 const selection = $getSelection();
111 let link = $getNodeFromSelection(selection, $isLinkNode);
112 if ($isLinkNode(link)) {
113 link.setURL(linkDetails.url);
114 link.setTarget(linkDetails.target);
115 link.setTitle(linkDetails.title);
117 link = $createLinkNode(linkDetails.url, {
118 title: linkDetails.title,
119 target: linkDetails.target,
122 $insertNodes([link]);
125 if ($isLinkNode(link)) {
126 for (const child of link.getChildren()) {
129 link.append($createTextNode(linkDetails.text));