@lobehub/editor 1.8.2 → 1.8.4
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/es/editor-kernel/react/PortalAnchor.d.ts +5 -0
- package/es/editor-kernel/react/PortalAnchor.js +13 -0
- package/es/editor-kernel/react/useAnchor.d.ts +1 -0
- package/es/editor-kernel/react/useAnchor.js +23 -0
- package/es/plugins/link/react/ReactLinkPlugin.js +9 -104
- package/es/plugins/link/react/components/LinkEdit.d.ts +6 -2
- package/es/plugins/link/react/components/LinkEdit.js +49 -94
- package/es/plugins/link/react/components/LinkToolbar.d.ts +1 -4
- package/es/plugins/link/react/components/LinkToolbar.js +87 -21
- package/es/plugins/link/react/style.js +1 -1
- package/es/plugins/math/react/{component → components}/MathEditor.js +11 -8
- package/es/plugins/math/react/{component → components}/MathEditorContainer.d.ts +2 -1
- package/es/plugins/math/react/{component → components}/MathEditorContainer.js +27 -31
- package/es/plugins/math/react/{component → components}/MathEditorContent.d.ts +2 -1
- package/es/plugins/math/react/{component → components}/MathEditorContent.js +3 -2
- package/es/plugins/math/react/index.js +2 -2
- package/es/plugins/math/react/style.js +1 -1
- package/es/plugins/slash/react/components/DefaultSlashMenu.d.ts +5 -0
- package/es/plugins/slash/react/components/DefaultSlashMenu.js +48 -0
- package/es/plugins/slash/react/components/SlashMenu.d.ts +2 -2
- package/es/plugins/slash/react/components/SlashMenu.js +16 -36
- package/es/plugins/table/react/TableActionMenu/ActionMenu.d.ts +19 -0
- package/es/plugins/table/react/TableActionMenu/ActionMenu.js +285 -0
- package/es/plugins/table/react/TableActionMenu/index.d.ts +2 -3
- package/es/plugins/table/react/TableActionMenu/index.js +27 -329
- package/es/plugins/table/react/TableActionMenu/style.js +1 -1
- package/es/plugins/table/react/TableHoverActions/index.d.ts +3 -4
- package/es/plugins/table/react/TableHoverActions/index.js +7 -20
- package/es/plugins/table/react/index.js +8 -5
- package/es/utils/updatePosition.d.ts +9 -0
- package/es/utils/updatePosition.js +27 -0
- package/package.json +1 -1
- /package/es/plugins/math/react/{component → components}/MathEditor.d.ts +0 -0
- /package/es/plugins/math/react/{component → components}/MathInline.d.ts +0 -0
- /package/es/plugins/math/react/{component → components}/MathInline.js +0 -0
- /package/es/plugins/math/react/{component → components}/Placeholder.d.ts +0 -0
- /package/es/plugins/math/react/{component → components}/Placeholder.js +0 -0
|
@@ -8,10 +8,11 @@ import { mergeRegister } from '@lexical/utils';
|
|
|
8
8
|
import { $getSelection, $isElementNode, $isNodeSelection, $isRangeSelection, $isTextNode } from 'lexical';
|
|
9
9
|
import { memo, useCallback, useEffect, useRef, useState } from 'react';
|
|
10
10
|
import { useLexicalComposerContext, useLexicalEditor } from "../../../../editor-kernel/react";
|
|
11
|
+
import PortalAnchor from "../../../../editor-kernel/react/PortalAnchor";
|
|
11
12
|
import { SELECT_MATH_SIDE_COMMAND, UPDATE_MATH_COMMAND } from "../../command";
|
|
12
13
|
import { $isMathNode, MathBlockNode } from "../../node";
|
|
13
|
-
import
|
|
14
|
-
import
|
|
14
|
+
import MathEditorContainer from "./MathEditorContainer";
|
|
15
|
+
import MathEditorContent from "./MathEditorContent";
|
|
15
16
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
16
17
|
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
17
18
|
var MathEdit = /*#__PURE__*/memo(function (_ref) {
|
|
@@ -294,12 +295,14 @@ var MathEdit = /*#__PURE__*/memo(function (_ref) {
|
|
|
294
295
|
}
|
|
295
296
|
|
|
296
297
|
// 否则使用默认的 MathEditorContainer
|
|
297
|
-
return /*#__PURE__*/_jsx(
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
298
|
+
return /*#__PURE__*/_jsx(PortalAnchor, {
|
|
299
|
+
children: /*#__PURE__*/_jsx(MathEditorContainer, {
|
|
300
|
+
isBlockMode: isBlockMode,
|
|
301
|
+
mathDOM: mathDOM,
|
|
302
|
+
onFocus: handleFocus,
|
|
303
|
+
prev: prev,
|
|
304
|
+
children: mathEditorContent
|
|
305
|
+
})
|
|
303
306
|
});
|
|
304
307
|
});
|
|
305
308
|
export default MathEdit;
|
|
@@ -11,4 +11,5 @@ export interface MathEditorContainerProps {
|
|
|
11
11
|
/** 是否从前一个位置进入 */
|
|
12
12
|
prev: boolean;
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
declare const MathEditorContainer: import("react").NamedExoticComponent<MathEditorContainerProps>;
|
|
15
|
+
export default MathEditorContainer;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { computePosition, flip, offset, shift } from '@floating-ui/dom';
|
|
2
1
|
import { Block } from '@lobehub/ui';
|
|
3
2
|
import { memo, useEffect, useRef } from 'react';
|
|
3
|
+
import { cleanPosition, updatePosition } from "../../../../utils/updatePosition";
|
|
4
4
|
import { useStyles } from "../style";
|
|
5
5
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
6
|
-
|
|
6
|
+
var MathEditorContainer = /*#__PURE__*/memo(function (_ref) {
|
|
7
7
|
var children = _ref.children,
|
|
8
8
|
isBlockMode = _ref.isBlockMode,
|
|
9
9
|
mathDOM = _ref.mathDOM,
|
|
@@ -12,34 +12,31 @@ export var MathEditorContainer = /*#__PURE__*/memo(function (_ref) {
|
|
|
12
12
|
var divRef = useRef(null);
|
|
13
13
|
var _useStyles = useStyles(),
|
|
14
14
|
styles = _useStyles.styles;
|
|
15
|
+
var updateMathPosition = function updateMathPosition() {
|
|
16
|
+
return updatePosition({
|
|
17
|
+
callback: function callback() {
|
|
18
|
+
if (!divRef.current || !mathDOM) return;
|
|
19
|
+
if (!isBlockMode) {
|
|
20
|
+
divRef.current.style.width = '';
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
var editorContainer = mathDOM.closest('[contenteditable="true"]');
|
|
24
|
+
if (editorContainer) {
|
|
25
|
+
var containerRect = editorContainer.getBoundingClientRect();
|
|
26
|
+
divRef.current.style.width = "".concat(containerRect.width, "px");
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
floating: divRef.current,
|
|
30
|
+
reference: mathDOM
|
|
31
|
+
});
|
|
32
|
+
};
|
|
15
33
|
useEffect(function () {
|
|
16
34
|
if (!mathDOM || !divRef.current) return;
|
|
17
35
|
var floating = divRef.current;
|
|
18
|
-
var updatePosition = function updatePosition() {
|
|
19
|
-
if (!mathDOM || !floating) return;
|
|
20
|
-
computePosition(mathDOM, floating, {
|
|
21
|
-
middleware: [offset(8), flip(), shift()],
|
|
22
|
-
placement: 'bottom-start'
|
|
23
|
-
}).then(function (_ref2) {
|
|
24
|
-
var x = _ref2.x,
|
|
25
|
-
y = _ref2.y;
|
|
26
|
-
if (!floating) return;
|
|
27
|
-
floating.style.left = "".concat(x, "px");
|
|
28
|
-
floating.style.top = "".concat(y, "px");
|
|
29
|
-
floating.style.width = '';
|
|
30
|
-
if (isBlockMode) {
|
|
31
|
-
var _editorContainer = mathDOM.closest('[contenteditable="true"]');
|
|
32
|
-
if (_editorContainer) {
|
|
33
|
-
var containerRect = _editorContainer.getBoundingClientRect();
|
|
34
|
-
floating.style.width = "".concat(containerRect.width, "px");
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
36
|
|
|
40
37
|
// 监听尺寸变化,随内容变化重新定位
|
|
41
38
|
var resizeObserver = new ResizeObserver(function () {
|
|
42
|
-
return
|
|
39
|
+
return updateMathPosition();
|
|
43
40
|
});
|
|
44
41
|
resizeObserver.observe(mathDOM);
|
|
45
42
|
resizeObserver.observe(floating);
|
|
@@ -50,19 +47,17 @@ export var MathEditorContainer = /*#__PURE__*/memo(function (_ref) {
|
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
// 窗口尺寸变化时也重新定位
|
|
53
|
-
window.addEventListener('resize',
|
|
50
|
+
window.addEventListener('resize', updateMathPosition);
|
|
54
51
|
return function () {
|
|
55
52
|
resizeObserver.disconnect();
|
|
56
|
-
window.removeEventListener('resize',
|
|
53
|
+
window.removeEventListener('resize', updateMathPosition);
|
|
57
54
|
};
|
|
58
55
|
}, [mathDOM, prev, isBlockMode, onFocus]);
|
|
59
56
|
|
|
60
57
|
// 当没有 mathDOM 时,隐藏容器
|
|
61
58
|
useEffect(function () {
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
divRef.current.style.top = "-9999px";
|
|
65
|
-
}
|
|
59
|
+
if (mathDOM || !divRef.current) return;
|
|
60
|
+
cleanPosition(divRef.current);
|
|
66
61
|
}, [mathDOM]);
|
|
67
62
|
return /*#__PURE__*/_jsx(Block, {
|
|
68
63
|
className: styles.mathEditor,
|
|
@@ -76,4 +71,5 @@ export var MathEditorContainer = /*#__PURE__*/memo(function (_ref) {
|
|
|
76
71
|
children: children
|
|
77
72
|
});
|
|
78
73
|
});
|
|
79
|
-
MathEditorContainer.displayName = 'MathEditorContainer';
|
|
74
|
+
MathEditorContainer.displayName = 'MathEditorContainer';
|
|
75
|
+
export default MathEditorContainer;
|
|
@@ -25,4 +25,5 @@ export interface MathEditorContentProps {
|
|
|
25
25
|
/** 当前输入值 */
|
|
26
26
|
value: string;
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
declare const MathEditorContent: import("react").NamedExoticComponent<MathEditorContentProps>;
|
|
29
|
+
export default MathEditorContent;
|
|
@@ -15,7 +15,7 @@ import { useStyles } from "../style";
|
|
|
15
15
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
16
16
|
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
17
17
|
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
18
|
-
|
|
18
|
+
var MathEditorContent = /*#__PURE__*/memo(function (_ref) {
|
|
19
19
|
var focusRef = _ref.focusRef,
|
|
20
20
|
mathNode = _ref.mathNode,
|
|
21
21
|
onArrowLeft = _ref.onArrowLeft,
|
|
@@ -164,4 +164,5 @@ export var MathEditorContent = /*#__PURE__*/memo(function (_ref) {
|
|
|
164
164
|
})]
|
|
165
165
|
});
|
|
166
166
|
});
|
|
167
|
-
MathEditorContent.displayName = 'MathEditorContent';
|
|
167
|
+
MathEditorContent.displayName = 'MathEditorContent';
|
|
168
|
+
export default MathEditorContent;
|
|
@@ -10,8 +10,8 @@ import { useLayoutEffect } from 'react';
|
|
|
10
10
|
import { useLexicalComposerContext } from "../../../editor-kernel/react/react-context";
|
|
11
11
|
import { MarkdownPlugin } from "../../markdown";
|
|
12
12
|
import { MathPlugin } from "../plugin";
|
|
13
|
-
import MathEdit from "./
|
|
14
|
-
import MathInline from "./
|
|
13
|
+
import MathEdit from "./components/MathEditor";
|
|
14
|
+
import MathInline from "./components/MathInline";
|
|
15
15
|
import { useStyles } from "./style";
|
|
16
16
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
17
17
|
export var ReactMathPlugin = function ReactMathPlugin(_ref) {
|
|
@@ -10,7 +10,7 @@ export var useStyles = createStyles(function (_ref) {
|
|
|
10
10
|
return {
|
|
11
11
|
mathInline: cx(latex, css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n display: inline-block;\n border-radius: calc(var(--lobe-markdown-border-radius) * 0.5px);\n "])))),
|
|
12
12
|
mathBlock: cx(latex, css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n overflow: auto hidden;\n display: block;\n border-radius: calc(var(--lobe-markdown-border-radius) * 1px);\n white-space: nowrap;\n "])))),
|
|
13
|
-
mathEditor: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n position: absolute;\n z-index: 999;\n
|
|
13
|
+
mathEditor: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n position: absolute;\n z-index: 999;\n width: 320px;\n background: ", ";\n\n textarea {\n width: 100%;\n\n font-family: ", ";\n font-size: 13px;\n\n background: transparent !important;\n\n transition: none !important;\n }\n "])), token.colorBgElevated, token.fontFamilyCode),
|
|
14
14
|
mathEditorFooter: css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n border-block-start: 1px solid ", ";\n background: ", ";\n "])), token.colorBorderSecondary, token.colorFillQuaternary)
|
|
15
15
|
};
|
|
16
16
|
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { SlashMenuProps } from '../type';
|
|
3
|
+
type DefaultSlashMenuProps = Omit<SlashMenuProps, 'customRender' | 'onActiveKeyChange' | 'editor'>;
|
|
4
|
+
declare const DefaultSlashMenu: import("react").NamedExoticComponent<DefaultSlashMenuProps>;
|
|
5
|
+
export default DefaultSlashMenu;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { Dropdown } from '@lobehub/ui';
|
|
4
|
+
import { memo, useCallback } from 'react';
|
|
5
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
6
|
+
var DefaultSlashMenu = /*#__PURE__*/memo(function (_ref) {
|
|
7
|
+
var activeKey = _ref.activeKey,
|
|
8
|
+
anchorClassName = _ref.anchorClassName,
|
|
9
|
+
loading = _ref.loading,
|
|
10
|
+
onSelect = _ref.onSelect,
|
|
11
|
+
open = _ref.open,
|
|
12
|
+
options = _ref.options,
|
|
13
|
+
position = _ref.position,
|
|
14
|
+
_onClose = _ref.onClose;
|
|
15
|
+
var handleMenuClick = useCallback(function (_ref2) {
|
|
16
|
+
var key = _ref2.key;
|
|
17
|
+
var option = options.find(function (item) {
|
|
18
|
+
return 'key' in item && item.key === key;
|
|
19
|
+
});
|
|
20
|
+
if (option) onSelect(option);
|
|
21
|
+
}, [options, onSelect]);
|
|
22
|
+
return /*#__PURE__*/_jsx("div", {
|
|
23
|
+
style: {
|
|
24
|
+
left: position.x,
|
|
25
|
+
position: 'fixed',
|
|
26
|
+
top: position.y,
|
|
27
|
+
zIndex: 9999
|
|
28
|
+
},
|
|
29
|
+
children: /*#__PURE__*/_jsx(Dropdown, {
|
|
30
|
+
menu: {
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
activeKey: activeKey,
|
|
33
|
+
items: loading ? [{
|
|
34
|
+
disabled: true,
|
|
35
|
+
key: 'loading',
|
|
36
|
+
label: 'Loading...'
|
|
37
|
+
}] : options,
|
|
38
|
+
onClick: handleMenuClick
|
|
39
|
+
},
|
|
40
|
+
open: open,
|
|
41
|
+
children: /*#__PURE__*/_jsx("span", {
|
|
42
|
+
className: anchorClassName
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
DefaultSlashMenu.displayName = 'DefaultSlashMenu';
|
|
48
|
+
export default DefaultSlashMenu;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="react" />
|
|
2
2
|
import type { SlashMenuProps } from '../type';
|
|
3
3
|
/**
|
|
4
4
|
* SlashMenu component - Only responsible for rendering the menu UI
|
|
5
5
|
* All state management and plugin registration is handled by ReactSlashPlugin
|
|
6
6
|
*/
|
|
7
|
-
declare const SlashMenu:
|
|
7
|
+
declare const SlashMenu: import("react").NamedExoticComponent<SlashMenuProps>;
|
|
8
8
|
export default SlashMenu;
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
|
|
3
|
+
import { memo, useCallback } from 'react';
|
|
4
|
+
import DefaultSlashMenu from "./DefaultSlashMenu";
|
|
5
|
+
|
|
6
6
|
/**
|
|
7
7
|
* SlashMenu component - Only responsible for rendering the menu UI
|
|
8
8
|
* All state management and plugin registration is handled by ReactSlashPlugin
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
11
|
+
var SlashMenu = /*#__PURE__*/memo(function (_ref) {
|
|
11
12
|
var activeKey = _ref.activeKey,
|
|
12
13
|
anchorClassName = _ref.anchorClassName,
|
|
13
14
|
CustomRender = _ref.customRender,
|
|
@@ -17,18 +18,11 @@ var SlashMenu = function SlashMenu(_ref) {
|
|
|
17
18
|
open = _ref.open,
|
|
18
19
|
options = _ref.options,
|
|
19
20
|
position = _ref.position,
|
|
20
|
-
|
|
21
|
+
onClose = _ref.onClose;
|
|
21
22
|
// Adapter for custom render component onSelect
|
|
22
23
|
var customRenderOnSelect = useCallback(function (option) {
|
|
23
24
|
onSelect(option);
|
|
24
25
|
}, [onSelect]);
|
|
25
|
-
var handleMenuClick = useCallback(function (_ref2) {
|
|
26
|
-
var key = _ref2.key;
|
|
27
|
-
var option = options.find(function (item) {
|
|
28
|
-
return 'key' in item && item.key === key;
|
|
29
|
-
});
|
|
30
|
-
if (option) onSelect(option);
|
|
31
|
-
}, [options, onSelect]);
|
|
32
26
|
|
|
33
27
|
/**
|
|
34
28
|
* Render the custom component if it exists
|
|
@@ -43,30 +37,16 @@ var SlashMenu = function SlashMenu(_ref) {
|
|
|
43
37
|
setActiveKey: onActiveKeyChange
|
|
44
38
|
});
|
|
45
39
|
}
|
|
46
|
-
return /*#__PURE__*/_jsx(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
// @ts-ignore
|
|
56
|
-
activeKey: activeKey,
|
|
57
|
-
items: loading ? [{
|
|
58
|
-
disabled: true,
|
|
59
|
-
key: 'loading',
|
|
60
|
-
label: 'Loading...'
|
|
61
|
-
}] : options,
|
|
62
|
-
onClick: handleMenuClick
|
|
63
|
-
},
|
|
64
|
-
open: open,
|
|
65
|
-
children: /*#__PURE__*/_jsx("span", {
|
|
66
|
-
className: anchorClassName
|
|
67
|
-
})
|
|
68
|
-
})
|
|
40
|
+
return /*#__PURE__*/_jsx(DefaultSlashMenu, {
|
|
41
|
+
activeKey: activeKey,
|
|
42
|
+
anchorClassName: anchorClassName,
|
|
43
|
+
loading: loading,
|
|
44
|
+
onClose: onClose,
|
|
45
|
+
onSelect: onSelect,
|
|
46
|
+
open: open,
|
|
47
|
+
options: options,
|
|
48
|
+
position: position
|
|
69
49
|
});
|
|
70
|
-
};
|
|
50
|
+
});
|
|
71
51
|
SlashMenu.displayName = 'SlashMenu';
|
|
72
52
|
export default SlashMenu;
|
|
@@ -0,0 +1,19 @@
|
|
|
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 { TableCellNode } from '@lexical/table';
|
|
9
|
+
import type { LexicalEditor } from 'lexical';
|
|
10
|
+
import { type ReactNode } from 'react';
|
|
11
|
+
interface TableCellActionMenuProps {
|
|
12
|
+
cellMerge?: boolean;
|
|
13
|
+
children: ReactNode;
|
|
14
|
+
className?: string;
|
|
15
|
+
editor: LexicalEditor;
|
|
16
|
+
tableCellNode: TableCellNode;
|
|
17
|
+
}
|
|
18
|
+
declare const TableActionMenu: import("react").NamedExoticComponent<TableCellActionMenuProps>;
|
|
19
|
+
export default TableActionMenu;
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
2
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
3
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
5
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
6
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
7
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
8
|
+
/**
|
|
9
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
10
|
+
*
|
|
11
|
+
* This source code is licensed under the MIT license found in the
|
|
12
|
+
* LICENSE file in the root directory of this source tree.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
import { $computeTableMapSkipCellCheck, $deleteTableColumnAtSelection, $deleteTableRowAtSelection, $getTableColumnIndexFromTableCellNode, $getTableNodeFromLexicalNodeOrThrow, $getTableRowIndexFromTableCellNode, $insertTableColumnAtSelection, $insertTableRowAtSelection, $isTableCellNode, $isTableSelection, $mergeCells, $unmergeCell, TableCellHeaderStates, TableCellNode, getTableElement, getTableObserverFromTableElement } from '@lexical/table';
|
|
16
|
+
import { Dropdown } from '@lobehub/ui';
|
|
17
|
+
import { $getSelection, $setSelection } from 'lexical';
|
|
18
|
+
import { Grid2X2XIcon, PanelBottomCloseIcon, PanelLeftCloseIcon, PanelRightCloseIcon, PanelTopCloseIcon, TableColumnsSplitIcon, TableRowsSplitIcon } from 'lucide-react';
|
|
19
|
+
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
|
|
20
|
+
import { useTranslation } from "../../../../editor-kernel/react/useTranslation";
|
|
21
|
+
import { $canUnmerge, $selectLastDescendant, computeSelectionCount, currentCellBackgroundColor } from "./utils";
|
|
22
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
23
|
+
var TableActionMenu = /*#__PURE__*/memo(function (_ref) {
|
|
24
|
+
var editor = _ref.editor,
|
|
25
|
+
_tableCellNode = _ref.tableCellNode,
|
|
26
|
+
cellMerge = _ref.cellMerge,
|
|
27
|
+
children = _ref.children;
|
|
28
|
+
var _useState = useState(_tableCellNode),
|
|
29
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
30
|
+
tableCellNode = _useState2[0],
|
|
31
|
+
updateTableCellNode = _useState2[1];
|
|
32
|
+
var _useState3 = useState({
|
|
33
|
+
columns: 1,
|
|
34
|
+
rows: 1
|
|
35
|
+
}),
|
|
36
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
37
|
+
selectionCounts = _useState4[0],
|
|
38
|
+
updateSelectionCounts = _useState4[1];
|
|
39
|
+
var _useState5 = useState(false),
|
|
40
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
41
|
+
canMergeCells = _useState6[0],
|
|
42
|
+
setCanMergeCells = _useState6[1];
|
|
43
|
+
var _useState7 = useState(false),
|
|
44
|
+
_useState8 = _slicedToArray(_useState7, 2),
|
|
45
|
+
canUnmergeCell = _useState8[0],
|
|
46
|
+
setCanUnmergeCell = _useState8[1];
|
|
47
|
+
var _useState9 = useState(function () {
|
|
48
|
+
return currentCellBackgroundColor(editor) || '';
|
|
49
|
+
}),
|
|
50
|
+
_useState10 = _slicedToArray(_useState9, 2),
|
|
51
|
+
setBackgroundColor = _useState10[1];
|
|
52
|
+
var t = useTranslation();
|
|
53
|
+
useEffect(function () {
|
|
54
|
+
return editor.registerMutationListener(TableCellNode, function (nodeMutations) {
|
|
55
|
+
var nodeUpdated = nodeMutations.get(tableCellNode.getKey()) === 'updated';
|
|
56
|
+
if (nodeUpdated) {
|
|
57
|
+
editor.getEditorState().read(function () {
|
|
58
|
+
updateTableCellNode(tableCellNode.getLatest());
|
|
59
|
+
});
|
|
60
|
+
setBackgroundColor(currentCellBackgroundColor(editor) || '');
|
|
61
|
+
}
|
|
62
|
+
}, {
|
|
63
|
+
skipInitialization: true
|
|
64
|
+
});
|
|
65
|
+
}, [editor, tableCellNode]);
|
|
66
|
+
useEffect(function () {
|
|
67
|
+
editor.getEditorState().read(function () {
|
|
68
|
+
var selection = $getSelection();
|
|
69
|
+
// Merge cells
|
|
70
|
+
if ($isTableSelection(selection)) {
|
|
71
|
+
var currentSelectionCounts = computeSelectionCount(selection);
|
|
72
|
+
updateSelectionCounts(computeSelectionCount(selection));
|
|
73
|
+
setCanMergeCells(currentSelectionCounts.columns > 1 || currentSelectionCounts.rows > 1);
|
|
74
|
+
}
|
|
75
|
+
// Unmerge cell
|
|
76
|
+
setCanUnmergeCell($canUnmerge());
|
|
77
|
+
});
|
|
78
|
+
}, [editor]);
|
|
79
|
+
var clearTableSelection = useCallback(function () {
|
|
80
|
+
editor.update(function () {
|
|
81
|
+
if (tableCellNode.isAttached()) {
|
|
82
|
+
var tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
83
|
+
var tableElement = getTableElement(tableNode, editor.getElementByKey(tableNode.getKey()));
|
|
84
|
+
if (tableElement === null) {
|
|
85
|
+
throw new Error('TableActionMenu: Expected to find tableElement in DOM');
|
|
86
|
+
}
|
|
87
|
+
var tableObserver = getTableObserverFromTableElement(tableElement);
|
|
88
|
+
if (tableObserver !== null) {
|
|
89
|
+
tableObserver.$clearHighlight();
|
|
90
|
+
}
|
|
91
|
+
tableNode.markDirty();
|
|
92
|
+
updateTableCellNode(tableCellNode.getLatest());
|
|
93
|
+
}
|
|
94
|
+
$setSelection(null);
|
|
95
|
+
});
|
|
96
|
+
}, [editor, tableCellNode]);
|
|
97
|
+
var mergeTableCellsAtSelection = function mergeTableCellsAtSelection() {
|
|
98
|
+
editor.update(function () {
|
|
99
|
+
var selection = $getSelection();
|
|
100
|
+
if (!$isTableSelection(selection)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
var nodes = selection.getNodes();
|
|
104
|
+
var tableCells = nodes.filter($isTableCellNode);
|
|
105
|
+
var targetCell = $mergeCells(tableCells);
|
|
106
|
+
if (targetCell) {
|
|
107
|
+
$selectLastDescendant(targetCell);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
var unmergeTableCellsAtSelection = function unmergeTableCellsAtSelection() {
|
|
112
|
+
editor.update(function () {
|
|
113
|
+
$unmergeCell();
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
var insertTableRowAtSelection = useCallback(function (shouldInsertAfter) {
|
|
117
|
+
editor.update(function () {
|
|
118
|
+
for (var i = 0; i < selectionCounts.rows; i++) {
|
|
119
|
+
$insertTableRowAtSelection(shouldInsertAfter);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}, [editor, selectionCounts.rows]);
|
|
123
|
+
var insertTableColumnAtSelection = useCallback(function (shouldInsertAfter) {
|
|
124
|
+
editor.update(function () {
|
|
125
|
+
for (var i = 0; i < selectionCounts.columns; i++) {
|
|
126
|
+
$insertTableColumnAtSelection(shouldInsertAfter);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}, [editor, selectionCounts.columns]);
|
|
130
|
+
var deleteTableRowAtSelection = useCallback(function () {
|
|
131
|
+
editor.update(function () {
|
|
132
|
+
$deleteTableRowAtSelection();
|
|
133
|
+
});
|
|
134
|
+
}, [editor]);
|
|
135
|
+
var deleteTableAtSelection = useCallback(function () {
|
|
136
|
+
editor.update(function () {
|
|
137
|
+
var tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
138
|
+
tableNode.remove();
|
|
139
|
+
clearTableSelection();
|
|
140
|
+
});
|
|
141
|
+
}, [editor, tableCellNode, clearTableSelection]);
|
|
142
|
+
var deleteTableColumnAtSelection = useCallback(function () {
|
|
143
|
+
editor.update(function () {
|
|
144
|
+
$deleteTableColumnAtSelection();
|
|
145
|
+
});
|
|
146
|
+
}, [editor]);
|
|
147
|
+
var toggleTableRowIsHeader = useCallback(function () {
|
|
148
|
+
editor.update(function () {
|
|
149
|
+
var tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
150
|
+
var tableRowIndex = $getTableRowIndexFromTableCellNode(tableCellNode);
|
|
151
|
+
var _$computeTableMapSkip = $computeTableMapSkipCellCheck(tableNode, null, null),
|
|
152
|
+
_$computeTableMapSkip2 = _slicedToArray(_$computeTableMapSkip, 1),
|
|
153
|
+
gridMap = _$computeTableMapSkip2[0];
|
|
154
|
+
var rowCells = new Set();
|
|
155
|
+
var newStyle = tableCellNode.getHeaderStyles() ^ TableCellHeaderStates.ROW;
|
|
156
|
+
for (var col = 0; col < gridMap[tableRowIndex].length; col++) {
|
|
157
|
+
var mapCell = gridMap[tableRowIndex][col];
|
|
158
|
+
if (!(mapCell !== null && mapCell !== void 0 && mapCell.cell)) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (!rowCells.has(mapCell.cell)) {
|
|
162
|
+
rowCells.add(mapCell.cell);
|
|
163
|
+
mapCell.cell.setHeaderStyles(newStyle, TableCellHeaderStates.ROW);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
clearTableSelection();
|
|
167
|
+
});
|
|
168
|
+
}, [editor, tableCellNode, clearTableSelection]);
|
|
169
|
+
var toggleTableColumnIsHeader = useCallback(function () {
|
|
170
|
+
editor.update(function () {
|
|
171
|
+
var tableNode = $getTableNodeFromLexicalNodeOrThrow(tableCellNode);
|
|
172
|
+
var tableColumnIndex = $getTableColumnIndexFromTableCellNode(tableCellNode);
|
|
173
|
+
var _$computeTableMapSkip3 = $computeTableMapSkipCellCheck(tableNode, null, null),
|
|
174
|
+
_$computeTableMapSkip4 = _slicedToArray(_$computeTableMapSkip3, 1),
|
|
175
|
+
gridMap = _$computeTableMapSkip4[0];
|
|
176
|
+
var columnCells = new Set();
|
|
177
|
+
var newStyle = tableCellNode.getHeaderStyles() ^ TableCellHeaderStates.COLUMN;
|
|
178
|
+
var _iterator = _createForOfIteratorHelper(gridMap),
|
|
179
|
+
_step;
|
|
180
|
+
try {
|
|
181
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
182
|
+
var element = _step.value;
|
|
183
|
+
var mapCell = element[tableColumnIndex];
|
|
184
|
+
if (!(mapCell !== null && mapCell !== void 0 && mapCell.cell)) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (!columnCells.has(mapCell.cell)) {
|
|
188
|
+
columnCells.add(mapCell.cell);
|
|
189
|
+
mapCell.cell.setHeaderStyles(newStyle, TableCellHeaderStates.COLUMN);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
} catch (err) {
|
|
193
|
+
_iterator.e(err);
|
|
194
|
+
} finally {
|
|
195
|
+
_iterator.f();
|
|
196
|
+
}
|
|
197
|
+
clearTableSelection();
|
|
198
|
+
});
|
|
199
|
+
}, [editor, tableCellNode, clearTableSelection]);
|
|
200
|
+
|
|
201
|
+
// Create menu items array with useMemo for performance
|
|
202
|
+
var menuItems = useMemo(function () {
|
|
203
|
+
return [{
|
|
204
|
+
icon: PanelTopCloseIcon,
|
|
205
|
+
key: 'table-insert-row-above',
|
|
206
|
+
label: t("table.insertRowAbove", {
|
|
207
|
+
count: selectionCounts.rows
|
|
208
|
+
}),
|
|
209
|
+
onClick: function onClick() {
|
|
210
|
+
return insertTableRowAtSelection(false);
|
|
211
|
+
}
|
|
212
|
+
}, {
|
|
213
|
+
icon: PanelBottomCloseIcon,
|
|
214
|
+
key: 'table-insert-row-below',
|
|
215
|
+
label: t("table.insertRowBelow", {
|
|
216
|
+
count: selectionCounts.rows
|
|
217
|
+
}),
|
|
218
|
+
onClick: function onClick() {
|
|
219
|
+
return insertTableRowAtSelection(true);
|
|
220
|
+
}
|
|
221
|
+
}, {
|
|
222
|
+
type: 'divider'
|
|
223
|
+
},
|
|
224
|
+
// Column operations
|
|
225
|
+
{
|
|
226
|
+
icon: PanelLeftCloseIcon,
|
|
227
|
+
key: 'table-insert-column-before',
|
|
228
|
+
// label: `Insert ${selectionCounts.columns === 1 ? 'column' : `${selectionCounts.columns} columns`} left`,
|
|
229
|
+
label: t("table.insertColumnLeft", {
|
|
230
|
+
count: selectionCounts.columns
|
|
231
|
+
}),
|
|
232
|
+
onClick: function onClick() {
|
|
233
|
+
return insertTableColumnAtSelection(false);
|
|
234
|
+
}
|
|
235
|
+
}, {
|
|
236
|
+
icon: PanelRightCloseIcon,
|
|
237
|
+
key: 'table-insert-column-after',
|
|
238
|
+
// label: `Insert ${selectionCounts.columns === 1 ? 'column' : `${selectionCounts.columns} columns`} right`,
|
|
239
|
+
label: t("table.insertColumnRight", {
|
|
240
|
+
count: selectionCounts.columns
|
|
241
|
+
}),
|
|
242
|
+
onClick: function onClick() {
|
|
243
|
+
return insertTableColumnAtSelection(true);
|
|
244
|
+
}
|
|
245
|
+
}, {
|
|
246
|
+
type: 'divider'
|
|
247
|
+
},
|
|
248
|
+
// Delete operations
|
|
249
|
+
{
|
|
250
|
+
icon: TableColumnsSplitIcon,
|
|
251
|
+
key: 'table-delete-columns',
|
|
252
|
+
// label: 'Delete column',
|
|
253
|
+
label: t("table.deleteColumn"),
|
|
254
|
+
onClick: function onClick() {
|
|
255
|
+
return deleteTableColumnAtSelection();
|
|
256
|
+
}
|
|
257
|
+
}, {
|
|
258
|
+
icon: TableRowsSplitIcon,
|
|
259
|
+
key: 'table-delete-rows',
|
|
260
|
+
// label: 'Delete row',
|
|
261
|
+
label: t("table.deleteRow"),
|
|
262
|
+
onClick: function onClick() {
|
|
263
|
+
return deleteTableRowAtSelection();
|
|
264
|
+
}
|
|
265
|
+
}, {
|
|
266
|
+
type: 'divider'
|
|
267
|
+
}, {
|
|
268
|
+
icon: Grid2X2XIcon,
|
|
269
|
+
key: 'table-delete',
|
|
270
|
+
// label: 'Delete table',
|
|
271
|
+
label: t("table.delete"),
|
|
272
|
+
onClick: function onClick() {
|
|
273
|
+
return deleteTableAtSelection();
|
|
274
|
+
}
|
|
275
|
+
}];
|
|
276
|
+
}, [cellMerge, canMergeCells, canUnmergeCell, selectionCounts.rows, selectionCounts.columns, tableCellNode.__headerState, mergeTableCellsAtSelection, unmergeTableCellsAtSelection, insertTableRowAtSelection, insertTableColumnAtSelection, deleteTableColumnAtSelection, deleteTableRowAtSelection, deleteTableAtSelection, toggleTableRowIsHeader, toggleTableColumnIsHeader]);
|
|
277
|
+
return /*#__PURE__*/_jsx(Dropdown, {
|
|
278
|
+
menu: {
|
|
279
|
+
items: menuItems
|
|
280
|
+
},
|
|
281
|
+
children: children
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
TableActionMenu.displayName = 'TableActionMenu';
|
|
285
|
+
export default TableActionMenu;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import type { LexicalEditor } from 'lexical';
|
|
3
|
-
declare const
|
|
4
|
-
anchorElem?: HTMLElement | undefined;
|
|
3
|
+
declare const TableActionMenu: import("react").NamedExoticComponent<{
|
|
5
4
|
cellMerge?: boolean | undefined;
|
|
6
5
|
editor: LexicalEditor;
|
|
7
6
|
}>;
|
|
8
|
-
export default
|
|
7
|
+
export default TableActionMenu;
|