Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ since the previous tag.

## [Unreleased]

### Selection bubble P0: a plugin-free controller and a first-line anchor

The selection bubble no longer registers a ProseMirror plugin from its lazy
chunk, and it anchors to the line it belongs to (spec
`2026-11-17-bubble-and-quick-insert-design.md` §12.1, v1 contract A18). The
editor chunk group is unchanged (393,171 B gz, −1 B against `main`), and the
bubble chunk shrinks from 12,238 B to 9,798 B gz (−2,440 B).

#### Changed

- **Bubble:** a `BubbleController` per editor shows, hides and positions the
bubble instead of Tiptap's `BubbleMenu`, so the bubble chunk landing no
longer rebuilds all 29 plugin views. The DOM contract is unchanged: one
`[data-smart-bubble]` root, appended to the scroll container on show and
removed on hide.
- **Bubble:** it anchors to the first selected line that shows, not to the
box between the selection's two ends: a selection that wraps onto the
next line is centred on its selected part of the first line, not the
middle of the column, and one that starts at the end of a paragraph
anchors to the next line's text, never to a whole block. Below the text,
it stays in view when the selection runs past the bottom of the window.
- **Bubble:** Esc on a bubble item plays the 90 ms exit fade before the
bubble goes; a re-shown bubble never flashes at its old position.

#### Fixed

- **Bubble:** select all (⌘A / Ctrl+A) shows the bubble, under the first
visible line; a whitespace-only selection shows none.

### Desktop P2.1b: `local-session`, the batcher and `MemoryDocStore`

The second half of desktop plan P2.1: the platform-neutral storage path that
Expand Down
213 changes: 213 additions & 0 deletions components/smart-toolbar/bubble/bubble-anchor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import { posToDOMRect } from "@tiptap/core";
import type { Node as ProseMirrorNode } from "@tiptap/pm/model";
import { TextSelection } from "@tiptap/pm/state";
import type { EditorView } from "@tiptap/pm/view";

import {
type BubblePlacement,
type BubbleRect,
firstLineRect,
lastLineRect,
} from "./bubble-rules";

/**
* The bubble's anchor (spec §12.1, BQ3): one selected line inside the
* visible band, never the union rectangle, measured from a DOM range of at
* most `MAX_RANGE` positions. `getClientRects` over a ⌘A range would lay
* out every `content-visibility`-skipped block.
*/

/** Most positions one anchor range spans. */
const MAX_RANGE = 300;
const STICKY_TOOLBAR = ':scope > [data-slot="toolbar"]';

interface Band {
top: number;
bottom: number;
}

/** Under the sticky row (else the scroll target's top), above its bottom. */
function visibleBand(scrollTarget: HTMLElement): Band {
const box = scrollTarget.getBoundingClientRect();
const toolbar = scrollTarget.querySelector(STICKY_TOOLBAR);
return {
top: toolbar ? toolbar.getBoundingClientRect().bottom : box.top,
bottom: box.bottom,
};
}

const clamp = (value: number, min: number, max: number) =>
Math.min(max, Math.max(min, value));

/**
* The document position where the line `top` px down starts. Both inline
* edges are probed and the smaller position wins: in a right-to-left line
* the left edge is the line's logical end.
*/
function posAtBandEdge(view: EditorView, top: number): number | undefined {
const { left, right } = view.dom.getBoundingClientRect();
let found: number | undefined;
for (const x of [left + 1, right - 1]) {
const pos = view.posAtCoords({ left: x, top })?.pos;
if (pos !== undefined && (found === undefined || pos < found)) found = pos;
}
return found;
}

/** Client rects of the DOM range between two document positions. */
function rangeRects(view: EditorView, from: number, to: number): DOMRect[] {
const start = view.domAtPos(from);
const end = view.domAtPos(to);
const range = view.dom.ownerDocument.createRange();
range.setStart(start.node, start.offset);
range.setEnd(end.node, end.offset);
return Array.from(range.getClientRects());
}

/**
* `pos`, or the nearest position inside a textblock in direction `dir`,
* never past `limit`. A range that starts (or ends) on a block boundary
* holds that whole block, and `getClientRects` returns its border box: the
* "first line" of select all would be the title's whole box.
*/
export function intoTextblock(
doc: ProseMirrorNode,
pos: number,
dir: 1 | -1,
limit: number,
): number {
const $pos = doc.resolve(pos);
if ($pos.parent.inlineContent) return pos;
const found = TextSelection.findFrom($pos, dir, true)?.head;
if (found === undefined) return pos;
return dir > 0 ? Math.min(found, limit) : Math.max(found, limit);
}

