silverbullet/web/cm_plugins/inline_content.ts

184 lines
5.3 KiB
TypeScript
Raw Normal View History

import { EditorState, Range } from "@codemirror/state";
import { syntaxTree } from "@codemirror/language";
import { Decoration, WidgetType } from "@codemirror/view";
import { MarkdownWidget } from "./markdown_widget.ts";
import { decoratorStateField } from "./util.ts";
2023-07-14 22:56:20 +08:00
import type { Client } from "../client.ts";
import { isFederationPath, isLocalPath, resolvePath } from "$sb/lib/resolve.ts";
import { parsePageRef } from "$sb/lib/page_ref.ts";
2024-07-06 21:45:04 +08:00
type ImageDimensions = {
width?: number;
height?: number;
};
class InlineContentWidget extends WidgetType {
constructor(
readonly url: string,
readonly title: string,
2024-07-06 21:45:04 +08:00
readonly dim: ImageDimensions | undefined,
2023-07-30 05:41:37 +08:00
readonly client: Client,
) {
2022-08-23 14:12:24 +08:00
super();
}
eq(other: InlineContentWidget) {
return other.url === this.url && other.title === this.title &&
2024-07-06 21:45:04 +08:00
JSON.stringify(other.dim) === JSON.stringify(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
return cachedHeight;
}
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) {
2024-07-06 21:45:04 +08:00
img.style.height = this.dim.height ? `${this.dim.height}px` : "";
img.style.width = this.dim.width ? `${this.dim.width}px` : "";
} 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;
}
}
2024-07-06 21:45:04 +08:00
// Parse an alias, possibly containing image dimensions into an object
// Formats supported: "alias", "alias|100", "alias|100x200", "100", "100x200"
function parseAlias(
text: string,
): { alias?: string; dim?: ImageDimensions } {
let alias: string | undefined;
let dim: ImageDimensions | undefined;
if (text.includes("|")) {
const [aliasPart, dimPart] = text.split("|");
alias = aliasPart;
const [width, height] = dimPart.split("x");
dim = {};
if (width) {
dim.width = parseInt(width);
}
if (height) {
dim.height = parseInt(height);
}
} else if (/^[x\d]/.test(text)) {
const [width, height] = text.split("x");
dim = {};
if (width) {
dim.width = parseInt(width);
}
if (height) {
dim.height = parseInt(height);
}
} else {
alias = text;
}
return { alias, dim };
}
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);
2024-07-06 21:45:04 +08:00
let [url, alias]: (string | null)[] = [null, null];
let dim: ImageDimensions | undefined;
2024-05-28 02:33:41 +08:00
let match: RegExpExecArray | null;
if ((match = /!?\[([^\]]*)\]\((.+)\)/g.exec(text))) {
[/* fullMatch */, alias, url] = match;
} else if (
(match = /(!?\[\[)([^\]\|]+)(?:\|([^\]]+))?(\]\])/g.exec(text))
) {
[/* fullMatch */, /* firstMark */ , url, alias] = match;
if (!isFederationPath(url)) {
url = "/" + url;
}
2024-05-28 02:33:41 +08:00
} 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) {
2024-07-06 21:45:04 +08:00
const { alias: parsedAlias, dim: parsedDim } = parseAlias(alias);
if (parsedAlias) {
alias = parsedAlias;
2024-05-28 02:33:41 +08:00
}
2024-07-06 21:45:04 +08:00
dim = parsedDim;
2024-05-28 02:33:41 +08:00
} 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);
const pageRef = parsePageRef(url);
if (
isFederationPath(pageRef.page) ||
client.clientSystem.allKnownFiles.has(pageRef.page + ".md")
) {
// This is a page reference, let's inline the content
const codeWidgetCallback = client.clientSystem.codeWidgetHook
.codeWidgetCallbacks.get("template");
if (!codeWidgetCallback) {
return;
}
widgets.push(
Decoration.line({
class: "sb-fenced-code-iframe",
}).range(node.to),
);
widgets.push(
Decoration.widget({
widget: new MarkdownWidget(
node.from,
client,
`widget:${client.currentPage}:${text}`,
2024-07-23 20:53:36 +08:00
`{{rewriteRefsAndFederationLinks([[${url}]], "${url}")}}`,
codeWidgetCallback,
"sb-markdown-widget sb-markdown-widget-inline",
),
block: true,
}).range(node.to),
);
return;
}
}
2024-05-28 02:33:41 +08:00
widgets.push(
Decoration.widget({
widget: new InlineContentWidget(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);
});
}