@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,271 @@
1
+ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
2
+ import {
3
+ $getSelection,
4
+ $isRangeSelection,
5
+ COMMAND_PRIORITY_LOW,
6
+ createCommand,
7
+ EditorConfig,
8
+ ElementNode,
9
+ LexicalEditor,
10
+ LexicalNode,
11
+ NodeKey,
12
+ } from "lexical";
13
+ import { $insertNodeToNearestRoot } from "@lexical/utils";
14
+ import { useEffect } from "react";
15
+
16
+ // Utility function to fetch metadata from a URL
17
+ async function fetchMetadata(url: string) {
18
+ try {
19
+ const proxyUrl = "https://api.allorigins.win/get?url=";
20
+ const response = await fetch(`${proxyUrl}${encodeURIComponent(url)}`);
21
+ const data = await response.json();
22
+
23
+ // Extract the HTML content from the proxy response
24
+ const html = data.contents;
25
+
26
+ // Parse the HTML using DOMParser
27
+ const parser = new DOMParser();
28
+ const doc = parser.parseFromString(html, "text/html");
29
+
30
+ // Helper function to extract meta tag content
31
+ const getMetaTagContent = (property: string) => {
32
+ const element =
33
+ doc.querySelector(`meta[property="${property}"]`) ||
34
+ doc.querySelector(`meta[name="${property}"]`);
35
+ return element ? element.getAttribute("content") : null;
36
+ };
37
+
38
+ // Extract metadata
39
+ const metadata = {
40
+ title:
41
+ doc.querySelector("title")?.textContent ||
42
+ getMetaTagContent("og:title") ||
43
+ "No title",
44
+ description:
45
+ getMetaTagContent("og:description") ||
46
+ getMetaTagContent("description") ||
47
+ "No description",
48
+ image: getMetaTagContent("og:image") || undefined, // Use a valid fallback image URL
49
+ website: getMetaTagContent("og:site_name") || new URL(url).hostname, // Use hostname as fallback
50
+ };
51
+
52
+ return metadata;
53
+ } catch (error) {
54
+ return {
55
+ title: "No title",
56
+ description: "No description",
57
+ image: undefined,
58
+ website: new URL(url).hostname,
59
+ };
60
+ }
61
+ }
62
+
63
+ export class LinkWithMetaDataNode extends ElementNode {
64
+ __url: string | null;
65
+ __metadata: {
66
+ title: string;
67
+ description: string;
68
+ image?: string | undefined |null;
69
+ website: string;
70
+ } | null = null;
71
+ __editor: LexicalEditor;
72
+
73
+ constructor(url: string | null, editor: LexicalEditor, key?: NodeKey) {
74
+ super(key);
75
+ this.__url = url;
76
+ this.__editor = editor; // Store the editor instance
77
+ }
78
+
79
+ static clone(node: LinkWithMetaDataNode): LinkWithMetaDataNode {
80
+ return new LinkWithMetaDataNode(node.__url, node.__editor, node.__key);
81
+ }
82
+ static importJSON(serializedNode: any): LinkWithMetaDataNode {
83
+ const node = $createLinkNode(serializedNode.url, serializedNode.__editor);
84
+ node.__metadata = serializedNode.metadata;
85
+ node.__url = serializedNode.url
86
+
87
+ return node;
88
+ }
89
+ exportJSON() {
90
+ return {
91
+ ...super.exportJSON(),
92
+ url: this.__url,
93
+ metadata: this.__metadata,
94
+ type: "link-with-metadata",
95
+ version: 1,
96
+ };
97
+ }
98
+
99
+ static getType(): string {
100
+ return "link-with-metadata";
101
+ }
102
+
103
+ // Method to update metadata
104
+ setMetadata(metadata: {
105
+ title: string;
106
+ description: string;
107
+ image?: string;
108
+ website: string;
109
+ }) {
110
+ const writable = this.getWritable();
111
+ writable.__metadata = metadata;
112
+ }
113
+
114
+ createDOM(): HTMLElement {
115
+ const elementDIV = document.createElement("div");
116
+ elementDIV.className ="dark:border-zinc-800 dark:bg-zinc-900 dark:text-zinc-50 border border-zinc-200 bg-white/90 text-zinc-950 shadow-md w-full cursor-pointer p-3 rounded-sm border flex flex-row items-center justify-between";
117
+ elementDIV.setAttribute("contenteditable", "false");
118
+
119
+ // Create a container for the left and right sections
120
+ const container = document.createElement("div");
121
+ container.className = "flex flex-row w-full max-sm:flex-col max-sm:gap-y-2 justify-between items-start gap-x-2";
122
+
123
+ // Conditionally create the left container for the image
124
+ if (this.__metadata?.image) {
125
+ const leftContainer = document.createElement("div");
126
+ leftContainer.className = "flex w-full h-full max-w-[250px] max-h-[100px] min-h-[100px] order-6";
127
+
128
+ // Create the image element
129
+ const image = document.createElement("img");
130
+ image.src = this.__metadata.image;
131
+ image.alt = `image for ${this.__metadata.title}`;
132
+ image.className = "w-full h-[100px] rounded-lg object-cover";
133
+ leftContainer.appendChild(image);
134
+
135
+ // Append the left container to the main container
136
+ container.appendChild(leftContainer);
137
+ }
138
+
139
+ // Right container for the text content
140
+ const rightContainer = document.createElement("div");
141
+ rightContainer.className = "flex flex-col flex-1 w-full flex-wrap items-start space-y-4";
142
+
143
+
144
+
145
+ // Create the title as a clickable link
146
+ const titleLink = document.createElement("a");
147
+ titleLink.className = "font-bold break-words hover:underline";
148
+ titleLink.href = this.__url || "#"; // Use the URL or a fallback
149
+ titleLink.textContent = this.__url || "No URL provided";
150
+ titleLink.target = "_blank"; // Open link in a new tab
151
+ rightContainer.appendChild(titleLink);
152
+
153
+ // Create the description small element
154
+ const descriptionSmall = document.createElement("small");
155
+ descriptionSmall.className = "font-[400] dark:text-gray-200 text-gray-500";
156
+ descriptionSmall.textContent =
157
+ this.__metadata?.description || "Loading metadata...";
158
+ rightContainer.appendChild(descriptionSmall);
159
+
160
+ // Create the source small element
161
+ const sourceSmall = document.createElement("small");
162
+ sourceSmall.className = "text-sm mt-4 font-[400] dark:text-gray-300 text-gray-500/50";
163
+ sourceSmall.textContent = this.__metadata?.website || "";
164
+ rightContainer.appendChild(sourceSmall);
165
+
166
+ // Append the right container to the main container
167
+ container.appendChild(rightContainer);
168
+
169
+ // Append the main container to the root div
170
+ elementDIV.appendChild(container);
171
+
172
+ // Fetch metadata and update the node
173
+ if (this.__url && !this.__metadata) {
174
+ fetchMetadata(this.__url)
175
+ .then((metadata) => {
176
+ // Use editor.update() to perform state updates
177
+ this.__editor.update(() => {
178
+ this.setMetadata(metadata);
179
+ });
180
+ })
181
+ .catch((error) => {
182
+ console.error("Error fetching metadata:", error);
183
+ });
184
+ }
185
+
186
+ return elementDIV;
187
+ }
188
+
189
+ updateDOM(prevNode: LinkWithMetaDataNode, dom: HTMLElement): boolean {
190
+ // Check if metadata has changed
191
+ if (this.__metadata !== prevNode.__metadata) {
192
+ const container = dom.querySelector("div");
193
+ const image = dom.querySelector("img");
194
+ const titleLink = dom.querySelector("a");
195
+ const descriptionSmall = dom.querySelector("small:nth-of-type(1)");
196
+ const sourceSmall = dom.querySelector("small:nth-of-type(2)");
197
+
198
+ if (titleLink && descriptionSmall && sourceSmall) {
199
+ titleLink.href = this.__url || "#";
200
+ titleLink.textContent =
201
+ this.__metadata?.title || this.__url || "No URL provided";
202
+ descriptionSmall.textContent =
203
+ this.__metadata?.description || "Loading metadata...";
204
+ sourceSmall.textContent = this.__metadata?.website || "";
205
+ }
206
+
207
+ // Handle image updates
208
+ if (this.__metadata?.image) {
209
+ if (!image) {
210
+ const leftContainer = document.createElement("div");
211
+ leftContainer.className = "flex h-full flex-shrink-0 order-6";
212
+
213
+ const newImage = document.createElement("img");
214
+ newImage.src = this.__metadata.image;
215
+ newImage.alt = "Custom Image";
216
+ newImage.className = "w-24 min-h-24 max-[250px] h-[200px] rounded-lg object-fill";
217
+ leftContainer.appendChild(newImage);
218
+
219
+ // Insert the left container before the right container
220
+ container?.insertBefore(leftContainer, container.firstChild);
221
+ } else {
222
+ image.src = this.__metadata.image;
223
+ }
224
+ } else if (image) {
225
+
226
+ image.remove();
227
+ }
228
+
229
+ return true;
230
+ }
231
+
232
+ return false;
233
+ }
234
+ }
235
+ export function $createLinkNode(
236
+ url: string | null,
237
+ editor: LexicalEditor
238
+ ): LinkWithMetaDataNode {
239
+ return new LinkWithMetaDataNode(url, editor);
240
+ }
241
+
242
+ export function $isLinkNode(node: LexicalNode): node is LinkWithMetaDataNode {
243
+ return node instanceof LinkWithMetaDataNode;
244
+ }
245
+
246
+ export const TOGGLE_LINK_COMMAND_LinkWithMetaDataNode = createCommand<string | null>("toggleLink");
247
+
248
+ export function LinkWithMetaDataPlugin(): null {
249
+ const [editor] = useLexicalComposerContext();
250
+
251
+ useEffect(() => {
252
+ if (!editor.hasNodes([LinkWithMetaDataNode])) {
253
+ throw new Error("LinkWithMetaDataPlugin: LinkWithMetaDataNode not registered on editor");
254
+ }
255
+ }, [editor]);
256
+
257
+ editor.registerCommand(
258
+ TOGGLE_LINK_COMMAND_LinkWithMetaDataNode,
259
+ (url: string | null) => {
260
+ const selection = $getSelection();
261
+ if ($isRangeSelection(selection)) {
262
+ const linkNode = $createLinkNode(url, editor);
263
+ $insertNodeToNearestRoot(linkNode);
264
+ }
265
+ return true;
266
+ },
267
+ COMMAND_PRIORITY_LOW
268
+ );
269
+
270
+ return null;
271
+ }
@@ -0,0 +1,11 @@
1
+
2
+ import type {JSX} from 'react';
3
+
4
+ import {MarkdownShortcutPlugin} from '@lexical/react/LexicalMarkdownShortcutPlugin';
5
+ import * as React from 'react';
6
+
7
+ import {PLAYGROUND_TRANSFORMERS} from '../MarkdownTransformers';
8
+
9
+ export default function MarkdownPlugin(): JSX.Element {
10
+ return <MarkdownShortcutPlugin transformers={PLAYGROUND_TRANSFORMERS} />;
11
+ }
@@ -0,0 +1,304 @@
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
+ import {
10
+ $convertFromMarkdownString,
11
+ $convertToMarkdownString,
12
+ CHECK_LIST,
13
+ ELEMENT_TRANSFORMERS,
14
+ ElementTransformer,
15
+ MULTILINE_ELEMENT_TRANSFORMERS,
16
+ TEXT_FORMAT_TRANSFORMERS,
17
+ TEXT_MATCH_TRANSFORMERS,
18
+ TextMatchTransformer,
19
+ Transformer,
20
+ } from "@lexical/markdown";
21
+ import {
22
+ $createHorizontalRuleNode,
23
+ $isHorizontalRuleNode,
24
+ HorizontalRuleNode,
25
+ } from "@lexical/react/LexicalHorizontalRuleNode";
26
+ import {
27
+ $createTableCellNode,
28
+ $createTableNode,
29
+ $createTableRowNode,
30
+ $isTableCellNode,
31
+ $isTableNode,
32
+ $isTableRowNode,
33
+ TableCellHeaderStates,
34
+ TableCellNode,
35
+ TableNode,
36
+ TableRowNode,
37
+ } from "@lexical/table";
38
+ import {
39
+ $createTextNode,
40
+ $isParagraphNode,
41
+ $isTextNode,
42
+ LexicalNode,
43
+ } from "lexical";
44
+
45
+ import {
46
+ $createImageNode,
47
+ $isImageNode,
48
+ ImageNode,
49
+ } from "../../nodes/ImageNode";
50
+
51
+ export const HR: ElementTransformer = {
52
+ dependencies: [HorizontalRuleNode],
53
+ export: (node: LexicalNode) => {
54
+ return $isHorizontalRuleNode(node) ? "***" : null;
55
+ },
56
+ regExp: /^(---|\*\*\*|___)\s?$/,
57
+ replace: (parentNode, _1, _2, isImport) => {
58
+ const line = $createHorizontalRuleNode();
59
+
60
+ // TODO: Get rid of isImport flag
61
+ if (isImport || parentNode.getNextSibling() != null) {
62
+ parentNode.replace(line);
63
+ } else {
64
+ parentNode.insertBefore(line);
65
+ }
66
+
67
+ line.selectNext();
68
+ },
69
+ type: "element",
70
+ };
71
+
72
+ export const IMAGE: TextMatchTransformer = {
73
+ dependencies: [ImageNode],
74
+ export: (node) => {
75
+ if (!$isImageNode(node)) {
76
+ return null;
77
+ }
78
+
79
+ return `![${node.getAltText()}](${node.getSrc()})`;
80
+ },
81
+ importRegExp: /!(?:\[([^[]*)\])(?:\(([^(]+)\))/,
82
+ regExp: /!(?:\[([^[]*)\])(?:\(([^(]+)\))$/,
83
+ replace: (textNode, match) => {
84
+ const [, altText, src] = match;
85
+ const imageNode = $createImageNode({
86
+ altText,
87
+ maxWidth: 800,
88
+ src,
89
+ });
90
+ textNode.replace(imageNode);
91
+ },
92
+ trigger: ")",
93
+ type: "text-match",
94
+ };
95
+
96
+
97
+
98
+ // export const EQUATION: TextMatchTransformer = {
99
+ // dependencies: [EquationNode],
100
+ // export: (node) => {
101
+ // if (!$isEquationNode(node)) {
102
+ // return null;
103
+ // }
104
+
105
+ // return `$${node.getEquation()}$`;
106
+ // },
107
+ // importRegExp: /\$([^$]+?)\$/,
108
+ // regExp: /\$([^$]+?)\$$/,
109
+ // replace: (textNode, match) => {
110
+ // const [, equation] = match;
111
+ // const equationNode = $createEquationNode(equation, true);
112
+ // textNode.replace(equationNode);
113
+ // },
114
+ // trigger: "$",
115
+ // type: "text-match",
116
+ // };
117
+
118
+ // export const TWEET: ElementTransformer = {
119
+ // dependencies: [TweetNode],
120
+ // export: (node) => {
121
+ // if (!$isTweetNode(node)) {
122
+ // return null;
123
+ // }
124
+
125
+ // return `<tweet id="${node.getId()}" />`;
126
+ // },
127
+ // regExp: /<tweet id="([^"]+?)"\s?\/>\s?$/,
128
+ // replace: (textNode, _1, match) => {
129
+ // const [, id] = match;
130
+ // const tweetNode = $createTweetNode(id);
131
+ // textNode.replace(tweetNode);
132
+ // },
133
+ // type: "element",
134
+ // };
135
+
136
+ // Very primitive table setup
137
+ const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
138
+ const TABLE_ROW_DIVIDER_REG_EXP = /^(\| ?:?-*:? ?)+\|\s?$/;
139
+
140
+ export const TABLE: ElementTransformer = {
141
+ dependencies: [TableNode, TableRowNode, TableCellNode],
142
+ export: (node: LexicalNode) => {
143
+ if (!$isTableNode(node)) {
144
+ return null;
145
+ }
146
+
147
+ const output: string[] = [];
148
+
149
+ for (const row of node.getChildren()) {
150
+ const rowOutput = [];
151
+ if (!$isTableRowNode(row)) {
152
+ continue;
153
+ }
154
+
155
+ let isHeaderRow = false;
156
+ for (const cell of row.getChildren()) {
157
+ // It's TableCellNode so it's just to make flow happy
158
+ if ($isTableCellNode(cell)) {
159
+ rowOutput.push(
160
+ $convertToMarkdownString(PLAYGROUND_TRANSFORMERS, cell)
161
+ .replace(/\n/g, "\\n")
162
+ .trim()
163
+ );
164
+ if (cell.__headerState === TableCellHeaderStates.ROW) {
165
+ isHeaderRow = true;
166
+ }
167
+ }
168
+ }
169
+
170
+ output.push(`| ${rowOutput.join(" | ")} |`);
171
+ if (isHeaderRow) {
172
+ output.push(`| ${rowOutput.map((_) => "---").join(" | ")} |`);
173
+ }
174
+ }
175
+
176
+ return output.join("\n");
177
+ },
178
+ regExp: TABLE_ROW_REG_EXP,
179
+ replace: (parentNode, _1, match) => {
180
+ // Header row
181
+ if (TABLE_ROW_DIVIDER_REG_EXP.test(match[0])) {
182
+ const table = parentNode.getPreviousSibling();
183
+ if (!table || !$isTableNode(table)) {
184
+ return;
185
+ }
186
+
187
+ const rows = table.getChildren();
188
+ const lastRow = rows[rows.length - 1];
189
+ if (!lastRow || !$isTableRowNode(lastRow)) {
190
+ return;
191
+ }
192
+
193
+ // Add header state to row cells
194
+ lastRow.getChildren().forEach((cell) => {
195
+ if (!$isTableCellNode(cell)) {
196
+ return;
197
+ }
198
+ cell.setHeaderStyles(
199
+ TableCellHeaderStates.ROW,
200
+ TableCellHeaderStates.ROW
201
+ );
202
+ });
203
+
204
+ // Remove line
205
+ parentNode.remove();
206
+ return;
207
+ }
208
+
209
+ const matchCells = mapToTableCells(match[0]);
210
+
211
+ if (matchCells == null) {
212
+ return;
213
+ }
214
+
215
+ const rows = [matchCells];
216
+ let sibling = parentNode.getPreviousSibling();
217
+ let maxCells = matchCells.length;
218
+
219
+ while (sibling) {
220
+ if (!$isParagraphNode(sibling)) {
221
+ break;
222
+ }
223
+
224
+ if (sibling.getChildrenSize() !== 1) {
225
+ break;
226
+ }
227
+
228
+ const firstChild = sibling.getFirstChild();
229
+
230
+ if (!$isTextNode(firstChild)) {
231
+ break;
232
+ }
233
+
234
+ const cells = mapToTableCells(firstChild.getTextContent());
235
+
236
+ if (cells == null) {
237
+ break;
238
+ }
239
+
240
+ maxCells = Math.max(maxCells, cells.length);
241
+ rows.unshift(cells);
242
+ const previousSibling = sibling.getPreviousSibling();
243
+ sibling.remove();
244
+ sibling = previousSibling;
245
+ }
246
+
247
+ const table = $createTableNode();
248
+
249
+ for (const cells of rows) {
250
+ const tableRow = $createTableRowNode();
251
+ table.append(tableRow);
252
+
253
+ for (let i = 0; i < maxCells; i++) {
254
+ tableRow.append(i < cells.length ? cells[i] : $createTableCell(""));
255
+ }
256
+ }
257
+
258
+ const previousSibling = parentNode.getPreviousSibling();
259
+ if (
260
+ $isTableNode(previousSibling) &&
261
+ getTableColumnsSize(previousSibling) === maxCells
262
+ ) {
263
+ previousSibling.append(...table.getChildren());
264
+ parentNode.remove();
265
+ } else {
266
+ parentNode.replace(table);
267
+ }
268
+
269
+ table.selectEnd();
270
+ },
271
+ type: "element",
272
+ };
273
+
274
+ function getTableColumnsSize(table: TableNode) {
275
+ const row = table.getFirstChild();
276
+ return $isTableRowNode(row) ? row.getChildrenSize() : 0;
277
+ }
278
+
279
+ const $createTableCell = (textContent: string): TableCellNode => {
280
+ textContent = textContent.replace(/\\n/g, "\n");
281
+ const cell = $createTableCellNode(TableCellHeaderStates.NO_STATUS);
282
+ $convertFromMarkdownString(textContent, PLAYGROUND_TRANSFORMERS, cell);
283
+ return cell;
284
+ };
285
+
286
+ const mapToTableCells = (textContent: string): Array<TableCellNode> | null => {
287
+ const match = textContent.match(TABLE_ROW_REG_EXP);
288
+ if (!match || !match[1]) {
289
+ return null;
290
+ }
291
+ return match[1].split("|").map((text) => $createTableCell(text));
292
+ };
293
+
294
+ export const PLAYGROUND_TRANSFORMERS: Array<Transformer> = [
295
+ TABLE,
296
+ HR,
297
+ IMAGE,
298
+ // TWEET,
299
+ CHECK_LIST,
300
+ ...ELEMENT_TRANSFORMERS,
301
+ ...MULTILINE_ELEMENT_TRANSFORMERS,
302
+ ...TEXT_FORMAT_TRANSFORMERS,
303
+ ...TEXT_MATCH_TRANSFORMERS,
304
+ ];
@@ -0,0 +1,49 @@
1
+
2
+ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
3
+ import {$wrapNodeInElement} from '@lexical/utils';
4
+ import {
5
+ $createParagraphNode,
6
+ $insertNodes,
7
+ $isRootOrShadowRoot,
8
+ COMMAND_PRIORITY_EDITOR,
9
+ createCommand,
10
+ LexicalCommand,
11
+ } from 'lexical';
12
+ import {useEffect} from 'react';
13
+ import * as React from 'react';
14
+ import { $createPollNode, createPollOption, PollNode } from '../../nodes/PollNode';
15
+
16
+
17
+
18
+ export const INSERT_POLL_COMMAND: LexicalCommand<string> = createCommand(
19
+ 'INSERT_POLL_COMMAND',
20
+ );
21
+
22
+
23
+
24
+ export default function PollPlugin(): React.JSX.Element | null {
25
+ const [editor] = useLexicalComposerContext();
26
+ useEffect(() => {
27
+ if (!editor.hasNodes([PollNode])) {
28
+ throw new Error('PollPlugin: PollNode not registered on editor');
29
+ }
30
+
31
+ return editor.registerCommand<string>(
32
+ INSERT_POLL_COMMAND,
33
+ (payload) => {
34
+ const pollNode = $createPollNode(payload, [
35
+ createPollOption(),
36
+ createPollOption(),
37
+ ]);
38
+ $insertNodes([pollNode]);
39
+ if ($isRootOrShadowRoot(pollNode.getParentOrThrow())) {
40
+ $wrapNodeInElement(pollNode, $createParagraphNode).selectEnd();
41
+ }
42
+
43
+ return true;
44
+ },
45
+ COMMAND_PRIORITY_EDITOR,
46
+ );
47
+ }, [editor]);
48
+ return null;
49
+ }