Skip to content

Commit

Permalink
Define entry points for File System API (#9128)
Browse files Browse the repository at this point in the history
Summary:
As a follow-up to #9127, this PR defines all currently available entry points for the [File System API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API):

- `window.showOpenFilePicker()`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/window/showOpenFilePicker), [WICG](https://wicg.github.io/file-system-access/#api-showopenfilepicker)
- `window.showSaveFilePicker()`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker), [WICG](https://wicg.github.io/file-system-access/#api-showsavefilepicker)
- `window.showDirectoryPicker()`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/window/showDirectoryPicker), [WICG](https://wicg.github.io/file-system-access/#api-showdirectorypicker)
- `DataTransferItem.getAsFileSystemHandle()`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/getAsFileSystemHandle), [WICG](https://wicg.github.io/file-system-access/#dom-datatransferitem-getasfilesystemhandle)
- `StorageManager.getDirectory()`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/StorageManager/getDirectory), [WHATWG](https://fs.spec.whatwg.org/#dom-storagemanager-getdirectory)

Pull Request resolved: #9128

Reviewed By: SamChou19815

Differential Revision: D54988446

Pulled By: ZelJin

fbshipit-source-id: 8758515aaf467f6c92c8a131436ea79000651c2b
  • Loading branch information
ZelJin authored and facebook-github-bot committed Mar 17, 2024
1 parent 11921a2 commit e3bac53
Show file tree
Hide file tree
Showing 5 changed files with 843 additions and 774 deletions.
1 change: 1 addition & 0 deletions lib/bom.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ declare class StorageManager {
persist: () => Promise<boolean>;
persisted: () => Promise<boolean>;
estimate?: () => Promise<StorageEstimate>;
getDirectory: () => Promise<FileSystemDirectoryHandle>;
}

type StorageManagerRegisteredEndpoint = 'caches' | 'indexedDB' | 'localStorage' | 'serviceWorkerRegistrations' | 'sessionStorage';
Expand Down
68 changes: 68 additions & 0 deletions lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ declare class DataTransferItemList {
clear(): void;
};

// https://wicg.github.io/file-system-access/#drag-and-drop
declare class DataTransferItem {
kind: string; // readonly
type: string; // readonly
Expand All @@ -124,6 +125,12 @@ declare class DataTransferItem {
* https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry
*/
webkitGetAsEntry(): void | () => any;
/*
* Not supported in all browsers
* For up to date compatibility information, please visit
* https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/getAsFileSystemHandle
*/
getAsFileSystemHandle?: () => Promise<?FileSystemHandle>;
};

/* DOM */
Expand Down Expand Up @@ -4522,6 +4529,67 @@ declare function scrollTo(options: ScrollToOptions): void;
declare function scrollBy(x: number, y: number): void;
declare function scrollBy(options: ScrollToOptions): void;

/* Window file picker */

type WindowFileSystemPickerFileType = {|
description?: string,
/*
* An Object with the keys set to the MIME type
* and the values an Array of file extensions
* Example:
* accept: {
* "image/*": [".png", ".gif", ".jpeg", ".jpg"],
* },
*/
accept: {
[string]: Array<string>,
},
|};

type WindowBaseFilePickerOptions = {|
id?: number,
startIn?:
| FileSystemHandle
| "desktop"
| "documents"
| "downloads"
| "music"
| "pictures"
| "videos",
|};

type WindowFilePickerOptions = WindowBaseFilePickerOptions & {|
excludeAcceptAllOption?: boolean,
types?: Array<WindowFileSystemPickerFileType>,
|};

type WindowOpenFilePickerOptions = WindowFilePickerOptions & {|
multiple?: boolean,
|};

type WindowSaveFilePickerOptions = WindowFilePickerOptions & {|
suggestedName?: string,
|};

type WindowDirectoryFilePickerOptions = WindowBaseFilePickerOptions & {|
mode?: "read" | "readwrite",
|};

// https://wicg.github.io/file-system-access/#api-showopenfilepicker
declare function showOpenFilePicker(
options?: WindowOpenFilePickerOptions
): Promise<Array<FileSystemFileHandle>>;

// https://wicg.github.io/file-system-access/#api-showsavefilepicker
declare function showSaveFilePicker(
options?: WindowSaveFilePickerOptions
): Promise<FileSystemFileHandle>;

// https://wicg.github.io/file-system-access/#api-showdirectorypicker
declare function showDirectoryPicker(
options?: WindowDirectoryFilePickerOptions
): Promise<FileSystemDirectoryHandle>;

/* Notification */
type NotificationPermission = 'default' | 'denied' | 'granted';
type NotificationDirection = 'auto' | 'ltr' | 'rtl';
Expand Down
Loading

0 comments on commit e3bac53

Please sign in to comment.