]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/utils/formats.ts
Meta: Added lexical licensing info and added TS/JS CI testing
[bookstack] / resources / js / wysiwyg / utils / formats.ts
1 import {$isQuoteNode, HeadingNode, HeadingTagType} from "@lexical/rich-text";
2 import {$createTextNode, $getSelection, $insertNodes, LexicalEditor, LexicalNode} from "lexical";
3 import {
4     $getBlockElementNodesInSelection,
5     $getNodeFromSelection,
6     $insertNewBlockNodeAtSelection, $selectionContainsNodeType,
7     $toggleSelectionBlockNodeType,
8     getLastSelection
9 } from "./selection";
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";
18
19 const $isHeaderNodeOfTag = (node: LexicalNode | null | undefined, tag: HeadingTagType) => {
20     return $isCustomHeadingNode(node) && (node as HeadingNode).getTag() === tag;
21 };
22
23 export function toggleSelectionAsHeading(editor: LexicalEditor, tag: HeadingTagType) {
24     editor.update(() => {
25         $toggleSelectionBlockNodeType(
26             (node) => $isHeaderNodeOfTag(node, tag),
27             () => $createCustomHeadingNode(tag),
28         )
29     });
30 }
31
32 export function toggleSelectionAsParagraph(editor: LexicalEditor) {
33     editor.update(() => {
34         $toggleSelectionBlockNodeType($isCustomParagraphNode, $createCustomParagraphNode);
35     });
36 }
37
38 export function toggleSelectionAsBlockquote(editor: LexicalEditor) {
39     editor.update(() => {
40         $toggleSelectionBlockNodeType($isQuoteNode, $createCustomQuoteNode);
41     });
42 }
43
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;
49         });
50
51         if (listSelected) {
52             removeList(editor);
53         } else {
54             insertList(editor, type);
55         }
56     });
57 }
58
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) {
65             editor.update(() => {
66                 const codeBlock = $createCodeBlockNode();
67                 codeBlock.setCode(selection?.getTextContent() || '');
68                 $insertNewBlockNodeAtSelection(codeBlock, true);
69                 $openCodeEditorForNode(editor, codeBlock);
70                 codeBlock.selectStart();
71             });
72         } else {
73             $openCodeEditorForNode(editor, codeBlock);
74         }
75     });
76 }
77
78 export function cycleSelectionCalloutFormats(editor: LexicalEditor) {
79     editor.update(() => {
80         const selection = $getSelection();
81         const blocks = $getBlockElementNodesInSelection(selection);
82
83         let created = false;
84         for (const block of blocks) {
85             if (!$isCalloutNode(block)) {
86                 block.replace($createCalloutNode('info'), true);
87                 created = true;
88             }
89         }
90
91         if (created) {
92             return;
93         }
94
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);
103             }
104         }
105     });
106 }
107
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);
116         } else {
117             link = $createLinkNode(linkDetails.url, {
118                 title: linkDetails.title,
119                 target: linkDetails.target,
120             });
121
122             $insertNodes([link]);
123         }
124
125         if ($isLinkNode(link)) {
126             for (const child of link.getChildren()) {
127                 child.remove(true);
128             }
129             link.append($createTextNode(linkDetails.text));
130         }
131     });
132 }