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,773 @@
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 { ElementNode, LexicalEditor } from 'lexical';
10
+
11
+ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
12
+ import { useLexicalEditable } from '@lexical/react/useLexicalEditable';
13
+ import {
14
+ $deleteTableColumn__EXPERIMENTAL,
15
+ $deleteTableRow__EXPERIMENTAL,
16
+ $getNodeTriplet,
17
+ $getTableCellNodeFromLexicalNode,
18
+ $getTableColumnIndexFromTableCellNode,
19
+ $getTableNodeFromLexicalNodeOrThrow,
20
+ $getTableRowIndexFromTableCellNode,
21
+ $insertTableColumn__EXPERIMENTAL,
22
+ $insertTableRow__EXPERIMENTAL,
23
+ $isTableCellNode,
24
+ $isTableRowNode,
25
+ $isTableSelection,
26
+ $unmergeCell,
27
+ getTableObserverFromTableElement,
28
+ HTMLTableElementWithWithTableSelectionState,
29
+ TableCellHeaderStates,
30
+ TableCellNode,
31
+ TableRowNode,
32
+ TableSelection,
33
+ } from '@lexical/table';
34
+ import {
35
+ $createParagraphNode,
36
+ $getRoot,
37
+ $getSelection,
38
+ $isElementNode,
39
+ $isParagraphNode,
40
+ $isRangeSelection,
41
+ $isTextNode,
42
+ } from 'lexical';
43
+ import * as React from 'react';
44
+ import { ReactPortal, useCallback, useEffect, useRef, useState } from 'react';
45
+ import { createPortal } from 'react-dom';
46
+
47
+ // Core
48
+ import useContext from '@/client/context';
49
+
50
+ import ColorPicker from '../../ui/ColorPicker';
51
+
52
+ function computeSelectionCount(selection: TableSelection): {
53
+ columns: number;
54
+ rows: number;
55
+ } {
56
+ const selectionShape = selection.getShape();
57
+ return {
58
+ columns: selectionShape.toX - selectionShape.fromX + 1,
59
+ rows: selectionShape.toY - selectionShape.fromY + 1,
60
+ };
61
+ }
62
+
63
+ function $canUnmerge(): boolean {
64
+ const selection = $getSelection();
65
+ if (
66
+ ($isRangeSelection(selection) && !selection.isCollapsed()) ||
67
+ ($isTableSelection(selection) && !selection.anchor.is(selection.focus)) ||
68
+ (!$isRangeSelection(selection) && !$isTableSelection(selection))
69
+ ) {
70
+ return false;
71
+ }
72
+ const [cell] = $getNodeTriplet(selection.anchor);
73
+ return cell.__colSpan > 1 || cell.__rowSpan > 1;
74
+ }
75
+
76
+ function $cellContainsEmptyParagraph(cell: TableCellNode): boolean {
77
+ if (cell.getChildrenSize() !== 1) {
78
+ return false;
79
+ }
80
+ const firstChild = cell.getFirstChildOrThrow();
81
+ if (!$isParagraphNode(firstChild) || !firstChild.isEmpty()) {
82
+ return false;
83
+ }
84
+ return true;
85
+ }
86
+
87
+ function $selectLastDescendant(node: ElementNode): void {
88
+ const lastDescendant = node.getLastDescendant();
89
+ if ($isTextNode(lastDescendant)) {
90
+ lastDescendant.select();
91
+ } else if ($isElementNode(lastDescendant)) {
92
+ lastDescendant.selectEnd();
93
+ } else if (lastDescendant !== null) {
94
+ lastDescendant.selectNext();
95
+ }
96
+ }
97
+
98
+ function currentCellBackgroundColor(editor: LexicalEditor): null | string {
99
+ return editor.getEditorState().read(() => {
100
+ const selection = $getSelection();
101
+ if ($isRangeSelection(selection) || $isTableSelection(selection)) {
102
+ const [cell] = $getNodeTriplet(selection.anchor);
103
+ if ($isTableCellNode(cell)) {
104
+ return cell.getBackgroundColor();
105
+ }
106
+ }
107
+ return null;
108
+ });
109
+ }
110
+
111
+ type TableCellActionMenuProps = Readonly<{
112
+ contextRef: { current: null | HTMLElement };
113
+ close: () => void;
114
+ setIsMenuOpen: (isOpen: boolean) => void;
115
+ tableCellNode: TableCellNode;
116
+ cellMerge: boolean;
117
+ }>;
118
+
119
+ function TableActionMenu({
120
+ close,
121
+ tableCellNode: _tableCellNode,
122
+ setIsMenuOpen,
123
+ contextRef,
124
+ cellMerge,
125
+ }: TableCellActionMenuProps) {
126
+
127
+ const { modal } = useContext();
128
+
129
+ const [editor] = useLexicalComposerContext();
130
+ const dropDownRef = useRef<HTMLDivElement | null>(null);
131
+ const [tableCellNode, updateTableCellNode] = useState(_tableCellNode);
132
+ const [selectionCounts, updateSelectionCounts] = useState({
133
+ columns: 1,
134
+ rows: 1,
135
+ });
136
+ const [canMergeCells, setCanMergeCells] = useState(false);
137
+ const [canUnmergeCell, setCanUnmergeCell] = useState(false);
138
+ const [backgroundColor, setBackgroundColor] = useState(
139
+ () => currentCellBackgroundColor(editor) || '',
140
+ );
141
+
142
+ useEffect(() => {
143
+ return editor.registerMutationListener(
144
+ TableCellNode,
145
+ (nodeMutations) => {
146
+ const nodeUpdated =
147
+ nodeMutations.get(tableCellNode.getKey()) === 'updated';
148
+
149
+ if (nodeUpdated) {
150
+ editor.getEditorState().read(() => {
151
+ updateTableCellNode(tableCellNode.getLatest());
152
+ });
153
+ setBackgroundColor(currentCellBackgroundColor(editor) || '');
154
+ }
155
+ },
156
+ { skipInitialization: true },
157
+ );
158
+ }, [editor, tableCellNode]);
159
+
160
+ useEffect(() => {
161
+ editor.getEditorState().read(() => {
162
+ const selection = $getSelection();
163
+ // Merge cells
164
+ if ($isTableSelection(selection)) {
165
+ const currentSelectionCounts = computeSelectionCount(selection);
166
+ updateSelectionCounts(computeSelectionCount(selection));
167
+ setCanMergeCells(
168
+ currentSelectionCounts.columns > 1 || currentSelectionCounts.rows > 1,
169
+ );
170
+ }
171
+ // Unmerge cell
172
+ setCanUnmergeCell($canUnmerge());
173
+ });
174
+ }, [editor]);
175
+
176
+ useEffect(() => {
177
+ const menuButtonElement = contextRef.current;
178
+ const dropDownElement = dropDownRef.current;
179
+ const rootElement = editor.getRootElement();
180
+
181
+ if (
182
+ menuButtonElement != null &&
183
+ dropDownElement != null &&
184
+ rootElement != null
185
+ ) {
186
+ const rootEleRect = rootElement.getBoundingClientRect();
187
+ const menuButtonRect = menuButtonElement.getBoundingClientRect();
188
+ dropDownElement.style.opacity = '1';
189
+ const dropDownElementRect = dropDownElement.getBoundingClientRect();
190
+ const margin = 5;
191
+ let leftPosition = menuButtonRect.right + margin;
192
+ if (
193
+ leftPosition + dropDownElementRect.width > window.innerWidth ||
194
+ leftPosition + dropDownElementRect.width > rootEleRect.right
195
+ ) {
196
+ const position =
197
+ menuButtonRect.left - dropDownElementRect.width - margin;
198
+ leftPosition = (position < 0 ? margin : position) + window.pageXOffset;
199
+ }
200
+ dropDownElement.style.left = `${leftPosition + window.pageXOffset}px`;
201
+
202
+ let topPosition = menuButtonRect.top;
203
+ if (topPosition + dropDownElementRect.height > window.innerHeight) {
204
+ const position = menuButtonRect.bottom - dropDownElementRect.height;
205
+ topPosition = (position < 0 ? margin : position) + window.pageYOffset;
206
+ }
207
+ dropDownElement.style.top = `${topPosition + +window.pageYOffset}px`;
208
+ }
209
+ }, [contextRef, dropDownRef, editor]);
210
+
211
+ useEffect(() => {
212
+ function handleClickOutside(event: MouseEvent) {
213
+ if (
214
+ dropDownRef.current != null &&
215
+ contextRef.current != null &&
216
+ !dropDownRef.current.contains(event.target as Node) &&
217
+ !contextRef.current.contains(event.target as Node)
218
+ ) {
219
+ setIsMenuOpen(false);
220
+ }
221
+ }
222
+
223
+ window.addEventListener('click', handleClickOutside);
224
+
225
+ return () => window.removeEventListener('click', handleClickOutside);
226
+ }, [setIsMenuOpen, contextRef]);
227
+
228
+ const clearTableSelection = useCallback(() => {
229
+ editor.update(() => {
230
+ if (tableCellNode.isAttached()) {
231
+ const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
232
+ const tableElement = editor.getElementByKey(
233
+ tableNode.getKey(),
234
+ ) as HTMLTableElementWithWithTableSelectionState;
235
+
236
+ if (!tableElement) {
237
+ throw new Error('Expected to find tableElement in DOM');
238
+ }
239
+
240
+ const tableObserver = getTableObserverFromTableElement(tableElement);
241
+ if (tableObserver !== null) {
242
+ tableObserver.clearHighlight();
243
+ }
244
+
245
+ tableNode.markDirty();
246
+ updateTableCellNode(tableCellNode.getLatest());
247
+ }
248
+
249
+ const rootNode = $getRoot();
250
+ rootNode.selectStart();
251
+ });
252
+ }, [editor, tableCellNode]);
253
+
254
+ const mergeTableCellsAtSelection = () => {
255
+ editor.update(() => {
256
+ const selection = $getSelection();
257
+ if ($isTableSelection(selection)) {
258
+ const { columns, rows } = computeSelectionCount(selection);
259
+ const nodes = selection.getNodes();
260
+ let firstCell: null | TableCellNode = null;
261
+ for (let i = 0; i < nodes.length; i++) {
262
+ const node = nodes[i];
263
+ if ($isTableCellNode(node)) {
264
+ if (firstCell === null) {
265
+ node.setColSpan(columns).setRowSpan(rows);
266
+ firstCell = node;
267
+ const isEmpty = $cellContainsEmptyParagraph(node);
268
+ let firstChild;
269
+ if (
270
+ isEmpty &&
271
+ $isParagraphNode((firstChild = node.getFirstChild()))
272
+ ) {
273
+ firstChild.remove();
274
+ }
275
+ } else if ($isTableCellNode(firstCell)) {
276
+ const isEmpty = $cellContainsEmptyParagraph(node);
277
+ if (!isEmpty) {
278
+ firstCell.append(...node.getChildren());
279
+ }
280
+ node.remove();
281
+ }
282
+ }
283
+ }
284
+ if (firstCell !== null) {
285
+ if (firstCell.getChildrenSize() === 0) {
286
+ firstCell.append($createParagraphNode());
287
+ }
288
+ $selectLastDescendant(firstCell);
289
+ }
290
+ close();
291
+ }
292
+ });
293
+ };
294
+
295
+ const unmergeTableCellsAtSelection = () => {
296
+ editor.update(() => {
297
+ $unmergeCell();
298
+ });
299
+ };
300
+
301
+ const insertTableRowAtSelection = useCallback(
302
+ (shouldInsertAfter: boolean) => {
303
+ editor.update(() => {
304
+ $insertTableRow__EXPERIMENTAL(shouldInsertAfter);
305
+ close();
306
+ });
307
+ },
308
+ [editor, close],
309
+ );
310
+
311
+ const insertTableColumnAtSelection = useCallback(
312
+ (shouldInsertAfter: boolean) => {
313
+ editor.update(() => {
314
+ for (let i = 0; i < selectionCounts.columns; i++) {
315
+ $insertTableColumn__EXPERIMENTAL(shouldInsertAfter);
316
+ }
317
+ close();
318
+ });
319
+ },
320
+ [editor, close, selectionCounts.columns],
321
+ );
322
+
323
+ const deleteTableRowAtSelection = useCallback(() => {
324
+ editor.update(() => {
325
+ $deleteTableRow__EXPERIMENTAL();
326
+ close();
327
+ });
328
+ }, [editor, close]);
329
+
330
+ const deleteTableAtSelection = useCallback(() => {
331
+ editor.update(() => {
332
+ const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
333
+ tableNode.remove();
334
+
335
+ clearTableSelection();
336
+ close();
337
+ });
338
+ }, [editor, tableCellNode, clearTableSelection, close]);
339
+
340
+ const deleteTableColumnAtSelection = useCallback(() => {
341
+ editor.update(() => {
342
+ $deleteTableColumn__EXPERIMENTAL();
343
+ close();
344
+ });
345
+ }, [editor, close]);
346
+
347
+ const toggleTableRowIsHeader = useCallback(() => {
348
+ editor.update(() => {
349
+ const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
350
+
351
+ const tableRowIndex = $getTableRowIndexFromTableCellNode(tableCellNode);
352
+
353
+ const tableRows = tableNode.getChildren();
354
+
355
+ if (tableRowIndex >= tableRows.length || tableRowIndex < 0) {
356
+ throw new Error('Expected table cell to be inside of table row.');
357
+ }
358
+
359
+ const tableRow = tableRows[tableRowIndex];
360
+
361
+ if (!$isTableRowNode(tableRow)) {
362
+ throw new Error('Expected table row');
363
+ }
364
+
365
+ const newStyle =
366
+ tableCellNode.getHeaderStyles() ^ TableCellHeaderStates.ROW;
367
+ tableRow.getChildren().forEach((tableCell) => {
368
+ if (!$isTableCellNode(tableCell)) {
369
+ throw new Error('Expected table cell');
370
+ }
371
+
372
+ tableCell.setHeaderStyles(newStyle, TableCellHeaderStates.ROW);
373
+ });
374
+
375
+ clearTableSelection();
376
+ close();
377
+ });
378
+ }, [editor, tableCellNode, clearTableSelection, close]);
379
+
380
+ const toggleTableColumnIsHeader = useCallback(() => {
381
+ editor.update(() => {
382
+ const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
383
+
384
+ const tableColumnIndex =
385
+ $getTableColumnIndexFromTableCellNode(tableCellNode);
386
+
387
+ const tableRows = tableNode.getChildren<TableRowNode>();
388
+ const maxRowsLength = Math.max(
389
+ ...tableRows.map((row) => row.getChildren().length),
390
+ );
391
+
392
+ if (tableColumnIndex >= maxRowsLength || tableColumnIndex < 0) {
393
+ throw new Error('Expected table cell to be inside of table row.');
394
+ }
395
+
396
+ const newStyle =
397
+ tableCellNode.getHeaderStyles() ^ TableCellHeaderStates.COLUMN;
398
+ for (let r = 0; r < tableRows.length; r++) {
399
+ const tableRow = tableRows[r];
400
+
401
+ if (!$isTableRowNode(tableRow)) {
402
+ throw new Error('Expected table row');
403
+ }
404
+
405
+ const tableCells = tableRow.getChildren();
406
+ if (tableColumnIndex >= tableCells.length) {
407
+ // if cell is outside of bounds for the current row (for example various merge cell cases) we shouldn't highlight it
408
+ continue;
409
+ }
410
+
411
+ const tableCell = tableCells[tableColumnIndex];
412
+
413
+ if (!$isTableCellNode(tableCell)) {
414
+ throw new Error('Expected table cell');
415
+ }
416
+
417
+ tableCell.setHeaderStyles(newStyle, TableCellHeaderStates.COLUMN);
418
+ }
419
+ clearTableSelection();
420
+ close();
421
+ });
422
+ }, [editor, tableCellNode, clearTableSelection, close]);
423
+
424
+ const toggleRowStriping = useCallback(() => {
425
+ editor.update(() => {
426
+ if (tableCellNode.isAttached()) {
427
+ const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
428
+ if (tableNode) {
429
+ tableNode.setRowStriping(!tableNode.getRowStriping());
430
+ }
431
+ }
432
+ clearTableSelection();
433
+ close();
434
+ });
435
+ }, [editor, tableCellNode, clearTableSelection, close]);
436
+
437
+ const handleCellBackgroundColor = useCallback(
438
+ (value: string) => {
439
+ editor.update(() => {
440
+ const selection = $getSelection();
441
+ if ($isRangeSelection(selection) || $isTableSelection(selection)) {
442
+ const [cell] = $getNodeTriplet(selection.anchor);
443
+ if ($isTableCellNode(cell)) {
444
+ cell.setBackgroundColor(value);
445
+ }
446
+
447
+ if ($isTableSelection(selection)) {
448
+ const nodes = selection.getNodes();
449
+
450
+ for (let i = 0; i < nodes.length; i++) {
451
+ const node = nodes[i];
452
+ if ($isTableCellNode(node)) {
453
+ node.setBackgroundColor(value);
454
+ }
455
+ }
456
+ }
457
+ }
458
+ });
459
+ },
460
+ [editor],
461
+ );
462
+
463
+ let mergeCellButton: null | JSX.Element = null;
464
+ if (cellMerge) {
465
+ if (canMergeCells) {
466
+ mergeCellButton = (
467
+ <button
468
+ type="button"
469
+ className="item"
470
+ onClick={() => mergeTableCellsAtSelection()}
471
+ data-test-id="table-merge-cells">
472
+ Merge cells
473
+ </button>
474
+ );
475
+ } else if (canUnmergeCell) {
476
+ mergeCellButton = (
477
+ <button
478
+ type="button"
479
+ className="item"
480
+ onClick={() => unmergeTableCellsAtSelection()}
481
+ data-test-id="table-unmerge-cells">
482
+ Unmerge cells
483
+ </button>
484
+ );
485
+ }
486
+ }
487
+
488
+ return createPortal(
489
+
490
+ // eslint-disable-next-line jsx-a11y/no-static-element-interactions
491
+ <div
492
+ className="dropdown"
493
+ ref={dropDownRef}
494
+ onClick={(e) => {
495
+ e.stopPropagation();
496
+ }}>
497
+ {mergeCellButton}
498
+ <button
499
+ type="button"
500
+ className="item"
501
+ onClick={() =>
502
+ modal.show('Cell background color', (
503
+ <ColorPicker
504
+ color={backgroundColor}
505
+ onChange={handleCellBackgroundColor}
506
+ />
507
+ ))
508
+ }
509
+ data-test-id="table-background-color">
510
+ <span className="text">Background color</span>
511
+ </button>
512
+ <button
513
+ type="button"
514
+ className="item"
515
+ onClick={() => toggleRowStriping()}
516
+ data-test-id="table-row-striping">
517
+ <span className="text">Toggle Row Striping</span>
518
+ </button>
519
+ <hr />
520
+ <button
521
+ type="button"
522
+ className="item"
523
+ onClick={() => insertTableRowAtSelection(false)}
524
+ data-test-id="table-insert-row-above">
525
+ <span className="text">
526
+ Insert{' '}
527
+ {selectionCounts.rows === 1 ? 'row' : `${selectionCounts.rows} rows`}{' '}
528
+ above
529
+ </span>
530
+ </button>
531
+ <button
532
+ type="button"
533
+ className="item"
534
+ onClick={() => insertTableRowAtSelection(true)}
535
+ data-test-id="table-insert-row-below">
536
+ <span className="text">
537
+ Insert{' '}
538
+ {selectionCounts.rows === 1 ? 'row' : `${selectionCounts.rows} rows`}{' '}
539
+ below
540
+ </span>
541
+ </button>
542
+ <hr />
543
+ <button
544
+ type="button"
545
+ className="item"
546
+ onClick={() => insertTableColumnAtSelection(false)}
547
+ data-test-id="table-insert-column-before">
548
+ <span className="text">
549
+ Insert{' '}
550
+ {selectionCounts.columns === 1
551
+ ? 'column'
552
+ : `${selectionCounts.columns} columns`}{' '}
553
+ left
554
+ </span>
555
+ </button>
556
+ <button
557
+ type="button"
558
+ className="item"
559
+ onClick={() => insertTableColumnAtSelection(true)}
560
+ data-test-id="table-insert-column-after">
561
+ <span className="text">
562
+ Insert{' '}
563
+ {selectionCounts.columns === 1
564
+ ? 'column'
565
+ : `${selectionCounts.columns} columns`}{' '}
566
+ right
567
+ </span>
568
+ </button>
569
+ <hr />
570
+ <button
571
+ type="button"
572
+ className="item"
573
+ onClick={() => deleteTableColumnAtSelection()}
574
+ data-test-id="table-delete-columns">
575
+ <span className="text">Delete column</span>
576
+ </button>
577
+ <button
578
+ type="button"
579
+ className="item"
580
+ onClick={() => deleteTableRowAtSelection()}
581
+ data-test-id="table-delete-rows">
582
+ <span className="text">Delete row</span>
583
+ </button>
584
+ <button
585
+ type="button"
586
+ className="item"
587
+ onClick={() => deleteTableAtSelection()}
588
+ data-test-id="table-delete">
589
+ <span className="text">Delete table</span>
590
+ </button>
591
+ <hr />
592
+ <button
593
+ type="button"
594
+ className="item"
595
+ onClick={() => toggleTableRowIsHeader()}>
596
+ <span className="text">
597
+ {(tableCellNode.__headerState & TableCellHeaderStates.ROW) ===
598
+ TableCellHeaderStates.ROW
599
+ ? 'Remove'
600
+ : 'Add'}{' '}
601
+ row header
602
+ </span>
603
+ </button>
604
+ <button
605
+ type="button"
606
+ className="item"
607
+ onClick={() => toggleTableColumnIsHeader()}
608
+ data-test-id="table-column-header">
609
+ <span className="text">
610
+ {(tableCellNode.__headerState & TableCellHeaderStates.COLUMN) ===
611
+ TableCellHeaderStates.COLUMN
612
+ ? 'Remove'
613
+ : 'Add'}{' '}
614
+ column header
615
+ </span>
616
+ </button>
617
+ </div>,
618
+ document.body,
619
+ );
620
+ }
621
+
622
+ function TableCellActionMenuContainer({
623
+ anchorElem,
624
+ cellMerge,
625
+ }: {
626
+ anchorElem: HTMLElement;
627
+ cellMerge: boolean;
628
+ }): JSX.Element {
629
+
630
+
631
+ const [editor] = useLexicalComposerContext();
632
+
633
+ const menuButtonRef = useRef(null);
634
+ const menuRootRef = useRef(null);
635
+ const [isMenuOpen, setIsMenuOpen] = useState(false);
636
+
637
+ const [tableCellNode, setTableMenuCellNode] = useState<TableCellNode | null>(
638
+ null,
639
+ );
640
+
641
+ const $moveMenu = useCallback(() => {
642
+ const menu = menuButtonRef.current;
643
+ const selection = $getSelection();
644
+ const nativeSelection = window.getSelection();
645
+ const activeElement = document.activeElement;
646
+
647
+ if (selection == null || menu == null) {
648
+ setTableMenuCellNode(null);
649
+ return;
650
+ }
651
+
652
+ const rootElement = editor.getRootElement();
653
+
654
+ if (
655
+ $isRangeSelection(selection) &&
656
+ rootElement !== null &&
657
+ nativeSelection !== null &&
658
+ rootElement.contains(nativeSelection.anchorNode)
659
+ ) {
660
+ const tableCellNodeFromSelection = $getTableCellNodeFromLexicalNode(
661
+ selection.anchor.getNode(),
662
+ );
663
+
664
+ if (tableCellNodeFromSelection == null) {
665
+ setTableMenuCellNode(null);
666
+ return;
667
+ }
668
+
669
+ const tableCellParentNodeDOM = editor.getElementByKey(
670
+ tableCellNodeFromSelection.getKey(),
671
+ );
672
+
673
+ if (tableCellParentNodeDOM == null) {
674
+ setTableMenuCellNode(null);
675
+ return;
676
+ }
677
+
678
+ setTableMenuCellNode(tableCellNodeFromSelection);
679
+ } else if (!activeElement) {
680
+ setTableMenuCellNode(null);
681
+ }
682
+ }, [editor]);
683
+
684
+ useEffect(() => {
685
+ return editor.registerUpdateListener(() => {
686
+ editor.getEditorState().read(() => {
687
+ $moveMenu();
688
+ });
689
+ });
690
+ });
691
+
692
+ useEffect(() => {
693
+ const menuButtonDOM = menuButtonRef.current as HTMLButtonElement | null;
694
+
695
+ if (menuButtonDOM != null && tableCellNode != null) {
696
+ const tableCellNodeDOM = editor.getElementByKey(tableCellNode.getKey());
697
+
698
+ if (tableCellNodeDOM != null) {
699
+ const tableCellRect = tableCellNodeDOM.getBoundingClientRect();
700
+ const menuRect = menuButtonDOM.getBoundingClientRect();
701
+ const anchorRect = anchorElem.getBoundingClientRect();
702
+
703
+ const top = tableCellRect.top - anchorRect.top + 4;
704
+ const left =
705
+ tableCellRect.right - menuRect.width - 10 - anchorRect.left;
706
+
707
+ menuButtonDOM.style.opacity = '1';
708
+ menuButtonDOM.style.transform = `translate(${left}px, ${top}px)`;
709
+ } else {
710
+ menuButtonDOM.style.opacity = '0';
711
+ menuButtonDOM.style.transform = 'translate(-10000px, -10000px)';
712
+ }
713
+ }
714
+ }, [menuButtonRef, tableCellNode, editor, anchorElem]);
715
+
716
+ const prevTableCellDOM = useRef(tableCellNode);
717
+
718
+ useEffect(() => {
719
+ if (prevTableCellDOM.current !== tableCellNode) {
720
+ setIsMenuOpen(false);
721
+ }
722
+
723
+ prevTableCellDOM.current = tableCellNode;
724
+ }, [prevTableCellDOM, tableCellNode]);
725
+
726
+ return (
727
+ <div className="table-cell-action-button-container" ref={menuButtonRef}>
728
+ {tableCellNode != null && (
729
+ <>
730
+ <button
731
+ type="button"
732
+ className="table-cell-action-button chevron-down"
733
+ onClick={(e) => {
734
+ e.stopPropagation();
735
+ setIsMenuOpen(!isMenuOpen);
736
+ }}
737
+ ref={menuRootRef}>
738
+ <i className="chevron-down" />
739
+ </button>
740
+
741
+ {isMenuOpen && (
742
+ <TableActionMenu
743
+ contextRef={menuRootRef}
744
+ setIsMenuOpen={setIsMenuOpen}
745
+ close={() => setIsMenuOpen(false)}
746
+ tableCellNode={tableCellNode}
747
+ cellMerge={cellMerge}
748
+ />
749
+ )}
750
+ </>
751
+ )}
752
+ </div>
753
+ );
754
+ }
755
+
756
+ export default function TableActionMenuPlugin({
757
+ anchorElem = document.body,
758
+ cellMerge = false,
759
+ }: {
760
+ anchorElem?: HTMLElement;
761
+ cellMerge?: boolean;
762
+ }): null | ReactPortal {
763
+ const isEditable = useLexicalEditable();
764
+ return createPortal(
765
+ isEditable ? (
766
+ <TableCellActionMenuContainer
767
+ anchorElem={anchorElem}
768
+ cellMerge={cellMerge}
769
+ />
770
+ ) : null,
771
+ anchorElem,
772
+ );
773
+ }