The editor gains the blocks and formatting it lacked next to AFFiNE: - Tables with a header row, resizable columns, a toolbar to add, delete, merge and split, and rows that move by a drag grip, toolbar buttons or Ctrl+Alt+Up/Down. - To-do lists (also from "[] "), callouts with an emoji and colour, and headings down to level 6 (Ctrl+Shift+0-6 for text and headings). - Text colour and highlights from a fixed palette that follows the theme, and left, centre and right alignment. - Optional line numbers on code blocks, with a default hosts can set. - PDF attachments show their pages inline; pdf.js loads only then, and the mobile bundle leaves it out. - The selection toolbar adds underline, colour, align, inline math, make a linked page and copy block link. A grip beside each block drags it and opens a menu: turn into, move, duplicate, copy, copy link, delete. - The slash menu adds Today, Tomorrow, Yesterday, Now and New page. The web app translates the new words and makes linked pages as children of the open page. The mobile bridge gets underline, to-do, callout and table commands, so its protocol moves to version 2 and the bundle is rebuilt. docwell-server's converters learn the new nodes: Markdown writes task lists, GitHub alerts and tables and reads them back; HTML keeps colours and alignment and reads its own output back; PDF draws tables, to-dos, callouts and coloured text; Word import keeps tables, alignment, H4-H6 and colours; Notion import turns databases into tables and keeps callouts and to-dos. Imported tables and H4-H6 headings no longer collapse into paragraphs and H3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
3.4 KiB
TypeScript
96 lines
3.4 KiB
TypeScript
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import type { EditorLabels } from "./labels";
|
|
|
|
/** The part of pdf.js the viewer uses; hosts load it (see pdfjs.ts) so the bridge can leave it out. */
|
|
export interface PdfJs {
|
|
getDocument(src: { url: string; withCredentials?: boolean }): {
|
|
promise: Promise<PdfDocument>;
|
|
destroy(): Promise<void>;
|
|
};
|
|
}
|
|
interface PdfDocument {
|
|
numPages: number;
|
|
getPage(n: number): Promise<PdfPage>;
|
|
}
|
|
interface PdfPage {
|
|
getViewport(o: { scale: number }): { width: number; height: number };
|
|
render(o: { canvas: HTMLCanvasElement; viewport: { width: number; height: number } }): { promise: Promise<void> };
|
|
}
|
|
|
|
export type PdfLoader = () => Promise<PdfJs>;
|
|
|
|
/** A PDF attachment's file name. */
|
|
export const isPdf = (mime: unknown, name: unknown) =>
|
|
mime === "application/pdf" || (typeof name === "string" && /\.pdf$/i.test(name));
|
|
|
|
/**
|
|
* Draws the pages of the PDF at url into box, each as it scrolls into view.
|
|
* Returns a function that stops loading and frees the document.
|
|
*/
|
|
export function showPdf(box: HTMLElement, url: string, load: PdfLoader, labels: EditorLabels): () => void {
|
|
let stopped = false;
|
|
let observer: IntersectionObserver | null = null;
|
|
let task: ReturnType<PdfJs["getDocument"]> | null = null;
|
|
const status = document.createElement("div");
|
|
status.className = "dw-pdf__status";
|
|
status.textContent = labels.blocks.pdfLoading;
|
|
box.replaceChildren(status);
|
|
|
|
void (async () => {
|
|
try {
|
|
const pdfjs = await load();
|
|
if (stopped) return;
|
|
task = pdfjs.getDocument({ url });
|
|
const doc = await task.promise;
|
|
if (stopped) return;
|
|
status.remove();
|
|
const width = Math.max(box.clientWidth - 2, 200);
|
|
const ratio = window.devicePixelRatio || 1;
|
|
observer = new IntersectionObserver(
|
|
(entries) => {
|
|
for (const entry of entries) {
|
|
if (!entry.isIntersecting) continue;
|
|
const slot = entry.target as HTMLElement;
|
|
observer?.unobserve(slot);
|
|
void draw(slot, Number(slot.dataset.page));
|
|
}
|
|
},
|
|
{ root: box, rootMargin: "400px 0px" },
|
|
);
|
|
const first = await doc.getPage(1);
|
|
const base = first.getViewport({ scale: 1 });
|
|
for (let n = 1; n <= doc.numPages; n++) {
|
|
const slot = document.createElement("div");
|
|
slot.className = "dw-pdf__page";
|
|
slot.dataset.page = String(n);
|
|
// Until it is drawn, a page takes the first page's shape.
|
|
slot.style.aspectRatio = `${base.width} / ${base.height}`;
|
|
slot.setAttribute("aria-label", labels.blocks.pdfPage.replace("{n}", String(n)));
|
|
box.append(slot);
|
|
observer.observe(slot);
|
|
}
|
|
async function draw(slot: HTMLElement, n: number) {
|
|
const page = await doc.getPage(n);
|
|
if (stopped) return;
|
|
const scale = width / page.getViewport({ scale: 1 }).width;
|
|
const viewport = page.getViewport({ scale: scale * ratio });
|
|
const canvas = document.createElement("canvas");
|
|
canvas.width = Math.floor(viewport.width);
|
|
canvas.height = Math.floor(viewport.height);
|
|
slot.style.aspectRatio = `${viewport.width} / ${viewport.height}`;
|
|
slot.append(canvas);
|
|
await page.render({ canvas, viewport }).promise;
|
|
}
|
|
} catch {
|
|
if (!stopped) status.textContent = labels.blocks.loadFailed;
|
|
}
|
|
})();
|
|
|
|
return () => {
|
|
stopped = true;
|
|
observer?.disconnect();
|
|
void task?.destroy();
|
|
};
|
|
}
|