scripting: add fileUpload function

This commit is contained in:
jean-emmanuel 2026-04-09 14:59:03 +02:00
parent 892cbd6886
commit 9d79e857f8
4 changed files with 50 additions and 4 deletions

View file

@ -12,7 +12,9 @@ export default function uiFileUpload(types, ok, error) {
if (SINGLETON) CONTAINER.removeChild(SINGLETON)
SINGLETON = html`<input type="file" accept="${types}" style="position:absolute;opacity:0;pointer-events:none;"/>`
SINGLETON = html`<input type="file" style="position:absolute;opacity:0;pointer-events:none;"/>`
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) {

View file

@ -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()
}