@collabchron/notiq 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/README.md +71 -0
  2. package/components.json +21 -0
  3. package/eslint.config.mjs +16 -0
  4. package/next.config.ts +12 -0
  5. package/package.json +108 -0
  6. package/postcss.config.mjs +5 -0
  7. package/public/file.svg +1 -0
  8. package/public/globe.svg +1 -0
  9. package/public/images/icons/plus.svg +10 -0
  10. package/public/next.svg +1 -0
  11. package/public/vercel.svg +1 -0
  12. package/public/window.svg +1 -0
  13. package/src/app/actions.ts +2 -0
  14. package/src/app/api/ai/route.ts +175 -0
  15. package/src/app/api/edgestore/[...edgestore]/route.ts +28 -0
  16. package/src/app/favicon.ico +0 -0
  17. package/src/app/globals.css +205 -0
  18. package/src/app/layout.tsx +38 -0
  19. package/src/app/page.tsx +12 -0
  20. package/src/components/editor/Core.tsx +220 -0
  21. package/src/components/editor/hooks/instructions-messages.ts +300 -0
  22. package/src/components/editor/hooks/use-mobile.ts +19 -0
  23. package/src/components/editor/hooks/useReport.ts +67 -0
  24. package/src/components/editor/hooks/useResizeObservert.ts +22 -0
  25. package/src/components/editor/index.tsx +39 -0
  26. package/src/components/editor/lexical-on-change.tsx +28 -0
  27. package/src/components/editor/nodes/CollapsibleNode/CollapsibleContainerNode.ts +92 -0
  28. package/src/components/editor/nodes/CollapsibleNode/CollapsibleContentNode.ts +65 -0
  29. package/src/components/editor/nodes/CollapsibleNode/CollapsibleTitleNode.ts +105 -0
  30. package/src/components/editor/nodes/EquationNode/EquationComponent.tsx +143 -0
  31. package/src/components/editor/nodes/EquationNode/EquationNode.tsx +170 -0
  32. package/src/components/editor/nodes/ExcalidrawNode/ExcalidrawComponent.tsx +228 -0
  33. package/src/components/editor/nodes/ExcalidrawNode/ExcalidrawImage.tsx +137 -0
  34. package/src/components/editor/nodes/ExcalidrawNode/ImageResizer.tsx +317 -0
  35. package/src/components/editor/nodes/ExcalidrawNode/index.tsx +204 -0
  36. package/src/components/editor/nodes/FigmaNode/FigmaNode.tsx +134 -0
  37. package/src/components/editor/nodes/Hint/HintComponet.tsx +221 -0
  38. package/src/components/editor/nodes/Hint/index.tsx +190 -0
  39. package/src/components/editor/nodes/ImageNode/index.tsx +328 -0
  40. package/src/components/editor/nodes/InlineImageNode/InlineImageComponent.tsx +383 -0
  41. package/src/components/editor/nodes/InlineImageNode/InlineImageNode.css +94 -0
  42. package/src/components/editor/nodes/InlineImageNode/InlineImageNode.tsx +309 -0
  43. package/src/components/editor/nodes/LayoutNode/LayoutContainerNode.ts +146 -0
  44. package/src/components/editor/nodes/LayoutNode/LayoutItemNode.ts +79 -0
  45. package/src/components/editor/nodes/PollNode/index.tsx +204 -0
  46. package/src/components/editor/nodes/Stepper/index.tsx +260 -0
  47. package/src/components/editor/nodes/TweetNode/index.tsx +214 -0
  48. package/src/components/editor/nodes/index.ts +81 -0
  49. package/src/components/editor/plugins/AutoEmbedPlugin/index.tsx +350 -0
  50. package/src/components/editor/plugins/AutoLinkPlugin/index.tsx +56 -0
  51. package/src/components/editor/plugins/CodeActionMenuPlugin/components/CopyButton.tsx +70 -0
  52. package/src/components/editor/plugins/CodeActionMenuPlugin/components/PrettierButton.tsx +192 -0
  53. package/src/components/editor/plugins/CodeActionMenuPlugin/index.tsx +217 -0
  54. package/src/components/editor/plugins/CodeActionMenuPlugin/utils.ts +26 -0
  55. package/src/components/editor/plugins/CodeHighlightPlugin/index.ts +21 -0
  56. package/src/components/editor/plugins/CollapsiblePlugin/Collapsible.css +76 -0
  57. package/src/components/editor/plugins/CollapsiblePlugin/index.ts +228 -0
  58. package/src/components/editor/plugins/DragDropPastePlugin/index.tsx +44 -0
  59. package/src/components/editor/plugins/DraggableBlockPlugin/index.tsx +52 -0
  60. package/src/components/editor/plugins/EquationsPlugin/index.tsx +85 -0
  61. package/src/components/editor/plugins/ExcalidrawPlugin/index.tsx +98 -0
  62. package/src/components/editor/plugins/FigmaPlugin/index.tsx +42 -0
  63. package/src/components/editor/plugins/FloatingLinkEditorPlugin/index.tsx +445 -0
  64. package/src/components/editor/plugins/FloatingTextFormatToolbarPlugin/index.tsx +275 -0
  65. package/src/components/editor/plugins/ImagesPlugin/index.tsx +222 -0
  66. package/src/components/editor/plugins/InlineImagePlugin/index.tsx +351 -0
  67. package/src/components/editor/plugins/LayoutPlugin/index.tsx +238 -0
  68. package/src/components/editor/plugins/LinkPlugin/index.tsx +36 -0
  69. package/src/components/editor/plugins/LinkWithMetaData/index.tsx +271 -0
  70. package/src/components/editor/plugins/MarkdownShortcutPlugin/index.tsx +11 -0
  71. package/src/components/editor/plugins/MarkdownTransformers/index.tsx +304 -0
  72. package/src/components/editor/plugins/PollPlugin/index.tsx +49 -0
  73. package/src/components/editor/plugins/ShortcutsPlugin/index.tsx +180 -0
  74. package/src/components/editor/plugins/ShortcutsPlugin/shortcuts.ts +253 -0
  75. package/src/components/editor/plugins/SlashCommand/index.tsx +621 -0
  76. package/src/components/editor/plugins/SpeechToTextPlugin/index.ts +127 -0
  77. package/src/components/editor/plugins/TabFocusPlugin/index.ts +58 -0
  78. package/src/components/editor/plugins/TableCellActionMenuPlugin/index.tsx +759 -0
  79. package/src/components/editor/plugins/TableCellResizer/index.tsx +438 -0
  80. package/src/components/editor/plugins/TableHoverActionsPlugin/index.tsx +314 -0
  81. package/src/components/editor/plugins/TablePlugin/index.tsx +99 -0
  82. package/src/components/editor/plugins/ToolbarPlugin/index.tsx +522 -0
  83. package/src/components/editor/plugins/TwitterPlugin/index.ts +35 -0
  84. package/src/components/editor/plugins/YouTubeNode/index.tsx +179 -0
  85. package/src/components/editor/plugins/YouTubePlugin/index.ts +41 -0
  86. package/src/components/editor/themes/editor-theme.ts +113 -0
  87. package/src/components/editor/themes/theme.css +377 -0
  88. package/src/components/editor/utils/ai.ts +291 -0
  89. package/src/components/editor/utils/canUseDOM.ts +12 -0
  90. package/src/components/editor/utils/editorFormatting.ts +282 -0
  91. package/src/components/editor/utils/environment.ts +50 -0
  92. package/src/components/editor/utils/extract-data.ts +166 -0
  93. package/src/components/editor/utils/getAllLexicalChildren.ts +13 -0
  94. package/src/components/editor/utils/getDOMRangeRect.ts +27 -0
  95. package/src/components/editor/utils/getSelectedNode.ts +27 -0
  96. package/src/components/editor/utils/gif.ts +29 -0
  97. package/src/components/editor/utils/invariant.ts +15 -0
  98. package/src/components/editor/utils/setFloatingElemPosition.ts +51 -0
  99. package/src/components/editor/utils/setFloatingElemPositionForLinkEditor.ts +40 -0
  100. package/src/components/editor/utils/setNodePlaceholderFromSelection/getNodePlaceholder.ts +51 -0
  101. package/src/components/editor/utils/setNodePlaceholderFromSelection/setNodePlaceholderFromSelection.ts +15 -0
  102. package/src/components/editor/utils/setNodePlaceholderFromSelection/setPlaceholderOnSelection.ts +114 -0
  103. package/src/components/editor/utils/setNodePlaceholderFromSelection/styles.css +6 -0
  104. package/src/components/editor/utils/url.ts +109 -0
  105. package/src/components/editor/utils/useLayoutEffect.ts +13 -0
  106. package/src/components/providers/QueryProvider.tsx +15 -0
  107. package/src/components/providers/SharedHistoryContext.tsx +28 -0
  108. package/src/components/providers/ToolbarContext.tsx +123 -0
  109. package/src/components/providers/theme-provider.tsx +11 -0
  110. package/src/components/theme/ModeToggle.tsx +40 -0
  111. package/src/components/ui/FileInput.tsx +40 -0
  112. package/src/components/ui/Input.css +32 -0
  113. package/src/components/ui/Select.css +42 -0
  114. package/src/components/ui/Select.tsx +36 -0
  115. package/src/components/ui/TextInput.tsx +48 -0
  116. package/src/components/ui/ai/ai-button.tsx +574 -0
  117. package/src/components/ui/ai/border.tsx +99 -0
  118. package/src/components/ui/ai/placeholder-input-vanish.tsx +282 -0
  119. package/src/components/ui/button.tsx +89 -0
  120. package/src/components/ui/card.tsx +76 -0
  121. package/src/components/ui/checkbox.tsx +30 -0
  122. package/src/components/ui/command.tsx +153 -0
  123. package/src/components/ui/dialog/Dialog.css +25 -0
  124. package/src/components/ui/dialog/Dialog.tsx +34 -0
  125. package/src/components/ui/dialog.tsx +122 -0
  126. package/src/components/ui/drop-downs/background-color.tsx +183 -0
  127. package/src/components/ui/drop-downs/block-format.tsx +159 -0
  128. package/src/components/ui/drop-downs/code.tsx +42 -0
  129. package/src/components/ui/drop-downs/color.tsx +177 -0
  130. package/src/components/ui/drop-downs/font-size.tsx +138 -0
  131. package/src/components/ui/drop-downs/font.tsx +155 -0
  132. package/src/components/ui/drop-downs/index.tsx +122 -0
  133. package/src/components/ui/drop-downs/insert-node.tsx +213 -0
  134. package/src/components/ui/drop-downs/text-align.tsx +123 -0
  135. package/src/components/ui/drop-downs/text-format.tsx +104 -0
  136. package/src/components/ui/dropdown-menu.tsx +201 -0
  137. package/src/components/ui/equation/EquationEditor.css +38 -0
  138. package/src/components/ui/equation/EquationEditor.tsx +56 -0
  139. package/src/components/ui/equation/KatexEquationAlterer.css +41 -0
  140. package/src/components/ui/equation/KatexEquationAlterer.tsx +83 -0
  141. package/src/components/ui/equation/KatexRenderer.tsx +66 -0
  142. package/src/components/ui/excalidraw/ExcalidrawModal.css +64 -0
  143. package/src/components/ui/excalidraw/ExcalidrawModal.tsx +234 -0
  144. package/src/components/ui/excalidraw/Modal.css +62 -0
  145. package/src/components/ui/excalidraw/Modal.tsx +110 -0
  146. package/src/components/ui/hover-card.tsx +29 -0
  147. package/src/components/ui/image/error-image.tsx +17 -0
  148. package/src/components/ui/image/file-upload.tsx +240 -0
  149. package/src/components/ui/image/image-resizer.tsx +297 -0
  150. package/src/components/ui/image/image-toolbar.tsx +264 -0
  151. package/src/components/ui/image/index.tsx +408 -0
  152. package/src/components/ui/image/lazy-image.tsx +68 -0
  153. package/src/components/ui/image/lazy-video.tsx +71 -0
  154. package/src/components/ui/input.tsx +22 -0
  155. package/src/components/ui/models/custom-dialog.tsx +320 -0
  156. package/src/components/ui/models/insert-gif.tsx +90 -0
  157. package/src/components/ui/models/insert-image.tsx +52 -0
  158. package/src/components/ui/models/insert-poll.tsx +29 -0
  159. package/src/components/ui/models/insert-table.tsx +62 -0
  160. package/src/components/ui/models/use-model.tsx +91 -0
  161. package/src/components/ui/poll/poll-component.tsx +304 -0
  162. package/src/components/ui/popover.tsx +33 -0
  163. package/src/components/ui/progress.tsx +28 -0
  164. package/src/components/ui/scroll-area.tsx +48 -0
  165. package/src/components/ui/separator.tsx +31 -0
  166. package/src/components/ui/skeleton.tsx +15 -0
  167. package/src/components/ui/sonner.tsx +31 -0
  168. package/src/components/ui/stepper/step.tsx +179 -0
  169. package/src/components/ui/stepper/stepper.tsx +89 -0
  170. package/src/components/ui/textarea.tsx +22 -0
  171. package/src/components/ui/toggle.tsx +71 -0
  172. package/src/components/ui/tooltip.tsx +32 -0
  173. package/src/components/ui/write/text-format-floting-toolbar.tsx +346 -0
  174. package/src/lib/edgestore.ts +9 -0
  175. package/src/lib/pinecone-client.ts +0 -0
  176. package/src/lib/utils.ts +6 -0
  177. package/src/utils/docSerialization.ts +77 -0
  178. package/src/utils/emoji-list.ts +16615 -0
  179. package/src/utils/getDOMRangeRect.ts +27 -0
  180. package/src/utils/getSelectedNode.ts +27 -0
  181. package/src/utils/getThemeSelector.ts +25 -0
  182. package/src/utils/isMobileWidth.ts +7 -0
  183. package/src/utils/joinClasses.ts +13 -0
  184. package/src/utils/setFloatingElemPosition.ts +74 -0
  185. package/src/utils/setFloatingElemPositionForLinkEditor.ts +46 -0
  186. package/src/utils/swipe.ts +127 -0
  187. package/src/utils/url.ts +38 -0
  188. package/tsconfig.json +27 -0
