Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(notes): show the empty-line formatting toolbar below the caret (#2509)

Joshua8dc1d15249d5
diff --git a/CLAUDE.md b/CLAUDE.md
index e62b44b2f..40bbe993b 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -203,7 +203,7 @@ OpenWhispr is an Electron-based desktop dictation application that uses whisper.
- **ui/RichTextEditor.tsx**: Tiptap note editor. Note bodies (`content`, `enhanced_content`) are stored as Markdown via tiptap-markdown (`html: false`)
- `RichTextEditorExtensions.ts` holds the extension list (`RichTextEditor` adds the mention extension in front of it); order matters: at equal priority, later extensions' keymaps and clipboard props run first
- `RichTextEditorTable.ts` keeps every table to what a GFM pipe table can store: header first row, one single-line paragraph per cell, no merged cells, no alignment or column widths. Pasted cells are flattened while parsing, a normalizer fixes what commands and pastes leave behind, and the table serializer escapes `|` in cells. Tests: `test/components/richTextEditor.test.js` (happy-dom)
- - Floating menus: `RichTextEditorFormatMenu.tsx` shows one formatting toolbar (marks, text style, lists, quote, insert table) above selected text (Tiptap `BubbleMenu`) and beside the caret on an empty top-level line (`FloatingMenu`); `RichTextEditorTableMenu.tsx` is the table's `⋯` menu. They share `RichTextEditorMenus.ts`: a menu hides when focus leaves it, so dropdowns portal into the menu element itself, never into the editor's scroller (EditorContent moves the scroller's children when a note closes); `useHideOnFocusLeave` hides a menu when focus leaves from inside it or after a click on it, which Tiptap misses. The menus detach their element without unmounting it, so buttons use a native `title` rather than `<Tooltip>`, and a dropdown inside a menu is controlled and closed from the menu's `onHide` — otherwise it stays open over a scroll-locked page when the menu hides. Keep menu props stable, or each render dispatches an `updateOptions` transaction
+ - Floating menus: `RichTextEditorFormatMenu.tsx` shows one formatting toolbar (marks, text style, lists, quote, insert table) above selected text (Tiptap `BubbleMenu`) and below the caret on an empty top-level line, flipping above when there's no room (`FloatingMenu`); `RichTextEditorTableMenu.tsx` is the table's `⋯` menu. They share `RichTextEditorMenus.ts`: a menu hides when focus leaves it, so dropdowns portal into the menu element itself, never into the editor's scroller (EditorContent moves the scroller's children when a note closes); `useHideOnFocusLeave` hides a menu when focus leaves from inside it or after a click on it, which Tiptap misses. The menus detach their element without unmounting it, so buttons use a native `title` rather than `<Tooltip>`, and a dropdown inside a menu is controlled and closed from the menu's `onHide` — otherwise it stays open over a scroll-locked page when the menu hides. Keep menu props stable, or each render dispatches an `updateOptions` transaction
### React Hooks (src/hooks/)
diff --git a/src/components/ui/RichTextEditorFormatMenu.tsx b/src/components/ui/RichTextEditorFormatMenu.tsx
index 2fa239156..fe1f38a0d 100644
--- a/src/components/ui/RichTextEditorFormatMenu.tsx
+++ b/src/components/ui/RichTextEditorFormatMenu.tsx
@@ -118,11 +118,15 @@ const canFormat = (editor: Editor, state: EditorState): boolean =>
const shouldShowOnSelection: BubbleMenuProps["shouldShow"] = ({ editor, state }) =>
canFormat(editor, state) && !state.selection.empty && !(state.selection instanceof NodeSelection);
-// On an empty top-level line, where a list, heading or table can start. The
-// menu sits on the line itself, beside the caret, so it covers no text.
+// On an empty top-level line, where a list, heading or table can start.
const shouldShowOnEmptyLine: FloatingMenuProps["shouldShow"] = ({ editor, state }) =>
canFormat(editor, state) && isOnEmptyLine(state.selection);
+// Below the caret, so it covers neither the line being typed on (Tiptap's
+// default puts it beside the caret) nor the text written above it. It flips
+// above only when there's no room below.
+const LINE_MENU_OPTIONS: FloatingMenuProps["options"] = { placement: "bottom-start" };
+
/** A toggle when `active` is set, a plain button otherwise. */
function ToolbarButton({
label,
@@ -256,7 +260,7 @@ function FormatToolbar({
/** Marks, text style, lists and tables for selected text or an empty line. */
export function RichTextEditorFormatMenu({ editor }: { editor: Editor }) {
const onSelection = useMenuDropdown();
- const onEmptyLine = useMenuDropdown();
+ const onEmptyLine = useMenuDropdown(LINE_MENU_OPTIONS);
useHideOnFocusLeave(editor, MENU_KEYS);
return (
<>
diff --git a/src/index.css b/src/index.css
index a10f0e6b2..ed70bd1d1 100644
--- a/src/index.css
+++ b/src/index.css
@@ -1898,11 +1898,6 @@ a:hover {
height: 0;
}
-/* The empty-line format toolbar sits where the placeholder would show. */
-.rich-text-editor-content:has(~ .rich-text-editor-line-menu) p.is-editor-empty:first-child::before {
- content: none;
-}
-
/* Dark mode adjustments */
.dark .rich-text-editor-content li::marker {
color: oklch(0.7 0 0 / 0.4);
diff --git a/test/components/richTextEditor.test.js b/test/components/richTextEditor.test.js
index 5c3316f1a..14d7260e3 100644
--- a/test/components/richTextEditor.test.js
+++ b/test/components/richTextEditor.test.js
@@ -815,6 +815,50 @@ test("the formatting toolbar shows on selected text and empty lines only", async
assert.deepEqual(notes.errors, []);
});
+test("the empty-line toolbar sits below the caret, and above it with no room below", async (t) => {
+ const notes = await mountNotes(t, "Intro text");
+ // happy-dom has no layout: give the page and the toolbar a size, and the caret a
+ // spot on the page. Without a toolbar size, start, centre and end alignment all
+ // land on the caret, and the toolbar never needs to flip.
+ const page = happyWindow.document.documentElement;
+ const proto = happyWindow.HTMLElement.prototype;
+ const pageSize = { clientWidth: 1024, clientHeight: 768 };
+ const menuSize = { offsetWidth: 320, offsetHeight: 40 };
+ for (const [key, value] of Object.entries(pageSize)) {
+ Object.defineProperty(page, key, { value, configurable: true });
+ }
+ const originals = {};
+ for (const [key, value] of Object.entries(menuSize)) {
+ originals[key] = Object.getOwnPropertyDescriptor(proto, key);
+ Object.defineProperty(proto, key, {
+ configurable: true,
+ get() {
+ return this.classList.contains("rich-text-editor-line-menu")
+ ? value
+ : originals[key].get.call(this);
+ },
+ });
+ }
+ t.after(() => {
+ for (const key of Object.keys(pageSize)) delete page[key];
+ for (const [key, descriptor] of Object.entries(originals)) {
+ Object.defineProperty(proto, key, descriptor);
+ }
+ });
+ let caret = { left: 500, right: 500, top: 300, bottom: 320 };
+ notes.editor().view.coordsAtPos = () => caret;
+ await notes.act(() => addEmptyLastLine(notes.editor()));
+ const menu = happyWindow.document.querySelector(".rich-text-editor-line-menu");
+ const top = () => parseFloat(menu.style.top);
+ assert.equal(parseFloat(menu.style.left), caret.left, "starts at the caret");
+ assert.ok(top() > caret.bottom, `below the line (top ${top()})`);
+
+ caret = { left: 500, right: 500, top: 740, bottom: 760 };
+ await notes.act(() => addEmptyLastLine(notes.editor()));
+ assert.ok(top() + menuSize.offsetHeight < caret.top, `above the line (top ${top()})`);
+ assert.deepEqual(notes.errors, []);
+});
+
test("the formatting toolbar stays away from code blocks, list items and nodes", async (t) => {
const notes = await mountNotes(t, "```js\nconst a = 1;\n```\n\n- item\n\n---\n\nAfter");
const editor = () => notes.editor();