2024-07-30 23:33:33 +08:00
|
|
|
import type { EditorState, Range } from "@codemirror/state";
|
2024-03-16 22:29:24 +08:00
|
|
|
import { syntaxTree } from "@codemirror/language";
|
|
|
|
import { Decoration, WidgetType } from "@codemirror/view";
|
2024-07-09 15:22:08 +08:00
|
|
|
import { MarkdownWidget } from "./markdown_widget.ts";
|
2024-08-13 02:12:28 +08:00
|
|
|
import {
|
|
|
|
decoratorStateField,
|
|
|
|
invisibleDecoration,
|
|
|
|
isCursorInRange,
|
|
|
|
shouldRenderWidgets,
|
|
|
|
} from "./util.ts";
|
2023-07-14 22:56:20 +08:00
|
|
|
import type { Client } from "../client.ts";
|
2024-08-07 02:11:38 +08:00
|
|
|
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;
|
|
|
|
};
|
2023-01-08 19:24:12 +08:00
|
|
|
|
2024-07-09 15:22:08 +08:00
|
|
|
class InlineContentWidget extends WidgetType {
|
2023-01-08 19:24:12 +08:00
|
|
|
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,
|
2023-01-08 19:24:12 +08:00
|
|
|
) {
|
2022-08-23 14:12:24 +08:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2024-10-10 18:52:28 +08:00
|
|
|
override eq(other: InlineContentWidget) {
|
2024-05-19 17:05:48 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2025-01-08 02:56:45 +08:00
|
|
|
const url = encodeURIComponent(this.url)
|
|
|
|
|
2024-08-02 22:30:39 +08:00
|
|
|
if (mimeType.startsWith("image/")) {
|
|
|
|
const img = document.createElement("img");
|
2025-01-08 02:56:45 +08:00
|
|
|
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");
|
2025-01-08 02:56:45 +08:00
|
|
|
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");
|
2025-01-08 02:56:45 +08:00
|
|
|
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;
|
2025-01-08 02:56:45 +08:00
|
|
|
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%";
|
|
|
|
|
2024-05-19 17:05:48 +08:00
|
|
|
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) {
|
2022-12-09 23:09:53 +08:00
|
|
|
return decoratorStateField((state: EditorState) => {
|
|
|
|
const widgets: Range<Decoration>[] = [];
|
2024-08-13 02:12:28 +08:00
|
|
|
if (!shouldRenderWidgets(client)) {
|
2024-08-15 20:36:12 +08:00
|
|
|
console.info("Not rendering widgets");
|
2024-08-13 02:12:28 +08:00
|
|
|
return Decoration.set([]);
|
|
|
|
}
|
2022-08-23 14:12:24 +08:00
|
|
|
|
2022-12-09 23:09:53 +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-08-09 02:19:41 +08:00
|
|
|
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;
|
2024-07-09 15:22:08 +08:00
|
|
|
if (!isFederationPath(url)) {
|
|
|
|
url = "/" + url;
|
|
|
|
}
|
2024-08-09 02:19:41 +08:00
|
|
|
}
|
|
|
|
if (!url) {
|
2022-08-23 14:12:24 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-08-29 21:47:16 +08:00
|
|
|
|
2024-08-09 02:19:41 +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)) {
|
2024-08-09 02:19:41 +08:00
|
|
|
url = resolvePath(
|
|
|
|
client.currentPage,
|
|
|
|
decodeURI(url),
|
|
|
|
true,
|
|
|
|
);
|
2024-07-09 15:22:08 +08:00
|
|
|
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
|
2024-08-09 02:19:41 +08:00
|
|
|
.codeWidgetCallbacks.get("transclusion");
|
2024-07-09 15:22:08 +08:00
|
|
|
|
|
|
|
if (!codeWidgetCallback) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
|
|
|
widget: new MarkdownWidget(
|
|
|
|
node.from,
|
|
|
|
client,
|
|
|
|
`widget:${client.currentPage}:${text}`,
|
2024-08-09 02:19:41 +08:00
|
|
|
text,
|
2024-07-09 15:22:08 +08:00
|
|
|
codeWidgetCallback,
|
|
|
|
"sb-markdown-widget sb-markdown-widget-inline",
|
|
|
|
),
|
|
|
|
block: true,
|
2024-08-15 20:36:12 +08:00
|
|
|
}).range(node.to),
|
2024-07-09 15:22:08 +08:00
|
|
|
);
|
2024-08-15 20:36:12 +08:00
|
|
|
|
|
|
|
if (!isCursorInRange(state, [node.from, node.to])) {
|
|
|
|
widgets.push(invisibleDecoration.range(node.from, node.to));
|
|
|
|
}
|
|
|
|
|
2024-07-09 15:22:08 +08:00
|
|
|
return;
|
|
|
|
}
|
2023-07-02 17:25:32 +08:00
|
|
|
}
|
2024-05-28 02:33:41 +08:00
|
|
|
|
2022-11-18 23:04:37 +08:00
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
2024-08-09 02:19:41 +08:00
|
|
|
widget: new InlineContentWidget(
|
|
|
|
url,
|
|
|
|
alias,
|
|
|
|
dim,
|
|
|
|
client,
|
|
|
|
),
|
2023-02-28 17:07:20 +08:00
|
|
|
block: true,
|
2024-08-13 02:12:28 +08:00
|
|
|
}).range(node.to),
|
2022-11-18 23:04:37 +08:00
|
|
|
);
|
2024-08-13 02:12:28 +08:00
|
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2022-12-09 23:09:53 +08:00
|
|
|
return Decoration.set(widgets, true);
|
|
|
|
});
|
|
|
|
}
|