From 4437f52e5b32e927ce9cd0f250e102a058ba65bc Mon Sep 17 00:00:00 2001 From: lqtdys Date: Thu, 3 Dec 2026 00:57:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(outline):=20=E9=92=89=E4=BD=8F=E5=90=8E?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E5=A4=96=E9=83=A8=E4=B8=8D=E5=86=8D=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E5=A4=A7=E7=BA=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pin 态下 onDown 直接 return,不再无条件 setPinned(false)/setHovering(false); 仅再次点击图钉或 Esc 关闭。非钉住 hover 行为不变。 fixes #1984 --- e2e/outline-pin-outside.spec.ts | 86 ++++++++++++++++++++++++++ src/components/Content/OutlineMenu.tsx | 4 +- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 e2e/outline-pin-outside.spec.ts diff --git a/e2e/outline-pin-outside.spec.ts b/e2e/outline-pin-outside.spec.ts new file mode 100644 index 00000000..2ed145a9 --- /dev/null +++ b/e2e/outline-pin-outside.spec.ts @@ -0,0 +1,86 @@ +import { test, expect, type Page } from "@playwright/test"; + +const APP_URL = "http://localhost:1424"; + +const DOC = ["# 第一章", "", "正文。", "", "## 第一节", "", "更多。", ""].join( + "\n", +); + +async function boot(page: Page): Promise { + await page.addInitScript({ path: "./e2e/mocks/tauri-mock.js" }); + await page.goto(APP_URL, { waitUntil: "networkidle" }); + await page.waitForSelector('[data-testid="pane-header"]', { timeout: 15000 }); + await page.evaluate((body) => { + window.__biaoyue_openFile__("/tmp/outline-pin.md", body, "outline-pin.md"); + window.__biaoyue_store__.getState().setEditorMode("edit"); + }, DOC); + await page.locator(".cm-content").waitFor({ timeout: 10000 }); + await page.waitForTimeout(400); +} + +/** 点击面板与按钮之外的稳定外部区域(编辑区左上角)。 */ +async function clickOutside(page: Page): Promise { + const box = await page.locator(".cm-content").boundingBox(); + if (!box) throw new Error("编辑区无边界"); + await page.mouse.click(box.x + 8, box.y + 8); +} + +test.describe("大纲钉住后点击外部不关闭(#1984)", () => { + test("钉住后点击外部保持展开;再点图钉收起", async ({ page }) => { + await boot(page); + const button = page.locator('[data-testid="outline-btn"]'); + const menu = page.locator('[data-testid="outline-menu"]'); + + // 悬停展开(未钉住)。 + await button.hover(); + await expect(menu).toBeVisible(); + + // 钉住。 + await button.click(); + await expect(button).toHaveAttribute("data-pinned", "true"); + + // 点击外部:钉住态必须保持展开(#1984 的核心断言)。 + await clickOutside(page); + await expect(menu).toBeVisible(); + await expect(button).toHaveAttribute("data-pinned", "true"); + await expect(button).toHaveAttribute("aria-expanded", "true"); + + // 指针移出浮层并等待 hover 收起延迟,使 hovering=false(pinned 仍为 true)。 + await page.mouse.move(8, 8); + await page.waitForTimeout(300); + await expect(menu).toBeVisible(); + + // 再点图钉:收起。 + await button.click(); + await expect(button).toHaveAttribute("data-pinned", "false"); + await expect(menu).toHaveCount(0); + }); + + test("钉住后 Esc 仍关闭", async ({ page }) => { + await boot(page); + const button = page.locator('[data-testid="outline-btn"]'); + const menu = page.locator('[data-testid="outline-menu"]'); + + await button.hover(); + await expect(menu).toBeVisible(); + await button.click(); + await expect(button).toHaveAttribute("data-pinned", "true"); + + await page.keyboard.press("Escape"); + await expect(menu).toHaveCount(0); + await expect(button).toHaveAttribute("data-pinned", "false"); + }); + + test("未钉住时点击外部仍收起(原行为不变)", async ({ page }) => { + await boot(page); + const button = page.locator('[data-testid="outline-btn"]'); + const menu = page.locator('[data-testid="outline-menu"]'); + + await button.hover(); + await expect(menu).toBeVisible(); + await expect(button).toHaveAttribute("data-pinned", "false"); + + await clickOutside(page); + await expect(menu).toHaveCount(0); + }); +}); diff --git a/src/components/Content/OutlineMenu.tsx b/src/components/Content/OutlineMenu.tsx index 476f479a..4a4e4d4c 100644 --- a/src/components/Content/OutlineMenu.tsx +++ b/src/components/Content/OutlineMenu.tsx @@ -415,6 +415,8 @@ export default function OutlineMenu({ useEffect(() => { if (!visible) return; const onDown = (event: PointerEvent) => { + // 钉住后点击外部不收起:仅再次点击图钉或 Esc 关闭(#1984)。 + if (pinned) return; const target = event.target as Node; if (menuRef.current?.contains(target)) return; if (btnRef.current?.contains(target)) return; @@ -423,7 +425,7 @@ export default function OutlineMenu({ }; document.addEventListener("pointerdown", onDown, true); return () => document.removeEventListener("pointerdown", onDown, true); - }, [visible]); + }, [visible, pinned]); // 面板底/标题行 pointer-events:none 后,指针在其上不触发 mouseenter; // 改用几何判定:指针在按钮或面板盒内即保留,出盒才延迟收起。