Ship file viewer, editor improvements, and private package releases
This commit is contained in:
1 parent
645914955c
commit
a4a31032b9
158 files changed
+10523
-137
No files matched your search
@@ -0,0 +1,28 @@
|
||||
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);
|
||||
};
|
||||
Reference in new issue
Block a user