@lobehub/editor
Version:
A powerful and extensible rich text editor built on Meta's Lexical framework, providing a modern editing experience with React integration.
161 lines (156 loc) • 6.62 kB
JavaScript
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; }
import { Button, Flexbox, Hotkey, Text, TextArea } from '@lobehub/ui';
import { renderToString } from 'katex';
import { isModifierMatch } from 'lexical';
import { memo, useCallback, useEffect, useRef, useState } from 'react';
import { CONTROL_OR_META } from "../../../../common/sys";
import { useTranslation } from "../../../../editor-kernel/react/useTranslation";
import { styles } from "../style";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var MathEditorContent = /*#__PURE__*/memo(function (_ref) {
var focusRef = _ref.focusRef,
mathNode = _ref.mathNode,
onArrowLeft = _ref.onArrowLeft,
onArrowRight = _ref.onArrowRight,
onCancel = _ref.onCancel,
onDelete = _ref.onDelete,
onSubmit = _ref.onSubmit,
onValidationChange = _ref.onValidationChange,
onValueChange = _ref.onValueChange,
value = _ref.value;
var t = useTranslation();
var textareaRef = useRef(null);
var _useState = useState(''),
_useState2 = _slicedToArray(_useState, 2),
latexError = _useState2[0],
setLatexError = _useState2[1];
// 将 ref 暴露给父组件
useEffect(function () {
focusRef === null || focusRef === void 0 || focusRef(textareaRef.current);
}, [focusRef]);
// // 聚焦和光标位置处理
// useEffect(() => {
// if (textareaRef.current) {
// textareaRef.current.focus();
// if (prev) {
// textareaRef.current.resizableTextArea?.textArea?.setSelectionRange(0, 0);
// }
// }
// }, [prev]);
// 实时验证 LaTeX 语法
useEffect(function () {
if (!mathNode) return;
var isEmpty = !value.trim();
if (isEmpty) {
setLatexError('');
onValidationChange === null || onValidationChange === void 0 || onValidationChange(true); // 空值视为有效
return;
}
// 使用防抖来避免过于频繁的验证
var timeoutId = setTimeout(function () {
try {
renderToString(value, {
displayMode: true,
throwOnError: true
});
// 验证成功:清除错误,通知父组件验证通过
setLatexError('');
onValidationChange === null || onValidationChange === void 0 || onValidationChange(true);
} catch (error) {
// 验证失败:设置错误信息,通知父组件验证失败
var errorMessage = error instanceof Error ? error.message : 'LaTeX Parse Error';
setLatexError(errorMessage);
onValidationChange === null || onValidationChange === void 0 || onValidationChange(false);
}
}, 50);
return function () {
return clearTimeout(timeoutId);
};
}, [value, mathNode, onValidationChange]);
var handleKeyDown = useCallback(function (e) {
if (!mathNode) return;
if (isModifierMatch(e, CONTROL_OR_META) && e.key === 'Enter') {
e.preventDefault();
onSubmit();
return;
}
if (e.key === 'Escape') {
e.preventDefault();
onCancel();
return;
}
// 当内容为空且按退格键时,删除数学节点
if (e.key === 'Backspace' && !value.trim()) {
e.preventDefault();
onDelete();
return;
}
if (e.key === 'ArrowLeft' && e.currentTarget.selectionStart === 0) {
e.preventDefault();
onArrowLeft();
}
if (e.key === 'ArrowRight' && e.currentTarget.selectionStart === e.currentTarget.value.length) {
e.preventDefault();
onArrowRight();
}
}, [mathNode, onSubmit, onCancel, onDelete, onArrowLeft, onArrowRight, value]);
return /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/_jsx(TextArea, {
autoFocus: true,
autoSize: {
maxRows: 6,
minRows: 1
},
className: styles.mathEditorTextArea,
onChange: function onChange(e) {
onValueChange(e.target.value);
},
onKeyDown: handleKeyDown,
placeholder: "".concat(t('math.placeholder'), "..."),
ref: textareaRef,
resize: false,
value: value,
variant: 'borderless'
}), latexError && /*#__PURE__*/_jsx(Flexbox, {
className: styles.mathEditorFooter,
horizontal: true,
paddingBlock: 4,
paddingInline: 12,
width: '100%',
children: /*#__PURE__*/_jsx(Text, {
fontSize: 13,
type: 'danger',
children: latexError
})
}), /*#__PURE__*/_jsx(Flexbox, {
className: styles.mathEditorFooter,
horizontal: true,
justify: 'flex-end',
padding: 4,
width: '100%',
children: /*#__PURE__*/_jsxs(Button, {
onClick: function onClick(e) {
e.preventDefault();
onSubmit();
},
size: 'small',
type: 'text',
variant: 'filled',
children: [t('confirm'), /*#__PURE__*/_jsx(Hotkey, {
compact: true,
keys: "mod+enter",
variant: 'borderless'
})]
})
})]
});
});
MathEditorContent.displayName = 'MathEditorContent';
export default MathEditorContent;