browser-nativefs
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "browser-nativefs", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Native File System API with legacy fallback in the browser.", | ||
@@ -9,7 +9,7 @@ "browser": "./dist/nativefs.mjs", | ||
"scripts": { | ||
"start": "npx http-server", | ||
"start": "npx http-server -o /demo/", | ||
"clean": "shx rm -rf ./dist", | ||
"build": "npm run clean && shx mkdir ./dist && npx ./build.sh", | ||
"prepare": "npm run lint && npm run build", | ||
"lint": "npx eslint ./src/*.mjs --fix" | ||
"lint": "npx eslint ./src/*.mjs --fix && npx eslint ./demo/*.mjs --fix" | ||
}, | ||
@@ -16,0 +16,0 @@ "repository": { |
# Browser-NativeFS | ||
Use the [Native File System API](https://wicg.github.io/native-file-system/) | ||
with legacy fallback in the browser. | ||
This module allows you to either use the | ||
[Native File System API](https://wicg.github.io/native-file-system/) on supporting browsers, | ||
with a transparent fallback to the `<input type="file">` and `<a download>` legacy methods. | ||
## Usage | ||
```bash | ||
The module feature-detects support for the Native File System API and | ||
only loads the actually relevant code. | ||
```js | ||
import { | ||
fileOpenPromise, | ||
fileSavePromise, | ||
imageToBlob | ||
} from 'https://unpkg.com/browser-nativefs'; | ||
(async () => { | ||
// This dynamically either loads the Native File System API | ||
// or the legacy module. | ||
const fileOpen = (await fileOpenPromise).default; | ||
const fileSave = (await fileSavePromise).default; | ||
const openButton = document.querySelector('#open'); | ||
const saveButton = document.querySelector('#save'); | ||
// Open a file. | ||
const blob = await fileOpen({mimeTypes: ['image/*']}); | ||
// Open multiple files | ||
const blobs = await fileOpen({mimeTypes: ['image/*'], multiple: true}); | ||
// Save a file. | ||
await fileSave(blob, {fileName: 'Untitled.png'}); | ||
})(); | ||
``` | ||
openButton.addEventListener('click', async (e) => { | ||
try { | ||
const blob = await fileOpen({mimeTypes: ['image/*']}); | ||
const img = document.createElement('img'); | ||
img.src = URL.createObjectURL(blob); | ||
document.body.append(img); | ||
} catch (err) { | ||
if (err.name !== 'AbortError') { | ||
console.error(err); | ||
} | ||
} | ||
}); | ||
## API | ||
saveButton.addEventListener('click', async (e) => { | ||
const blob = await imageToBlob(document.querySelector('img')); | ||
try { | ||
await fileSave(blob, {fileName: 'Untitled.png'}); | ||
} catch (err) { | ||
if (err.name !== 'AbortError') { | ||
console.error(err); | ||
} | ||
} | ||
}); | ||
```js | ||
const options = { | ||
mimeTypes: ['image/*'], | ||
extensions: ['png', 'jpg', 'jpeg', 'webp'], | ||
multiple: true, | ||
description: 'Image files', | ||
}; | ||
const blobs = await fileOpen(options); | ||
``` | ||
openButton.disabled = false; | ||
saveButton.disabled = false; | ||
})(); | ||
```js | ||
const options = { | ||
fileName: 'Untitled.txt', | ||
}; | ||
await fileSave(someBlob, options); | ||
``` | ||
@@ -50,0 +51,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16554
55