Skip to content

Commit

Permalink
fix(blocks): resolve on file input dialog cancel (#6949)
Browse files Browse the repository at this point in the history
  • Loading branch information
shvixxl committed May 10, 2024
1 parent f785542 commit 70b7881
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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
34 changes: 34 additions & 0 deletions tests/attachment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,37 @@ test('press backspace after bookmark block can select bookmark block', async ({
await assertBlockSelections(page, [['0', '1', '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);
});

0 comments on commit 70b7881

Please sign in to comment.