silverbullet/web/cm_plugins/inline_image.ts

118 lines
3.5 KiB
TypeScript
Raw Normal View History

import { EditorState, Range } from "@codemirror/state";
import { syntaxTree } from "@codemirror/language";
import { Decoration, WidgetType } from "@codemirror/view";
import { decoratorStateField } from "./util.ts";
2023-07-14 22:56:20 +08:00
import type { Client } from "../client.ts";
2024-05-28 02:33:41 +08:00
import { isLocalPath, resolvePath } from "$sb/lib/resolve.ts";
import { mdLinkRegex, wikiLinkRegex } from "$common/markdown_parser/parser.ts";
2022-08-23 14:12:24 +08:00
class InlineImageWidget extends WidgetType {
constructor(
readonly url: string,
readonly title: string,
readonly dim: string | null,
2023-07-30 05:41:37 +08:00
readonly client: Client,
) {
2022-08-23 14:12:24 +08:00
super();
2023-05-29 16:26:56 +08:00
// console.log("Creating widget", url);
2022-08-23 14:12:24 +08:00
}
eq(other: InlineImageWidget) {
return other.url === this.url && other.title === this.title &&
other.dim === this.dim;
2022-08-23 14:12:24 +08:00
}
2023-05-29 16:26:56 +08:00
get estimatedHeight(): number {
2024-01-02 18:32:57 +08:00
const cachedHeight = this.client.getCachedWidgetHeight(`image:${this.url}`);
2023-05-29 16:26:56 +08:00
// console.log("Estimated height requested", this.url, cachedHeight);
return cachedHeight;
}
private getDimensions(dimensionsToParse: string) {
const [, width, widthUnit = "px", height, heightUnit = "px"] =
2024-05-28 02:33:41 +08:00
dimensionsToParse.match(/(\d*)(\S*?x?)??[xX](\d*)(.*)?/) ?? [];
return { width, widthUnit, height, heightUnit };
}
2022-08-23 14:12:24 +08:00
toDOM() {
2022-08-29 21:47:16 +08:00
const img = document.createElement("img");
2023-05-29 16:26:56 +08:00
// console.log("Creating DOM", this.url);
2024-01-02 18:32:57 +08:00
const cachedImageHeight = this.client.getCachedWidgetHeight(
`image:${this.url}`,
);
2023-05-29 16:26:56 +08:00
img.onload = () => {
// console.log("Loaded", this.url, "with height", img.height);
if (img.height !== cachedImageHeight) {
2024-01-02 18:32:57 +08:00
this.client.setCachedWidgetHeight(`image:${this.url}`, img.height);
2023-05-29 16:26:56 +08:00
}
};
2024-05-28 02:33:41 +08:00
img.src = this.url;
img.alt = this.title;
img.title = this.title;
2022-08-29 21:47:16 +08:00
img.style.display = "block";
img.className = "sb-inline-img";
if (this.dim) {
const { width, widthUnit, height, heightUnit } = this.getDimensions(
this.dim,
);
img.style.height = height ? `${height}${heightUnit}` : "";
img.style.width = width ? `${width}${widthUnit}` : "";
} else if (cachedImageHeight > 0) {
2023-05-29 16:26:56 +08:00
img.height = cachedImageHeight;
}
2022-08-23 14:12:24 +08:00
return img;
}
}
2023-07-30 05:41:37 +08:00
export function inlineImagesPlugin(client: Client) {
return decoratorStateField((state: EditorState) => {
const widgets: Range<Decoration>[] = [];
2022-08-23 14:12:24 +08:00
syntaxTree(state).iterate({
2022-08-23 14:12:24 +08:00
enter: (node) => {
2024-05-28 02:33:41 +08:00
if (node.name !== "Image") {
2022-08-29 21:47:16 +08:00
return;
2022-08-23 14:12:24 +08:00
}
2022-08-29 21:47:16 +08:00
2024-05-28 02:33:41 +08:00
const text = state.sliceDoc(node.from, node.to);
let [url, alias, dim]: (string | null)[] = [null, null, null];
let match: RegExpExecArray | null;
if ((match = /!?\[([^\]]*)\]\((.+)\)/g.exec(text))) {
[/* fullMatch */, alias, url] = match;
} else if (
(match = /(!?\[\[)([^\]\|]+)(?:\|([^\]]+))?(\]\])/g.exec(text))
) {
[/* fullMatch */, /* firstMark */ , url, alias] = match;
url = "/" + url;
} else {
2022-08-23 14:12:24 +08:00
return;
}
2022-08-29 21:47:16 +08:00
2024-05-28 02:33:41 +08:00
if (alias) {
const dimReg = /\d*[^\|\s]*?[xX]\d*[^\|\s]*/.exec(alias);
if (dimReg) {
dim = dimReg[0];
alias = alias.replace(dim, "").replace("|", "");
}
} else {
alias = "";
}
2023-12-20 00:20:47 +08:00
2024-05-28 02:33:41 +08:00
if (isLocalPath(url)) {
url = resolvePath(client.currentPage, decodeURI(url), true);
}
2024-05-28 02:33:41 +08:00
widgets.push(
Decoration.widget({
2024-05-28 02:33:41 +08:00
widget: new InlineImageWidget(url, alias, dim, client),
block: true,
}).range(node.to),
);
2022-08-29 21:47:16 +08:00
},
2022-08-23 14:12:24 +08:00
});
return Decoration.set(widgets, true);
});
}