Skip to content

Commit

Permalink
autofocus MilkdownEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
sytolk committed Jun 12, 2024
1 parent d790901 commit 74ba0d4
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/tagspaces-md/src/MilkdownEditorRef.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useInstance } from '@milkdown/react';
import { replaceAll } from '@milkdown/utils';
import { MilkdownRef } from './MilkdownEditor';
import { useSetDarkMode } from './providers/DarkModeProvider';
import { EditorStatus } from '@milkdown/core';
import { EditorStatus, editorViewCtx } from '@milkdown/core';

type Props = {
milkdownRef: ForwardedRef<MilkdownRef>;
Expand All @@ -14,6 +14,18 @@ const MilkdownEditorRef: React.FC<Props> = ({ milkdownRef }) => {
const [loading, getEditor] = useInstance();
const setDarkMode = useSetDarkMode();

React.useEffect(() => {
// autofocus editor https://github.com/orgs/Milkdown/discussions/843
if (loading) {
return;
}
const editor = getEditor();
if (!editor) {
return;
}
editor.ctx.get(editorViewCtx).dom.focus();
}, [loading]);

React.useImperativeHandle(milkdownRef, () => ({
update: (markdown: string) => {
const editor = getEditor();
Expand Down

0 comments on commit 74ba0d4

Please sign in to comment.