silverbullet/web/cm_plugins/inline_content.ts

263 lines
7.1 KiB
TypeScript
Raw Permalink Normal View History

2024-07-30 23:33:33 +08:00
import type { EditorState, Range } from "@codemirror/state";
import { syntaxTree } from "@codemirror/language";
import { Decoration, WidgetType } from "@codemirror/view";
import { MarkdownWidget } from "./markdown_widget.ts";
import {
decoratorStateField,
invisibleDecoration,
isCursorInRange,
shouldRenderWidgets,
} from "./util.ts";
2023-07-14 22:56:20 +08:00
import type { Client } from "../client.ts";
import {
isFederationPath,
isLocalPath,
resolvePath,
} from "@silverbulletmd/silverbullet/lib/resolve";
import { parsePageRef } from "@silverbulletmd/silverbullet/lib/page_ref";
2024-08-02 22:30:39 +08:00
import { mime } from "mimetypes";
2024-07-06 21:45:04 +08:00
2024-08-02 22:30:39 +08:00
type ContentDimensions = {
2024-07-06 21:45:04 +08:00
width?: number;
height?: number;
};
class InlineContentWidget extends WidgetType {
constructor(
readonly url: string,
readonly title: string,
2024-08-02 22:30:39 +08:00
readonly dim: ContentDimensions | undefined,
2023-07-30 05:41:37 +08:00
readonly client: Client,
) {
2022-08-23 14:12:24 +08:00
super();
}
2024-10-10 18:52:28 +08:00
override 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
}
2024-10-10 18:52:28 +08:00
override get estimatedHeight(): number {
2024-08-02 22:30:39 +08:00
const cachedHeight = this.client.getCachedWidgetHeight(
`content:${this.url}`,
);
2023-05-29 16:26:56 +08:00
return cachedHeight;
}
2022-08-23 14:12:24 +08:00
toDOM() {
2024-08-02 22:30:39 +08:00
const div = document.createElement("div");
div.className = "sb-inline-content";
div.style.display = "block";
const mimeType = mime.getType(
this.url.substring(this.url.lastIndexOf(".") + 1),
);
if (!mimeType) {
return div;
}
const url = encodeURIComponent(this.url)
2024-08-02 22:30:39 +08:00
if (mimeType.startsWith("image/")) {
const img = document.createElement("img");
img.src = url;
2024-08-02 22:30:39 +08:00
img.alt = this.title;
this.setDim(img, "load");
div.appendChild(img);
} else if (mimeType.startsWith("video/")) {
const video = document.createElement("video");
video.src = url;
2024-08-02 22:30:39 +08:00
video.title = this.title;
video.controls = true;
video.autoplay = false;
this.setDim(video, "loadeddata");
div.appendChild(video);
} else if (mimeType.startsWith("audio/")) {
const audio = document.createElement("audio");
audio.src = url;
2024-08-02 22:30:39 +08:00
audio.title = this.title;
audio.controls = true;
audio.autoplay = false;
this.setDim(audio, "loadeddata");
div.appendChild(audio);
} else if (mimeType === "application/pdf") {
const embed = document.createElement("object");
embed.type = mimeType;
embed.data = url;
2024-08-02 22:30:39 +08:00
embed.style.width = "100%";
embed.style.height = "20em";
this.setDim(embed, "load");
div.appendChild(embed);
}
return div;
}
setDim(el: HTMLElement, event: string) {
const cachedContentHeight = this.client.getCachedWidgetHeight(
`content:${this.url}`,
2024-01-02 18:32:57 +08:00
);
2024-08-02 22:30:39 +08:00
el.addEventListener(event, () => {
if (el.clientHeight !== cachedContentHeight) {
this.client.setCachedWidgetHeight(
`content:${this.url}`,
el.clientHeight,
);
2023-05-29 16:26:56 +08:00
}
2024-08-02 22:30:39 +08:00
});
el.style.maxWidth = "100%";
if (this.dim) {
2024-08-02 22:30:39 +08:00
if (this.dim.height) {
el.style.height = `${this.dim.height}px`;
}
if (this.dim.width) {
el.style.width = `${this.dim.width}px`;
}
} else if (cachedContentHeight > 0) {
el.style.height = cachedContentHeight.toString();
2023-05-29 16:26:56 +08:00
}
2022-08-23 14:12:24 +08:00
}
}
2024-08-02 22:30:39 +08:00
// Parse an alias, possibly containing dimensions into an object
2024-07-06 21:45:04 +08:00
// Formats supported: "alias", "alias|100", "alias|100x200", "100", "100x200"
function parseAlias(
text: string,
2024-08-02 22:30:39 +08:00
): { alias?: string; dim?: ContentDimensions } {
2024-07-06 21:45:04 +08:00
let alias: string | undefined;
2024-08-02 22:30:39 +08:00
let dim: ContentDimensions | undefined;
2024-07-06 21:45:04 +08:00
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 };
}
2024-08-02 22:30:39 +08:00
export function inlineContentPlugin(client: Client) {
return decoratorStateField((state: EditorState) => {
const widgets: Range<Decoration>[] = [];
if (!shouldRenderWidgets(client)) {
2024-08-15 20:36:12 +08:00
console.info("Not rendering widgets");
return Decoration.set([]);
}
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]: (string | undefined)[] = [undefined, 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;
}
}
if (!url) {
2022-08-23 14:12:24 +08:00
return;
}
2022-08-29 21:47:16 +08:00
let dim: ContentDimensions | undefined;
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("transclusion");
if (!codeWidgetCallback) {
return;
}
widgets.push(
Decoration.widget({
widget: new MarkdownWidget(
node.from,
client,
`widget:${client.currentPage}:${text}`,
text,
codeWidgetCallback,
"sb-markdown-widget sb-markdown-widget-inline",
),
block: true,
2024-08-15 20:36:12 +08:00
}).range(node.to),
);
2024-08-15 20:36:12 +08:00
if (!isCursorInRange(state, [node.from, node.to])) {
widgets.push(invisibleDecoration.range(node.from, 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),
);
if (!isCursorInRange(state, [node.from, node.to])) {
widgets.push(invisibleDecoration.range(node.from, node.to));
}
2022-08-29 21:47:16 +08:00
},
2022-08-23 14:12:24 +08:00
});
return Decoration.set(widgets, true);
});
}