/**
* The first line that shows in the band, read forward from `start` one
* textblock at a time: each range stays inside one textblock, so it never
* holds a whole block (whose border box `getClientRects` would return).
* A position at a textblock's end, or an empty textblock, has no line:
* the scan goes on to the next. At most `MAX_RANGE` positions in all.
*/
function firstLineFrom(
view: EditorView,
band: Band,
start: number,
to: number,
) {
const { doc } = view.state;
let pos = start;
let budget = MAX_RANGE;
while (budget > 0 && pos < to) {
pos = intoTextblock(doc, pos, 1, to);
const $pos = doc.resolve(pos);
if (!$pos.parent.inlineContent) return null;
const end = Math.min($pos.end(), to, pos + budget);
if (end > pos) {
const line = firstLineRect(rangeRects(view, pos, end), band);
if (line) return line;
}
budget -= Math.max(1, end - pos);
pos = $pos.after();
}
return null;
}

/** Like `firstLineFrom`, backward from `end` for the last line. */
function lastLineFrom(view: EditorView, band: Band, from: number, end: number) {
const { doc } = view.state;
let pos = end;
let budget = MAX_RANGE;
while (budget > 0 && pos > from) {
pos = intoTextblock(doc, pos, -1, from);
const $pos = doc.resolve(pos);
if (!$pos.parent.inlineContent) return null;
const start = Math.max($pos.start(), from, pos - budget);
if (pos > start) {
const line = lastLineRect(rangeRects(view, start, pos), band);
if (line) return line;
}
budget -= Math.max(1, pos - start);
pos = $pos.before();
}
return null;
}

/**
* First selected line in the band, from where the band starts. Positions
* are moved into a textblock before `coordsAtPos` reads them: at the
* document's edges it measures whatever the view's first or last child is.
*/
function topAnchor(view: EditorView, band: Band, from: number, to: number) {
let start = intoTextblock(view.state.doc, from, 1, to);
if (view.coordsAtPos(start).bottom < band.top) {
const hit = posAtBandEdge(view, band.top + 1);
if (hit !== undefined) start = clamp(hit, from, to);
}
return firstLineFrom(view, band, start, to);
}

/**
* Last selected line, with `room` px free under it inside the band. When
* the selection runs past the band's bottom (select all), or its last line
* sits too close to that edge, the bubble under it would leave the view:
* it goes under the first line that shows instead.
*/
function bottomAnchor(
view: EditorView,
band: Band,
from: number,
to: number,
room: number,
) {
const end = intoTextblock(view.state.doc, to, -1, from);
if (view.coordsAtPos(end).top > band.bottom) {
return topAnchor(view, band, from, to);
}
const line = lastLineFrom(view, band, from, end);
if (line && line.bottom + room > band.bottom) {
return topAnchor(view, band, from, to);
}
return line;
}

const toDOMRect = ({ left, top, right, bottom }: BubbleRect) =>
new DOMRect(left, top, right - left, bottom - top);

/** The selection's union rect (the plugin's anchor), or an empty rect. */
function selectionRect(view: EditorView, from: number, to: number): DOMRect {
try {
return toDOMRect(posToDOMRect(view, from, to));
} catch {
return new DOMRect();
}
}

/**
* The rect the bubble anchors to: the first selected line that shows in
* the scroll target's band for `"top"`, the last selected line for
* `"bottom"` (the first that shows when the selection runs past the band,
* or when `room`, the bubble's height plus its offset, does not fit under
* the last). Falls back to the selection's union rect when no line shows
* or measuring throws.
*/
export function bubbleReferenceRect(
view: EditorView,
scrollTarget: HTMLElement,
placement: BubblePlacement,
room = 0,
): DOMRect {
const { from, to } = view.state.selection;
try {
const band = visibleBand(scrollTarget);
const line =
placement === "top"
? topAnchor(view, band, from, to)
: bottomAnchor(view, band, from, to, room);
if (line) return toDOMRect(line);
} catch {
// Positions the DOM no longer has (a remote step replaced the block).
}
return selectionRect(view, from, to);
}
Loading
Loading