2022-08-29 13:47:16 +00:00
|
|
|
import {
|
|
|
|
Decoration,
|
2022-12-09 15:09:53 +00:00
|
|
|
EditorState,
|
2022-11-18 15:04:37 +00:00
|
|
|
Range,
|
|
|
|
syntaxTree,
|
2022-08-29 13:47:16 +00:00
|
|
|
WidgetType,
|
2022-11-18 15:04:37 +00:00
|
|
|
} from "../deps.ts";
|
2022-12-09 15:09:53 +00:00
|
|
|
import { decoratorStateField } from "./util.ts";
|
2022-08-23 06:12:24 +00:00
|
|
|
|
2023-07-14 14:56:20 +00:00
|
|
|
import type { Client } from "../client.ts";
|
2023-07-29 21:41:37 +00:00
|
|
|
import { resolvePath } from "$sb/lib/resolve.ts";
|
2023-12-19 16:20:47 +00:00
|
|
|
import { folderName, resolve } from "../../common/path.ts";
|
2023-01-08 11:24:12 +00:00
|
|
|
|
2022-08-23 06:12:24 +00:00
|
|
|
class InlineImageWidget extends WidgetType {
|
2023-01-08 11:24:12 +00:00
|
|
|
constructor(
|
|
|
|
readonly url: string,
|
|
|
|
readonly title: string,
|
2023-07-29 21:41:37 +00:00
|
|
|
readonly client: Client,
|
2023-01-08 11:24:12 +00:00
|
|
|
) {
|
2022-08-23 06:12:24 +00:00
|
|
|
super();
|
2023-05-29 08:26:56 +00:00
|
|
|
// console.log("Creating widget", url);
|
2022-08-23 06:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eq(other: InlineImageWidget) {
|
2022-08-23 16:53:05 +00:00
|
|
|
return other.url === this.url && other.title === this.title;
|
2022-08-23 06:12:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 08:26:56 +00:00
|
|
|
get estimatedHeight(): number {
|
2023-07-29 21:41:37 +00:00
|
|
|
const cachedHeight = this.client.space.getCachedImageHeight(this.url);
|
2023-05-29 08:26:56 +00:00
|
|
|
// console.log("Estimated height requested", this.url, cachedHeight);
|
|
|
|
return cachedHeight;
|
|
|
|
}
|
|
|
|
|
2022-08-23 06:12:24 +00:00
|
|
|
toDOM() {
|
2022-08-29 13:47:16 +00:00
|
|
|
const img = document.createElement("img");
|
2023-07-29 21:41:37 +00:00
|
|
|
let url = this.url;
|
|
|
|
url = resolvePath(this.client.currentPage!, url, true);
|
2023-05-29 08:26:56 +00:00
|
|
|
// console.log("Creating DOM", this.url);
|
2023-07-29 21:41:37 +00:00
|
|
|
const cachedImageHeight = this.client.space.getCachedImageHeight(url);
|
2023-05-29 08:26:56 +00:00
|
|
|
img.onload = () => {
|
|
|
|
// console.log("Loaded", this.url, "with height", img.height);
|
|
|
|
if (img.height !== cachedImageHeight) {
|
2023-07-29 21:41:37 +00:00
|
|
|
this.client.space.setCachedImageHeight(url, img.height);
|
2023-05-29 08:26:56 +00:00
|
|
|
}
|
|
|
|
};
|
2023-07-29 21:41:37 +00:00
|
|
|
img.src = url;
|
2022-08-23 16:53:05 +00:00
|
|
|
img.alt = this.title;
|
|
|
|
img.title = this.title;
|
2022-08-29 13:47:16 +00:00
|
|
|
img.style.display = "block";
|
|
|
|
img.className = "sb-inline-img";
|
2023-05-29 08:26:56 +00:00
|
|
|
if (cachedImageHeight > 0) {
|
|
|
|
img.height = cachedImageHeight;
|
|
|
|
}
|
2022-08-23 06:12:24 +00:00
|
|
|
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 21:41:37 +00:00
|
|
|
export function inlineImagesPlugin(client: Client) {
|
2022-12-09 15:09:53 +00:00
|
|
|
return decoratorStateField((state: EditorState) => {
|
|
|
|
const widgets: Range<Decoration>[] = [];
|
|
|
|
const imageRegex = /!\[(?<title>[^\]]*)\]\((?<url>.+)\)/;
|
2022-08-23 06:12:24 +00:00
|
|
|
|
2022-12-09 15:09:53 +00:00
|
|
|
syntaxTree(state).iterate({
|
2022-08-23 06:12:24 +00:00
|
|
|
enter: (node) => {
|
2022-08-29 13:47:16 +00:00
|
|
|
if (node.name !== "Image") {
|
|
|
|
return;
|
2022-08-23 06:12:24 +00:00
|
|
|
}
|
2022-08-29 13:47:16 +00:00
|
|
|
|
|
|
|
const imageRexexResult = imageRegex.exec(
|
2022-12-09 15:09:53 +00:00
|
|
|
state.sliceDoc(node.from, node.to),
|
2022-08-29 13:47:16 +00:00
|
|
|
);
|
2022-08-23 06:12:24 +00:00
|
|
|
if (imageRexexResult === null || !imageRexexResult.groups) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-29 13:47:16 +00:00
|
|
|
|
2023-07-02 09:25:32 +00:00
|
|
|
let url = imageRexexResult.groups.url;
|
2022-08-23 16:53:05 +00:00
|
|
|
const title = imageRexexResult.groups.title;
|
2023-12-19 16:20:47 +00:00
|
|
|
|
|
|
|
const currentPage = client.currentPage!;
|
|
|
|
const currentFolder = folderName(currentPage);
|
|
|
|
|
|
|
|
if (url.indexOf("://") === -1 && !url.startsWith("/")) {
|
|
|
|
url = resolve(currentFolder, decodeURI(url));
|
2023-07-02 09:25:32 +00:00
|
|
|
}
|
2022-11-18 15:04:37 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
2023-07-29 21:41:37 +00:00
|
|
|
widget: new InlineImageWidget(url, title, client),
|
2023-02-28 09:07:20 +00:00
|
|
|
block: true,
|
2022-11-18 15:04:37 +00:00
|
|
|
}).range(node.to),
|
|
|
|
);
|
2022-08-29 13:47:16 +00:00
|
|
|
},
|
2022-08-23 06:12:24 +00:00
|
|
|
});
|
|
|
|
|
2022-12-09 15:09:53 +00:00
|
|
|
return Decoration.set(widgets, true);
|
|
|
|
});
|
|
|
|
}
|