A library's size counts what is under its top-level nodes, the same in /api/libraries and the root's byline (a root alone is 0 nodes)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
1 parent
a6dbfbc19f
commit
f888525808
5 files changed
+13
-8
No files matched your search
+3
-2
@@ -24,7 +24,7 @@ import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync
|
||||
import { join } from "node:path";
|
||||
import { LIBRARY_ID } from "../src/core/address";
|
||||
import { FormatError } from "../src/core/format";
|
||||
import { Doc, retitleOps } from "../src/core/model";
|
||||
import { Doc, libraryNodeCount, retitleOps } from "../src/core/model";
|
||||
import { writeText } from "../src/core/seed";
|
||||
import { LibraryStore } from "./library";
|
||||
import { HttpError, writeAtomic } from "./util";
|
||||
@@ -48,6 +48,7 @@ export interface LibraryInfo {
|
||||
id: string;
|
||||
title: string;
|
||||
created: string;
|
||||
/** Nodes under its top-level nodes (the root and the Wikipedia container not counted). */
|
||||
nodes: number;
|
||||
branches: string[];
|
||||
}
|
||||
@@ -189,7 +190,7 @@ export class Libraries {
|
||||
e.title = title;
|
||||
renamed = true;
|
||||
}
|
||||
out.push({ id: e.id, title, created: e.created, nodes: Object.keys(s.main.snap().nodes).length, branches: [...s.branches.keys()] });
|
||||
out.push({ id: e.id, title, created: e.created, nodes: libraryNodeCount(s.main.snap()), branches: [...s.branches.keys()] });
|
||||
}
|
||||
if (renamed) this.saveIndex();
|
||||
return out;
|
||||
|
||||
@@ -897,6 +897,9 @@ export function tops(snap: Snap): NodeId[] {
|
||||
|
||||
export const isLibraryRoot = (snap: Snap, id: NodeId) => snap.library && id === snap.root;
|
||||
|
||||
/** How many nodes a library holds: everything under its top-level nodes, the root and the Wikipedia container left out (0 for a root alone). */
|
||||
export const libraryNodeCount = (snap: Snap) => tops(snap).reduce((n, d) => n + 1 + descendants(snap, d).length, 0);
|
||||
|
||||
/**
|
||||
* The top of a node's branch: its ancestor (or itself) among the root's children. A relation, not
|
||||
* a kind of node. None for the library root itself; in a single tree, the root.
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
/** Rendering shared by both page layouts: a node's body with its notes, the outline rows, and the top of the page. */
|
||||
import { Component, type ComponentChildren } from "preact";
|
||||
import { useLayoutEffect, useRef, useState } from "preact/hooks";
|
||||
import { anchorLost, ancestors, descendants, isLibraryRoot, label, notesOf, sections, staleness, type NodeId, type Snap } from "../core/model";
|
||||
import { anchorLost, ancestors, descendants, isLibraryRoot, label, libraryNodeCount, notesOf, sections, staleness, type NodeId, type Snap } from "../core/model";
|
||||
import type { Ref } from "../core/ref";
|
||||
import { clip, overlaps, paragraphs, plain, type Form, type Range } from "../core/text";
|
||||
import { streamView } from "../core/marks";
|
||||
@@ -462,7 +462,7 @@ export function TopMatter({ id }: { id: NodeId }) {
|
||||
const editing = store.edit?.node === id && store.edit.field === "title";
|
||||
const library = isLibraryRoot(snap, id);
|
||||
// The library root's count leaves out the Wikipedia container, which is nobody's section.
|
||||
const count = library ? kids.reduce((n, d) => n + 1 + descendants(snap, d).length, 0) : descendants(snap, id).length;
|
||||
const count = library ? libraryNodeCount(snap) : descendants(snap, id).length;
|
||||
return (
|
||||
<>
|
||||
{crumbs.length > 0 && (
|
||||
|
||||
@@ -28,6 +28,7 @@ export interface LibraryInfo {
|
||||
id: string;
|
||||
title: string;
|
||||
created: string;
|
||||
/** Nodes under its top-level nodes (the root and the Wikipedia container not counted). */
|
||||
nodes: number;
|
||||
branches: string[];
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ describe("the libraries on disk", () => {
|
||||
expect(work.titles()).toEqual([]);
|
||||
expect(work.id).not.toBe(first.id);
|
||||
expect(libs.list().map((l) => [l.title, l.nodes])).toEqual([
|
||||
["Library", 2],
|
||||
["Work notes", 1],
|
||||
["Library", 1],
|
||||
["Work notes", 0],
|
||||
]);
|
||||
// An op on one never touches the other: separate databases, separate files.
|
||||
const hash = first.main.stateHash();
|
||||
@@ -150,7 +150,7 @@ describe("the server with several libraries", () => {
|
||||
const made = await send("POST", "/api/libraries", { title: "Second" });
|
||||
expect(made.status).toBe(201);
|
||||
const b = (await made.json()).library;
|
||||
expect(b).toMatchObject({ title: "Second", nodes: 1 });
|
||||
expect(b).toMatchObject({ title: "Second", nodes: 0 });
|
||||
const state = await get(`/api/l/${b.id}/state`);
|
||||
expect(state).toMatchObject({ library: b.id, title: "Second" });
|
||||
expect(state.libraries.map((l: { id: string }) => l.id)).toEqual([a.id, b.id]);
|
||||
@@ -228,7 +228,7 @@ describe("the server with several libraries", () => {
|
||||
const again = serve(data, starters);
|
||||
await up();
|
||||
const listed = (await get("/api/libraries")).libraries;
|
||||
expect(listed.map((l: { id: string; nodes: number }) => [l.id, l.nodes])).toEqual([[a.id, Object.keys(treeA.nodes).length]]);
|
||||
expect(listed.map((l: { id: string; nodes: number }) => [l.id, l.nodes])).toEqual([[a.id, Object.keys(treeA.nodes).length - 1]]);
|
||||
again.child.kill("SIGTERM");
|
||||
await again.exited;
|
||||
}, 60_000);
|
||||
|
||||
Reference in new issue
Block a user