@@ -0,0 +1,291 @@
1
+ import { $createCodeNode } from "@lexical/code";
2
+ import {
3
+ $createParagraphNode,
4
+ $createRangeSelection,
5
+ $createTextNode,
6
+ $getRoot,
7
+ $getSelection,
8
+ $isRangeSelection,
9
+ $isTextNode,
10
+ createEditor,
11
+ ElementNode,
12
+ LexicalEditor,
13
+ LexicalNode,
14
+ TextNode,
15
+ } from "lexical";
16
+ import {
17
+ $createHeadingNode,} from '@lexical/rich-text';
18
+ import { $createStepperNode, StepsType } from "../nodes/Stepper";
19
+
20
+ function getWordsBeforeSelection(
21
+ editor: LexicalEditor,
22
+ numWords: number
23
+ ): string {
24
+ let wordsBeforeSelection = "";
25
+
26
+ editor.update(() => {
27
+ const selection = $getSelection();
28
+ if (!$isRangeSelection(selection)) {
29
+ console.warn("No valid text selection found.");
30
+ return;
31
+ }
32
+
33
+ const anchor = selection.anchor;
34
+ const firstTextNode = findFirstTextNode($getRoot());
35
+
36
+ if (!firstTextNode) {
37
+ console.warn("No text nodes found in the document.");
38
+ return;
39
+ }
40
+
41
+ // Create a range from the first text node start to the selection anchor
42
+ const range = $createRangeSelection();
43
+ range.anchor.set(firstTextNode.__key, 0, 'text');
44
+ range.focus.set(anchor.key, anchor.offset, anchor.type);
45
+
46
+ const textContent = range.getTextContent();
47
+ const words = textContent.split(/\s+/).filter(word => word.length > 0);
48
+
49
+ const startIndex = Math.max(0, words.length - numWords);
50
+ wordsBeforeSelection = words.slice(startIndex, words.length).join(" ");
51
+ });
52
+
53
+ return wordsBeforeSelection;
54
+ }
55
+
56
+ // Helper function to find the first text node in document order
57
+ function findFirstTextNode(node: LexicalNode): TextNode | null {
58
+ if ($isTextNode(node)) {
59
+ return node;
60
+ }
61
+
62
+ const children = (node as ElementNode).getChildren();
63
+ for (const child of children) {
64
+ const result = findFirstTextNode(child);
65
+ if (result) {
66
+ return result;
67
+ }
68
+ }
69
+
70
+ return null;
71
+ }
72
+
73
+
74
+ function isJSON(text: string): boolean {
75
+ try {
76
+ JSON.parse(text);
77
+ return true;
78
+ } catch (err) {
79
+ return false;
80
+ }
81
+ }
82
+ function createEditorWithText(text: string): LexicalEditor {
83
+ const editor = createEditor();
84
+ const editorState = editor.parseEditorState(
85
+ JSON.stringify({
86
+ root: {
87
+ children: [
88
+ {
89
+ children: [
90
+ {
91
+ detail: 0,
92
+ format: 0,
93
+ mode: "normal",
94
+ style: "",
95
+ text: text,
96
+ type: "text",
97
+ version: 1,
98
+ },
99
+ ],
100
+ direction: null,
101
+ format: "",
102
+ indent: 0,
103
+ type: "paragraph",
104
+ version: 1,
105
+ textFormat: 0,
106
+ textStyle: "",
107
+ },
108
+ ],
109
+ direction: null,
110
+ format: "",
111
+ indent: 0,
112
+ type: "root",
113
+ version: 1,
114
+ },
115
+ })
116
+ );
117
+ editor.setEditorState(editorState);
118
+ return editor;
119
+ }
120
+
121
+ function GenerateSteps(json:string, editor: LexicalEditor){
122
+ const stepsData = JSON.parse(json);
123
+ const steps: StepsType = stepsData.map((step: { id: number; title: string; content: string }) => {
124
+ return {
125
+ id: step.id,
126
+ title: step.title,
127
+ content: createEditorWithText(step.content),
128
+ };
129
+ });
130
+ editor.update(()=>{
131
+ const selection = $getSelection();
132
+ if ($isRangeSelection(selection)) {
133
+ const stepperNode = $createStepperNode(steps);
134
+ selection.insertNodes([stepperNode]);
135
+ }
136
+ return
137
+ })
138
+ }
139
+
140
+ // getMore = number of words to get if the selection was less than 30 len.
141
+ const getSelectedText = (editor: LexicalEditor,getMore?:number) => {
142
+ let selectedText = "";
143
+ let context = getMore?getWordsBeforeSelection(editor, getMore):null
144
+ editor.update(() => {
145
+ const selection = $getSelection();
146
+ if ($isRangeSelection(selection)) {
147
+ const selectionText = selection.getTextContent();
148
+ if (selectionText.length > 30) {
149
+ selectedText = selectionText;
150
+ } else {
151
+ selectedText = context+selectionText ;
152
+ }
153
+ }
154
+ });
155
+
156
+ return selectedText;
157
+ };
158
+
159
+
160
+ function applyStyles(AIString: string) {
161
+ // Regex now matches:
162
+ // 1. Code blocks delimited by triple backticks (```...```)
163
+ // 2. Bold text delimited by ** (e.g., **bold**)
164
+ // 3. Inline code delimited by a single backtick (e.g., `some code`)
165
+ // 4. Headers starting with ### (e.g., ### Header)
166
+ const pattern = /(```[\s\S]*?```)|(\*\*[\s\S]+?\*\*)|(`[\s\S]+?`)|(###\s+[\s\S]+?(?=\n|$))/g;
167
+ let lastIndex = 0;
168
+ const nodes = [];
169
+
170
+ // Process all matches of code, bold, or inline code segments.
171
+ AIString.replace(
172
+ pattern,
173
+ (match, codeBlockMatch, boldMatch, codeMatch,headerMatch, offset) => {
174
+ // Append any plain text before the matched segment.
175
+ if (offset > lastIndex) {
176
+ const plainText = AIString.slice(lastIndex, offset);
177
+ nodes.push($createTextNode(plainText));
178
+ }
179
+ if (codeBlockMatch) {
180
+ // TODO: extract the code lang and apply it to the code block.
181
+
182
+ const content = codeBlockMatch.slice(3, -3);
183
+ const codeNode = $createCodeNode();
184
+ codeNode.append($createTextNode(content));
185
+ nodes.push(codeNode);
186
+ } else if (boldMatch) {
187
+ // Found bold text. Remove the leading and trailing "**".
188
+ const content = boldMatch.slice(2, -2);
189
+ const boldTextNode = $createTextNode(content);
190
+ boldTextNode.setFormat("bold");
191
+ nodes.push(boldTextNode);
192
+ } else if (codeMatch) {
193
+ const content = codeMatch.slice(1, -1);
194
+ const codeTextNode = $createTextNode(content);
195
+ codeTextNode.setFormat("code");
196
+ nodes.push(codeTextNode);
197
+ }else if(headerMatch){
198
+ const content = headerMatch.slice(4).trim();
199
+ const headerNode = $createHeadingNode("h3");
200
+ headerNode.append($createTextNode(content));
201
+ nodes.push(headerNode);
202
+ }
203
+ lastIndex = offset + match.length;
204
+ return match;
205
+ }
206
+ );
207
+
208
+ // Append any remaining plain text after the last match.
209
+ if (lastIndex < AIString.length) {
210
+ nodes.push($createTextNode(AIString.slice(lastIndex)));
211
+ }
212
+
213
+ return nodes;
214
+ }
215
+
216
+
217
+ function insertText(text: string, editor: LexicalEditor) {
218
+ if(isJSON(text) || text.startsWith("json")){
219
+ GenerateSteps(text,editor)
220
+ return
221
+ }
222
+ editor.update(() => {
223
+ const selection = $getSelection();
224
+ if (!$isRangeSelection(selection))
225
+ return console.log("No valid text selection found.");
226
+
227
+ const nodesInSelection = selection.getNodes();
228
+ if (nodesInSelection.length > 0) {
229
+ let insertionPoint = nodesInSelection[nodesInSelection.length - 1];
230
+ const formattedNodes = applyStyles(text);
231
+
232
+ formattedNodes.forEach((node: any) => {
233
+ insertionPoint.insertAfter(node);
234
+ insertionPoint = node;
235
+ });
236
+ }
237
+ });
238
+ }
239
+
240
+ function replaceSelectedText(text: string, editor: LexicalEditor) {
241
+
242
+ if(isJSON(text) || text.startsWith("json")){
243
+ GenerateSteps(text,editor)
244
+ return
245
+ }
246
+
247
+
248
+ editor.update(() => {
249
+ const selection = $getSelection();
250
+ if (!$isRangeSelection(selection)) {
251
+ console.warn("No valid text selection found.");
252
+ return;
253
+ }
254
+ selection.removeText();
255
+ const formattedNodes = applyStyles(text);
256
+ selection.insertNodes(formattedNodes);
257
+ });
258
+ }
259
+
260
+
261
+ function insertTextUnderSelected(text: string, editor: LexicalEditor) {
262
+ if(isJSON(text) || text.startsWith("json")){
263
+ GenerateSteps(text,editor)
264
+ return
265
+ }
266
+ editor.update(() => {
267
+ const selection = $getSelection();
268
+ if (!$isRangeSelection(selection)) return;
269
+
270
+ const nodes = selection.getNodes();
271
+ if (nodes.length > 0) {
272
+ const lastNode = nodes[nodes.length - 1];
273
+
274
+ const newParagraph = $createParagraphNode();
275
+
276
+ const formattedNodes = applyStyles(text);
277
+
278
+ newParagraph.append(...formattedNodes);
279
+
280
+ lastNode.insertAfter(newParagraph);
281
+ }
282
+ });
283
+ }
284
+
285
+ export {
286
+ replaceSelectedText,
287
+ insertTextUnderSelected,
288
+ getSelectedText,
289
+ insertText,
290
+ getWordsBeforeSelection,
291
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ export const CAN_USE_DOM: boolean =
10
+ typeof window !== 'undefined' &&
11
+ typeof window.document !== 'undefined' &&
12
+ typeof window.document.createElement !== 'undefined';
@@ -0,0 +1,282 @@
1
+
2
+ import { DEFAULT_FONT_SIZE, MAX_ALLOWED_FONT_SIZE, MIN_ALLOWED_FONT_SIZE } from '@/components/providers/ToolbarContext';
3
+ import {$createCodeNode} from '@lexical/code';
4
+ import {
5
+ INSERT_CHECK_LIST_COMMAND,
6
+ INSERT_ORDERED_LIST_COMMAND,
7
+ INSERT_UNORDERED_LIST_COMMAND,
8
+ } from '@lexical/list';
9
+ import {$isDecoratorBlockNode} from '@lexical/react/LexicalDecoratorBlockNode';
10
+ import {
11
+ $createHeadingNode,
12
+ $createQuoteNode,
13
+ $isHeadingNode,
14
+ $isQuoteNode,
15
+ HeadingTagType,
16
+ } from '@lexical/rich-text';
17
+ import {$patchStyleText, $setBlocksType} from '@lexical/selection';
18
+ import { $isTableSelection} from '@lexical/table';
19
+ import {$getNearestBlockElementAncestorOrThrow} from '@lexical/utils';
20
+ import {
21
+ $createParagraphNode,
22
+ $getSelection,
23
+ $isRangeSelection,
24
+ $isTextNode,
25
+ LexicalEditor,
26
+ } from 'lexical';
27
+
28
+
29
+ // eslint-disable-next-line no-shadow
30
+ export enum UpdateFontSizeType {
31
+ increment = 1,
32
+ decrement,
33
+ }
34
+
35
+ /**
36
+ * Calculates the new font size based on the update type.
37
+ * @param currentFontSize - The current font size
38
+ * @param updateType - The type of change, either increment or decrement
39
+ * @returns the next font size
40
+ */
41
+ export const calculateNextFontSize = (
42
+ currentFontSize: number,
43
+ updateType: UpdateFontSizeType | null,
44
+ ) => {
45
+ if (!updateType) {
46
+ return currentFontSize;
47
+ }
48
+
49
+ let updatedFontSize: number = currentFontSize;
50
+ switch (updateType) {
51
+ case UpdateFontSizeType.decrement:
52
+ switch (true) {
53
+ case currentFontSize > MAX_ALLOWED_FONT_SIZE:
54
+ updatedFontSize = MAX_ALLOWED_FONT_SIZE;
55
+ break;
56
+ case currentFontSize >= 48:
57
+ updatedFontSize -= 12;
58
+ break;
59
+ case currentFontSize >= 24:
60
+ updatedFontSize -= 4;
61
+ break;
62
+ case currentFontSize >= 14:
63
+ updatedFontSize -= 2;
64
+ break;
65
+ case currentFontSize >= 9:
66
+ updatedFontSize -= 1;
67
+ break;
68
+ default:
69
+ updatedFontSize = MIN_ALLOWED_FONT_SIZE;
70
+ break;
71
+ }
72
+ break;
73
+
74
+ case UpdateFontSizeType.increment:
75
+ switch (true) {
76
+ case currentFontSize < MIN_ALLOWED_FONT_SIZE:
77
+ updatedFontSize = MIN_ALLOWED_FONT_SIZE;
78
+ break;
79
+ case currentFontSize < 12:
80
+ updatedFontSize += 1;
81
+ break;
82
+ case currentFontSize < 20:
83
+ updatedFontSize += 2;
84
+ break;
85
+ case currentFontSize < 36:
86
+ updatedFontSize += 4;
87
+ break;
88
+ case currentFontSize <= 60:
89
+ updatedFontSize += 12;
90
+ break;
91
+ default:
92
+ updatedFontSize = MAX_ALLOWED_FONT_SIZE;
93
+ break;
94
+ }
95
+ break;
96
+
97
+ default:
98
+ break;
99
+ }
100
+ return updatedFontSize;
101
+ };
102
+
103
+ /**
104
+ * Patches the selection with the updated font size.
105
+ */
106
+ export const updateFontSizeInSelection = (
107
+ editor: LexicalEditor,
108
+ newFontSize: string | null,
109
+ updateType: UpdateFontSizeType | null,
110
+ ) => {
111
+ const getNextFontSize = (prevFontSize: string | null): string => {
112
+ if (!prevFontSize) {
113
+ prevFontSize = `${DEFAULT_FONT_SIZE}px`;
114
+ }
115
+ prevFontSize = prevFontSize.slice(0, -2);
116
+ const nextFontSize = calculateNextFontSize(
117
+ Number(prevFontSize),
118
+ updateType,
119
+ );
120
+ return `${nextFontSize}px`;
121
+ };
122
+
123
+ editor.update(() => {
124
+ if (editor.isEditable()) {
125
+ const selection = $getSelection();
126
+ if (selection !== null) {
127
+ $patchStyleText(selection, {
128
+ 'font-size': newFontSize || getNextFontSize,
129
+ });
130
+ }
131
+ }
132
+ });
133
+ };
134
+
135
+ export const updateFontSize = (
136
+ editor: LexicalEditor,
137
+ updateType: UpdateFontSizeType,
138
+ inputValue: string,
139
+ ) => {
140
+ if (inputValue !== '') {
141
+ const nextFontSize = calculateNextFontSize(Number(inputValue), updateType);
142
+ updateFontSizeInSelection(editor, String(nextFontSize) + 'px', null);
143
+ } else {
144
+ updateFontSizeInSelection(editor, null, updateType);
145
+ }
146
+ };
147
+
148
+ export const formatParagraph = (editor: LexicalEditor) => {
149
+ editor.update(() => {
150
+ const selection = $getSelection();
151
+ if ($isRangeSelection(selection)) {
152
+ $setBlocksType(selection, () => $createParagraphNode());
153
+ }
154
+ });
155
+ };
156
+
157
+ export const formatHeading = (
158
+ editor: LexicalEditor,
159
+ blockType: string,
160
+ headingSize: HeadingTagType,
161
+ ) => {
162
+ if (blockType !== headingSize) {
163
+ editor.update(() => {
164
+ const selection = $getSelection();
165
+ $setBlocksType(selection, () => $createHeadingNode(headingSize));
166
+ });
167
+ }
168
+ };
169
+
170
+ export const formatBulletList = (editor: LexicalEditor, blockType: string) => {
171
+ if (blockType !== 'bullet') {
172
+ editor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND, undefined);
173
+ } else {
174
+ formatParagraph(editor);
175
+ }
176
+ };
177
+
178
+ export const formatCheckList = (editor: LexicalEditor, blockType: string) => {
179
+ if (blockType !== 'check') {
180
+ editor.dispatchCommand(INSERT_CHECK_LIST_COMMAND, undefined);
181
+ } else {
182
+ formatParagraph(editor);
183
+ }
184
+ };
185
+
186
+ export const formatNumberedList = (
187
+ editor: LexicalEditor,
188
+ blockType: string,
189
+ ) => {
190
+ if (blockType !== 'number') {
191
+ editor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND, undefined);
192
+ } else {
193
+ formatParagraph(editor);
194
+ }
195
+ };
196
+
197
+ export const formatQuote = (editor: LexicalEditor, blockType: string) => {
198
+ if (blockType !== 'quote') {
199
+ editor.update(() => {
200
+ const selection = $getSelection();
201
+ $setBlocksType(selection, () => $createQuoteNode());
202
+ });
203
+ }
204
+ };
205
+
206
+ export const formatCode = (editor: LexicalEditor, blockType: string) => {
207
+ if (blockType !== 'code') {
208
+ editor.update(() => {
209
+ let selection = $getSelection();
210
+
211
+ if (selection !== null) {
212
+ if (selection.isCollapsed()) {
213
+ $setBlocksType(selection, () => $createCodeNode());
214
+ } else {
215
+ const textContent = selection.getTextContent();
216
+ const codeNode = $createCodeNode();
217
+ selection.insertNodes([codeNode]);
218
+ selection = $getSelection();
219
+ if ($isRangeSelection(selection)) {
220
+ selection.insertRawText(textContent);
221
+ }
222
+ }
223
+ }
224
+ });
225
+ }
226
+ };
227
+
228
+ export const clearFormatting = (editor: LexicalEditor) => {
229
+ editor.update(() => {
230
+ const selection = $getSelection();
231
+ if ($isRangeSelection(selection) || $isTableSelection(selection)) {
232
+ const anchor = selection.anchor;
233
+ const focus = selection.focus;
234
+ const nodes = selection.getNodes();
235
+ const extractedNodes = selection.extract();
236
+
237
+ if (anchor.key === focus.key && anchor.offset === focus.offset) {
238
+ return;
239
+ }
240
+
241
+ nodes.forEach((node, idx) => {
242
+ // We split the first and last node by the selection
243
+ // So that we don't format unselected text inside those nodes
244
+ if ($isTextNode(node)) {
245
+ // Use a separate variable to ensure TS does not lose the refinement
246
+ let textNode = node;
247
+ if (idx === 0 && anchor.offset !== 0) {
248
+ textNode = textNode.splitText(anchor.offset)[1] || textNode;
249
+ }
250
+ if (idx === nodes.length - 1) {
251
+ textNode = textNode.splitText(focus.offset)[0] || textNode;
252
+ }
253
+ /**
254
+ * If the selected text has one format applied
255
+ * selecting a portion of the text, could
256
+ * clear the format to the wrong portion of the text.
257
+ *
258
+ * The cleared text is based on the length of the selected text.
259
+ */
260
+ // We need this in case the selected text only has one format
261
+ const extractedTextNode = extractedNodes[0];
262
+ if (nodes.length === 1 && $isTextNode(extractedTextNode)) {
263
+ textNode = extractedTextNode;
264
+ }
265
+
266
+ if (textNode.__style !== '') {
267
+ textNode.setStyle('');
268
+ }
269
+ if (textNode.__format !== 0) {
270
+ textNode.setFormat(0);
271
+ $getNearestBlockElementAncestorOrThrow(textNode).setFormat('');
272
+ }
273
+ node = textNode;
274
+ } else if ($isHeadingNode(node) || $isQuoteNode(node)) {
275
+ node.replace($createParagraphNode(), true);
276
+ } else if ($isDecoratorBlockNode(node)) {
277
+ node.setFormat('');
278
+ }
279
+ });
280
+ }
281
+ });
282
+ };
@@ -0,0 +1,50 @@
1
+
2
+
3
+ import {CAN_USE_DOM} from './canUseDOM';
4
+
5
+ declare global {
6
+ interface Document {
7
+ documentMode?: unknown;
8
+ }
9
+
10
+ interface Window {
11
+ MSStream?: unknown;
12
+ }
13
+ }
14
+
15
+ const documentMode =
16
+ CAN_USE_DOM && 'documentMode' in document ? document.documentMode : null;
17
+
18
+ export const IS_APPLE: boolean =
19
+ CAN_USE_DOM && /Mac|iPod|iPhone|iPad/.test(navigator.platform);
20
+
21
+ export const IS_FIREFOX: boolean =
22
+ CAN_USE_DOM && /^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);
23
+
24
+ export const CAN_USE_BEFORE_INPUT: boolean =
25
+ CAN_USE_DOM && 'InputEvent' in window && !documentMode
26
+ ? 'getTargetRanges' in new window.InputEvent('input')
27
+ : false;
28
+
29
+ export const IS_SAFARI: boolean =
30
+ CAN_USE_DOM && /Version\/[\d.]+.*Safari/.test(navigator.userAgent);
31
+
32
+ export const IS_IOS: boolean =
33
+ CAN_USE_DOM &&
34
+ /iPad|iPhone|iPod/.test(navigator.userAgent) &&
35
+ !window.MSStream;
36
+
37
+ export const IS_ANDROID: boolean =
38
+ CAN_USE_DOM && /Android/.test(navigator.userAgent);
39
+
40
+ // Keep these in case we need to use them in the future.
41
+ // export const IS_WINDOWS: boolean = CAN_USE_DOM && /Win/.test(navigator.platform);
42
+ export const IS_CHROME: boolean =
43
+ CAN_USE_DOM && /^(?=.*Chrome).*/i.test(navigator.userAgent);
44
+ // export const canUseTextInputEvent: boolean = CAN_USE_DOM && 'TextEvent' in window && !documentMode;
45
+
46
+ export const IS_ANDROID_CHROME: boolean =
47
+ CAN_USE_DOM && IS_ANDROID && IS_CHROME;
48
+
49
+ export const IS_APPLE_WEBKIT =
50
+ CAN_USE_DOM && /AppleWebKit\/[\d.]+/.test(navigator.userAgent) && !IS_CHROME;