import { tr } from '../i18n/index.ts'; import { EditorView, WidgetType } from "@codemirror/view"; import { isolateHistory } from "@codemirror/commands"; import { imageControls, type ImageActions } from "./imageControls"; import { editImageSource } from "./imageSource"; import { requestText } from "../workspace/dialogs.svelte"; import { isRemoteImage } from "../workspace/media"; import { actionIcon } from "../ui/actionIcons"; export type ImageContext = { open?: (image: HTMLImageElement) => void; reveal?: (image: HTMLImageElement) => Promise; validate?: (source: string) => Promise; report?: (message: string) => void; }; const cleanups = new WeakMap void>(); export class ImagePreview extends WidgetType { constructor(readonly html: string, readonly source: string, readonly context: ImageContext, readonly editing = false, readonly block = true) { super(); } eq(other: ImagePreview) { return this.html === other.html && this.source === other.source && this.editing === other.editing && this.block === other.block; } toDOM(view: EditorView) { const dom = document.createElement(this.block ? "div" : "span"); dom.className = `reading writer-preview ${this.block ? "writer-image" : "writer-image-inline"}`; dom.innerHTML = this.html; const range = () => { const position = view.posAtDOM(dom); const from = this.editing ? position - this.source.length : position; return { from, to: from + this.source.length }; }; const replace = (insert: string) => { if (!dom.isConnected) return; const target = range(); view.dispatch({ changes: { ...target, insert }, selection: { anchor: target.from + insert.length }, effects: editImageSource.of(null), annotations: isolateHistory.of("full"), userEvent: "input.image" }); view.focus(); }; const actions: ImageActions = { editing: this.editing, select: () => { const { from, to } = range(); view.dispatch({ selection: { anchor: from, head: to } }); }, edit: () => { const target = range(); view.dispatch({ effects: [editImageSource.of(target), EditorView.scrollIntoView(target.from, { y: "nearest" })], selection: { anchor: target.from, head: target.to } }); view.focus(); }, done: () => { view.dispatch({ effects: editImageSource.of(null) }); view.focus(); }, remove: () => replace(""), markdown: () => this.source, open: this.context.open, reveal: this.context.reveal, report: this.context.report, swap: async image => { const before = view.state.doc; const path = (await requestText(tr("Image path relative to this note, or https URL"), image.dataset.mdSrc || ""))?.trim(); if (!path || !dom.isConnected || view.state.doc !== before) return; if (!isRemoteImage(path) && /^[a-z][a-z\d+.-]*:/i.test(path)) throw new Error(tr("Choose a workspace image path or an http/https URL.")); await this.context.validate?.(path); if (!dom.isConnected || view.state.doc !== before) return; const target = path.replace(/[<>\s]/g, char => encodeURIComponent(char)); const alt = image.alt.replace(/[\\\[\]]/g, "\\$&"); replace(`![${alt}](<${target}>)`); }, }; cleanups.set(dom, imageControls(dom, actions, () => { if (dom.isConnected) view.requestMeasure(); })); // A missing attachment must still offer a way to repair its hidden source. if (!dom.querySelector("img")) { const edit = document.createElement("button"); edit.type = "button"; edit.className = "image-action"; const label = this.editing ? tr("Hide image source") : tr("Edit image source"); edit.setAttribute("aria-label", label); edit.dataset.editorTooltip = label; edit.append(actionIcon("code")); edit.addEventListener("click", event => { event.preventDefault(); event.stopPropagation(); (this.editing ? actions.done : actions.edit)?.(); }); dom.append(edit); } return dom; } destroy(dom: HTMLElement) { cleanups.get(dom)?.(); } ignoreEvent() { return true; } }