CI / go (Server) (push) Failing after 20m11s
CI / mobile (push) Failing after 7m33s
CI / go (Web-App) (push) Canceled after 4s
CI / docker (push) Successful in 49m7s
Docs / build (push) Successful in 12m55s
Docs / deploy (push) Skipped
CI / frontend (push) Failing after 8m8s
CI / desktop (ubuntu-latest) (push) Successful in 1h41m4s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
A record can be linked like a block: the page's address plus "#r-<database>-<record>". Opening the link loads the page, scrolls to the database and opens the record beside it; a link to a deleted record just shows the page. Records stay part of their page, so the link needs nothing beyond the page's own access. "Copy link to record" is in the record's panel header and in the row's right-click menu, which readers now get too (Open and Copy link only). The page view opens record links the way it scrolls to block links, retrying while the shared document loads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// Links to one record of a database: the page's address plus
|
|
// "#r-<database id>-<record id>". Opening one scrolls to the database and
|
|
// opens the record beside the page. Records stay part of their page: the
|
|
// link needs the page's own access, nothing more.
|
|
|
|
import type { Editor } from "@tiptap/core";
|
|
|
|
const ID = "[A-Za-z0-9]{1,32}";
|
|
|
|
/** The #fragment of a link to a record. */
|
|
export const recordHash = (database: string, row: string) => `#r-${database}-${row}`;
|
|
|
|
/** The database and record in a link's #fragment, or null. */
|
|
export function linkedRecord(hash: string): { database: string; row: string } | null {
|
|
const m = new RegExp(`^#?r-(${ID})-(${ID})$`).exec(hash);
|
|
return m ? { database: m[1]!, row: m[2]! } : null;
|
|
}
|
|
|
|
/** The event a database block listens for to open one of its records. */
|
|
export const OPEN_RECORD = "dw-open-record";
|
|
|
|
export interface OpenRecordDetail {
|
|
row: string;
|
|
/** Set by the block when it has the record and opened it. */
|
|
found?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Opens a record of the page's database. Returns false while the page does
|
|
* not show it (yet): the document may still be loading, or it was deleted.
|
|
*/
|
|
export function openRecord(editor: Editor, database: string, row: string): boolean {
|
|
let pos = -1;
|
|
editor.state.doc.descendants((n, p) => {
|
|
if (pos >= 0) return false;
|
|
if (n.type.name === "database" && n.attrs.id === database) pos = p;
|
|
return true;
|
|
});
|
|
if (pos < 0) return false;
|
|
const dom = editor.view.nodeDOM(pos);
|
|
if (!(dom instanceof HTMLElement)) return false;
|
|
const detail: OpenRecordDetail = { row };
|
|
dom.dispatchEvent(new CustomEvent(OPEN_RECORD, { detail }));
|
|
return !!detail.found;
|
|
}
|
|
|
|
/** Copies text to the clipboard; resolves false when the browser refused. */
|
|
export async function copyText(text: string): Promise<boolean> {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|