diff --git a/CHANGELOG.md b/CHANGELOG.md index 316c5db5..6c905b52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ - knob: add `horizontal` mode - eq: filter responses are now only computed when needed; the widget's value is no longer exposed / sync'ed. +- scripting + - add `uploadFile` function for reading client-side files + ## 1.30.2 - bug fixes diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 45c94034..64921dea 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -416,10 +416,19 @@ Returns the instance of [Navigator](https://developer.mozilla.org/en-US/docs/Web #### `browseFile(options, callback)` -Opens the file file browser and pass selected file to a callback function (does not create nor read files by itself). +Opens the server file browser and pass selected file to a callback function (does not create nor read files by itself). - `options`: object with the following keys: - `extension`: allowed extension (default: "*") - `directory`: starting directory for browser (default: user's home) - `allowDir`: allow choosing a directory instead of a file (default: false) - `mode`: "save" or "open" +- `callback`: function called with 1 argument (file path) + +#### `uploadFile(options, callback)` + +Opens the client file upload prompt and pass the raw content of selected file to a callback function. + +- `options`: object with the following keys: + - `types`: equivalent of HTML attribute [`accept`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/accept) +- `callback`: function called with 1 argument (file content) diff --git a/src/client/ui/ui-fileupload.mjs b/src/client/ui/ui-fileupload.mjs index 3f03163a..818f5c06 100644 --- a/src/client/ui/ui-fileupload.mjs +++ b/src/client/ui/ui-fileupload.mjs @@ -12,7 +12,9 @@ export default function uiFileUpload(types, ok, error) { if (SINGLETON) CONTAINER.removeChild(SINGLETON) - SINGLETON = html`` + SINGLETON = html`` + + if (types) SINGLETON.accept = types CONTAINER.appendChild(SINGLETON) @@ -24,9 +26,9 @@ export default function uiFileUpload(types, ok, error) { uiLoading(locales('loading_upload')) - reader.onerror = reader.onabort = function() { + reader.onerror = reader.onabort = function(e) { uiLoading(false) - error() + error(e) } reader.onload = function(e) { diff --git a/src/client/widgets/scripts/script-vm.mjs b/src/client/widgets/scripts/script-vm.mjs index 4170bedc..ac8ce537 100644 --- a/src/client/widgets/scripts/script-vm.mjs +++ b/src/client/widgets/scripts/script-vm.mjs @@ -6,6 +6,7 @@ import {urlParser} from '../utils.mjs' import Vm from '../vm.mjs' import ipc from '../../ipc/index.mjs' import uiFilebrowser from '../../ui/ui-filebrowser.mjs' +import uiFileUpload from '../../ui/ui-fileupload.mjs' import {setFOCUSABLE} from '../../globals.mjs' var toolbar @@ -627,6 +628,37 @@ class ScriptVm extends Vm { } } + this.globals.uploadFile = (options, callback)=>{ + this.checkContext('uploadFile') + if (typeof options === 'function') { + callback = options + options = {} + } + + + if (typeof callback === 'function') { + + var widget = this.getWidget(), + valueOptions = this.getValueOptions() + + uiFileUpload(options.types, (path, content)=>{ + this.setWidget(widget) + this.setValueOptions(valueOptions) + try { + callback(content) + } catch(e) { + widget.errorProp('script', 'uploadFile', e) + } + this.setWidget() + this.setValueOptions() + }, (e)=>{ + widget.errorProp('script', 'uploadFile', e) + }) + } else { + widget.errorProp('script', 'browseFile', 'callback argument must be a function') + } + } + super.registerGlobals() }