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,12 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ .TableCellResizer__resizer {
11
+ position: absolute;
12
+ }
@@ -0,0 +1,436 @@
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
+ import type {TableCellNode, TableDOMCell, TableMapType} from '@lexical/table';
9
+ import type {LexicalEditor} from 'lexical';
10
+
11
+ import './index.css';
12
+
13
+ import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
14
+ import {useLexicalEditable} from '@lexical/react/useLexicalEditable';
15
+ import {
16
+ $computeTableMapSkipCellCheck,
17
+ $getTableNodeFromLexicalNodeOrThrow,
18
+ $getTableRowIndexFromTableCellNode,
19
+ $isTableCellNode,
20
+ $isTableRowNode,
21
+ getDOMCellFromTarget,
22
+ TableNode,
23
+ } from '@lexical/table';
24
+ import {calculateZoomLevel} from '@lexical/utils';
25
+ import {$getNearestNodeFromDOMNode} from 'lexical';
26
+ import * as React from 'react';
27
+ import {
28
+ MouseEventHandler,
29
+ ReactPortal,
30
+ useCallback,
31
+ useEffect,
32
+ useMemo,
33
+ useRef,
34
+ useState,
35
+ } from 'react';
36
+ import {createPortal} from 'react-dom';
37
+
38
+ type MousePosition = {
39
+ x: number;
40
+ y: number;
41
+ };
42
+
43
+ type MouseDraggingDirection = 'right' | 'bottom';
44
+
45
+ const MIN_ROW_HEIGHT = 33;
46
+ const MIN_COLUMN_WIDTH = 92;
47
+
48
+ function TableCellResizer({editor}: {editor: LexicalEditor}): JSX.Element {
49
+ const targetRef = useRef<HTMLElement | null>(null);
50
+ const resizerRef = useRef<HTMLDivElement | null>(null);
51
+ const tableRectRef = useRef<ClientRect | null>(null);
52
+
53
+ const mouseStartPosRef = useRef<MousePosition | null>(null);
54
+ const [mouseCurrentPos, updateMouseCurrentPos] =
55
+ useState<MousePosition | null>(null);
56
+
57
+ const [activeCell, updateActiveCell] = useState<TableDOMCell | null>(null);
58
+ const [isMouseDown, updateIsMouseDown] = useState<boolean>(false);
59
+ const [draggingDirection, updateDraggingDirection] =
60
+ useState<MouseDraggingDirection | null>(null);
61
+
62
+ const resetState = useCallback(() => {
63
+ updateActiveCell(null);
64
+ targetRef.current = null;
65
+ updateDraggingDirection(null);
66
+ mouseStartPosRef.current = null;
67
+ tableRectRef.current = null;
68
+ }, []);
69
+
70
+ const isMouseDownOnEvent = (event: MouseEvent) => {
71
+ return (event.buttons & 1) === 1;
72
+ };
73
+
74
+ useEffect(() => {
75
+ return editor.registerNodeTransform(TableNode, (tableNode) => {
76
+ if (tableNode.getColWidths()) {
77
+ return tableNode;
78
+ }
79
+
80
+ const numColumns = tableNode.getColumnCount();
81
+ const columnWidth = MIN_COLUMN_WIDTH;
82
+
83
+ tableNode.setColWidths(Array(numColumns).fill(columnWidth));
84
+ return tableNode;
85
+ });
86
+ }, [editor]);
87
+
88
+ useEffect(() => {
89
+ const onMouseMove = (event: MouseEvent) => {
90
+ setTimeout(() => {
91
+ const target = event.target;
92
+
93
+ if (draggingDirection) {
94
+ updateMouseCurrentPos({
95
+ x: event.clientX,
96
+ y: event.clientY,
97
+ });
98
+ return;
99
+ }
100
+ updateIsMouseDown(isMouseDownOnEvent(event));
101
+ if (resizerRef.current && resizerRef.current.contains(target as Node)) {
102
+ return;
103
+ }
104
+
105
+ if (targetRef.current !== target) {
106
+ targetRef.current = target as HTMLElement;
107
+ const cell = getDOMCellFromTarget(target as HTMLElement);
108
+
109
+ if (cell && activeCell !== cell) {
110
+ editor.update(() => {
111
+ const tableCellNode = $getNearestNodeFromDOMNode(cell.elem);
112
+ if (!tableCellNode) {
113
+ throw new Error('TableCellResizer: Table cell node not found.');
114
+ }
115
+
116
+ const tableNode =
117
+ $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
118
+ const tableElement = editor.getElementByKey(tableNode.getKey());
119
+
120
+ if (!tableElement) {
121
+ throw new Error('TableCellResizer: Table element not found.');
122
+ }
123
+
124
+ targetRef.current = target as HTMLElement;
125
+ tableRectRef.current = tableElement.getBoundingClientRect();
126
+ updateActiveCell(cell);
127
+ });
128
+ } else if (cell == null) {
129
+ resetState();
130
+ }
131
+ }
132
+ }, 0);
133
+ };
134
+
135
+ const onMouseDown = (event: MouseEvent) => {
136
+ setTimeout(() => {
137
+ updateIsMouseDown(true);
138
+ }, 0);
139
+ };
140
+
141
+ const onMouseUp = (event: MouseEvent) => {
142
+ setTimeout(() => {
143
+ updateIsMouseDown(false);
144
+ }, 0);
145
+ };
146
+
147
+ const removeRootListener = editor.registerRootListener(
148
+ (rootElement, prevRootElement) => {
149
+ prevRootElement?.removeEventListener('mousemove', onMouseMove);
150
+ prevRootElement?.removeEventListener('mousedown', onMouseDown);
151
+ prevRootElement?.removeEventListener('mouseup', onMouseUp);
152
+ rootElement?.addEventListener('mousemove', onMouseMove);
153
+ rootElement?.addEventListener('mousedown', onMouseDown);
154
+ rootElement?.addEventListener('mouseup', onMouseUp);
155
+ },
156
+ );
157
+
158
+ return () => {
159
+ removeRootListener();
160
+ };
161
+ }, [activeCell, draggingDirection, editor, resetState]);
162
+
163
+ const isHeightChanging = (direction: MouseDraggingDirection) => {
164
+ if (direction === 'bottom') {
165
+ return true;
166
+ }
167
+ return false;
168
+ };
169
+
170
+ const updateRowHeight = useCallback(
171
+ (heightChange: number) => {
172
+ if (!activeCell) {
173
+ throw new Error('TableCellResizer: Expected active cell.');
174
+ }
175
+
176
+ editor.update(
177
+ () => {
178
+ const tableCellNode = $getNearestNodeFromDOMNode(activeCell.elem);
179
+ if (!$isTableCellNode(tableCellNode)) {
180
+ throw new Error('TableCellResizer: Table cell node not found.');
181
+ }
182
+
183
+ const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
184
+
185
+ const tableRowIndex =
186
+ $getTableRowIndexFromTableCellNode(tableCellNode) +
187
+ tableCellNode.getRowSpan() -
188
+ 1;
189
+
190
+ const tableRows = tableNode.getChildren();
191
+
192
+ if (tableRowIndex >= tableRows.length || tableRowIndex < 0) {
193
+ throw new Error('Expected table cell to be inside of table row.');
194
+ }
195
+
196
+ const tableRow = tableRows[tableRowIndex];
197
+
198
+ if (!$isTableRowNode(tableRow)) {
199
+ throw new Error('Expected table row');
200
+ }
201
+
202
+ let height = tableRow.getHeight();
203
+ if (height === undefined) {
204
+ const rowCells = tableRow.getChildren<TableCellNode>();
205
+ height = Math.min(
206
+ ...rowCells.map(
207
+ (cell) => getCellNodeHeight(cell, editor) ?? Infinity,
208
+ ),
209
+ );
210
+ }
211
+
212
+ const newHeight = Math.max(height + heightChange, MIN_ROW_HEIGHT);
213
+ tableRow.setHeight(newHeight);
214
+ },
215
+ {tag: 'skip-scroll-into-view'},
216
+ );
217
+ },
218
+ [activeCell, editor],
219
+ );
220
+
221
+ const getCellNodeHeight = (
222
+ cell: TableCellNode,
223
+ activeEditor: LexicalEditor,
224
+ ): number | undefined => {
225
+ const domCellNode = activeEditor.getElementByKey(cell.getKey());
226
+ return domCellNode?.clientHeight;
227
+ };
228
+
229
+ const getCellColumnIndex = (
230
+ tableCellNode: TableCellNode,
231
+ tableMap: TableMapType,
232
+ ) => {
233
+ for (let row = 0; row < tableMap.length; row++) {
234
+ for (let column = 0; column < tableMap[row].length; column++) {
235
+ if (tableMap[row][column].cell === tableCellNode) {
236
+ return column;
237
+ }
238
+ }
239
+ }
240
+ };
241
+
242
+ const updateColumnWidth = useCallback(
243
+ (widthChange: number) => {
244
+ if (!activeCell) {
245
+ throw new Error('TableCellResizer: Expected active cell.');
246
+ }
247
+ editor.update(
248
+ () => {
249
+ const tableCellNode = $getNearestNodeFromDOMNode(activeCell.elem);
250
+ if (!$isTableCellNode(tableCellNode)) {
251
+ throw new Error('TableCellResizer: Table cell node not found.');
252
+ }
253
+
254
+ const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
255
+ const [tableMap] = $computeTableMapSkipCellCheck(
256
+ tableNode,
257
+ null,
258
+ null,
259
+ );
260
+ const columnIndex = getCellColumnIndex(tableCellNode, tableMap);
261
+ if (columnIndex === undefined) {
262
+ throw new Error('TableCellResizer: Table column not found.');
263
+ }
264
+
265
+ const colWidths = tableNode.getColWidths();
266
+ if (!colWidths) {
267
+ return;
268
+ }
269
+ const width = colWidths[columnIndex];
270
+ if (width === undefined) {
271
+ return;
272
+ }
273
+ const newColWidths = [...colWidths];
274
+ const newWidth = Math.max(width + widthChange, MIN_COLUMN_WIDTH);
275
+ newColWidths[columnIndex] = newWidth;
276
+ tableNode.setColWidths(newColWidths);
277
+ },
278
+ {tag: 'skip-scroll-into-view'},
279
+ );
280
+ },
281
+ [activeCell, editor],
282
+ );
283
+
284
+ const mouseUpHandler = useCallback(
285
+ (direction: MouseDraggingDirection) => {
286
+ const handler = (event: MouseEvent) => {
287
+ event.preventDefault();
288
+ event.stopPropagation();
289
+
290
+ if (!activeCell) {
291
+ throw new Error('TableCellResizer: Expected active cell.');
292
+ }
293
+
294
+ if (mouseStartPosRef.current) {
295
+ const {x, y} = mouseStartPosRef.current;
296
+
297
+ if (activeCell === null) {
298
+ return;
299
+ }
300
+ const zoom = calculateZoomLevel(event.target as Element);
301
+
302
+ if (isHeightChanging(direction)) {
303
+ const heightChange = (event.clientY - y) / zoom;
304
+ updateRowHeight(heightChange);
305
+ } else {
306
+ const widthChange = (event.clientX - x) / zoom;
307
+ updateColumnWidth(widthChange);
308
+ }
309
+
310
+ resetState();
311
+ document.removeEventListener('mouseup', handler);
312
+ }
313
+ };
314
+ return handler;
315
+ },
316
+ [activeCell, resetState, updateColumnWidth, updateRowHeight],
317
+ );
318
+
319
+ const toggleResize = useCallback(
320
+ (direction: MouseDraggingDirection): MouseEventHandler<HTMLDivElement> =>
321
+ (event) => {
322
+ event.preventDefault();
323
+ event.stopPropagation();
324
+
325
+ if (!activeCell) {
326
+ throw new Error('TableCellResizer: Expected active cell.');
327
+ }
328
+
329
+ mouseStartPosRef.current = {
330
+ x: event.clientX,
331
+ y: event.clientY,
332
+ };
333
+ updateMouseCurrentPos(mouseStartPosRef.current);
334
+ updateDraggingDirection(direction);
335
+
336
+ document.addEventListener('mouseup', mouseUpHandler(direction));
337
+ },
338
+ [activeCell, mouseUpHandler],
339
+ );
340
+
341
+ const getResizers = useCallback(() => {
342
+ if (activeCell) {
343
+ const {height, width, top, left} =
344
+ activeCell.elem.getBoundingClientRect();
345
+ const zoom = calculateZoomLevel(activeCell.elem);
346
+ const zoneWidth = 10; // Pixel width of the zone where you can drag the edge
347
+ const styles = {
348
+ bottom: {
349
+ backgroundColor: 'none',
350
+ cursor: 'row-resize',
351
+ height: `${zoneWidth}px`,
352
+ left: `${window.pageXOffset + left}px`,
353
+ top: `${window.pageYOffset + top + height - zoneWidth / 2}px`,
354
+ width: `${width}px`,
355
+ },
356
+ right: {
357
+ backgroundColor: 'none',
358
+ cursor: 'col-resize',
359
+ height: `${height}px`,
360
+ left: `${window.pageXOffset + left + width - zoneWidth / 2}px`,
361
+ top: `${window.pageYOffset + top}px`,
362
+ width: `${zoneWidth}px`,
363
+ },
364
+ };
365
+
366
+ const tableRect = tableRectRef.current;
367
+
368
+ if (draggingDirection && mouseCurrentPos && tableRect) {
369
+ if (isHeightChanging(draggingDirection)) {
370
+ styles[draggingDirection].left = `${
371
+ window.pageXOffset + tableRect.left
372
+ }px`;
373
+ styles[draggingDirection].top = `${
374
+ window.pageYOffset + mouseCurrentPos.y / zoom
375
+ }px`;
376
+ styles[draggingDirection].height = '3px';
377
+ styles[draggingDirection].width = `${tableRect.width}px`;
378
+ } else {
379
+ styles[draggingDirection].top = `${
380
+ window.pageYOffset + tableRect.top
381
+ }px`;
382
+ styles[draggingDirection].left = `${
383
+ window.pageXOffset + mouseCurrentPos.x / zoom
384
+ }px`;
385
+ styles[draggingDirection].width = '3px';
386
+ styles[draggingDirection].height = `${tableRect.height}px`;
387
+ }
388
+
389
+ styles[draggingDirection].backgroundColor = '#adf';
390
+ }
391
+
392
+ return styles;
393
+ }
394
+
395
+ return {
396
+ bottom: null,
397
+ left: null,
398
+ right: null,
399
+ top: null,
400
+ };
401
+ }, [activeCell, draggingDirection, mouseCurrentPos]);
402
+
403
+ const resizerStyles = getResizers();
404
+
405
+ return (
406
+ <div ref={resizerRef}>
407
+ {activeCell != null && !isMouseDown && (
408
+ <>
409
+ <div
410
+ className="TableCellResizer__resizer TableCellResizer__ui"
411
+ style={resizerStyles.right || undefined}
412
+ onMouseDown={toggleResize('right')}
413
+ />
414
+ <div
415
+ className="TableCellResizer__resizer TableCellResizer__ui"
416
+ style={resizerStyles.bottom || undefined}
417
+ onMouseDown={toggleResize('bottom')}
418
+ />
419
+ </>
420
+ )}
421
+ </div>
422
+ );
423
+ }
424
+
425
+ export default function TableCellResizerPlugin(): null | ReactPortal {
426
+ const [editor] = useLexicalComposerContext();
427
+ const isEditable = useLexicalEditable();
428
+
429
+ return useMemo(
430
+ () =>
431
+ isEditable
432
+ ? createPortal(<TableCellResizer editor={editor} />, document.body)
433
+ : null,
434
+ [editor, isEditable],
435
+ );
436
+ }