Several libraries on the server: one directory, store, notice stream, link index, pending links, reviewer and jobs each; /api/libraries and /api/l/<id>/…; the page holds one library, named by its address, and switches by a page load
The old single-library layout is not opened (the server says to run the migration, which comes next); the per-document migration, /recover and the old replicas check are gone. A server lock keeps two servers off one data directory. 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
045a8459da
commit
79aa85e883
22 files changed
+1468
-759
No files matched your search
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Where the page's requests about its library go. The page holds one library, named by its
|
||||
* address (`/l/<id>`); everything that reads or writes a library's content asks under
|
||||
* `/api/l/<id>/`, and what holds no library's content (the model, Wikipedia, usage) under `/api/`.
|
||||
* Switching library is a page load (`/l/<other id>`), so the prefix is set once, at boot.
|
||||
*/
|
||||
let library: string | null = null;
|
||||
|
||||
/** Set the library this page holds. */
|
||||
export function setLibrary(id: string) {
|
||||
library = id;
|
||||
}
|
||||
|
||||
/** The library this page holds, once boot has chosen it. */
|
||||
export const currentLibrary = () => library;
|
||||
|
||||
/**
|
||||
* A path under this library's API: libApi("/links/jobs") → /api/l/<id>/links/jobs. Before a library
|
||||
* is chosen (only in the tests: the page chooses one before anything asks) the unprefixed route,
|
||||
* which the server answers for its first library.
|
||||
*/
|
||||
export function libApi(path: string): string {
|
||||
return library ? `/api/l/${encodeURIComponent(library)}${path}` : `/api${path}`;
|
||||
}
|
||||
|
||||
/** A library as /api/libraries lists it. */
|
||||
export interface LibraryInfo {
|
||||
id: string;
|
||||
title: string;
|
||||
created: string;
|
||||
nodes: number;
|
||||
branches: string[];
|
||||
}
|
||||
|
||||
async function call<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const res = await fetch(path, init);
|
||||
let body: Record<string, unknown> = {};
|
||||
try {
|
||||
body = await res.json();
|
||||
} catch {
|
||||
/* not JSON */
|
||||
}
|
||||
if (!res.ok) throw new Error(String(body.error ?? `${res.status} ${res.statusText}`));
|
||||
return body as T;
|
||||
}
|
||||
const send = (method: string, body?: unknown): RequestInit => ({ method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
|
||||
/** The libraries, in order. */
|
||||
export const listLibraries = () => call<{ libraries: LibraryInfo[] }>("/api/libraries").then((r) => r.libraries);
|
||||
/** A new library (its root alone), at the end of the list. */
|
||||
export const createLibrary = (title: string) => call<{ library: LibraryInfo }>("/api/libraries", send("POST", { title })).then((r) => r.library);
|
||||
/** Rename a library (a commit on its root's title). */
|
||||
export const renameLibrary = (id: string, title: string) => call<{ library: LibraryInfo }>(`/api/libraries/${encodeURIComponent(id)}`, send("PATCH", { title })).then((r) => r.library);
|
||||
/** Delete a library: the server moves it aside whole. Refused for the last one. */
|
||||
export const deleteLibrary = (id: string) => call<{ deleted: string; movedTo: string }>(`/api/libraries/${encodeURIComponent(id)}`, send("DELETE"));
|
||||
+2
-1
@@ -88,6 +88,7 @@ export const HELP: [string, [string, string][]][] = [
|
||||
"The library",
|
||||
[
|
||||
["", "everything is a node of one tree, whose root is the library: the first page. Any node can be moved under any other (M, > <), copied (/duplicate), saved to a file (/export) or given a file's tree under it (/import); each acts on the node you are on, with everything under it. The ⋯ menu of any node offers the same"],
|
||||
["", "there can be several libraries, each a tree of its own with its own address (/l/…): nothing links, moves or is found from one to another (export, then import, carries a tree across). :/library lists them, :/library name opens one, :/library new name makes one"],
|
||||
],
|
||||
],
|
||||
[
|
||||
@@ -121,7 +122,7 @@ export const HELP: [string, [string, string][]][] = [
|
||||
["/duplicate title", "save the node you are on, with everything under it, as a copy just after it (titled \"… (copy)\" unless given one); the copy has the focus"],
|
||||
["/export", "save the node you are on, with everything under it, to a .md file (on the library root, the whole library)"],
|
||||
["/import", "read a .md file into the node you are on, as its last child (on the library root, a new top-level node)"],
|
||||
["/recover", "save edits this browser made before the library existed, and never sent, as .md files"],
|
||||
["/library [name]", "list the libraries; with a name, open that library (a page load); /library new name makes one and opens it. Libraries are separate trees: nothing links, moves or is found across them"],
|
||||
["/link target", "link the selection (or the node): w:Title, /Top title/Section (titles from the library root), an address, or a Wikipedia title"],
|
||||
["/unlink", "remove the link on the selection (for good: its words are declined; a note that also links keeps its text), or the node's own target"],
|
||||
["/autolink review|suggest|on|off", "after an edit: the model reviews what was found and links what it approves (review, the default with a model; u undoes, S lists every decision), suggest everything for you to decide (suggest; what review does without a model, or when the model fails or its limit is reached), link the scanner's sure ones unreviewed (on), or look for nothing (off)"],
|
||||
|
||||
+4
-3
@@ -17,6 +17,7 @@ import { applyLinks, nodeRef, phraseKey } from "../core/links";
|
||||
import type { Mark } from "../core/marks";
|
||||
import { ancestors, isLibraryRoot, isLink, label, notesOf, topOf, tops, type Doc, type NodeId, type Op, type Snap } from "../core/model";
|
||||
import { describeTarget, sameTarget, type Ref } from "../core/ref";
|
||||
import { libApi } from "./api";
|
||||
import { clip, overlaps, type Range } from "../core/text";
|
||||
import type { Nearby } from "../llm/prompts";
|
||||
|
||||
@@ -76,7 +77,7 @@ interface Task {
|
||||
const idle = (fn: () => void) => (typeof requestIdleCallback === "function" ? requestIdleCallback(fn, { timeout: 1000 }) : setTimeout(fn, 50));
|
||||
|
||||
const post = (path: string, body: unknown, signal?: AbortSignal) =>
|
||||
fetch(`/api/links/${path}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body), signal });
|
||||
fetch(libApi(`/links/${path}`), { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body), signal });
|
||||
|
||||
/** Turned down: declined by a person, or rejected by the reviewer and not linked by a person since. */
|
||||
const turnedDown = (snap: Snap, s: Sugg) => !!snap.nodes[s.node]?.nolink?.includes(s.key) || s.decided?.link === false || (s.review?.link === false && !s.decided?.link);
|
||||
@@ -356,7 +357,7 @@ export class Linker {
|
||||
if (!force && this.fetched.has(id)) return { found: [], apply: [] };
|
||||
this.fetched.add(id);
|
||||
try {
|
||||
const res = await fetch(`/api/links/pending?node=${encodeURIComponent(id)}&subtree=1&all=1${claim ? "&claim=1" : ""}`);
|
||||
const res = await fetch(libApi(`/links/pending?node=${encodeURIComponent(id)}&subtree=1&all=1${claim ? "&claim=1" : ""}`));
|
||||
if (!res.ok) return { found: [], apply: [] };
|
||||
const r = (await res.json()) as { suggestions: Sugg[]; apply?: Sugg[] };
|
||||
const by = new Map<NodeId, Sugg[]>();
|
||||
@@ -502,7 +503,7 @@ export class Linker {
|
||||
}
|
||||
|
||||
async cancelJob(): Promise<boolean> {
|
||||
const res = await fetch("/api/links/jobs", { method: "DELETE" });
|
||||
const res = await fetch(libApi("/links/jobs"), { method: "DELETE" });
|
||||
const r = await res.json().catch(() => ({}));
|
||||
if (r.job) this.job = r.job;
|
||||
return !!r.cancelled;
|
||||
|
||||
+5
-4
@@ -5,6 +5,7 @@
|
||||
import { VersionVector } from "loro-crdt";
|
||||
import { Doc } from "../core/model";
|
||||
import { Repo, decodeVV, encodeVV, mergeUpdate, vvIncludes, type MergeReport } from "../core/repo";
|
||||
import { libApi } from "./api";
|
||||
import { idbGet, idbSet } from "./idb";
|
||||
|
||||
type VV = Map<`${number}`, number>;
|
||||
@@ -85,7 +86,7 @@ export class Session {
|
||||
}
|
||||
|
||||
private async fetchBranch(name: string): Promise<Doc | null> {
|
||||
const res = await fetch(`/api/branch/${encodeURIComponent(name)}`);
|
||||
const res = await fetch(libApi(`/branch/${encodeURIComponent(name)}`));
|
||||
if (!res.ok) return null;
|
||||
if (res.headers.get("x-library") !== this.library) {
|
||||
this.lost();
|
||||
@@ -131,7 +132,7 @@ export class Session {
|
||||
const known = this.serverVV.get(name);
|
||||
if (known && vvIncludes(known, doc.loro.version().toJSON())) continue;
|
||||
const bytes = known ? doc.loro.export({ mode: "update", from: new VersionVector(known) }) : doc.snapshotBytes();
|
||||
navigator.sendBeacon(`/api/sync/${encodeURIComponent(name)}?library=${encodeURIComponent(this.library)}`, new Blob([bytes as BlobPart], { type: "application/octet-stream" }));
|
||||
navigator.sendBeacon(libApi(`/sync/${encodeURIComponent(name)}?library=${encodeURIComponent(this.library)}`), new Blob([bytes as BlobPart], { type: "application/octet-stream" }));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,7 +170,7 @@ export class Session {
|
||||
let ok = false;
|
||||
try {
|
||||
const bytes = !known ? doc.snapshotBytes() : vvIncludes(known, mine) ? new Uint8Array() : doc.loro.export({ mode: "update", from: new VersionVector(known) });
|
||||
const res = await fetch(`/api/sync/${encodeURIComponent(name)}`, {
|
||||
const res = await fetch(libApi(`/sync/${encodeURIComponent(name)}`), {
|
||||
method: "POST",
|
||||
headers: { "x-vv": encodeVV(mine), "x-client": this.clientId, "x-library": this.library, "content-type": "application/octet-stream" },
|
||||
body: bytes as BodyInit,
|
||||
@@ -203,7 +204,7 @@ export class Session {
|
||||
/** Listen for other writers. */
|
||||
connect() {
|
||||
if (this.events || this.foreign) return;
|
||||
const events = new EventSource("/api/events");
|
||||
const events = new EventSource(libApi("/events"));
|
||||
events.onmessage = (e) => {
|
||||
const msg = JSON.parse(e.data) as { branch?: string; from?: string };
|
||||
if (msg.branch && msg.from !== this.clientId) void this.sync(msg.branch);
|
||||
|
||||
+132
-66
@@ -13,10 +13,10 @@ import {
|
||||
staleness, topOf, tops, type NodeId, type Op, type Snap,
|
||||
} from "../core/model";
|
||||
import { FormatError, serializeDoc } from "../core/format";
|
||||
import { address, resolve } from "../core/address";
|
||||
import { address, inLibrary, resolve, splitLibrary } from "../core/address";
|
||||
import { IMPORT_MAX, importTree } from "../core/seed";
|
||||
import {
|
||||
Repo, at, decodeVV, healAnchors, history, reopenConflict, resolveConflict, revertSubtreeTo, revertTo, touched, vvIncludes, type Commit,
|
||||
Repo, at, healAnchors, history, reopenConflict, resolveConflict, revertSubtreeTo, revertTo, touched, type Commit,
|
||||
type MergeReport,
|
||||
} from "../core/repo";
|
||||
import { clip, formFor, plain, rangeAt, sentences, slugify, words, type Form, type Range } from "../core/text";
|
||||
@@ -32,7 +32,7 @@ import {
|
||||
} from "../llm/tasks";
|
||||
import type { LLM } from "../llm/types";
|
||||
import { DEFAULT_LIMITS, wantsResearch } from "../llm/web";
|
||||
import { idbGet, idbKeys, idbSet } from "./idb";
|
||||
import { createLibrary, deleteLibrary, libApi, listLibraries, renameLibrary, setLibrary, type LibraryInfo } from "./api";
|
||||
import { remoteLLM } from "./remote";
|
||||
import { Session } from "./session";
|
||||
import { refreshAfterCalls } from "./usage";
|
||||
@@ -140,19 +140,6 @@ export interface Sample {
|
||||
at?: number;
|
||||
}
|
||||
|
||||
/** A replica of a document from before the library, holding edits the server never received. */
|
||||
interface OldReplica {
|
||||
slug: string;
|
||||
title: string;
|
||||
branch: string;
|
||||
bytes: Uint8Array;
|
||||
}
|
||||
|
||||
/** What the server recorded when it built the library from separate documents. */
|
||||
interface Migrated {
|
||||
docs: { slug: string; uid: string | null; title: string; versions: Record<string, string> }[];
|
||||
}
|
||||
|
||||
// ---- store ----
|
||||
|
||||
const clientId = Math.random().toString(36).slice(2);
|
||||
@@ -289,7 +276,9 @@ class Store {
|
||||
/** Set at a keypress; cleared when the resulting frame has been painted. */
|
||||
t0: { what: string; at: number } | null = null;
|
||||
/** Replicas of pre-library documents whose edits never reached the server. */
|
||||
oldReplicas: OldReplica[] = [];
|
||||
/** The library this page holds (its id, as in its address), and every library the server has, as it last said. */
|
||||
library = "";
|
||||
libraries: LibraryInfo[] = [];
|
||||
|
||||
/** The panes as a forest of forks (forks.ts); its focused branch is `trail`, and `paneAt` is the pane on it that holds the focus: the active pane. */
|
||||
forest: Forest<PaneRef, Back> = { trees: [], at: 0 };
|
||||
@@ -1112,15 +1101,41 @@ class Store {
|
||||
|
||||
async boot() {
|
||||
try {
|
||||
let state: { user: string; llm: LLM["kind"]; model: string | null; library?: string; branches?: { name: string }[]; migrated?: Migrated | null; wiki?: WikiConfig; linkJob?: JobState | null; web?: { on: boolean; searches: number; fetches: number } } | null = null;
|
||||
// The address names the library; an address without one (`/`, or one from before libraries)
|
||||
// is read in the library last used here, if the server still has it, else in the first.
|
||||
const { library: named, part } = splitLibrary(location.pathname);
|
||||
let library = named;
|
||||
if (!library) {
|
||||
let list: LibraryInfo[] | null = null;
|
||||
try {
|
||||
list = await listLibraries();
|
||||
} catch {
|
||||
/* offline: fall back to what is stored locally */
|
||||
}
|
||||
const last = lsGet<string | null>("ie:library", null);
|
||||
library = list ? ((list.find((l) => l.id === last) ?? list[0])?.id ?? null) : last;
|
||||
if (!library) throw new Error("Could not load a library: the server is not reachable and nothing is stored in this browser.");
|
||||
window.history.replaceState(null, "", inLibrary(library, part));
|
||||
}
|
||||
setLibrary(library);
|
||||
this.library = library;
|
||||
let state: { user: string; llm: LLM["kind"]; model: string | null; library?: string; title?: string; libraries?: LibraryInfo[]; branches?: { name: string }[]; wiki?: WikiConfig; linkJob?: JobState | null; web?: { on: boolean; searches: number; fetches: number } } | null = null;
|
||||
try {
|
||||
const res = await fetch("/api/state");
|
||||
const res = await fetch(libApi("/state"));
|
||||
if (res.status === 404) {
|
||||
// Not a library of this server (deleted, or from another data directory): to the first one.
|
||||
if (lsGet("ie:library", null) === library) lsSet("ie:library", null);
|
||||
sessionStorage.setItem("ie:flash", `There is no library ${library} here; this is the first one.`);
|
||||
location.replace("/");
|
||||
return;
|
||||
}
|
||||
if (res.ok) state = await res.json();
|
||||
} catch {
|
||||
/* offline: fall back to what is stored locally */
|
||||
}
|
||||
if (state && !state.library) throw new Error("This page is newer than the server it came from. Restart the server (npm run dev) and reload.");
|
||||
this.online = !!state;
|
||||
this.libraries = state?.libraries ?? [];
|
||||
if (state?.wiki) this.wiki = state.wiki;
|
||||
this.web = state?.web ?? { on: false, searches: 0, fetches: 0 };
|
||||
if (state?.linkJob?.state === "running") this.links.job = state.linkJob;
|
||||
@@ -1132,8 +1147,6 @@ class Store {
|
||||
});
|
||||
this.user = lsGet("ie:user", state?.user ?? "you");
|
||||
if (state) this.llm = refreshAfterCalls(remoteLLM(state.llm, state.model ?? undefined));
|
||||
const library = state?.library ?? lsGet<string | null>("ie:library", null);
|
||||
if (!library) throw new Error("Could not load the library: the server is not reachable and nothing is stored in this browser.");
|
||||
lsSet("ie:library", library);
|
||||
|
||||
this.session = new Session(library, this.user, clientId, {
|
||||
@@ -1153,16 +1166,26 @@ class Store {
|
||||
foreign: () => this.say("The server now holds a different library than this page. Edits here are no longer sent; reload the page."),
|
||||
message: (msg) => {
|
||||
if (msg.linkJob) this.jobUpdate(msg.linkJob as JobState);
|
||||
if (msg.library === "deleted") {
|
||||
this.session.foreign = true;
|
||||
this.session.disconnect();
|
||||
this.say("This library has been deleted (the server moved its files aside). Edits here are no longer sent; go to / for another library.");
|
||||
}
|
||||
},
|
||||
});
|
||||
await this.session.load((state?.branches ?? []).map((b) => b.name), this.online);
|
||||
const branch = lsGet("ie:branch", "main");
|
||||
const branch = lsGet(this.branchKey(), "main");
|
||||
this.repo.current = this.repo.branches.has(branch) ? branch : "main";
|
||||
this.restoreView();
|
||||
this.undoManager();
|
||||
this.navigate(location.pathname, "replace");
|
||||
this.navigate(splitLibrary(location.pathname).part, "replace");
|
||||
this.ready = true;
|
||||
this.prefetchLinks(this.focus.node);
|
||||
const flash = sessionStorage.getItem("ie:flash");
|
||||
if (flash) {
|
||||
sessionStorage.removeItem("ie:flash");
|
||||
this.say(flash);
|
||||
}
|
||||
this.emit();
|
||||
// The first commit hashes the whole library once (later ones only the path to what they
|
||||
// changed); do that while the page is idle rather than on the reader's first edit.
|
||||
@@ -1177,52 +1200,91 @@ class Store {
|
||||
this.session.connect();
|
||||
}
|
||||
// Back and forward move between places in the library.
|
||||
addEventListener("popstate", () => this.navigate(location.pathname, "none"));
|
||||
if (state?.migrated) void this.checkOldReplicas(state.migrated);
|
||||
addEventListener("popstate", () => {
|
||||
const at = splitLibrary(location.pathname);
|
||||
// Another library's address (one this page did not push): that library, by a page load.
|
||||
if (at.library && at.library !== this.library) location.reload();
|
||||
else this.navigate(at.part, "none");
|
||||
});
|
||||
} catch (err) {
|
||||
this.fatal = err instanceof Error ? err.message : String(err);
|
||||
this.emit();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The version that kept each document separately stored its replicas under `doc:<uid>:…`. They
|
||||
* are never loaded into the library, but one may hold edits made offline that never reached the
|
||||
* server: compare each with what the server had when it built the library, and say so if it is
|
||||
* ahead.
|
||||
*/
|
||||
private async checkOldReplicas(migrated: Migrated) {
|
||||
const flag = `lib:${this.session.library}:old-checked`;
|
||||
if (await idbGet(flag)) return;
|
||||
const keys = await idbKeys();
|
||||
const found: OldReplica[] = [];
|
||||
const check = async (key: string, entry: Migrated["docs"][number] | undefined, branch: string) => {
|
||||
const bytes = await idbGet<Uint8Array>(key);
|
||||
if (!bytes || !entry) return;
|
||||
const had = entry.versions[branch];
|
||||
const mine = Doc.load(bytes, this.user).loro.version().toJSON();
|
||||
if (!had || !vvIncludes(decodeVV(had) as never, mine)) found.push({ slug: entry.slug, title: entry.title, branch, bytes });
|
||||
};
|
||||
for (const key of keys) {
|
||||
const m = /^doc:([0-9a-f]+):branch:(.+)$/.exec(key);
|
||||
if (m) await check(key, migrated.docs.find((d) => d.uid === m[1]), m[2]);
|
||||
}
|
||||
this.oldReplicas = found;
|
||||
if (!found.length) return void idbSet(flag, true);
|
||||
const names = found.map((r) => `“${clip(r.title, 30)}”${r.branch === "main" ? "" : ` (${r.branch})`}`).join(", ");
|
||||
this.say(`This browser holds edits made before the library existed that never reached the server: ${names}. They were not merged. :/recover saves each as a .md file.`);
|
||||
// ---- libraries ----
|
||||
|
||||
/** The libraries the server has, in order (also kept in `libraries`). */
|
||||
async listLibraries(): Promise<LibraryInfo[]> {
|
||||
this.libraries = await listLibraries();
|
||||
this.emit();
|
||||
return this.libraries;
|
||||
}
|
||||
|
||||
/** :/recover: save each old replica that is ahead of the server as a file, then stop mentioning them. */
|
||||
recover() {
|
||||
if (!this.oldReplicas.length) return this.say("Nothing to recover: no old replica in this browser holds unsent edits.");
|
||||
for (const r of this.oldReplicas) {
|
||||
const { text } = serializeDoc(Doc.load(r.bytes, this.user).snap());
|
||||
download(`${r.slug}${r.branch === "main" ? "" : `-${r.branch.replace(/[^\w-]+/g, "-")}`}-unsent.md`, text);
|
||||
/** Open another library: a page load, so nothing of this one stays in memory. */
|
||||
switchLibrary(id: string) {
|
||||
this.session?.flush();
|
||||
location.assign(inLibrary(id, "/"));
|
||||
}
|
||||
|
||||
/** A new library, with its root alone; `open` switches to it. */
|
||||
async createLibrary(title: string, open = false): Promise<LibraryInfo | null> {
|
||||
try {
|
||||
const lib = await createLibrary(title);
|
||||
await this.listLibraries();
|
||||
if (open) this.switchLibrary(lib.id);
|
||||
else this.say(`Made the library “${lib.title}”.`);
|
||||
return lib;
|
||||
} catch (err) {
|
||||
this.say(`Could not make the library: ${(err as Error).message}`);
|
||||
return null;
|
||||
}
|
||||
this.say(`Saved ${this.oldReplicas.length} file${this.oldReplicas.length > 1 ? "s" : ""}; :/import brings one in under the node you are on.`);
|
||||
this.oldReplicas = [];
|
||||
void idbSet(`lib:${this.session.library}:old-checked`, true);
|
||||
}
|
||||
|
||||
/** Rename a library (an ordinary commit on its root's title, made by the server). */
|
||||
async renameLibrary(id: string, title: string): Promise<boolean> {
|
||||
try {
|
||||
const lib = await renameLibrary(id, title);
|
||||
await this.listLibraries();
|
||||
this.say(`Renamed the library to “${lib.title}”.`);
|
||||
return true;
|
||||
} catch (err) {
|
||||
this.say(`Could not rename the library: ${(err as Error).message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Delete a library: the server moves its files aside; the last one cannot go. Deleting this one goes to the first other. */
|
||||
async deleteLibrary(id: string): Promise<boolean> {
|
||||
try {
|
||||
await deleteLibrary(id);
|
||||
const left = await this.listLibraries();
|
||||
if (id === this.library) {
|
||||
sessionStorage.setItem("ie:flash", "Deleted the library; its files were moved aside on the server.");
|
||||
this.switchLibrary(left[0].id);
|
||||
} else this.say("Deleted the library; its files were moved aside on the server.");
|
||||
return true;
|
||||
} catch (err) {
|
||||
this.say(`Could not delete the library: ${(err as Error).message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* :/library: with no name, list the libraries; with a name (or id), switch to it; `new <name>`
|
||||
* makes one and switches to it.
|
||||
*/
|
||||
async libraryCommand(arg: string) {
|
||||
const list = await this.listLibraries().catch(() => this.libraries);
|
||||
const words = arg.trim();
|
||||
if (!words) return this.say(`Libraries: ${list.map((l) => `${l.id === this.library ? "▸ " : ""}${l.title} (${l.nodes} nodes)`).join(" · ")}. :/library <name> opens one, :/library new <name> makes one.`);
|
||||
const made = /^new\s+(.+)$/.exec(words);
|
||||
if (made) return void this.createLibrary(made[1], true);
|
||||
const lower = words.toLowerCase();
|
||||
const hit = list.find((l) => l.id === words) ?? list.find((l) => l.title.toLowerCase() === lower) ?? list.find((l) => l.title.toLowerCase().startsWith(lower));
|
||||
if (!hit) return this.say(`No library “${words}”. :/library lists them.`);
|
||||
if (hit.id === this.library) return this.say(`This is “${hit.title}”.`);
|
||||
this.switchLibrary(hit.id);
|
||||
}
|
||||
|
||||
/** The window's title: the article on the column's article page, else the node the reader is on. */
|
||||
@@ -1242,8 +1304,12 @@ class Store {
|
||||
return this.session.synced();
|
||||
}
|
||||
|
||||
/** Views and the branch are kept per library, so each library opens where it was left. */
|
||||
private viewKey(branch = this.repo.current) {
|
||||
return `ie:view:${branch}`;
|
||||
return `ie:view:${this.library}:${branch}`;
|
||||
}
|
||||
private branchKey() {
|
||||
return `ie:branch:${this.library}`;
|
||||
}
|
||||
private restoreView() {
|
||||
const s = this.doc.snap();
|
||||
@@ -1280,11 +1346,11 @@ class Store {
|
||||
|
||||
/** The address of what is on screen: the focused node, so a reload, a link or Back lands on it again. */
|
||||
here(): string {
|
||||
if (!this.panes && this.colWiki) return `/w/${this.colWiki.lang}/${encodeURIComponent(this.colWiki.title.replace(/ /g, "_"))}`;
|
||||
if (!this.panes && this.colWiki) return inLibrary(this.library, `/w/${this.colWiki.lang}/${encodeURIComponent(this.colWiki.title.replace(/ /g, "_"))}`);
|
||||
const s = this.snap;
|
||||
// An annotation has no address of its own: the address stays on what shows its article.
|
||||
if (inArticles(s, this.focus.node)) return location.pathname;
|
||||
return address(s, this.focus.node);
|
||||
return inLibrary(this.library, address(s, this.focus.node));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1306,7 +1372,7 @@ class Store {
|
||||
else this.addressTimer = setTimeout(write, 250);
|
||||
}
|
||||
|
||||
/** Show what a path names: on boot, and on Back and Forward. */
|
||||
/** Show what a path within the library names (`/`, `/n/<id>`, `/w/<lang>/<title>`): on boot, and on Back and Forward. */
|
||||
navigate(path: string, history: "replace" | "none") {
|
||||
const s = this.snap;
|
||||
// An article page of the column has an address of its own, so Back returns to it and from it.
|
||||
@@ -2181,6 +2247,7 @@ class Store {
|
||||
this.branchFrom = null;
|
||||
},
|
||||
switch: () => this.switchBranch(arg),
|
||||
library: () => void this.libraryCommand(arg),
|
||||
merge: () => this.mergeFrom(arg),
|
||||
user: () => {
|
||||
this.user = arg || this.user;
|
||||
@@ -2196,7 +2263,6 @@ class Store {
|
||||
duplicate: () => this.duplicateNode(arg),
|
||||
export: () => this.exportNode(),
|
||||
import: () => this.pickFile(),
|
||||
recover: () => this.recover(),
|
||||
link: () => this.linkTo(arg),
|
||||
unlink: () => this.unlink(),
|
||||
autolink: () => this.setAutolink(arg),
|
||||
@@ -3416,7 +3482,7 @@ class Store {
|
||||
if (!this.repo.branches.has(name)) return this.say(`No branch named ${name}.`);
|
||||
this.preview = null;
|
||||
this.repo.switch(name);
|
||||
lsSet("ie:branch", name);
|
||||
lsSet(this.branchKey(), name);
|
||||
this.restoreView();
|
||||
// Undo covers everything done on this branch from here on.
|
||||
this.undoManager();
|
||||
|
||||
Reference in new issue
Block a user