2022-08-29 13:47:16 +00:00
|
|
|
import {
|
|
|
|
Decoration,
|
|
|
|
DecorationSet,
|
|
|
|
EditorView,
|
2022-11-18 15:04:37 +00:00
|
|
|
Range,
|
|
|
|
syntaxTree,
|
2022-08-29 13:47:16 +00:00
|
|
|
ViewPlugin,
|
|
|
|
ViewUpdate,
|
|
|
|
WidgetType,
|
2022-11-18 15:04:37 +00:00
|
|
|
} from "../deps.ts";
|
|
|
|
import { invisibleDecoration, isCursorInRange } from "./util.ts";
|
2022-08-23 06:12:24 +00:00
|
|
|
|
|
|
|
class InlineImageWidget extends WidgetType {
|
2022-08-23 16:53:05 +00:00
|
|
|
constructor(readonly url: string, readonly title: string) {
|
2022-08-23 06:12:24 +00:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
toDOM() {
|
2022-08-29 13:47:16 +00:00
|
|
|
const img = document.createElement("img");
|
2022-09-06 12:36:06 +00:00
|
|
|
if (this.url.startsWith("http")) {
|
|
|
|
img.src = this.url;
|
|
|
|
} else {
|
2022-09-12 12:50:37 +00:00
|
|
|
img.src = `fs/${this.url}`;
|
2022-09-06 12:36:06 +00:00
|
|
|
}
|
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";
|
2022-08-23 06:12:24 +00:00
|
|
|
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const inlineImages = (view: EditorView) => {
|
2022-10-15 17:02:56 +00:00
|
|
|
const widgets: Range<Decoration>[] = [];
|
2022-08-23 16:53:05 +00:00
|
|
|
const imageRegex = /!\[(?<title>[^\]]*)\]\((?<url>.+)\)/;
|
2022-08-23 06:12:24 +00:00
|
|
|
|
2022-10-15 17:02:56 +00:00
|
|
|
for (const { from, to } of view.visibleRanges) {
|
2022-08-23 06:12:24 +00:00
|
|
|
syntaxTree(view.state).iterate({
|
2022-08-29 13:47:16 +00:00
|
|
|
from,
|
|
|
|
to,
|
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-10-10 12:50:21 +00:00
|
|
|
view.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
|
|
|
|
2022-08-23 06:12:24 +00:00
|
|
|
const url = imageRexexResult.groups.url;
|
2022-08-23 16:53:05 +00:00
|
|
|
const title = imageRexexResult.groups.title;
|
2022-11-18 15:04:37 +00:00
|
|
|
widgets.push(
|
|
|
|
Decoration.widget({
|
|
|
|
widget: new InlineImageWidget(url, title),
|
|
|
|
}).range(node.to),
|
|
|
|
);
|
2022-08-29 13:47:16 +00:00
|
|
|
},
|
2022-08-23 06:12:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Decoration.set(widgets, true);
|
2022-08-29 13:47:16 +00:00
|
|
|
};
|
2022-08-23 06:12:24 +00:00
|
|
|
|
|
|
|
export const inlineImagesPlugin = () =>
|
|
|
|
ViewPlugin.fromClass(
|
|
|
|
class {
|
|
|
|
decorations: DecorationSet;
|
|
|
|
|
|
|
|
constructor(view: EditorView) {
|
|
|
|
this.decorations = inlineImages(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
update(update: ViewUpdate) {
|
|
|
|
if (update.docChanged) {
|
|
|
|
this.decorations = inlineImages(update.view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decorations: (v) => v.decorations,
|
2022-10-10 12:50:21 +00:00
|
|
|
},
|
2022-08-23 06:12:24 +00:00
|
|
|
);
|