Initial RuneStone project
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
<script lang="ts">
|
||||
import { tick, type Snippet } from "svelte";
|
||||
import { workspaceReadDataUrl } from "$lib/workspace/api";
|
||||
import { markdownImages, renderMarkdownHtml, renderMarkdownSections, type MarkdownSection } from "$lib/workspace/noteHtml";
|
||||
import { renderDiagrams } from "$lib/workspace/diagrams";
|
||||
import { isRemoteImage, resolveImageRel, markdownTitle } from "$lib/workspace/media";
|
||||
import { imageControls } from "$lib/editor/imageControls";
|
||||
|
||||
type Props = {
|
||||
noteRel: string;
|
||||
contents: string;
|
||||
onOpenLink?: (kind: "md" | "wiki", target: string) => void;
|
||||
onEditBlock?: (start: number, end: number) => void;
|
||||
editing?: { start: number; end: number } | null;
|
||||
blockEditor?: Snippet;
|
||||
};
|
||||
|
||||
let { noteRel, contents, onOpenLink, onEditBlock, editing = null, blockEditor }: Props = $props();
|
||||
let html = $state("");
|
||||
let sections = $state<MarkdownSection[]>([]);
|
||||
let root: HTMLDivElement | undefined = $state();
|
||||
|
||||
async function loadUrls(noteRel: string, contents: string): Promise<Map<string, string>> {
|
||||
const urls = new Map<string, string>();
|
||||
await Promise.all(markdownImages(contents).map(async source => {
|
||||
if (isRemoteImage(source)) {
|
||||
urls.set(source, source.trim());
|
||||
return;
|
||||
}
|
||||
let path = source;
|
||||
try { path = decodeURIComponent(source); } catch { /* Keep literal malformed escapes. */ }
|
||||
const primary = resolveImageRel(noteRel, path);
|
||||
const normalized = path.replaceAll("\\", "/").replace(/^\/+/, "");
|
||||
const base = normalized.split("/").pop() ?? path;
|
||||
for (const candidate of new Set([primary, normalized, base])) {
|
||||
try {
|
||||
urls.set(source, await workspaceReadDataUrl(candidate));
|
||||
return;
|
||||
} catch { /* Try the workspace-relative attachment next. */ }
|
||||
}
|
||||
urls.set(source, "");
|
||||
}));
|
||||
return urls;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
const path = noteRel;
|
||||
const text = contents;
|
||||
let cancelled = false;
|
||||
html = renderMarkdownHtml(text, new Map());
|
||||
sections = renderMarkdownSections(text, new Map());
|
||||
void loadUrls(path, text).then(urls => {
|
||||
if (!cancelled) {
|
||||
html = renderMarkdownHtml(text, urls);
|
||||
sections = renderMarkdownSections(text, urls);
|
||||
}
|
||||
});
|
||||
return () => { cancelled = true; };
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
void html;
|
||||
void sections;
|
||||
void editing;
|
||||
const el = root;
|
||||
let cancelled = false;
|
||||
let releaseImages: (() => void) | undefined;
|
||||
if (el) void tick().then(() => {
|
||||
if (cancelled) return;
|
||||
releaseImages = imageControls(el);
|
||||
return renderDiagrams(el, () => cancelled);
|
||||
});
|
||||
return () => { cancelled = true; releaseImages?.(); };
|
||||
});
|
||||
|
||||
async function copyFence(button: HTMLButtonElement, body: string) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(body);
|
||||
} catch {
|
||||
const ta = document.createElement("textarea");
|
||||
ta.value = body;
|
||||
ta.setAttribute("readonly", "");
|
||||
ta.style.position = "fixed";
|
||||
ta.style.left = "-9999px";
|
||||
document.body.appendChild(ta);
|
||||
ta.select();
|
||||
try {
|
||||
document.execCommand("copy");
|
||||
} finally {
|
||||
ta.remove();
|
||||
}
|
||||
}
|
||||
button.dataset.copied = "true";
|
||||
button.setAttribute("aria-label", "Copied");
|
||||
button.title = "Copied";
|
||||
window.setTimeout(() => {
|
||||
if (button.isConnected) {
|
||||
delete button.dataset.copied;
|
||||
button.setAttribute("aria-label", "Copy code");
|
||||
button.title = "Copy code";
|
||||
}
|
||||
}, 1200);
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- Events are delegated from generated native links and buttons, which support keyboard activation. -->
|
||||
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
|
||||
<div
|
||||
class="reading"
|
||||
bind:this={root}
|
||||
onkeydown={(event) => {
|
||||
if (event.target instanceof HTMLElement && event.target.classList.contains("md-edit-block") && (event.key === "Enter" || event.key === " ")) {
|
||||
event.preventDefault();
|
||||
onEditBlock?.(Number(event.target.dataset.start), Number(event.target.dataset.end));
|
||||
}
|
||||
}}
|
||||
onclick={(event) => {
|
||||
const copy = event.target instanceof Element ? event.target.closest("button.rs-fence-copy") : null;
|
||||
if (copy instanceof HTMLButtonElement) {
|
||||
event.preventDefault();
|
||||
const pre = copy.closest("pre.md-code") ?? copy.closest("pre");
|
||||
const body = pre?.querySelector("code")?.textContent ?? "";
|
||||
void copyFence(copy, body);
|
||||
return;
|
||||
}
|
||||
const node = event.target instanceof Element ? event.target.closest("a") : null;
|
||||
if (!node) {
|
||||
const target = event.target instanceof Element ? event.target : null;
|
||||
if (target?.closest("textarea, button, summary")) return;
|
||||
const block = target?.closest<HTMLElement>(".md-edit-block");
|
||||
if (block) onEditBlock?.(Number(block.dataset.start), Number(block.dataset.end));
|
||||
return;
|
||||
}
|
||||
const href = node.getAttribute("href") ?? "";
|
||||
const kind = node.getAttribute("data-kind");
|
||||
if (kind === "wiki") {
|
||||
event.preventDefault();
|
||||
onOpenLink?.("wiki", href);
|
||||
return;
|
||||
}
|
||||
if (kind === "md") {
|
||||
event.preventDefault();
|
||||
onOpenLink?.("md", href);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<h1 class="document-title">{markdownTitle(noteRel)}</h1>
|
||||
{#if onEditBlock}
|
||||
{#each sections.filter(section => !editing || section.start < editing.start) as section (section.start)}
|
||||
<div class="md-edit-block" role="button" tabindex="0" aria-label="Edit document block" data-start={section.start} data-end={section.end}>{@html section.html}</div>
|
||||
{/each}
|
||||
{#if editing && blockEditor}
|
||||
{@render blockEditor()}
|
||||
{#each sections.filter(section => section.start >= editing.end && section.end > editing.end) as section (section.start)}
|
||||
<div class="md-edit-block" role="button" tabindex="0" aria-label="Edit document block" data-start={section.start} data-end={section.end}>{@html section.html}</div>
|
||||
{/each}
|
||||
{/if}
|
||||
{:else}
|
||||
{@html html}
|
||||
{/if}
|
||||
</div>
|
||||
Reference in New Issue
Block a user