scripting: expose file browser (from the file widget) as a scripting function

This commit is contained in:
jean-emmanuel 2023-02-02 10:37:04 +01:00
parent ce73b5d374
commit 0ea2c49860
3 changed files with 39 additions and 1 deletions

View file

@ -5,6 +5,9 @@
- bug fixes
- linkId not working between two encoder widgets
- scripting
- expose file browser (from the file widget) as a scripting function
- misc
- add polish locales (@ Manz4rk)

View file

@ -369,3 +369,15 @@ If the event that triggered the script's execution was not initiated by a user i
#### `getNavigator()`
Returns the instance of [Navigator](https://developer.mozilla.org/en-US/docs/Web/API/Navigator).
----
#### `browseFile(options, callback)`
Opens the file 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"

View file

@ -5,6 +5,7 @@ var widgetManager = require('../../managers/widgets'),
{urlParser} = require('../utils'),
Vm = require('../vm'),
ipc = require('../../ipc'),
uiFilebrowser = require('../../ui/ui-filebrowser'),
toolbar
@ -555,11 +556,33 @@ class ScriptVm extends Vm {
}
this.sandbox.contentWindow.browseFile = (options, callback)=>{
this.checkContext('browseFile')
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof callback === 'function') {
uiFilebrowser({
extension: options.extention || '*',
directory: options.directory || undefined,
loadDir: !!options.allowDir,
save: options.mode === 'save'
}, (path)=>{
callback(path.join(path[0][0] === '/' ? '/' : '\\'))
})
} else {
widget.errorProp('script', 'browseFile', 'callback argument must be a function')
}
}
this.globals = {}
for (var imports of ['set', 'get', 'getProp', 'getIndex', 'updateProp', 'send', 'httpGet', 'stateGet', 'stateSet', 'storage',
'setInterval', 'clearInterval', 'setTimeout', 'cthislearTimeout', 'setFocus', 'unfocus' , 'setFocusable', 'setScroll', 'getScroll', 'toolbar',
'openUrl', 'getVar', 'setVar', 'runAs', 'reload', 'Image', 'updateCanvas', 'getNavigator']) {
'openUrl', 'getVar', 'setVar', 'runAs', 'reload', 'Image', 'updateCanvas', 'getNavigator', 'browseFile']) {
this.sanitize(this.sandbox.contentWindow[imports])
this.globals[imports] = true
}