@usememos/editor 26.10.16 → 26.10.18
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 +4 -4
- package/dist/base.css +20 -10
- package/dist/index.js +348 -188
- package/dist/index.js.map +4 -4
- package/dist/style.css +27 -14
- package/dist/theme.css +4 -1
- package/dist/types/editor.d.ts +2 -6
- package/dist/types/labels.d.ts +5 -1
- package/dist/types/ui/imageToolbar.d.ts +28 -0
- package/dist/types/ui/resizeHandles.d.ts +2 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2464,8 +2464,12 @@ var DEFAULT_LABELS = {
|
|
|
2464
2464
|
alignCenter: "Center",
|
|
2465
2465
|
alignRight: "Right",
|
|
2466
2466
|
tableSection: "Table",
|
|
2467
|
-
/** The title of the block menu's section of image block actions. */
|
|
2467
|
+
/** The title of the block menu's section of image block actions, and the accessible name of the image toolbar. */
|
|
2468
2468
|
imageSection: "Image",
|
|
2469
|
+
/** The image toolbar's alignment buttons, for image blocks. */
|
|
2470
|
+
imageAlignLeft: "Align left",
|
|
2471
|
+
imageAlignCenter: "Align center",
|
|
2472
|
+
imageAlignRight: "Align right",
|
|
2469
2473
|
/** Removes an image's width, so it shows at its own size. */
|
|
2470
2474
|
resetImageSize: "Reset size",
|
|
2471
2475
|
/** Edits an image's alt text, and names the field that edits it. */
|
|
@@ -6822,6 +6826,309 @@ var InputPopover = class {
|
|
|
6822
6826
|
}
|
|
6823
6827
|
};
|
|
6824
6828
|
|
|
6829
|
+
// src/ui/resizeHandles.ts
|
|
6830
|
+
var MIN_WIDTH = 24;
|
|
6831
|
+
var SNAP = 8;
|
|
6832
|
+
var MIN_HANDLE = 16;
|
|
6833
|
+
var ResizeHandles = class {
|
|
6834
|
+
constructor(editor) {
|
|
6835
|
+
this.editor = editor;
|
|
6836
|
+
const root = editor.root;
|
|
6837
|
+
const doc = root.ownerDocument;
|
|
6838
|
+
const labels = editor.labels;
|
|
6839
|
+
this.frame = doc.createElement("div");
|
|
6840
|
+
this.frame.className = "mbe-ui mbe-image-frame";
|
|
6841
|
+
this.frame.hidden = true;
|
|
6842
|
+
const resizeHandle = (side) => {
|
|
6843
|
+
const el = doc.createElement("div");
|
|
6844
|
+
el.className = `mbe-resize-handle mbe-resize-handle-${side}`;
|
|
6845
|
+
el.dataset.side = side;
|
|
6846
|
+
el.title = labels.resizeImage;
|
|
6847
|
+
el.setAttribute("aria-hidden", "true");
|
|
6848
|
+
this.frame.appendChild(el);
|
|
6849
|
+
return el;
|
|
6850
|
+
};
|
|
6851
|
+
this.handles = { left: resizeHandle("left"), right: resizeHandle("right") };
|
|
6852
|
+
const on = (target, type, fn) => {
|
|
6853
|
+
target.addEventListener(type, fn);
|
|
6854
|
+
this.cleanup.push(() => target.removeEventListener(type, fn));
|
|
6855
|
+
};
|
|
6856
|
+
on(root, "mouseover", (e) => this.onOver(e));
|
|
6857
|
+
on(root, "mouseleave", () => this.scheduleUnhover());
|
|
6858
|
+
for (const el of [this.frame, this.handles.left, this.handles.right]) {
|
|
6859
|
+
on(el, "mouseenter", () => this.cancelUnhover());
|
|
6860
|
+
on(el, "mouseleave", () => this.scheduleUnhover());
|
|
6861
|
+
}
|
|
6862
|
+
on(this.frame, "mousedown", (e) => e.preventDefault());
|
|
6863
|
+
for (const side of ["left", "right"]) on(this.handles[side], "pointerdown", (e) => this.startDrag(e, side));
|
|
6864
|
+
on(this.frame, "pointermove", (e) => this.moveDrag(e));
|
|
6865
|
+
on(this.frame, "pointerup", (e) => this.endDrag(e, true));
|
|
6866
|
+
on(this.frame, "pointercancel", (e) => this.endDrag(e, false));
|
|
6867
|
+
on(this.frame, "lostpointercapture", (e) => this.endDrag(e, true));
|
|
6868
|
+
}
|
|
6869
|
+
/** A frame over the image, holding the two handles. It takes no pointer events itself. */
|
|
6870
|
+
frame;
|
|
6871
|
+
handles;
|
|
6872
|
+
/** The image under the pointer, which shows handles while it is hovered. */
|
|
6873
|
+
hovered = null;
|
|
6874
|
+
/** The selected image, which shows handles. */
|
|
6875
|
+
selected = null;
|
|
6876
|
+
/** The image the frame is shown for. */
|
|
6877
|
+
framed = null;
|
|
6878
|
+
drag = null;
|
|
6879
|
+
hideTimer = null;
|
|
6880
|
+
cleanup = [];
|
|
6881
|
+
/** True when `node` is the frame or one of its resize handles. */
|
|
6882
|
+
contains(node) {
|
|
6883
|
+
return this.frame.contains(node);
|
|
6884
|
+
}
|
|
6885
|
+
/** True while a handle is being dragged. */
|
|
6886
|
+
get isDragging() {
|
|
6887
|
+
return this.drag !== null;
|
|
6888
|
+
}
|
|
6889
|
+
/** Shows the handles for the selected image, or for none. */
|
|
6890
|
+
update(selected) {
|
|
6891
|
+
this.selected = selected;
|
|
6892
|
+
if (!this.drag) this.placeFrame();
|
|
6893
|
+
}
|
|
6894
|
+
/** Moves the resize handles to where their image is now, after scrolling. */
|
|
6895
|
+
reposition() {
|
|
6896
|
+
if (this.drag) return;
|
|
6897
|
+
if (this.selected && !this.selected.el.isConnected) this.selected = null;
|
|
6898
|
+
this.placeFrame();
|
|
6899
|
+
}
|
|
6900
|
+
hide() {
|
|
6901
|
+
if (this.drag) return;
|
|
6902
|
+
this.hovered = null;
|
|
6903
|
+
this.selected = null;
|
|
6904
|
+
this.placeFrame();
|
|
6905
|
+
}
|
|
6906
|
+
destroy() {
|
|
6907
|
+
this.cancelUnhover();
|
|
6908
|
+
for (const fn of this.cleanup) fn();
|
|
6909
|
+
this.frame.remove();
|
|
6910
|
+
}
|
|
6911
|
+
// -- Hover ----------------------------------------------------------------
|
|
6912
|
+
onOver(e) {
|
|
6913
|
+
if (this.drag || !this.editor.isEditable) return;
|
|
6914
|
+
const target = e.target;
|
|
6915
|
+
const img = target instanceof HTMLImageElement && target.classList.contains("mbe-image") ? target : null;
|
|
6916
|
+
if (img) {
|
|
6917
|
+
this.cancelUnhover();
|
|
6918
|
+
this.hovered = img;
|
|
6919
|
+
this.placeFrame();
|
|
6920
|
+
} else if (this.hovered) {
|
|
6921
|
+
this.scheduleUnhover();
|
|
6922
|
+
}
|
|
6923
|
+
}
|
|
6924
|
+
scheduleUnhover() {
|
|
6925
|
+
this.cancelUnhover();
|
|
6926
|
+
this.hideTimer = setTimeout(() => {
|
|
6927
|
+
this.hideTimer = null;
|
|
6928
|
+
this.hovered = null;
|
|
6929
|
+
this.placeFrame();
|
|
6930
|
+
}, 250);
|
|
6931
|
+
}
|
|
6932
|
+
cancelUnhover() {
|
|
6933
|
+
if (this.hideTimer !== null) clearTimeout(this.hideTimer);
|
|
6934
|
+
this.hideTimer = null;
|
|
6935
|
+
}
|
|
6936
|
+
/** Shows the handles over the hovered image, else the selected one. Only images that loaded can be resized. */
|
|
6937
|
+
placeFrame() {
|
|
6938
|
+
const editable = this.editor.isEditable;
|
|
6939
|
+
const hovered = this.hovered?.isConnected ? this.hovered : null;
|
|
6940
|
+
const el = this.drag?.el ?? (editable ? hovered ?? this.selected?.el ?? null : null);
|
|
6941
|
+
const img = el instanceof HTMLImageElement && el.isConnected ? el : null;
|
|
6942
|
+
const rect = img?.getBoundingClientRect();
|
|
6943
|
+
if (!img || !rect || rect.width === 0 || rect.height === 0) {
|
|
6944
|
+
this.frame.hidden = true;
|
|
6945
|
+
this.framed = null;
|
|
6946
|
+
return;
|
|
6947
|
+
}
|
|
6948
|
+
if (this.framed !== img) {
|
|
6949
|
+
this.framed = img;
|
|
6950
|
+
if (!img.complete) img.addEventListener("load", () => this.framed === img && this.placeFrame(), { once: true });
|
|
6951
|
+
}
|
|
6952
|
+
const shown = visibleRect(img, rect);
|
|
6953
|
+
if (!shown || shown.width < Math.min(rect.width, MIN_HANDLE) || shown.height < Math.min(rect.height, MIN_HANDLE)) {
|
|
6954
|
+
this.frame.hidden = true;
|
|
6955
|
+
return;
|
|
6956
|
+
}
|
|
6957
|
+
this.frame.hidden = false;
|
|
6958
|
+
const offset = attach(this.frame, this.editor.root);
|
|
6959
|
+
this.frame.style.left = `${Math.round(shown.left - offset.x)}px`;
|
|
6960
|
+
this.frame.style.top = `${Math.round(shown.top - offset.y)}px`;
|
|
6961
|
+
this.frame.style.width = `${Math.round(shown.width)}px`;
|
|
6962
|
+
this.frame.style.height = `${Math.round(shown.height)}px`;
|
|
6963
|
+
this.frame.classList.toggle("mbe-image-frame-small", shown.width < 64 || shown.height < 40);
|
|
6964
|
+
}
|
|
6965
|
+
// -- Resizing -------------------------------------------------------------
|
|
6966
|
+
startDrag(e, side) {
|
|
6967
|
+
const el = this.framed;
|
|
6968
|
+
if (!el || this.drag || e.button !== 0 || !this.editor.isEditable) return;
|
|
6969
|
+
const target = this.editor.imageTarget(el);
|
|
6970
|
+
if (!target) return;
|
|
6971
|
+
e.preventDefault();
|
|
6972
|
+
this.handles[side].setPointerCapture?.(e.pointerId);
|
|
6973
|
+
const width = el.getBoundingClientRect().width;
|
|
6974
|
+
const others = Array.from(this.editor.root.querySelectorAll("img.mbe-image")).filter((other) => other !== el).map((other) => other.getBoundingClientRect().width).filter((w) => w > 0);
|
|
6975
|
+
const max = Math.max(MIN_WIDTH, columnWidth(el));
|
|
6976
|
+
const factor = el.closest(".mbe-image-block")?.getAttribute("data-align") === "center" ? 2 : 1;
|
|
6977
|
+
this.drag = { el, side, pointer: e.pointerId, startX: e.clientX, startWidth: width, max, snaps: [...others, max], width, factor };
|
|
6978
|
+
this.frame.classList.add("mbe-image-resizing");
|
|
6979
|
+
this.editor.selectImageAt(target.pos);
|
|
6980
|
+
}
|
|
6981
|
+
moveDrag(e) {
|
|
6982
|
+
const drag = this.drag;
|
|
6983
|
+
if (!drag || e.pointerId !== drag.pointer) return;
|
|
6984
|
+
e.preventDefault();
|
|
6985
|
+
const dx = (e.clientX - drag.startX) * (drag.side === "right" ? 1 : -1) * drag.factor;
|
|
6986
|
+
let width = Math.min(drag.max, Math.max(MIN_WIDTH, drag.startWidth + dx));
|
|
6987
|
+
const snap = drag.snaps.find((w) => Math.abs(w - width) <= SNAP);
|
|
6988
|
+
if (snap !== void 0) width = Math.min(drag.max, snap);
|
|
6989
|
+
drag.width = Math.round(width);
|
|
6990
|
+
drag.el.style.width = `${drag.width}px`;
|
|
6991
|
+
drag.el.style.height = "auto";
|
|
6992
|
+
this.placeFrame();
|
|
6993
|
+
}
|
|
6994
|
+
endDrag(e, commit) {
|
|
6995
|
+
const drag = this.drag;
|
|
6996
|
+
if (!drag || e.pointerId !== drag.pointer) return;
|
|
6997
|
+
this.drag = null;
|
|
6998
|
+
this.frame.classList.remove("mbe-image-resizing");
|
|
6999
|
+
this.handles[drag.side].releasePointerCapture?.(e.pointerId);
|
|
7000
|
+
drag.el.style.width = "";
|
|
7001
|
+
drag.el.style.height = "";
|
|
7002
|
+
const target = drag.el.isConnected ? this.editor.imageTarget(drag.el) : null;
|
|
7003
|
+
if (commit && target && Math.round(drag.width) !== Math.round(drag.startWidth)) {
|
|
7004
|
+
this.editor.updateImageAt(target.pos, { width: drag.width });
|
|
7005
|
+
} else {
|
|
7006
|
+
this.editor.refreshImageUi();
|
|
7007
|
+
}
|
|
7008
|
+
}
|
|
7009
|
+
};
|
|
7010
|
+
function visibleRect(img, rect) {
|
|
7011
|
+
const doc = img.ownerDocument;
|
|
7012
|
+
const view = doc.defaultView;
|
|
7013
|
+
let left = rect.left;
|
|
7014
|
+
let top = rect.top;
|
|
7015
|
+
let right = rect.right;
|
|
7016
|
+
let bottom = rect.bottom;
|
|
7017
|
+
const clip = (l, t, r, b) => {
|
|
7018
|
+
left = Math.max(left, l);
|
|
7019
|
+
top = Math.max(top, t);
|
|
7020
|
+
right = Math.min(right, r);
|
|
7021
|
+
bottom = Math.min(bottom, b);
|
|
7022
|
+
};
|
|
7023
|
+
for (let el = img.parentElement; el && el !== doc.body && el !== doc.documentElement; el = el.parentElement) {
|
|
7024
|
+
const style = view?.getComputedStyle(el);
|
|
7025
|
+
const scrollsY = el.scrollHeight > el.clientHeight && /auto|scroll|overlay/.test(style?.overflowY ?? "");
|
|
7026
|
+
const scrollsX = el.scrollWidth > el.clientWidth && /auto|scroll|overlay/.test(style?.overflowX ?? "");
|
|
7027
|
+
if (!scrollsY && !scrollsX) continue;
|
|
7028
|
+
const box = el.getBoundingClientRect();
|
|
7029
|
+
const viewLeft = box.left + el.clientLeft;
|
|
7030
|
+
const viewTop = box.top + el.clientTop;
|
|
7031
|
+
clip(viewLeft, viewTop, viewLeft + el.clientWidth, viewTop + el.clientHeight);
|
|
7032
|
+
}
|
|
7033
|
+
clip(0, 0, doc.documentElement.clientWidth, doc.documentElement.clientHeight);
|
|
7034
|
+
if (right <= left || bottom <= top) return null;
|
|
7035
|
+
return { left, top, width: right - left, height: bottom - top };
|
|
7036
|
+
}
|
|
7037
|
+
function columnWidth(el) {
|
|
7038
|
+
let box = el.parentElement;
|
|
7039
|
+
const view = el.ownerDocument.defaultView;
|
|
7040
|
+
while (box && view.getComputedStyle(box).display.startsWith("inline")) box = box.parentElement;
|
|
7041
|
+
if (!box) return el.getBoundingClientRect().width;
|
|
7042
|
+
const cs = view.getComputedStyle(box);
|
|
7043
|
+
return box.clientWidth - parseFloat(cs.paddingLeft || "0") - parseFloat(cs.paddingRight || "0");
|
|
7044
|
+
}
|
|
7045
|
+
|
|
7046
|
+
// src/ui/imageToolbar.ts
|
|
7047
|
+
var ALIGNS = [
|
|
7048
|
+
{ align: "left", icon: "imageAlignLeft" },
|
|
7049
|
+
{ align: "center", icon: "imageAlignCenter" },
|
|
7050
|
+
{ align: "right", icon: "imageAlignRight" }
|
|
7051
|
+
];
|
|
7052
|
+
var ImageToolbar = class {
|
|
7053
|
+
constructor(editor, actions) {
|
|
7054
|
+
this.editor = editor;
|
|
7055
|
+
const doc = editor.root.ownerDocument;
|
|
7056
|
+
const labels = editor.labels;
|
|
7057
|
+
this.popup = new Popup(editor.root, "mbe-image-toolbar");
|
|
7058
|
+
const el = this.popup.el;
|
|
7059
|
+
el.setAttribute("role", "toolbar");
|
|
7060
|
+
el.setAttribute("aria-label", labels.imageSection);
|
|
7061
|
+
const button = (name, className, onClick) => {
|
|
7062
|
+
const b = doc.createElement("button");
|
|
7063
|
+
b.type = "button";
|
|
7064
|
+
b.className = `mbe-tool ${className}`;
|
|
7065
|
+
const icon = iconNode(doc, editor.icon(name));
|
|
7066
|
+
if (icon) b.appendChild(icon);
|
|
7067
|
+
b.setAttribute("aria-label", labels[name]);
|
|
7068
|
+
b.title = labels[name];
|
|
7069
|
+
b.addEventListener("click", () => {
|
|
7070
|
+
if (this.target) onClick();
|
|
7071
|
+
});
|
|
7072
|
+
return b;
|
|
7073
|
+
};
|
|
7074
|
+
for (const { align, icon } of ALIGNS) {
|
|
7075
|
+
const b = button(icon, `mbe-tool-${icon}`, () => actions.align(this.target, align));
|
|
7076
|
+
b.setAttribute("aria-pressed", "false");
|
|
7077
|
+
this.alignButtons.push({ align, button: b });
|
|
7078
|
+
el.appendChild(b);
|
|
7079
|
+
}
|
|
7080
|
+
this.alignSep = doc.createElement("span");
|
|
7081
|
+
this.alignSep.className = "mbe-tool-sep";
|
|
7082
|
+
el.appendChild(this.alignSep);
|
|
7083
|
+
el.appendChild(button("altText", "mbe-tool-altText", () => actions.altText(this.target)));
|
|
7084
|
+
this.resetButton = button("resetImageSize", "mbe-tool-resetImageSize", () => actions.resetSize(this.target));
|
|
7085
|
+
el.appendChild(this.resetButton);
|
|
7086
|
+
const sep = doc.createElement("span");
|
|
7087
|
+
sep.className = "mbe-tool-sep";
|
|
7088
|
+
el.appendChild(sep);
|
|
7089
|
+
el.appendChild(button("delete", "mbe-tool-delete", () => actions.remove(this.target)));
|
|
7090
|
+
}
|
|
7091
|
+
popup;
|
|
7092
|
+
alignButtons = [];
|
|
7093
|
+
alignSep;
|
|
7094
|
+
resetButton;
|
|
7095
|
+
target = null;
|
|
7096
|
+
get isOpen() {
|
|
7097
|
+
return this.popup.isOpen;
|
|
7098
|
+
}
|
|
7099
|
+
/** True if `node` is in the toolbar. */
|
|
7100
|
+
contains(node) {
|
|
7101
|
+
return this.popup.el.contains(node);
|
|
7102
|
+
}
|
|
7103
|
+
/**
|
|
7104
|
+
* Shows the toolbar over `target`, the selected image, with the alignment
|
|
7105
|
+
* buttons only for an image block (`block`). Over the part of the image its
|
|
7106
|
+
* scrolling container shows, so it stays by the image in a short composer.
|
|
7107
|
+
*/
|
|
7108
|
+
show(target, block3) {
|
|
7109
|
+
const shown = visibleRect(target.el, target.el.getBoundingClientRect());
|
|
7110
|
+
if (!shown) return this.hide();
|
|
7111
|
+
this.target = target;
|
|
7112
|
+
const align = target.image.align ?? "left";
|
|
7113
|
+
for (const { align: value, button } of this.alignButtons) {
|
|
7114
|
+
button.hidden = !block3;
|
|
7115
|
+
button.classList.toggle("mbe-active", block3 && value === align);
|
|
7116
|
+
button.setAttribute("aria-pressed", String(block3 && value === align));
|
|
7117
|
+
}
|
|
7118
|
+
this.alignSep.hidden = !block3;
|
|
7119
|
+
this.resetButton.hidden = !target.image.width;
|
|
7120
|
+
const { left, top, width, height } = shown;
|
|
7121
|
+
this.popup.show({ left, top, right: left + width, bottom: top + height }, isTouch() ? "below" : "above", 8);
|
|
7122
|
+
}
|
|
7123
|
+
hide() {
|
|
7124
|
+
this.target = null;
|
|
7125
|
+
this.popup.hide();
|
|
7126
|
+
}
|
|
7127
|
+
destroy() {
|
|
7128
|
+
this.popup.destroy();
|
|
7129
|
+
}
|
|
7130
|
+
};
|
|
7131
|
+
|
|
6825
7132
|
// src/view/dom.ts
|
|
6826
7133
|
var BLOCK_ATTR = "data-block-id";
|
|
6827
7134
|
function codeViewName(language) {
|
|
@@ -8115,186 +8422,6 @@ function setIcon(button, editor, name) {
|
|
|
8115
8422
|
button.replaceChildren(...icon ? [icon] : []);
|
|
8116
8423
|
}
|
|
8117
8424
|
|
|
8118
|
-
// src/ui/resizeHandles.ts
|
|
8119
|
-
var MIN_WIDTH = 24;
|
|
8120
|
-
var SNAP = 8;
|
|
8121
|
-
var ResizeHandles = class {
|
|
8122
|
-
constructor(editor) {
|
|
8123
|
-
this.editor = editor;
|
|
8124
|
-
const root = editor.root;
|
|
8125
|
-
const doc = root.ownerDocument;
|
|
8126
|
-
const labels = editor.labels;
|
|
8127
|
-
this.frame = doc.createElement("div");
|
|
8128
|
-
this.frame.className = "mbe-ui mbe-image-frame";
|
|
8129
|
-
this.frame.hidden = true;
|
|
8130
|
-
const resizeHandle = (side) => {
|
|
8131
|
-
const el = doc.createElement("div");
|
|
8132
|
-
el.className = `mbe-resize-handle mbe-resize-handle-${side}`;
|
|
8133
|
-
el.dataset.side = side;
|
|
8134
|
-
el.title = labels.resizeImage;
|
|
8135
|
-
el.setAttribute("aria-hidden", "true");
|
|
8136
|
-
this.frame.appendChild(el);
|
|
8137
|
-
return el;
|
|
8138
|
-
};
|
|
8139
|
-
this.handles = { left: resizeHandle("left"), right: resizeHandle("right") };
|
|
8140
|
-
const on = (target, type, fn) => {
|
|
8141
|
-
target.addEventListener(type, fn);
|
|
8142
|
-
this.cleanup.push(() => target.removeEventListener(type, fn));
|
|
8143
|
-
};
|
|
8144
|
-
on(root, "mouseover", (e) => this.onOver(e));
|
|
8145
|
-
on(root, "mouseleave", () => this.scheduleUnhover());
|
|
8146
|
-
for (const el of [this.frame, this.handles.left, this.handles.right]) {
|
|
8147
|
-
on(el, "mouseenter", () => this.cancelUnhover());
|
|
8148
|
-
on(el, "mouseleave", () => this.scheduleUnhover());
|
|
8149
|
-
}
|
|
8150
|
-
on(this.frame, "mousedown", (e) => e.preventDefault());
|
|
8151
|
-
for (const side of ["left", "right"]) on(this.handles[side], "pointerdown", (e) => this.startDrag(e, side));
|
|
8152
|
-
on(this.frame, "pointermove", (e) => this.moveDrag(e));
|
|
8153
|
-
on(this.frame, "pointerup", (e) => this.endDrag(e, true));
|
|
8154
|
-
on(this.frame, "pointercancel", (e) => this.endDrag(e, false));
|
|
8155
|
-
on(this.frame, "lostpointercapture", (e) => this.endDrag(e, true));
|
|
8156
|
-
}
|
|
8157
|
-
/** A frame over the image, holding the two handles. It takes no pointer events itself. */
|
|
8158
|
-
frame;
|
|
8159
|
-
handles;
|
|
8160
|
-
/** The image under the pointer, which shows handles while it is hovered. */
|
|
8161
|
-
hovered = null;
|
|
8162
|
-
/** The selected image, which shows handles. */
|
|
8163
|
-
selected = null;
|
|
8164
|
-
/** The image the frame is shown for. */
|
|
8165
|
-
framed = null;
|
|
8166
|
-
drag = null;
|
|
8167
|
-
hideTimer = null;
|
|
8168
|
-
cleanup = [];
|
|
8169
|
-
/** True when `node` is the frame or one of its resize handles. */
|
|
8170
|
-
contains(node) {
|
|
8171
|
-
return this.frame.contains(node);
|
|
8172
|
-
}
|
|
8173
|
-
/** Shows the handles for the selected image, or for none. */
|
|
8174
|
-
update(selected) {
|
|
8175
|
-
this.selected = selected;
|
|
8176
|
-
if (!this.drag) this.placeFrame();
|
|
8177
|
-
}
|
|
8178
|
-
/** Moves the resize handles to where their image is now, after scrolling. */
|
|
8179
|
-
reposition() {
|
|
8180
|
-
if (this.drag) return;
|
|
8181
|
-
if (this.selected && !this.selected.el.isConnected) this.selected = null;
|
|
8182
|
-
this.placeFrame();
|
|
8183
|
-
}
|
|
8184
|
-
hide() {
|
|
8185
|
-
if (this.drag) return;
|
|
8186
|
-
this.hovered = null;
|
|
8187
|
-
this.selected = null;
|
|
8188
|
-
this.placeFrame();
|
|
8189
|
-
}
|
|
8190
|
-
destroy() {
|
|
8191
|
-
this.cancelUnhover();
|
|
8192
|
-
for (const fn of this.cleanup) fn();
|
|
8193
|
-
this.frame.remove();
|
|
8194
|
-
}
|
|
8195
|
-
// -- Hover ----------------------------------------------------------------
|
|
8196
|
-
onOver(e) {
|
|
8197
|
-
if (this.drag || !this.editor.isEditable) return;
|
|
8198
|
-
const target = e.target;
|
|
8199
|
-
const img = target instanceof HTMLImageElement && target.classList.contains("mbe-image") ? target : null;
|
|
8200
|
-
if (img) {
|
|
8201
|
-
this.cancelUnhover();
|
|
8202
|
-
this.hovered = img;
|
|
8203
|
-
this.placeFrame();
|
|
8204
|
-
} else if (this.hovered) {
|
|
8205
|
-
this.scheduleUnhover();
|
|
8206
|
-
}
|
|
8207
|
-
}
|
|
8208
|
-
scheduleUnhover() {
|
|
8209
|
-
this.cancelUnhover();
|
|
8210
|
-
this.hideTimer = setTimeout(() => {
|
|
8211
|
-
this.hideTimer = null;
|
|
8212
|
-
this.hovered = null;
|
|
8213
|
-
this.placeFrame();
|
|
8214
|
-
}, 250);
|
|
8215
|
-
}
|
|
8216
|
-
cancelUnhover() {
|
|
8217
|
-
if (this.hideTimer !== null) clearTimeout(this.hideTimer);
|
|
8218
|
-
this.hideTimer = null;
|
|
8219
|
-
}
|
|
8220
|
-
/** Shows the handles over the hovered image, else the selected one. Only images that loaded can be resized. */
|
|
8221
|
-
placeFrame() {
|
|
8222
|
-
const editable = this.editor.isEditable;
|
|
8223
|
-
const hovered = this.hovered?.isConnected ? this.hovered : null;
|
|
8224
|
-
const el = this.drag?.el ?? (editable ? hovered ?? this.selected?.el ?? null : null);
|
|
8225
|
-
const img = el instanceof HTMLImageElement && el.isConnected ? el : null;
|
|
8226
|
-
const rect = img?.getBoundingClientRect();
|
|
8227
|
-
if (!img || !rect || rect.width === 0 || rect.height === 0) {
|
|
8228
|
-
this.frame.hidden = true;
|
|
8229
|
-
this.framed = null;
|
|
8230
|
-
return;
|
|
8231
|
-
}
|
|
8232
|
-
if (this.framed !== img) {
|
|
8233
|
-
this.framed = img;
|
|
8234
|
-
if (!img.complete) img.addEventListener("load", () => this.framed === img && this.placeFrame(), { once: true });
|
|
8235
|
-
}
|
|
8236
|
-
this.frame.hidden = false;
|
|
8237
|
-
const offset = attach(this.frame, this.editor.root);
|
|
8238
|
-
this.frame.style.left = `${Math.round(rect.left - offset.x)}px`;
|
|
8239
|
-
this.frame.style.top = `${Math.round(rect.top - offset.y)}px`;
|
|
8240
|
-
this.frame.style.width = `${Math.round(rect.width)}px`;
|
|
8241
|
-
this.frame.style.height = `${Math.round(rect.height)}px`;
|
|
8242
|
-
this.frame.classList.toggle("mbe-image-frame-small", rect.width < 64 || rect.height < 40);
|
|
8243
|
-
}
|
|
8244
|
-
// -- Resizing -------------------------------------------------------------
|
|
8245
|
-
startDrag(e, side) {
|
|
8246
|
-
const el = this.framed;
|
|
8247
|
-
if (!el || this.drag || e.button !== 0 || !this.editor.isEditable) return;
|
|
8248
|
-
const target = this.editor.imageTarget(el);
|
|
8249
|
-
if (!target) return;
|
|
8250
|
-
e.preventDefault();
|
|
8251
|
-
this.handles[side].setPointerCapture?.(e.pointerId);
|
|
8252
|
-
const width = el.getBoundingClientRect().width;
|
|
8253
|
-
const others = Array.from(this.editor.root.querySelectorAll("img.mbe-image")).filter((other) => other !== el).map((other) => other.getBoundingClientRect().width).filter((w) => w > 0);
|
|
8254
|
-
const max = Math.max(MIN_WIDTH, columnWidth(el));
|
|
8255
|
-
const factor = el.closest(".mbe-image-block")?.getAttribute("data-align") === "center" ? 2 : 1;
|
|
8256
|
-
this.drag = { el, side, pointer: e.pointerId, startX: e.clientX, startWidth: width, max, snaps: [...others, max], width, factor };
|
|
8257
|
-
this.frame.classList.add("mbe-image-resizing");
|
|
8258
|
-
this.editor.selectImageAt(target.pos);
|
|
8259
|
-
}
|
|
8260
|
-
moveDrag(e) {
|
|
8261
|
-
const drag = this.drag;
|
|
8262
|
-
if (!drag || e.pointerId !== drag.pointer) return;
|
|
8263
|
-
e.preventDefault();
|
|
8264
|
-
const dx = (e.clientX - drag.startX) * (drag.side === "right" ? 1 : -1) * drag.factor;
|
|
8265
|
-
let width = Math.min(drag.max, Math.max(MIN_WIDTH, drag.startWidth + dx));
|
|
8266
|
-
const snap = drag.snaps.find((w) => Math.abs(w - width) <= SNAP);
|
|
8267
|
-
if (snap !== void 0) width = Math.min(drag.max, snap);
|
|
8268
|
-
drag.width = Math.round(width);
|
|
8269
|
-
drag.el.style.width = `${drag.width}px`;
|
|
8270
|
-
drag.el.style.height = "auto";
|
|
8271
|
-
this.placeFrame();
|
|
8272
|
-
}
|
|
8273
|
-
endDrag(e, commit) {
|
|
8274
|
-
const drag = this.drag;
|
|
8275
|
-
if (!drag || e.pointerId !== drag.pointer) return;
|
|
8276
|
-
this.drag = null;
|
|
8277
|
-
this.frame.classList.remove("mbe-image-resizing");
|
|
8278
|
-
this.handles[drag.side].releasePointerCapture?.(e.pointerId);
|
|
8279
|
-
drag.el.style.width = "";
|
|
8280
|
-
drag.el.style.height = "";
|
|
8281
|
-
const target = drag.el.isConnected ? this.editor.imageTarget(drag.el) : null;
|
|
8282
|
-
if (commit && target && Math.round(drag.width) !== Math.round(drag.startWidth)) {
|
|
8283
|
-
this.editor.updateImageAt(target.pos, { width: drag.width });
|
|
8284
|
-
} else {
|
|
8285
|
-
this.editor.refreshImageUi();
|
|
8286
|
-
}
|
|
8287
|
-
}
|
|
8288
|
-
};
|
|
8289
|
-
function columnWidth(el) {
|
|
8290
|
-
let box = el.parentElement;
|
|
8291
|
-
const view = el.ownerDocument.defaultView;
|
|
8292
|
-
while (box && view.getComputedStyle(box).display.startsWith("inline")) box = box.parentElement;
|
|
8293
|
-
if (!box) return el.getBoundingClientRect().width;
|
|
8294
|
-
const cs = view.getComputedStyle(box);
|
|
8295
|
-
return box.clientWidth - parseFloat(cs.paddingLeft || "0") - parseFloat(cs.paddingRight || "0");
|
|
8296
|
-
}
|
|
8297
|
-
|
|
8298
8425
|
// src/view/reveal.ts
|
|
8299
8426
|
function revealedViews(state, views, active) {
|
|
8300
8427
|
const out = /* @__PURE__ */ new Map();
|
|
@@ -8710,7 +8837,7 @@ var Editor = class {
|
|
|
8710
8837
|
focused = false;
|
|
8711
8838
|
/** A mouse button is down in the editor. Rendered views keep their state until it is released, so a drag is not disturbed. */
|
|
8712
8839
|
pointerDown = false;
|
|
8713
|
-
/** The selection the formatting toolbar was closed for with Escape. It stays closed until the selection changes. */
|
|
8840
|
+
/** The selection the formatting or image toolbar was closed for with Escape. It stays closed until the selection changes. */
|
|
8714
8841
|
toolbarClosedFor = null;
|
|
8715
8842
|
/** Handling a key press, typed text, a paste or a drop: changes then scroll the caret into view. */
|
|
8716
8843
|
typing = 0;
|
|
@@ -8724,6 +8851,7 @@ var Editor = class {
|
|
|
8724
8851
|
suggestions;
|
|
8725
8852
|
blockMenu;
|
|
8726
8853
|
toolbar;
|
|
8854
|
+
imageToolbar;
|
|
8727
8855
|
/** The link popover, and the field for an image's alt text. */
|
|
8728
8856
|
inputPopover;
|
|
8729
8857
|
blockHandle;
|
|
@@ -8838,6 +8966,13 @@ var Editor = class {
|
|
|
8838
8966
|
this.inputPopover = new InputPopover(this);
|
|
8839
8967
|
this.toolbar = options.toolbar === false ? null : new FormatToolbar(this, typeof options.toolbar === "object" ? options.toolbar.items : void 0);
|
|
8840
8968
|
if (this.toolbar) this.toolbar.onMenuChange = () => this.syncActiveDescendant();
|
|
8969
|
+
this.imageToolbar = options.toolbar === false ? null : new ImageToolbar(this, {
|
|
8970
|
+
// An image without an alignment sits on the left, so Left removes the word.
|
|
8971
|
+
align: (target, align) => this.updateImageAt(target.pos, { align: align === "left" ? null : align }),
|
|
8972
|
+
altText: (target) => this.editAltText(target.pos, target.image.alt),
|
|
8973
|
+
resetSize: (target) => this.updateImageAt(target.pos, { width: null }),
|
|
8974
|
+
remove: (target) => this.deleteImageAt(target.pos)
|
|
8975
|
+
});
|
|
8841
8976
|
this.blockHandle = new BlockHandle(this);
|
|
8842
8977
|
this.linkCard = options.linkCard === false ? null : new LinkCard(this, {
|
|
8843
8978
|
open: (target, event) => this.openHref(target.el.getAttribute("href"), event),
|
|
@@ -9498,6 +9633,7 @@ var Editor = class {
|
|
|
9498
9633
|
for (const widget of [...this.widgets]) widget.remove();
|
|
9499
9634
|
this.blockMenu.destroy();
|
|
9500
9635
|
this.toolbar?.destroy();
|
|
9636
|
+
this.imageToolbar?.destroy();
|
|
9501
9637
|
this.inputPopover.destroy();
|
|
9502
9638
|
this.blockHandle.destroy();
|
|
9503
9639
|
this.linkCard?.destroy();
|
|
@@ -10127,6 +10263,7 @@ var Editor = class {
|
|
|
10127
10263
|
this.composing = true;
|
|
10128
10264
|
this.composition = { blockId: this.state.selection.focus.blockId, marks: this.state.storedMarks, recorded: false };
|
|
10129
10265
|
this.toolbar?.hide();
|
|
10266
|
+
this.imageToolbar?.hide();
|
|
10130
10267
|
}
|
|
10131
10268
|
onCompositionEnd(e) {
|
|
10132
10269
|
if (this.fromControl(e)) return;
|
|
@@ -10302,10 +10439,11 @@ var Editor = class {
|
|
|
10302
10439
|
if (e.key === "Enter" && menu.count > 0) return handled(() => menu.pick());
|
|
10303
10440
|
if (e.key === "Escape") return handled(() => menu === this.blockMenu ? this.closeBlockMenu() : this.toolbar.closeMenu());
|
|
10304
10441
|
}
|
|
10305
|
-
if (e.key === "Escape" && this.toolbar?.isOpen) {
|
|
10442
|
+
if (e.key === "Escape" && (this.toolbar?.isOpen || this.imageToolbar?.isOpen)) {
|
|
10306
10443
|
return handled(() => {
|
|
10307
10444
|
this.toolbarClosedFor = this.state.selection;
|
|
10308
|
-
this.toolbar
|
|
10445
|
+
this.toolbar?.hide();
|
|
10446
|
+
this.imageToolbar?.hide();
|
|
10309
10447
|
});
|
|
10310
10448
|
}
|
|
10311
10449
|
if (this.readOnly) return;
|
|
@@ -10598,7 +10736,7 @@ var Editor = class {
|
|
|
10598
10736
|
this.blockHandle.hide();
|
|
10599
10737
|
this.linkCard?.reposition();
|
|
10600
10738
|
this.resizeHandles.reposition();
|
|
10601
|
-
if (this.suggestions.isActive || this.toolbar?.isOpen || this.blockMenu.isOpen) this.updateUi();
|
|
10739
|
+
if (this.suggestions.isActive || this.toolbar?.isOpen || this.imageToolbar?.isOpen || this.blockMenu.isOpen) this.updateUi();
|
|
10602
10740
|
}
|
|
10603
10741
|
// -------------------------------------------------------------------------
|
|
10604
10742
|
// Clipboard
|
|
@@ -10794,6 +10932,7 @@ var Editor = class {
|
|
|
10794
10932
|
this.history.breakGroup();
|
|
10795
10933
|
this.suggestions.close();
|
|
10796
10934
|
this.toolbar?.hide();
|
|
10935
|
+
this.imageToolbar?.hide();
|
|
10797
10936
|
this.blockMenu.hide();
|
|
10798
10937
|
this.refreshRevealed();
|
|
10799
10938
|
this.paintBlockSelection();
|
|
@@ -11032,6 +11171,7 @@ var Editor = class {
|
|
|
11032
11171
|
this.dropBlockSelection();
|
|
11033
11172
|
this.suggestions.close();
|
|
11034
11173
|
this.toolbar?.hide();
|
|
11174
|
+
this.imageToolbar?.hide();
|
|
11035
11175
|
const imageOffset = imageBlockOffset(getBlock(this.state.doc, blockId));
|
|
11036
11176
|
if (imageOffset !== null) {
|
|
11037
11177
|
this.current = selectImage(this.state, { blockId, offset: imageOffset });
|
|
@@ -11059,7 +11199,7 @@ var Editor = class {
|
|
|
11059
11199
|
}
|
|
11060
11200
|
closeBlockMenu() {
|
|
11061
11201
|
this.blockMenu.hide();
|
|
11062
|
-
this.
|
|
11202
|
+
this.updateUi();
|
|
11063
11203
|
}
|
|
11064
11204
|
/**
|
|
11065
11205
|
* A press anywhere but the block menu or the block handle closes the block menu,
|
|
@@ -11119,10 +11259,15 @@ var Editor = class {
|
|
|
11119
11259
|
const image = focused && !this.readOnly && !this.blockSel ? this.imageTargetAt(this.state) : null;
|
|
11120
11260
|
this.paintSelectedImage(image?.el ?? null);
|
|
11121
11261
|
this.resizeHandles.update(image);
|
|
11262
|
+
const closed = this.toolbarClosedFor;
|
|
11263
|
+
if (closed && !(samePosition(closed.anchor, this.state.selection.anchor) && samePosition(closed.focus, pos))) this.toolbarClosedFor = null;
|
|
11264
|
+
if (this.imageToolbar) {
|
|
11265
|
+
const show = image && !this.toolbarClosedFor && !this.composing && !this.suggestions.isActive && !this.blockMenu.isOpen && !this.inputPopover.isOpen && !this.resizeHandles.isDragging;
|
|
11266
|
+
if (show) this.imageToolbar.show(image, selectedImageBlock(this.state) !== null);
|
|
11267
|
+
else this.imageToolbar.hide();
|
|
11268
|
+
}
|
|
11122
11269
|
if (this.toolbar) {
|
|
11123
11270
|
const block3 = this.state.doc.blocks.find((b) => b.id === pos.blockId);
|
|
11124
|
-
const closed = this.toolbarClosedFor;
|
|
11125
|
-
if (closed && !(samePosition(closed.anchor, this.state.selection.anchor) && samePosition(closed.focus, pos))) this.toolbarClosedFor = null;
|
|
11126
11271
|
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";
|
|
11127
11272
|
if (show) this.toolbar.show();
|
|
11128
11273
|
else this.toolbar.hide();
|
|
@@ -11294,12 +11439,14 @@ var Editor = class {
|
|
|
11294
11439
|
this.run((s) => updateImage(s, pos, change));
|
|
11295
11440
|
}
|
|
11296
11441
|
/**
|
|
11442
|
+
* @internal
|
|
11297
11443
|
* Opens the alt text field under the image after `pos`. Enter saves the text,
|
|
11298
11444
|
* Escape keeps the old one. Either way the image stays selected.
|
|
11299
11445
|
*/
|
|
11300
11446
|
editAltText(pos, alt) {
|
|
11301
11447
|
const rect = (this.imageTargetAt(this.state)?.el ?? this.blockElement(pos.blockId))?.getBoundingClientRect();
|
|
11302
11448
|
if (!rect) return;
|
|
11449
|
+
this.imageToolbar?.hide();
|
|
11303
11450
|
const { labels } = this;
|
|
11304
11451
|
const options = { value: alt, label: labels.altText, placeholder: labels.altTextPlaceholder, type: "text" };
|
|
11305
11452
|
this.inputPopover.open(rect, options, (value) => {
|
|
@@ -11308,6 +11455,18 @@ var Editor = class {
|
|
|
11308
11455
|
this.focus();
|
|
11309
11456
|
});
|
|
11310
11457
|
}
|
|
11458
|
+
/**
|
|
11459
|
+
* @internal
|
|
11460
|
+
* Deletes the image after `pos`: the whole block of an image block, as
|
|
11461
|
+
* Backspace on the selected image does, else the image alone.
|
|
11462
|
+
*/
|
|
11463
|
+
deleteImageAt(pos) {
|
|
11464
|
+
this.run((s) => {
|
|
11465
|
+
const selected = selectImage(s, pos);
|
|
11466
|
+
if (selected === s) return null;
|
|
11467
|
+
return selectedImageBlock(selected) ? deleteBlock(selected, pos.blockId) : deleteSelection(selected);
|
|
11468
|
+
});
|
|
11469
|
+
}
|
|
11311
11470
|
/** @internal Shows the resize handles again, after a resize that changed nothing. */
|
|
11312
11471
|
refreshImageUi() {
|
|
11313
11472
|
this.updateUi();
|
|
@@ -11323,6 +11482,7 @@ var Editor = class {
|
|
|
11323
11482
|
this.suggestions.close();
|
|
11324
11483
|
this.blockMenu.hide();
|
|
11325
11484
|
this.toolbar?.hide();
|
|
11485
|
+
this.imageToolbar?.hide();
|
|
11326
11486
|
this.blockHandle.hide();
|
|
11327
11487
|
this.linkCard?.setCaret(null);
|
|
11328
11488
|
this.resizeHandles.hide();
|