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.
- package/package.json +5 -1
- package/src/client/assets/css/components/table.less +2 -0
- package/src/client/components/Form.ts +1 -1
- package/src/client/components/button.tsx +2 -1
- package/src/client/components/containers/Popover/index.tsx +2 -2
- package/src/client/components/dropdown/index.tsx +16 -6
- package/src/client/components/input/Slider/index.tsx +0 -2
- package/src/client/components/inputv3/Rte/Editor.tsx +271 -0
- package/src/client/components/inputv3/Rte/ToolbarPlugin/BlockFormat.tsx +220 -0
- package/src/client/components/inputv3/Rte/ToolbarPlugin/ElementFormat.tsx +107 -0
- package/src/client/components/inputv3/Rte/ToolbarPlugin/index.tsx +768 -0
- package/src/client/components/inputv3/Rte/appSettings.ts +36 -0
- package/src/client/components/inputv3/Rte/context/FlashMessageContext.tsx +68 -0
- package/src/client/components/inputv3/Rte/context/SettingsContext.tsx +71 -0
- package/src/client/components/inputv3/Rte/context/SharedAutocompleteContext.tsx +71 -0
- package/src/client/components/inputv3/Rte/context/SharedHistoryContext.tsx +35 -0
- package/src/client/components/inputv3/Rte/currentEditor.ts +42 -0
- package/src/client/components/inputv3/Rte/hooks/useFlashMessage.tsx +16 -0
- package/src/client/components/inputv3/Rte/hooks/useReport.ts +67 -0
- package/src/client/components/inputv3/Rte/images/emoji/1F600.png +0 -0
- package/src/client/components/inputv3/Rte/images/emoji/1F641.png +0 -0
- package/src/client/components/inputv3/Rte/images/emoji/1F642.png +0 -0
- package/src/client/components/inputv3/Rte/images/emoji/2764.png +0 -0
- package/src/client/components/inputv3/Rte/images/emoji/LICENSE.md +5 -0
- package/src/client/components/inputv3/Rte/images/icons/draggable-block-menu.svg +1 -0
- package/src/client/components/inputv3/Rte/images/icons/prettier-error.svg +1 -0
- package/src/client/components/inputv3/Rte/images/icons/prettier.svg +1 -0
- package/src/client/components/inputv3/Rte/images/image/LICENSE.md +5 -0
- package/src/client/components/inputv3/Rte/images/image-broken.svg +4 -0
- package/src/client/components/inputv3/Rte/images/logo.svg +1 -0
- package/src/client/components/inputv3/Rte/index.tsx +63 -79
- package/src/client/components/inputv3/Rte/nodes/AutocompleteNode.tsx +119 -0
- package/src/client/components/inputv3/Rte/nodes/EmojiNode.tsx +102 -0
- package/src/client/components/inputv3/Rte/nodes/EquationComponent.tsx +141 -0
- package/src/client/components/inputv3/Rte/nodes/EquationNode.tsx +174 -0
- package/src/client/components/inputv3/Rte/nodes/FigmaNode.tsx +135 -0
- package/src/client/components/inputv3/Rte/nodes/ImageComponent.tsx +468 -0
- package/src/client/components/inputv3/Rte/nodes/ImageNode.css +43 -0
- package/src/client/components/inputv3/Rte/nodes/ImageNode.tsx +266 -0
- package/src/client/components/inputv3/Rte/nodes/InlineImageNode/InlineImageComponent.tsx +402 -0
- package/src/client/components/inputv3/Rte/nodes/InlineImageNode/InlineImageNode.css +94 -0
- package/src/client/components/inputv3/Rte/nodes/InlineImageNode/InlineImageNode.tsx +294 -0
- package/src/client/components/inputv3/Rte/nodes/KeywordNode.ts +67 -0
- package/src/client/components/inputv3/Rte/nodes/LayoutContainerNode.ts +137 -0
- package/src/client/components/inputv3/Rte/nodes/LayoutItemNode.ts +71 -0
- package/src/client/components/inputv3/Rte/nodes/MentionNode.ts +130 -0
- package/src/client/components/inputv3/Rte/nodes/PageBreakNode/index.css +62 -0
- package/src/client/components/inputv3/Rte/nodes/PageBreakNode/index.tsx +170 -0
- package/src/client/components/inputv3/Rte/nodes/PlaygroundNodes.ts +76 -0
- package/src/client/components/inputv3/Rte/nodes/PollComponent.tsx +249 -0
- package/src/client/components/inputv3/Rte/nodes/PollNode.css +187 -0
- package/src/client/components/inputv3/Rte/nodes/PollNode.tsx +209 -0
- package/src/client/components/inputv3/Rte/nodes/StickyComponent.tsx +261 -0
- package/src/client/components/inputv3/Rte/nodes/StickyNode.css +37 -0
- package/src/client/components/inputv3/Rte/nodes/StickyNode.tsx +150 -0
- package/src/client/components/inputv3/Rte/nodes/TweetNode.tsx +223 -0
- package/src/client/components/inputv3/Rte/nodes/YouTubeNode.tsx +184 -0
- package/src/client/components/inputv3/Rte/plugins/ActionsPlugin/index.tsx +334 -0
- package/src/client/components/inputv3/Rte/plugins/AutoEmbedPlugin/index.tsx +352 -0
- package/src/client/components/inputv3/Rte/plugins/AutoLinkPlugin/index.tsx +32 -0
- package/src/client/components/inputv3/Rte/plugins/AutocompletePlugin/index.tsx +2529 -0
- package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/components/CopyButton/index.tsx +70 -0
- package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/components/PrettierButton/index.css +14 -0
- package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/components/PrettierButton/index.tsx +156 -0
- package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/index.css +54 -0
- package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/index.tsx +190 -0
- package/src/client/components/inputv3/Rte/plugins/CodeActionMenuPlugin/utils.ts +33 -0
- package/src/client/components/inputv3/Rte/plugins/CodeHighlightPlugin/index.ts +21 -0
- package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/Collapsible.css +57 -0
- package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleContainerNode.ts +168 -0
- package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleContentNode.ts +127 -0
- package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleTitleNode.ts +152 -0
- package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/CollapsibleUtils.ts +17 -0
- package/src/client/components/inputv3/Rte/plugins/CollapsiblePlugin/index.ts +284 -0
- package/src/client/components/inputv3/Rte/plugins/ComponentPickerPlugin/index.tsx +370 -0
- package/src/client/components/inputv3/Rte/plugins/ContextMenuPlugin/index.tsx +270 -0
- package/src/client/components/inputv3/Rte/plugins/DocsPlugin/index.tsx +20 -0
- package/src/client/components/inputv3/Rte/plugins/DragDropPastePlugin/index.ts +51 -0
- package/src/client/components/inputv3/Rte/plugins/DraggableBlockPlugin/index.css +36 -0
- package/src/client/components/inputv3/Rte/plugins/DraggableBlockPlugin/index.tsx +43 -0
- package/src/client/components/inputv3/Rte/plugins/EmojiPickerPlugin/index.tsx +198 -0
- package/src/client/components/inputv3/Rte/plugins/EmojisPlugin/index.ts +75 -0
- package/src/client/components/inputv3/Rte/plugins/EquationsPlugin/index.tsx +82 -0
- package/src/client/components/inputv3/Rte/plugins/FigmaPlugin/index.tsx +40 -0
- package/src/client/components/inputv3/Rte/plugins/FloatingLinkEditorPlugin/index.css +41 -0
- package/src/client/components/inputv3/Rte/plugins/FloatingLinkEditorPlugin/index.tsx +393 -0
- package/src/client/components/inputv3/Rte/plugins/FloatingTextFormatToolbarPlugin/index.css +141 -0
- package/src/client/components/inputv3/Rte/plugins/FloatingTextFormatToolbarPlugin/index.tsx +388 -0
- package/src/client/components/inputv3/Rte/plugins/ImagesPlugin/index.tsx +350 -0
- package/src/client/components/inputv3/Rte/plugins/InlineImagePlugin/index.tsx +336 -0
- package/src/client/components/inputv3/Rte/plugins/KeywordsPlugin/index.ts +56 -0
- package/src/client/components/inputv3/Rte/plugins/LayoutPlugin/InsertLayoutDialog.tsx +58 -0
- package/src/client/components/inputv3/Rte/plugins/LayoutPlugin/LayoutPlugin.tsx +219 -0
- package/src/client/components/inputv3/Rte/plugins/LinkPlugin/index.tsx +34 -0
- package/src/client/components/inputv3/Rte/plugins/ListMaxIndentLevelPlugin/index.ts +85 -0
- package/src/client/components/inputv3/Rte/plugins/MarkdownShortcutPlugin/index.tsx +16 -0
- package/src/client/components/inputv3/Rte/plugins/MarkdownTransformers/index.ts +324 -0
- package/src/client/components/inputv3/Rte/plugins/MaxLengthPlugin/index.tsx +53 -0
- package/src/client/components/inputv3/Rte/plugins/MentionsPlugin/index.tsx +696 -0
- package/src/client/components/inputv3/Rte/plugins/PageBreakPlugin/index.tsx +57 -0
- package/src/client/components/inputv3/Rte/plugins/PasteLogPlugin/index.tsx +54 -0
- package/src/client/components/inputv3/Rte/plugins/PollPlugin/index.tsx +86 -0
- package/src/client/components/inputv3/Rte/plugins/SpeechToTextPlugin/index.ts +125 -0
- package/src/client/components/inputv3/Rte/plugins/StickyPlugin/index.ts +22 -0
- package/src/client/components/inputv3/Rte/plugins/TabFocusPlugin/index.tsx +65 -0
- package/src/client/components/inputv3/Rte/plugins/TableActionMenuPlugin/index.tsx +773 -0
- package/src/client/components/inputv3/Rte/plugins/TableCellResizer/index.css +12 -0
- package/src/client/components/inputv3/Rte/plugins/TableCellResizer/index.tsx +436 -0
- package/src/client/components/inputv3/Rte/plugins/TableHoverActionsPlugin/index.tsx +287 -0
- package/src/client/components/inputv3/Rte/plugins/TableOfContentsPlugin/index.css +95 -0
- package/src/client/components/inputv3/Rte/plugins/TableOfContentsPlugin/index.tsx +197 -0
- package/src/client/components/inputv3/Rte/plugins/TablePlugin.tsx +178 -0
- package/src/client/components/inputv3/Rte/plugins/TestRecorderPlugin/index.tsx +468 -0
- package/src/client/components/inputv3/Rte/plugins/TreeViewPlugin/index.tsx +26 -0
- package/src/client/components/inputv3/Rte/plugins/TwitterPlugin/index.ts +41 -0
- package/src/client/components/inputv3/Rte/plugins/TypingPerfPlugin/index.ts +117 -0
- package/src/client/components/inputv3/Rte/plugins/YouTubePlugin/index.ts +41 -0
- package/src/client/components/inputv3/Rte/shared/canUseDOM.ts +4 -0
- package/src/client/components/inputv3/Rte/shared/caretFromPoint.ts +40 -0
- package/src/client/components/inputv3/Rte/shared/environment.ts +56 -0
- package/src/client/components/inputv3/Rte/shared/invariant.ts +26 -0
- package/src/client/components/inputv3/Rte/shared/normalizeClassNames.ts +21 -0
- package/src/client/components/inputv3/Rte/shared/react-test-utils.ts +18 -0
- package/src/client/components/inputv3/Rte/shared/reactPatches.ts +22 -0
- package/src/client/components/inputv3/Rte/shared/simpleDiffWithCursor.ts +49 -0
- package/src/client/components/inputv3/Rte/shared/useLayoutEffect.ts +19 -0
- package/src/client/components/inputv3/Rte/shared/warnOnlyOnce.ts +20 -0
- package/src/client/components/inputv3/Rte/style.less +30 -60
- package/src/client/components/inputv3/Rte/themes/CommentEditorTheme.css +13 -0
- package/src/client/components/inputv3/Rte/themes/CommentEditorTheme.ts +20 -0
- package/src/client/components/inputv3/Rte/themes/PlaygroundEditorTheme.css +447 -0
- package/src/client/components/inputv3/Rte/themes/PlaygroundEditorTheme.ts +120 -0
- package/src/client/components/inputv3/Rte/themes/StickyEditorTheme.css +13 -0
- package/src/client/components/inputv3/Rte/themes/StickyEditorTheme.ts +20 -0
- package/src/client/components/inputv3/Rte/ui/ColorPicker.css +88 -0
- package/src/client/components/inputv3/Rte/ui/ColorPicker.tsx +365 -0
- package/src/client/components/inputv3/Rte/ui/ContentEditable.css +44 -0
- package/src/client/components/inputv3/Rte/ui/ContentEditable.tsx +36 -0
- package/src/client/components/inputv3/Rte/ui/DropDown.tsx +259 -0
- package/src/client/components/inputv3/Rte/ui/DropdownColorPicker.tsx +41 -0
- package/src/client/components/inputv3/Rte/ui/EquationEditor.css +38 -0
- package/src/client/components/inputv3/Rte/ui/EquationEditor.tsx +56 -0
- package/src/client/components/inputv3/Rte/ui/FileInput.tsx +38 -0
- package/src/client/components/inputv3/Rte/ui/FlashMessage.css +28 -0
- package/src/client/components/inputv3/Rte/ui/FlashMessage.tsx +29 -0
- package/src/client/components/inputv3/Rte/ui/ImageResizer.tsx +316 -0
- package/src/client/components/inputv3/Rte/ui/Input.css +32 -0
- package/src/client/components/inputv3/Rte/ui/KatexRenderer.tsx +54 -0
- package/src/client/components/inputv3/Rte/ui/Switch.tsx +36 -0
- package/src/client/components/inputv3/Rte/utils/docSerialization.ts +77 -0
- package/src/client/components/inputv3/Rte/utils/emoji-list.ts +16615 -0
- package/src/client/components/inputv3/Rte/utils/getDOMRangeRect.ts +27 -0
- package/src/client/components/inputv3/Rte/utils/getSelectedNode.ts +27 -0
- package/src/client/components/inputv3/Rte/utils/guard.ts +10 -0
- package/src/client/components/inputv3/Rte/utils/isMobileWidth.ts +7 -0
- package/src/client/components/inputv3/Rte/utils/joinClasses.ts +13 -0
- package/src/client/components/inputv3/Rte/utils/setFloatingElemPosition.ts +51 -0
- package/src/client/components/inputv3/Rte/utils/setFloatingElemPositionForLinkEditor.ts +46 -0
- package/src/client/components/inputv3/Rte/utils/swipe.ts +127 -0
- package/src/client/components/inputv3/Rte/utils/url.ts +38 -0
- package/src/client/components/inputv3/base.tsx +8 -5
- package/src/client/components/inputv3/file/index.tsx +11 -5
- package/src/common/data/rte/nodes.ts +60 -9
- package/src/common/validation/index.ts +21 -2
- package/src/common/validation/schema.ts +42 -10
- package/src/common/validation/validator.ts +12 -4
- package/src/common/validation/validators.ts +82 -53
- package/src/server/services/router/http/multipart.ts +0 -1
- package/src/server/services/schema/index.ts +24 -2
- package/src/server/services/schema/request.ts +3 -2
- package/src/server/services/schema/rte.ts +110 -0
- package/src/{common/data/rte/index.ts → server/utils/rte.ts} +27 -16
- package/src/client/components/inputv3/Rte/ExampleTheme.tsx +0 -42
- package/src/client/components/inputv3/Rte/ToolbarPlugin.tsx +0 -167
- package/src/client/components/inputv3/Rte/icons/LICENSE.md +0 -5
- package/src/client/components/inputv3/Rte/icons/arrow-clockwise.svg +0 -4
- package/src/client/components/inputv3/Rte/icons/arrow-counterclockwise.svg +0 -4
- package/src/client/components/inputv3/Rte/icons/journal-text.svg +0 -5
- package/src/client/components/inputv3/Rte/icons/justify.svg +0 -3
- package/src/client/components/inputv3/Rte/icons/text-center.svg +0 -3
- package/src/client/components/inputv3/Rte/icons/text-left.svg +0 -3
- package/src/client/components/inputv3/Rte/icons/text-paragraph.svg +0 -3
- package/src/client/components/inputv3/Rte/icons/text-right.svg +0 -3
- package/src/client/components/inputv3/Rte/icons/type-bold.svg +0 -3
- package/src/client/components/inputv3/Rte/icons/type-italic.svg +0 -3
- package/src/client/components/inputv3/Rte/icons/type-strikethrough.svg +0 -3
- 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
|
+
}
|