Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(web): highlight and scroll note search matches

tianma-if20c4d06edb6a
diff --git a/apps/web/package.json b/apps/web/package.json
index f625c385..b801ea37 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -5,6 +5,7 @@
"type": "module",
"dependencies": {
"@tiptap/extension-code-block-lowlight": "3.27.1",
+ "@tiptap/pm": "3.27.1",
"lowlight": "^3.3.0",
"mermaid": "^11.16.0"
}
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index 6e69ba3b..f3cf5ddb 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -1,6 +1,8 @@
import { useRef, useState, useEffect, useCallback, useMemo, type PointerEvent as ReactPointerEvent, type ReactNode } from "react";
import { useQueryClient, useMutation } from "@tanstack/react-query";
import { NodeViewWrapper, ReactNodeViewRenderer, useEditor, EditorContent, type Editor, type NodeViewProps } from "@tiptap/react";
+import { Plugin, PluginKey } from "@tiptap/pm/state";
+import { Decoration, DecorationSet } from "@tiptap/pm/view";
import StarterKit from "@tiptap/starter-kit";
import Image from "@tiptap/extension-image";
import Placeholder from "@tiptap/extension-placeholder";
@@ -87,6 +89,7 @@ const SUPPORTED_PASTE_IMAGE_TYPES = new Set(["image/png", "image/jpeg", "image/g
const MOBILE_EDITOR_QUERY = "(max-width: 639px)";
const EDITOR_AUTO_SAVE_DELAY_MS = 1200;
const MOBILE_DRAFT_PERSIST_DELAY_MS = 800;
+const NOTE_SEARCH_HIGHLIGHT_PLUGIN_KEY = new PluginKey("edgeever-note-search-highlight");
type NoteSearchMatch = {
from: number;
@@ -177,17 +180,17 @@ const focusMobilePlainTextElement = (element: MobilePlainTextElement | null) =>
selection?.addRange(range);
};
-const getEditorSearchMatches = (editor: Editor | null, query: string): NoteSearchMatch[] => {
+const getSearchMatchesFromDocument = (doc: Editor["state"]["doc"], query: string): NoteSearchMatch[] => {
const needle = query.trim().toLocaleLowerCase();
- if (!isEditorReady(editor) || needle.length === 0) {
+ if (needle.length === 0) {
return [];
}
const characters: Array<{ char: string; pos: number }> = [];
let previousTextEnd: number | null = null;
- editor.state.doc.descendants((node, pos) => {
+ doc.descendants((node, pos) => {
if (!node.isText || !node.text) {
return;
}
@@ -221,6 +224,14 @@ const getEditorSearchMatches = (editor: Editor | null, query: string): NoteSearc
return matches;
};
+const getEditorSearchMatches = (editor: Editor | null, query: string): NoteSearchMatch[] => {
+ if (!isEditorReady(editor)) {
+ return [];
+ }
+
+ return getSearchMatchesFromDocument(editor.state.doc, query);
+};
+
const getImageFilesFromDataTransfer = (dataTransfer: DataTransfer | null) => {
if (!dataTransfer) {
return [];
@@ -1138,6 +1149,7 @@ const RichEditorPane = ({
const memoRef = useRef<MemoDetail | null>(memo);
const editSessionRef = useRef<MemoEditSession | null>(null);
const editorRef = useRef<Editor | null>(null);
+ const editorScrollContainerRef = useRef<HTMLDivElement | null>(null);
const mobileTextAreaRef = useRef<MobilePlainTextElement | null>(null);
const mobileDraftTimerRef = useRef<number | null>(null);
const mobileSaveTimerRef = useRef<number | null>(null);
@@ -1412,11 +1424,77 @@ const RichEditorPane = ({
return;
}
- editor.commands.setTextSelection({ from: match.from, to: match.to });
+ // `setTextSelection` updates the ProseMirror selection but does not
+ // request the view to scroll to it. This is especially visible while
+ // the search input keeps focus, because the browser cannot scroll the
+ // editor selection for us in that case.
+ editor.chain().setTextSelection({ from: match.from, to: match.to }).scrollIntoView().run();
+
+ window.requestAnimationFrame(() => {
+ const scrollContainer = editorScrollContainerRef.current;
+ const domPosition = editor.view.domAtPos(match.from);
+ const node = domPosition.node.nodeType === Node.TEXT_NODE
+ ? domPosition.node.parentElement
+ : domPosition.node instanceof Element
+ ? domPosition.node
+ : domPosition.node.parentElement;
+
+ if (!scrollContainer || !node) {
+ return;
+ }
+
+ const containerRect = scrollContainer.getBoundingClientRect();
+ const nodeRect = node.getBoundingClientRect();
+ const padding = 24;
+ const isAbove = nodeRect.top < containerRect.top + padding;
+ const isBelow = nodeRect.bottom > containerRect.bottom - padding;
+
+ if (isAbove || isBelow) {
+ const targetTop = scrollContainer.scrollTop + nodeRect.top - containerRect.top
+ - (scrollContainer.clientHeight - nodeRect.height) / 2;
+ scrollContainer.scrollTo({ top: Math.max(0, targetTop), behavior: "smooth" });
+ }
+ });
},
[editor, noteSearchMatches]
);
+ useEffect(() => {
+ if (!isEditorReady(editor)) {
+ return;
+ }
+
+ const searchHighlightPlugin = new Plugin({
+ key: NOTE_SEARCH_HIGHLIGHT_PLUGIN_KEY,
+ props: {
+ decorations: (state) => {
+ const currentMatches = noteSearchOpen ? getSearchMatchesFromDocument(state.doc, noteSearchQuery) : [];
+
+ if (currentMatches.length === 0) {
+ return DecorationSet.empty;
+ }
+
+ return DecorationSet.create(
+ state.doc,
+ currentMatches.map((match, index) =>
+ Decoration.inline(match.from, match.to, {
+ class: index === noteSearchIndex ? "edgeever-search-match edgeever-search-match-active" : "edgeever-search-match",
+ })
+ )
+ );
+ },
+ },
+ });
+
+ editor.registerPlugin(searchHighlightPlugin);
+
+ return () => {
+ if (isEditorReady(editor)) {
+ editor.unregisterPlugin(NOTE_SEARCH_HIGHLIGHT_PLUGIN_KEY);
+ }
+ };
+ }, [editor, noteSearchIndex, noteSearchMatches, noteSearchOpen]);
+
const focusNoteSearchInput = useCallback(() => {
window.requestAnimationFrame(() => {
noteSearchInputRef.current?.focus();
@@ -2703,6 +2781,7 @@ const RichEditorPane = ({
</header>
<div
+ ref={editorScrollContainerRef}
className={cn(
"edgeever-editor relative min-h-0 flex-1 bg-white",
useMobilePlainTextEditor ? "overflow-visible" : "overflow-y-auto"
diff --git a/apps/web/src/styles/globals.css b/apps/web/src/styles/globals.css
index dad86646..e12e8d2a 100644
--- a/apps/web/src/styles/globals.css
+++ b/apps/web/src/styles/globals.css
@@ -178,6 +178,17 @@ textarea {
padding: 24px 28px 56px;
}
+.ProseMirror .edgeever-search-match {
+ border-radius: 0.2rem;
+ background-color: rgb(254 240 138 / 0.8);
+ box-shadow: 0 0 0 1px rgb(234 179 8 / 0.25);
+}
+
+.ProseMirror .edgeever-search-match-active {
+ background-color: rgb(251 191 36 / 0.9);
+ box-shadow: 0 0 0 2px rgb(217 119 6 / 0.45);
+}
+
@media (max-width: 1023px) {
.ProseMirror {
padding: 18px 16px max(4rem, env(safe-area-inset-bottom));
diff --git a/bun.lock b/bun.lock
index 4f3b1316..7e302c86 100644
--- a/bun.lock
+++ b/bun.lock
@@ -21,6 +21,7 @@
"@tiptap/extension-image": "^3.27.1",
"@tiptap/extension-placeholder": "latest",
"@tiptap/extension-table": "3.27.1",
+ "@tiptap/pm": "3.27.1",
"@tiptap/react": "latest",
"@tiptap/starter-kit": "latest",
"class-variance-authority": "latest",
@@ -164,6 +165,7 @@
"version": "0.1.3",
"dependencies": {
"@tiptap/extension-code-block-lowlight": "3.27.1",
+ "@tiptap/pm": "3.27.1",
"lowlight": "^3.3.0",
"mermaid": "^11.16.0",
},