@crystallize/design-system 1.3.2 → 1.4.1
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/CHANGELOG.md +12 -0
- package/dist/TableComponent-I2YOOYOU.css +281 -0
- package/dist/TableComponent-QINOO453.mjs +1377 -0
- package/dist/arrow-clockwise-Z2G6UEGP.svg +1 -0
- package/dist/arrow-counterclockwise-2O5EYVJT.svg +1 -0
- package/dist/bg-color-HB2WDYGO.svg +1 -0
- package/dist/camera-CR7D2PNH.svg +1 -0
- package/dist/caret-right-fill-FFBNEXVX.svg +1 -0
- package/dist/chat-square-quote-CI6PUJHH.svg +1 -0
- package/dist/chevron-down-3FRWSIKS.svg +1 -0
- package/dist/chunk-VUXQZRSP.mjs +737 -0
- package/dist/clipboard-OSEFDF25.svg +1 -0
- package/dist/close-FH57ZMJF.svg +1 -0
- package/dist/code-SEVR6TIQ.svg +1 -0
- package/dist/copy-DMGDODUL.svg +1 -0
- package/dist/diagram-2-CEJUD2B4.svg +1 -0
- package/dist/download-JXUGIUCX.svg +1 -0
- package/dist/draggable-block-menu-KKHDNKJA.svg +1 -0
- package/dist/dropdown-more-BHZ5COKX.svg +1 -0
- package/dist/file-image-TIQPFJX4.svg +1 -0
- package/dist/filetype-gif-OG2BEYYK.svg +1 -0
- package/dist/font-color-J4GA3ZJO.svg +1 -0
- package/dist/font-family-ZU5N6TTE.svg +1 -0
- package/dist/gear-ICMT4NTP.svg +1 -0
- package/dist/horizontal-rule-N6RD2V7H.svg +1 -0
- package/dist/indent-MJ6JIMCK.svg +1 -0
- package/dist/index.css +2711 -315
- package/dist/index.d.ts +145 -40
- package/dist/index.js +10376 -1481
- package/dist/index.mjs +7609 -746
- package/dist/journal-code-XUT44HDV.svg +1 -0
- package/dist/justify-J7X5JEEX.svg +1 -0
- package/dist/link-W52N4JKZ.svg +1 -0
- package/dist/list-ol-2ZEUN4Z7.svg +1 -0
- package/dist/list-ul-DVKNUP47.svg +1 -0
- package/dist/lock-WCYOZOHW.svg +1 -0
- package/dist/lock-fill-JZSKOSHK.svg +1 -0
- package/dist/markdown-4BGQNLLT.svg +1 -0
- package/dist/mic-H5FNOMM7.svg +1 -0
- package/dist/outdent-2LUMUMIP.svg +1 -0
- package/dist/paint-bucket-VCISMZTH.svg +1 -0
- package/dist/palette-SWGFPRWZ.svg +1 -0
- package/dist/pencil-fill-STFSC26F.svg +1 -0
- package/dist/plug-HGGGEVS3.svg +1 -0
- package/dist/plug-fill-OTG3U4TN.svg +1 -0
- package/dist/plus-CQISIKEC.svg +1 -0
- package/dist/plus-slash-minus-N22JU4TI.svg +1 -0
- package/dist/prettier-WUJ7B5NV.svg +1 -0
- package/dist/prettier-error-DYJSLYDP.svg +1 -0
- package/dist/square-check-UTG6FU6D.svg +1 -0
- package/dist/success-YVXUMPEZ.svg +1 -0
- package/dist/table-BR6DI4ZQ.svg +1 -0
- package/dist/text-center-UQI6PAEF.svg +1 -0
- package/dist/text-left-KT2B6TR3.svg +1 -0
- package/dist/text-paragraph-MFTUIIQG.svg +1 -0
- package/dist/text-right-SKELPISG.svg +1 -0
- package/dist/trash-UOM6D7TD.svg +1 -0
- package/dist/type-bold-PY7COC3N.svg +1 -0
- package/dist/type-h1-6KJP7YOM.svg +1 -0
- package/dist/type-h2-VHI2USC3.svg +1 -0
- package/dist/type-h3-JIU77CHO.svg +1 -0
- package/dist/type-h4-P5EHKDAL.svg +1 -0
- package/dist/type-h5-CS2KYVRG.svg +1 -0
- package/dist/type-h6-J2O74LJZ.svg +1 -0
- package/dist/type-italic-3DSFOSG2.svg +1 -0
- package/dist/type-strikethrough-E2KKQFSX.svg +1 -0
- package/dist/type-subscript-BMPTRIBU.svg +1 -0
- package/dist/type-superscript-EDF6EPAA.svg +1 -0
- package/dist/type-underline-CBFA5VLF.svg +1 -0
- package/dist/upload-Q6KICGZW.svg +1 -0
- package/dist/user-EOI2NEFZ.svg +1 -0
- package/package.json +30 -6
- package/src/dialog/dialog.tsx +1 -0
- package/src/icon-button/icon-button.css +16 -14
- package/src/index.ts +4 -4
- package/src/input/input.css +1 -1
- package/src/input-with-label/input-with-label.css +1 -1
- package/src/rich-text-editor/appSettings.ts +28 -0
- package/src/rich-text-editor/context/SettingsContext.tsx +71 -0
- package/src/rich-text-editor/context/SharedAutocompleteContext.tsx +60 -0
- package/src/rich-text-editor/context/SharedHistoryContext.tsx +25 -0
- package/src/rich-text-editor/hooks/useReport.ts +64 -0
- package/src/rich-text-editor/images/cat-typing.gif +0 -0
- package/src/rich-text-editor/images/emoji/1F600.png +0 -0
- package/src/rich-text-editor/images/emoji/1F641.png +0 -0
- package/src/rich-text-editor/images/emoji/1F642.png +0 -0
- package/src/rich-text-editor/images/emoji/2764.png +0 -0
- package/src/rich-text-editor/images/emoji/LICENSE.md +5 -0
- package/src/rich-text-editor/images/icons/LICENSE.md +5 -0
- package/src/rich-text-editor/images/icons/arrow-clockwise.svg +1 -0
- package/src/rich-text-editor/images/icons/arrow-counterclockwise.svg +1 -0
- package/src/rich-text-editor/images/icons/bg-color.svg +1 -0
- package/src/rich-text-editor/images/icons/camera.svg +1 -0
- package/src/rich-text-editor/images/icons/card-checklist.svg +1 -0
- package/src/rich-text-editor/images/icons/caret-right-fill.svg +1 -0
- package/src/rich-text-editor/images/icons/chat-left-text.svg +1 -0
- package/src/rich-text-editor/images/icons/chat-right-dots.svg +1 -0
- package/src/rich-text-editor/images/icons/chat-right-text.svg +1 -0
- package/src/rich-text-editor/images/icons/chat-right.svg +1 -0
- package/src/rich-text-editor/images/icons/chat-square-quote.svg +1 -0
- package/src/rich-text-editor/images/icons/chevron-down.svg +1 -0
- package/src/rich-text-editor/images/icons/clipboard.svg +1 -0
- package/src/rich-text-editor/images/icons/close.svg +1 -0
- package/src/rich-text-editor/images/icons/code.svg +1 -0
- package/src/rich-text-editor/images/icons/comments.svg +1 -0
- package/src/rich-text-editor/images/icons/copy.svg +1 -0
- package/src/rich-text-editor/images/icons/diagram-2.svg +1 -0
- package/src/rich-text-editor/images/icons/download.svg +1 -0
- package/src/rich-text-editor/images/icons/draggable-block-menu.svg +1 -0
- package/src/rich-text-editor/images/icons/dropdown-more.svg +1 -0
- package/src/rich-text-editor/images/icons/figma.svg +1 -0
- package/src/rich-text-editor/images/icons/file-image.svg +1 -0
- package/src/rich-text-editor/images/icons/filetype-gif.svg +1 -0
- package/src/rich-text-editor/images/icons/font-color.svg +1 -0
- package/src/rich-text-editor/images/icons/font-family.svg +1 -0
- package/src/rich-text-editor/images/icons/gear.svg +1 -0
- package/src/rich-text-editor/images/icons/horizontal-rule.svg +1 -0
- package/src/rich-text-editor/images/icons/indent.svg +1 -0
- package/src/rich-text-editor/images/icons/journal-code.svg +1 -0
- package/src/rich-text-editor/images/icons/journal-text.svg +1 -0
- package/src/rich-text-editor/images/icons/justify.svg +1 -0
- package/src/rich-text-editor/images/icons/link.svg +1 -0
- package/src/rich-text-editor/images/icons/list-ol.svg +1 -0
- package/src/rich-text-editor/images/icons/list-ul.svg +1 -0
- package/src/rich-text-editor/images/icons/lock-fill.svg +1 -0
- package/src/rich-text-editor/images/icons/lock.svg +1 -0
- package/src/rich-text-editor/images/icons/markdown.svg +1 -0
- package/src/rich-text-editor/images/icons/mic.svg +1 -0
- package/src/rich-text-editor/images/icons/outdent.svg +1 -0
- package/src/rich-text-editor/images/icons/paint-bucket.svg +1 -0
- package/src/rich-text-editor/images/icons/palette.svg +1 -0
- package/src/rich-text-editor/images/icons/pencil-fill.svg +1 -0
- package/src/rich-text-editor/images/icons/plug-fill.svg +1 -0
- package/src/rich-text-editor/images/icons/plug.svg +1 -0
- package/src/rich-text-editor/images/icons/plus-slash-minus.svg +1 -0
- package/src/rich-text-editor/images/icons/plus.svg +1 -0
- package/src/rich-text-editor/images/icons/prettier-error.svg +1 -0
- package/src/rich-text-editor/images/icons/prettier.svg +1 -0
- package/src/rich-text-editor/images/icons/send.svg +1 -0
- package/src/rich-text-editor/images/icons/square-check.svg +1 -0
- package/src/rich-text-editor/images/icons/sticky.svg +1 -0
- package/src/rich-text-editor/images/icons/success.svg +1 -0
- package/src/rich-text-editor/images/icons/table.svg +1 -0
- package/src/rich-text-editor/images/icons/text-center.svg +1 -0
- package/src/rich-text-editor/images/icons/text-left.svg +1 -0
- package/src/rich-text-editor/images/icons/text-paragraph.svg +1 -0
- package/src/rich-text-editor/images/icons/text-right.svg +1 -0
- package/src/rich-text-editor/images/icons/trash.svg +1 -0
- package/src/rich-text-editor/images/icons/trash3.svg +1 -0
- package/src/rich-text-editor/images/icons/tweet.svg +1 -0
- package/src/rich-text-editor/images/icons/type-bold.svg +1 -0
- package/src/rich-text-editor/images/icons/type-h1.svg +1 -0
- package/src/rich-text-editor/images/icons/type-h2.svg +1 -0
- package/src/rich-text-editor/images/icons/type-h3.svg +1 -0
- package/src/rich-text-editor/images/icons/type-h4.svg +1 -0
- package/src/rich-text-editor/images/icons/type-h5.svg +1 -0
- package/src/rich-text-editor/images/icons/type-h6.svg +1 -0
- package/src/rich-text-editor/images/icons/type-italic.svg +1 -0
- package/src/rich-text-editor/images/icons/type-strikethrough.svg +1 -0
- package/src/rich-text-editor/images/icons/type-subscript.svg +1 -0
- package/src/rich-text-editor/images/icons/type-superscript.svg +1 -0
- package/src/rich-text-editor/images/icons/type-underline.svg +1 -0
- package/src/rich-text-editor/images/icons/upload.svg +1 -0
- package/src/rich-text-editor/images/icons/user.svg +1 -0
- package/src/rich-text-editor/images/icons/youtube.svg +1 -0
- package/src/rich-text-editor/images/image/LICENSE.md +5 -0
- package/src/rich-text-editor/images/landscape.jpg +0 -0
- package/src/rich-text-editor/images/logo.svg +1 -0
- package/src/rich-text-editor/images/yellow-flower-small.jpg +0 -0
- package/src/rich-text-editor/images/yellow-flower.jpg +0 -0
- package/src/rich-text-editor/index.ts +1 -0
- package/src/rich-text-editor/model/crystallize-rich-text-types/code.ts +39 -0
- package/src/rich-text-editor/model/crystallize-rich-text-types/headings.ts +12 -0
- package/src/rich-text-editor/model/crystallize-rich-text-types/index.ts +69 -0
- package/src/rich-text-editor/model/crystallize-rich-text-types/link.ts +9 -0
- package/src/rich-text-editor/model/crystallize-rich-text-types/table.ts +16 -0
- package/src/rich-text-editor/model/crystallize-to-lexical.ts +186 -0
- package/src/rich-text-editor/model/lexical-to-crystallize.ts +232 -0
- package/src/rich-text-editor/nodes/AutocompleteNode.tsx +96 -0
- package/src/rich-text-editor/nodes/BaseNodes.ts +45 -0
- package/src/rich-text-editor/nodes/KeywordNode.ts +73 -0
- package/src/rich-text-editor/nodes/TableCellNodes.ts +31 -0
- package/src/rich-text-editor/nodes/TableComponent.tsx +1547 -0
- package/src/rich-text-editor/nodes/TableNode.tsx +398 -0
- package/src/rich-text-editor/plugins/ActionsPlugin/index.tsx +83 -0
- package/src/rich-text-editor/plugins/AutoLinkPlugin/index.tsx +47 -0
- package/src/rich-text-editor/plugins/AutocompletePlugin/index.tsx +2536 -0
- package/src/rich-text-editor/plugins/CodeActionMenuPlugin/components/CopyButton/index.tsx +60 -0
- package/src/rich-text-editor/plugins/CodeActionMenuPlugin/components/PrettierButton/index.css +14 -0
- package/src/rich-text-editor/plugins/CodeActionMenuPlugin/components/PrettierButton/index.tsx +140 -0
- package/src/rich-text-editor/plugins/CodeActionMenuPlugin/index.css +46 -0
- package/src/rich-text-editor/plugins/CodeActionMenuPlugin/index.tsx +155 -0
- package/src/rich-text-editor/plugins/CodeHighlightPlugin/index.ts +21 -0
- package/src/rich-text-editor/plugins/ComponentPickerPlugin/index.tsx +320 -0
- package/src/rich-text-editor/plugins/DragDropPastePlugin/index.ts +40 -0
- package/src/rich-text-editor/plugins/DraggableBlockPlugin/index.css +36 -0
- package/src/rich-text-editor/plugins/DraggableBlockPlugin/index.tsx +368 -0
- package/src/rich-text-editor/plugins/FloatingLinkEditorPlugin/index.css +40 -0
- package/src/rich-text-editor/plugins/FloatingLinkEditorPlugin/index.tsx +305 -0
- package/src/rich-text-editor/plugins/FloatingTextFormatToolbarPlugin/index.css +128 -0
- package/src/rich-text-editor/plugins/FloatingTextFormatToolbarPlugin/index.tsx +351 -0
- package/src/rich-text-editor/plugins/LinkPlugin/index.tsx +16 -0
- package/src/rich-text-editor/plugins/ListMaxIndentLevelPlugin/index.ts +86 -0
- package/src/rich-text-editor/plugins/MarkdownShortcutPlugin/index.tsx +16 -0
- package/src/rich-text-editor/plugins/MarkdownTransformers/index.ts +195 -0
- package/src/rich-text-editor/plugins/MaxLengthPlugin/index.tsx +49 -0
- package/src/rich-text-editor/plugins/SpeechToTextPlugin/index.ts +113 -0
- package/src/rich-text-editor/plugins/TabFocusPlugin/index.tsx +65 -0
- package/src/rich-text-editor/plugins/TableActionMenuPlugin/index.tsx +481 -0
- package/src/rich-text-editor/plugins/TableCellResizer/index.css +12 -0
- package/src/rich-text-editor/plugins/TableCellResizer/index.tsx +386 -0
- package/src/rich-text-editor/plugins/TablePlugin.tsx +190 -0
- package/src/rich-text-editor/plugins/ToolbarPlugin/index.tsx +726 -0
- package/src/rich-text-editor/plugins/TreeViewPlugin/index.tsx +25 -0
- package/src/rich-text-editor/plugins/TypingPerfPlugin/index.ts +117 -0
- package/src/rich-text-editor/rich-text-editor.css +1396 -0
- package/src/rich-text-editor/rich-text-editor.stories.tsx +385 -0
- package/src/rich-text-editor/rich-text-editor.tsx +228 -0
- package/src/rich-text-editor/tests/rich-text-editor-basic-rendering.test.tsx +47 -0
- package/src/rich-text-editor/tests/rich-text-editor-code.test.tsx +39 -0
- package/src/rich-text-editor/tests/rich-text-editor-model-basics.test.tsx +56 -0
- package/src/rich-text-editor/tests/rich-text-editor-model-conversions.test.tsx +195 -0
- package/src/rich-text-editor/tests/rich-text-editor-onchange.test.tsx +37 -0
- package/src/rich-text-editor/tests/rich-text-editor-quote.test.tsx +36 -0
- package/src/rich-text-editor/tests/rich-text-editor-text-formats.test.tsx +135 -0
- package/src/rich-text-editor/tests/rich-text-editor-typing.test.tsx +73 -0
- package/src/rich-text-editor/tests/utils.ts +23 -0
- package/src/rich-text-editor/themes/PlaygroundEditorTheme.css +433 -0
- package/src/rich-text-editor/themes/PlaygroundEditorTheme.ts +113 -0
- package/src/rich-text-editor/types.ts +5 -0
- package/src/rich-text-editor/ui/ContentEditable.css +13 -0
- package/src/rich-text-editor/ui/ContentEditable.tsx +15 -0
- package/src/rich-text-editor/ui/LinkPreview.css +57 -0
- package/src/rich-text-editor/ui/LinkPreview.tsx +169 -0
- package/src/rich-text-editor/utils/environment.ts +1 -0
- package/src/rich-text-editor/utils/getDOMRangeRect.ts +42 -0
- package/src/rich-text-editor/utils/getSelectedNode.ts +27 -0
- package/src/rich-text-editor/utils/guard.ts +10 -0
- package/src/rich-text-editor/utils/isMobileWidth.ts +7 -0
- package/src/rich-text-editor/utils/joinClasses.ts +13 -0
- package/src/rich-text-editor/utils/point.ts +55 -0
- package/src/rich-text-editor/utils/rect.ts +158 -0
- package/src/rich-text-editor/utils/setFloatingElemPosition.ts +46 -0
- package/src/rich-text-editor/utils/swipe.ts +127 -0
- package/src/rich-text-editor/utils/url.ts +33 -0
- package/src/Tokens.stories.tsx +0 -18
|
@@ -0,0 +1,481 @@
|
|
|
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 * as React from 'react';
|
|
10
|
+
import { ReactPortal, useCallback, useEffect, useRef, useState } from 'react';
|
|
11
|
+
import { $getRoot, $getSelection, $isRangeSelection, DEPRECATED_$isGridSelection } from 'lexical';
|
|
12
|
+
import { createPortal } from 'react-dom';
|
|
13
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
14
|
+
import useLexicalEditable from '@lexical/react/useLexicalEditable';
|
|
15
|
+
import {
|
|
16
|
+
$deleteTableColumn,
|
|
17
|
+
$getElementGridForTableNode,
|
|
18
|
+
$getTableCellNodeFromLexicalNode,
|
|
19
|
+
$getTableColumnIndexFromTableCellNode,
|
|
20
|
+
$getTableNodeFromLexicalNodeOrThrow,
|
|
21
|
+
$getTableRowIndexFromTableCellNode,
|
|
22
|
+
$insertTableColumn,
|
|
23
|
+
$insertTableRow,
|
|
24
|
+
$isTableCellNode,
|
|
25
|
+
$isTableRowNode,
|
|
26
|
+
$removeTableRowAtIndex,
|
|
27
|
+
getTableSelectionFromTableElement,
|
|
28
|
+
HTMLTableElementWithWithTableSelectionState,
|
|
29
|
+
TableCellHeaderStates,
|
|
30
|
+
TableCellNode,
|
|
31
|
+
} from '@lexical/table';
|
|
32
|
+
|
|
33
|
+
import { IconButton } from '../../../icon-button';
|
|
34
|
+
import { Icon } from '../../../iconography';
|
|
35
|
+
|
|
36
|
+
type TableCellActionMenuProps = Readonly<{
|
|
37
|
+
contextRef: { current: null | HTMLElement };
|
|
38
|
+
onClose: () => void;
|
|
39
|
+
setIsMenuOpen: (isOpen: boolean) => void;
|
|
40
|
+
tableCellNode: TableCellNode;
|
|
41
|
+
}>;
|
|
42
|
+
|
|
43
|
+
function TableActionMenu({
|
|
44
|
+
onClose,
|
|
45
|
+
tableCellNode: _tableCellNode,
|
|
46
|
+
setIsMenuOpen,
|
|
47
|
+
contextRef,
|
|
48
|
+
}: TableCellActionMenuProps) {
|
|
49
|
+
const [editor] = useLexicalComposerContext();
|
|
50
|
+
const dropDownRef = useRef<HTMLDivElement | null>(null);
|
|
51
|
+
const [tableCellNode, updateTableCellNode] = useState(_tableCellNode);
|
|
52
|
+
const [selectionCounts, updateSelectionCounts] = useState({
|
|
53
|
+
columns: 1,
|
|
54
|
+
rows: 1,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
return editor.registerMutationListener(TableCellNode, nodeMutations => {
|
|
59
|
+
const nodeUpdated = nodeMutations.get(tableCellNode.getKey()) === 'updated';
|
|
60
|
+
|
|
61
|
+
if (nodeUpdated) {
|
|
62
|
+
editor.getEditorState().read(() => {
|
|
63
|
+
updateTableCellNode(tableCellNode.getLatest());
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}, [editor, tableCellNode]);
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
editor.getEditorState().read(() => {
|
|
71
|
+
const selection = $getSelection();
|
|
72
|
+
|
|
73
|
+
if (DEPRECATED_$isGridSelection(selection)) {
|
|
74
|
+
const selectionShape = selection.getShape();
|
|
75
|
+
|
|
76
|
+
updateSelectionCounts({
|
|
77
|
+
columns: selectionShape.toX - selectionShape.fromX + 1,
|
|
78
|
+
rows: selectionShape.toY - selectionShape.fromY + 1,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}, [editor]);
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const menuButtonElement = contextRef.current;
|
|
86
|
+
const dropDownElement = dropDownRef.current;
|
|
87
|
+
|
|
88
|
+
if (menuButtonElement != null && dropDownElement != null) {
|
|
89
|
+
const menuButtonRect = menuButtonElement.getBoundingClientRect();
|
|
90
|
+
|
|
91
|
+
dropDownElement.style.opacity = '1';
|
|
92
|
+
|
|
93
|
+
dropDownElement.style.left = `${menuButtonRect.left + menuButtonRect.width + window.pageXOffset + 5}px`;
|
|
94
|
+
|
|
95
|
+
dropDownElement.style.top = `${menuButtonRect.top + window.pageYOffset}px`;
|
|
96
|
+
}
|
|
97
|
+
}, [contextRef, dropDownRef]);
|
|
98
|
+
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
function handleClickOutside(event: MouseEvent) {
|
|
101
|
+
if (
|
|
102
|
+
dropDownRef.current != null &&
|
|
103
|
+
contextRef.current != null &&
|
|
104
|
+
!dropDownRef.current.contains(event.target as Node) &&
|
|
105
|
+
!contextRef.current.contains(event.target as Node)
|
|
106
|
+
) {
|
|
107
|
+
setIsMenuOpen(false);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
window.addEventListener('click', handleClickOutside);
|
|
112
|
+
|
|
113
|
+
return () => window.removeEventListener('click', handleClickOutside);
|
|
114
|
+
}, [setIsMenuOpen, contextRef]);
|
|
115
|
+
|
|
116
|
+
const clearTableSelection = useCallback(() => {
|
|
117
|
+
editor.update(() => {
|
|
118
|
+
if (tableCellNode.isAttached()) {
|
|
119
|
+
const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
120
|
+
const tableElement = editor.getElementByKey(tableNode.getKey()) as HTMLTableElementWithWithTableSelectionState;
|
|
121
|
+
|
|
122
|
+
if (!tableElement) {
|
|
123
|
+
throw new Error('Expected to find tableElement in DOM');
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const tableSelection = getTableSelectionFromTableElement(tableElement);
|
|
127
|
+
if (tableSelection !== null) {
|
|
128
|
+
tableSelection.clearHighlight();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
tableNode.markDirty();
|
|
132
|
+
updateTableCellNode(tableCellNode.getLatest());
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const rootNode = $getRoot();
|
|
136
|
+
rootNode.selectStart();
|
|
137
|
+
});
|
|
138
|
+
}, [editor, tableCellNode]);
|
|
139
|
+
|
|
140
|
+
const insertTableRowAtSelection = useCallback(
|
|
141
|
+
(shouldInsertAfter: boolean) => {
|
|
142
|
+
editor.update(() => {
|
|
143
|
+
const selection = $getSelection();
|
|
144
|
+
|
|
145
|
+
const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
146
|
+
|
|
147
|
+
let tableRowIndex;
|
|
148
|
+
|
|
149
|
+
if (DEPRECATED_$isGridSelection(selection)) {
|
|
150
|
+
const selectionShape = selection.getShape();
|
|
151
|
+
tableRowIndex = shouldInsertAfter ? selectionShape.toY : selectionShape.fromY;
|
|
152
|
+
} else {
|
|
153
|
+
tableRowIndex = $getTableRowIndexFromTableCellNode(tableCellNode);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const grid = $getElementGridForTableNode(editor, tableNode);
|
|
157
|
+
|
|
158
|
+
$insertTableRow(tableNode, tableRowIndex, shouldInsertAfter, selectionCounts.rows, grid);
|
|
159
|
+
|
|
160
|
+
clearTableSelection();
|
|
161
|
+
|
|
162
|
+
onClose();
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
[editor, tableCellNode, selectionCounts.rows, clearTableSelection, onClose],
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const insertTableColumnAtSelection = useCallback(
|
|
169
|
+
(shouldInsertAfter: boolean) => {
|
|
170
|
+
editor.update(() => {
|
|
171
|
+
const selection = $getSelection();
|
|
172
|
+
|
|
173
|
+
const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
174
|
+
|
|
175
|
+
let tableColumnIndex;
|
|
176
|
+
|
|
177
|
+
if (DEPRECATED_$isGridSelection(selection)) {
|
|
178
|
+
const selectionShape = selection.getShape();
|
|
179
|
+
tableColumnIndex = shouldInsertAfter ? selectionShape.toX : selectionShape.fromX;
|
|
180
|
+
} else {
|
|
181
|
+
tableColumnIndex = $getTableColumnIndexFromTableCellNode(tableCellNode);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const grid = $getElementGridForTableNode(editor, tableNode);
|
|
185
|
+
|
|
186
|
+
$insertTableColumn(tableNode, tableColumnIndex, shouldInsertAfter, selectionCounts.columns, grid);
|
|
187
|
+
|
|
188
|
+
clearTableSelection();
|
|
189
|
+
|
|
190
|
+
onClose();
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
[editor, tableCellNode, selectionCounts.columns, clearTableSelection, onClose],
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
const deleteTableRowAtSelection = useCallback(() => {
|
|
197
|
+
editor.update(() => {
|
|
198
|
+
const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
199
|
+
const tableRowIndex = $getTableRowIndexFromTableCellNode(tableCellNode);
|
|
200
|
+
|
|
201
|
+
$removeTableRowAtIndex(tableNode, tableRowIndex);
|
|
202
|
+
|
|
203
|
+
clearTableSelection();
|
|
204
|
+
onClose();
|
|
205
|
+
});
|
|
206
|
+
}, [editor, tableCellNode, clearTableSelection, onClose]);
|
|
207
|
+
|
|
208
|
+
const deleteTableAtSelection = useCallback(() => {
|
|
209
|
+
editor.update(() => {
|
|
210
|
+
const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
211
|
+
tableNode.remove();
|
|
212
|
+
|
|
213
|
+
clearTableSelection();
|
|
214
|
+
onClose();
|
|
215
|
+
});
|
|
216
|
+
}, [editor, tableCellNode, clearTableSelection, onClose]);
|
|
217
|
+
|
|
218
|
+
const deleteTableColumnAtSelection = useCallback(() => {
|
|
219
|
+
editor.update(() => {
|
|
220
|
+
const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
221
|
+
|
|
222
|
+
const tableColumnIndex = $getTableColumnIndexFromTableCellNode(tableCellNode);
|
|
223
|
+
|
|
224
|
+
$deleteTableColumn(tableNode, tableColumnIndex);
|
|
225
|
+
|
|
226
|
+
clearTableSelection();
|
|
227
|
+
onClose();
|
|
228
|
+
});
|
|
229
|
+
}, [editor, tableCellNode, clearTableSelection, onClose]);
|
|
230
|
+
|
|
231
|
+
const toggleTableRowIsHeader = useCallback(() => {
|
|
232
|
+
editor.update(() => {
|
|
233
|
+
const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
234
|
+
|
|
235
|
+
const tableRowIndex = $getTableRowIndexFromTableCellNode(tableCellNode);
|
|
236
|
+
|
|
237
|
+
const tableRows = tableNode.getChildren();
|
|
238
|
+
|
|
239
|
+
if (tableRowIndex >= tableRows.length || tableRowIndex < 0) {
|
|
240
|
+
throw new Error('Expected table cell to be inside of table row.');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const tableRow = tableRows[tableRowIndex];
|
|
244
|
+
|
|
245
|
+
if (!$isTableRowNode(tableRow)) {
|
|
246
|
+
throw new Error('Expected table row');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
tableRow.getChildren().forEach(tableCell => {
|
|
250
|
+
if (!$isTableCellNode(tableCell)) {
|
|
251
|
+
throw new Error('Expected table cell');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
tableCell.toggleHeaderStyle(TableCellHeaderStates.ROW);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
clearTableSelection();
|
|
258
|
+
onClose();
|
|
259
|
+
});
|
|
260
|
+
}, [editor, tableCellNode, clearTableSelection, onClose]);
|
|
261
|
+
|
|
262
|
+
const toggleTableColumnIsHeader = useCallback(() => {
|
|
263
|
+
editor.update(() => {
|
|
264
|
+
const tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
265
|
+
|
|
266
|
+
const tableColumnIndex = $getTableColumnIndexFromTableCellNode(tableCellNode);
|
|
267
|
+
|
|
268
|
+
const tableRows = tableNode.getChildren();
|
|
269
|
+
|
|
270
|
+
for (let r = 0; r < tableRows.length; r++) {
|
|
271
|
+
const tableRow = tableRows[r];
|
|
272
|
+
|
|
273
|
+
if (!$isTableRowNode(tableRow)) {
|
|
274
|
+
throw new Error('Expected table row');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const tableCells = tableRow.getChildren();
|
|
278
|
+
|
|
279
|
+
if (tableColumnIndex >= tableCells.length || tableColumnIndex < 0) {
|
|
280
|
+
throw new Error('Expected table cell to be inside of table row.');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const tableCell = tableCells[tableColumnIndex];
|
|
284
|
+
|
|
285
|
+
if (!$isTableCellNode(tableCell)) {
|
|
286
|
+
throw new Error('Expected table cell');
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
tableCell.toggleHeaderStyle(TableCellHeaderStates.COLUMN);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
clearTableSelection();
|
|
293
|
+
onClose();
|
|
294
|
+
});
|
|
295
|
+
}, [editor, tableCellNode, clearTableSelection, onClose]);
|
|
296
|
+
|
|
297
|
+
return createPortal(
|
|
298
|
+
<div
|
|
299
|
+
className="dropdown"
|
|
300
|
+
ref={dropDownRef}
|
|
301
|
+
onClick={e => {
|
|
302
|
+
e.stopPropagation();
|
|
303
|
+
}}
|
|
304
|
+
>
|
|
305
|
+
<button className="item" onClick={() => insertTableRowAtSelection(false)}>
|
|
306
|
+
<span className="text-sm">
|
|
307
|
+
Insert {selectionCounts.rows === 1 ? 'row' : `${selectionCounts.rows} rows`} above
|
|
308
|
+
</span>
|
|
309
|
+
</button>
|
|
310
|
+
<button className="item" onClick={() => insertTableRowAtSelection(true)}>
|
|
311
|
+
<span className="text-sm">
|
|
312
|
+
Insert {selectionCounts.rows === 1 ? 'row' : `${selectionCounts.rows} rows`} below
|
|
313
|
+
</span>
|
|
314
|
+
</button>
|
|
315
|
+
<hr className="my-1" />
|
|
316
|
+
<button className="item" onClick={() => insertTableColumnAtSelection(false)}>
|
|
317
|
+
<span className="text-sm">
|
|
318
|
+
Insert {selectionCounts.columns === 1 ? 'column' : `${selectionCounts.columns} columns`} left
|
|
319
|
+
</span>
|
|
320
|
+
</button>
|
|
321
|
+
<button className="item" onClick={() => insertTableColumnAtSelection(true)}>
|
|
322
|
+
<span className="text-sm">
|
|
323
|
+
Insert {selectionCounts.columns === 1 ? 'column' : `${selectionCounts.columns} columns`} right
|
|
324
|
+
</span>
|
|
325
|
+
</button>
|
|
326
|
+
<hr className="my-1" />
|
|
327
|
+
<button className="item" onClick={() => deleteTableColumnAtSelection()}>
|
|
328
|
+
<span className="text-sm">Delete column</span>
|
|
329
|
+
</button>
|
|
330
|
+
<button className="item" onClick={() => deleteTableRowAtSelection()}>
|
|
331
|
+
<span className="text-sm">Delete row</span>
|
|
332
|
+
</button>
|
|
333
|
+
<button className="item" onClick={() => deleteTableAtSelection()}>
|
|
334
|
+
<span className="text-sm">Delete table</span>
|
|
335
|
+
</button>
|
|
336
|
+
<hr className="my-1" />
|
|
337
|
+
<button className="item" onClick={() => toggleTableRowIsHeader()}>
|
|
338
|
+
<span className="text-sm">
|
|
339
|
+
{(tableCellNode.__headerState & TableCellHeaderStates.ROW) === TableCellHeaderStates.ROW ? 'Remove' : 'Add'}{' '}
|
|
340
|
+
row header
|
|
341
|
+
</span>
|
|
342
|
+
</button>
|
|
343
|
+
<button className="item" onClick={() => toggleTableColumnIsHeader()}>
|
|
344
|
+
<span className="text-sm">
|
|
345
|
+
{(tableCellNode.__headerState & TableCellHeaderStates.COLUMN) === TableCellHeaderStates.COLUMN
|
|
346
|
+
? 'Remove'
|
|
347
|
+
: 'Add'}{' '}
|
|
348
|
+
column header
|
|
349
|
+
</span>
|
|
350
|
+
</button>
|
|
351
|
+
</div>,
|
|
352
|
+
document.body,
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function TableCellActionMenuContainer({ anchorElem }: { anchorElem: HTMLElement }): JSX.Element {
|
|
357
|
+
const [editor] = useLexicalComposerContext();
|
|
358
|
+
|
|
359
|
+
const menuButtonRef = useRef(null);
|
|
360
|
+
const menuRootRef = useRef(null);
|
|
361
|
+
const [isMenuOpen, setIsMenuOpen] = useState(false);
|
|
362
|
+
|
|
363
|
+
const [tableCellNode, setTableMenuCellNode] = useState<TableCellNode | null>(null);
|
|
364
|
+
|
|
365
|
+
const moveMenu = useCallback(() => {
|
|
366
|
+
const menu = menuButtonRef.current;
|
|
367
|
+
const selection = $getSelection();
|
|
368
|
+
const nativeSelection = window.getSelection();
|
|
369
|
+
const activeElement = document.activeElement;
|
|
370
|
+
|
|
371
|
+
if (selection == null || menu == null) {
|
|
372
|
+
setTableMenuCellNode(null);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const rootElement = editor.getRootElement();
|
|
377
|
+
|
|
378
|
+
if (
|
|
379
|
+
$isRangeSelection(selection) &&
|
|
380
|
+
rootElement !== null &&
|
|
381
|
+
nativeSelection !== null &&
|
|
382
|
+
rootElement.contains(nativeSelection.anchorNode)
|
|
383
|
+
) {
|
|
384
|
+
const tableCellNodeFromSelection = $getTableCellNodeFromLexicalNode(selection.anchor.getNode());
|
|
385
|
+
|
|
386
|
+
if (tableCellNodeFromSelection == null) {
|
|
387
|
+
setTableMenuCellNode(null);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const tableCellParentNodeDOM = editor.getElementByKey(tableCellNodeFromSelection.getKey());
|
|
392
|
+
|
|
393
|
+
if (tableCellParentNodeDOM == null) {
|
|
394
|
+
setTableMenuCellNode(null);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
setTableMenuCellNode(tableCellNodeFromSelection);
|
|
399
|
+
} else if (!activeElement) {
|
|
400
|
+
setTableMenuCellNode(null);
|
|
401
|
+
}
|
|
402
|
+
}, [editor]);
|
|
403
|
+
|
|
404
|
+
useEffect(() => {
|
|
405
|
+
return editor.registerUpdateListener(() => {
|
|
406
|
+
editor.getEditorState().read(() => {
|
|
407
|
+
moveMenu();
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
useEffect(() => {
|
|
413
|
+
const menuButtonDOM = menuButtonRef.current as HTMLButtonElement | null;
|
|
414
|
+
|
|
415
|
+
if (menuButtonDOM != null && tableCellNode != null) {
|
|
416
|
+
const tableCellNodeDOM = editor.getElementByKey(tableCellNode.getKey());
|
|
417
|
+
|
|
418
|
+
if (tableCellNodeDOM != null) {
|
|
419
|
+
const tableCellRect = tableCellNodeDOM.getBoundingClientRect();
|
|
420
|
+
const menuRect = menuButtonDOM.getBoundingClientRect();
|
|
421
|
+
const anchorRect = anchorElem.getBoundingClientRect();
|
|
422
|
+
|
|
423
|
+
const top = tableCellRect.top - anchorRect.top + 4;
|
|
424
|
+
const left = tableCellRect.right - menuRect.width - 10 - anchorRect.left;
|
|
425
|
+
|
|
426
|
+
menuButtonDOM.style.opacity = '1';
|
|
427
|
+
menuButtonDOM.style.transform = `translate(${left}px, ${top}px)`;
|
|
428
|
+
} else {
|
|
429
|
+
menuButtonDOM.style.opacity = '0';
|
|
430
|
+
menuButtonDOM.style.transform = 'translate(-10000px, -10000px)';
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}, [menuButtonRef, tableCellNode, editor, anchorElem]);
|
|
434
|
+
|
|
435
|
+
const prevTableCellDOM = useRef(tableCellNode);
|
|
436
|
+
|
|
437
|
+
useEffect(() => {
|
|
438
|
+
if (prevTableCellDOM.current !== tableCellNode) {
|
|
439
|
+
setIsMenuOpen(false);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
prevTableCellDOM.current = tableCellNode;
|
|
443
|
+
}, [prevTableCellDOM, tableCellNode]);
|
|
444
|
+
|
|
445
|
+
return (
|
|
446
|
+
<div className="table-cell-action-button-container" ref={menuButtonRef}>
|
|
447
|
+
{tableCellNode != null && (
|
|
448
|
+
<>
|
|
449
|
+
<IconButton
|
|
450
|
+
size="xs"
|
|
451
|
+
className="table-cell-action-button chevron-down"
|
|
452
|
+
onClick={e => {
|
|
453
|
+
e.stopPropagation();
|
|
454
|
+
setIsMenuOpen(!isMenuOpen);
|
|
455
|
+
}}
|
|
456
|
+
ref={menuRootRef}
|
|
457
|
+
>
|
|
458
|
+
<Icon.Arrow />
|
|
459
|
+
</IconButton>
|
|
460
|
+
{isMenuOpen && (
|
|
461
|
+
<TableActionMenu
|
|
462
|
+
contextRef={menuRootRef}
|
|
463
|
+
setIsMenuOpen={setIsMenuOpen}
|
|
464
|
+
onClose={() => setIsMenuOpen(false)}
|
|
465
|
+
tableCellNode={tableCellNode}
|
|
466
|
+
/>
|
|
467
|
+
)}
|
|
468
|
+
</>
|
|
469
|
+
)}
|
|
470
|
+
</div>
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
export default function TableActionMenuPlugin({
|
|
475
|
+
anchorElem = document.body,
|
|
476
|
+
}: {
|
|
477
|
+
anchorElem?: HTMLElement;
|
|
478
|
+
}): null | ReactPortal {
|
|
479
|
+
const isEditable = useLexicalEditable();
|
|
480
|
+
return createPortal(isEditable ? <TableCellActionMenuContainer anchorElem={anchorElem} /> : null, anchorElem);
|
|
481
|
+
}
|
|
@@ -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
|
+
}
|