@usememos/editor 26.10.7 → 26.10.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -4
- package/dist/base.css +43 -0
- package/dist/index.js +426 -22
- package/dist/index.js.map +4 -4
- package/dist/style.css +85 -0
- package/dist/theme.css +16 -0
- package/dist/types/commands.d.ts +16 -1
- package/dist/types/editor.d.ts +17 -2
- package/dist/types/icons.d.ts +7 -0
- package/dist/types/labels.d.ts +4 -0
- package/dist/types/markdown/ast.d.ts +13 -0
- package/dist/types/markdown/inline.d.ts +1 -1
- package/dist/types/model.d.ts +7 -0
- package/dist/types/ui/image.d.ts +33 -0
- package/dist/types/view/dom.d.ts +6 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -39,7 +39,9 @@ __export(commands_exports, {
|
|
|
39
39
|
previousGrapheme: () => previousGrapheme,
|
|
40
40
|
replaceBlocks: () => replaceBlocks,
|
|
41
41
|
selectAll: () => selectAll,
|
|
42
|
+
selectImage: () => selectImage,
|
|
42
43
|
selectedBlocks: () => selectedBlocks,
|
|
44
|
+
selectedImage: () => selectedImage,
|
|
43
45
|
setBlockType: () => setBlockType,
|
|
44
46
|
setCodeLanguage: () => setCodeLanguage,
|
|
45
47
|
setLink: () => setLink,
|
|
@@ -48,7 +50,8 @@ __export(commands_exports, {
|
|
|
48
50
|
tableAction: () => tableAction,
|
|
49
51
|
toggleBlockType: () => toggleBlockType,
|
|
50
52
|
toggleMark: () => toggleMark,
|
|
51
|
-
toggleTask: () => toggleTask
|
|
53
|
+
toggleTask: () => toggleTask,
|
|
54
|
+
updateImage: () => updateImage
|
|
52
55
|
});
|
|
53
56
|
|
|
54
57
|
// src/model.ts
|
|
@@ -794,6 +797,48 @@ function setLink(state, href) {
|
|
|
794
797
|
const blocks = mapTextInRange(state, from, to, (content, a, b) => setLinkInRange(content, a, b, href));
|
|
795
798
|
return { ...state, doc: { blocks }, storedMarks: null };
|
|
796
799
|
}
|
|
800
|
+
function selectedImage(state) {
|
|
801
|
+
const { anchor, focus } = state.selection;
|
|
802
|
+
if (anchor.blockId !== focus.blockId || !sameCell(anchor.cell, focus.cell)) return null;
|
|
803
|
+
if (Math.abs(anchor.offset - focus.offset) !== 1) return null;
|
|
804
|
+
const from = Math.min(anchor.offset, focus.offset);
|
|
805
|
+
const content = containerContent(getBlock(state.doc, anchor.blockId), anchor.cell);
|
|
806
|
+
const image = content ? sliceInline(content, from, from + 1)[0]?.image : void 0;
|
|
807
|
+
return image ? { pos: { ...anchor, offset: from }, image } : null;
|
|
808
|
+
}
|
|
809
|
+
function selectImage(state, pos) {
|
|
810
|
+
const block3 = state.doc.blocks.find((b) => b.id === pos.blockId);
|
|
811
|
+
const content = block3 && containerContent(block3, pos.cell);
|
|
812
|
+
if (!content || !sliceInline(content, pos.offset, pos.offset + 1)[0]?.image) return state;
|
|
813
|
+
return { ...state, selection: { anchor: pos, focus: { ...pos, offset: pos.offset + 1 } }, storedMarks: null };
|
|
814
|
+
}
|
|
815
|
+
function updateImage(state, pos, change) {
|
|
816
|
+
const block3 = state.doc.blocks.find((b) => b.id === pos.blockId);
|
|
817
|
+
const content = block3 && containerContent(block3, pos.cell);
|
|
818
|
+
const span = content ? sliceInline(content, pos.offset, pos.offset + 1)[0] : void 0;
|
|
819
|
+
if (!block3 || !content || !span?.image) return state;
|
|
820
|
+
const { title: _title, width: _width, height: _height, ...rest } = span.image;
|
|
821
|
+
let title = span.image.title;
|
|
822
|
+
if (change.title !== void 0) title = change.title || void 0;
|
|
823
|
+
let width = span.image.width;
|
|
824
|
+
let height = span.image.height;
|
|
825
|
+
if (change.width !== void 0) {
|
|
826
|
+
width = change.width === null ? void 0 : Math.max(1, Math.min(99999, Math.round(change.width)));
|
|
827
|
+
if (width !== span.image.width) height = void 0;
|
|
828
|
+
}
|
|
829
|
+
const image = {
|
|
830
|
+
...rest,
|
|
831
|
+
...change.alt !== void 0 ? { alt: change.alt } : {},
|
|
832
|
+
...title ? { title } : {},
|
|
833
|
+
...width ? { width } : {},
|
|
834
|
+
...width && height ? { height } : {}
|
|
835
|
+
};
|
|
836
|
+
const selected = selectImage(state, pos).selection;
|
|
837
|
+
const same = image.alt === span.image.alt && image.title === span.image.title && image.width === span.image.width && image.height === span.image.height;
|
|
838
|
+
if (same) return selected === state.selection ? state : { ...state, selection: selected, storedMarks: null };
|
|
839
|
+
const next = concatInline(sliceInline(content, 0, pos.offset), [{ ...span, text: ATOM_CHAR, image }], sliceInline(content, pos.offset + 1));
|
|
840
|
+
return replaceBlock(state, withContainerContent(block3, pos.cell, next), selected);
|
|
841
|
+
}
|
|
797
842
|
function convert(block3, spec) {
|
|
798
843
|
const content = block3.content;
|
|
799
844
|
switch (spec.type) {
|
|
@@ -1474,8 +1519,10 @@ function inlineHtml(inline3) {
|
|
|
1474
1519
|
function spanHtml(span) {
|
|
1475
1520
|
let html;
|
|
1476
1521
|
if (span.image) {
|
|
1477
|
-
const { src, alt, title } = span.image;
|
|
1478
|
-
html = `<img src="${escapeHtml(src)}" alt="${escapeHtml(alt)}"${title ? ` title="${escapeHtml(title)}"` : ""}
|
|
1522
|
+
const { src, alt, title, width, height } = span.image;
|
|
1523
|
+
html = `<img src="${escapeHtml(src)}" alt="${escapeHtml(alt)}"${title ? ` title="${escapeHtml(title)}"` : ""}`;
|
|
1524
|
+
if (width) html += ` width="${width}"${height ? ` height="${height}"` : ""}`;
|
|
1525
|
+
html += ">";
|
|
1479
1526
|
} else if (span.marks.includes("html")) {
|
|
1480
1527
|
html = span.text;
|
|
1481
1528
|
} else {
|
|
@@ -1730,7 +1777,20 @@ var Converter = class _Converter {
|
|
|
1730
1777
|
if (!src || /^(?:javascript|vbscript):/i.test(src)) return;
|
|
1731
1778
|
const alt = el.getAttribute("alt") ?? "";
|
|
1732
1779
|
const title = el.getAttribute("title");
|
|
1733
|
-
|
|
1780
|
+
const size = (name) => {
|
|
1781
|
+
const value = el.getAttribute(name)?.trim() ?? "";
|
|
1782
|
+
return /^[1-9][0-9]{0,4}$/.test(value) ? Number(value) : 0;
|
|
1783
|
+
};
|
|
1784
|
+
const width = size("width");
|
|
1785
|
+
const height = width ? size("height") : 0;
|
|
1786
|
+
const image = {
|
|
1787
|
+
src,
|
|
1788
|
+
alt,
|
|
1789
|
+
...title ? { title } : {},
|
|
1790
|
+
...width ? { width } : {},
|
|
1791
|
+
...height ? { height } : {}
|
|
1792
|
+
};
|
|
1793
|
+
this.push(ATOM_CHAR, format, { image });
|
|
1734
1794
|
return;
|
|
1735
1795
|
}
|
|
1736
1796
|
const block3 = BLOCKS.has(tag);
|
|
@@ -2011,7 +2071,11 @@ var DEFAULT_LABELS = {
|
|
|
2011
2071
|
alignLeft: "Align left",
|
|
2012
2072
|
alignCenter: "Align center",
|
|
2013
2073
|
alignRight: "Align right",
|
|
2014
|
-
tableSection: "Table"
|
|
2074
|
+
tableSection: "Table",
|
|
2075
|
+
/** Shown in place of an image that did not load and has no alt text. */
|
|
2076
|
+
imageUnavailable: "Image unavailable",
|
|
2077
|
+
/** Accessible name of the handles that resize an image. */
|
|
2078
|
+
resizeImage: "Resize image"
|
|
2015
2079
|
};
|
|
2016
2080
|
|
|
2017
2081
|
// src/icons.ts
|
|
@@ -2075,6 +2139,15 @@ var ICONS = {
|
|
|
2075
2139
|
inlineCode: ["m16 18 6-6-6-6", "m8 6-6 6 6 6"],
|
|
2076
2140
|
/** `link` */
|
|
2077
2141
|
link: ["M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71", "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"],
|
|
2142
|
+
/** `image-off` */
|
|
2143
|
+
imageUnavailable: [
|
|
2144
|
+
["line", { x1: "2", x2: "22", y1: "2", y2: "22" }],
|
|
2145
|
+
"M10.41 10.41a2 2 0 1 1-2.83-2.83",
|
|
2146
|
+
["line", { x1: "13.5", x2: "6", y1: "13.5", y2: "21" }],
|
|
2147
|
+
["line", { x1: "18", x2: "21", y1: "12", y2: "15" }],
|
|
2148
|
+
"M3.59 3.59A1.99 1.99 0 0 0 3 5v14a2 2 0 0 0 2 2h14c.55 0 1.052-.22 1.41-.59",
|
|
2149
|
+
"M21 15V5a2 2 0 0 0-2-2H9"
|
|
2150
|
+
],
|
|
2078
2151
|
/** `grip-vertical` */
|
|
2079
2152
|
blockHandle: [["circle", { cx: "9", cy: "12", r: "1" }], ["circle", { cx: "9", cy: "5", r: "1" }], ["circle", { cx: "9", cy: "19", r: "1" }], ["circle", { cx: "15", cy: "12", r: "1" }], ["circle", { cx: "15", cy: "5", r: "1" }], ["circle", { cx: "15", cy: "19", r: "1" }]]
|
|
2080
2153
|
};
|
|
@@ -2106,10 +2179,8 @@ function iconNode(doc, icon) {
|
|
|
2106
2179
|
return typeof icon === "string" ? doc.createTextNode(icon) : doc.importNode(icon, true);
|
|
2107
2180
|
}
|
|
2108
2181
|
|
|
2109
|
-
// src/markdown/inline.ts
|
|
2110
|
-
import { lookupEntity } from "#entities";
|
|
2111
|
-
|
|
2112
2182
|
// src/markdown/ast.ts
|
|
2183
|
+
var IMAGE_SIZE = /\|([1-9][0-9]{0,4})(?:x([1-9][0-9]{0,4}))?$/;
|
|
2113
2184
|
function inlineNodesText(nodes, soft) {
|
|
2114
2185
|
let s = "";
|
|
2115
2186
|
for (const node of nodes) {
|
|
@@ -2133,6 +2204,9 @@ function inlineNodesText(nodes, soft) {
|
|
|
2133
2204
|
return s;
|
|
2134
2205
|
}
|
|
2135
2206
|
|
|
2207
|
+
// src/markdown/inline.ts
|
|
2208
|
+
import { lookupEntity } from "#entities";
|
|
2209
|
+
|
|
2136
2210
|
// src/markdown/options.ts
|
|
2137
2211
|
var DEFAULT_MARKDOWN = { breaks: false, setext: true, autolinks: true, syntax: [], inline: [], block: [], escape: "" };
|
|
2138
2212
|
var resolved = /* @__PURE__ */ new WeakMap();
|
|
@@ -2210,14 +2284,23 @@ function lowerInto(nodes, marks, link, out, soft) {
|
|
|
2210
2284
|
case "link":
|
|
2211
2285
|
lowerInto(node.children, marks, node.destination, out, soft);
|
|
2212
2286
|
break;
|
|
2213
|
-
case "image":
|
|
2214
|
-
|
|
2215
|
-
push({ text: ATOM_CHAR, marks, image: node.title ? { src: node.destination, alt, title: node.title } : { src: node.destination, alt } });
|
|
2287
|
+
case "image":
|
|
2288
|
+
push({ text: ATOM_CHAR, marks, image: imageAtom(node) });
|
|
2216
2289
|
break;
|
|
2217
|
-
}
|
|
2218
2290
|
}
|
|
2219
2291
|
}
|
|
2220
2292
|
}
|
|
2293
|
+
function imageAtom(node) {
|
|
2294
|
+
let alt = inlineNodesText(node.children, " ");
|
|
2295
|
+
if (node.width) alt = alt.replace(IMAGE_SIZE, "");
|
|
2296
|
+
return {
|
|
2297
|
+
src: node.destination,
|
|
2298
|
+
alt,
|
|
2299
|
+
...node.title ? { title: node.title } : {},
|
|
2300
|
+
...node.width ? { width: node.width } : {},
|
|
2301
|
+
...node.height ? { height: node.height } : {}
|
|
2302
|
+
};
|
|
2303
|
+
}
|
|
2221
2304
|
function withMark(marks, mark) {
|
|
2222
2305
|
return marks.includes(mark) ? marks : sortMarks([...marks, mark]);
|
|
2223
2306
|
}
|
|
@@ -2417,6 +2500,9 @@ var Node2 = class {
|
|
|
2417
2500
|
depth = 0;
|
|
2418
2501
|
destination = "";
|
|
2419
2502
|
title = "";
|
|
2503
|
+
/** An image's size (see `IMAGE_SIZE`), 0 when none. */
|
|
2504
|
+
width = 0;
|
|
2505
|
+
height = 0;
|
|
2420
2506
|
/** An escaped "@": kept apart from the text around it, so it does not join an e-mail literal. */
|
|
2421
2507
|
apart = false;
|
|
2422
2508
|
parent = null;
|
|
@@ -2494,9 +2580,13 @@ function toAst(parent) {
|
|
|
2494
2580
|
out.push({ type: n.type, children: toAst(n) });
|
|
2495
2581
|
break;
|
|
2496
2582
|
case "link":
|
|
2497
|
-
case "image":
|
|
2498
2583
|
out.push({ type: n.type, destination: n.destination, title: n.title, children: toAst(n) });
|
|
2499
2584
|
break;
|
|
2585
|
+
case "image": {
|
|
2586
|
+
const image = { type: n.type, destination: n.destination, title: n.title, children: toAst(n) };
|
|
2587
|
+
out.push(n.width ? { ...image, width: n.width, ...n.height ? { height: n.height } : {} } : image);
|
|
2588
|
+
break;
|
|
2589
|
+
}
|
|
2500
2590
|
case "root":
|
|
2501
2591
|
break;
|
|
2502
2592
|
}
|
|
@@ -2952,6 +3042,14 @@ var InlineParser = class {
|
|
|
2952
3042
|
const link = new Node2(opener.image ? "image" : "link");
|
|
2953
3043
|
link.destination = dest ?? "";
|
|
2954
3044
|
link.title = title ?? "";
|
|
3045
|
+
if (opener.image) {
|
|
3046
|
+
const label = this.subject.slice(opener.index + 1, startPos - 1);
|
|
3047
|
+
const size = IMAGE_SIZE.exec(label);
|
|
3048
|
+
if (size && !/(^|[^\\])(\\\\)*\\$/.test(label.slice(0, size.index))) {
|
|
3049
|
+
link.width = Number(size[1]);
|
|
3050
|
+
link.height = size[2] ? Number(size[2]) : 0;
|
|
3051
|
+
}
|
|
3052
|
+
}
|
|
2955
3053
|
let n = opener.node.next;
|
|
2956
3054
|
while (n) {
|
|
2957
3055
|
const next = n.next;
|
|
@@ -3967,7 +4065,7 @@ var STRATEGIES = [
|
|
|
3967
4065
|
function sameInline(a, b) {
|
|
3968
4066
|
return a.length === b.length && a.every((s, i) => {
|
|
3969
4067
|
const t = b[i];
|
|
3970
|
-
return s.text === t.text && s.link === t.link && sameMarks(s.marks, t.marks) && s.image?.src === t.image?.src && s.image?.alt === t.image?.alt && s.image?.title === t.image?.title;
|
|
4068
|
+
return s.text === t.text && s.link === t.link && sameMarks(s.marks, t.marks) && s.image?.src === t.image?.src && s.image?.alt === t.image?.alt && s.image?.title === t.image?.title && s.image?.width === t.image?.width && s.image?.height === t.image?.height;
|
|
3971
4069
|
});
|
|
3972
4070
|
}
|
|
3973
4071
|
var reSpecWhitespace = /^[\t\n\f\r\p{Zs}]+|[\t\n\f\r\p{Zs}]+$/gu;
|
|
@@ -4348,7 +4446,9 @@ function imageMarkdown(image, ctx) {
|
|
|
4348
4446
|
atEnd: false
|
|
4349
4447
|
});
|
|
4350
4448
|
const title = image.title ? ` "${escapeEntities(image.title.replace(/["\\]/g, "\\$&"))}"` : "";
|
|
4351
|
-
|
|
4449
|
+
const size = image.width ? `|${image.width}${image.height ? `x${image.height}` : ""}` : "";
|
|
4450
|
+
const text = size ? alt + size : IMAGE_SIZE.test(alt) ? alt.replace(/\|(?=[^|]*$)/, "|") : alt;
|
|
4451
|
+
return `}${title})`;
|
|
4352
4452
|
}
|
|
4353
4453
|
|
|
4354
4454
|
// src/markdown/source.ts
|
|
@@ -5963,14 +6063,23 @@ function renderSpan(span, ctx, viewed = false) {
|
|
|
5963
6063
|
const view = viewed ? ctx.view?.(span.token, span.text, "inline") : null;
|
|
5964
6064
|
if (view) return wrapMarks(span, atom(view, classes("mbe-view", `mbe-view-${span.token}`, ctx.classes.token?.[span.token]), span.text), ctx);
|
|
5965
6065
|
if (span.image) {
|
|
6066
|
+
const image = span.image;
|
|
6067
|
+
const src = ctx.resolveUrl(image.src, "image");
|
|
6068
|
+
if (src === null) return unavailableImage(image, ctx);
|
|
5966
6069
|
const img = doc.createElement("img");
|
|
5967
6070
|
img.setAttribute("loading", "lazy");
|
|
5968
6071
|
img.setAttribute("decoding", "async");
|
|
5969
6072
|
if (ctx.referrerPolicy) img.setAttribute("referrerpolicy", ctx.referrerPolicy);
|
|
5970
|
-
|
|
5971
|
-
|
|
5972
|
-
img.
|
|
5973
|
-
if (
|
|
6073
|
+
img.src = src;
|
|
6074
|
+
img.alt = image.alt;
|
|
6075
|
+
if (image.title) img.title = image.title;
|
|
6076
|
+
if (image.width) img.width = image.width;
|
|
6077
|
+
if (image.height) img.height = image.height;
|
|
6078
|
+
img.addEventListener("error", () => {
|
|
6079
|
+
if (!img.parentNode) return;
|
|
6080
|
+
img.replaceWith(unavailableImage(image, ctx));
|
|
6081
|
+
ctx.onImageError?.();
|
|
6082
|
+
});
|
|
5974
6083
|
return atom(img, classes("mbe-image", ctx.classes.image));
|
|
5975
6084
|
}
|
|
5976
6085
|
let node = doc.createTextNode(span.text);
|
|
@@ -5983,6 +6092,21 @@ function renderSpan(span, ctx, viewed = false) {
|
|
|
5983
6092
|
}
|
|
5984
6093
|
return wrapMarks(span, node, ctx);
|
|
5985
6094
|
}
|
|
6095
|
+
function unavailableImage(image, ctx) {
|
|
6096
|
+
const doc = ctx.doc;
|
|
6097
|
+
const el = doc.createElement("span");
|
|
6098
|
+
const text = image.alt || ctx.labels.imageUnavailable;
|
|
6099
|
+
el.setAttribute("role", "img");
|
|
6100
|
+
el.setAttribute("aria-label", text);
|
|
6101
|
+
if (image.title) el.title = image.title;
|
|
6102
|
+
const icon = ctx.icon?.("imageUnavailable");
|
|
6103
|
+
if (icon) el.appendChild(icon);
|
|
6104
|
+
const label = doc.createElement("span");
|
|
6105
|
+
label.className = "mbe-image-alt";
|
|
6106
|
+
label.textContent = text;
|
|
6107
|
+
el.appendChild(label);
|
|
6108
|
+
return atom(el, classes("mbe-image", "mbe-image-unavailable", ctx.classes.image));
|
|
6109
|
+
}
|
|
5986
6110
|
function wrapMarks(span, node, ctx) {
|
|
5987
6111
|
for (let i = MARK_ORDER.length - 1; i >= 0; i--) {
|
|
5988
6112
|
const mark = MARK_ORDER[i];
|
|
@@ -6028,6 +6152,8 @@ var BlockRenderer = class {
|
|
|
6028
6152
|
icon;
|
|
6029
6153
|
/** Code blocks get a copy button. */
|
|
6030
6154
|
codeCopy = false;
|
|
6155
|
+
/** An image failed to load and now shows as unavailable. */
|
|
6156
|
+
onImageError;
|
|
6031
6157
|
/** Per block, the views that show their source. A block re-renders when its entry changes. */
|
|
6032
6158
|
revealed = /* @__PURE__ */ new Map();
|
|
6033
6159
|
/**
|
|
@@ -6053,7 +6179,8 @@ var BlockRenderer = class {
|
|
|
6053
6179
|
view: this.views && ((syntax, source, kind) => this.renderView(syntax, source, kind)),
|
|
6054
6180
|
classes: this.classes,
|
|
6055
6181
|
icon: this.icon,
|
|
6056
|
-
codeCopy: this.codeCopy
|
|
6182
|
+
codeCopy: this.codeCopy,
|
|
6183
|
+
onImageError: this.onImageError
|
|
6057
6184
|
};
|
|
6058
6185
|
}
|
|
6059
6186
|
renderView(syntax, source, kind) {
|
|
@@ -6281,6 +6408,17 @@ function domToPosition(root, node, offset) {
|
|
|
6281
6408
|
if (!content) return { blockId, offset: 0 };
|
|
6282
6409
|
return { blockId, offset: offsetIn(content, target, off) };
|
|
6283
6410
|
}
|
|
6411
|
+
function atomAt(blockEl, pos) {
|
|
6412
|
+
const content = contentElement(blockEl, pos.cell);
|
|
6413
|
+
if (!content) return null;
|
|
6414
|
+
let acc = 0;
|
|
6415
|
+
for (const leaf of leaves(content)) {
|
|
6416
|
+
if (acc === pos.offset && leaf.nodeType !== 3) return leaf;
|
|
6417
|
+
acc += leafLength(leaf);
|
|
6418
|
+
if (acc > pos.offset) return null;
|
|
6419
|
+
}
|
|
6420
|
+
return null;
|
|
6421
|
+
}
|
|
6284
6422
|
function positionToDom(blockEl, pos) {
|
|
6285
6423
|
const content = contentElement(blockEl, pos.cell);
|
|
6286
6424
|
if (!content) {
|
|
@@ -6393,6 +6531,185 @@ var BlockHandle = class {
|
|
|
6393
6531
|
}
|
|
6394
6532
|
};
|
|
6395
6533
|
|
|
6534
|
+
// src/ui/image.ts
|
|
6535
|
+
var MIN_WIDTH = 24;
|
|
6536
|
+
var SNAP = 8;
|
|
6537
|
+
var ImageControls = class {
|
|
6538
|
+
constructor(editor) {
|
|
6539
|
+
this.editor = editor;
|
|
6540
|
+
const root = editor.root;
|
|
6541
|
+
const doc = root.ownerDocument;
|
|
6542
|
+
const labels = editor.labels;
|
|
6543
|
+
this.frame = doc.createElement("div");
|
|
6544
|
+
this.frame.className = "mbe-ui mbe-image-frame";
|
|
6545
|
+
this.frame.hidden = true;
|
|
6546
|
+
const handle = (side) => {
|
|
6547
|
+
const el = doc.createElement("div");
|
|
6548
|
+
el.className = `mbe-image-handle mbe-image-handle-${side}`;
|
|
6549
|
+
el.dataset.side = side;
|
|
6550
|
+
el.title = labels.resizeImage;
|
|
6551
|
+
el.setAttribute("aria-hidden", "true");
|
|
6552
|
+
this.frame.appendChild(el);
|
|
6553
|
+
return el;
|
|
6554
|
+
};
|
|
6555
|
+
this.handles = { left: handle("left"), right: handle("right") };
|
|
6556
|
+
const on = (target, type, fn) => {
|
|
6557
|
+
target.addEventListener(type, fn);
|
|
6558
|
+
this.cleanup.push(() => target.removeEventListener(type, fn));
|
|
6559
|
+
};
|
|
6560
|
+
on(root, "mouseover", (e) => this.onOver(e));
|
|
6561
|
+
on(root, "mouseleave", () => this.scheduleUnhover());
|
|
6562
|
+
for (const el of [this.frame, this.handles.left, this.handles.right]) {
|
|
6563
|
+
on(el, "mouseenter", () => this.cancelUnhover());
|
|
6564
|
+
on(el, "mouseleave", () => this.scheduleUnhover());
|
|
6565
|
+
}
|
|
6566
|
+
on(this.frame, "mousedown", (e) => e.preventDefault());
|
|
6567
|
+
for (const side of ["left", "right"]) on(this.handles[side], "pointerdown", (e) => this.startDrag(e, side));
|
|
6568
|
+
on(this.frame, "pointermove", (e) => this.moveDrag(e));
|
|
6569
|
+
on(this.frame, "pointerup", (e) => this.endDrag(e, true));
|
|
6570
|
+
on(this.frame, "pointercancel", (e) => this.endDrag(e, false));
|
|
6571
|
+
on(this.frame, "lostpointercapture", (e) => this.endDrag(e, true));
|
|
6572
|
+
}
|
|
6573
|
+
/** A frame over the image, holding the two handles. It takes no pointer events itself. */
|
|
6574
|
+
frame;
|
|
6575
|
+
handles;
|
|
6576
|
+
/** The image under the pointer, which shows handles while it is hovered. */
|
|
6577
|
+
hovered = null;
|
|
6578
|
+
/** The selected image, which shows handles. */
|
|
6579
|
+
selected = null;
|
|
6580
|
+
/** The image the frame is shown for. */
|
|
6581
|
+
framed = null;
|
|
6582
|
+
drag = null;
|
|
6583
|
+
hideTimer = null;
|
|
6584
|
+
cleanup = [];
|
|
6585
|
+
/** True when `node` is part of these controls. */
|
|
6586
|
+
contains(node) {
|
|
6587
|
+
return this.frame.contains(node);
|
|
6588
|
+
}
|
|
6589
|
+
/** Shows the handles for the selected image, or for none. */
|
|
6590
|
+
update(selected) {
|
|
6591
|
+
this.selected = selected;
|
|
6592
|
+
if (!this.drag) this.placeFrame();
|
|
6593
|
+
}
|
|
6594
|
+
/** Moves the controls to where their image is now, after scrolling. */
|
|
6595
|
+
reposition() {
|
|
6596
|
+
if (this.drag) return;
|
|
6597
|
+
if (this.selected && !this.selected.el.isConnected) this.selected = null;
|
|
6598
|
+
this.placeFrame();
|
|
6599
|
+
}
|
|
6600
|
+
hide() {
|
|
6601
|
+
if (this.drag) return;
|
|
6602
|
+
this.hovered = null;
|
|
6603
|
+
this.selected = null;
|
|
6604
|
+
this.placeFrame();
|
|
6605
|
+
}
|
|
6606
|
+
destroy() {
|
|
6607
|
+
this.cancelUnhover();
|
|
6608
|
+
for (const fn of this.cleanup) fn();
|
|
6609
|
+
this.frame.remove();
|
|
6610
|
+
}
|
|
6611
|
+
// -- Hover ----------------------------------------------------------------
|
|
6612
|
+
onOver(e) {
|
|
6613
|
+
if (this.drag || !this.editor.isEditable) return;
|
|
6614
|
+
const target = e.target;
|
|
6615
|
+
const img = target instanceof HTMLImageElement && target.classList.contains("mbe-image") ? target : null;
|
|
6616
|
+
if (img) {
|
|
6617
|
+
this.cancelUnhover();
|
|
6618
|
+
this.hovered = img;
|
|
6619
|
+
this.placeFrame();
|
|
6620
|
+
} else if (this.hovered) {
|
|
6621
|
+
this.scheduleUnhover();
|
|
6622
|
+
}
|
|
6623
|
+
}
|
|
6624
|
+
scheduleUnhover() {
|
|
6625
|
+
this.cancelUnhover();
|
|
6626
|
+
this.hideTimer = setTimeout(() => {
|
|
6627
|
+
this.hideTimer = null;
|
|
6628
|
+
this.hovered = null;
|
|
6629
|
+
this.placeFrame();
|
|
6630
|
+
}, 250);
|
|
6631
|
+
}
|
|
6632
|
+
cancelUnhover() {
|
|
6633
|
+
if (this.hideTimer !== null) clearTimeout(this.hideTimer);
|
|
6634
|
+
this.hideTimer = null;
|
|
6635
|
+
}
|
|
6636
|
+
/** Shows the handles over the hovered image, else the selected one. Only images that loaded can be resized. */
|
|
6637
|
+
placeFrame() {
|
|
6638
|
+
const editable = this.editor.isEditable;
|
|
6639
|
+
const hovered = this.hovered?.isConnected ? this.hovered : null;
|
|
6640
|
+
const el = this.drag?.el ?? (editable ? hovered ?? this.selected?.el ?? null : null);
|
|
6641
|
+
const img = el instanceof HTMLImageElement && el.isConnected ? el : null;
|
|
6642
|
+
const rect = img?.getBoundingClientRect();
|
|
6643
|
+
if (!img || !rect || rect.width === 0 || rect.height === 0) {
|
|
6644
|
+
this.frame.hidden = true;
|
|
6645
|
+
this.framed = null;
|
|
6646
|
+
return;
|
|
6647
|
+
}
|
|
6648
|
+
if (this.framed !== img) {
|
|
6649
|
+
this.framed = img;
|
|
6650
|
+
if (!img.complete) img.addEventListener("load", () => this.framed === img && this.placeFrame(), { once: true });
|
|
6651
|
+
}
|
|
6652
|
+
this.frame.hidden = false;
|
|
6653
|
+
const offset = attach(this.frame, this.editor.root);
|
|
6654
|
+
this.frame.style.left = `${Math.round(rect.left - offset.x)}px`;
|
|
6655
|
+
this.frame.style.top = `${Math.round(rect.top - offset.y)}px`;
|
|
6656
|
+
this.frame.style.width = `${Math.round(rect.width)}px`;
|
|
6657
|
+
this.frame.style.height = `${Math.round(rect.height)}px`;
|
|
6658
|
+
this.frame.classList.toggle("mbe-image-frame-small", rect.width < 64 || rect.height < 40);
|
|
6659
|
+
}
|
|
6660
|
+
// -- Resizing -------------------------------------------------------------
|
|
6661
|
+
startDrag(e, side) {
|
|
6662
|
+
const el = this.framed;
|
|
6663
|
+
if (!el || this.drag || e.button !== 0 || !this.editor.isEditable) return;
|
|
6664
|
+
const target = this.editor.imageTarget(el);
|
|
6665
|
+
if (!target) return;
|
|
6666
|
+
e.preventDefault();
|
|
6667
|
+
this.handles[side].setPointerCapture?.(e.pointerId);
|
|
6668
|
+
const width = el.getBoundingClientRect().width;
|
|
6669
|
+
const others = Array.from(this.editor.root.querySelectorAll("img.mbe-image")).filter((other) => other !== el).map((other) => other.getBoundingClientRect().width).filter((w) => w > 0);
|
|
6670
|
+
const max = Math.max(MIN_WIDTH, columnWidth(el));
|
|
6671
|
+
this.drag = { el, side, pointer: e.pointerId, startX: e.clientX, startWidth: width, max, snaps: [...others, max], width };
|
|
6672
|
+
this.frame.classList.add("mbe-image-resizing");
|
|
6673
|
+
this.editor.selectImageAt(target.pos);
|
|
6674
|
+
}
|
|
6675
|
+
moveDrag(e) {
|
|
6676
|
+
const drag = this.drag;
|
|
6677
|
+
if (!drag || e.pointerId !== drag.pointer) return;
|
|
6678
|
+
e.preventDefault();
|
|
6679
|
+
const dx = (e.clientX - drag.startX) * (drag.side === "right" ? 1 : -1);
|
|
6680
|
+
let width = Math.min(drag.max, Math.max(MIN_WIDTH, drag.startWidth + dx));
|
|
6681
|
+
const snap = drag.snaps.find((w) => Math.abs(w - width) <= SNAP);
|
|
6682
|
+
if (snap !== void 0) width = Math.min(drag.max, snap);
|
|
6683
|
+
drag.width = Math.round(width);
|
|
6684
|
+
drag.el.style.width = `${drag.width}px`;
|
|
6685
|
+
drag.el.style.height = "auto";
|
|
6686
|
+
this.placeFrame();
|
|
6687
|
+
}
|
|
6688
|
+
endDrag(e, commit) {
|
|
6689
|
+
const drag = this.drag;
|
|
6690
|
+
if (!drag || e.pointerId !== drag.pointer) return;
|
|
6691
|
+
this.drag = null;
|
|
6692
|
+
this.frame.classList.remove("mbe-image-resizing");
|
|
6693
|
+
this.handles[drag.side].releasePointerCapture?.(e.pointerId);
|
|
6694
|
+
drag.el.style.width = "";
|
|
6695
|
+
drag.el.style.height = "";
|
|
6696
|
+
const target = drag.el.isConnected ? this.editor.imageTarget(drag.el) : null;
|
|
6697
|
+
if (commit && target && Math.round(drag.width) !== Math.round(drag.startWidth)) {
|
|
6698
|
+
this.editor.updateImageAt(target.pos, { width: drag.width });
|
|
6699
|
+
} else {
|
|
6700
|
+
this.editor.refreshImageUi();
|
|
6701
|
+
}
|
|
6702
|
+
}
|
|
6703
|
+
};
|
|
6704
|
+
function columnWidth(el) {
|
|
6705
|
+
let box = el.parentElement;
|
|
6706
|
+
const view = el.ownerDocument.defaultView;
|
|
6707
|
+
while (box && view.getComputedStyle(box).display.startsWith("inline")) box = box.parentElement;
|
|
6708
|
+
if (!box) return el.getBoundingClientRect().width;
|
|
6709
|
+
const cs = view.getComputedStyle(box);
|
|
6710
|
+
return box.clientWidth - parseFloat(cs.paddingLeft || "0") - parseFloat(cs.paddingRight || "0");
|
|
6711
|
+
}
|
|
6712
|
+
|
|
6396
6713
|
// src/view/reveal.ts
|
|
6397
6714
|
function revealedViews(state, views, active) {
|
|
6398
6715
|
const out = /* @__PURE__ */ new Map();
|
|
@@ -6630,6 +6947,9 @@ var Editor = class {
|
|
|
6630
6947
|
toolbar;
|
|
6631
6948
|
linkPopover;
|
|
6632
6949
|
handle;
|
|
6950
|
+
imageControls;
|
|
6951
|
+
/** The selected image's element, which carries the selected class. */
|
|
6952
|
+
selectedImageEl = null;
|
|
6633
6953
|
hintEl = null;
|
|
6634
6954
|
/**
|
|
6635
6955
|
* The block selection, by block ID, or null for a text selection. While it
|
|
@@ -6730,6 +7050,12 @@ var Editor = class {
|
|
|
6730
7050
|
this.linkPopover = new LinkPopover(this);
|
|
6731
7051
|
this.toolbar = options.toolbar === false ? null : new FormatToolbar(this);
|
|
6732
7052
|
this.handle = new BlockHandle(this);
|
|
7053
|
+
this.imageControls = new ImageControls(this);
|
|
7054
|
+
this.renderer.onImageError = () => {
|
|
7055
|
+
if (this.destroyed) return;
|
|
7056
|
+
if (!this.composing) this.syncDomSelection();
|
|
7057
|
+
this.updateUi();
|
|
7058
|
+
};
|
|
6733
7059
|
this.listen(root, "beforeinput", this.typed((e) => this.onBeforeInput(e)));
|
|
6734
7060
|
this.listen(root, "input", this.typed((e) => this.onInput(e)));
|
|
6735
7061
|
this.listen(root, "compositionstart", (e) => this.onCompositionStart(e));
|
|
@@ -6817,6 +7143,12 @@ var Editor = class {
|
|
|
6817
7143
|
return isMarkActive(s, name);
|
|
6818
7144
|
case "link":
|
|
6819
7145
|
return activeLink(s) !== null;
|
|
7146
|
+
case "image": {
|
|
7147
|
+
const selected = selectedImage(s);
|
|
7148
|
+
if (!selected || !attrs) return selected !== null;
|
|
7149
|
+
const actual2 = { ...selected.image };
|
|
7150
|
+
return Object.entries(attrs).every(([k, v]) => actual2[k] === v);
|
|
7151
|
+
}
|
|
6820
7152
|
}
|
|
6821
7153
|
const block3 = getBlock(s.doc, s.selection.focus.blockId);
|
|
6822
7154
|
if (blockName(block3) !== name) return false;
|
|
@@ -6824,13 +7156,14 @@ var Editor = class {
|
|
|
6824
7156
|
const actual = this.getAttributes(name);
|
|
6825
7157
|
return Object.entries(attrs).every(([k, v]) => actual[k] === v);
|
|
6826
7158
|
}
|
|
6827
|
-
/** Attributes of `name` at the selection: `href` for a link, `level` for a heading, `language` for a code block, `checked` for a task, `syntax` for a raw block. */
|
|
7159
|
+
/** Attributes of `name` at the selection: `src`, `alt`, `title`, `width` and `height` for a selected image, `href` for a link, `level` for a heading, `language` for a code block, `checked` for a task, `syntax` for a raw block. */
|
|
6828
7160
|
getAttributes(name) {
|
|
6829
7161
|
const s = this.current;
|
|
6830
7162
|
if (name === "link") {
|
|
6831
7163
|
const link = activeLink(s);
|
|
6832
7164
|
return link ? { href: link.href } : {};
|
|
6833
7165
|
}
|
|
7166
|
+
if (name === "image") return { ...selectedImage(s)?.image };
|
|
6834
7167
|
const block3 = getBlock(s.doc, s.selection.focus.blockId);
|
|
6835
7168
|
if (blockName(block3) !== name) return {};
|
|
6836
7169
|
switch (block3.type) {
|
|
@@ -7050,6 +7383,10 @@ var Editor = class {
|
|
|
7050
7383
|
toggleCodeBlock: (attrs = {}) => block3({ type: "code", language: attrs.language }),
|
|
7051
7384
|
setLink: ({ href }) => run((s) => setLink(s, href)),
|
|
7052
7385
|
unsetLink: () => run((s) => setLink(s, null)),
|
|
7386
|
+
updateImage: (attrs) => run((s) => {
|
|
7387
|
+
const selected = selectedImage(s);
|
|
7388
|
+
return selected ? updateImage(s, selected.pos, attrs) : null;
|
|
7389
|
+
}),
|
|
7053
7390
|
insertText: (text, options = {}) => run((s) => {
|
|
7054
7391
|
const range = options.range;
|
|
7055
7392
|
if (!range) return insertText(s, text);
|
|
@@ -7191,6 +7528,7 @@ var Editor = class {
|
|
|
7191
7528
|
this.toolbar?.destroy();
|
|
7192
7529
|
this.linkPopover.destroy();
|
|
7193
7530
|
this.handle.destroy();
|
|
7531
|
+
this.imageControls.destroy();
|
|
7194
7532
|
setPopupContainer(this.root, void 0);
|
|
7195
7533
|
for (const name of Object.keys(this.runtime.attributes ?? {})) this.root.removeAttribute(name);
|
|
7196
7534
|
for (const name of this.tokenNames) this.root.style.removeProperty(name);
|
|
@@ -7894,6 +8232,14 @@ var Editor = class {
|
|
|
7894
8232
|
e.preventDefault();
|
|
7895
8233
|
return;
|
|
7896
8234
|
}
|
|
8235
|
+
if (e.key === "Enter" && !mod2 && !e.altKey) {
|
|
8236
|
+
this.refreshSelection();
|
|
8237
|
+
const image = selectedImage(this.state);
|
|
8238
|
+
if (image) {
|
|
8239
|
+
const after = { ...image.pos, offset: image.pos.offset + 1 };
|
|
8240
|
+
this.setSelection({ anchor: after, focus: after });
|
|
8241
|
+
}
|
|
8242
|
+
}
|
|
7897
8243
|
let refreshed = false;
|
|
7898
8244
|
for (const binding of this.bindings) {
|
|
7899
8245
|
if (!matchesKey(binding, e)) continue;
|
|
@@ -8008,6 +8354,13 @@ var Editor = class {
|
|
|
8008
8354
|
}
|
|
8009
8355
|
this.dropBlockSelection();
|
|
8010
8356
|
if (target === this.root && e.button === 0 && !this.readOnly && this.clickBelowLastBlock(e)) return;
|
|
8357
|
+
const imageEl = e.button === 0 && !e.shiftKey && !this.readOnly ? target.closest?.(".mbe-image") : null;
|
|
8358
|
+
const image = imageEl && this.root.contains(imageEl) ? this.imageTarget(imageEl) : null;
|
|
8359
|
+
if (image) {
|
|
8360
|
+
e.preventDefault();
|
|
8361
|
+
this.selectImageAt(image.pos);
|
|
8362
|
+
return;
|
|
8363
|
+
}
|
|
8011
8364
|
const view = e.button === 0 && !this.readOnly ? target.closest?.(".mbe-view, .mbe-view-block") : null;
|
|
8012
8365
|
if (view && this.root.contains(view)) {
|
|
8013
8366
|
e.preventDefault();
|
|
@@ -8129,6 +8482,7 @@ var Editor = class {
|
|
|
8129
8482
|
}
|
|
8130
8483
|
onScroll() {
|
|
8131
8484
|
this.handle.hide();
|
|
8485
|
+
this.imageControls.reposition();
|
|
8132
8486
|
if (this.suggestions.isActive || this.toolbar?.isOpen || this.blockMenu.isOpen) this.updateUi();
|
|
8133
8487
|
}
|
|
8134
8488
|
// -------------------------------------------------------------------------
|
|
@@ -8488,11 +8842,14 @@ var Editor = class {
|
|
|
8488
8842
|
this.hintEl = nextHint;
|
|
8489
8843
|
}
|
|
8490
8844
|
this.suggestions.update(focused);
|
|
8845
|
+
const image = focused && !this.readOnly && !this.blockSel ? this.imageTargetAt(this.state) : null;
|
|
8846
|
+
this.paintSelectedImage(image?.el ?? null);
|
|
8847
|
+
this.imageControls.update(image);
|
|
8491
8848
|
if (this.toolbar) {
|
|
8492
8849
|
const block3 = this.state.doc.blocks.find((b) => b.id === pos.blockId);
|
|
8493
8850
|
const closed = this.toolbarClosedFor;
|
|
8494
8851
|
if (closed && !(samePosition(closed.anchor, this.state.selection.anchor) && samePosition(closed.focus, pos))) this.toolbarClosedFor = null;
|
|
8495
|
-
const show = !this.toolbarClosedFor && focused && !this.readOnly && !this.composing && !isCollapsed(this.state.selection) && !this.blockSel && !this.suggestions.isActive && !this.blockMenu.isOpen && !!block3 && !isPlainTextBlock(block3) && block3.type !== "divider";
|
|
8852
|
+
const show = !this.toolbarClosedFor && focused && !this.readOnly && !this.composing && !isCollapsed(this.state.selection) && !this.blockSel && !image && !this.suggestions.isActive && !this.blockMenu.isOpen && !!block3 && !isPlainTextBlock(block3) && block3.type !== "divider";
|
|
8496
8853
|
if (show) this.toolbar.show();
|
|
8497
8854
|
else this.toolbar.hide();
|
|
8498
8855
|
}
|
|
@@ -8503,6 +8860,51 @@ var Editor = class {
|
|
|
8503
8860
|
if (!focused && this.blockMenu.isOpen && !this.root.ownerDocument.activeElement?.closest?.(".mbe-ui")) this.blockMenu.hide();
|
|
8504
8861
|
this.syncActiveDescendant();
|
|
8505
8862
|
}
|
|
8863
|
+
/** Marks the selected image's element, and hides the browser's selection highlight over it. */
|
|
8864
|
+
paintSelectedImage(el) {
|
|
8865
|
+
if (this.selectedImageEl !== el) {
|
|
8866
|
+
this.selectedImageEl?.classList.remove("mbe-image-selected");
|
|
8867
|
+
el?.classList.add("mbe-image-selected");
|
|
8868
|
+
this.selectedImageEl = el;
|
|
8869
|
+
}
|
|
8870
|
+
this.root.classList.toggle("mbe-image-selection", el !== null);
|
|
8871
|
+
}
|
|
8872
|
+
// -------------------------------------------------------------------------
|
|
8873
|
+
// Images (see ui/image.ts)
|
|
8874
|
+
/** The selected image of `state` and its element, or null. */
|
|
8875
|
+
imageTargetAt(state) {
|
|
8876
|
+
const selected = selectedImage(state);
|
|
8877
|
+
if (!selected) return null;
|
|
8878
|
+
const blockEl = this.renderer.elementFor(selected.pos.blockId);
|
|
8879
|
+
const el = blockEl ? atomAt(blockEl, selected.pos) : null;
|
|
8880
|
+
if (!el?.classList.contains("mbe-image")) return null;
|
|
8881
|
+
return { el, ...selected };
|
|
8882
|
+
}
|
|
8883
|
+
/** @internal The image an element shows, and the position before it. Null when it is not an image in this editor. */
|
|
8884
|
+
imageTarget(el) {
|
|
8885
|
+
if (!el.parentNode || !this.root.contains(el)) return null;
|
|
8886
|
+
const pos = domToPosition(this.root, el.parentNode, childIndex(el));
|
|
8887
|
+
if (!pos) return null;
|
|
8888
|
+
const next = selectImage(this.current, pos);
|
|
8889
|
+
const selected = next === this.current ? null : selectedImage(next);
|
|
8890
|
+
return selected ? { el, ...selected } : null;
|
|
8891
|
+
}
|
|
8892
|
+
/** @internal Selects the image after `pos` and focuses the editor. */
|
|
8893
|
+
selectImageAt(pos) {
|
|
8894
|
+
if (this.readOnly || this.destroyed) return;
|
|
8895
|
+
const next = selectImage(this.current, pos);
|
|
8896
|
+
if (next === this.current) return;
|
|
8897
|
+
if (!this.hasFocus()) this.root.focus({ preventScroll: true });
|
|
8898
|
+
this.setSelection(next.selection);
|
|
8899
|
+
}
|
|
8900
|
+
/** @internal Changes the image after `pos` and selects it, as one undo step. */
|
|
8901
|
+
updateImageAt(pos, change) {
|
|
8902
|
+
this.run((s) => updateImage(s, pos, change));
|
|
8903
|
+
}
|
|
8904
|
+
/** @internal Shows the image controls again, after a resize that changed nothing. */
|
|
8905
|
+
refreshImageUi() {
|
|
8906
|
+
this.updateUi();
|
|
8907
|
+
}
|
|
8506
8908
|
syncActiveDescendant() {
|
|
8507
8909
|
const id = this.suggestions.activeDescendant ?? (this.blockMenu.isOpen ? this.blockMenu.activeDescendant : null);
|
|
8508
8910
|
if (id) this.root.setAttribute("aria-activedescendant", id);
|
|
@@ -8514,6 +8916,8 @@ var Editor = class {
|
|
|
8514
8916
|
this.blockMenu.hide();
|
|
8515
8917
|
this.toolbar?.hide();
|
|
8516
8918
|
this.handle.hide();
|
|
8919
|
+
this.imageControls.hide();
|
|
8920
|
+
this.paintSelectedImage(null);
|
|
8517
8921
|
this.hintEl?.classList.remove("mbe-show-hint");
|
|
8518
8922
|
this.hintEl = null;
|
|
8519
8923
|
}
|