@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,264 @@
1
+ import { $getNodeByKey, LexicalEditor, NodeKey } from "lexical";
2
+ import React, { useCallback, useEffect, useState } from "react";
3
+ import { $isImageNode } from "../../editor/nodes/ImageNode";
4
+ import { Button } from "@/components/ui/button";
5
+ import {
6
+ AlignCenterVertical,
7
+ CaptionsIcon,
8
+ Fullscreen,
9
+ PanelLeftClose,
10
+ Radius,
11
+ } from "lucide-react";
12
+ import { cn } from "@/lib/utils";
13
+ import { Input } from "@/components/ui/input";
14
+ import { Separator } from "@/components/ui/separator";
15
+
16
+ export default function ImageToolBar({
17
+ editor,
18
+ nodeKey,
19
+ height,
20
+ width,
21
+ }: {
22
+ editor: LexicalEditor;
23
+ nodeKey: NodeKey;
24
+ height: string | number;
25
+ width: string | number;
26
+ }) {
27
+ const [showResizeInput, setShowResizeInput] = useState(false);
28
+ const [widthInput, setWidthInput] = useState(String(width));
29
+ const [heightInput, setHeightInput] = useState(String(height));
30
+ const [showInput, setShowInput] = useState(false);
31
+
32
+ const toggleResizeInput = useCallback(() => {
33
+ setShowResizeInput((prev) => !prev);
34
+ }, []);
35
+ const setShowCaption = () => {
36
+ editor.update(() => {
37
+ const node = $getNodeByKey(nodeKey);
38
+ if ($isImageNode(node)) {
39
+ const currentState = node.__showCaption;
40
+ node.setShowCaption(!currentState);
41
+ }
42
+ });
43
+ };
44
+
45
+ const updateWidthAndHeight = useCallback(() => {
46
+ const newWidth = Number(widthInput);
47
+ const newHeight = Number(heightInput);
48
+ console.log("update", newWidth, newHeight);
49
+
50
+ if (isNaN(newWidth) || isNaN(newHeight)) return;
51
+ if (newWidth > 990 || newHeight > 1800) return;
52
+ if (newWidth < 140 || newHeight < 90) return;
53
+
54
+ editor.update(() => {
55
+ const node = $getNodeByKey(nodeKey);
56
+ if ($isImageNode(node)) {
57
+ node.setWidthAndHeight(newWidth, newHeight);
58
+ }
59
+ });
60
+ }, [editor, nodeKey, widthInput, heightInput]);
61
+
62
+ useEffect(() => {
63
+ const debounceTimeout = setTimeout(() => {
64
+ updateWidthAndHeight();
65
+ }, 500);
66
+ return () => clearTimeout(debounceTimeout);
67
+ }, [widthInput, heightInput, updateWidthAndHeight]);
68
+
69
+ const handleKeyDown = useCallback(
70
+ (e: React.KeyboardEvent<HTMLInputElement>) => {
71
+ if (e.key === "Enter") {
72
+ updateWidthAndHeight();
73
+ }
74
+ },
75
+ [updateWidthAndHeight]
76
+ );
77
+
78
+ const handleWidthChange = useCallback(
79
+ (e: React.ChangeEvent<HTMLInputElement>) => {
80
+ setWidthInput(e.target.value);
81
+ },
82
+ []
83
+ );
84
+
85
+ const handleHeightChange = useCallback(
86
+ (e: React.ChangeEvent<HTMLInputElement>) => {
87
+ setHeightInput(e.target.value);
88
+ },
89
+ []
90
+ );
91
+ useEffect(() => {
92
+ setWidthInput(String(width));
93
+ setHeightInput(String(height));
94
+ }, [height, width]);
95
+
96
+ const ChangeSideToLeft = useCallback(() => {
97
+ editor.update(() => {
98
+ const node = $getNodeByKey(nodeKey);
99
+ if ($isImageNode(node)) {
100
+ node.setAlignment("start");
101
+ }
102
+ });
103
+ }, [editor, nodeKey]);
104
+
105
+ const ChangeSideToCenter = useCallback(() => {
106
+ editor.update(() => {
107
+ const node = $getNodeByKey(nodeKey);
108
+ if ($isImageNode(node)) {
109
+ node.setAlignment("center");
110
+ }
111
+ });
112
+ }, [editor, nodeKey]);
113
+
114
+ const ChangeSideToRight = useCallback(() => {
115
+ editor.update(() => {
116
+ const node = $getNodeByKey(nodeKey);
117
+ if ($isImageNode(node)) {
118
+ node.setAlignment("end");
119
+ }
120
+ });
121
+ }, [editor, nodeKey]);
122
+ const ChangeRounded = useCallback(
123
+ (event: React.ChangeEvent<HTMLInputElement>) => {
124
+ editor.update(() => {
125
+ const node = $getNodeByKey(nodeKey);
126
+ if ($isImageNode(node)) {
127
+ const roundedValue = parseInt(event.target.value);
128
+ node.setRounded(roundedValue);
129
+ }
130
+ });
131
+ },
132
+ [editor, nodeKey]
133
+ );
134
+ return (
135
+ <div className="flex flex-row items-center z-50 gap-x-2 absolute top-1 left-1 group-hover:opacity-100 duration-500 opacity-0 transition-all">
136
+ <div className="p-1 rounded-sm bg-background/40 flex flex-row items-center justify-between">
137
+ <div className="flex flex-row items-center">
138
+ <Button
139
+ onClick={toggleResizeInput}
140
+ tip="Resize image"
141
+ variant={"ghost"}
142
+ size={"sm"}
143
+ className="p-1 w-6 h-6 "
144
+ >
145
+ <Fullscreen className="size-2" />
146
+ </Button>
147
+ <div
148
+ className={cn(
149
+ "transition-all duration-500 ease-in-out",
150
+ showResizeInput ? "w-[30px] mx-1" : "w-0"
151
+ )}
152
+ >
153
+ <Input
154
+ type="number"
155
+ placeholder="0px"
156
+ max={990}
157
+ min={120}
158
+ value={widthInput}
159
+ onChange={handleWidthChange}
160
+ onKeyDown={handleKeyDown}
161
+ className={cn(
162
+ "h-5 px-0.5 w-full rounded-sm text-xs outline-none transition-opacity duration-300 ease-in-out",
163
+ showResizeInput ? "opacity-100" : "opacity-0"
164
+ )}
165
+ />
166
+ </div>
167
+ <div
168
+ className={cn(
169
+ "transition-all duration-500 ease-in-out",
170
+ showResizeInput ? "w-[30px]" : "w-0"
171
+ )}
172
+ >
173
+ <Input
174
+ type="number"
175
+ placeholder="0px"
176
+ max={1800}
177
+ min={120}
178
+ value={heightInput}
179
+ onChange={handleHeightChange}
180
+ onKeyDown={handleKeyDown}
181
+ className={cn(
182
+ "h-5 px-0.5 w-full text-xs rounded-sm outline-none transition-opacity duration-300 ease-in-out",
183
+ showResizeInput ? "opacity-100" : "opacity-0"
184
+ )}
185
+ />
186
+ </div>
187
+ </div>
188
+ <Separator orientation="vertical" className="mx-1 h-5" />
189
+ <div className="flex flex-row items-center gap-x-1">
190
+ <Button
191
+ onClick={ChangeSideToLeft}
192
+ variant={"ghost"}
193
+ size={"sm"}
194
+ tip="move left"
195
+ className="w-6 h-6 p-1 "
196
+ >
197
+ <PanelLeftClose />
198
+ </Button>
199
+ <Button
200
+ size={"sm"}
201
+ variant={"ghost"}
202
+ onClick={ChangeSideToCenter}
203
+ className=" mx-1 w-6 h-6 p-1 opacity-[0.70] hover:opacity-100 transition-opacity"
204
+ tip="move center"
205
+ >
206
+ <AlignCenterVertical />
207
+ </Button>
208
+ <Button
209
+ size={"sm"}
210
+ variant={"ghost"}
211
+ onClick={ChangeSideToRight}
212
+ className=" w-6 h-6 p-1 opacity-[0.70] hover:opacity-100 transition-opacity"
213
+ tip="move right"
214
+ >
215
+ <PanelLeftClose />
216
+ </Button>
217
+ </div>
218
+ <Separator orientation="vertical" className="mx-1 h-5" />
219
+
220
+ <Button
221
+ size={"sm"}
222
+ variant={"ghost"}
223
+ onClick={() => {
224
+ setShowInput(!showInput);
225
+ }}
226
+ className=" w-6 h-6 p-1 opacity-[0.70] hover:opacity-100 transition-opacity"
227
+ tip="border radius"
228
+ >
229
+ <Radius />
230
+ </Button>
231
+ <div
232
+ className={cn(
233
+ "overflow-hidden transition-all duration-500 ease-in-out ",
234
+ showInput ? "w-[60px]" : "w-0"
235
+ )}
236
+ >
237
+ <Input
238
+ type="range"
239
+ min="0"
240
+ max="120"
241
+ defaultValue="0"
242
+ onChange={ChangeRounded}
243
+ className={cn(
244
+ "h-5 px-1 py-0 transition-opacity duration-300 ease-in-out",
245
+ showInput ? "opacity-100" : "opacity-0"
246
+ )}
247
+ />
248
+ </div>
249
+
250
+ <Separator orientation="vertical" className="mx-1 h-5" />
251
+
252
+ <Button
253
+ size={"sm"}
254
+ variant={"ghost"}
255
+ onClick={setShowCaption}
256
+ className=" w-6 h-6 p-1 opacity-[0.70] hover:opacity-100 transition-opacity"
257
+ tip="add caption"
258
+ >
259
+ <CaptionsIcon />
260
+ </Button>
261
+ </div>
262
+ </div>
263
+ );
264
+ }
@@ -0,0 +1,408 @@
1
+ import type {
2
+ BaseSelection,
3
+ LexicalCommand,
4
+ LexicalEditor,
5
+ NodeKey,
6
+ } from "lexical";
7
+ import type { JSX } from "react";
8
+
9
+ import { LinkNode } from "@lexical/link";
10
+ import { AutoFocusPlugin } from "@lexical/react/LexicalAutoFocusPlugin";
11
+ import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
12
+ import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";
13
+ import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
14
+ import { LexicalNestedComposer } from "@lexical/react/LexicalNestedComposer";
15
+ import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
16
+ import { useLexicalEditable } from "@lexical/react/useLexicalEditable";
17
+ import { useLexicalNodeSelection } from "@lexical/react/useLexicalNodeSelection";
18
+ import { mergeRegister } from "@lexical/utils";
19
+ import {
20
+ $getNodeByKey,
21
+ $getSelection,
22
+ $isNodeSelection,
23
+ $isRangeSelection,
24
+ $setSelection,
25
+ CLICK_COMMAND,
26
+ COMMAND_PRIORITY_LOW,
27
+ createCommand,
28
+ DRAGSTART_COMMAND,
29
+ KEY_BACKSPACE_COMMAND,
30
+ KEY_DELETE_COMMAND,
31
+ KEY_ENTER_COMMAND,
32
+ KEY_ESCAPE_COMMAND,
33
+ LineBreakNode,
34
+ ParagraphNode,
35
+ RootNode,
36
+ SELECTION_CHANGE_COMMAND,
37
+ TextNode,
38
+ } from "lexical";
39
+ import * as React from "react";
40
+ import { Suspense, useCallback, useEffect, useRef, useState } from "react";
41
+ import { $isImageNode } from "../../editor/nodes/ImageNode";
42
+ import { useSharedHistoryContext } from "@/components/providers/SharedHistoryContext";
43
+ import ErrorImage from "./error-image";
44
+ import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
45
+ import { ContentEditable } from "@lexical/react/LexicalContentEditable";
46
+ import ImageResizer from "./image-resizer";
47
+ import LazyImage from "./lazy-image";
48
+ import LazyVideo from "./lazy-video";
49
+ import ImageToolBar from "./image-toolbar";
50
+
51
+ export const RIGHT_CLICK_IMAGE_COMMAND: LexicalCommand<MouseEvent> =
52
+ createCommand("RIGHT_CLICK_IMAGE_COMMAND");
53
+
54
+ export default function ImageComponent({
55
+ src,
56
+ altText,
57
+ nodeKey,
58
+ width,
59
+ height,
60
+ maxWidth,
61
+ resizable,
62
+ showCaption,
63
+ caption,
64
+ rounded,
65
+ }: {
66
+ altText: string;
67
+ caption: LexicalEditor;
68
+ height: "inherit" | number;
69
+ maxWidth: number;
70
+ nodeKey: NodeKey;
71
+ resizable: boolean;
72
+ showCaption: boolean;
73
+ src: string;
74
+ width: "inherit" | number;
75
+ rounded: number;
76
+ captionsEnabled: boolean;
77
+ }): JSX.Element {
78
+ const MediaRef = useRef<HTMLImageElement | HTMLVideoElement | null>(null); // Updated type
79
+ const buttonRef = useRef<HTMLButtonElement | null>(null);
80
+ const [isSelected, setSelected, clearSelection] =
81
+ useLexicalNodeSelection(nodeKey);
82
+ const [isResizing, setIsResizing] = useState<boolean>(false);
83
+ const [editor] = useLexicalComposerContext();
84
+ const [selection, setSelection] = useState<BaseSelection | null>(null);
85
+ const activeEditorRef = useRef<LexicalEditor | null>(null);
86
+ const [isLoadError, setIsLoadError] = useState<boolean>(false);
87
+ const isEditable = useLexicalEditable();
88
+ const [MediaType, setMediaType] = useState<"IMAGE" | "VIDEO">("IMAGE");
89
+
90
+ const $onDelete = useCallback(
91
+ (payload: KeyboardEvent) => {
92
+ const deleteSelection = $getSelection();
93
+ if (isSelected && $isNodeSelection(deleteSelection)) {
94
+ const event: KeyboardEvent = payload;
95
+ event.preventDefault();
96
+ deleteSelection.getNodes().forEach((node) => {
97
+ if ($isImageNode(node)) {
98
+ node.remove();
99
+ }
100
+ });
101
+ }
102
+ return false;
103
+ },
104
+ [isSelected]
105
+ );
106
+
107
+ const $onEnter = useCallback(
108
+ (event: KeyboardEvent) => {
109
+ const latestSelection = $getSelection();
110
+ const buttonElem = buttonRef.current;
111
+ if (
112
+ isSelected &&
113
+ $isNodeSelection(latestSelection) &&
114
+ latestSelection.getNodes().length === 1
115
+ ) {
116
+ if (showCaption) {
117
+ // Move focus into nested editor
118
+ $setSelection(null);
119
+ event.preventDefault();
120
+ caption.focus();
121
+ return true;
122
+ } else if (
123
+ buttonElem !== null &&
124
+ buttonElem !== document.activeElement
125
+ ) {
126
+ event.preventDefault();
127
+ buttonElem.focus();
128
+ return true;
129
+ }
130
+ }
131
+ return false;
132
+ },
133
+ [caption, isSelected, showCaption]
134
+ );
135
+
136
+ const $onEscape = useCallback(
137
+ (event: KeyboardEvent) => {
138
+ if (
139
+ activeEditorRef.current === caption ||
140
+ buttonRef.current === event.target
141
+ ) {
142
+ $setSelection(null);
143
+ editor.update(() => {
144
+ setSelected(true);
145
+ const parentRootElement = editor.getRootElement();
146
+ if (parentRootElement !== null) {
147
+ parentRootElement.focus();
148
+ }
149
+ });
150
+ return true;
151
+ }
152
+ return false;
153
+ },
154
+ [caption, editor, setSelected]
155
+ );
156
+
157
+ const onClick = useCallback(
158
+ (payload: MouseEvent) => {
159
+ const event = payload;
160
+
161
+ if (isResizing) {
162
+ return true;
163
+ }
164
+ if (event.target === MediaRef.current) {
165
+ if (event.shiftKey) {
166
+ setSelected(!isSelected);
167
+ } else {
168
+ clearSelection();
169
+ setSelected(true);
170
+ }
171
+ return true;
172
+ }
173
+
174
+ return false;
175
+ },
176
+ [isResizing, isSelected, setSelected, clearSelection]
177
+ );
178
+
179
+ const onRightClick = useCallback(
180
+ (event: MouseEvent): void => {
181
+ editor.getEditorState().read(() => {
182
+ const latestSelection = $getSelection();
183
+ const domElement = event.target as HTMLElement;
184
+ if (
185
+ (domElement.tagName === "IMG" || domElement.tagName === "VIDEO") &&
186
+ $isRangeSelection(latestSelection) &&
187
+ latestSelection.getNodes().length === 1
188
+ ) {
189
+ editor.dispatchCommand(
190
+ RIGHT_CLICK_IMAGE_COMMAND,
191
+ event as MouseEvent
192
+ );
193
+ }
194
+ });
195
+ },
196
+ [editor]
197
+ );
198
+
199
+ useEffect(() => {
200
+ let isMounted = true;
201
+ const rootElement = editor.getRootElement();
202
+ const unregister = mergeRegister(
203
+ editor.registerUpdateListener(({ editorState }) => {
204
+ if (isMounted) {
205
+ setSelection(editorState.read(() => $getSelection()));
206
+ }
207
+ }),
208
+ editor.registerCommand(
209
+ SELECTION_CHANGE_COMMAND,
210
+ (_, activeEditor) => {
211
+ activeEditorRef.current = activeEditor;
212
+ return false;
213
+ },
214
+ COMMAND_PRIORITY_LOW
215
+ ),
216
+ editor.registerCommand<MouseEvent>(
217
+ CLICK_COMMAND,
218
+ onClick,
219
+ COMMAND_PRIORITY_LOW
220
+ ),
221
+ editor.registerCommand<MouseEvent>(
222
+ RIGHT_CLICK_IMAGE_COMMAND,
223
+ onClick,
224
+ COMMAND_PRIORITY_LOW
225
+ ),
226
+ editor.registerCommand(
227
+ DRAGSTART_COMMAND,
228
+ (event) => {
229
+ if (event.target === MediaRef.current) {
230
+ event.preventDefault();
231
+ return true;
232
+ }
233
+ return false;
234
+ },
235
+ COMMAND_PRIORITY_LOW
236
+ ),
237
+ editor.registerCommand(
238
+ KEY_DELETE_COMMAND,
239
+ $onDelete,
240
+ COMMAND_PRIORITY_LOW
241
+ ),
242
+ editor.registerCommand(
243
+ KEY_BACKSPACE_COMMAND,
244
+ $onDelete,
245
+ COMMAND_PRIORITY_LOW
246
+ ),
247
+ editor.registerCommand(KEY_ENTER_COMMAND, $onEnter, COMMAND_PRIORITY_LOW),
248
+ editor.registerCommand(
249
+ KEY_ESCAPE_COMMAND,
250
+ $onEscape,
251
+ COMMAND_PRIORITY_LOW
252
+ )
253
+ );
254
+
255
+ rootElement?.addEventListener("contextmenu", onRightClick);
256
+
257
+ return () => {
258
+ isMounted = false;
259
+ unregister();
260
+ rootElement?.removeEventListener("contextmenu", onRightClick);
261
+ };
262
+ }, [
263
+ clearSelection,
264
+ editor,
265
+ isResizing,
266
+ isSelected,
267
+ nodeKey,
268
+ $onDelete,
269
+ $onEnter,
270
+ $onEscape,
271
+ onClick,
272
+ onRightClick,
273
+ setSelected,
274
+ ]);
275
+
276
+ useEffect(() => {
277
+ if (src.endsWith(".mp4")) {
278
+ setMediaType("VIDEO");
279
+ } else if (
280
+ src.endsWith(".jpg") ||
281
+ src.endsWith(".jpeg") ||
282
+ src.endsWith(".png")
283
+ ) {
284
+ setMediaType("IMAGE");
285
+ }
286
+ }, [src]);
287
+
288
+ const onResizeEnd = (
289
+ nextWidth: "inherit" | number,
290
+ nextHeight: "inherit" | number
291
+ ) => {
292
+ setTimeout(() => {
293
+ setIsResizing(false);
294
+ }, 200);
295
+
296
+ editor.update(() => {
297
+ const node = $getNodeByKey(nodeKey);
298
+ if ($isImageNode(node)) {
299
+ node.setWidthAndHeight(nextWidth, nextHeight);
300
+ }
301
+ });
302
+ };
303
+
304
+ const onResizeStart = () => {
305
+ setIsResizing(true);
306
+ };
307
+
308
+ const { historyState } = useSharedHistoryContext();
309
+
310
+ const draggable = isSelected && $isNodeSelection(selection) && !isResizing;
311
+ const isFocused = (isSelected || isResizing) && isEditable;
312
+
313
+ return (
314
+ <Suspense fallback={null}>
315
+ <div className="relative inline-block group">
316
+ <div draggable={draggable}>
317
+ {isLoadError ? (
318
+ <ErrorImage />
319
+ ) : (
320
+ <>
321
+ {MediaType === "IMAGE" ? (
322
+ <LazyImage
323
+ style={{
324
+ width: width === "inherit" ? "400px" : `${width}px`,
325
+ height: height === "inherit" ? "400px" : `${height}px`,
326
+ borderRadius: `${rounded}px`,
327
+ }}
328
+ src={src}
329
+ altText={altText}
330
+ imageRef={MediaRef as React.RefObject<HTMLImageElement>}
331
+ onError={() => setIsLoadError(true)}
332
+ />
333
+ ) : (
334
+ <LazyVideo
335
+ src={src}
336
+ style={{
337
+ width: width === "inherit" ? "800px" : `${width}px`,
338
+ height: height === "inherit" ? "450px" : `${height}px`,
339
+ borderRadius: `${rounded}px`,
340
+ }}
341
+ videoRef={MediaRef as React.RefObject<HTMLVideoElement>}
342
+ onError={() => setIsLoadError(true)}
343
+ rounded={rounded}
344
+ controls={true}
345
+ />
346
+ )}
347
+ </>
348
+ )}
349
+ </div>
350
+ {showCaption && (
351
+ <div>
352
+ <LexicalNestedComposer
353
+ initialEditor={caption}
354
+ initialNodes={[
355
+ RootNode,
356
+ TextNode,
357
+ LineBreakNode,
358
+ ParagraphNode,
359
+ LinkNode,
360
+ ]}
361
+ >
362
+ <AutoFocusPlugin />
363
+ <LinkPlugin />
364
+ <HistoryPlugin externalHistoryState={historyState} />
365
+ <RichTextPlugin
366
+ contentEditable={
367
+ <ContentEditable
368
+ aria-placeholder="Enter a caption..."
369
+ placeholder={() => (
370
+ <span
371
+ className="
372
+ color-[#999] text-xs overflow-hidden absolute text-ellipsis
373
+ select-none text-nowrap inline-block pointer-events-none
374
+ transform -translate-x-1/2 -translate-y-1/2 bottom-[-8px]
375
+ "
376
+ >
377
+ Enter a caption...
378
+ </span>
379
+ )}
380
+ className="text-center relative text-xs text-muted-foreground outline-none"
381
+ />
382
+ }
383
+ ErrorBoundary={LexicalErrorBoundary}
384
+ />
385
+ </LexicalNestedComposer>
386
+ </div>
387
+ )}
388
+ {isEditable && !isResizing && (
389
+ <ImageToolBar
390
+ width={width}
391
+ height={height}
392
+ editor={editor}
393
+ nodeKey={nodeKey}
394
+ />
395
+ )}
396
+
397
+ {resizable && $isNodeSelection(selection) && isFocused && (
398
+ <ImageResizer
399
+ editor={editor}
400
+ MediaRef={MediaRef}
401
+ onResizeStart={onResizeStart}
402
+ onResizeEnd={onResizeEnd}
403
+ />
404
+ )}
405
+ </div>
406
+ </Suspense>
407
+ );
408
+ }