UNPKG

@lobehub/editor

Version:

A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.

146 lines (142 loc) 9.59 kB
'use client'; 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); } var _excluded = ["defaultHeight", "height", "maxHeight", "minHeight", "resizeMaxHeightOffset", "resize", "onSizeChange", "onSizeDragging", "className", "children", "footer", "header", "style", "slashMenuRef", "classNames", "fullscreen", "showResizeHandle", "onBodyClick", "styles"]; 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; } 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; } 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; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } 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."); } 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); } 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; } 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; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } 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; } 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; } import { Flexbox } from '@lobehub/ui'; import { cx, useThemeMode } from 'antd-style'; import { Resizable } from 're-resizable'; import { useCallback } from 'react'; import useMergeState from 'use-merge-value'; import { useHeight } from "../hooks/useSize"; import { styles } from "./style"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var ChatInput = function ChatInput(props) { var _props$defaultHeight = props.defaultHeight, defaultHeight = _props$defaultHeight === void 0 ? props.defaultHeight || props.minHeight || 64 : _props$defaultHeight, height = props.height, _props$maxHeight = props.maxHeight, maxHeight = _props$maxHeight === void 0 ? 320 : _props$maxHeight, _props$minHeight = props.minHeight, minHeight = _props$minHeight === void 0 ? 64 : _props$minHeight, _props$resizeMaxHeigh = props.resizeMaxHeightOffset, resizeMaxHeightOffset = _props$resizeMaxHeigh === void 0 ? 120 : _props$resizeMaxHeigh, _props$resize = props.resize, resize = _props$resize === void 0 ? true : _props$resize, onSizeChange = props.onSizeChange, onSizeDragging = props.onSizeDragging, className = props.className, children = props.children, footer = props.footer, header = props.header, style = props.style, slashMenuRef = props.slashMenuRef, classNames = props.classNames, fullscreen = props.fullscreen, showResizeHandle = props.showResizeHandle, onBodyClick = props.onBodyClick, customStyles = props.styles, rest = _objectWithoutProperties(props, _excluded); var _useThemeMode = useThemeMode(), isDarkMode = _useThemeMode.isDarkMode; var _useHeight = useHeight(), headerRef = _useHeight.ref, _useHeight$height = _useHeight.height, headerHeight = _useHeight$height === void 0 ? 0 : _useHeight$height; // 使用 useMergeState 管理高度状态 var _useMergeState = useMergeState(defaultHeight, { defaultValue: defaultHeight, onChange: onSizeChange, value: height }), _useMergeState2 = _slicedToArray(_useMergeState, 2), currentHeight = _useMergeState2[0], setCurrentHeight = _useMergeState2[1]; // 处理尺寸变化 var handleResizeStop = useCallback(function (e, direction, ref) { var newHeight = ref.style.height ? parseInt(ref.style.height) : defaultHeight; setCurrentHeight(newHeight); }, [setCurrentHeight, defaultHeight]); // 处理拖拽过程中的尺寸变化 var handleResize = useCallback(function (e, direction, ref) { var newHeight = ref.style.height ? parseInt(ref.style.height) : defaultHeight; onSizeDragging === null || onSizeDragging === void 0 || onSizeDragging(newHeight); }, [onSizeDragging, defaultHeight]); var bodyNode = /*#__PURE__*/_jsx("div", { className: cx(styles.editor, styles.bodyEditor, classNames === null || classNames === void 0 ? void 0 : classNames.body), draggable: false, onClick: onBodyClick, style: _objectSpread(_objectSpread({}, customStyles === null || customStyles === void 0 ? void 0 : customStyles.body), {}, { maxHeight: fullscreen ? '100%' : maxHeight, minHeight: resize ? currentHeight : minHeight }), children: children }); return /*#__PURE__*/_jsxs(Flexbox, _objectSpread(_objectSpread({ className: cx(isDarkMode ? styles.containerDark : styles.containerLight, styles.root, className), height: fullscreen ? '100%' : undefined, style: style, width: "100%" }, rest), {}, { children: [slashMenuRef && /*#__PURE__*/_jsx("div", { ref: slashMenuRef }), /*#__PURE__*/_jsx("div", { className: cx(styles.header, classNames === null || classNames === void 0 ? void 0 : classNames.header), ref: headerRef, style: customStyles === null || customStyles === void 0 ? void 0 : customStyles.header, children: header }), resize ? /*#__PURE__*/_jsx(Resizable, { className: styles.resizableContainer, enable: fullscreen ? false : { top: true }, handleClasses: { top: showResizeHandle ? styles.resizeHandle : undefined }, handleStyles: { top: { backgroundColor: 'transparent', borderRadius: '4px', cursor: 'ns-resize', height: '8px', left: '50%', top: !!header ? -3 - headerHeight : -3, transform: 'translateX(-50%)', width: '100%' } }, maxHeight: fullscreen ? undefined : maxHeight + resizeMaxHeightOffset, minHeight: fullscreen ? undefined : minHeight, onResize: handleResize, onResizeStop: handleResizeStop, size: { height: fullscreen ? undefined : 'auto', width: '100%' }, style: fullscreen ? { flex: 1, overflow: 'hidden', position: 'relative' } : undefined, children: bodyNode }) : bodyNode, /*#__PURE__*/_jsx("div", { className: cx(styles.footer, classNames === null || classNames === void 0 ? void 0 : classNames.footer), style: customStyles === null || customStyles === void 0 ? void 0 : customStyles.footer, children: footer })] })); }; ChatInput.displayName = 'ChatInput'; export default ChatInput;