Core for several libraries: graft carries the Wikipedia flag, keeps a top's anchors when asked and maps old ids to new; a library's title at creation; addresses under /l/<library>
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
c4d6856649
commit
045a8459da
2 files changed
+44
-8
No files matched your search
+29
-3
@@ -1,15 +1,41 @@
|
|||||||
/**
|
/**
|
||||||
* Where a node is, as the page's address: `/` is the library root, `/n/<node id>` any other node.
|
* Where a node is, as the page's address. Every address starts with its library: `/l/<library id>`
|
||||||
* The id is all there is to it, so an address survives renames and moves.
|
* is the library's root, `/l/<library id>/n/<node id>` any other node of it, and
|
||||||
|
* `/l/<library id>/w/<lang>/<title>` an article as a page of its own. The node id is all there is to
|
||||||
|
* a node's part, so an address survives renames and moves. An address without a library (`/`, or
|
||||||
|
* one from before libraries, `/n/…`) is read in the library last used.
|
||||||
*/
|
*/
|
||||||
import type { NodeId, Snap } from "./model";
|
import type { NodeId, Snap } from "./model";
|
||||||
|
|
||||||
|
/** A library's id: what `Doc.library` makes (base 36), also a directory name on the server. */
|
||||||
|
export const LIBRARY_ID = /^[0-9a-z]{1,32}$/;
|
||||||
|
|
||||||
|
/** The part of an address within its library: `/` for the root, `/n/<node id>` for any other node. */
|
||||||
export function address(snap: Snap, id: NodeId): string {
|
export function address(snap: Snap, id: NodeId): string {
|
||||||
if (!snap.nodes[id] || id === snap.root) return "/";
|
if (!snap.nodes[id] || id === snap.root) return "/";
|
||||||
return `/n/${encodeURIComponent(id)}`;
|
return `/n/${encodeURIComponent(id)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The node a path names, or why there is none. */
|
/** A whole address: the library's prefix, then the part within it. */
|
||||||
|
export function inLibrary(library: string, part: string): string {
|
||||||
|
const base = `/l/${encodeURIComponent(library)}`;
|
||||||
|
return part === "/" || part === "" ? base : base + part;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** An address split into its library and the part within it; no library if it names none. */
|
||||||
|
export function splitLibrary(path: string): { library: string | null; part: string } {
|
||||||
|
const m = /^\/l\/([^/]+)(\/.*)?$/.exec(path);
|
||||||
|
if (!m) return { library: null, part: path || "/" };
|
||||||
|
let id = m[1];
|
||||||
|
try {
|
||||||
|
id = decodeURIComponent(id);
|
||||||
|
} catch {
|
||||||
|
/* kept as it came; it will name no library */
|
||||||
|
}
|
||||||
|
return { library: id, part: m[2] || "/" };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The node a path within a library names, or why there is none. */
|
||||||
export function resolve(snap: Snap, path: string): { node: NodeId } | { missing: string } {
|
export function resolve(snap: Snap, path: string): { node: NodeId } | { missing: string } {
|
||||||
let parts: string[];
|
let parts: string[];
|
||||||
try {
|
try {
|
||||||
|
|||||||
+15
-5
@@ -222,13 +222,13 @@ export class Doc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** An empty library. Its id is kept in the database, so a replica of another library is never mistaken for it. */
|
/** An empty library. Its id is kept in the database, so a replica of another library is never mistaken for it. */
|
||||||
static library(author: string, id = randomPeer().toString(36)): Doc {
|
static library(author: string, id = randomPeer().toString(36), title = LIBRARY_TITLE): Doc {
|
||||||
const loro = new LoroDoc();
|
const loro = new LoroDoc();
|
||||||
loro.setPeerId(randomPeer());
|
loro.setPeerId(randomPeer());
|
||||||
const doc = new Doc(loro, author);
|
const doc = new Doc(loro, author);
|
||||||
doc.begin("Create the library", "human");
|
doc.begin("Create the library", "human");
|
||||||
const root = loro.getTree("tree").createNode();
|
const root = loro.getTree("tree").createNode();
|
||||||
initRoot(root, LIBRARY_TITLE, "", "human");
|
initRoot(root, title, "", "human");
|
||||||
root.data.set("library", true);
|
root.data.set("library", true);
|
||||||
loro.getMap("meta").set("library", id);
|
loro.getMap("meta").set("library", id);
|
||||||
doc.commit("Create the library", "human");
|
doc.commit("Create the library", "human");
|
||||||
@@ -622,14 +622,22 @@ export class Doc {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a copy of a subtree (from this document or another) under `parent`, as it stands:
|
* Write a copy of a subtree (from this document or another) under `parent`, as it stands:
|
||||||
* content, authorship, pins and the hashes it was last reconciled with. Must run inside a commit.
|
* content, authorship, locks, targets, declined phrases, the Wikipedia flags and the hashes it was
|
||||||
|
* last reconciled with. Must run inside a commit.
|
||||||
|
*
|
||||||
|
* The top's anchors are offsets into its parent's text, so they are kept only where that text is
|
||||||
|
* the same: when the copy lands beside the original, or when the caller says the new parent's
|
||||||
|
* text is a copy of the old one's (`keepAnchors`). `ids`, if given, is filled with each copied
|
||||||
|
* node's new id by its old one. Targets are copied as they are; a caller copying between
|
||||||
|
* libraries rewrites the node targets through `ids`.
|
||||||
*/
|
*/
|
||||||
graft(src: Snap, from: NodeId, parent: NodeId, opts: { index?: number; title?: string } = {}): NodeId {
|
graft(src: Snap, from: NodeId, parent: NodeId, opts: { index?: number; title?: string; keepAnchors?: boolean; ids?: Map<NodeId, NodeId> } = {}): NodeId {
|
||||||
const { index, title } = opts;
|
const { index, title } = opts;
|
||||||
const into = this.node(parent);
|
const into = this.node(parent);
|
||||||
const visit = (id: NodeId, under: LoroTreeNode, at?: number): NodeId => {
|
const visit = (id: NodeId, under: LoroTreeNode, at?: number): NodeId => {
|
||||||
const s = src.nodes[id];
|
const s = src.nodes[id];
|
||||||
const n = at === undefined ? under.createNode() : under.createNode(at);
|
const n = at === undefined ? under.createNode() : under.createNode(at);
|
||||||
|
opts.ids?.set(id, n.id);
|
||||||
n.data.set("kind", s.kind);
|
n.data.set("kind", s.kind);
|
||||||
n.data.set("title", id === from && title !== undefined ? title : s.title);
|
n.data.set("title", id === from && title !== undefined ? title : s.title);
|
||||||
(n.data.setContainer("body", new LoroText()) as LoroText).insert(0, s.body);
|
(n.data.setContainer("body", new LoroText()) as LoroText).insert(0, s.body);
|
||||||
@@ -639,12 +647,14 @@ export class Doc {
|
|||||||
if (s.lens) n.data.set("lens", s.lens);
|
if (s.lens) n.data.set("lens", s.lens);
|
||||||
if (s.ref) n.data.set("ref", s.ref as never);
|
if (s.ref) n.data.set("ref", s.ref as never);
|
||||||
if (s.wanchor) n.data.set("wanchor", s.wanchor as never);
|
if (s.wanchor) n.data.set("wanchor", s.wanchor as never);
|
||||||
|
// Only a section of the library root can be the Wikipedia container, as `create` has it.
|
||||||
|
if (s.articles && under.data.get("library")) n.data.set("articles", true);
|
||||||
if (s.nolink?.length) setNolink(n, s.nolink);
|
if (s.nolink?.length) setNolink(n, s.nolink);
|
||||||
n.data.set("up", s.seenUp);
|
n.data.set("up", s.seenUp);
|
||||||
n.data.set("down", s.seenDown);
|
n.data.set("down", s.seenDown);
|
||||||
// The copy's anchors are offsets into its parent's body, which is a copy too; except at the
|
// The copy's anchors are offsets into its parent's body, which is a copy too; except at the
|
||||||
// top, where they would be offsets into a different text unless it lands beside the original.
|
// top, where they would be offsets into a different text unless it lands beside the original.
|
||||||
const kept = id !== from || parent === s.parent ? s.anchors.filter((a) => a.end > a.start) : [];
|
const kept = id !== from || parent === s.parent || opts.keepAnchors ? s.anchors.filter((a) => a.end > a.start) : [];
|
||||||
if (kept.length) n.data.set("anchors", this.makeAnchors(under, kept.map((a) => [a.start, a.end]), kept.map((a) => a.quote)) as never);
|
if (kept.length) n.data.set("anchors", this.makeAnchors(under, kept.map((a) => [a.start, a.end]), kept.map((a) => a.quote)) as never);
|
||||||
this.mark(n.id);
|
this.mark(n.id);
|
||||||
for (const c of s.children) visit(c, n);
|
for (const c of s.children) visit(c, n);
|
||||||
|
|||||||
Reference in new issue
Block a user