Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions web/src/clipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ interface FakeArea {

/** node 里没有 DOM:一个只够 copyText 用的 document。`execCommand` 记下它被调的那一刻
* 页面上选中的是哪段字——那正是浏览器会放进剪贴板的东西 */
function page(result: boolean | "throw") {
const before = { focus: vi.fn() };
function page(result: boolean | "throw", inDialog = false) {
let focusedArea: FakeArea | undefined;
let dialogArea: FakeArea | undefined;
const dialog = {
appendChild: (area: FakeArea) => {
dialogArea = area;
area.attached = true;
},
};
const before = { focus: vi.fn(), closest: () => inDialog ? dialog : null };
const areas: FakeArea[] = [];
const copied: (string | undefined)[] = [];
vi.stubGlobal("document", {
Expand All @@ -44,9 +52,11 @@ function page(result: boolean | "throw") {
setAttribute: (name) => {
if (name === "readonly") area.readOnly = true;
},
focus: () => {},
focus: () => {
if (!inDialog || area === dialogArea) focusedArea = area;
},
select: () => {
area.selected = true;
area.selected = focusedArea === area;
},
remove: () => {
area.attached = false;
Expand All @@ -56,9 +66,10 @@ function page(result: boolean | "throw") {
return area;
},
execCommand: (command: string) => {
if (command === "copy") copied.push(areas.find((a) => a.attached && a.selected)?.value);
const selected = areas.find((area) => area.attached && area.selected);
if (command === "copy") copied.push(selected?.value);
if (result === "throw") throw new Error("not allowed here");
return result;
return result && selected !== undefined;
},
});
return { before, areas, copied };
Expand Down Expand Up @@ -86,6 +97,15 @@ describe("copyText", () => {
expect(before.focus).toHaveBeenCalled();
});

it("copies and restores focus when a modal traps focus", async () => {
vi.stubGlobal("navigator", {});
const { before, areas, copied } = page(true, true);
expect(await copyText("token")).toBe(true);
expect(copied).toEqual(["token"]);
expect(areas[0].attached).toBe(false);
expect(before.focus).toHaveBeenCalledOnce();
});

it("falls back the same way when the API refuses", async () => {
vi.stubGlobal("navigator", {
clipboard: { writeText: vi.fn().mockRejectedValue(new Error("denied")) },
Expand Down
4 changes: 3 additions & 1 deletion web/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export async function copyText(text: string): Promise<boolean> {
// 权限被拒、页面没有焦点:走下面的老办法
}
const focused = document.activeElement as HTMLElement | null;
// 挂到 body 会让模态框的焦点陷阱把焦点从临时输入框拉走。
const host = focused?.closest('dialog, [role="dialog"], [role="alertdialog"]') ?? document.body;
const area = document.createElement("textarea");
area.value = text;
// 只读:手机上不弹键盘;固定在视口里、透明:不把页面滚走,也看不见
Expand All @@ -26,7 +28,7 @@ export async function copyText(text: string): Promise<boolean> {
area.style.top = "0";
area.style.left = "0";
area.style.opacity = "0";
document.body.appendChild(area);
host.appendChild(area);
try {
area.focus();
area.select();
Expand Down
Loading