Fixes #902
parent
2eb7e25854
commit
104b6356df
|
@ -4,22 +4,25 @@ import { Decoration, WidgetType } from "@codemirror/view";
|
||||||
import { decoratorStateField } from "./util.ts";
|
import { decoratorStateField } from "./util.ts";
|
||||||
import type { Client } from "../client.ts";
|
import type { Client } from "../client.ts";
|
||||||
import { isLocalPath, resolvePath } from "$sb/lib/resolve.ts";
|
import { isLocalPath, resolvePath } from "$sb/lib/resolve.ts";
|
||||||
import { mdLinkRegex, wikiLinkRegex } from "$common/markdown_parser/parser.ts";
|
|
||||||
|
type ImageDimensions = {
|
||||||
|
width?: number;
|
||||||
|
height?: number;
|
||||||
|
};
|
||||||
|
|
||||||
class InlineImageWidget extends WidgetType {
|
class InlineImageWidget extends WidgetType {
|
||||||
constructor(
|
constructor(
|
||||||
readonly url: string,
|
readonly url: string,
|
||||||
readonly title: string,
|
readonly title: string,
|
||||||
readonly dim: string | null,
|
readonly dim: ImageDimensions | undefined,
|
||||||
readonly client: Client,
|
readonly client: Client,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
// console.log("Creating widget", url);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eq(other: InlineImageWidget) {
|
eq(other: InlineImageWidget) {
|
||||||
return other.url === this.url && other.title === this.title &&
|
return other.url === this.url && other.title === this.title &&
|
||||||
other.dim === this.dim;
|
JSON.stringify(other.dim) === JSON.stringify(this.dim);
|
||||||
}
|
}
|
||||||
|
|
||||||
get estimatedHeight(): number {
|
get estimatedHeight(): number {
|
||||||
|
@ -28,12 +31,6 @@ class InlineImageWidget extends WidgetType {
|
||||||
return cachedHeight;
|
return cachedHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getDimensions(dimensionsToParse: string) {
|
|
||||||
const [, width, widthUnit = "px", height, heightUnit = "px"] =
|
|
||||||
dimensionsToParse.match(/(\d*)(\S*?x?)??[xX](\d*)(.*)?/) ?? [];
|
|
||||||
return { width, widthUnit, height, heightUnit };
|
|
||||||
}
|
|
||||||
|
|
||||||
toDOM() {
|
toDOM() {
|
||||||
const img = document.createElement("img");
|
const img = document.createElement("img");
|
||||||
// console.log("Creating DOM", this.url);
|
// console.log("Creating DOM", this.url);
|
||||||
|
@ -52,11 +49,8 @@ class InlineImageWidget extends WidgetType {
|
||||||
img.style.display = "block";
|
img.style.display = "block";
|
||||||
img.className = "sb-inline-img";
|
img.className = "sb-inline-img";
|
||||||
if (this.dim) {
|
if (this.dim) {
|
||||||
const { width, widthUnit, height, heightUnit } = this.getDimensions(
|
img.style.height = this.dim.height ? `${this.dim.height}px` : "";
|
||||||
this.dim,
|
img.style.width = this.dim.width ? `${this.dim.width}px` : "";
|
||||||
);
|
|
||||||
img.style.height = height ? `${height}${heightUnit}` : "";
|
|
||||||
img.style.width = width ? `${width}${widthUnit}` : "";
|
|
||||||
} else if (cachedImageHeight > 0) {
|
} else if (cachedImageHeight > 0) {
|
||||||
img.height = cachedImageHeight;
|
img.height = cachedImageHeight;
|
||||||
}
|
}
|
||||||
|
@ -65,6 +59,40 @@ class InlineImageWidget extends WidgetType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 };
|
||||||
|
}
|
||||||
|
|
||||||
export function inlineImagesPlugin(client: Client) {
|
export function inlineImagesPlugin(client: Client) {
|
||||||
return decoratorStateField((state: EditorState) => {
|
return decoratorStateField((state: EditorState) => {
|
||||||
const widgets: Range<Decoration>[] = [];
|
const widgets: Range<Decoration>[] = [];
|
||||||
|
@ -76,7 +104,8 @@ export function inlineImagesPlugin(client: Client) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const text = state.sliceDoc(node.from, node.to);
|
const text = state.sliceDoc(node.from, node.to);
|
||||||
let [url, alias, dim]: (string | null)[] = [null, null, null];
|
let [url, alias]: (string | null)[] = [null, null];
|
||||||
|
let dim: ImageDimensions | undefined;
|
||||||
let match: RegExpExecArray | null;
|
let match: RegExpExecArray | null;
|
||||||
if ((match = /!?\[([^\]]*)\]\((.+)\)/g.exec(text))) {
|
if ((match = /!?\[([^\]]*)\]\((.+)\)/g.exec(text))) {
|
||||||
[/* fullMatch */, alias, url] = match;
|
[/* fullMatch */, alias, url] = match;
|
||||||
|
@ -90,11 +119,11 @@ export function inlineImagesPlugin(client: Client) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (alias) {
|
if (alias) {
|
||||||
const dimReg = /\d*[^\|\s]*?[xX]\d*[^\|\s]*/.exec(alias);
|
const { alias: parsedAlias, dim: parsedDim } = parseAlias(alias);
|
||||||
if (dimReg) {
|
if (parsedAlias) {
|
||||||
dim = dimReg[0];
|
alias = parsedAlias;
|
||||||
alias = alias.replace(dim, "").replace("|", "");
|
|
||||||
}
|
}
|
||||||
|
dim = parsedDim;
|
||||||
} else {
|
} else {
|
||||||
alias = "";
|
alias = "";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue