From 5b1de90abaee46e971a0ff515c5047bf5f1a42cc Mon Sep 17 00:00:00 2001 From: wangtiance Date: Sat, 5 Dec 2026 12:25:01 +0800 Subject: [PATCH] [bugfix] Keep selected canvas node titles clear of the toolbar --- src/canvas/selectionPresentation.ts | 13 ++++++-- tests/browser/canvas_toolbar_topbar.mjs | 36 +++++++++++++++++++++++ tests/canvasSelectionPresentation.test.ts | 17 ++++++++++- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/canvas/selectionPresentation.ts b/src/canvas/selectionPresentation.ts index 9660c974b..d7df09a32 100644 --- a/src/canvas/selectionPresentation.ts +++ b/src/canvas/selectionPresentation.ts @@ -14,6 +14,7 @@ export type SelectionChrome = { export const SELECTION_TITLE_SPACE = 32; const TITLE_SPACE = SELECTION_TITLE_SPACE; +const NODE_TOP_CONTROLS_SPACE = 40; const TOOLBAR_GAP = 8; const COMPOSER_GAP = 16; const clamp = (value: number, min: number, max: number) => @@ -209,12 +210,18 @@ export function selectionPlacement( const bottom = top + projected.height; const left = (width: number) => clamp(center - width / 2, safe.x, safe.x + safe.width - width); + const toolbarTop = Math.max(safe.y, top - TITLE_SPACE - TOOLBAR_GAP - toolbar.height); + const headingTop = top - TITLE_SPACE * view.zoom; + const controlsBottom = top + NODE_TOP_CONTROLS_SPACE * view.zoom; + const coversNodeControls = toolbar.height > 0 && toolbarTop === safe.y && + toolbarTop < controlsBottom && toolbarTop + toolbar.height > headingTop; + const belowNode = bottom + TOOLBAR_GAP; + const fitsBelowNode = composer.height === 0 && belowNode + toolbar.height <= safe.y + safe.height; + const fallbackTop = fitsBelowNode ? belowNode : controlsBottom + TOOLBAR_GAP; return { toolbar: { left: left(toolbar.width), - // Never above the safe area: the top bar's global controls sit there and - // the toolbar would intercept their clicks when the node is near the top. - top: Math.max(safe.y, top - TITLE_SPACE - TOOLBAR_GAP - toolbar.height), + top: coversNodeControls ? fallbackTop : toolbarTop, }, composer: { left: scaleComposer diff --git a/tests/browser/canvas_toolbar_topbar.mjs b/tests/browser/canvas_toolbar_topbar.mjs index c1e85e66d..f0d860d5e 100644 --- a/tests/browser/canvas_toolbar_topbar.mjs +++ b/tests/browser/canvas_toolbar_topbar.mjs @@ -155,6 +155,42 @@ async function verify(browser, locale, zoom) { assert.deepEqual(await steadyViewport(), [afterPan], "The pan is not undone by automatic fitting"); await expectClear("after manual pan"); + if (zoom === 1) { + const title = node(fixture.second).locator(".cw-node-title"); + const titleBox = await title.boundingBox(); + const workspace = await page.locator(".cw-workspace").boundingBox(); + const offset = workspace.y + 90 - (titleBox.y + titleBox.height / 2); + await page.mouse.move(750, 700); + await page.mouse.down({ button: "middle" }); + await page.mouse.move(750, 700 + offset, { steps: 6 }); + await page.mouse.up({ button: "middle" }); + assert.equal((await steadyViewport()).length, 1, "The rename position stays stable"); + await expectClear("near the upper canvas edge"); + const hit = await title.evaluate((element) => { + const bounds = element.getBoundingClientRect(); + const target = document.elementFromPoint( + bounds.x + bounds.width / 2, bounds.y + bounds.height / 2, + ); + return { + title: element.contains(target), + target: target?.className, + titleBounds: bounds.toJSON(), + toolbarBounds: document.querySelector(".cw-node-toolbar-anchor").getBoundingClientRect().toJSON(), + }; + }); + if (locale === "en-US" && process.env.CANVAS_TOOLBAR_RENAME_SCREENSHOT) + await page.screenshot({ path: process.env.CANVAS_TOOLBAR_RENAME_SCREENSHOT }); + assert.ok(hit.title, `The node title receives the pointer: ${JSON.stringify(hit)}`); + await title.dblclick({ timeout: 5000 }); + const input = node(fixture.second).locator(".cw-node-title-input"); + await input.waitFor(); + await input.press("Escape"); + await input.waitFor({ state: "detached" }); + await presets.click({ timeout: 5000 }); + assert.equal(await presets.getAttribute("aria-expanded"), "true"); + await page.keyboard.press("Escape"); + } + // 4. Switching away and back restores the canvas. await picker.click({ timeout: 5000 }); await page.getByRole("menuitemradio", { name: fixture.b.name, exact: true }).click(); diff --git a/tests/canvasSelectionPresentation.test.ts b/tests/canvasSelectionPresentation.test.ts index 5598790af..d247fa02f 100644 --- a/tests/canvasSelectionPresentation.test.ts +++ b/tests/canvasSelectionPresentation.test.ts @@ -111,7 +111,7 @@ test("toolbar never rises above the safe area into the top bar (#513)", () => { { x: 0, y: 0, zoom: 1.5 }, ]) { const placed = selectionPlacement(node, viewport, chrome); - assert.equal(placed.toolbar.top, chrome.safe.y, JSON.stringify(viewport)); + assert.ok(placed.toolbar.top >= chrome.safe.y, JSON.stringify(viewport)); assert.equal(placed.visible, true); } // A fitted selection keeps the ordinary anchored position above the node. @@ -126,6 +126,21 @@ test("toolbar never rises above the safe area into the top bar (#513)", () => { ); }); +test("a clamped toolbar leaves the selected node title and history control reachable (#693)", () => { + const node = { x: 240, y: 118, width: 300, height: 300 }; + const chrome: SelectionChrome = { + safe: { x: 80, y: 72, width: 1100, height: 850 }, + toolbar: { width: 500, height: 36 }, + composer: { width: 0, height: 0 }, + }; + const view = { x: 0, y: 0, zoom: 1 }; + assert.ok(selectionPlacement(node, view, chrome).toolbar.top > node.y + node.height); + const withComposer = { ...chrome, composer: { width: 500, height: 300 } }; + const placement = selectionPlacement(node, { ...view, y: -50 }, withComposer); + assert.ok(placement.toolbar.top >= node.y - 50 + 40); + assert.ok(placement.toolbar.top + withComposer.toolbar.height < placement.composer.top); +}); + test("fitting a distant node preserves its screen center while reducing zoom", () => { const node = { x: 10000, y: 8000, width: 600, height: 600 }; const view = { x: 640 - 10300, y: 200 - 8000, zoom: 1 };