Files
WRNexusJS/packages/viewer/src/renderers/rtf.ts
T

29 lines
1.1 KiB
TypeScript

import { rtfToText } from "../parse/rtf.ts";
import { elementFactory, requireFile, setButtonLabel } from "./dom.ts";
import type { ViewerMount } from "../types.ts";
/** RTF needs no dependency at all: it is text once the control words are gone. */
export const mountRtf: ViewerMount = async ({ parent, file, button, signal }) => {
const element = elementFactory(parent.ownerDocument);
const source = await requireFile(file).text();
if (signal.aborted) return;
const text = rtfToText(source);
if (!text) throw new Error("Document holds no readable text");
const article = element("article");
article.className = "wrn-viewer-document";
for (const paragraph of text.split(/\n{2,}/))
for (const line of paragraph.split("\n")) article.append(element("p", line));
const raw = element("pre", source);
let showingRaw = false;
const toggle = button("Source", () => {
showingRaw = !showingRaw;
setButtonLabel(toggle, showingRaw ? "Text" : "Source");
toggle.setAttribute("aria-pressed", String(showingRaw));
parent.replaceChildren(showingRaw ? raw : article);
});
toggle.setAttribute("aria-pressed", "false");
parent.append(article);
};