5htp-core 0.4.8 → 0.4.9

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 (187) hide show
  1. package/package.json +5 -1
  2. package/src/client/assets/css/components/table.less +2 -0
  3. package/src/client/components/Form.ts +1 -1
  4. package/src/client/components/button.tsx +2 -1
  5. package/src/client/components/containers/Popover/index.tsx +2 -2
  6. package/src/client/components/dropdown/index.tsx +16 -6
  7. package/src/client/components/input/Slider/index.tsx +0 -2
  8. package/src/client/components/inputv3/Rte/Editor.tsx +271 -0
  9. package/src/client/components/inputv3/Rte/ToolbarPlugin/BlockFormat.tsx +220 -0
  10. package/src/client/components/inputv3/Rte/ToolbarPlugin/ElementFormat.tsx +107 -0
  11. package/src/client/components/inputv3/Rte/ToolbarPlugin/index.tsx +768 -0
  12. package/src/client/components/inputv3/Rte/appSettings.ts +36 -0
  13. package/src/client/components/inputv3/Rte/context/FlashMessageContext.tsx +68 -0
  14. package/src/client/components/inputv3/Rte/context/SettingsContext.tsx +71 -0
  15. package/src/client/components/inputv3/Rte/context/SharedAutocompleteContext.tsx +71 -0
  16. package/src/client/components/inputv3/Rte/context/SharedHistoryContext.tsx +35 -0
  17. package/src/client/components/inputv3/Rte/currentEditor.ts +42 -0
  18. package/src/client/components/inputv3/Rte/hooks/useFlashMessage.tsx +16 -0
  19. package/src/client/components/inputv3/Rte/hooks/useReport.ts +67 -0
  20. package/src/client/components/inputv3/Rte/images/emoji/1F600.png +0 -0
  21. package/src/client/components/inputv3/Rte/images/emoji/1F641.png +0 -0
  22. package/src/client/components/inputv3/Rte/images/emoji/1F642.png +0 -0
  23. package/src/client/components/inputv3/Rte/images/emoji/2764.png +0 -0
  24. package/src/client/components/inputv3/Rte/images/emoji/LICENSE.md +5 -0
  25. package/src/client/components/inputv3/Rte/images/icons/draggable-block-menu.svg +1 -0
  26. package/src/client/components/inputv3/Rte/images/icons/prettier-error.svg +1 -0
  27. package/src/client/components/inputv3/Rte/images/icons/prettier.svg +1 -0
  28. package/src/client/components/inputv3/Rte/images/image/LICENSE.md +5 -0
  29. package/src/client/components/inputv3/Rte/images/image-broken.svg +4 -0
  30. package/src/client/components/inputv3/Rte/images/logo.svg +1 -0
  31. package/src/client/components/inputv3/Rte/index.tsx +63 -79
  32. package/src/client/components/inputv3/Rte/nodes/AutocompleteNode.tsx +119 -0
  33. package/src/client/components/inputv3/Rte/nodes/EmojiNode.tsx +102 -0
  34. package/src/client/components/inputv3/Rte/nodes/EquationComponent.tsx +141 -0
  35. package/src/client/components/inputv3/Rte/nodes/EquationNode.tsx +174 -0
  36. package/src/client/components/inputv3/Rte/nodes/FigmaNode.tsx +135 -0
  37. package/src/client/components/inputv3/Rte/nodes/ImageComponent.tsx +468 -0
  38. package/src/client/components/inputv3/Rte/nodes/ImageNode.css +43 -0
  39. package/src/client/components/inputv3/Rte/nodes/ImageNode.tsx +266 -0
  40. package/src/client/components/inputv3/Rte/nodes/InlineImageNode/InlineImageComponent.tsx +402 -0
  41. package/src/client/components/inputv3/Rte/nodes/InlineImageNode/InlineImageNode.css +94 -0
  42. package/src/client/components/inputv3/Rte/nodes/InlineImageNode/InlineImageNode.tsx +294 -0
  43. package/src/client/components/inputv3/Rte/nodes/KeywordNode.ts +67 -0
  44. package/src/client/components/inputv3/Rte/nodes/LayoutContainerNode.ts +137 -0
  45. package/src/client/components/inputv3/Rte/nodes/LayoutItemNode.ts +71 -0
  46. package/src/client/components/inputv3/Rte/nodes/MentionNode.ts +130 -0
  47. package/src/client/components/inputv3/Rte/nodes/PageBreakNode/index.css +62 -0
  48. package/src/client/components/inputv3/Rte/nodes/PageBreakNode/index.tsx +170 -0
  49. package/src/client/components/inputv3/Rte/nodes/PlaygroundNodes.ts +76 -0
  50. package/src/client/components/inputv3/Rte/nodes/PollComponent.tsx +249 -0
  51. package/src/client/components/inputv3/Rte/nodes/PollNode.css +187 -0
  52. package/src/client/components/inputv3/Rte/nodes/PollNode.tsx +209 -0
  53. package/src/client/components/inputv3/Rte/nodes/StickyComponent.tsx +261 -0
  54. package/src/client/components/inputv3/Rte/nodes/StickyNode.css +37 -0
  55. package/src/client/components/inputv3/Rte/nodes/StickyNode.tsx +150 -0
  56. package/src/client/components/inputv3/Rte/nodes/TweetNode.tsx +223 -0
  57. package/src/client/components/inputv3/Rte/nodes/YouTubeNode.tsx +184 -0
  58. package/src/client/components/inputv3/Rte/plugins/ActionsPlugin/index.tsx +334 -0
  59. package/src/client/components/inputv3/Rte/plugins/AutoEmbedPlugin/index.tsx +352 -0
  60. package/src/client/components/inputv3/Rte/plugins/AutoLinkPlugin/index.tsx +32 -0
  61. package/src/client/components/inputv3/Rte/plugins/AutocompletePlugin/index.tsx +2529 -0
  62. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/components/CopyButton/index.tsx +70 -0
  63. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/components/PrettierButton/index.css +14 -0
  64. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/components/PrettierButton/index.tsx +156 -0
  65. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/index.css +54 -0
  66. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/index.tsx +190 -0
  67. package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/utils.ts +33 -0
  68. package/src/client/components/inputv3/Rte/plugins/CodeHighlightPlugin/index.ts +21 -0
  69. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/Collapsible.css +57 -0
  70. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleContainerNode.ts +168 -0
  71. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleContentNode.ts +127 -0
  72. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleTitleNode.ts +152 -0
  73. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleUtils.ts +17 -0
  74. package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/index.ts +284 -0
  75. package/src/client/components/inputv3/Rte/plugins/ComponentPickerPlugin/index.tsx +370 -0
  76. package/src/client/components/inputv3/Rte/plugins/ContextMenuPlugin/index.tsx +270 -0
  77. package/src/client/components/inputv3/Rte/plugins/DocsPlugin/index.tsx +20 -0
  78. package/src/client/components/inputv3/Rte/plugins/DragDropPastePlugin/index.ts +51 -0
  79. package/src/client/components/inputv3/Rte/plugins/DraggableBlockPlugin/index.css +36 -0
  80. package/src/client/components/inputv3/Rte/plugins/DraggableBlockPlugin/index.tsx +43 -0
  81. package/src/client/components/inputv3/Rte/plugins/EmojiPickerPlugin/index.tsx +198 -0
  82. package/src/client/components/inputv3/Rte/plugins/EmojisPlugin/index.ts +75 -0
  83. package/src/client/components/inputv3/Rte/plugins/EquationsPlugin/index.tsx +82 -0
  84. package/src/client/components/inputv3/Rte/plugins/FigmaPlugin/index.tsx +40 -0
  85. package/src/client/components/inputv3/Rte/plugins/FloatingLinkEditorPlugin/index.css +41 -0
  86. package/src/client/components/inputv3/Rte/plugins/FloatingLinkEditorPlugin/index.tsx +393 -0
  87. package/src/client/components/inputv3/Rte/plugins/FloatingTextFormatToolbarPlugin/index.css +141 -0
  88. package/src/client/components/inputv3/Rte/plugins/FloatingTextFormatToolbarPlugin/index.tsx +388 -0
  89. package/src/client/components/inputv3/Rte/plugins/ImagesPlugin/index.tsx +350 -0
  90. package/src/client/components/inputv3/Rte/plugins/InlineImagePlugin/index.tsx +336 -0
  91. package/src/client/components/inputv3/Rte/plugins/KeywordsPlugin/index.ts +56 -0
  92. package/src/client/components/inputv3/Rte/plugins/LayoutPlugin/InsertLayoutDialog.tsx +58 -0
  93. package/src/client/components/inputv3/Rte/plugins/LayoutPlugin/LayoutPlugin.tsx +219 -0
  94. package/src/client/components/inputv3/Rte/plugins/LinkPlugin/index.tsx +34 -0
  95. package/src/client/components/inputv3/Rte/plugins/ListMaxIndentLevelPlugin/index.ts +85 -0
  96. package/src/client/components/inputv3/Rte/plugins/MarkdownShortcutPlugin/index.tsx +16 -0
  97. package/src/client/components/inputv3/Rte/plugins/MarkdownTransformers/index.ts +324 -0
  98. package/src/client/components/inputv3/Rte/plugins/MaxLengthPlugin/index.tsx +53 -0
  99. package/src/client/components/inputv3/Rte/plugins/MentionsPlugin/index.tsx +696 -0
  100. package/src/client/components/inputv3/Rte/plugins/PageBreakPlugin/index.tsx +57 -0
  101. package/src/client/components/inputv3/Rte/plugins/PasteLogPlugin/index.tsx +54 -0
  102. package/src/client/components/inputv3/Rte/plugins/PollPlugin/index.tsx +86 -0
  103. package/src/client/components/inputv3/Rte/plugins/SpeechToTextPlugin/index.ts +125 -0
  104. package/src/client/components/inputv3/Rte/plugins/StickyPlugin/index.ts +22 -0
  105. package/src/client/components/inputv3/Rte/plugins/TabFocusPlugin/index.tsx +65 -0
  106. package/src/client/components/inputv3/Rte/plugins/TableActionMenuPlugin/index.tsx +773 -0
  107. package/src/client/components/inputv3/Rte/plugins/TableCellResizer/index.css +12 -0
  108. package/src/client/components/inputv3/Rte/plugins/TableCellResizer/index.tsx +436 -0
  109. package/src/client/components/inputv3/Rte/plugins/TableHoverActionsPlugin/index.tsx +287 -0
  110. package/src/client/components/inputv3/Rte/plugins/TableOfContentsPlugin/index.css +95 -0
  111. package/src/client/components/inputv3/Rte/plugins/TableOfContentsPlugin/index.tsx +197 -0
  112. package/src/client/components/inputv3/Rte/plugins/TablePlugin.tsx +178 -0
  113. package/src/client/components/inputv3/Rte/plugins/TestRecorderPlugin/index.tsx +468 -0
  114. package/src/client/components/inputv3/Rte/plugins/TreeViewPlugin/index.tsx +26 -0
  115. package/src/client/components/inputv3/Rte/plugins/TwitterPlugin/index.ts +41 -0
  116. package/src/client/components/inputv3/Rte/plugins/TypingPerfPlugin/index.ts +117 -0
  117. package/src/client/components/inputv3/Rte/plugins/YouTubePlugin/index.ts +41 -0
  118. package/src/client/components/inputv3/Rte/shared/canUseDOM.ts +4 -0
  119. package/src/client/components/inputv3/Rte/shared/caretFromPoint.ts +40 -0
  120. package/src/client/components/inputv3/Rte/shared/environment.ts +56 -0
  121. package/src/client/components/inputv3/Rte/shared/invariant.ts +26 -0
  122. package/src/client/components/inputv3/Rte/shared/normalizeClassNames.ts +21 -0
  123. package/src/client/components/inputv3/Rte/shared/react-test-utils.ts +18 -0
  124. package/src/client/components/inputv3/Rte/shared/reactPatches.ts +22 -0
  125. package/src/client/components/inputv3/Rte/shared/simpleDiffWithCursor.ts +49 -0
  126. package/src/client/components/inputv3/Rte/shared/useLayoutEffect.ts +19 -0
  127. package/src/client/components/inputv3/Rte/shared/warnOnlyOnce.ts +20 -0
  128. package/src/client/components/inputv3/Rte/style.less +30 -60
  129. package/src/client/components/inputv3/Rte/themes/CommentEditorTheme.css +13 -0
  130. package/src/client/components/inputv3/Rte/themes/CommentEditorTheme.ts +20 -0
  131. package/src/client/components/inputv3/Rte/themes/PlaygroundEditorTheme.css +447 -0
  132. package/src/client/components/inputv3/Rte/themes/PlaygroundEditorTheme.ts +120 -0
  133. package/src/client/components/inputv3/Rte/themes/StickyEditorTheme.css +13 -0
  134. package/src/client/components/inputv3/Rte/themes/StickyEditorTheme.ts +20 -0
  135. package/src/client/components/inputv3/Rte/ui/ColorPicker.css +88 -0
  136. package/src/client/components/inputv3/Rte/ui/ColorPicker.tsx +365 -0
  137. package/src/client/components/inputv3/Rte/ui/ContentEditable.css +44 -0
  138. package/src/client/components/inputv3/Rte/ui/ContentEditable.tsx +36 -0
  139. package/src/client/components/inputv3/Rte/ui/DropDown.tsx +259 -0
  140. package/src/client/components/inputv3/Rte/ui/DropdownColorPicker.tsx +41 -0
  141. package/src/client/components/inputv3/Rte/ui/EquationEditor.css +38 -0
  142. package/src/client/components/inputv3/Rte/ui/EquationEditor.tsx +56 -0
  143. package/src/client/components/inputv3/Rte/ui/FileInput.tsx +38 -0
  144. package/src/client/components/inputv3/Rte/ui/FlashMessage.css +28 -0
  145. package/src/client/components/inputv3/Rte/ui/FlashMessage.tsx +29 -0
  146. package/src/client/components/inputv3/Rte/ui/ImageResizer.tsx +316 -0
  147. package/src/client/components/inputv3/Rte/ui/Input.css +32 -0
  148. package/src/client/components/inputv3/Rte/ui/KatexRenderer.tsx +54 -0
  149. package/src/client/components/inputv3/Rte/ui/Switch.tsx +36 -0
  150. package/src/client/components/inputv3/Rte/utils/docSerialization.ts +77 -0
  151. package/src/client/components/inputv3/Rte/utils/emoji-list.ts +16615 -0
  152. package/src/client/components/inputv3/Rte/utils/getDOMRangeRect.ts +27 -0
  153. package/src/client/components/inputv3/Rte/utils/getSelectedNode.ts +27 -0
  154. package/src/client/components/inputv3/Rte/utils/guard.ts +10 -0
  155. package/src/client/components/inputv3/Rte/utils/isMobileWidth.ts +7 -0
  156. package/src/client/components/inputv3/Rte/utils/joinClasses.ts +13 -0
  157. package/src/client/components/inputv3/Rte/utils/setFloatingElemPosition.ts +51 -0
  158. package/src/client/components/inputv3/Rte/utils/setFloatingElemPositionForLinkEditor.ts +46 -0
  159. package/src/client/components/inputv3/Rte/utils/swipe.ts +127 -0
  160. package/src/client/components/inputv3/Rte/utils/url.ts +38 -0
  161. package/src/client/components/inputv3/base.tsx +8 -5
  162. package/src/client/components/inputv3/file/index.tsx +11 -5
  163. package/src/common/data/rte/nodes.ts +60 -9
  164. package/src/common/validation/index.ts +21 -2
  165. package/src/common/validation/schema.ts +42 -10
  166. package/src/common/validation/validator.ts +12 -4
  167. package/src/common/validation/validators.ts +82 -53
  168. package/src/server/services/router/http/multipart.ts +0 -1
  169. package/src/server/services/schema/index.ts +24 -2
  170. package/src/server/services/schema/request.ts +3 -2
  171. package/src/server/services/schema/rte.ts +110 -0
  172. package/src/{common/data/rte/index.ts → server/utils/rte.ts} +27 -16
  173. package/src/client/components/inputv3/Rte/ExampleTheme.tsx +0 -42
  174. package/src/client/components/inputv3/Rte/ToolbarPlugin.tsx +0 -167
  175. package/src/client/components/inputv3/Rte/icons/LICENSE.md +0 -5
  176. package/src/client/components/inputv3/Rte/icons/arrow-clockwise.svg +0 -4
  177. package/src/client/components/inputv3/Rte/icons/arrow-counterclockwise.svg +0 -4
  178. package/src/client/components/inputv3/Rte/icons/journal-text.svg +0 -5
  179. package/src/client/components/inputv3/Rte/icons/justify.svg +0 -3
  180. package/src/client/components/inputv3/Rte/icons/text-center.svg +0 -3
  181. package/src/client/components/inputv3/Rte/icons/text-left.svg +0 -3
  182. package/src/client/components/inputv3/Rte/icons/text-paragraph.svg +0 -3
  183. package/src/client/components/inputv3/Rte/icons/text-right.svg +0 -3
  184. package/src/client/components/inputv3/Rte/icons/type-bold.svg +0 -3
  185. package/src/client/components/inputv3/Rte/icons/type-italic.svg +0 -3
  186. package/src/client/components/inputv3/Rte/icons/type-strikethrough.svg +0 -3
  187. package/src/client/components/inputv3/Rte/icons/type-underline.svg +0 -3
