From 397023d7c0fedcebe9ed4806f162ee46a2670d1e Mon Sep 17 00:00:00 2001 From: Harvey Date: Sun, 22 Nov 2026 10:25:52 +0330 Subject: [PATCH 001/129] feat(bubble): compute the Style face --- components/smart-toolbar/bubble/style-face.ts | 92 ++++++++++++++ tests/unit/components/style-face.test.ts | 120 ++++++++++++++++++ 2 files changed, 212 insertions(+) create mode 100644 components/smart-toolbar/bubble/style-face.ts create mode 100644 tests/unit/components/style-face.test.ts diff --git a/components/smart-toolbar/bubble/style-face.ts b/components/smart-toolbar/bubble/style-face.ts new file mode 100644 index 00000000..48b9105c --- /dev/null +++ b/components/smart-toolbar/bubble/style-face.ts @@ -0,0 +1,92 @@ +import type { Node as PMNode } from "@tiptap/pm/model"; +import type { EditorState } from "@tiptap/pm/state"; + +/** + * The Style ▾ face (spec §7.1, §7.2): the selection's block type as one + * fixed word (BQ11). Faces use H1–H6; menus and names use "Heading 2". + */ +export type StyleFace = + | "Text" + | "H1" + | "H2" + | "H3" + | "H4" + | "H5" + | "H6" + | "Title" + | "List" + | "Quote" + | "Code" + | "Mixed"; + +/** Every face word, for sizing the face to the longest once (§7.1 Sizes). */ +export const STYLE_FACES: readonly StyleFace[] = [ + "Text", + "H1", + "H2", + "H3", + "H4", + "H5", + "H6", + "Title", + "List", + "Quote", + "Code", + "Mixed", +]; + +/** The face's accessible name, where the word alone is short ("H2"). */ +export const STYLE_FACE_NAMES: Readonly> = { + Text: "Text", + H1: "Heading 1", + H2: "Heading 2", + H3: "Heading 3", + H4: "Heading 4", + H5: "Heading 5", + H6: "Heading 6", + Title: "Title", + List: "List", + Quote: "Quote", + Code: "Code block", + Mixed: "Mixed", +}; + +const LISTS = new Set(["bulletList", "orderedList", "taskList"]); + +/** + * One top-level block's face, or null for a block that has no type to + * turn into (a rule, an image, a diagram), which the walk skips. A table + * reads as its cells' paragraphs (spec §7.2). + */ +function blockFace(node: PMNode, index: number): StyleFace | null { + const name = node.type.name; + if (index === 0 && name === "heading") return "Title"; + if (name === "heading") return `H${node.attrs.level as 1}`; + if (name === "paragraph" || name === "table") return "Text"; + if (LISTS.has(name)) return "List"; + if (name === "blockquote") return "Quote"; + if (name === "codeBlock") return "Code"; + return null; +} + +/** + * The face for `state`'s selection. Walks the top-level blocks the + * selection touches by index, never `nodesBetween` (which visits every + * block of a ⌘A range), and returns at the second distinct type, so the + * cost is O(blocks until two types differ) (spec §7.2 "Very long + * selection"). + */ +export function styleFace(state: EditorState): StyleFace { + const { doc, selection } = state; + const last = doc.childCount - 1; + const start = Math.min(selection.$from.index(0), last); + const end = Math.min(selection.$to.index(0), last); + let face: StyleFace | null = null; + for (let index = start; index <= end; index += 1) { + const own = blockFace(doc.child(index), index); + if (own === null || own === face) continue; + if (face !== null) return "Mixed"; + face = own; + } + return face ?? "Text"; +} diff --git a/tests/unit/components/style-face.test.ts b/tests/unit/components/style-face.test.ts new file mode 100644 index 00000000..3a7d588a --- /dev/null +++ b/tests/unit/components/style-face.test.ts @@ -0,0 +1,120 @@ +import { afterEach, describe, expect, it } from "bun:test"; + +import type { Editor } from "@tiptap/core"; +import { AllSelection, TextSelection } from "@tiptap/pm/state"; + +import { styleFace } from "@/components/smart-toolbar/bubble/style-face"; + +import { createTestEditor } from "../../helpers/create-test-editor"; +import { + blockquote, + bulletList, + codeBlock, + doc, + h1, + h2, + listItem, + orderedList, + p, + posInside, + taskItem, + taskList, +} from "../../helpers/document-builders"; + +let editor: Editor; + +function setup(...blocks: Parameters) { + editor = createTestEditor({ content: doc(...blocks).toJSON() }); +} + +afterEach(() => editor?.destroy()); + +/** Selects `from` through `to` (two words of the doc) as a text range. */ +function selectBetween(from: string, to: string) { + const start = posInside(editor, from); + const end = posInside(editor, to) + 1; + editor.view.dispatch( + editor.state.tr.setSelection( + TextSelection.create(editor.state.doc, start, end), + ), + ); +} + +function caretIn(word: string) { + editor.commands.setTextSelection(posInside(editor, word)); +} + +describe("styleFace", () => { + it("names a paragraph Text, a heading by level and the first H1 Title", () => { + setup(h1("Title"), h2("Section"), p("Body")); + caretIn("Body"); + expect(styleFace(editor.state)).toBe("Text"); + caretIn("Section"); + expect(styleFace(editor.state)).toBe("H2"); + caretIn("Title"); + expect(styleFace(editor.state)).toBe("Title"); + }); + + it("names bullet, ordered and task items List and a blockquote Quote", () => { + setup( + h1("Title"), + bulletList(listItem(p("Bullet"))), + orderedList(listItem(p("Ordered"))), + taskList(taskItem(p("Task"))), + blockquote(p("Quoted")), + codeBlock("const x = 1;"), + ); + for (const word of ["Bullet", "Ordered", "Task"]) { + caretIn(word); + expect(styleFace(editor.state)).toBe("List"); + } + caretIn("Quoted"); + expect(styleFace(editor.state)).toBe("Quote"); + caretIn("const"); + expect(styleFace(editor.state)).toBe("Code"); + }); + + it("is Mixed across a paragraph and a heading", () => { + setup(h1("Title"), p("Body"), h2("Section")); + selectBetween("Body", "Section"); + expect(styleFace(editor.state)).toBe("Mixed"); + }); + + it("is one type across blocks that share it", () => { + setup(h1("Title"), p("One"), p("Two")); + selectBetween("One", "Two"); + expect(styleFace(editor.state)).toBe("Text"); + }); + + it("is Mixed for select all, which includes the title", () => { + setup(h1("Title"), p("Body")); + editor.view.dispatch( + editor.state.tr.setSelection(new AllSelection(editor.state.doc)), + ); + expect(styleFace(editor.state)).toBe("Mixed"); + }); + + it("returns at the second distinct type: 2,000 paragraphs visit 2 blocks", () => { + setup( + h1("Title"), + p("Start"), + h2("Break"), + ...Array.from({ length: 2_000 }, (_, i) => p(`Filler ${i}`)), + ); + selectBetween("Start", "Filler 1999"); + const { doc: root } = editor.state; + const child = root.child.bind(root); + let visits = 0; + let walked = false; + root.child = (index: number) => { + visits += 1; + return child(index); + }; + root.nodesBetween = () => { + walked = true; + }; + expect(styleFace(editor.state)).toBe("Mixed"); + expect(visits).toBeLessThanOrEqual(2); + expect(walked).toBe(false); + }); +}); From 13862769d2bedc4559671d9e452fced69de49b41 Mon Sep 17 00:00:00 2001 From: Harvey Date: Sun, 22 Nov 2026 10:27:09 +0330 Subject: [PATCH 002/129] feat(slash): suggest recent insert kinds INSERT_KIND_CHOICE keeps the last 3 kinds (contract A19). The "/" menu leads with a Suggested section of the recent kinds that can run at the caret, each also in its own section; a query drops it and breaks rank ties toward recent kinds. --- components/smart-toolbar/slash/slash-menu.tsx | 8 +- .../2026-11-13-smart-toolbar-v1-contract.md | 5 + lib/remembered-choice.ts | 7 +- lib/slash-menu/slash-items.ts | 74 ++++++++++++-- tests/e2e/slash-menu.spec.ts | 32 ++++++ tests/unit/lib/remembered-choice.test.ts | 39 ++++++++ tests/unit/lib/slash-items.test.ts | 97 +++++++++++++++++++ 7 files changed, 252 insertions(+), 10 deletions(-) diff --git a/components/smart-toolbar/slash/slash-menu.tsx b/components/smart-toolbar/slash/slash-menu.tsx index b8d21313..9eb89fc2 100644 --- a/components/smart-toolbar/slash/slash-menu.tsx +++ b/components/smart-toolbar/slash/slash-menu.tsx @@ -20,7 +20,12 @@ import { import { editorActions } from "@/lib/editor-actions"; import { computeContext } from "@/lib/editor-context/snapshot"; import { SCROLL_CLEARANCE } from "@/lib/editor-scroll-clearance"; -import { readChoice, TABLE_SIZE_CHOICE } from "@/lib/remembered-choice"; +import { + INSERT_KIND_CHOICE, + isInsertKind, + readChoice, + TABLE_SIZE_CHOICE, +} from "@/lib/remembered-choice"; import { runSlashPick } from "@/lib/slash-menu/run-slash-pick"; import { buildSlashItems, @@ -238,6 +243,7 @@ export function SlashMenu({ editor, state, announce }: SlashMenuProps) { computeContext(editor), editorActions.list(), readChoice(TABLE_SIZE_CHOICE).last, + readChoice(INSERT_KIND_CHOICE).recent.filter(isInsertKind), ), ); const [coarse] = useState(isCoarse); diff --git a/docs/superpowers/specs/2026-11-13-smart-toolbar-v1-contract.md b/docs/superpowers/specs/2026-11-13-smart-toolbar-v1-contract.md index 39290967..45a22f13 100644 --- a/docs/superpowers/specs/2026-11-13-smart-toolbar-v1-contract.md +++ b/docs/superpowers/specs/2026-11-13-smart-toolbar-v1-contract.md @@ -747,6 +747,11 @@ Recorded by the owner of the files they touch. Each one names what changed and w - **§3.2:** the "/" menu (`lib/slash-menu/slash-items.ts`) is a third view that lists from the registry, next to the palette and Insert. It lists the `style` and `list` groups and the `insert.*` actions that can run at the caret (the others only say why when a query matches nothing else), and, as Insert's Diagram ▸ (design pass §9), shows Diagram as one row or as the `template.mermaid` options, never both. Picks run through `runAction` (`lib/slash-menu/run-slash-pick.ts`, one Yjs undo step). - **Eager:** `SlashTrigger` (`extensions/slash-trigger/slash-trigger.ts`, one plugin with state only) and `SlashMenuHost`. Keys (a window capture listener), the combobox attributes (set on `view.dom`), items, matching and the pick runner are the lazy menu chunk, fetched on idle and on the first "/". - **Measured** (same method as A9 and A13; both builds with the same `NEXT_PUBLIC_*` values) on `main` at `b7eddfb` (396,248 B gz, +18,360 B) plus this change: **397,136 B gz, +19,248 B (+888 B eager), 4,816 B under the 23.5 KB budget**, 10 files. On `4b55ec6` it was +923 B; a first build with keys and attributes in the trigger measured +1,398 B there. The menu chunk: **5,291 B gz** (`SLASH_BUDGET=1`, budget 6 KB). The palette's first Alt+/ (`PALETTE_BUDGET=1`, `next start`): 403 B + 20,072 B = **20,475 B, the same files at the same sizes as `main`.** +- **A19 (bubble and quick insert P2, 2026-11-22): `INSERT_KIND_CHOICE` keeps 3 recent kinds.** Spec: `docs/superpowers/specs/2026-11-17-bubble-and-quick-insert-design.md` §8.5 (O12 a; the spec calls this A18, which P0's branch took first). No new slot, key, write path or `STORAGE_VERSION`, so `contract.test.ts` is unchanged. + - **§6:** `INSERT_KIND_CHOICE.maxRecent` goes from 0 to 3. `last`, which the row's Insert face reads (M10), is unchanged. The stored shape was always `{ last, recent }`, so entries written while `maxRecent` was 0 (`recent: []`) still read; `remembered-choice.test.ts` pins this. + - **Writes:** unchanged. The `insert.*` actions' `run()` already calls `rememberChoice(INSERT_KIND_CHOICE, kind)` after the command returned true; `recent` now keeps the newest 3, deduplicated. + - **Reads:** the "/" menu's Suggested section (`buildSlashItems(…, recentKinds)`) shows the recent kinds that can run at the caret, newest first; each also stays in its own section. No recents, no section. A query drops Suggested and breaks rank ties toward recent kinds. + - **Privacy and Forget:** local only, under `tinydocy-choices`, so "Forget my choices" clears it and `NEXT_PUBLIC_REMEMBER_CHOICES=0` reads none. ### Open owner decisions (v1 → `main` blockers) Not decided by any track. Each needs the owner's call recorded here before v1 opens against `main`. O1, O2, O3, O4, O6, O8, O9 and O10 are decided; O7 is closed by measurement. O5 is the owner's palette budget, which E records as A7 on `feat/smart-toolbar-e-v11`; it is not a v1 item. diff --git a/lib/remembered-choice.ts b/lib/remembered-choice.ts index 52677359..d78d84ae 100644 --- a/lib/remembered-choice.ts +++ b/lib/remembered-choice.ts @@ -341,10 +341,13 @@ export const LIST_TYPE_CHOICE: ChoiceSpec = { fallback: "bulletList", }; -/** M10: the last inserted kind (the T1 face; pre-highlighted in the menu). */ +/** + * M10: the last inserted kind (the T1 face; pre-highlighted in the menu). + * A19: its last 3 kinds are the "/" menu's Suggested section. + */ export const INSERT_KIND_CHOICE: ChoiceSpec = { slot: "insertKind", - maxRecent: 0, + maxRecent: 3, validate: isInsertKind, // Nothing remembered: the Insert face stays "+ Insert" (D2 Decision 1). fallback: null, diff --git a/lib/slash-menu/slash-items.ts b/lib/slash-menu/slash-items.ts index 07f5c2b6..496390b7 100644 --- a/lib/slash-menu/slash-items.ts +++ b/lib/slash-menu/slash-items.ts @@ -21,9 +21,14 @@ import { NetworkIcon, TypeIcon, } from "@/lib/icons"; -import type { TableSize } from "@/lib/remembered-choice"; +import type { InsertKind, TableSize } from "@/lib/remembered-choice"; -export type SlashSection = "Text" | "Lists" | "Insert" | "Diagrams"; +export type SlashSection = + | "Suggested" + | "Text" + | "Lists" + | "Insert" + | "Diagrams"; export interface SlashItem { /** `actionId`, or `actionId: