Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(open-knowledge): keep selection toolbar inside visible editor area (#2548)
Andrew Mikofalvy25abdf2f2fff
diff --git a/.changeset/bubble-menu-respects-bottom-bar.md b/.changeset/bubble-menu-respects-bottom-bar.mdnew file mode 100644index 000000000..abfae6796--- /dev/null+++ b/.changeset/bubble-menu-respects-bottom-bar.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++The text-selection formatting toolbar now stays within the editor's visible content area. Previously, selecting text and then scrolling let the floating toolbar ride over the bottom status bar and the Ask AI composer (collapsed or open). It now hides when the selected text scrolls out of view — behind the top toolbar, the composer, or past the edge of the pane — and reappears when the selection scrolls back in.diff --git a/packages/app/src/editor/bubble-menu/BubbleMenuBar.tsx b/packages/app/src/editor/bubble-menu/BubbleMenuBar.tsxindex 2bc96080d..39345ccd7 100644--- a/packages/app/src/editor/bubble-menu/BubbleMenuBar.tsx+++ b/packages/app/src/editor/bubble-menu/BubbleMenuBar.tsx@@ -1,4 +1,4 @@-import { autoUpdate, computePosition, flip, offset, shift } from '@floating-ui/dom';+import { autoUpdate, computePosition, flip, hide, offset, shift } from '@floating-ui/dom';import { posToDOMRect } from '@tiptap/core';import type { Editor } from '@tiptap/react';import { useEditorState } from '@tiptap/react';@@ -6,6 +6,7 @@ import { BubbleMenu } from '@tiptap/react/menus';import { useRef, useState } from 'react';import { Separator } from '@/components/ui/separator';import { BlockTypeSelector } from './BlockTypeSelector';+import { deriveEditorClipOptions } from './bubble-menu-clip';import { shouldShowBubbleMenu } from './bubble-menu-state';import { EditWithAiBubbleButton } from './EditWithAiBubbleButton';import { FileBubbleButtons, isFileNodeSelected } from './FileBubbleButtons';@@ -56,6 +57,11 @@ export function BubbleMenuBar({contextElement: editor.view.dom,};+ // Clips placement to the editor's visible content region and hides the bar+ // when the selection scrolls behind the toolbar / bottom composer / footer —+ // see bubble-menu-clip.ts for why the viewport alone is the wrong boundary.+ const clipOptions = deriveEditorClipOptions(editor);+const onShow = () => {const popup = menuRef.current;if (!popup) return;@@ -64,13 +70,17 @@ export function BubbleMenuBar({computePosition(virtualEl, popup, {placement: 'top',strategy: 'fixed',- middleware: [offset(8), flip(), shift({ padding: 8 })],+ middleware: [offset(8), flip(clipOptions), shift({ padding: 8 }), hide(clipOptions)],})- .then(({ x, y }) => {+ .then(({ x, y, middlewareData }) => {if (popup.isConnected) {popup.style.position = 'fixed';popup.style.left = `${x}px`;popup.style.top = `${y}px`;+ // Hide rather than clamp once the selection is fully occluded — a+ // clamped bar floats over the footer/composer with no visible+ // selection anchoring it.+ popup.style.visibility = middlewareData.hide?.referenceHidden ? 'hidden' : 'visible';}}).catch(() => {@@ -95,7 +105,19 @@ export function BubbleMenuBar({appendTo={() => document.body}shouldShow={shouldShowBubbleMenu}updateDelay={250}- options={{ onShow, onHide, strategy: 'fixed' }}+ // flip/shift/hide mirror the autoUpdate loop above: the plugin runs its+ // own computePosition on editor transactions (remote CRDT edits+ // included), so both paths must agree on clipping and on when the+ // selection counts as occluded — the plugin applies `referenceHidden`+ // itself.+ options={{+ onShow,+ onHide,+ strategy: 'fixed',+ flip: clipOptions,+ shift: { padding: 8 },+ hide: clipOptions,+ }}className="z-50 flex items-center gap-0.5 rounded-lg border bg-background p-1 shadow-md">{isImageMode ? (diff --git a/packages/app/src/editor/bubble-menu/bubble-menu-clip.dom.test.tsx b/packages/app/src/editor/bubble-menu/bubble-menu-clip.dom.test.tsxnew file mode 100644index 000000000..88d84f310--- /dev/null+++ b/packages/app/src/editor/bubble-menu/bubble-menu-clip.dom.test.tsx@@ -0,0 +1,106 @@+/**+ * DOM-tier tests for `deriveEditorClipOptions` — the floating-ui clipping+ * config that keeps the selection bubble menu inside the editor's visible+ * content region (scroll container minus the toolbar band and the live+ * bottom-composer / conflict-footer overlays).+ *+ * DOM tier because the derivable resolves the `.editor-doc-scroll` ancestor+ * via `closest()` on a rendered tree and reads the overlay-height CSS vars+ * off the document root's inline style — both real-DOM behaviors.+ *+ * The load-bearing property is liveness: the options function is re-invoked+ * per `computePosition` pass and must reflect the overlay vars *at that+ * moment* (the composer card grows with its draft and collapses to nothing),+ * not a snapshot from when the menu mounted.+ *+ * Invocation: `bun run test:dom` from `packages/app/`.+ */++import { afterEach, describe, expect, test } from 'bun:test';+import { cleanup, render } from '@testing-library/react';+import type { Editor } from '@tiptap/react';+import { TOOLBAR_HEIGHT } from '../extensions/frozen-table-headers';+import { deriveEditorClipOptions } from './bubble-menu-clip';++/** Renders the editor-DOM shape the derivable walks: a `.editor-doc-scroll`+ * scroll container wrapping the ProseMirror mount the editor points at. */+function renderEditorInScroller(): { editor: Editor; scroller: HTMLElement } {+ const { container } = render(+ <div className="editor-doc-scroll">+ <div data-testid="pm-mount" />+ </div>,+ );+ const scroller = container.querySelector('.editor-doc-scroll') as HTMLElement;+ const dom = container.querySelector('[data-testid="pm-mount"]') as HTMLElement;+ return { editor: { view: { dom } } as unknown as Editor, scroller };+}++afterEach(() => {+ cleanup();+ document.documentElement.style.removeProperty('--ask-composer-height');+ document.documentElement.style.removeProperty('--conflict-footer-height');+});++describe('deriveEditorClipOptions', () => {+ test('bounds to the editor scroll container with the toolbar band as top inset', () => {+ const { editor, scroller } = renderEditorInScroller();+ const options = deriveEditorClipOptions(editor)();+ expect(options.boundary).toBe(scroller);+ expect(options.padding).toEqual({ top: TOOLBAR_HEIGHT, bottom: 0 });+ });++ test('bottom inset tracks the published composer height', () => {+ const { editor } = renderEditorInScroller();+ document.documentElement.style.setProperty('--ask-composer-height', '236px');+ expect(deriveEditorClipOptions(editor)().padding.bottom).toBe(236);+ });++ test('conflict-footer overlay alone sets the bottom inset', () => {+ const { editor } = renderEditorInScroller();+ document.documentElement.style.setProperty('--conflict-footer-height', '48px');+ expect(deriveEditorClipOptions(editor)().padding.bottom).toBe(48);+ });++ test('composer and conflict-footer overlays stack into one bottom inset', () => {+ const { editor } = renderEditorInScroller();+ document.documentElement.style.setProperty('--ask-composer-height', '236px');+ document.documentElement.style.setProperty('--conflict-footer-height', '48px');+ expect(deriveEditorClipOptions(editor)().padding.bottom).toBe(284);+ });++ test('re-reads overlay heights on every invocation', () => {+ const { editor } = renderEditorInScroller();+ const derive = deriveEditorClipOptions(editor);+ expect(derive().padding.bottom).toBe(0);+ document.documentElement.style.setProperty('--ask-composer-height', '180px');+ expect(derive().padding.bottom).toBe(180);+ document.documentElement.style.removeProperty('--ask-composer-height');+ expect(derive().padding.bottom).toBe(0);+ });++ test('malformed overlay value reads as no inset', () => {+ const { editor } = renderEditorInScroller();+ document.documentElement.style.setProperty('--ask-composer-height', 'auto');+ expect(deriveEditorClipOptions(editor)().padding.bottom).toBe(0);+ });++ test('omits the boundary when no scroll container is resolvable', () => {+ const { container } = render(<div data-testid="pm-mount" />);+ const dom = container.querySelector('[data-testid="pm-mount"]') as HTMLElement;+ const editor = { view: { dom } } as unknown as Editor;+ const options = deriveEditorClipOptions(editor)();+ expect('boundary' in options).toBe(false);+ expect(options.padding.top).toBe(TOOLBAR_HEIGHT);+ });++ test('pre-mount editor (throwing view proxy) falls back to the default boundary', () => {+ const editor = {+ get view(): never {+ throw new Error('view not mounted');+ },+ } as unknown as Editor;+ const options = deriveEditorClipOptions(editor)();+ expect('boundary' in options).toBe(false);+ expect(options.padding.top).toBe(TOOLBAR_HEIGHT);+ });+});diff --git a/packages/app/src/editor/bubble-menu/bubble-menu-clip.ts b/packages/app/src/editor/bubble-menu/bubble-menu-clip.tsnew file mode 100644index 000000000..ef866dab6--- /dev/null+++ b/packages/app/src/editor/bubble-menu/bubble-menu-clip.ts@@ -0,0 +1,57 @@+import type { Editor } from '@tiptap/react';+import { TOOLBAR_HEIGHT } from '../extensions/frozen-table-headers';++/**+ * Floating-UI clipping options that keep selection-anchored menus inside the+ * editor's *visible* content region, not just inside the viewport.+ *+ * `.editor-doc-scroll` clips the document, but the region where a selection+ * actually reads as visible is smaller than the container's box: the+ * EditorToolbar overlays the container's top exclusion zone, and the Ask AI+ * bottom composer (stacked above the conflict-resolution footer when one is+ * up) floats over the container's bottom edge. None of these are clipping+ * ancestors, so a body-appended `position: fixed` bubble menu keeps tracking+ * a selection that has scrolled behind them — sliding over the composer card+ * and the status footer below the container.+ *+ * Pass the result to `flip()` so placement decisions stay inside the visible+ * region, and to `hide()` (default strategy `referenceHidden`) so the menu+ * disappears once the selection itself is fully occluded — matching what the+ * user can see rather than what the DOM clips.+ *+ * Shaped as a floating-ui derivable (re-evaluated on every `computePosition`+ * pass) because the composer publishes a live height — its card grows with+ * the draft and collapses to nothing — and the scroll container can be+ * remounted across document switches.+ */+export function deriveEditorClipOptions(editor: Editor) {+ return () => {+ let boundary: Element | null = null;+ try {+ boundary = editor.view.dom.closest('.editor-doc-scroll');+ } catch {+ // `editor.view` is a throwing proxy until the ProseMirror view mounts+ // (recycle/remount race) — fall back to the default boundary this pass.+ }+ const padding = {+ top: TOOLBAR_HEIGHT,+ bottom:+ readRootInlinePxVar('--ask-composer-height') ++ readRootInlinePxVar('--conflict-footer-height'),+ };+ // No resolvable scroll container (detached view, non-doc host): fall back+ // to floating-ui's default boundary rather than pinning a stale element.+ return boundary ? { boundary, padding } : { padding };+ };+}++/**+ * Both overlay heights are published as inline styles on the document root+ * (BottomComposer and use-conflict-footer-height.ts), so read the inline+ * declaration directly instead of paying a `getComputedStyle` resolution on+ * every scroll tick. Absent (overlay closed) or malformed values read as 0.+ */+function readRootInlinePxVar(name: string): number {+ const value = Number.parseFloat(document.documentElement.style.getPropertyValue(name));+ return Number.isFinite(value) ? value : 0;+}diff --git a/packages/app/src/editor/extensions/frozen-table-headers.ts b/packages/app/src/editor/extensions/frozen-table-headers.tsindex 877147959..362ff80ef 100644--- a/packages/app/src/editor/extensions/frozen-table-headers.ts+++ b/packages/app/src/editor/extensions/frozen-table-headers.ts@@ -57,8 +57,10 @@ import { Plugin, PluginKey } from 'prosemirror-state';// EditorToolbar is absolutely positioned at the top of the scroll container,// 3.5rem tall. Frozen headers must clear it. One of the four load-bearing// toolbar-height constants listed in components/EditorActivityPool.tsx —-// move them together.-const TOOLBAR_HEIGHT = 56;+// move them together. Exported so selection-anchored floating menus can treat+// the toolbar band as occluded (editor/bubble-menu/bubble-menu-clip.ts)+// instead of keeping a fifth drifting copy of the value.+export const TOOLBAR_HEIGHT = 56;// Subtle shadow to indicate the frozen row overlaps the table body.const FROZEN_SHADOW = '0 2px 4px rgba(0, 0, 0, 0.08)';