Files
superbrain/web/lib/pages.ts
T
Andrew S Erwin 9dbf9fa3ea Library sidebar: folders, pinned-top, per-page menus, and an all-pages view
- Page.isFolder (migration 027): folders are content-less pages reusing
  parentId; folder delete reparents children one level up
- Sidebar: Pages group becomes Library; Pinned at top; folder tree; page
  menu (duplicate/pin/copy link/rename/move/trash); folder menu
- /pages index: filterable Library table (all/recent/pinned/untagged) with
  folder path + last edit (components/library-index.tsx)
- Docs: API.md isFolder + folder-delete semantics
2026-12-01 06:07:12 -06:00

92 lines
3.5 KiB
TypeScript

export const PAGES_CHANGED = 'superbrain:pages-changed'
export const notifyPagesChanged = () => window.dispatchEvent(new Event(PAGES_CHANGED))
// Notion-style breadcrumbs: the pages workspace announces which page is open
// so the top bar can show "superbrain / Pages / <title>" without refetching.
export const ACTIVE_PAGE_CHANGED = 'superbrain:active-page-changed'
export const notifyActivePage = (title: string | null) =>
window.dispatchEvent(new CustomEvent<string | null>(ACTIVE_PAGE_CHANGED, { detail: title }))
export type PageSummary = {
id: string
title: string
pinned: boolean
position: number
parentId: string | null
isFolder?: boolean
updatedAt?: string
tags?: { tag: { id: string; name: string; color: string } }[]
_count?: { bookmarks: number; children?: number }
}
// Connected pages (phase 3): the same link forms the editor understands,
// shared by the page client, sidebar and Markdown preview. Page links use
// `superbrain:page/<id>`; /pages?id=<id> is accepted for hand-written links.
export function internalPageId(href: string): string | null {
const value = href.trim().replace(/^<|>$/g, '')
let raw = ''
if (value.startsWith('superbrain://page/')) raw = value.slice('superbrain://page/'.length)
else if (value.startsWith('superbrain:page/')) raw = value.slice('superbrain:page/'.length)
else if (value.startsWith('page/')) raw = value.slice('page/'.length)
else if (value.startsWith('/pages/')) raw = value.slice('/pages/'.length)
else if (value.startsWith('/pages?') || value.startsWith('superbrain:page?id=') || value.startsWith('superbrain://page?id=')) {
try {
const id = new URL(value, 'http://localhost').searchParams.get('id')
raw = id ?? ''
} catch { raw = '' }
}
raw = raw.replace(/\/$/, '')
return /^[A-Za-z0-9_-]{1,128}$/.test(raw) ? raw : null
}
export const pageLinkMarkdown = (page: Pick<PageSummary, 'id' | 'title'>) =>
`[${page.title.replace(/[\[\]\n]/g, ' ')}](superbrain:page/${page.id})`
export type PageEmbedMarker = { collectionId: string; viewId: string | null }
export function parsePageEmbed(value: string): PageEmbedMarker | null {
const m = /^\{\{\s*collection:([A-Za-z0-9_-]{1,128})(?:\s*\|\s*view=([A-Za-z0-9_-]{1,128}))?\s*\}\}$/.exec(value.trim())
if (!m) return null
return { collectionId: m[1], viewId: m[2] ?? null }
}
export const collectionEmbedMarkdown = (collectionId: string, viewId?: string | null) =>
`{{collection:${collectionId}${viewId ? `|view=${viewId}` : ''}}}`
// Ids below `id` in the current page tree. Used to keep the parent picker
// from offering a descendant (the server rejects the cycle regardless).
export function descendantPageIds(pages: PageSummary[], id: string): Set<string> {
const out = new Set<string>()
const children = new Map<string, string[]>()
for (const p of pages) {
if (!p.parentId) continue
const list = children.get(p.parentId) ?? []
list.push(p.id)
children.set(p.parentId, list)
}
const walk = (parent: string) => {
for (const child of children.get(parent) ?? []) {
if (out.has(child)) continue
out.add(child)
walk(child)
}
}
walk(id)
return out
}
// Pages start as "Untitled 1", "Untitled 2", ... until the user renames them.
// Pick the lowest positive integer not already used by an untitled page.
export function nextUntitledTitle(titles: string[]): string {
const used = new Set<number>()
for (const t of titles) {
const m = /^Untitled (\d+)$/.exec(t.trim())
if (m) used.add(parseInt(m[1], 10))
}
let n = 1
while (used.has(n)) n++
return `Untitled ${n}`
}