-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.d.ts
234 lines (218 loc) · 8 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Properties shared by all `options` provided to file save and open operations
*/
export interface CoreFileOptions {
/** Acceptable file extensions. Defaults to [""]. */
extensions?: string[];
/** Suggested file description. Defaults to "". */
description?: string;
/** Acceptable MIME types. [] */
mimeTypes?: string[];
}
/**
* Properties shared by the _first_ `options` object provided to file save and
* open operations (any additional options objects provided to those operations
* cannot have these properties)
*/
export interface FirstCoreFileOptions extends CoreFileOptions {
startIn?: WellKnownDirectory | FileSystemHandle;
/** By specifying an ID, the user agent can remember different directories for different IDs. */
id?: string;
excludeAcceptAllOption?: boolean | false;
}
/**
* The first `options` object passed to file save operations can also specify
* a filename
*/
export interface FirstFileSaveOptions extends FirstCoreFileOptions {
/** Suggested file name. Defaults to "Untitled". */
fileName?: string;
/**
* Configurable cleanup and `Promise` rejector usable with legacy API for
* determining when (and reacting if) a user cancels the operation. The
* method will be passed a reference to the internal `rejectionHandler` that
* can, e.g., be attached to/removed from the window or called after a
* timeout. The method should return a function that will be called when
* either the user chooses to open a file or the `rejectionHandler` is
* called. In the latter case, the returned function will also be passed a
* reference to the `reject` callback for the `Promise` returned by
* `fileOpen`, so that developers may reject the `Promise` when desired at
* that time.
* Example rejector:
*
* const file = await fileOpen({
* legacySetup: (rejectionHandler) => {
* const timeoutId = setTimeout(rejectionHandler, 10_000);
* return (reject) => {
* clearTimeout(timeoutId);
* if (reject) {
* reject('My error message here.');
* }
* };
* },
* });
*
* ToDo: Remove this workaround once
* https://github.com/whatwg/html/issues/6376 is specified and supported.
*/
legacySetup?: (
resolve: (value: Blob) => void,
rejectionHandler: () => void,
anchor: HTMLAnchorElement
) => (reject?: (reason?: any) => void) => void;
}
/**
* The first `options` object passed to file open operations can specify
* whether multiple files can be selected (the return type of the operation
* will be updated appropriately) and a way of handling cleanup and rejection
* for legacy open operations.
*/
export interface FirstFileOpenOptions<M extends boolean | undefined>
extends FirstCoreFileOptions {
/** Allow multiple files to be selected. Defaults to false. */
multiple?: M;
/**
* Configurable cleanup and `Promise` rejector usable with legacy API for
* determining when (and reacting if) a user cancels the operation. The
* method will be passed a reference to the internal `rejectionHandler` that
* can, e.g., be attached to/removed from the window or called after a
* timeout. The method should return a function that will be called when
* either the user chooses to open a file or the `rejectionHandler` is
* called. In the latter case, the returned function will also be passed a
* reference to the `reject` callback for the `Promise` returned by
* `fileOpen`, so that developers may reject the `Promise` when desired at
* that time.
* Example rejector:
*
* const file = await fileOpen({
* legacySetup: (rejectionHandler) => {
* const timeoutId = setTimeout(rejectionHandler, 10_000);
* return (reject) => {
* clearTimeout(timeoutId);
* if (reject) {
* reject('My error message here.');
* }
* };
* },
* });
*
* ToDo: Remove this workaround once
* https://github.com/whatwg/html/issues/6376 is specified and supported.
*/
legacySetup?: (
resolve: (
value: M extends false | undefined ? FileWithHandle : FileWithHandle[]
) => void,
rejectionHandler: () => void,
input: HTMLInputElement
) => (reject?: (reason?: any) => void) => void;
}
/**
* Opens file(s) from disk.
*/
export function fileOpen<M extends boolean | undefined = false>(
options?:
| [FirstFileOpenOptions<M>, ...CoreFileOptions[]]
| FirstFileOpenOptions<M>
): M extends false | undefined
? Promise<FileWithHandle>
: Promise<FileWithHandle[]>;
export type WellKnownDirectory =
| 'desktop'
| 'documents'
| 'downloads'
| 'music'
| 'pictures'
| 'videos';
/**
* Saves a file to disk.
* @returns Optional file handle to save in place.
*/
export function fileSave(
/** To-be-saved blob */
blob: Blob,
options?: [FirstFileSaveOptions, ...CoreFileOptions[]] | FirstFileSaveOptions,
/**
* A potentially existing file handle for a file to save to. Defaults to
* null.
*/
existingHandle?: FileSystemHandle | null,
/**
* Determines whether to throw (rather than open a new file save dialog)
* when existingHandle is no longer good. Defaults to false.
*/
throwIfExistingHandleNotGood?: boolean | false
): Promise<FileSystemHandle | null>;
/**
* Opens a directory from disk using the File System Access API.
* @returns Contained files.
*/
export function directoryOpen(options?: {
/** Whether to recursively get subdirectories. */
recursive: boolean;
/** Suggested directory in which the file picker opens. */
startIn?: WellKnownDirectory | FileSystemHandle;
/** By specifying an ID, the user agent can remember different directories for different IDs. */
id?: string;
/**
* Configurable setup, cleanup and `Promise` rejector usable with legacy API
* for determining when (and reacting if) a user cancels the operation. The
* method will be passed a reference to the internal `rejectionHandler` that
* can, e.g., be attached to/removed from the window or called after a
* timeout. The method should return a function that will be called when
* either the user chooses to open a file or the `rejectionHandler` is
* called. In the latter case, the returned function will also be passed a
* reference to the `reject` callback for the `Promise` returned by
* `fileOpen`, so that developers may reject the `Promise` when desired at
* that time.
* Example rejector:
*
* const file = await directoryOpen({
* legacySetup: (rejectionHandler) => {
* const timeoutId = setTimeout(rejectionHandler, 10_000);
* return (reject) => {
* clearTimeout(timeoutId);
* if (reject) {
* reject('My error message here.');
* }
* };
* },
* });
*
* ToDo: Remove this workaround once
* https://github.com/whatwg/html/issues/6376 is specified and supported.
*/
legacySetup?: (
resolve: (value: FileWithDirectoryHandle) => void,
rejectionHandler: () => void,
input: HTMLInputElement
) => (reject?: (reason?: any) => void) => void;
}): Promise<FileWithDirectoryHandle[]>;
/**
* Whether the File System Access API is supported.
*/
export const supported: boolean;
export function imageToBlob(img: HTMLImageElement): Promise<Blob>;
export interface FileWithHandle extends File {
handle?: FileSystemHandle;
}
export interface FileWithDirectoryHandle extends File {
directoryHandle?: FileSystemHandle;
}
// The following typings implement the relevant parts of the File System Access
// API. This can be removed once the specification reaches the Candidate phase
// and is implemented as part of microsoft/TSJS-lib-generator.
export interface FileSystemHandlePermissionDescriptor {
mode?: 'read' | 'readwrite';
}
export interface FileSystemHandle {
readonly kind: 'file' | 'directory';
readonly name: string;
isSameEntry: (other: FileSystemHandle) => Promise<boolean>;
queryPermission: (
descriptor?: FileSystemHandlePermissionDescriptor
) => Promise<PermissionState>;
requestPermission: (
descriptor?: FileSystemHandlePermissionDescriptor
) => Promise<PermissionState>;
}