Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(blocks): resolve on file input dialog cancel #6950

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
4 changes: 4 additions & 0 deletions packages/blocks/src/_common/utils/filesys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ export async function openFileOrFiles({
}
resolve(input.files[0]);
});
// The `cancel` event fires when the user cancels the dialog.
input.addEventListener('cancel', () => {
resolve(null);
});
// Show the picker.
if ('showPicker' in HTMLInputElement.prototype) {
input.showPicker();
Expand Down
38 changes: 37 additions & 1 deletion tests/attachment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@
await type(page, 'file', 100);
await expect(slashMenu).toBeVisible();

const fileChooser = page.waitForEvent('filechooser');

Check failure on line 63 in tests/attachment.spec.ts

View workflow job for this annotation

GitHub Actions / Playground E2E test (1)

attachment.spec.ts:400:1 › should attachment can be deleted

1) attachment.spec.ts:400:1 › should attachment can be deleted ─────────────────────────────────── TimeoutError: page.waitForEvent: Timeout 5000ms exceeded while waiting for event "filechooser" =========================== logs =========================== waiting for event "filechooser" ============================================================ 61 | await expect(slashMenu).toBeVisible(); 62 | > 63 | const fileChooser = page.waitForEvent('filechooser'); | ^ 64 | await pressEnter(page); 65 | await (await fileChooser).setFiles(FILE_PATH); 66 | at insertAttachment (/home/runner/work/blocksuite/blocksuite/tests/attachment.spec.ts:63:30) at /home/runner/work/blocksuite/blocksuite/tests/attachment.spec.ts:406:3

Check failure on line 63 in tests/attachment.spec.ts

View workflow job for this annotation

GitHub Actions / Playground E2E test (1)

attachment.spec.ts:608:1 › press backspace after bookmark block can select bookmark block

2) attachment.spec.ts:608:1 › press backspace after bookmark block can select bookmark block ───── TimeoutError: page.waitForEvent: Timeout 5000ms exceeded while waiting for event "filechooser" =========================== logs =========================== waiting for event "filechooser" ============================================================ 61 | await expect(slashMenu).toBeVisible(); 62 | > 63 | const fileChooser = page.waitForEvent('filechooser'); | ^ 64 | await pressEnter(page); 65 | await (await fileChooser).setFiles(FILE_PATH); 66 | at insertAttachment (/home/runner/work/blocksuite/blocksuite/tests/attachment.spec.ts:63:30) at /home/runner/work/blocksuite/blocksuite/tests/attachment.spec.ts:618:3
await pressEnter(page);
await page.setInputFiles("input[type='file']", FILE_PATH);
await (await fileChooser).setFiles(FILE_PATH);

// Try to break the undo redo test
await captureHistory(page);

Expand Down Expand Up @@ -624,3 +626,37 @@
await assertBlockSelections(page, ['4']);
await assertBlockCount(page, 'paragraph', 0);
});

test('cancel file picker with input element resolves', async ({ page }) => {
await enterPlaygroundRoom(page);
await initEmptyParagraphState(page);

const { attachment } = getAttachment(page);

await focusRichText(page);
await pressEnter(page);
await pressArrowUp(page);

await page.evaluate(() => {
// Force fallback to input[type=file]
window.showOpenFilePicker = undefined;
});

const slashMenu = page.locator(`.slash-menu`);
await waitNextFrame(page);
await type(page, '/file', 100);
await expect(slashMenu).toBeVisible();

const fileChooser = page.waitForEvent('filechooser');
await pressEnter(page);
const inputFile = page.locator("input[type='file']");
await expect(inputFile).toHaveCount(1);

// This does not trigger `cancel` event and,
// therefore, the test isn't representative.
// Waiting for https://github.com/microsoft/playwright/issues/27524
await (await fileChooser).setFiles([]);

await expect(attachment).toHaveCount(0);
await expect(inputFile).toHaveCount(0);
});
Loading