diff --git a/docs/testing-evidence/embed-controls-2026-11-06.md b/docs/testing-evidence/embed-controls-2026-11-06.md index 01a6d684..783c97a3 100644 --- a/docs/testing-evidence/embed-controls-2026-11-06.md +++ b/docs/testing-evidence/embed-controls-2026-11-06.md @@ -26,6 +26,12 @@ titles, aliases, style suffixes, whitespace and escaped table separators remain unchanged. Existing wrapper layout remains responsible for alignment and percentage sizing. Controls follow the visible image bounds. +Cancellation restores control placement immediately; scrolling and viewport +changes remeasure it. Hover toolbars touch the image boundary so pointer travel +does not cross a disappearing hover gap. Always-visible touch toolbars occupy +normal document flow below the image. A full-width no-op keeps percentage syntax +instead of shrinking solely to create space for the handle. + Fullscreen uses the existing exporter with the same drawing/reference and theme, but bypasses the shared image cache. It owns and releases that fresh image. SVG links are inactive in the viewer. Closing a source owner closes its viewers and diff --git a/scripts/testing/embedControls.source.test.mjs b/scripts/testing/embedControls.source.test.mjs index 355b6240..ba077f4d 100644 --- a/scripts/testing/embedControls.source.test.mjs +++ b/scripts/testing/embedControls.source.test.mjs @@ -67,3 +67,35 @@ test("keyboard resizing commits once on release, keeps shortcuts, and reset pres } finally { f.close(); } assert.equal(f.modals[0].active, false); }); + +test("cancel and viewport changes restore controls to the visible image corner", async () => { + const f = await fixture(); + try { + const originalFrame = { ...f.frame.bounds }, originalImage = { ...f.image.bounds }; + f.frame.getBoundingClientRect = () => f.frame.hasClass("excalidraw-embed-resizing") ? { ...originalFrame, right: 220, width: 220, bottom: 159, height: 159 } : originalFrame; + f.image.getBoundingClientRect = () => f.frame.hasClass("excalidraw-embed-resizing") ? { ...originalImage, right: 220, width: 220, bottom: 159, height: 159 } : originalImage; + start(f); f.fire(f.window, "pointermove", { pointerId: 1, clientX: 220, clientY: 130 }); + assert.equal(f.frame.style["--excalidraw-embed-control-right"], "-8px"); + f.fire(f.window, "keydown", { key: "Escape" }); + assert.equal(f.frame.style["--excalidraw-embed-control-right"], "512px"); assert.deepEqual(f.writes, []); + originalImage.left = 50; originalImage.right = 230; f.fire(f.window, "resize"); + assert.equal(f.frame.style["--excalidraw-embed-control-left"], "50px"); + assert.equal(f.frame.style["--excalidraw-embed-control-right"], "462px"); + } finally { f.close(); } +}); + +test("a full-width no-op retains percentage syntax and a replacement gesture uses restored dimensions", async () => { + const f = await fixture("![[Map|100%]]"); + try { + f.frame.parentElement.bounds.right = 180; + start(f); f.fire(f.window, "pointermove", { pointerId: 1, clientX: 200, clientY: 130 }); + f.fire(f.window, "pointerup", { pointerId: 1 }); assert.deepEqual(f.writes, []); + f.frame.parentElement.bounds.right = 700; + const original = { ...f.image.bounds }; + f.image.getBoundingClientRect = () => f.frame.hasClass("excalidraw-embed-resizing") ? { ...original, width: Number.parseFloat(f.frame.style["--excalidraw-embed-resize-width"]), height: Number.parseFloat(f.frame.style["--excalidraw-embed-resize-height"]) } : original; + start(f); f.fire(f.window, "pointermove", { pointerId: 1, clientX: 220, clientY: 130 }); + f.handle.fire("pointerdown", { button: 0, pointerId: 2, clientX: 180, clientY: 130 }); + f.fire(f.window, "pointermove", { pointerId: 2, clientX: 188, clientY: 130 }); + f.fire(f.window, "pointerup", { pointerId: 2 }); assert.deepEqual(f.writes, ["![[Map|188]]"]); + } finally { f.close(); } +}); diff --git a/scripts/testing/host/embedControls.mjs b/scripts/testing/host/embedControls.mjs index 5bf8144d..240d410a 100644 --- a/scripts/testing/host/embedControls.mjs +++ b/scripts/testing/host/embedControls.mjs @@ -93,6 +93,10 @@ async function exerciseEmbedControls(c, env, template, modifier) { p.settings.previewImageType = mode; await open(); if (mainWindow) { mainWindow.minimize(); browserWindow.focus(); check(mainWindow.isMinimized(), "main window was not minimized for popout input"); } + if (touch) { + const frame = frames()[0], image = frame.querySelector(".excalidraw-embedded-img").getBoundingClientRect(), toolbar = frame.querySelector(".excalidraw-embed-toolbar").getBoundingClientRect(); + check(toolbar.top >= image.bottom && toolbar.bottom <= frame.getBoundingClientRect().bottom + 1, "touch toolbar overlaps image or following note content"); + } let cm = leaf.view.editor.cm; const changed = content.replace(token + "\n\n" + token, token + "\n\n" + token.replace("|180", "|220")); await drag(1, 40); @@ -112,6 +116,9 @@ async function exerciseEmbedControls(c, env, template, modifier) { await c.waitFor(() => frames().length === 3, "restored embeds"); await drag(1, 35, true); check(cm.state.doc.toString() === content && !frames()[1].hasClass("excalidraw-embed-resizing"), "Escape committed or retained preview"); + const cancelledFrame = frames()[1], cancelledImage = cancelledFrame.querySelector(".excalidraw-embedded-img"); + const imageCorner = (cancelledImage.querySelector("svg") ?? cancelledImage).getBoundingClientRect(), handleCorner = cancelledFrame.querySelector('[data-excalidraw-embed-action="resize"]').getBoundingClientRect(); + check(Math.abs(handleCorner.right - imageCorner.right - 8) < 2 && Math.abs(handleCorner.bottom - imageCorner.bottom - 8) < 2, "cancel left the resize handle away from the visible corner"); frames()[1].querySelector('[data-excalidraw-embed-action="resize"]').focus(); await key("ArrowRight", "ArrowRight", 39); await c.waitFor(() => cm.state.doc.toString() === content.replace(token + "\n\n" + token, token + "\n\n" + token.replace("|180", "|188")), "keyboard resize commits on key release"); diff --git a/src/core/editor/drawingEmbedExtension.ts b/src/core/editor/drawingEmbedExtension.ts index 915fb701..acb0a0fa 100644 --- a/src/core/editor/drawingEmbedExtension.ts +++ b/src/core/editor/drawingEmbedExtension.ts @@ -99,13 +99,13 @@ export function createDrawingEmbedExtension(plugin: ExcalidrawPlugin): { extensi const path = hash < 0 ? target : target.slice(0, hash), reference = hash < 0 ? "" : target.slice(hash); const drawing = plugin.app.metadataCache.getFirstLinkpathDest(path, sourceFile.path); if (!drawing || !plugin.isExcalidrawFile(drawing) || decodeTarget(drawing.path + reference) !== decodeTarget(actions.target)) continue; - const sourcePath = sourceFile.path; + const sourcePath = sourceFile.path, ownerDocument = view.dom.ownerDocument; const source = new DrawingEmbedSource(view, from, from + token.text.length, token.text, () => this.active && !disposed && sourceFile.path === sourcePath && view.state.field(editorInfoField, false)?.file === sourceFile && !!view.state.field(editorLivePreviewField, false) && !view.state.facet(EditorState.readOnly) && view.state.facet(EditorView.editable) && frame.isConnected && view.contentDOM.contains(frame) && nativeEmbed.contains(frame) && - frame.ownerDocument === view.dom.ownerDocument && actions.isCurrent()); + frame.ownerDocument === ownerDocument && view.dom.ownerDocument === ownerDocument && actions.isCurrent()); // A renderer may invalidate its generation before replacing the DOM. // Do not mount a stale binding: mounting/unloading it would schedule // another DOM sync and starve the renderer's debounce timer. diff --git a/src/core/managers/DrawingEmbedControls.ts b/src/core/managers/DrawingEmbedControls.ts index 6cc5492b..ed4dbd99 100644 --- a/src/core/managers/DrawingEmbedControls.ts +++ b/src/core/managers/DrawingEmbedControls.ts @@ -73,6 +73,9 @@ export class DrawingEmbedControls extends MarkdownRenderChild { this.register(() => observer.disconnect()); this.registerDomEvent(frame, "pointerenter", () => this.placeToolbar()); this.registerDomEvent(frame, "focusin", () => this.placeToolbar()); + this.registerDomEvent(win, "resize", () => this.placeToolbar()); + const scroller = frame.closest(".cm-scroller, .markdown-preview-view"); + if (scroller) this.registerDomEvent(scroller, "scroll", () => this.placeToolbar(), { passive: true }); this.refresh(); } @@ -96,6 +99,7 @@ export class DrawingEmbedControls extends MarkdownRenderChild { private placeToolbar(): void { const frame = this.containerEl, bounds = frame.getBoundingClientRect(); + if (!this.active || !frame.isConnected) return; const visual = this.visual?.getBoundingClientRect() ?? bounds; const editor = frame.closest(".cm-scroller"); const top = editor?.getBoundingClientRect().top ?? 0; @@ -113,11 +117,11 @@ export class DrawingEmbedControls extends MarkdownRenderChild { private start(kind: ResizeGesture["kind"], x = 0, id?: number, y = 0): boolean { if (!this.source || !this.current() || !this.visual) return false; + this.finish(false); const bounds = this.visual.getBoundingClientRect(); if (bounds.width <= 0 || bounds.height <= 0) return false; - this.finish(false); const frame = this.containerEl, content = frame.closest(".cm-content"); - const max = Math.max(1, (content?.getBoundingClientRect().right ?? frame.ownerDocument.defaultView.innerWidth) - frame.getBoundingClientRect().left - 8); + const max = Math.max(bounds.width, (content?.getBoundingClientRect().right ?? frame.ownerDocument.defaultView.innerWidth) - frame.getBoundingClientRect().left - 8); const owner = new Component(); owner.load(); this.gesture = { owner, kind, id, startX: x, startY: y, startWidth: bounds.width, width: bounds.width, aspect: bounds.width / bounds.height, max, min: Math.min(32, max) }; owner.registerDomEvent(frame.ownerDocument.defaultView, "keydown", event => { @@ -174,6 +178,7 @@ export class DrawingEmbedControls extends MarkdownRenderChild { if (gesture.id !== undefined && this.handle?.hasPointerCapture(gesture.id)) this.handle.releasePointerCapture(gesture.id); this.containerEl.removeClass("excalidraw-embed-resizing"); removeStyle(this.containerEl, ["--excalidraw-embed-resize-width", "--excalidraw-embed-resize-height"]); + this.placeToolbar(); if (!commit || !this.current() || Math.abs(gesture.width - gesture.startWidth) < 1) return; const dimensions = getEmbedDimensions(this.source.text); const text = rewriteEmbedDimensions(this.source.text, { width: gesture.width, ...(dimensions?.height ? { height: Math.max(1, Math.round(gesture.width / gesture.aspect)) } : {}) }); diff --git a/styles.css b/styles.css index 729770be..e9aa6cfa 100644 --- a/styles.css +++ b/styles.css @@ -2537,7 +2537,7 @@ button.excalidraw-font-picker { .excalidraw-embed-toolbar { position: absolute; left: var(--excalidraw-embed-control-left, 0px); - bottom: calc(100% - var(--excalidraw-embed-control-top, 0px) + 4px); + bottom: calc(100% - var(--excalidraw-embed-control-top, 0px)); z-index: 10; display: flex; flex-wrap: wrap; @@ -2554,7 +2554,7 @@ button.excalidraw-font-picker { } .excalidraw-embed-controls-below > .excalidraw-embed-toolbar { bottom: auto; - top: calc(var(--excalidraw-embed-control-below, 100%) + 4px); + top: var(--excalidraw-embed-control-below, 100%); } .excalidraw-embed-toolbar button, .excalidraw-embed-preview-toolbar button { @@ -2642,6 +2642,12 @@ button.excalidraw-font-picker { padding: 16px; } @media (hover: none), (pointer: coarse) { + .excalidraw-embed-controls-frame > .excalidraw-embed-toolbar { + position: relative; + top: auto; + bottom: auto; + margin-top: 4px; + } .excalidraw-embed-controls-frame > .excalidraw-embed-toolbar, .excalidraw-embed-controls-frame > .excalidraw-embed-resize-handle { opacity: 1; @@ -2659,3 +2665,9 @@ button.excalidraw-font-picker { opacity: 1; pointer-events: auto; } +.is-mobile .excalidraw-embed-controls-frame > .excalidraw-embed-toolbar { + position: relative; + top: auto; + bottom: auto; + margin-top: 4px; +}