@lobehub/editor 1.4.6 → 1.5.0
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/kernel.d.ts +4 -1
- package/es/editor-kernel/kernel.js +56 -1
- package/es/index.d.ts +2 -0
- package/es/index.js +2 -0
- package/es/locale/index.d.ts +7 -0
- package/es/locale/index.js +7 -0
- package/es/plugins/code/command/index.d.ts +3 -0
- package/es/plugins/code/command/index.js +48 -0
- package/es/plugins/code/index.d.ts +3 -0
- package/es/plugins/code/index.js +3 -0
- package/es/plugins/code/node/code.d.ts +19 -0
- package/es/plugins/{common/node/LexicalLineBreakNode.js → code/node/code.js} +93 -95
- package/es/plugins/code/plugin/index.d.ts +5 -0
- package/es/plugins/{common/node/ParagraphNode.js → code/plugin/index.js} +40 -33
- package/es/plugins/code/plugin/registry.d.ts +3 -0
- package/es/plugins/code/plugin/registry.js +48 -0
- package/es/plugins/code/react/CodeReactPlugin.d.ts +4 -0
- package/es/plugins/code/react/CodeReactPlugin.js +31 -0
- package/es/plugins/code/react/index.d.ts +1 -0
- package/es/plugins/code/react/index.js +1 -0
- package/es/plugins/code/react/style.d.ts +3 -0
- package/es/plugins/code/react/style.js +10 -0
- package/es/plugins/code/react/type.d.ts +3 -0
- package/es/plugins/code/react/type.js +1 -0
- package/es/plugins/common/data-source/json-data-source.js +42 -5
- package/es/plugins/common/data-source/text-data-source.js +2 -2
- package/es/plugins/common/index.d.ts +1 -0
- package/es/plugins/common/index.js +1 -0
- package/es/plugins/common/node/ElementDOMSlot.d.ts +3 -5
- package/es/plugins/common/node/ElementDOMSlot.js +29 -56
- package/es/plugins/common/node/cursor.d.ts +12 -0
- package/es/plugins/common/node/cursor.js +303 -0
- package/es/plugins/common/plugin/index.js +5 -9
- package/es/plugins/common/plugin/register.d.ts +3 -1
- package/es/plugins/common/plugin/register.js +46 -13
- package/es/plugins/link/command/index.d.ts +5 -1
- package/es/plugins/link/command/index.js +21 -3
- package/es/plugins/link/react/ReactLinkPlugin.js +6 -9
- package/es/plugins/link/react/components/LinkEdit.d.ts +2 -1
- package/es/plugins/link/react/components/LinkEdit.js +195 -51
- package/es/plugins/link/react/components/LinkToolbar.d.ts +10 -0
- package/es/plugins/link/react/components/LinkToolbar.js +73 -0
- package/es/plugins/link/react/style.d.ts +3 -2
- package/es/plugins/link/react/style.js +8 -5
- package/es/plugins/list/command/index.d.ts +1 -0
- package/es/plugins/list/command/index.js +1 -0
- package/es/plugins/list/plugin/checkList.d.ts +3 -0
- package/es/plugins/list/plugin/checkList.js +167 -0
- package/es/plugins/list/plugin/index.js +9 -2
- package/es/plugins/list/react/style.js +4 -2
- package/es/plugins/markdown/data-source/markdown-data-source.js +5 -1
- package/es/plugins/markdown/service/shortcut.d.ts +3 -3
- package/es/plugins/math/command/index.d.ts +13 -0
- package/es/plugins/math/command/index.js +47 -0
- package/es/plugins/math/index.d.ts +3 -0
- package/es/plugins/math/index.js +3 -0
- package/es/plugins/math/node/index.d.ts +45 -0
- package/es/plugins/math/node/index.js +259 -0
- package/es/plugins/math/plugin/index.d.ts +11 -0
- package/es/plugins/math/plugin/index.js +103 -0
- package/es/plugins/math/react/component/MathEditor.d.ts +10 -0
- package/es/plugins/math/react/component/MathEditor.js +253 -0
- package/es/plugins/math/react/component/MathEditorContainer.d.ts +14 -0
- package/es/plugins/math/react/component/MathEditorContainer.js +60 -0
- package/es/plugins/math/react/component/MathEditorContent.d.ts +26 -0
- package/es/plugins/math/react/component/MathEditorContent.js +163 -0
- package/es/plugins/math/react/component/MathInline.d.ts +10 -0
- package/es/plugins/math/react/component/MathInline.js +105 -0
- package/es/plugins/math/react/component/Placeholder.d.ts +3 -0
- package/es/plugins/math/react/component/Placeholder.js +19 -0
- package/es/plugins/math/react/index.d.ts +3 -0
- package/es/plugins/math/react/index.js +43 -0
- package/es/plugins/math/react/style.d.ts +6 -0
- package/es/plugins/math/react/style.js +16 -0
- package/es/plugins/math/react/type.d.ts +13 -0
- package/es/plugins/math/react/type.js +1 -0
- package/es/plugins/slash/react/ReactSlashPlugin.js +12 -10
- package/es/react/ChatInputActions/ChatInputActions.js +38 -119
- package/es/react/ChatInputActions/components/ActionItem.d.ts +13 -0
- package/es/react/ChatInputActions/components/ActionItem.js +60 -0
- package/es/react/ChatInputActions/components/ActionRender.d.ts +9 -0
- package/es/react/ChatInputActions/components/ActionRender.js +64 -0
- package/es/react/ChatInputActions/components/CollapsedActions.d.ts +4 -0
- package/es/react/ChatInputActions/components/CollapsedActions.js +82 -0
- package/es/react/ChatInputActions/components/useContainerSize.d.ts +9 -0
- package/es/react/ChatInputActions/components/useContainerSize.js +43 -0
- package/es/react/ChatInputActions/components/useDisplayActionCount.d.ts +13 -0
- package/es/react/ChatInputActions/components/useDisplayActionCount.js +66 -0
- package/es/react/ChatInputActions/style.js +1 -1
- package/es/react/ChatInputActions/type.d.ts +10 -9
- package/es/react/FloatMenu/FloatMenu.d.ts +4 -0
- package/es/react/FloatMenu/FloatMenu.js +49 -0
- package/es/react/FloatMenu/index.d.ts +2 -0
- package/es/react/FloatMenu/index.js +2 -0
- package/es/react/FloatMenu/type.d.ts +19 -0
- package/es/react/FloatMenu/type.js +1 -0
- package/es/react/SlashMenu/SlashMenu.js +32 -48
- package/es/react/SlashMenu/type.d.ts +5 -13
- package/es/react/hooks/useEditorState/index.d.ts +4 -0
- package/es/react/hooks/useEditorState/index.js +40 -4
- package/es/react/index.d.ts +1 -0
- package/es/react/index.js +1 -0
- package/es/types/global.d.ts +4 -0
- package/es/types/kernel.d.ts +26 -1
- package/package.json +2 -1
- package/es/plugins/common/node/LexicalLineBreakNode.d.ts +0 -31
- package/es/plugins/common/node/ParagraphNode.d.ts +0 -7
- package/es/plugins/link/react/components/Toolbar.d.ts +0 -7
- package/es/plugins/link/react/components/Toolbar.js +0 -63
- package/es/react/ChatInputActions/components/ChatInputActionsCollapse.d.ts +0 -4
- package/es/react/ChatInputActions/components/ChatInputActionsCollapse.js +0 -97
- /package/es/react/{SlashMenu → FloatMenu}/style.d.ts +0 -0
- /package/es/react/{SlashMenu → FloatMenu}/style.js +0 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
4
|
+
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."); }
|
|
5
|
+
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); }
|
|
6
|
+
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; }
|
|
7
|
+
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; } }
|
|
8
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
9
|
+
import { useLayoutEffect } from 'react';
|
|
10
|
+
import { useLexicalComposerContext } from "../../../editor-kernel/react/react-context";
|
|
11
|
+
import { MarkdownPlugin } from "../../markdown";
|
|
12
|
+
import { MathPlugin } from "../plugin";
|
|
13
|
+
import MathEdit from "./component/MathEditor";
|
|
14
|
+
import MathInline from "./component/MathInline";
|
|
15
|
+
import { useStyles } from "./style";
|
|
16
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
17
|
+
export var ReactMathPlugin = function ReactMathPlugin(_ref) {
|
|
18
|
+
var className = _ref.className,
|
|
19
|
+
renderComp = _ref.renderComp,
|
|
20
|
+
theme = _ref.theme;
|
|
21
|
+
var _useLexicalComposerCo = useLexicalComposerContext(),
|
|
22
|
+
_useLexicalComposerCo2 = _slicedToArray(_useLexicalComposerCo, 1),
|
|
23
|
+
editor = _useLexicalComposerCo2[0];
|
|
24
|
+
var _useStyles = useStyles(),
|
|
25
|
+
styles = _useStyles.styles;
|
|
26
|
+
useLayoutEffect(function () {
|
|
27
|
+
editor.registerPlugin(MarkdownPlugin);
|
|
28
|
+
editor.registerPlugin(MathPlugin, {
|
|
29
|
+
decorator: function decorator(node, editor) {
|
|
30
|
+
return /*#__PURE__*/_jsx(MathInline, {
|
|
31
|
+
className: className,
|
|
32
|
+
editor: editor,
|
|
33
|
+
node: node
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
theme: theme || styles
|
|
37
|
+
});
|
|
38
|
+
}, [editor]);
|
|
39
|
+
return /*#__PURE__*/_jsx(MathEdit, {
|
|
40
|
+
renderComp: renderComp
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
ReactMathPlugin.displayName = 'ReactMathPlugin';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5;
|
|
2
|
+
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
|
|
3
|
+
/* eslint-disable sort-keys-fix/sort-keys-fix */
|
|
4
|
+
import { createStyles } from 'antd-style';
|
|
5
|
+
export var useStyles = createStyles(function (_ref) {
|
|
6
|
+
var cx = _ref.cx,
|
|
7
|
+
css = _ref.css,
|
|
8
|
+
token = _ref.token;
|
|
9
|
+
var latex = css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n cursor: pointer;\n user-select: none;\n font-size: 1em;\n\n .katex-error {\n color: ", " !important;\n }\n\n .katex-html {\n overflow: auto hidden;\n padding: 3px;\n\n .base {\n margin-block: 0;\n margin-inline: auto;\n }\n\n .tag {\n position: relative !important;\n display: inline-block;\n padding-inline-start: 0.5rem;\n }\n }\n\n &.selected {\n color: #000;\n background: ", ";\n }\n\n &:hover {\n background: ", ";\n }\n\n &.editing {\n background: ", ";\n }\n\n &:has(.katex-error) {\n background: ", ";\n }\n "])), token.colorError, token.yellow, token.colorFillTertiary, token.colorFillTertiary, token.colorErrorBg);
|
|
10
|
+
return {
|
|
11
|
+
mathInline: cx(latex, css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n display: inline-block;\n "])))),
|
|
12
|
+
mathBlock: cx(latex, css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n overflow: auto hidden;\n display: block;\n white-space: nowrap;\n "])))),
|
|
13
|
+
mathEditor: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n position: absolute;\n z-index: 999;\n inset-block-start: -9999px;\n inset-inline-start: -9999px;\n\n width: 320px;\n\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
|
+
mathEditorFooter: css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n border-block-start: 1px solid ", ";\n background: ", ";\n "])), token.colorBorderSecondary, token.colorFillQuaternary)
|
|
15
|
+
};
|
|
16
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FC, ReactNode } from 'react';
|
|
2
|
+
export interface ReactMathPluginProps {
|
|
3
|
+
className?: string;
|
|
4
|
+
/** 自定义渲染组件,接收 MathEditorContent 作为子节点 */
|
|
5
|
+
renderComp?: FC<{
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
open?: boolean;
|
|
8
|
+
}>;
|
|
9
|
+
theme?: {
|
|
10
|
+
mathBlock?: string;
|
|
11
|
+
mathInline?: string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -7,7 +7,7 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
|
|
|
7
7
|
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; } }
|
|
8
8
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
9
9
|
import { mergeRegister } from '@lexical/utils';
|
|
10
|
-
import {
|
|
10
|
+
import { COMMAND_PRIORITY_CRITICAL, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_UP_COMMAND, KEY_ENTER_COMMAND, KEY_ESCAPE_COMMAND, KEY_TAB_COMMAND } from 'lexical';
|
|
11
11
|
import { Children, useCallback, useLayoutEffect, useRef, useState } from 'react';
|
|
12
12
|
import { useLexicalEditor } from "../../../editor-kernel/react";
|
|
13
13
|
import { useLexicalComposerContext } from "../../../editor-kernel/react/react-context";
|
|
@@ -140,11 +140,11 @@ var ReactSlashPlugin = function ReactSlashPlugin(_ref) {
|
|
|
140
140
|
}
|
|
141
141
|
close();
|
|
142
142
|
}, [editor, resolution, close]);
|
|
143
|
-
useLexicalEditor(function (
|
|
143
|
+
useLexicalEditor(function () {
|
|
144
144
|
var pureOptions = options.filter(function (item) {
|
|
145
145
|
return !('type' in item && item.type === 'divider') && 'key' in item && Boolean(item.key);
|
|
146
146
|
});
|
|
147
|
-
return mergeRegister(editor.
|
|
147
|
+
return mergeRegister(editor.registerHighCommand(KEY_ARROW_DOWN_COMMAND, function (payload) {
|
|
148
148
|
var event = payload;
|
|
149
149
|
if (pureOptions !== null && pureOptions.length) {
|
|
150
150
|
var currentIndex = activeKey ? pureOptions.findIndex(function (opt) {
|
|
@@ -154,9 +154,10 @@ var ReactSlashPlugin = function ReactSlashPlugin(_ref) {
|
|
|
154
154
|
setActiveKey(String(pureOptions[newIndex].key));
|
|
155
155
|
event.preventDefault();
|
|
156
156
|
event.stopImmediatePropagation();
|
|
157
|
+
return true;
|
|
157
158
|
}
|
|
158
|
-
return
|
|
159
|
-
},
|
|
159
|
+
return false;
|
|
160
|
+
}, COMMAND_PRIORITY_CRITICAL), editor.registerHighCommand(KEY_ARROW_UP_COMMAND, function (payload) {
|
|
160
161
|
var event = payload;
|
|
161
162
|
if (pureOptions !== null && pureOptions.length) {
|
|
162
163
|
var currentIndex = activeKey ? pureOptions.findIndex(function (opt) {
|
|
@@ -166,15 +167,16 @@ var ReactSlashPlugin = function ReactSlashPlugin(_ref) {
|
|
|
166
167
|
setActiveKey(String(pureOptions[newIndex].key));
|
|
167
168
|
event.preventDefault();
|
|
168
169
|
event.stopImmediatePropagation();
|
|
170
|
+
return true;
|
|
169
171
|
}
|
|
170
|
-
return
|
|
171
|
-
},
|
|
172
|
+
return false;
|
|
173
|
+
}, COMMAND_PRIORITY_CRITICAL), editor.registerHighCommand(KEY_ESCAPE_COMMAND, function (payload) {
|
|
172
174
|
var event = payload;
|
|
173
175
|
event.preventDefault();
|
|
174
176
|
event.stopImmediatePropagation();
|
|
175
177
|
close();
|
|
176
178
|
return true;
|
|
177
|
-
},
|
|
179
|
+
}, COMMAND_PRIORITY_CRITICAL), editor.registerHighCommand(KEY_TAB_COMMAND, function (payload) {
|
|
178
180
|
var event = payload;
|
|
179
181
|
if (options === null || activeKey === null) {
|
|
180
182
|
return false;
|
|
@@ -189,7 +191,7 @@ var ReactSlashPlugin = function ReactSlashPlugin(_ref) {
|
|
|
189
191
|
event.stopImmediatePropagation();
|
|
190
192
|
handleMenuSelect(selectedOption);
|
|
191
193
|
return true;
|
|
192
|
-
},
|
|
194
|
+
}, COMMAND_PRIORITY_CRITICAL), editor.registerHighCommand(KEY_ENTER_COMMAND, function (event) {
|
|
193
195
|
if (options === null || activeKey === null) {
|
|
194
196
|
return false;
|
|
195
197
|
}
|
|
@@ -205,7 +207,7 @@ var ReactSlashPlugin = function ReactSlashPlugin(_ref) {
|
|
|
205
207
|
}
|
|
206
208
|
handleMenuSelect(selectedOption);
|
|
207
209
|
return true;
|
|
208
|
-
},
|
|
210
|
+
}, COMMAND_PRIORITY_CRITICAL));
|
|
209
211
|
}, [options, activeKey, handleActiveKeyChange, handleMenuSelect]);
|
|
210
212
|
|
|
211
213
|
// Get custom render component if available
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
-
var _excluded = ["gap", "disabled", "items", "onActionClick", "className", "collapseOffset"]
|
|
5
|
-
_excluded2 = ["wrapper", "icon", "key", "label", "onClick", "danger", "loading"];
|
|
4
|
+
var _excluded = ["gap", "disabled", "items", "onActionClick", "className", "collapseOffset", "autoCollapse", "defaultGroupCollapse", "onGroupCollapseChange", "groupCollapse"];
|
|
6
5
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
7
6
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
8
7
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
@@ -20,12 +19,11 @@ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" !=
|
|
|
20
19
|
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
21
20
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
22
21
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
23
|
-
import {
|
|
24
|
-
import { useSize } from 'ahooks';
|
|
25
|
-
import { Divider } from 'antd';
|
|
26
|
-
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
22
|
+
import { memo, useMemo } from 'react';
|
|
27
23
|
import { Flexbox } from 'react-layout-kit';
|
|
28
|
-
import
|
|
24
|
+
import useMergeState from 'use-merge-value';
|
|
25
|
+
import ActionItem from "./components/ActionItem";
|
|
26
|
+
import { useDisplayActionCount } from "./components/useDisplayActionCount";
|
|
29
27
|
import { useStyles } from "./style";
|
|
30
28
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
31
29
|
var ChatInputActions = /*#__PURE__*/memo(function (_ref) {
|
|
@@ -38,41 +36,32 @@ var ChatInputActions = /*#__PURE__*/memo(function (_ref) {
|
|
|
38
36
|
className = _ref.className,
|
|
39
37
|
_ref$collapseOffset = _ref.collapseOffset,
|
|
40
38
|
collapseOffset = _ref$collapseOffset === void 0 ? 0 : _ref$collapseOffset,
|
|
39
|
+
_ref$autoCollapse = _ref.autoCollapse,
|
|
40
|
+
autoCollapse = _ref$autoCollapse === void 0 ? true : _ref$autoCollapse,
|
|
41
|
+
_ref$defaultGroupColl = _ref.defaultGroupCollapse,
|
|
42
|
+
defaultGroupCollapse = _ref$defaultGroupColl === void 0 ? false : _ref$defaultGroupColl,
|
|
43
|
+
onGroupCollapseChange = _ref.onGroupCollapseChange,
|
|
44
|
+
groupCollapse = _ref.groupCollapse,
|
|
41
45
|
rest = _objectWithoutProperties(_ref, _excluded);
|
|
42
46
|
var _useStyles = useStyles(),
|
|
43
47
|
cx = _useStyles.cx,
|
|
44
48
|
styles = _useStyles.styles;
|
|
45
|
-
var
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
var
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
});
|
|
62
|
-
}, [items]);
|
|
63
|
-
|
|
64
|
-
// Temp fix https://github.com/lobehub/lobe-chat/issues/9139
|
|
65
|
-
useEffect(function () {
|
|
66
|
-
if (!(size !== null && size !== void 0 && size.width)) return;
|
|
67
|
-
var length = flatItems.length + 1;
|
|
68
|
-
var calcMaxCount = Math.floor((size.width - collapseOffset) / 38);
|
|
69
|
-
// setMaxCount(calcMaxCount);
|
|
70
|
-
if (calcMaxCount < length) {
|
|
71
|
-
// setCollapsed(true);
|
|
72
|
-
} else {
|
|
73
|
-
// setCollapsed(false);
|
|
74
|
-
}
|
|
75
|
-
}, [size, flatItems.length, collapseOffset]);
|
|
49
|
+
var _useMergeState = useMergeState(defaultGroupCollapse, {
|
|
50
|
+
defaultValue: defaultGroupCollapse,
|
|
51
|
+
onChange: onGroupCollapseChange,
|
|
52
|
+
value: groupCollapse
|
|
53
|
+
}),
|
|
54
|
+
_useMergeState2 = _slicedToArray(_useMergeState, 2),
|
|
55
|
+
groupCollapsed = _useMergeState2[0],
|
|
56
|
+
setGroupCollapsed = _useMergeState2[1];
|
|
57
|
+
var _useDisplayActionCoun = useDisplayActionCount({
|
|
58
|
+
autoCollapse: autoCollapse,
|
|
59
|
+
collapseOffset: collapseOffset,
|
|
60
|
+
items: items
|
|
61
|
+
}),
|
|
62
|
+
ref = _useDisplayActionCoun.ref,
|
|
63
|
+
maxCount = _useDisplayActionCoun.maxCount,
|
|
64
|
+
collapsed = _useDisplayActionCoun.collapsed;
|
|
76
65
|
var calcItem = useMemo(function () {
|
|
77
66
|
if (!collapsed) return items;
|
|
78
67
|
var alwaysDisplayItems = items.filter(function (item) {
|
|
@@ -93,95 +82,25 @@ var ChatInputActions = /*#__PURE__*/memo(function (_ref) {
|
|
|
93
82
|
return item.type !== 'divider';
|
|
94
83
|
}).slice(sliceCount)), _toConsumableArray(collapseItems.children))
|
|
95
84
|
})], _toConsumableArray(alwaysDisplayItems)).filter(Boolean);
|
|
96
|
-
}, [collapsed, items,
|
|
97
|
-
var mapActions = useCallback(function (item, i) {
|
|
98
|
-
if (item.type === 'divider') {
|
|
99
|
-
return /*#__PURE__*/_jsx(Divider, {
|
|
100
|
-
className: styles.divider,
|
|
101
|
-
style: {
|
|
102
|
-
height: 20
|
|
103
|
-
},
|
|
104
|
-
type: 'vertical'
|
|
105
|
-
}, "divider-".concat(i));
|
|
106
|
-
}
|
|
107
|
-
var wrapper = item.wrapper,
|
|
108
|
-
icon = item.icon,
|
|
109
|
-
key = item.key,
|
|
110
|
-
label = item.label,
|
|
111
|
-
_onClick = item.onClick,
|
|
112
|
-
danger = item.danger,
|
|
113
|
-
loading = item.loading,
|
|
114
|
-
itemRest = _objectWithoutProperties(item, _excluded2);
|
|
115
|
-
var node = item.children || /*#__PURE__*/_jsx(ActionIcon, {
|
|
116
|
-
active: item.active,
|
|
117
|
-
danger: danger,
|
|
118
|
-
disabled: disabled || loading || (itemRest === null || itemRest === void 0 ? void 0 : itemRest.disabled),
|
|
119
|
-
icon: icon,
|
|
120
|
-
loading: loading,
|
|
121
|
-
onClick: function onClick(e) {
|
|
122
|
-
onActionClick === null || onActionClick === void 0 || onActionClick({
|
|
123
|
-
domEvent: e,
|
|
124
|
-
key: String(key),
|
|
125
|
-
keyPath: [String(key)]
|
|
126
|
-
});
|
|
127
|
-
_onClick === null || _onClick === void 0 || _onClick(e);
|
|
128
|
-
},
|
|
129
|
-
size: {
|
|
130
|
-
blockSize: 36,
|
|
131
|
-
size: 20
|
|
132
|
-
},
|
|
133
|
-
title: label,
|
|
134
|
-
tooltipProps: {
|
|
135
|
-
placement: 'top'
|
|
136
|
-
}
|
|
137
|
-
}, key);
|
|
138
|
-
if (!wrapper) return node;
|
|
139
|
-
return wrapper(node, String(key));
|
|
140
|
-
}, [disabled, onActionClick, styles.divider]);
|
|
85
|
+
}, [collapsed, items, maxCount]);
|
|
141
86
|
return /*#__PURE__*/_jsx(Flexbox, _objectSpread(_objectSpread({
|
|
142
87
|
align: 'center',
|
|
143
88
|
className: cx(styles.container, className),
|
|
144
|
-
flex:
|
|
89
|
+
flex: 1,
|
|
145
90
|
gap: gap,
|
|
146
91
|
horizontal: true,
|
|
147
92
|
ref: ref
|
|
148
93
|
}, rest), {}, {
|
|
149
94
|
children: calcItem.map(function (item, index) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
})
|
|
160
|
-
}, "collapse-".concat(index));
|
|
161
|
-
}
|
|
162
|
-
if (item.type === 'dropdown') {
|
|
163
|
-
return /*#__PURE__*/_jsx(Dropdown, {
|
|
164
|
-
menu: {
|
|
165
|
-
items: item.children
|
|
166
|
-
},
|
|
167
|
-
children: /*#__PURE__*/_jsx(ActionIcon, {
|
|
168
|
-
active: item.active,
|
|
169
|
-
danger: item.danger,
|
|
170
|
-
disabled: disabled || item.loading || (item === null || item === void 0 ? void 0 : item.disabled),
|
|
171
|
-
icon: item.icon,
|
|
172
|
-
loading: item.loading,
|
|
173
|
-
size: {
|
|
174
|
-
blockSize: 36,
|
|
175
|
-
size: 20
|
|
176
|
-
},
|
|
177
|
-
title: item.label,
|
|
178
|
-
tooltipProps: {
|
|
179
|
-
placement: 'top'
|
|
180
|
-
}
|
|
181
|
-
})
|
|
182
|
-
}, item.key);
|
|
183
|
-
}
|
|
184
|
-
return mapActions(item, index);
|
|
95
|
+
return /*#__PURE__*/_jsx(ActionItem, {
|
|
96
|
+
collapsed: collapsed,
|
|
97
|
+
disabled: disabled,
|
|
98
|
+
gap: gap,
|
|
99
|
+
groupCollapsed: groupCollapsed,
|
|
100
|
+
item: item,
|
|
101
|
+
onActionClick: onActionClick,
|
|
102
|
+
setGroupCollapsed: setGroupCollapsed
|
|
103
|
+
}, item.key || index);
|
|
185
104
|
})
|
|
186
105
|
}));
|
|
187
106
|
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { ChatInputActionItem, ChatInputActionsProps } from '../type';
|
|
3
|
+
interface ChatInputActionItemProps {
|
|
4
|
+
collapsed?: boolean;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
gap?: string | number;
|
|
7
|
+
groupCollapsed?: boolean;
|
|
8
|
+
item: ChatInputActionItem;
|
|
9
|
+
onActionClick: ChatInputActionsProps['onActionClick'];
|
|
10
|
+
setGroupCollapsed?: (collapse: boolean) => void;
|
|
11
|
+
}
|
|
12
|
+
declare const ActionItem: import("react").NamedExoticComponent<ChatInputActionItemProps>;
|
|
13
|
+
export default ActionItem;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { ActionIcon, Dropdown } from '@lobehub/ui';
|
|
4
|
+
import { memo } from 'react';
|
|
5
|
+
import ActionRender from "./ActionRender";
|
|
6
|
+
import CollapsedActions from "./CollapsedActions";
|
|
7
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
8
|
+
var ActionItem = /*#__PURE__*/memo(function (_ref) {
|
|
9
|
+
var item = _ref.item,
|
|
10
|
+
disabled = _ref.disabled,
|
|
11
|
+
onActionClick = _ref.onActionClick,
|
|
12
|
+
groupCollapsed = _ref.groupCollapsed,
|
|
13
|
+
collapsed = _ref.collapsed,
|
|
14
|
+
gap = _ref.gap,
|
|
15
|
+
setGroupCollapsed = _ref.setGroupCollapsed;
|
|
16
|
+
if (item.type === 'collapse') {
|
|
17
|
+
return /*#__PURE__*/_jsx(CollapsedActions, {
|
|
18
|
+
gap: gap,
|
|
19
|
+
groupCollapse: groupCollapsed,
|
|
20
|
+
mode: collapsed ? 'popup' : 'default',
|
|
21
|
+
onGroupCollapseChange: setGroupCollapsed,
|
|
22
|
+
children: item.children.map(function (child, childIndex) {
|
|
23
|
+
return /*#__PURE__*/_jsx(ActionRender, {
|
|
24
|
+
disabled: disabled,
|
|
25
|
+
item: child,
|
|
26
|
+
onActionClick: onActionClick
|
|
27
|
+
}, (child === null || child === void 0 ? void 0 : child.key) || "action-".concat(childIndex));
|
|
28
|
+
})
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (item.type === 'dropdown') {
|
|
32
|
+
return /*#__PURE__*/_jsx(Dropdown, {
|
|
33
|
+
menu: {
|
|
34
|
+
items: item.children
|
|
35
|
+
},
|
|
36
|
+
children: /*#__PURE__*/_jsx(ActionIcon, {
|
|
37
|
+
active: item.active,
|
|
38
|
+
danger: item.danger,
|
|
39
|
+
disabled: disabled || item.loading || (item === null || item === void 0 ? void 0 : item.disabled),
|
|
40
|
+
icon: item.icon,
|
|
41
|
+
loading: item.loading,
|
|
42
|
+
size: {
|
|
43
|
+
blockSize: 36,
|
|
44
|
+
size: 20
|
|
45
|
+
},
|
|
46
|
+
title: item.label,
|
|
47
|
+
tooltipProps: {
|
|
48
|
+
placement: 'top'
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
}, item.key);
|
|
52
|
+
}
|
|
53
|
+
return /*#__PURE__*/_jsx(ActionRender, {
|
|
54
|
+
disabled: disabled,
|
|
55
|
+
item: item,
|
|
56
|
+
onActionClick: onActionClick
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
ActionItem.displayName = 'ChatInputActionItem';
|
|
60
|
+
export default ActionItem;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { ChatInputActionItem, ChatInputActionsProps } from '../type';
|
|
3
|
+
interface ActionRenderProps {
|
|
4
|
+
disabled?: boolean;
|
|
5
|
+
item: ChatInputActionItem;
|
|
6
|
+
onActionClick: ChatInputActionsProps['onActionClick'];
|
|
7
|
+
}
|
|
8
|
+
declare const ActionRender: import("react").NamedExoticComponent<ActionRenderProps>;
|
|
9
|
+
export default ActionRender;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
var _excluded = ["wrapper", "icon", "key", "label", "onClick", "danger", "loading", "active"];
|
|
2
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
3
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
4
|
+
import { ActionIcon } from '@lobehub/ui';
|
|
5
|
+
import { Divider } from 'antd';
|
|
6
|
+
import { isValidElement, memo } from 'react';
|
|
7
|
+
import { useStyles } from "../style";
|
|
8
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
9
|
+
var ActionRender = /*#__PURE__*/memo(function (_ref) {
|
|
10
|
+
var item = _ref.item,
|
|
11
|
+
onActionClick = _ref.onActionClick,
|
|
12
|
+
disabled = _ref.disabled;
|
|
13
|
+
var _useStyles = useStyles(),
|
|
14
|
+
styles = _useStyles.styles;
|
|
15
|
+
if (item.type === 'divider') {
|
|
16
|
+
return /*#__PURE__*/_jsx(Divider, {
|
|
17
|
+
className: styles.divider,
|
|
18
|
+
style: {
|
|
19
|
+
height: 20
|
|
20
|
+
},
|
|
21
|
+
type: 'vertical'
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
var _ref2 = item,
|
|
25
|
+
wrapper = _ref2.wrapper,
|
|
26
|
+
icon = _ref2.icon,
|
|
27
|
+
key = _ref2.key,
|
|
28
|
+
label = _ref2.label,
|
|
29
|
+
_onClick = _ref2.onClick,
|
|
30
|
+
danger = _ref2.danger,
|
|
31
|
+
loading = _ref2.loading,
|
|
32
|
+
active = _ref2.active,
|
|
33
|
+
itemRest = _objectWithoutProperties(_ref2, _excluded);
|
|
34
|
+
if (item.children && /*#__PURE__*/isValidElement(item.children)) {
|
|
35
|
+
if (!wrapper) return item.children;
|
|
36
|
+
return wrapper(item.children);
|
|
37
|
+
}
|
|
38
|
+
var action = /*#__PURE__*/_jsx(ActionIcon, {
|
|
39
|
+
active: active,
|
|
40
|
+
danger: danger,
|
|
41
|
+
disabled: disabled || loading || (itemRest === null || itemRest === void 0 ? void 0 : itemRest.disabled),
|
|
42
|
+
icon: icon,
|
|
43
|
+
loading: loading,
|
|
44
|
+
onClick: function onClick(e) {
|
|
45
|
+
onActionClick === null || onActionClick === void 0 || onActionClick({
|
|
46
|
+
domEvent: e,
|
|
47
|
+
key: String(key),
|
|
48
|
+
keyPath: [String(key)]
|
|
49
|
+
});
|
|
50
|
+
_onClick === null || _onClick === void 0 || _onClick(e);
|
|
51
|
+
},
|
|
52
|
+
size: {
|
|
53
|
+
blockSize: 36,
|
|
54
|
+
size: 20
|
|
55
|
+
},
|
|
56
|
+
title: label,
|
|
57
|
+
tooltipProps: {
|
|
58
|
+
placement: 'top'
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
if (!wrapper) return action;
|
|
62
|
+
return wrapper(action);
|
|
63
|
+
});
|
|
64
|
+
export default ActionRender;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { ActionIcon } from '@lobehub/ui';
|
|
4
|
+
import { Popover } from 'antd';
|
|
5
|
+
import { motion } from 'framer-motion';
|
|
6
|
+
import { CircleChevronLeftIcon, CircleChevronRightIcon, CircleChevronUpIcon } from 'lucide-react';
|
|
7
|
+
import { memo } from 'react';
|
|
8
|
+
import { Flexbox } from 'react-layout-kit';
|
|
9
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
10
|
+
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
11
|
+
var CollapsedActions = /*#__PURE__*/memo(function (_ref) {
|
|
12
|
+
var children = _ref.children,
|
|
13
|
+
_ref$groupCollapse = _ref.groupCollapse,
|
|
14
|
+
groupCollapse = _ref$groupCollapse === void 0 ? false : _ref$groupCollapse,
|
|
15
|
+
onGroupCollapseChange = _ref.onGroupCollapseChange,
|
|
16
|
+
gap = _ref.gap,
|
|
17
|
+
mode = _ref.mode;
|
|
18
|
+
if (mode === 'popup') {
|
|
19
|
+
return /*#__PURE__*/_jsx(Popover, {
|
|
20
|
+
arrow: false,
|
|
21
|
+
content: /*#__PURE__*/_jsx(Flexbox, {
|
|
22
|
+
align: 'center',
|
|
23
|
+
gap: gap,
|
|
24
|
+
horizontal: true,
|
|
25
|
+
children: children
|
|
26
|
+
}),
|
|
27
|
+
styles: {
|
|
28
|
+
body: {
|
|
29
|
+
padding: 4
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
children: /*#__PURE__*/_jsx(ActionIcon, {
|
|
33
|
+
icon: CircleChevronUpIcon,
|
|
34
|
+
size: {
|
|
35
|
+
blockSize: 36,
|
|
36
|
+
size: 20
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return /*#__PURE__*/_jsxs(Flexbox, {
|
|
42
|
+
align: 'center',
|
|
43
|
+
flex: 'none',
|
|
44
|
+
gap: gap,
|
|
45
|
+
horizontal: true,
|
|
46
|
+
children: [/*#__PURE__*/_jsx(motion.div, {
|
|
47
|
+
animate: groupCollapse ? 'closed' : 'open',
|
|
48
|
+
initial: groupCollapse ? 'closed' : 'open',
|
|
49
|
+
style: {
|
|
50
|
+
alignItems: 'center',
|
|
51
|
+
display: 'flex',
|
|
52
|
+
gap: gap,
|
|
53
|
+
overflow: 'hidden'
|
|
54
|
+
},
|
|
55
|
+
transition: {
|
|
56
|
+
duration: 0.2
|
|
57
|
+
},
|
|
58
|
+
variants: {
|
|
59
|
+
closed: {
|
|
60
|
+
opacity: 0,
|
|
61
|
+
width: 0
|
|
62
|
+
},
|
|
63
|
+
open: {
|
|
64
|
+
opacity: 1,
|
|
65
|
+
width: 'auto'
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
children: children
|
|
69
|
+
}), /*#__PURE__*/_jsx(ActionIcon, {
|
|
70
|
+
icon: groupCollapse ? CircleChevronRightIcon : CircleChevronLeftIcon,
|
|
71
|
+
onClick: function onClick() {
|
|
72
|
+
return onGroupCollapseChange === null || onGroupCollapseChange === void 0 ? void 0 : onGroupCollapseChange(!groupCollapse);
|
|
73
|
+
},
|
|
74
|
+
size: {
|
|
75
|
+
blockSize: 36,
|
|
76
|
+
size: 20
|
|
77
|
+
}
|
|
78
|
+
})]
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
CollapsedActions.displayName = 'ChatInputActionsCollapse';
|
|
82
|
+
export default CollapsedActions;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
interface UseContainerSizeOptions {
|
|
3
|
+
debounceMs?: number;
|
|
4
|
+
}
|
|
5
|
+
export declare const useContainerSize: (options?: UseContainerSizeOptions) => {
|
|
6
|
+
ref: import("react").RefObject<HTMLElement | null>;
|
|
7
|
+
size: number | undefined;
|
|
8
|
+
};
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
+
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."); }
|
|
3
|
+
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); }
|
|
4
|
+
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; }
|
|
5
|
+
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; } }
|
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
// hooks/useContainerSize.ts
|
|
8
|
+
import { debounce } from 'lodash-es';
|
|
9
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
10
|
+
export var useContainerSize = function useContainerSize() {
|
|
11
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
12
|
+
var _options$debounceMs = options.debounceMs,
|
|
13
|
+
debounceMs = _options$debounceMs === void 0 ? 100 : _options$debounceMs;
|
|
14
|
+
var ref = useRef(null);
|
|
15
|
+
var _useState = useState(),
|
|
16
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
17
|
+
size = _useState2[0],
|
|
18
|
+
setSize = _useState2[1];
|
|
19
|
+
var resizeObserverRef = useRef(null);
|
|
20
|
+
var updateSize = useCallback(debounce(function (entries) {
|
|
21
|
+
if (entries[0]) {
|
|
22
|
+
var width = entries[0].contentRect.width;
|
|
23
|
+
setSize(Math.floor(width));
|
|
24
|
+
}
|
|
25
|
+
}, debounceMs), [debounceMs]);
|
|
26
|
+
useEffect(function () {
|
|
27
|
+
var element = ref.current;
|
|
28
|
+
if (!element) return;
|
|
29
|
+
resizeObserverRef.current = new ResizeObserver(updateSize);
|
|
30
|
+
resizeObserverRef.current.observe(element);
|
|
31
|
+
return function () {
|
|
32
|
+
if (resizeObserverRef.current) {
|
|
33
|
+
var _updateSize$cancel;
|
|
34
|
+
resizeObserverRef.current.disconnect();
|
|
35
|
+
(_updateSize$cancel = updateSize.cancel) === null || _updateSize$cancel === void 0 || _updateSize$cancel.call(updateSize); // 清理防抖
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}, [updateSize]);
|
|
39
|
+
return {
|
|
40
|
+
ref: ref,
|
|
41
|
+
size: size
|
|
42
|
+
};
|
|
43
|
+
};
|