Skip to content

Commit

Permalink
NoteEditor: autofocus (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
vangberg committed Dec 21, 2024
1 parent 4c26d30 commit 4323122
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/skrift-electron/renderer/components/NoteEditor/AutoFocus.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useEditorEffect } from "@nytimes/react-prosemirror";
import { TextSelection } from "prosemirror-state";
import React from "react";

export const AutoFocus: React.FC = () => {
useEditorEffect((view) => {
// Set initial selection to end of document, so the initial
// focus after render starts at the end of the note.
view.dispatch(
view.state.tr.setSelection(TextSelection.atEnd(view.state.doc))
);

view.dom.focus();
}, []);

return null;
};
2 changes: 2 additions & 0 deletions src/skrift-electron/renderer/components/NoteEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { mouseEventToMode } from "../../mouseEventToMode";
import { nodeViews } from "./nodeViews";
import { noteLinkPlugin } from "./noteLinkPlugin";
import { buildInputRules } from "./inputrules";
import { AutoFocus } from "./AutoFocus";

interface Props {
note: NoteWithLinks;
Expand Down Expand Up @@ -126,6 +127,7 @@ export const NoteEditor: React.FC<Props> = ({
handleDOMEvents={handleDOMEvents}
nodeViews={_nodeViews}
>
<AutoFocus />
<div ref={setMount} />
</ProseMirror>
</div>
Expand Down

0 comments on commit 4323122

Please sign in to comment.