rm fs copy
parent
a47df8e6a8
commit
ef694c04f4
|
@ -1,32 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
import supported from './supported.mjs';
|
|
||||||
|
|
||||||
const implementation = !supported
|
|
||||||
? import('./legacy/directory-open.mjs')
|
|
||||||
: supported === 'chooseFileSystemEntries'
|
|
||||||
? import('./fs-access-legacy/directory-open.mjs')
|
|
||||||
: import('./fs-access/directory-open.mjs');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For opening directories, dynamically either loads the File System Access API
|
|
||||||
* module or the legacy method.
|
|
||||||
*/
|
|
||||||
export async function directoryOpen(...args) {
|
|
||||||
return (await implementation).default(...args);
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
import supported from './supported.mjs';
|
|
||||||
|
|
||||||
const implementation = !supported
|
|
||||||
? import('./legacy/file-open.mjs')
|
|
||||||
: supported === 'chooseFileSystemEntries'
|
|
||||||
? import('./fs-access-legacy/file-open.mjs')
|
|
||||||
: import('./fs-access/file-open.mjs');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For opening files, dynamically either loads the File System Access API module
|
|
||||||
* or the legacy method.
|
|
||||||
*/
|
|
||||||
export async function fileOpen(...args) {
|
|
||||||
return (await implementation).default(...args);
|
|
||||||
}
|
|
|
@ -1,32 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
import supported from './supported.mjs';
|
|
||||||
|
|
||||||
const implementation = !supported
|
|
||||||
? import('./legacy/file-save.mjs')
|
|
||||||
: supported === 'chooseFileSystemEntries'
|
|
||||||
? import('./fs-access-legacy/file-save.mjs')
|
|
||||||
: import('./fs-access/file-save.mjs');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* For saving files, dynamically either loads the File System Access API module
|
|
||||||
* or the legacy method.
|
|
||||||
*/
|
|
||||||
export async function fileSave(...args) {
|
|
||||||
return (await implementation).default(...args);
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
const getFiles = async (dirHandle, recursive, path = dirHandle.name) => {
|
|
||||||
const dirs = [];
|
|
||||||
const files = [];
|
|
||||||
for await (const entry of dirHandle.getEntries()) {
|
|
||||||
const nestedPath = `${path}/${entry.name}`;
|
|
||||||
if (entry.isFile) {
|
|
||||||
files.push(
|
|
||||||
entry.getFile().then((file) =>
|
|
||||||
Object.defineProperty(file, 'webkitRelativePath', {
|
|
||||||
configurable: true,
|
|
||||||
enumerable: true,
|
|
||||||
get: () => nestedPath,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} else if (entry.isDirectory && recursive) {
|
|
||||||
dirs.push(getFiles(entry, recursive, nestedPath));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return [...(await Promise.all(dirs)).flat(), ...(await Promise.all(files))];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a directory from disk using the (legacy) File System Access API.
|
|
||||||
* @type { typeof import("../../index").directoryOpen }
|
|
||||||
*/
|
|
||||||
export default async (options = {}) => {
|
|
||||||
options.recursive = options.recursive || false;
|
|
||||||
const handle = await window.chooseFileSystemEntries({
|
|
||||||
type: 'open-directory',
|
|
||||||
});
|
|
||||||
return getFiles(handle, options.recursive);
|
|
||||||
};
|
|
|
@ -1,43 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
const getFileWithHandle = async (handle) => {
|
|
||||||
const file = await handle.getFile();
|
|
||||||
file.handle = handle;
|
|
||||||
return file;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a file from disk using the (legacy) File System Access API.
|
|
||||||
* @type { typeof import("../../index").fileOpen }
|
|
||||||
*/
|
|
||||||
export default async (options = {}) => {
|
|
||||||
const handleOrHandles = await window.chooseFileSystemEntries({
|
|
||||||
accepts: [
|
|
||||||
{
|
|
||||||
description: options.description || '',
|
|
||||||
mimeTypes: options.mimeTypes || ['*/*'],
|
|
||||||
extensions: options.extensions || [''],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
multiple: options.multiple || false,
|
|
||||||
});
|
|
||||||
if (options.multiple) {
|
|
||||||
return Promise.all(handleOrHandles.map(getFileWithHandle));
|
|
||||||
}
|
|
||||||
return getFileWithHandle(handleOrHandles);
|
|
||||||
};
|
|
|
@ -1,40 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves a file to disk using the (legacy) File System Access API.
|
|
||||||
* @type { typeof import("../../index").fileSave }
|
|
||||||
*/
|
|
||||||
export default async (blob, options = {}, handle = null) => {
|
|
||||||
options.fileName = options.fileName || 'Untitled';
|
|
||||||
handle =
|
|
||||||
handle ||
|
|
||||||
(await window.chooseFileSystemEntries({
|
|
||||||
type: 'save-file',
|
|
||||||
accepts: [
|
|
||||||
{
|
|
||||||
description: options.description || '',
|
|
||||||
mimeTypes: [blob.type],
|
|
||||||
extensions: options.extensions || [''],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}));
|
|
||||||
const writable = await handle.createWritable();
|
|
||||||
await writable.write(blob);
|
|
||||||
await writable.close();
|
|
||||||
return handle;
|
|
||||||
};
|
|
|
@ -1,48 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
const getFiles = async (dirHandle, recursive, path = dirHandle.name) => {
|
|
||||||
const dirs = [];
|
|
||||||
const files = [];
|
|
||||||
for await (const entry of dirHandle.values()) {
|
|
||||||
const nestedPath = `${path}/${entry.name}`;
|
|
||||||
if (entry.kind === 'file') {
|
|
||||||
files.push(
|
|
||||||
entry.getFile().then((file) =>
|
|
||||||
Object.defineProperty(file, 'webkitRelativePath', {
|
|
||||||
configurable: true,
|
|
||||||
enumerable: true,
|
|
||||||
get: () => nestedPath,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} else if (entry.kind === 'directory' && recursive) {
|
|
||||||
dirs.push(getFiles(entry, recursive, nestedPath));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return [...(await Promise.all(dirs)).flat(), ...(await Promise.all(files))];
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a directory from disk using the File System Access API.
|
|
||||||
* @type { typeof import("../../index").directoryOpen }
|
|
||||||
*/
|
|
||||||
export default async (options = {}) => {
|
|
||||||
options.recursive = options.recursive || false;
|
|
||||||
const handle = await window.showDirectoryPicker();
|
|
||||||
return getFiles(handle, options.recursive);
|
|
||||||
};
|
|
|
@ -1,51 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
const getFileWithHandle = async (handle) => {
|
|
||||||
const file = await handle.getFile();
|
|
||||||
file.handle = handle;
|
|
||||||
return file;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a file from disk using the File System Access API.
|
|
||||||
* @type { typeof import("../../index").fileOpen }
|
|
||||||
*/
|
|
||||||
export default async (options = {}) => {
|
|
||||||
const accept = {};
|
|
||||||
if (options.mimeTypes) {
|
|
||||||
options.mimeTypes.map((mimeType) => {
|
|
||||||
accept[mimeType] = options.extensions || [];
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
accept['*/*'] = options.extensions || [];
|
|
||||||
}
|
|
||||||
const handleOrHandles = await window.showOpenFilePicker({
|
|
||||||
types: [
|
|
||||||
{
|
|
||||||
description: options.description || '',
|
|
||||||
accept: accept,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
multiple: options.multiple || false,
|
|
||||||
});
|
|
||||||
const files = await Promise.all(handleOrHandles.map(getFileWithHandle));
|
|
||||||
if (options.multiple) {
|
|
||||||
return files;
|
|
||||||
}
|
|
||||||
return files[0];
|
|
||||||
};
|
|
|
@ -1,64 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves a file to disk using the File System Access API.
|
|
||||||
* @type { typeof import("../../index").fileSave }
|
|
||||||
*/
|
|
||||||
export default async (
|
|
||||||
blob,
|
|
||||||
options = {},
|
|
||||||
existingHandle = null,
|
|
||||||
throwIfExistingHandleNotGood = false
|
|
||||||
) => {
|
|
||||||
options.fileName = options.fileName || 'Untitled';
|
|
||||||
const accept = {};
|
|
||||||
if (options.mimeTypes) {
|
|
||||||
options.mimeTypes.push(blob.type);
|
|
||||||
options.mimeTypes.map((mimeType) => {
|
|
||||||
accept[mimeType] = options.extensions || [];
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
accept[blob.type] = options.extensions || [];
|
|
||||||
}
|
|
||||||
if (existingHandle) {
|
|
||||||
try {
|
|
||||||
// Check if the file still exists.
|
|
||||||
await existingHandle.getFile();
|
|
||||||
} catch (err) {
|
|
||||||
existingHandle = null;
|
|
||||||
if (throwIfExistingHandleNotGood) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const handle =
|
|
||||||
existingHandle ||
|
|
||||||
(await window.showSaveFilePicker({
|
|
||||||
suggestedName: options.fileName,
|
|
||||||
types: [
|
|
||||||
{
|
|
||||||
description: options.description || '',
|
|
||||||
accept: accept,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}));
|
|
||||||
const writable = await handle.createWritable();
|
|
||||||
await writable.write(blob);
|
|
||||||
await writable.close();
|
|
||||||
return handle;
|
|
||||||
};
|
|
|
@ -1,24 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @module browser-fs-access
|
|
||||||
*/
|
|
||||||
export { fileOpen } from './file-open.mjs';
|
|
||||||
export { directoryOpen } from './directory-open.mjs';
|
|
||||||
export { fileSave } from './file-save.mjs';
|
|
||||||
export { default as supported } from './supported.mjs';
|
|
|
@ -1,58 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a directory from disk using the legacy
|
|
||||||
* `<input type="file" webkitdirectory>` method.
|
|
||||||
* @type { typeof import("../../index").directoryOpen }
|
|
||||||
*/
|
|
||||||
export default async (options = {}) => {
|
|
||||||
options.recursive = options.recursive || false;
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const input = document.createElement('input');
|
|
||||||
input.type = 'file';
|
|
||||||
input.webkitdirectory = true;
|
|
||||||
|
|
||||||
// ToDo: Remove this workaround once
|
|
||||||
// https://github.com/whatwg/html/issues/6376 is specified and supported.
|
|
||||||
const rejectOnPageInteraction = () => {
|
|
||||||
window.removeEventListener('pointermove', rejectOnPageInteraction);
|
|
||||||
window.removeEventListener('pointerdown', rejectOnPageInteraction);
|
|
||||||
window.removeEventListener('keydown', rejectOnPageInteraction);
|
|
||||||
reject(new DOMException('The user aborted a request.', 'AbortError'));
|
|
||||||
};
|
|
||||||
|
|
||||||
window.addEventListener('pointermove', rejectOnPageInteraction);
|
|
||||||
window.addEventListener('pointerdown', rejectOnPageInteraction);
|
|
||||||
window.addEventListener('keydown', rejectOnPageInteraction);
|
|
||||||
|
|
||||||
input.addEventListener('change', () => {
|
|
||||||
window.removeEventListener('pointermove', rejectOnPageInteraction);
|
|
||||||
window.removeEventListener('pointerdown', rejectOnPageInteraction);
|
|
||||||
window.removeEventListener('keydown', rejectOnPageInteraction);
|
|
||||||
let files = Array.from(input.files);
|
|
||||||
if (!options.recursive) {
|
|
||||||
files = files.filter((file) => {
|
|
||||||
return file.webkitRelativePath.split('/').length === 2;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
resolve(files);
|
|
||||||
});
|
|
||||||
|
|
||||||
input.click();
|
|
||||||
});
|
|
||||||
};
|
|
|
@ -1,56 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a file from disk using the legacy `<input type="file">` method.
|
|
||||||
* @type { typeof import("../../index").fileOpen }
|
|
||||||
*/
|
|
||||||
export default async (options = {}) => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const input = document.createElement('input');
|
|
||||||
input.type = 'file';
|
|
||||||
const accept = [
|
|
||||||
...(options.mimeTypes ? options.mimeTypes : []),
|
|
||||||
options.extensions ? options.extensions : [],
|
|
||||||
].join();
|
|
||||||
input.multiple = options.multiple || false;
|
|
||||||
// Empty string allows everything.
|
|
||||||
input.accept = accept || '';
|
|
||||||
|
|
||||||
// ToDo: Remove this workaround once
|
|
||||||
// https://github.com/whatwg/html/issues/6376 is specified and supported.
|
|
||||||
const rejectOnPageInteraction = () => {
|
|
||||||
window.removeEventListener('pointermove', rejectOnPageInteraction);
|
|
||||||
window.removeEventListener('pointerdown', rejectOnPageInteraction);
|
|
||||||
window.removeEventListener('keydown', rejectOnPageInteraction);
|
|
||||||
reject(new DOMException('The user aborted a request.', 'AbortError'));
|
|
||||||
};
|
|
||||||
|
|
||||||
window.addEventListener('pointermove', rejectOnPageInteraction);
|
|
||||||
window.addEventListener('pointerdown', rejectOnPageInteraction);
|
|
||||||
window.addEventListener('keydown', rejectOnPageInteraction);
|
|
||||||
|
|
||||||
input.addEventListener('change', () => {
|
|
||||||
window.removeEventListener('pointermove', rejectOnPageInteraction);
|
|
||||||
window.removeEventListener('pointerdown', rejectOnPageInteraction);
|
|
||||||
window.removeEventListener('keydown', rejectOnPageInteraction);
|
|
||||||
resolve(input.multiple ? input.files : input.files[0]);
|
|
||||||
});
|
|
||||||
|
|
||||||
input.click();
|
|
||||||
});
|
|
||||||
};
|
|
|
@ -1,32 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves a file to disk using the legacy `<a download>` method.
|
|
||||||
* @type { typeof import("../../index").fileSave }
|
|
||||||
*/
|
|
||||||
export default async (blob, options = {}) => {
|
|
||||||
const a = document.createElement('a');
|
|
||||||
a.download = options.fileName || 'Untitled';
|
|
||||||
a.href = URL.createObjectURL(blob);
|
|
||||||
a.addEventListener('click', () => {
|
|
||||||
// `setTimeout()` due to
|
|
||||||
// https://github.com/LLK/scratch-gui/issues/1783#issuecomment-426286393
|
|
||||||
setTimeout(() => URL.revokeObjectURL(a.href), 30 * 1000);
|
|
||||||
});
|
|
||||||
a.click();
|
|
||||||
};
|
|
|
@ -1,43 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright 2020 Google LLC
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
// @license © 2020 Google LLC. Licensed under the Apache License, Version 2.0.
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns whether the File System Access API is supported and usable in the
|
|
||||||
* current context (for example cross-origin iframes).
|
|
||||||
* @returns {boolean} Returns `true` if the File System Access API is supported and usable, else returns `false`.
|
|
||||||
*/
|
|
||||||
const supported = (() => {
|
|
||||||
// ToDo: Remove this check once Permissions Policy integration
|
|
||||||
// has happened, tracked in
|
|
||||||
// https://github.com/WICG/file-system-access/issues/245.
|
|
||||||
if ('top' in self && self !== top) {
|
|
||||||
try {
|
|
||||||
// This will succeed on same-origin iframes,
|
|
||||||
// but fail on cross-origin iframes.
|
|
||||||
top.location + '';
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if ('chooseFileSystemEntries' in self) {
|
|
||||||
return 'chooseFileSystemEntries';
|
|
||||||
} else if ('showOpenFilePicker' in self) {
|
|
||||||
return 'showOpenFilePicker';
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
})();
|
|
||||||
|
|
||||||
export default supported;
|
|
Loading…
Reference in New Issue