@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,228 @@
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 type {ExcalidrawInitialElements} from '@/components/ui/excalidraw/ExcalidrawModal';
10
+ import type {AppState, BinaryFiles} from '@excalidraw/excalidraw/types';
11
+ import type {NodeKey} from 'lexical';
12
+ import type {JSX} from 'react';
13
+
14
+ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
15
+ import {useLexicalEditable} from '@lexical/react/useLexicalEditable';
16
+ import {useLexicalNodeSelection} from '@lexical/react/useLexicalNodeSelection';
17
+ import {mergeRegister} from '@lexical/utils';
18
+ import {
19
+ $getNodeByKey,
20
+ CLICK_COMMAND,
21
+ COMMAND_PRIORITY_LOW,
22
+ isDOMNode,
23
+ } from 'lexical';
24
+ import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
25
+ import * as React from 'react';
26
+
27
+ import ExcalidrawModal from '@/components/ui/excalidraw/ExcalidrawModal';
28
+ import {$isExcalidrawNode} from '.';
29
+ import ExcalidrawImage from './ExcalidrawImage';
30
+ import ImageResizer from './ImageResizer';
31
+
32
+ export default function ExcalidrawComponent({
33
+ nodeKey,
34
+ data,
35
+ width,
36
+ height,
37
+ }: {
38
+ data: string;
39
+ nodeKey: NodeKey;
40
+ width: 'inherit' | number;
41
+ height: 'inherit' | number;
42
+ }): JSX.Element {
43
+ const [editor] = useLexicalComposerContext();
44
+ const isEditable = useLexicalEditable();
45
+ const [isModalOpen, setModalOpen] = useState<boolean>(
46
+ data === '[]' && editor.isEditable(),
47
+ );
48
+ const imageContainerRef = useRef<HTMLDivElement | null>(null);
49
+ const buttonRef = useRef<HTMLButtonElement | null>(null);
50
+ const captionButtonRef = useRef<HTMLButtonElement | null>(null);
51
+ const [isSelected, setSelected, clearSelection] =
52
+ useLexicalNodeSelection(nodeKey);
53
+ const [isResizing, setIsResizing] = useState<boolean>(false);
54
+
55
+ useEffect(() => {
56
+ if (!isEditable) {
57
+ if (isSelected) {
58
+ clearSelection();
59
+ }
60
+ return;
61
+ }
62
+ return mergeRegister(
63
+ editor.registerCommand(
64
+ CLICK_COMMAND,
65
+ (event: MouseEvent) => {
66
+ const buttonElem = buttonRef.current;
67
+ const eventTarget = event.target;
68
+
69
+ if (isResizing) {
70
+ return true;
71
+ }
72
+
73
+ if (
74
+ buttonElem !== null &&
75
+ isDOMNode(eventTarget) &&
76
+ buttonElem.contains(eventTarget)
77
+ ) {
78
+ if (!event.shiftKey) {
79
+ clearSelection();
80
+ }
81
+ setSelected(!isSelected);
82
+ if (event.detail > 1) {
83
+ setModalOpen(true);
84
+ }
85
+ return true;
86
+ }
87
+
88
+ return false;
89
+ },
90
+ COMMAND_PRIORITY_LOW,
91
+ ),
92
+ );
93
+ }, [clearSelection, editor, isSelected, isResizing, setSelected, isEditable]);
94
+
95
+ const deleteNode = useCallback(() => {
96
+ setModalOpen(false);
97
+ return editor.update(() => {
98
+ const node = $getNodeByKey(nodeKey);
99
+ if (node) {
100
+ node.remove();
101
+ }
102
+ });
103
+ }, [editor, nodeKey]);
104
+
105
+ const setData = (
106
+ els: ExcalidrawInitialElements,
107
+ aps: Partial<AppState>,
108
+ fls: BinaryFiles,
109
+ ) => {
110
+ return editor.update(() => {
111
+ const node = $getNodeByKey(nodeKey);
112
+ if ($isExcalidrawNode(node)) {
113
+ if ((els && els.length > 0) || Object.keys(fls).length > 0) {
114
+ node.setData(
115
+ JSON.stringify({
116
+ appState: aps,
117
+ elements: els,
118
+ files: fls,
119
+ }),
120
+ );
121
+ } else {
122
+ node.remove();
123
+ }
124
+ }
125
+ });
126
+ };
127
+
128
+ const onResizeStart = () => {
129
+ setIsResizing(true);
130
+ };
131
+
132
+ const onResizeEnd = (
133
+ nextWidth: 'inherit' | number,
134
+ nextHeight: 'inherit' | number,
135
+ ) => {
136
+ // Delay hiding the resize bars for click case
137
+ setTimeout(() => {
138
+ setIsResizing(false);
139
+ }, 200);
140
+
141
+ editor.update(() => {
142
+ const node = $getNodeByKey(nodeKey);
143
+
144
+ if ($isExcalidrawNode(node)) {
145
+ node.setWidth(nextWidth);
146
+ node.setHeight(nextHeight);
147
+ }
148
+ });
149
+ };
150
+
151
+ const openModal = useCallback(() => {
152
+ setModalOpen(true);
153
+ }, []);
154
+
155
+ const {
156
+ elements = [],
157
+ files = {},
158
+ appState = {},
159
+ } = useMemo(() => JSON.parse(data), [data]);
160
+
161
+ const closeModal = useCallback(() => {
162
+ setModalOpen(false);
163
+ if (elements.length === 0) {
164
+ editor.update(() => {
165
+ const node = $getNodeByKey(nodeKey);
166
+ if (node) {
167
+ node.remove();
168
+ }
169
+ });
170
+ }
171
+ }, [editor, nodeKey, elements.length]);
172
+
173
+ return (
174
+ <>
175
+ {isEditable && isModalOpen && (
176
+ <ExcalidrawModal
177
+ initialElements={elements}
178
+ initialFiles={files}
179
+ initialAppState={appState}
180
+ isShown={isModalOpen}
181
+ onDelete={deleteNode}
182
+ onClose={closeModal}
183
+ onSave={(els, aps, fls) => {
184
+ setData(els, aps, fls);
185
+ setModalOpen(false);
186
+ }}
187
+ closeOnClickOutside={false}
188
+ />
189
+ )}
190
+ {elements.length > 0 && (
191
+ <button
192
+ ref={buttonRef}
193
+ className={`excalidraw-button ${isSelected ? 'selected' : ''}`}>
194
+ <ExcalidrawImage
195
+ imageContainerRef={imageContainerRef}
196
+ className="image"
197
+ elements={elements}
198
+ files={files}
199
+ appState={appState}
200
+ width={width}
201
+ height={height}
202
+ />
203
+ {isSelected && isEditable && (
204
+ <div
205
+ className="image-edit-button"
206
+ role="button"
207
+ tabIndex={0}
208
+ onMouseDown={(event) => event.preventDefault()}
209
+ onClick={openModal}
210
+ />
211
+ )}
212
+ {(isSelected || isResizing) && isEditable && (
213
+ <ImageResizer
214
+ buttonRef={captionButtonRef}
215
+ showCaption={true}
216
+ setShowCaption={() => null}
217
+ imageRef={imageContainerRef}
218
+ editor={editor}
219
+ onResizeStart={onResizeStart}
220
+ onResizeEnd={onResizeEnd}
221
+ captionsEnabled={true}
222
+ />
223
+ )}
224
+ </button>
225
+ )}
226
+ </>
227
+ );
228
+ }
@@ -0,0 +1,137 @@
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 type {
10
+ ExcalidrawElement,
11
+ NonDeleted,
12
+ } from '@excalidraw/excalidraw/element/types';
13
+ import type {AppState, BinaryFiles} from '@excalidraw/excalidraw/types';
14
+ import type {JSX} from 'react';
15
+
16
+ import {exportToSvg} from '@excalidraw/excalidraw';
17
+ import * as React from 'react';
18
+ import {useEffect, useState} from 'react';
19
+
20
+ type ImageType = 'svg' | 'canvas';
21
+
22
+ type Dimension = 'inherit' | number;
23
+
24
+ type Props = {
25
+ /**
26
+ * Configures the export setting for SVG/Canvas
27
+ */
28
+ appState: AppState;
29
+ /**
30
+ * The css class applied to image to be rendered
31
+ */
32
+ className?: string;
33
+ /**
34
+ * The Excalidraw elements to be rendered as an image
35
+ */
36
+ elements: NonDeleted<ExcalidrawElement>[];
37
+ /**
38
+ * The Excalidraw files associated with the elements
39
+ */
40
+ files: BinaryFiles;
41
+ /**
42
+ * The height of the image to be rendered
43
+ */
44
+ height?: Dimension;
45
+ /**
46
+ * The ref object to be used to render the image
47
+ */
48
+ imageContainerRef: React.MutableRefObject<HTMLDivElement | null>;
49
+ /**
50
+ * The type of image to be rendered
51
+ */
52
+ imageType?: ImageType;
53
+ /**
54
+ * The css class applied to the root element of this component
55
+ */
56
+ rootClassName?: string | null;
57
+ /**
58
+ * The width of the image to be rendered
59
+ */
60
+ width?: Dimension;
61
+ };
62
+
63
+ // exportToSvg has fonts from excalidraw.com
64
+ // We don't want them to be used in open source
65
+ const removeStyleFromSvg_HACK = (svg: SVGElement) => {
66
+ const styleTag = svg?.firstElementChild?.firstElementChild;
67
+
68
+ // Generated SVG is getting double-sized by height and width attributes
69
+ // We want to match the real size of the SVG element
70
+ const viewBox = svg.getAttribute('viewBox');
71
+ if (viewBox != null) {
72
+ const viewBoxDimensions = viewBox.split(' ');
73
+ svg.setAttribute('width', viewBoxDimensions[2]);
74
+ svg.setAttribute('height', viewBoxDimensions[3]);
75
+ }
76
+
77
+ if (styleTag && styleTag.tagName === 'style') {
78
+ styleTag.remove();
79
+ }
80
+ };
81
+
82
+ /**
83
+ * @explorer-desc
84
+ * A component for rendering Excalidraw elements as a static image
85
+ */
86
+ export default function ExcalidrawImage({
87
+ elements,
88
+ files,
89
+ imageContainerRef,
90
+ appState,
91
+ rootClassName = null,
92
+ width = 'inherit',
93
+ height = 'inherit',
94
+ }: Props): JSX.Element {
95
+ const [Svg, setSvg] = useState<SVGElement | null>(null);
96
+
97
+ useEffect(() => {
98
+ const setContent = async () => {
99
+ const svg: SVGElement = await exportToSvg({
100
+ appState,
101
+ elements,
102
+ files,
103
+ });
104
+ removeStyleFromSvg_HACK(svg);
105
+
106
+ svg.setAttribute('width', '100%');
107
+ svg.setAttribute('height', '100%');
108
+ svg.setAttribute('display', 'block');
109
+
110
+ setSvg(svg);
111
+ };
112
+ setContent();
113
+ }, [elements, files, appState]);
114
+
115
+ const containerStyle: React.CSSProperties = {};
116
+ if (width !== 'inherit') {
117
+ containerStyle.width = `${width}px`;
118
+ }
119
+ if (height !== 'inherit') {
120
+ containerStyle.height = `${height}px`;
121
+ }
122
+
123
+ return (
124
+ <div
125
+ ref={(node) => {
126
+ if (node) {
127
+ if (imageContainerRef) {
128
+ imageContainerRef.current = node;
129
+ }
130
+ }
131
+ }}
132
+ className={rootClassName ?? ''}
133
+ style={containerStyle}
134
+ dangerouslySetInnerHTML={{__html: Svg?.outerHTML ?? ''}}
135
+ />
136
+ );
137
+ }
@@ -0,0 +1,317 @@
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 type {LexicalEditor} from 'lexical';
10
+ import type {JSX} from 'react';
11
+
12
+ import {calculateZoomLevel} from '@lexical/utils';
13
+ import * as React from 'react';
14
+ import {useRef} from 'react';
15
+
16
+ function clamp(value: number, min: number, max: number) {
17
+ return Math.min(Math.max(value, min), max);
18
+ }
19
+
20
+ const Direction = {
21
+ east: 1 << 0,
22
+ north: 1 << 3,
23
+ south: 1 << 1,
24
+ west: 1 << 2,
25
+ };
26
+
27
+ export default function ImageResizer({
28
+ onResizeStart,
29
+ onResizeEnd,
30
+ buttonRef,
31
+ imageRef,
32
+ maxWidth,
33
+ editor,
34
+ showCaption,
35
+ setShowCaption,
36
+ captionsEnabled,
37
+ }: {
38
+ editor: LexicalEditor;
39
+ buttonRef: {current: null | HTMLButtonElement};
40
+ imageRef: {current: null | HTMLElement};
41
+ maxWidth?: number;
42
+ onResizeEnd: (width: 'inherit' | number, height: 'inherit' | number) => void;
43
+ onResizeStart: () => void;
44
+ setShowCaption: (show: boolean) => void;
45
+ showCaption: boolean;
46
+ captionsEnabled: boolean;
47
+ }): JSX.Element {
48
+ const controlWrapperRef = useRef<HTMLDivElement>(null);
49
+ const userSelect = useRef({
50
+ priority: '',
51
+ value: 'default',
52
+ });
53
+ const positioningRef = useRef<{
54
+ currentHeight: 'inherit' | number;
55
+ currentWidth: 'inherit' | number;
56
+ direction: number;
57
+ isResizing: boolean;
58
+ ratio: number;
59
+ startHeight: number;
60
+ startWidth: number;
61
+ startX: number;
62
+ startY: number;
63
+ }>({
64
+ currentHeight: 0,
65
+ currentWidth: 0,
66
+ direction: 0,
67
+ isResizing: false,
68
+ ratio: 0,
69
+ startHeight: 0,
70
+ startWidth: 0,
71
+ startX: 0,
72
+ startY: 0,
73
+ });
74
+ const editorRootElement = editor.getRootElement();
75
+ // Find max width, accounting for editor padding.
76
+ const maxWidthContainer = maxWidth
77
+ ? maxWidth
78
+ : editorRootElement !== null
79
+ ? editorRootElement.getBoundingClientRect().width - 20
80
+ : 100;
81
+ const maxHeightContainer =
82
+ editorRootElement !== null
83
+ ? editorRootElement.getBoundingClientRect().height - 20
84
+ : 100;
85
+
86
+ const minWidth = 100;
87
+ const minHeight = 100;
88
+
89
+ const setStartCursor = (direction: number) => {
90
+ const ew = direction === Direction.east || direction === Direction.west;
91
+ const ns = direction === Direction.north || direction === Direction.south;
92
+ const nwse =
93
+ (direction & Direction.north && direction & Direction.west) ||
94
+ (direction & Direction.south && direction & Direction.east);
95
+
96
+ const cursorDir = ew ? 'ew' : ns ? 'ns' : nwse ? 'nwse' : 'nesw';
97
+
98
+ if (editorRootElement !== null) {
99
+ editorRootElement.style.setProperty(
100
+ 'cursor',
101
+ `${cursorDir}-resize`,
102
+ 'important',
103
+ );
104
+ }
105
+ if (document.body !== null) {
106
+ document.body.style.setProperty(
107
+ 'cursor',
108
+ `${cursorDir}-resize`,
109
+ 'important',
110
+ );
111
+ userSelect.current.value = document.body.style.getPropertyValue(
112
+ '-webkit-user-select',
113
+ );
114
+ userSelect.current.priority = document.body.style.getPropertyPriority(
115
+ '-webkit-user-select',
116
+ );
117
+ document.body.style.setProperty(
118
+ '-webkit-user-select',
119
+ `none`,
120
+ 'important',
121
+ );
122
+ }
123
+ };
124
+
125
+ const setEndCursor = () => {
126
+ if (editorRootElement !== null) {
127
+ editorRootElement.style.setProperty('cursor', 'text');
128
+ }
129
+ if (document.body !== null) {
130
+ document.body.style.setProperty('cursor', 'default');
131
+ document.body.style.setProperty(
132
+ '-webkit-user-select',
133
+ userSelect.current.value,
134
+ userSelect.current.priority,
135
+ );
136
+ }
137
+ };
138
+
139
+ const handlePointerDown = (
140
+ event: React.PointerEvent<HTMLDivElement>,
141
+ direction: number,
142
+ ) => {
143
+ if (!editor.isEditable()) {
144
+ return;
145
+ }
146
+
147
+ const image = imageRef.current;
148
+ const controlWrapper = controlWrapperRef.current;
149
+
150
+ if (image !== null && controlWrapper !== null) {
151
+ event.preventDefault();
152
+ const {width, height} = image.getBoundingClientRect();
153
+ const zoom = calculateZoomLevel(image);
154
+ const positioning = positioningRef.current;
155
+ positioning.startWidth = width;
156
+ positioning.startHeight = height;
157
+ positioning.ratio = width / height;
158
+ positioning.currentWidth = width;
159
+ positioning.currentHeight = height;
160
+ positioning.startX = event.clientX / zoom;
161
+ positioning.startY = event.clientY / zoom;
162
+ positioning.isResizing = true;
163
+ positioning.direction = direction;
164
+
165
+ setStartCursor(direction);
166
+ onResizeStart();
167
+
168
+ controlWrapper.classList.add('image-control-wrapper--resizing');
169
+ image.style.height = `${height}px`;
170
+ image.style.width = `${width}px`;
171
+
172
+ document.addEventListener('pointermove', handlePointerMove);
173
+ document.addEventListener('pointerup', handlePointerUp);
174
+ }
175
+ };
176
+ const handlePointerMove = (event: PointerEvent) => {
177
+ const image = imageRef.current;
178
+ const positioning = positioningRef.current;
179
+
180
+ const isHorizontal =
181
+ positioning.direction & (Direction.east | Direction.west);
182
+ const isVertical =
183
+ positioning.direction & (Direction.south | Direction.north);
184
+
185
+ if (image !== null && positioning.isResizing) {
186
+ const zoom = calculateZoomLevel(image);
187
+ // Corner cursor
188
+ if (isHorizontal && isVertical) {
189
+ let diff = Math.floor(positioning.startX - event.clientX / zoom);
190
+ diff = positioning.direction & Direction.east ? -diff : diff;
191
+
192
+ const width = clamp(
193
+ positioning.startWidth + diff,
194
+ minWidth,
195
+ maxWidthContainer,
196
+ );
197
+
198
+ const height = width / positioning.ratio;
199
+ image.style.width = `${width}px`;
200
+ image.style.height = `${height}px`;
201
+ positioning.currentHeight = height;
202
+ positioning.currentWidth = width;
203
+ } else if (isVertical) {
204
+ let diff = Math.floor(positioning.startY - event.clientY / zoom);
205
+ diff = positioning.direction & Direction.south ? -diff : diff;
206
+
207
+ const height = clamp(
208
+ positioning.startHeight + diff,
209
+ minHeight,
210
+ maxHeightContainer,
211
+ );
212
+
213
+ image.style.height = `${height}px`;
214
+ positioning.currentHeight = height;
215
+ } else {
216
+ let diff = Math.floor(positioning.startX - event.clientX / zoom);
217
+ diff = positioning.direction & Direction.east ? -diff : diff;
218
+
219
+ const width = clamp(
220
+ positioning.startWidth + diff,
221
+ minWidth,
222
+ maxWidthContainer,
223
+ );
224
+
225
+ image.style.width = `${width}px`;
226
+ positioning.currentWidth = width;
227
+ }
228
+ }
229
+ };
230
+ const handlePointerUp = () => {
231
+ const image = imageRef.current;
232
+ const positioning = positioningRef.current;
233
+ const controlWrapper = controlWrapperRef.current;
234
+ if (image !== null && controlWrapper !== null && positioning.isResizing) {
235
+ const width = positioning.currentWidth;
236
+ const height = positioning.currentHeight;
237
+ positioning.startWidth = 0;
238
+ positioning.startHeight = 0;
239
+ positioning.ratio = 0;
240
+ positioning.startX = 0;
241
+ positioning.startY = 0;
242
+ positioning.currentWidth = 0;
243
+ positioning.currentHeight = 0;
244
+ positioning.isResizing = false;
245
+
246
+ controlWrapper.classList.remove('image-control-wrapper--resizing');
247
+
248
+ setEndCursor();
249
+ onResizeEnd(width, height);
250
+
251
+ document.removeEventListener('pointermove', handlePointerMove);
252
+ document.removeEventListener('pointerup', handlePointerUp);
253
+ }
254
+ };
255
+ return (
256
+ <div ref={controlWrapperRef}>
257
+ {!showCaption && captionsEnabled && (
258
+ <button
259
+ className="image-caption-button"
260
+ ref={buttonRef}
261
+ onClick={() => {
262
+ setShowCaption(!showCaption);
263
+ }}>
264
+ Add Caption
265
+ </button>
266
+ )}
267
+ <div
268
+ className="image-resizer image-resizer-n"
269
+ onPointerDown={(event) => {
270
+ handlePointerDown(event, Direction.north);
271
+ }}
272
+ />
273
+ <div
274
+ className="image-resizer image-resizer-ne"
275
+ onPointerDown={(event) => {
276
+ handlePointerDown(event, Direction.north | Direction.east);
277
+ }}
278
+ />
279
+ <div
280
+ className="image-resizer image-resizer-e"
281
+ onPointerDown={(event) => {
282
+ handlePointerDown(event, Direction.east);
283
+ }}
284
+ />
285
+ <div
286
+ className="image-resizer image-resizer-se"
287
+ onPointerDown={(event) => {
288
+ handlePointerDown(event, Direction.south | Direction.east);
289
+ }}
290
+ />
291
+ <div
292
+ className="image-resizer image-resizer-s"
293
+ onPointerDown={(event) => {
294
+ handlePointerDown(event, Direction.south);
295
+ }}
296
+ />
297
+ <div
298
+ className="image-resizer image-resizer-sw"
299
+ onPointerDown={(event) => {
300
+ handlePointerDown(event, Direction.south | Direction.west);
301
+ }}
302
+ />
303
+ <div
304
+ className="image-resizer image-resizer-w"
305
+ onPointerDown={(event) => {
306
+ handlePointerDown(event, Direction.west);
307
+ }}
308
+ />
309
+ <div
310
+ className="image-resizer image-resizer-nw"
311
+ onPointerDown={(event) => {
312
+ handlePointerDown(event, Direction.north | Direction.west);
313
+ }}
314
+ />
315
+ </div>
316
+ );
317
+ }