You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Motivation for the change, related issues
We would like to enable developers to mount local directories into their
Playground-based projects by prompting users to select a directory.
After the directory is selected the folder can be mounted into
Playground and used by it.
For example, this could be used to load local plugins and themes into
Playground.
## Implementation details
This PR adds a window message listener that takes the provided directory
handle and mounts it to the mountpoint.
```javascript
window.showDirectoryPicker().then(function (directoryHandle) {
window.parent.postMessage({
type: 'mount-directory-handle',
directoryHandle,
mountpoint: '/wordpress/wp-content/uploads/markdown/',
});
});
```
## Testing Instructions (or ideally a Blueprint)
- Add this code after line 280 in
`packages/playground/remote/src/lib/worker-thread.ts`
```
primaryPhp.writeFile(
'/wordpress/mount.php',
`<!DOCTYPE html>
<html>
<head>
<title>Directory picker</title>
</head>
<body>
<button id="pick">Pick directory</button>
<script>
document.getElementById('pick').addEventListener('click', function() {
if (!('showDirectoryPicker' in window)) {
alert('Your browser does not support the Directory Picker API');
return;
}
window.showDirectoryPicker().then(function(directoryHandle) {
window.parent.postMessage(
{
type: 'mount-directory-handle',
directoryHandle,
mountpoint: '/wordpress/wp-content/uploads/markdown/',
}
);
});
});
</script>
</body>
</html>`
);
```
- open this blueprint
http://localhost:5400/website-server/?url=/mount.php
- Click the button and select a local folder that contains an HTML file
- Change the URL in the Playground header to
`/wp-content/uploads/markdown/YOUR-HTML-FILE.html`
- Confirm that your file was loaded
You can mount a directory from the browser to Playground using the `window.showDirectoryPicker` API. Check the [Browser compatibility](https://developer.mozilla.org/en-US/docs/Web/API/Window/showDirectoryPicker#browser_compatibility) before using this API.
0 commit comments