Skip to content

Commit ea1442f

Browse files
authored
optimize code (#222)
1 parent 1173bf0 commit ea1442f

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/editors/simple-slate-editor/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import './style.css';
1313

1414
const isMacOS = isMac();
1515

16-
const SimpleSlateEditor = ({ value, editorApi, onSave, columns, onContentChanged, isSupportFormula, onExpandEditorToggle }) => {
16+
const SimpleSlateEditor = ({ value, focusEnd, editorApi, onSave, columns, onContentChanged, isSupportFormula, onExpandEditorToggle }) => {
1717
const [slateValue, setSlateValue] = useState(value);
1818

1919
const editor = useMemo(() => withPropsEditor(baseEditor, { editorApi, onSave, columns }), [columns, editorApi, onSave]);
@@ -42,12 +42,22 @@ const SimpleSlateEditor = ({ value, editorApi, onSave, columns, onContentChanged
4242
const [firstNodeFirstChild] = firstNode.children;
4343
if (firstNodeFirstChild) {
4444
const startOfFirstNode = Editor.start(editor, [0, 0]);
45-
const range = {
45+
let range = {
4646
anchor: startOfFirstNode,
4747
focus: startOfFirstNode,
4848
};
49+
if (focusEnd) {
50+
const firstNode = editor.children[0];
51+
const endOfFirstNode = Editor.end(editor, [0, firstNode.children.length - 1]);
52+
range = {
53+
anchor: endOfFirstNode,
54+
focus: endOfFirstNode,
55+
};
56+
}
57+
4958
focusEditor(editor, range);
5059
}
60+
// eslint-disable-next-line react-hooks/exhaustive-deps
5161
}, []);
5262

5363
// const focusEndNode = useCallback((editor) => {

src/pages/longtext-editor-dialog/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default function LongTextEditorDialog({
1515
value,
1616
autoSave = true,
1717
saveDelay = 60000,
18+
focusEnd = false,
1819
isCheckBrowser = false,
1920
mathJaxSource,
2021
className,
@@ -130,6 +131,7 @@ export default function LongTextEditorDialog({
130131
<SimpleEditor
131132
ref={editorRef}
132133
value={value}
134+
focusEnd={focusEnd}
133135
editorApi={editorApi}
134136
mathJaxSource={mathJaxSource}
135137
onContentChanged={onContentChanged}

src/pages/simple-editor.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const SimpleEditor = forwardRef(({
1010
isInline,
1111
isFetching,
1212
value,
13+
focusEnd = false,
1314
editorApi,
1415
mathJaxSource,
1516
onSave: propsOnSave,
@@ -50,6 +51,7 @@ const SimpleEditor = forwardRef(({
5051
const props = {
5152
isSupportFormula: !!mathJaxSource,
5253
value: richValue,
54+
focusEnd,
5355
editorApi: editorApi,
5456
onSave: propsOnSave,
5557
onContentChanged: onContentChanged,
@@ -74,6 +76,7 @@ SimpleEditor.propTypes = {
7476
isInline: PropTypes.bool,
7577
isFetching: PropTypes.bool,
7678
value: PropTypes.string,
79+
focusEnd: PropTypes.bool,
7780
editorApi: PropTypes.object,
7881
mathJaxSource: PropTypes.string,
7982
onSave: PropTypes.func,

0 commit comments

Comments
 (0)