Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: focus search input on pressing ctrl+f

Abdullah Atta4040c62ddad2
diff --git a/packages/editor/src/extensions/search-replace/search-replace.ts b/packages/editor/src/extensions/search-replace/search-replace.ts
index 780fd4f15..cb978dc84 100644
--- a/packages/editor/src/extensions/search-replace/search-replace.ts
+++ b/packages/editor/src/extensions/search-replace/search-replace.ts
@@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { Editor, Extension } from "@tiptap/core";
+import { Extension } from "@tiptap/core";
import { Decoration, DecorationSet } from "prosemirror-view";
import {
EditorState,
@@ -62,6 +62,7 @@ export type SearchStorage = SearchSettings & {
searchTerm: string;
selectedIndex: number;
isSearching: boolean;
+ focusNonce: number;
selectedText?: string;
results?: Result[];
};
@@ -210,6 +211,7 @@ export const SearchReplace = Extension.create<SearchOptions, SearchStorage>({
startSearch:
() =>
({ state }) => {
+ this.storage.focusNonce = Math.random();
this.storage.isSearching = true;
if (!state.selection.empty) {
this.storage.selectedText = state.doc.textBetween(
@@ -258,7 +260,7 @@ export const SearchReplace = Extension.create<SearchOptions, SearchStorage>({
const { from, to } = results[nextIndex];
commands.setTextSelection({ from, to });
- scrollIntoView(this.editor, from);
+ scrollIntoView();
this.storage.selectedIndex = nextIndex;
if (dispatch) updateView(state, dispatch);
@@ -275,7 +277,7 @@ export const SearchReplace = Extension.create<SearchOptions, SearchStorage>({
const { from, to } = results[prevIndex];
commands.setTextSelection({ from, to });
- scrollIntoView(this.editor, from);
+ scrollIntoView();
this.storage.selectedIndex = prevIndex;
if (dispatch) updateView(state, dispatch);
@@ -386,7 +388,7 @@ export const SearchReplace = Extension.create<SearchOptions, SearchStorage>({
}
});
-function scrollIntoView(editor: Editor, from: number) {
+function scrollIntoView() {
setTimeout(() => {
const domNode = document.querySelector(".search-result.selected");
if (!(domNode instanceof HTMLElement)) return;
diff --git a/packages/editor/src/toolbar/popups/search-replace.tsx b/packages/editor/src/toolbar/popups/search-replace.tsx
index 4d70406ec..4d86c7efd 100644
--- a/packages/editor/src/toolbar/popups/search-replace.tsx
+++ b/packages/editor/src/toolbar/popups/search-replace.tsx
@@ -27,7 +27,7 @@ import { Editor } from "../../types";
export type SearchReplacePopupProps = { editor: Editor };
export function SearchReplacePopup(props: SearchReplacePopupProps) {
const { editor } = props;
- const { selectedText, results, selectedIndex } = editor.storage
+ const { selectedText, results, selectedIndex, focusNonce } = editor.storage
.searchreplace as SearchStorage;
const [isReplacing, setIsReplacing] = useState(false);
@@ -68,6 +68,15 @@ export function SearchReplacePopup(props: SearchReplacePopupProps) {
}
}, [selectedText, search]);
+ useEffect(() => {
+ if (searchInputRef.current) {
+ const input = searchInputRef.current;
+ setTimeout(() => {
+ input.focus();
+ }, 0);
+ }
+ }, [focusNonce]);
+
return (
<Flex
sx={{