Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | 3x 39x 39x 39x 39x 39x 39x 39x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 1x 1x 1x 1x 37x 37x 37x 37x 6x 6x 6x 6x 6x 1x 1x 6x 1x 1x 39x 39x 38x 38x 5x 4x 4x 2x 2x 2x 2x 2x 2x 1x 2x 1x 1x 1x 1x 2x 2x 2x 2x 38x 38x 38x 38x 4x 38x 38x 38x 3x 3x 1x 1x 35x 35x 35x 38x 38x 38x 38x 38x 1x 37x 2x 35x 37x 37x 35x 35x 1x 6x 6x 6x 6x 35x 35x 35x 11x 11x 24x 24x 18x 18x 18x 18x 18x 18x 6x 2x 11x 11x 10x 5x 5x 6x 1x 1x 5x 5x 5x 5x 5x 5x 6x 1x 1x | import { App, Component, MarkdownRenderer, Modal, Notice } from "obsidian";
import type { LilbeeClient } from "../api";
import type { Source, SourceContent } from "../types";
import { CONTENT_TYPE, isPdfContentType } from "../types";
import { MESSAGES } from "../locales/en";
import { bindEscapeToClose, errorMessage } from "../utils";
import { citedLineScrollTop, languageForSource, toCodeFence } from "../utils/code-preview";
import { formatLocation } from "./results";
// Text/* mime types that should NOT render inline through MarkdownRenderer
// even though they pass the broad text/* category. Keep narrow and explicit;
// broadening this set requires a security review.
const TEXT_INLINE_RENDER_DENY = new Set([
"text/html",
"text/javascript",
"application/javascript",
"application/xhtml+xml",
"text/css",
]);
/**
* Read-only preview for sources that aren't in the local vault (external
* server mode, or a vault-bound source whose file was removed). Fetches
* content via `/api/source` and renders markdown inline. PDFs render as a blob
* URL in a type-locked `<object type="application/pdf">` with a `#page=N`
* fragment, which Chromium's PDFium viewer opens at that page. The type lock
* narrows the rendering surface for any non-PDF bytes.
*
* The blob is required: every server route needs the session token, and an
* `<object data>` load cannot send an `Authorization` header.
*
* A future "Save to vault" button will push the content into `<vault>/lilbee/`
* once the server exposes the write path — disabled for now with a tooltip.
*/
export class SourcePreviewModal extends Modal {
private api: LilbeeClient;
private source: Source;
private renderComponent: Component;
/** Live blob URL backing the embedded PDF; revoked when the modal closes. */
private pdfObjectUrl: string | null = null;
private closed = false;
constructor(app: App, api: LilbeeClient, source: Source) {
super(app);
this.api = api;
this.source = source;
this.renderComponent = new Component();
bindEscapeToClose(this);
}
onOpen(): void {
this.renderComponent.load();
const { contentEl, modalEl } = this;
contentEl.empty();
contentEl.addClass("lilbee-modal");
contentEl.addClass("lilbee-preview-modal");
// The outer modal frame carries sizing, ``resize: both``, and
// ``position: fixed`` via this class; the inner content element would
// be clipped by the frame's default ``overflow: hidden``.
modalEl.addClass("lilbee-preview-modal-frame");
contentEl.createEl("h2", { text: MESSAGES.TITLE_SOURCE_PREVIEW });
this.makeDraggable(contentEl);
this.renderHeader(contentEl);
if (!this.source.source) {
new Notice(MESSAGES.ERROR_PREVIEW_INVALID_SOURCE);
contentEl.createEl("p", { text: MESSAGES.ERROR_PREVIEW_INVALID_SOURCE, cls: "lilbee-preview-error" });
this.renderFooter(contentEl);
return;
}
const bodyHost = contentEl.createDiv({ cls: "lilbee-preview-host" });
bodyHost.createDiv({ cls: "lilbee-preview-loading" });
this.renderFooter(contentEl);
void this.loadContent(bodyHost);
}
onClose(): void {
this.closed = true;
this.releasePdfObjectUrl();
this.renderComponent.unload();
this.contentEl.empty();
if (this.dragMoveHandler) {
window.removeEventListener("pointermove", this.dragMoveHandler);
this.dragMoveHandler = null;
}
if (this.dragUpHandler) {
window.removeEventListener("pointerup", this.dragUpHandler);
this.dragUpHandler = null;
}
}
private dragMoveHandler: ((e: PointerEvent) => void) | null = null;
private dragUpHandler: ((e: PointerEvent) => void) | null = null;
// Promotes Obsidian's auto-centered modal to explicit top/left on the
// first drag so the modal stays where the user puts it across resize.
// The whole chrome (title, header, footer, padding) is the drag surface,
// but pointer events that land inside the embedded body or on an
// interactive element (button/input/link) pass through.
private makeDraggable(handle: HTMLElement): void {
handle.addClass("lilbee-preview-drag-handle");
handle.addEventListener("pointerdown", (down: PointerEvent) => {
if (down.button !== 0) return;
const target = down.target as HTMLElement | null;
if (target?.closest(".lilbee-preview-host, button, input, textarea, select, a")) return;
down.preventDefault();
const rect = this.modalEl.getBoundingClientRect();
const offsetX = down.clientX - rect.left;
const offsetY = down.clientY - rect.top;
this.modalEl.setCssProps({ margin: "0", left: `${rect.left}px`, top: `${rect.top}px` });
const move = (e: PointerEvent): void => {
this.modalEl.setCssProps({ left: `${e.clientX - offsetX}px`, top: `${e.clientY - offsetY}px` });
};
const up = (): void => {
window.removeEventListener("pointermove", move);
window.removeEventListener("pointerup", up);
this.dragMoveHandler = null;
this.dragUpHandler = null;
};
this.dragMoveHandler = move;
this.dragUpHandler = up;
window.addEventListener("pointermove", move);
window.addEventListener("pointerup", up);
});
}
private renderHeader(container: HTMLElement): void {
const header = container.createDiv({ cls: "lilbee-preview-header" });
header.createSpan({
text: this.source.vault_path ?? this.source.source,
cls: "lilbee-preview-path",
});
const loc = formatLocation(this.source);
if (loc) {
container.createSpan({ text: loc, cls: "lilbee-preview-meta" });
}
}
private renderFooter(container: HTMLElement): void {
const footer = container.createDiv({ cls: "lilbee-preview-footer" });
// When the source resolves to a vault file, swap the disabled
// "Save to vault" stub for an "Open in vault" affordance — clicking
// it dismisses the preview and opens the original file in the main
// pane via Obsidian's standard link routing.
const inVaultPath = this.resolveVaultPath();
if (inVaultPath !== null) {
const openBtn = footer.createEl("button", {
text: MESSAGES.LABEL_PREVIEW_OPEN_IN_VAULT,
cls: "lilbee-preview-open-vault",
});
openBtn.addEventListener("click", () => {
void this.app.workspace.openLinkText(inVaultPath, "");
this.close();
});
} else {
const save = footer.createEl("button", {
text: MESSAGES.LABEL_PREVIEW_SAVE_TO_VAULT,
cls: "lilbee-preview-save",
});
save.disabled = true;
save.setAttribute("title", MESSAGES.TOOLTIP_PREVIEW_SAVE_SOON);
}
const close = footer.createEl("button", {
text: MESSAGES.LABEL_PREVIEW_CLOSE,
cls: "lilbee-preview-close mod-cta",
});
close.addEventListener("click", () => this.close());
}
private resolveVaultPath(): string | null {
const vault = this.app.vault;
const vaultPath = this.source.vault_path;
if (vaultPath && vault.getAbstractFileByPath(vaultPath)) {
return vaultPath;
}
if (this.source.source && vault.getAbstractFileByPath(this.source.source)) {
return this.source.source;
}
return null;
}
private async loadContent(host: HTMLElement): Promise<void> {
try {
const content = await this.api.getSource(this.source.source);
host.empty();
this.renderBody(host, content);
} catch (err) {
this.showLoadError(host, err);
}
}
private showLoadError(host: HTMLElement, err: unknown): void {
const message = MESSAGES.ERROR_PREVIEW_LOAD(errorMessage(err, String(err)));
host.empty();
host.createEl("p", { text: message, cls: "lilbee-preview-error" });
new Notice(message);
}
private renderBody(host: HTMLElement, content: SourceContent): void {
const ct = content.content_type;
const isPdf = isPdfContentType(this.source.content_type) || isPdfContentType(ct);
if (isPdf) {
void this.renderPdf(host);
return;
}
// Mirror the server's raw-content allowlist: any text/* may render through
// MarkdownRenderer, except formats that can carry script (text/html,
// text/javascript, application/xhtml+xml) or styling (text/css). The
// <object> tag protects the PDF path; this gate protects the JSON path.
const textBlocked = TEXT_INLINE_RENDER_DENY.has(ct);
if (!textBlocked && ct.startsWith("text/")) {
const body = host.createDiv({ cls: "lilbee-preview-body" });
// Code files render as raw text through MarkdownRenderer (no
// highlighting); fence them in their language so Obsidian's
// highlighter styles them. Markdown/plain text render as-is.
const lang = languageForSource(this.source.source);
const markdown = lang === null ? content.markdown : toCodeFence(content.markdown, lang);
void MarkdownRenderer.render(this.app, markdown, body, "", this.renderComponent).then(() => {
if (lang !== null) this.scrollToCitedLine(host, body, content.markdown);
});
return;
}
host.createEl("p", {
text: MESSAGES.ERROR_PREVIEW_UNSUPPORTED(ct),
cls: "lilbee-preview-error",
});
}
/** Scroll the rendered code so the cited chunk's first line is in view. */
private scrollToCitedLine(host: HTMLElement, body: HTMLElement, code: string): void {
host.scrollTop = citedLineScrollTop(body.scrollHeight, code.split("\n").length, this.source.line_start);
}
/** Fetch the PDF bytes through the authenticated client into a blob URL. */
private async renderPdf(host: HTMLElement): Promise<void> {
let url: string;
try {
const res = await this.api.getSourceRaw(this.source.source);
url = URL.createObjectURL(await res.blob());
} catch (err) {
this.showLoadError(host, err);
return;
}
if (this.closed) {
// Dismissed mid-fetch: onClose already ran and never saw this URL.
URL.revokeObjectURL(url);
return;
}
this.pdfObjectUrl = url;
const body = host.createDiv({ cls: "lilbee-preview-body" });
const frame = body.createEl("object", { cls: "lilbee-preview-pdf-frame" });
frame.setAttribute("type", CONTENT_TYPE.PDF);
// `#page=N` makes Chromium's PDFium viewer open at the chunk's page.
const page = this.source.page_start;
frame.setAttribute("data", page && page > 0 ? `${url}#page=${page}` : url);
}
private releasePdfObjectUrl(): void {
if (this.pdfObjectUrl === null) return;
URL.revokeObjectURL(this.pdfObjectUrl);
this.pdfObjectUrl = null;
}
}
|