@@ -0,0 +1,468 @@
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
+ BaseSelection,
11
+ LexicalCommand,
12
+ LexicalEditor,
13
+ NodeKey,
14
+ } from 'lexical';
15
+
16
+ import './ImageNode.css';
17
+
18
+ import {HashtagNode} from '@lexical/hashtag';
19
+ import {LinkNode} from '@lexical/link';
20
+ import {AutoFocusPlugin} from '@lexical/react/LexicalAutoFocusPlugin';
21
+ import {useCollaborationContext} from '@lexical/react/LexicalCollaborationContext';
22
+ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
23
+ import {LexicalErrorBoundary} from '@lexical/react/LexicalErrorBoundary';
24
+ import {HashtagPlugin} from '@lexical/react/LexicalHashtagPlugin';
25
+ import {HistoryPlugin} from '@lexical/react/LexicalHistoryPlugin';
26
+ import {LexicalNestedComposer} from '@lexical/react/LexicalNestedComposer';
27
+ import {RichTextPlugin} from '@lexical/react/LexicalRichTextPlugin';
28
+ import {useLexicalEditable} from '@lexical/react/useLexicalEditable';
29
+ import {useLexicalNodeSelection} from '@lexical/react/useLexicalNodeSelection';
30
+ import {mergeRegister} from '@lexical/utils';
31
+ import {
32
+ $getNodeByKey,
33
+ $getSelection,
34
+ $isNodeSelection,
35
+ $isRangeSelection,
36
+ $setSelection,
37
+ CLICK_COMMAND,
38
+ COMMAND_PRIORITY_LOW,
39
+ createCommand,
40
+ DRAGSTART_COMMAND,
41
+ KEY_BACKSPACE_COMMAND,
42
+ KEY_DELETE_COMMAND,
43
+ KEY_ENTER_COMMAND,
44
+ KEY_ESCAPE_COMMAND,
45
+ LineBreakNode,
46
+ ParagraphNode,
47
+ RootNode,
48
+ SELECTION_CHANGE_COMMAND,
49
+ TextNode,
50
+ } from 'lexical';
51
+ import * as React from 'react';
52
+ import {Suspense, useCallback, useEffect, useRef, useState} from 'react';
53
+
54
+ import {useSettings} from '../context/SettingsContext';
55
+ import {useSharedHistoryContext} from '../context/SharedHistoryContext';
56
+ import EmojisPlugin from '../plugins/EmojisPlugin';
57
+ import KeywordsPlugin from '../plugins/KeywordsPlugin';
58
+ import LinkPlugin from '../plugins/LinkPlugin';
59
+ import MentionsPlugin from '../plugins/MentionsPlugin';
60
+ import TreeViewPlugin from '../plugins/TreeViewPlugin';
61
+ import ContentEditable from '../ui/ContentEditable';
62
+ import ImageResizer from '../ui/ImageResizer';
63
+ import {EmojiNode} from './EmojiNode';
64
+ import {$isImageNode} from './ImageNode';
65
+ import {KeywordNode} from './KeywordNode';
66
+
67
+ const imageCache = new Set();
68
+
69
+ export const RIGHT_CLICK_IMAGE_COMMAND: LexicalCommand<MouseEvent> =
70
+ createCommand('RIGHT_CLICK_IMAGE_COMMAND');
71
+
72
+ function useSuspenseImage(src: string) {
73
+ if (!imageCache.has(src)) {
74
+ throw new Promise((resolve) => {
75
+ const img = new Image();
76
+ img.src = src;
77
+ img.onload = () => {
78
+ imageCache.add(src);
79
+ resolve(null);
80
+ };
81
+ img.onerror = () => {
82
+ imageCache.add(src);
83
+ };
84
+ });
85
+ }
86
+ }
87
+
88
+ function LazyImage({
89
+ altText,
90
+ className,
91
+ imageRef,
92
+ src,
93
+ width,
94
+ height,
95
+ maxWidth,
96
+ onError,
97
+ }: {
98
+ altText: string;
99
+ className: string | null;
100
+ height: 'inherit' | number;
101
+ imageRef: {current: null | HTMLImageElement};
102
+ maxWidth: number;
103
+ src: string;
104
+ width: 'inherit' | number;
105
+ onError: () => void;
106
+ }): JSX.Element {
107
+ useSuspenseImage(src);
108
+ return (
109
+ <img
110
+ className={className || undefined}
111
+ src={src}
112
+ alt={altText}
113
+ ref={imageRef}
114
+ style={{
115
+ height,
116
+ maxWidth,
117
+ width,
118
+ }}
119
+ onError={onError}
120
+ draggable="false"
121
+ />
122
+ );
123
+ }
124
+
125
+ function BrokenImage(): JSX.Element {
126
+ return (
127
+ <i src="image" class="l" />
128
+ );
129
+ }
130
+
131
+ export default function ImageComponent({
132
+ src,
133
+ altText,
134
+ nodeKey,
135
+ width,
136
+ height,
137
+ maxWidth,
138
+ resizable,
139
+ showCaption,
140
+ caption,
141
+ captionsEnabled,
142
+ }: {
143
+ altText: string;
144
+ caption: LexicalEditor;
145
+ height: 'inherit' | number;
146
+ maxWidth: number;
147
+ nodeKey: NodeKey;
148
+ resizable: boolean;
149
+ showCaption: boolean;
150
+ src: string;
151
+ width: 'inherit' | number;
152
+ captionsEnabled: boolean;
153
+ }): JSX.Element {
154
+ const imageRef = useRef<null | HTMLImageElement>(null);
155
+ const buttonRef = useRef<HTMLButtonElement | null>(null);
156
+ const [isSelected, setSelected, clearSelection] =
157
+ useLexicalNodeSelection(nodeKey);
158
+ const [isResizing, setIsResizing] = useState<boolean>(false);
159
+ const {isCollabActive} = useCollaborationContext();
160
+ const [editor] = useLexicalComposerContext();
161
+ const [selection, setSelection] = useState<BaseSelection | null>(null);
162
+ const activeEditorRef = useRef<LexicalEditor | null>(null);
163
+ const [isLoadError, setIsLoadError] = useState<boolean>(false);
164
+ const isEditable = useLexicalEditable();
165
+
166
+ const $onDelete = useCallback(
167
+ (payload: KeyboardEvent) => {
168
+ const deleteSelection = $getSelection();
169
+ if (isSelected && $isNodeSelection(deleteSelection)) {
170
+ const event: KeyboardEvent = payload;
171
+ event.preventDefault();
172
+ editor.update(() => {
173
+ deleteSelection.getNodes().forEach((node) => {
174
+ if ($isImageNode(node)) {
175
+ node.remove();
176
+ }
177
+ });
178
+ });
179
+ }
180
+ return false;
181
+ },
182
+ [editor, isSelected],
183
+ );
184
+
185
+ const $onEnter = useCallback(
186
+ (event: KeyboardEvent) => {
187
+ const latestSelection = $getSelection();
188
+ const buttonElem = buttonRef.current;
189
+ if (
190
+ isSelected &&
191
+ $isNodeSelection(latestSelection) &&
192
+ latestSelection.getNodes().length === 1
193
+ ) {
194
+ if (showCaption) {
195
+ // Move focus into nested editor
196
+ $setSelection(null);
197
+ event.preventDefault();
198
+ caption.focus();
199
+ return true;
200
+ } else if (
201
+ buttonElem !== null &&
202
+ buttonElem !== document.activeElement
203
+ ) {
204
+ event.preventDefault();
205
+ buttonElem.focus();
206
+ return true;
207
+ }
208
+ }
209
+ return false;
210
+ },
211
+ [caption, isSelected, showCaption],
212
+ );
213
+
214
+ const $onEscape = useCallback(
215
+ (event: KeyboardEvent) => {
216
+ if (
217
+ activeEditorRef.current === caption ||
218
+ buttonRef.current === event.target
219
+ ) {
220
+ $setSelection(null);
221
+ editor.update(() => {
222
+ setSelected(true);
223
+ const parentRootElement = editor.getRootElement();
224
+ if (parentRootElement !== null) {
225
+ parentRootElement.focus();
226
+ }
227
+ });
228
+ return true;
229
+ }
230
+ return false;
231
+ },
232
+ [caption, editor, setSelected],
233
+ );
234
+
235
+ const onClick = useCallback(
236
+ (payload: MouseEvent) => {
237
+ const event = payload;
238
+
239
+ if (isResizing) {
240
+ return true;
241
+ }
242
+ if (event.target === imageRef.current) {
243
+ if (event.shiftKey) {
244
+ setSelected(!isSelected);
245
+ } else {
246
+ clearSelection();
247
+ setSelected(true);
248
+ }
249
+ return true;
250
+ }
251
+
252
+ return false;
253
+ },
254
+ [isResizing, isSelected, setSelected, clearSelection],
255
+ );
256
+
257
+ const onRightClick = useCallback(
258
+ (event: MouseEvent): void => {
259
+ editor.getEditorState().read(() => {
260
+ const latestSelection = $getSelection();
261
+ const domElement = event.target as HTMLElement;
262
+ if (
263
+ domElement.tagName === 'IMG' &&
264
+ $isRangeSelection(latestSelection) &&
265
+ latestSelection.getNodes().length === 1
266
+ ) {
267
+ editor.dispatchCommand(
268
+ RIGHT_CLICK_IMAGE_COMMAND,
269
+ event as MouseEvent,
270
+ );
271
+ }
272
+ });
273
+ },
274
+ [editor],
275
+ );
276
+
277
+ useEffect(() => {
278
+ let isMounted = true;
279
+ const rootElement = editor.getRootElement();
280
+ const unregister = mergeRegister(
281
+ editor.registerUpdateListener(({editorState}) => {
282
+ if (isMounted) {
283
+ setSelection(editorState.read(() => $getSelection()));
284
+ }
285
+ }),
286
+ editor.registerCommand(
287
+ SELECTION_CHANGE_COMMAND,
288
+ (_, activeEditor) => {
289
+ activeEditorRef.current = activeEditor;
290
+ return false;
291
+ },
292
+ COMMAND_PRIORITY_LOW,
293
+ ),
294
+ editor.registerCommand<MouseEvent>(
295
+ CLICK_COMMAND,
296
+ onClick,
297
+ COMMAND_PRIORITY_LOW,
298
+ ),
299
+ editor.registerCommand<MouseEvent>(
300
+ RIGHT_CLICK_IMAGE_COMMAND,
301
+ onClick,
302
+ COMMAND_PRIORITY_LOW,
303
+ ),
304
+ editor.registerCommand(
305
+ DRAGSTART_COMMAND,
306
+ (event) => {
307
+ if (event.target === imageRef.current) {
308
+ // TODO This is just a temporary workaround for FF to behave like other browsers.
309
+ // Ideally, this handles drag & drop too (and all browsers).
310
+ event.preventDefault();
311
+ return true;
312
+ }
313
+ return false;
314
+ },
315
+ COMMAND_PRIORITY_LOW,
316
+ ),
317
+ editor.registerCommand(
318
+ KEY_DELETE_COMMAND,
319
+ $onDelete,
320
+ COMMAND_PRIORITY_LOW,
321
+ ),
322
+ editor.registerCommand(
323
+ KEY_BACKSPACE_COMMAND,
324
+ $onDelete,
325
+ COMMAND_PRIORITY_LOW,
326
+ ),
327
+ editor.registerCommand(KEY_ENTER_COMMAND, $onEnter, COMMAND_PRIORITY_LOW),
328
+ editor.registerCommand(
329
+ KEY_ESCAPE_COMMAND,
330
+ $onEscape,
331
+ COMMAND_PRIORITY_LOW,
332
+ ),
333
+ );
334
+
335
+ rootElement?.addEventListener('contextmenu', onRightClick);
336
+
337
+ return () => {
338
+ isMounted = false;
339
+ unregister();
340
+ rootElement?.removeEventListener('contextmenu', onRightClick);
341
+ };
342
+ }, [
343
+ clearSelection,
344
+ editor,
345
+ isResizing,
346
+ isSelected,
347
+ nodeKey,
348
+ $onDelete,
349
+ $onEnter,
350
+ $onEscape,
351
+ onClick,
352
+ onRightClick,
353
+ setSelected,
354
+ ]);
355
+
356
+ const setShowCaption = () => {
357
+ editor.update(() => {
358
+ const node = $getNodeByKey(nodeKey);
359
+ if ($isImageNode(node)) {
360
+ node.setShowCaption(true);
361
+ }
362
+ });
363
+ };
364
+
365
+ const onResizeEnd = (
366
+ nextWidth: 'inherit' | number,
367
+ nextHeight: 'inherit' | number,
368
+ ) => {
369
+ // Delay hiding the resize bars for click case
370
+ setTimeout(() => {
371
+ setIsResizing(false);
372
+ }, 200);
373
+
374
+ editor.update(() => {
375
+ const node = $getNodeByKey(nodeKey);
376
+ if ($isImageNode(node)) {
377
+ node.setWidthAndHeight(nextWidth, nextHeight);
378
+ }
379
+ });
380
+ };
381
+
382
+ const onResizeStart = () => {
383
+ setIsResizing(true);
384
+ };
385
+
386
+ const {historyState} = useSharedHistoryContext();
387
+ const {
388
+ settings: {showNestedEditorTreeView},
389
+ } = useSettings();
390
+
391
+ const draggable = isSelected && $isNodeSelection(selection) && !isResizing;
392
+ const isFocused = (isSelected || isResizing) && isEditable;
393
+ return (
394
+ <Suspense fallback={null}>
395
+ <>
396
+ <div draggable={draggable}>
397
+ {isLoadError ? (
398
+ <BrokenImage />
399
+ ) : (
400
+ <LazyImage
401
+ className={
402
+ isFocused
403
+ ? `focused ${$isNodeSelection(selection) ? 'draggable' : ''}`
404
+ : null
405
+ }
406
+ src={src}
407
+ altText={altText}
408
+ imageRef={imageRef}
409
+ width={width}
410
+ height={height}
411
+ maxWidth={maxWidth}
412
+ onError={() => setIsLoadError(true)}
413
+ />
414
+ )}
415
+ </div>
416
+
417
+ {showCaption && (
418
+ <div className="image-caption-container">
419
+ <LexicalNestedComposer
420
+ initialEditor={caption}
421
+ initialNodes={[
422
+ RootNode,
423
+ TextNode,
424
+ LineBreakNode,
425
+ ParagraphNode,
426
+ LinkNode,
427
+ EmojiNode,
428
+ HashtagNode,
429
+ KeywordNode,
430
+ ]}>
431
+ <AutoFocusPlugin />
432
+ <MentionsPlugin />
433
+ <LinkPlugin />
434
+ <EmojisPlugin />
435
+ <HashtagPlugin />
436
+ <KeywordsPlugin />
437
+ <HistoryPlugin externalHistoryState={historyState} />
438
+ <RichTextPlugin
439
+ contentEditable={
440
+ <ContentEditable
441
+ placeholder="Enter a caption..."
442
+ placeholderClassName="ImageNode__placeholder"
443
+ className="ImageNode__contentEditable"
444
+ />
445
+ }
446
+ ErrorBoundary={LexicalErrorBoundary}
447
+ />
448
+ {showNestedEditorTreeView === true ? <TreeViewPlugin /> : null}
449
+ </LexicalNestedComposer>
450
+ </div>
451
+ )}
452
+ {resizable && $isNodeSelection(selection) && isFocused && (
453
+ <ImageResizer
454
+ showCaption={showCaption}
455
+ setShowCaption={setShowCaption}
456
+ editor={editor}
457
+ buttonRef={buttonRef}
458
+ imageRef={imageRef}
459
+ maxWidth={maxWidth}
460
+ onResizeStart={onResizeStart}
461
+ onResizeEnd={onResizeEnd}
462
+ captionsEnabled={!isLoadError && captionsEnabled}
463
+ />
464
+ )}
465
+ </>
466
+ </Suspense>
467
+ );
468
+ }
@@ -0,0 +1,43 @@
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
+
10
+ .ImageNode__contentEditable {
11
+ min-height: 20px;
12
+ border: 0px;
13
+ resize: none;
14
+ cursor: text;
15
+ caret-color: rgb(5, 5, 5);
16
+ display: block;
17
+ position: relative;
18
+ outline: 0px;
19
+ padding: 10px;
20
+ user-select: text;
21
+ font-size: 12px;
22
+ width: calc(100% - 20px);
23
+ white-space: pre-wrap;
24
+ word-break: break-word;
25
+ }
26
+
27
+ .ImageNode__placeholder {
28
+ font-size: 12px;
29
+ color: #888;
30
+ overflow: hidden;
31
+ position: absolute;
32
+ text-overflow: ellipsis;
33
+ top: 10px;
34
+ left: 10px;
35
+ user-select: none;
36
+ white-space: nowrap;
37
+ display: inline-block;
38
+ pointer-events: none;
39
+ }
40
+
41
+ .image-control-wrapper--resizing {
42
+ touch-action: none;
43
+ }