From 0ea2c49860081e0c03a9661d1816cc8f9e1c4d73 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 2 Feb 2023 10:37:04 +0100 Subject: [PATCH] scripting: expose file browser (from the file widget) as a scripting function --- CHANGELOG.md | 3 +++ resources/docs/docs/widgets/scripting.md | 12 ++++++++++++ src/client/widgets/scripts/script-vm.js | 25 +++++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aeffa82e..736f6734 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index b2d4f02e..37959f9b 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -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" diff --git a/src/client/widgets/scripts/script-vm.js b/src/client/widgets/scripts/script-vm.js index 2b51c8bd..3b1d7920 100644 --- a/src/client/widgets/scripts/script-vm.js +++ b/src/client/widgets/scripts/script-vm.js @@ -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 }