From 51e1cd80b08af60a0a7c5560417de0d0ed438e88 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Thu, 15 Oct 2026 12:17:14 +0000 Subject: [PATCH] feat(notes): a note says where it is and what's inside it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Above a note's title, Notes › namespace › each note it's inside, every one a link; under its text, Inside this note lists the notes inside it with Add a note inside. The text starts in line with the title (BlockSuite's own side margin is gone), the word count shows next to Saved, Pin, History, Download and Delete move into the note's ⋯ menu, and Costs and activity is folded and loads only when opened. GET /notes/{pid} gives path and children; read_note gives path and notes_inside. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01RrY2byt5BHVeBXJUyfwWpb --- CHANGELOG.md | 4 + README.md | 10 +- docs/api.md | 10 +- docs/notes.md | 11 + docs/requirements.md | 1 + fastapi_backend/app/api/mcp_tools.py | 5 +- fastapi_backend/app/api/v1/routes/notebook.py | 2 + fastapi_backend/app/domain/ai_tools.py | 5 +- fastapi_backend/app/domain/notebook.py | 23 +++ fastapi_backend/app/schemas/notebook.py | 8 + fastapi_backend/tests/api/test_mcp.py | 4 +- fastapi_backend/tests/api/test_notebook.py | 24 +++ nextjs-frontend/__tests__/note-page.test.tsx | 135 ++++++++++++ nextjs-frontend/app/openapi-client/index.ts | 1 + .../app/openapi-client/types.gen.ts | 31 +++ .../components/notes/block-editor.tsx | 5 +- .../components/notes/note-page.tsx | 195 ++++++++++++++++-- nextjs-frontend/openapi.json | 48 +++++ 18 files changed, 494 insertions(+), 28 deletions(-) create mode 100644 nextjs-frontend/__tests__/note-page.test.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 2493b7e30..2695c9939 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,10 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version - **Templates.** Notes inside a namespace's Templates note are templates: **New note from a template…** on the Notes page and in Jump to starts a note from one, with its text, drawings, files and filing. Any note's menu in the tree can also **Make a copy** ([Templates](docs/notes.md#templates)). +- **A note says where it is.** Above a note's title, **Notes › namespace ›** each note it's inside, every one a link; + under its text, **Inside this note** lists the notes inside it with **Add a note inside**. The text now starts in line + with the title, the word count shows next to Saved, and Pin, History, Download and Delete are in the note's **⋯** + menu. **Costs and activity** is folded at the bottom ([On a note](docs/notes.md#on-a-note)). - **Automations for agents.** Agents list, save, try and run automations through the MCP tools `automations`, `automation_actions`, `save_automation`, `test_automation` and `run_automation`, with problems in plain words. The assistant saves them switched off and asks before switching one on or running it diff --git a/README.md b/README.md index 52c4669a6..07431bf3d 100644 --- a/README.md +++ b/README.md @@ -126,11 +126,11 @@ listed under [In progress](#in-progress), and what comes next under [Planned](#p [long recordings](docs/speech-providers.md#long-recordings); [checks](docs/requirements.md#understand-it)) - **Learn from it.** Make a two-host podcast episode about what you pick, every claim cited and checked against its source, read aloud in two voices. Keep notes beside every asset, entity and topic, written by you or the assistant, in - a tree where notes are renamed, moved, copied and pinned (pins are your own), with today's note one click away, - templates to start from, a search by their words, a download as Markdown, and Undo and Recently deleted instead of a - confirm. Export an asset or a collection as an evidence PDF with everything Lens found about it, or the parts you - pick. ([Podcasts](docs/podcasts.md), [notes](docs/notes.md), - [evidence PDFs](docs/api.md#evidence-pdfs); [checks](docs/requirements.md#learn-from-it)) + a tree where notes are renamed, moved, copied and pinned (pins are your own) and each note shows the notes it's + inside and the ones inside it, with today's note one click away, templates to start from, a search by their words, a + download as Markdown, and Undo and Recently deleted instead of a confirm. Export an asset or a collection as an + evidence PDF with everything Lens found about it, or the parts you pick. ([Podcasts](docs/podcasts.md), + [notes](docs/notes.md), [evidence PDFs](docs/api.md#evidence-pdfs); [checks](docs/requirements.md#learn-from-it)) - **Automate it.** Pipelines and workflows on a canvas, routines on a schedule, batch runs over a collection, and notifications to webhooks, Slack, Discord and Matterbridge. Everything that happens in Lens (new assets, emails, calendar events, files, what people say to Lens, sensor readings, notes, graph changes, runs, budget warnings) is kept diff --git a/docs/api.md b/docs/api.md index 81552f2da..523b44320 100644 --- a/docs/api.md +++ b/docs/api.md @@ -842,10 +842,12 @@ DELETE /api/v1/notes/{pid}/files ``` Note pages ([Notes](notes.md)), apart from the notes on an asset above. `GET /notes?ns=` is a namespace's page tree -(`all=true` adds the pages of things). `today` gives the namespace's note for `date`, made the first time it's asked -for ([Today's note](notes.md#todays-note)). `templates` lists the notes inside the namespace's Templates note, and -`POST /notes/templates` makes that note the first time; `copy` makes a new note from a page, with its text, drawings, -files and filing ([Templates](notes.md#templates)). `search?q=` finds pages by the words in their title, summary or text, title +(`all=true` adds the pages of things). `GET /notes/{pid}` gives a page with its text, links, backlinks, `path` (the +notes it's inside, from the top) and `children` (the notes inside it) ([On a note](notes.md#on-a-note)). `today` gives +the namespace's note for `date`, made the first time it's asked for ([Today's note](notes.md#todays-note)). +`templates` lists the notes inside the namespace's Templates note, and `POST /notes/templates` makes that note the first +time; `copy` makes a new note from a page, with its text, drawings, files and filing ([Templates](notes.md#templates)). +`search?q=` finds pages by the words in their title, summary or text, title matches first, each with the words around the match ([Finding notes](notes.md#finding-notes)). `DELETE /notes/{pid}` moves a page to recently deleted for 30 days: `deleted` lists them, `restore` puts one back where it was and `DELETE /notes/deleted/{pid}` deletes one for good ([Deleting](notes.md#deleting-and-putting-back)). `move` puts a diff --git a/docs/notes.md b/docs/notes.md index 9a8dd6fe4..ee98f3fee 100644 --- a/docs/notes.md +++ b/docs/notes.md @@ -31,6 +31,17 @@ draft with its name and the pages that already link to it. A page follows its as thing is deleted, or an entity is merged into another, its page stays as a free note at the top of the tree, so nothing written on it is lost. +## On a note + +Above the title, a note says where it is: **Notes › namespace ›** each note it's inside, every one a link. Next to that +are how many words it has, whether it's saved, and its **⋯** menu: **Add a note inside**, **Pin**, **History**, +**Download as Markdown** and **Delete** (with Undo). The text starts in line with the title. Under the text, **Inside +this note** lists the notes inside it in the tree's order, with **Add a note inside**, then the notes that link to it; +**Costs and activity** is folded at the bottom and loads when opened. + +`GET /api/v1/notes/{pid}` gives `path` (the notes it's inside, from the top) and `children` (the notes inside it); the +assistant's and MCP's `read_note` give them as `path` and `notes_inside`. + ## Entity pages Every entity (a person, organisation, product, place, event, work or term) has a page of its own, named by its name diff --git a/docs/requirements.md b/docs/requirements.md index 41ecf6972..56ad8cc10 100644 --- a/docs/requirements.md +++ b/docs/requirements.md @@ -152,6 +152,7 @@ request, to another check that proves the same thing; loosening a requirement or | A tree of notes renamed, moved and pinned (pins are your own), today's note one click away, and a search | [test_notebook.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_notebook.py) `test_tree`, `test_pins`, `test_search`; [notes-tree.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/notes-tree.test.tsx); [notes-today.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/notes-today.test.tsx) `is one click from the Notes page` | | A note, or all of a namespace's notes, downloaded as Markdown in folders that follow the tree | [test_notebook.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_notebook.py) `test_markdown_export`; [notes-tree.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/notes-tree.test.tsx) `downloads a note as Markdown`; [notes-find.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/notes-find.test.tsx) `downloads every note as Markdown from its menu` | | Templates a note starts from, and a copy of any note | [test_notebook.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_notebook.py) `test_templates_and_copies`; [notes-templates.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/notes-templates.test.tsx); [notes-tree.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/notes-tree.test.tsx) `makes a copy beside the note and opens it` | +| A note shows the notes it's inside and the notes inside it, to people and agents | [test_notebook.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_notebook.py) `test_path_and_children`, `test_assistant_keeps_notes`; [test_mcp.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_mcp.py) `test_note_tools`; [note-page.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/note-page.test.tsx) | | Undo and Recently deleted instead of a confirm | [test_notebook.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_notebook.py) `test_recently_deleted`; [notes-delete.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/notes-delete.test.tsx) | | An asset or a collection exported as an evidence PDF, whole or the parts you pick | [test_evidence.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_evidence.py) `test_a_collection_as_one_evidence_pdf`, `test_everything_lens_found_goes_in`, `test_picking_the_parts`; [evidence-parts.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/evidence-parts.test.tsx) | diff --git a/fastapi_backend/app/api/mcp_tools.py b/fastapi_backend/app/api/mcp_tools.py index 39bab5979..60da0a8dc 100644 --- a/fastapi_backend/app/api/mcp_tools.py +++ b/fastapi_backend/app/api/mcp_tools.py @@ -1683,7 +1683,8 @@ def find_notes(ctx: Context, query: str | None, namespace: str | None, place: st @tool( "read_note", "Read a note", - "A note's Markdown, with what it links to and the notes linking to it: by note_id, or the page of a thing " + "A note's Markdown, with what it links to, the notes linking to it, the notes it is inside (path, from the top) and " + "the notes inside it: by note_id, or the page of a thing " '(about, like "recording:12", "entity:5" or "topic:9"). Links in the Markdown are written @[label](kind:id) and, for ' "topics, #[label](topic:id).", Arg("note_id", "integer", "the note (from find_notes)"), @@ -1716,6 +1717,8 @@ def read_note(ctx: Context, note_id: int | None, about: str | None) -> dict[str, "body": _cut(p.body, FETCH_CHARS), "links": [{"target": x.target, "name": x.name} for x in p.links] or None, "linked_from": [{"note_id": b.page, "title": b.title} for b in p.backlinks] or None, + "path": [{"note_id": c.id, "title": c.title} for c in p.path] or None, + "notes_inside": [{"note_id": c.id, "title": c.title} for c in p.children] or None, "url": _note_url(ctx, p.id), } ) diff --git a/fastapi_backend/app/api/v1/routes/notebook.py b/fastapi_backend/app/api/v1/routes/notebook.py index d9ba3bbd5..3872b1d2c 100644 --- a/fastapi_backend/app/api/v1/routes/notebook.py +++ b/fastapi_backend/app/api/v1/routes/notebook.py @@ -88,6 +88,8 @@ def _out(acl: Access, db: DB, p: dict[str, Any], user: Principal) -> NotePage: created_by=(writer or {}).get("email"), links=out_links, backlinks=[b for b in _backlinks(acl, db, p["space"], targets) if b["page"] != p["id"]], + path=notebook.path(db, p), + children=notebook.children(db, p) if not p.get("about") else [], can_edit=user.can_write and auth.allows(acl.roles, p["space"], "editor"), ) diff --git a/fastapi_backend/app/domain/ai_tools.py b/fastapi_backend/app/domain/ai_tools.py index 04859908f..3989f63d8 100644 --- a/fastapi_backend/app/domain/ai_tools.py +++ b/fastapi_backend/app/domain/ai_tools.py @@ -275,7 +275,8 @@ ), ( "read_note", - "Read a note's Markdown, with its links and the notes linking to it: by note_id, or the page of a thing " + "Read a note's Markdown, with its links, the notes linking to it, the notes it's inside (path) and the notes " + "inside it: by note_id, or the page of a thing " '(about, like "recording:12" or "entity:5").', {"note_id": _I, "about": _S}, [], @@ -1752,6 +1753,8 @@ def t_read_note(self, note_id=None, about=None): "body": (p.get("body") or "")[:20_000], "links": notebook.links(self.db, p["id"]), "linked_from": [b for b in notebook.backlinks(self.db, p["space"], targets) if b["page"] != p["id"]], + "path": [{"id": x["id"], "title": x["title"]} for x in notebook.path(self.db, p)] or None, + "notes_inside": [{"id": x["id"], "title": x["title"]} for x in notebook.children(self.db, p)] or None, "could_link": [f"{x['sign']}[{x['label']}]({x['target']})" for x in notebook.suggest_links(self.db, p)] or None, "files": [{"name": f.get("name"), "type": f["type"], "size": f["size"]} for f in notebook.files(self.db, p["id"])] or None, "could_go_in": [ diff --git a/fastapi_backend/app/domain/notebook.py b/fastapi_backend/app/domain/notebook.py index 7b968c715..b4ab881a8 100644 --- a/fastapi_backend/app/domain/notebook.py +++ b/fastapi_backend/app/domain/notebook.py @@ -174,6 +174,29 @@ def _check_parent(db, sid, pid, parent): return int(parent) +def path(db, p): + """The notes p is inside, from the top of the tree down to its parent: [{id, title}], for breadcrumbs.""" + out, at = [], p.get("parent") + while at is not None and len(out) <= DEPTH_MAX: + row = db.one("SELECT record::id(id) AS id, title, parent FROM $r", r=R("note_page", int(at))) + if not row: + break + out.append({"id": row["id"], "title": row["title"]}) + at = row.get("parent") + return out[::-1] + + +def children(db, p): + """The notes inside p, in the tree's order: [{id, title, updated_at}].""" + rows = db.rows( + "SELECT record::id(id) AS id, title, position, updated_at FROM note_page WHERE space = $s AND parent = $p AND about = NONE", + s=p["space"], + p=int(p["id"]), + ) + rows.sort(key=lambda r: (r.get("position") or 0, r["id"])) + return [{"id": r["id"], "title": r["title"], "updated_at": r.get("updated_at")} for r in rows] + + def _last_position(db, sid, parent): rows = db.values( "SELECT VALUE position FROM note_page WHERE space = $s AND parent = $p AND about = NONE", diff --git a/fastapi_backend/app/schemas/notebook.py b/fastapi_backend/app/schemas/notebook.py index 3837a730f..a6792d1d9 100644 --- a/fastapi_backend/app/schemas/notebook.py +++ b/fastapi_backend/app/schemas/notebook.py @@ -52,6 +52,12 @@ class NoteBacklink(ResponseModel): updated_at: str | None = None +class NoteCrumb(ResponseModel): + id: int + title: str + updated_at: str | None = None + + class NoteVersionItem(ResponseModel): id: int at: str = Field(description="when the page changed from this") @@ -79,6 +85,8 @@ class NotePage(NotePageItem): created_by: str | None = Field(None, description="its writer's email") links: list[NoteLink] = [] backlinks: list[NoteBacklink] = [] + path: list[NoteCrumb] = Field([], description="the notes it's inside, from the top of the tree down to its parent") + children: list[NoteCrumb] = Field([], description="the notes inside it, in the tree's order") can_edit: bool = False diff --git a/fastapi_backend/tests/api/test_mcp.py b/fastapi_backend/tests/api/test_mcp.py index 6e720b9df..83cbf0340 100644 --- a/fastapi_backend/tests/api/test_mcp.py +++ b/fastapi_backend/tests/api/test_mcp.py @@ -474,7 +474,7 @@ def test_note_tools(client, db, env): plan = notebook.create( db, pods, None, "Capsid plan", f"Ship on Friday, see @[ep1](recording:{a}).", "Shipping capsids", place="project" ) - notebook.create(db, pods, None, "Hiring", "Two roles open.") + hiring = notebook.create(db, pods, None, "Hiring", "Two roles open.", parent=plan) notebook.create(db, calls, None, "Capsid call notes", "Not for pods viewers.") found = tool(client, h, "find_notes", query="capsid") assert [(n["note_id"], n["summary"], n["place"]) for n in found["notes"]] == [(plan, "Shipping capsids", "project")] @@ -482,6 +482,8 @@ def test_note_tools(client, db, env): assert tool(client, h, "find_notes", query="friday")["notes"][0]["snippet"] == "Ship on Friday, see ep1." assert tool(client, h, "find_notes", place="area")["total"] == 0 got = tool(client, h, "read_note", note_id=plan) + assert got["notes_inside"] == [{"note_id": hiring, "title": "Hiring"}] and "path" not in got + assert tool(client, h, "read_note", note_id=hiring)["path"] == [{"note_id": plan, "title": "Capsid plan"}] assert got["body"].startswith("Ship on Friday") and got["links"] == [{"target": f"recording:{a}", "name": got["links"][0]["name"]}] assert tool(client, h, "read_note", about=f"recording:{a}") == { "page_of": f"recording:{a}", diff --git a/fastapi_backend/tests/api/test_notebook.py b/fastapi_backend/tests/api/test_notebook.py index 6e8cb0368..4b72fcb3e 100644 --- a/fastapi_backend/tests/api/test_notebook.py +++ b/fastapi_backend/tests/api/test_notebook.py @@ -84,6 +84,26 @@ def test_tree(client, env): assert sorted((p["title"], p["parent"]) for p in tree) == [("Budget", None), ("Hiring", None)] +def test_path_and_children(client, env): + """A note says which notes it's inside, from the top down, and lists the notes inside it in the tree's order.""" + he, hv = env["he"], env["hv"] + top = _new(client, he, title="Home lab") + mid = _new(client, he, title="Pi 5", parent=top["id"]) + leaf = _new(client, he, title="Shopping list", parent=mid["id"]) + second = _new(client, he, title="Cases", parent=mid["id"]) + client.post(f"/api/v1/notes/{second['id']}/move", headers=he, json={"parent": mid["id"], "before": leaf["id"]}) + got = client.get(f"/api/v1/notes/{leaf['id']}", headers=hv).json() + assert [(c["id"], c["title"]) for c in got["path"]] == [(top["id"], "Home lab"), (mid["id"], "Pi 5")] + assert got["children"] == [] + got = client.get(f"/api/v1/notes/{mid['id']}", headers=hv).json() + assert [c["title"] for c in got["path"]] == ["Home lab"] + assert [c["title"] for c in got["children"]] == ["Cases", "Shopping list"] + assert client.get(f"/api/v1/notes/{top['id']}", headers=hv).json()["path"] == [] + # a thing's page isn't in the tree, so nothing is inside it + page = _new(client, he, title="About the call", about=f"recording:{env['a']}") + assert (page["path"], page["children"]) == ([], []) + + def test_links_and_backlinks(client, env, db): he, a = env["he"], env["a"] term, person = _entity(db, "Capsids", "TERM"), _entity(db, "Ada Lovelace", "PERSON") @@ -278,6 +298,10 @@ def call(name, **args): got = call("read_note", note_id=w["note_id"]) assert got["body"].endswith("Alice sends them.") and got["summary"] == "When the samples ship" assert got["links"] == [{"sign": "@", "target": f"recording:{env['a']}", "label": "ep1"}] + assert "path" not in got and "notes_inside" not in got # at the top, with nothing inside + kid = call("write_note", namespace="pods", title="Samples", body="Two boxes.", parent_id=w["note_id"])["note_id"] + assert call("read_note", note_id=w["note_id"])["notes_inside"] == [{"id": kid, "title": "Samples"}] + assert call("read_note", note_id=kid)["path"] == [{"id": w["note_id"], "title": "Shipping"}] assert call("read_note", about=f"recording:{env['a']}")["note"] is None assert [n["id"] for n in call("find_notes", query="alice")["notes"]] == [w["note_id"]] assert call("find_notes", place="area")["notes"] == [] diff --git a/nextjs-frontend/__tests__/note-page.test.tsx b/nextjs-frontend/__tests__/note-page.test.tsx new file mode 100644 index 000000000..ceb51bcfb --- /dev/null +++ b/nextjs-frontend/__tests__/note-page.test.tsx @@ -0,0 +1,135 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Notes } from "@/app/openapi-client"; +import { NotePage, wordCount } from "@/components/notes/note-page"; +import { TooltipProvider } from "@/components/ui/tooltip"; + +jest.mock("@/app/openapi-client", () => ({ + Notes: { + getPage: jest.fn(), + createPage: jest.fn(), + updatePage: jest.fn(), + deletePage: jest.fn(), + restoreDeleted: jest.fn(), + pinPage: jest.fn(), + unpinPage: jest.fn(), + }, +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +const push = jest.fn(); +jest.mock("next/navigation", () => ({ + useRouter: () => ({ push }), + useSearchParams: () => new URLSearchParams(), +})); +// the editor loads BlockSuite in a browser; here it only has to be there +jest.mock("next/dynamic", () => () => () =>
); +const toast = jest.fn(); +jest.mock("@/components/ui/toast", () => ({ useToast: () => toast })); +const download = jest.fn, [string, string | undefined, string]>(() => Promise.resolve()); +jest.mock("@/components/library/actions", () => ({ + downloadWithToken: (url: string, token: string | undefined, name: string) => download(url, token, name), +})); +jest.mock("@/components/costs/costs", () => ({ ActivityPanel: () =>
})); +jest.mock("@/components/notes/home-suggestions", () => ({ HomeSuggestion: () => null })); +jest.mock("@/components/notes/link-suggestions", () => ({ LinkSuggestions: () => null })); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const m = (f: unknown) => f as jest.Mock; +const show = (ui: React.ReactNode) => + render( + + {ui} + , + ); + +const PAGE = { + id: 2, + title: "Pi 5 shopping list", + namespace: "home", + author: "person", + body: "Parts for @[Home lab](page:1).\n\n1. Pi 5 8GB\n2. NVMe hat", + parent: 1, + pinned: false, + can_edit: true, + links: [], + backlinks: [], + path: [{ id: 1, title: "Home lab" }], + children: [{ id: 3, title: "NVMe hat options", updated_at: "2026-10-15T11:50:00Z" }], +}; + +/** Open the note's ⋯ menu and choose an item (items run once the menu has closed). */ +async function choose(item: string) { + fireEvent.keyDown(await screen.findByRole("button", { name: "More on this note" }), { key: "Enter" }); + fireEvent.click(await screen.findByRole("menuitem", { name: item })); +} + +beforeEach(() => { + jest.clearAllMocks(); + m(Notes.getPage).mockImplementation(() => ok(PAGE)); +}); + +describe("word count", () => { + it("counts the words a reader sees", () => { + expect(wordCount(PAGE.body)).toBe(9); + expect(wordCount("- [ ] Flash the SD card\n- [x] Pick a cloud model")).toBe(8); + expect(wordCount("## Plan\n\n**well-known** words — here")).toBe(4); + expect(wordCount("")).toBe(0); + }); +}); + +describe("a note's page", () => { + it("says where it is, with each note it's inside a link", async () => { + show(); + const crumbs = await screen.findByRole("navigation", { name: "Where this note is" }); + expect(within(crumbs).getByRole("link", { name: "Notes" })).toHaveAttribute("href", "/notes"); + expect(within(crumbs).getByText("home")).toBeInTheDocument(); + expect(within(crumbs).getByRole("link", { name: "Home lab" })).toHaveAttribute("href", "/notes/1"); + expect(screen.getByText("9 words")).toBeInTheDocument(); + }); + + it("lists the notes inside it, and Add makes one there with its title ready", async () => { + m(Notes.createPage).mockImplementation(() => ok({ ...PAGE, id: 9, title: "Untitled", parent: 2 })); + show(); + expect(await screen.findByRole("link", { name: "NVMe hat options" })).toHaveAttribute("href", "/notes/3"); + fireEvent.click(screen.getByRole("button", { name: "Add a note inside" })); + await waitFor(() => expect(push).toHaveBeenCalledWith("/notes/9?new=1")); + expect(m(Notes.createPage).mock.calls[0][0].body).toEqual({ ns: "home", title: "Untitled", parent: 2 }); + }); + + it("keeps costs and activity folded until opened", async () => { + show(); + const fold = await screen.findByText("Costs and activity"); + expect(screen.queryByTestId("activity")).not.toBeInTheDocument(); + const details = fold.closest("details")!; + details.open = true; + fireEvent(details, new Event("toggle")); + expect(await screen.findByTestId("activity")).toBeInTheDocument(); + }); + + it("has History, Pin, Download and Delete in its menu", async () => { + m(Notes.pinPage).mockImplementation(() => ok({ ok: true })); + m(Notes.deletePage).mockImplementation(() => ok({ ok: true })); + show(); + await choose("Pin"); + await waitFor(() => expect(m(Notes.pinPage).mock.calls[0][0].path).toEqual({ pid: 2 })); + await choose("Download as Markdown"); + await waitFor(() => + expect(download).toHaveBeenCalledWith("/api/v1/notes/2/markdown", "t", "Pi 5 shopping list.md"), + ); + await choose("Delete"); + await waitFor(() => expect(push).toHaveBeenCalledWith("/notes")); + expect(toast.mock.calls[0][0].action.label).toBe("Undo"); + }); + + it("shows a viewer the notes inside but not Add, Delete or Add inside", async () => { + m(Notes.getPage).mockImplementation(() => ok({ ...PAGE, can_edit: false })); + show(); + expect(await screen.findByRole("link", { name: "NVMe hat options" })).toBeInTheDocument(); + expect(screen.queryByRole("button", { name: "Add a note inside" })).not.toBeInTheDocument(); + fireEvent.keyDown(screen.getByRole("button", { name: "More on this note" }), { key: "Enter" }); + const items = (await screen.findAllByRole("menuitem")).map((i) => i.textContent); + expect(items).toEqual(["Pin", "History", "Download as Markdown"]); + }); +}); diff --git a/nextjs-frontend/app/openapi-client/index.ts b/nextjs-frontend/app/openapi-client/index.ts index d4701a488..12051afa0 100644 --- a/nextjs-frontend/app/openapi-client/index.ts +++ b/nextjs-frontend/app/openapi-client/index.ts @@ -2260,6 +2260,7 @@ export type { NoteBacklink, NoteCopy, NoteCreate, + NoteCrumb, NoteDeleted, NoteFile, NoteHistory, diff --git a/nextjs-frontend/app/openapi-client/types.gen.ts b/nextjs-frontend/app/openapi-client/types.gen.ts index 48100156e..32807aa98 100644 --- a/nextjs-frontend/app/openapi-client/types.gen.ts +++ b/nextjs-frontend/app/openapi-client/types.gen.ts @@ -12715,6 +12715,25 @@ export type NoteCreate = { shared?: boolean; }; +/** + * NoteCrumb + */ +export type NoteCrumb = { + /** + * Id + */ + id: number; + /** + * Title + */ + title: string; + /** + * Updated At + */ + updated_at?: string | null; + [key: string]: unknown; +}; + /** * NoteDeleted */ @@ -13079,6 +13098,18 @@ export type NotePage = { * Backlinks */ backlinks?: Array; + /** + * Path + * + * the notes it's inside, from the top of the tree down to its parent + */ + path?: Array; + /** + * Children + * + * the notes inside it, in the tree's order + */ + children?: Array; /** * Can Edit */ diff --git a/nextjs-frontend/components/notes/block-editor.tsx b/nextjs-frontend/components/notes/block-editor.tsx index 7feee3303..0308bd73d 100644 --- a/nextjs-frontend/components/notes/block-editor.tsx +++ b/nextjs-frontend/components/notes/block-editor.tsx @@ -277,12 +277,13 @@ export default function BlockEditor({ }; }, []); - // The page's title is Lens's own field above the editor, so the editor's title line is hidden. + // The page's title is Lens's own field above the editor, so the editor's title line is hidden, and the text starts + // where the title does (the editor's own side margin, 96px or 24px on a phone, is for a page of its own). return (
("page"); + const [words, setWords] = useState(0); const loadedFor = useRef(null); // Just made with New note (?new=1): the title takes the focus with "Untitled" selected, so typing names it const fresh = useRef(useSearchParams().get("new") === "1"); @@ -81,6 +99,7 @@ export function NotePage({ id, about, embedded = false }: { id?: number; about?: setDate(saved?.date ?? ""); setPlace(saved?.place ?? ""); setView((saved?.view as View | null) ?? "page"); + setWords(wordCount(page.body ?? "")); }, [page, saved]); useEffect(() => { const el = titleRef.current; @@ -219,6 +238,30 @@ export function NotePage({ id, about, embedded = false }: { id?: number; about?: [router, page?.namespace], ); + // A new note inside this one, opened with its title ready to type + const [adding, setAdding] = useState(false); + const addInside = async () => { + if (savedId.current == null || !page) return; + setAdding(true); + try { + const p = await data( + Notes.createPage({ client, body: { ns: page.namespace, title: "Untitled", parent: savedId.current } }), + ); + qc.invalidateQueries({ queryKey: ["notes-tree"] }); + router.push(`/notes/${p.id}?new=1`); + } catch (err) { + toast({ + title: "Couldn’t add a note", + body: err instanceof ApiError ? err.message : "Please try again.", + tone: "red", + }); + } finally { + setAdding(false); + } + }; + const pin = usePin(); + const downloads = useNoteDownloads(); + // No question first: the notice has Undo, and the note waits in recently deleted const deleteNote = useDeleteNote(); const remove = async () => { @@ -280,7 +323,7 @@ export function NotePage({ id, about, embedded = false }: { id?: number; about?: return (
- {!embedded && {page.namespace}} + {!embedded && } {aboutHref && ( Open {page.about?.split(":")[0]} @@ -293,20 +336,47 @@ export function NotePage({ id, about, embedded = false }: { id?: number; about?: )} {embedded && views} + {view === "page" && words > 0 && {words === 1 ? "1 word" : `${words.toLocaleString()} words`}} {canEdit && ( {state === "saving" ? "Saving…" : state === "error" ? "Not saved" : saved ? "Saved" : "Draft"} )} {saved && ( - - )} - {canEdit && saved && ( - + + + + + + + + {canEdit && !saved.about && ( + } disabled={adding} onSelect={() => void addInside()}> + Add a note inside + + )} + : } + onSelect={() => void pin(page.namespace, saved.id, !saved.pinned)} + > + {saved.pinned ? "Unpin" : "Pin"} + + } onSelect={() => setShowHistory(true)}> + History + + } onSelect={() => void downloads.note(saved.id, title.trim() || saved.title)}> + Download as Markdown + + {canEdit && ( + <> + + } danger onSelect={() => void remove()}> + Delete + + + )} + + )}
{!embedded && ( @@ -438,7 +508,10 @@ export function NotePage({ id, about, embedded = false }: { id?: number; about?: readOnly={!canEdit} view={view} stale={Boolean(saved?.doc_stale)} - onChange={(c: EditorChange) => change({ body: c.markdown, doc: c.doc })} + onChange={(c: EditorChange) => { + setWords(wordCount(c.markdown)); + change({ body: c.markdown, doc: c.doc }); + }} search={search} onOpenLink={openLink} blobs={blobs} @@ -470,6 +543,14 @@ export function NotePage({ id, about, embedded = false }: { id?: number; about?: }} /> )} + {saved && !saved.about && !embedded && ( + void addInside()} + /> + )} {showHistory && saved && ( )} - {saved && !embedded && } + {saved && !embedded && }
); } +/** Words as a reader counts them: the text of links, not their addresses, list numbers or Markdown's marks. */ +export function wordCount(markdown: string): number { + const text = markdown + .replace(/^\s*(?:[-*+]|\d+[.)])\s+(?:\[[ xX]\]\s+)?/gm, "") + .replace(/[@#]?\[([^\]\n]*)\]\([^)\n]*\)/g, "$1") + .replace(/<[^>\n]+>/g, " "); + return text.split(/\s+/).filter((w) => /[\p{L}\p{N}]/u.test(w)).length; +} + +/** Notes › namespace › the notes this one is inside, each a link. */ +function Breadcrumbs({ namespace, path }: { namespace: string; path: NoteCrumb[] }) { + const sep = ; + return ( + + ); +} + +/** The notes inside this one, in the tree's order, with Add. */ +function Inside({ + items, + canEdit, + adding, + onAdd, +}: { + items: NoteCrumb[]; + canEdit: boolean; + adding: boolean; + onAdd: () => void; +}) { + if (!items.length && !canEdit) return null; + return ( +
+

+ Inside this note + {items.length > 0 && {items.length}} +

+ {items.length > 0 && ( +
    + {items.map((c) => ( +
  • + + {c.title} + + {c.updated_at && {shortDate(c.updated_at)}} +
  • + ))} +
+ )} + {canEdit && ( + + )} +
+ ); +} + +/** Costs and activity, folded: most people never open it, so it loads only when they do. */ +function CostsFold({ resource }: { resource: string }) { + const [open, setOpen] = useState(false); + return ( +
setOpen(e.currentTarget.open)}> + Costs and activity + {open && ( +
+ +
+ )} +
+ ); +} + function Backlinks({ items }: { items: { page: number; title: string; about?: string | null }[] }) { if (!items.length) return null; return ( diff --git a/nextjs-frontend/openapi.json b/nextjs-frontend/openapi.json index e85a1052f..85dbabba5 100644 --- a/nextjs-frontend/openapi.json +++ b/nextjs-frontend/openapi.json @@ -52751,6 +52751,36 @@ ], "title": "NoteCreate" }, + "NoteCrumb": { + "properties": { + "id": { + "type": "integer", + "title": "Id" + }, + "title": { + "type": "string", + "title": "Title" + }, + "updated_at": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ], + "title": "Updated At" + } + }, + "additionalProperties": true, + "type": "object", + "required": [ + "id", + "title" + ], + "title": "NoteCrumb" + }, "NoteDeleted": { "properties": { "id": { @@ -53401,6 +53431,24 @@ "title": "Backlinks", "default": [] }, + "path": { + "items": { + "$ref": "#/components/schemas/NoteCrumb" + }, + "type": "array", + "title": "Path", + "description": "the notes it's inside, from the top of the tree down to its parent", + "default": [] + }, + "children": { + "items": { + "$ref": "#/components/schemas/NoteCrumb" + }, + "type": "array", + "title": "Children", + "description": "the notes inside it, in the tree's order", + "default": [] + }, "can_edit": { "type": "boolean", "title": "Can Edit",