`<pacem-directory>` links one local folder to the page: the user picks it once, the element lists its files, watches them, reads them, and (if you let it) writes them. It can even keep the folder across visits, or serve it under a URL of your site. One element, one folder. ```html <!-- read-only, listed and watched, back on the next visit --> <pacem-directory id="photos" persist-as="photos"></pacem-directory> <pacem-button on-click="#photos.pick()">choose a folder</pacem-button> <!-- its files at ./assets/… (your service worker must serve the mount) --> <pacem-directory mount="assets/" readonly="false"></pacem-directory> ```
## Usage
**Link it.** `pick()` opens the browser's folder picker (call it from a click: browsers want a user gesture).
`directory` then holds the folder's handle, `name` its name, `files` its paths (`clips/intro.mp4`, sorted),
and `directorychange` fires. `unlink()` lets it go.
**Where it stands.** `state` is `none` (nothing linked), `prompt` (linked, but the access must be granted again:
typical after a reload) or `ready`. A `prompt` needs one click on something that calls `grant()`.
```html
<pacem-button hide="{{ #photos.state !== 'prompt' }}" on-click="#photos.grant()">allow</pacem-button>
```
**What gets listed.** Sub-folders are walked (`recursive="false"` stops that), names starting with a dot are left out
(`include-hidden` takes them in), and at most `max-files` (500) are listed: `truncated` tells you when the list stopped.
**Watching.** Where the browser has `FileSystemObserver` (Chromium), changes made by any app fire `change` once they
pause, the list already refreshed. `detail.paths` holds the changed paths, or `null` when anything may have changed.
`watch="false"` switches it off; `refresh()` lists again on demand.
**Reading.** `resolve(path)` gives you the `File`, or a reason when there's none: `'not-found'`, `'invalid'` (a path
climbing out of the folder), `'not-allowed'`, `'unavailable'`. It's read when you ask, so it's always current.
**Keeping it.** `persist-as="key"` stores the handle in IndexedDB and brings it back on the next visit (usually as
`prompt`). Change the key and the old one is forgotten.
> Handles can't go into `localStorage`: that's why `persist-as` uses IndexedDB, one database
> (`pacem-directory-mounts`) for every element, one record per key.
**Writing.** The folder is read-only by default. With `readonly="false"` the picker asks for write access, and
`write(path, data)` (missing folders get created) and `removeEntry(path, recursive)` work. Both answer `null`, or why not.
**Where it works.** `supported` says how: `'handle'` (File System Access: Chromium browsers), `'snapshot'` (elsewhere:
the browser's own folder dialog reads the files once, read-only, not watched, not kept) or `'none'`.
**Serving it.** `mount="assets/"` makes the files answer at `assets/…`, relative to the page: an `<img src="assets/logo.png">`,
a `<video>` that seeks (range requests), a `fetch()`. Requests under the mount get the file read at that moment.
**`mount` does nothing by itself.** The page must be controlled by a service worker whose script loads foundation and
calls `DirectoryMounts.serve(evt, mounts)` **first** in its `fetch` listener, listing every mounted path as the
elements resolve it: `mount="assets/"` on `/paper/` is `'/paper/assets/'`.
```js
importScripts('/js/pacem-foundation.js'); // or import it in a module worker
self.addEventListener('fetch', evt => {
if (Pacem.DirectoryMounts.serve(evt, ['/paper/assets/'])) {
return;
}
// ...the worker's own routing
});
```
The mounts live in the worker's script on purpose: a page can't add a `fetch` listener to its worker, and a worker
forgets anything it's told at runtime whenever the browser stops it. Until the page is controlled (a first visit,
before the worker activates and claims it), mounted requests go to the network.
## Examples ### Peek into a file, back on the next visit `persist-as` keeps the folder: reload the page and it comes back, asking for one click. The eye of a file `resolve()`s it.
### A scratch folder you can write `readonly="false"`: the picker asks for write access, then `write()` saves a note and `removeEntry()` deletes it. It writes on your disk for real: pick an empty folder.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.