From bcb74dc0b6471fa12a84034cef650ab54fc65374 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 5 Feb 2022 13:50:28 +0100 Subject: [PATCH] scripting: add screen.orientation and make screen.height/width dynamic --- CHANGELOG.md | 4 ++++ resources/docs/docs/widgets/advanced-syntaxes.md | 3 ++- resources/docs/docs/widgets/scripting.md | 4 ++-- src/client/widgets/vm.js | 10 ++++++++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9457cd9e..8d2ce057 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ - inspector - color picker: inline picker widget (no longer in a modal); show color changes on the fly +- scripting + - `screen.height` and `screen.width` always returns the current screen dimensions + - added `screen.orientation` global variable + - misc - some performance improvements diff --git a/resources/docs/docs/widgets/advanced-syntaxes.md b/resources/docs/docs/widgets/advanced-syntaxes.md index 07b8b4b5..05bceb7e 100644 --- a/resources/docs/docs/widgets/advanced-syntaxes.md +++ b/resources/docs/docs/widgets/advanced-syntaxes.md @@ -91,12 +91,13 @@ A property *cannot* contain multiple `JS{}` blocks - `console`: javascript console - `locals`: object for storing/reading arbitrary values. Changing its properties *does not* trigger any synchronisation even if the value is used somewhere else. - `globals`: same as `locals` but shared between all widgets, contains a few useful variables: - - `screen`: client screen dimensions (`{width, height}`) + - `screen`: client screen informations (`{width, height, orientation}`) - `env`: client options (ie: url query options), - `ip`: client's ip address, - `url`: server url, - `platform`: operating system as seen by the client - `session`: session file path + - `clipboard`: instance of navigator [Clipboard](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard) In this context, `@{} / OSC{}` are seen as variables. When they change, the whole block will be evaluated again. diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 29793772..d91ace88 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -4,7 +4,7 @@ Widgets can run [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaSc !!! warning "Advanced syntaxe" Most [advanced syntaxes](../advanced-syntaxes/) should be avoided as much as possible in scripting properties: - + - `@{}`: use `get()` and `getProp()` instead - `OSC{}`: use `get()` instead (using another widget as receiver) - `VAR{}`: use `getVar()`instead @@ -104,7 +104,7 @@ The following variables and functions are accessible in this context. - `console`: javascript console - `locals`: object for storing/reading arbitrary values. Changing its properties *does not* trigger any synchronisation even if the value is used somewhere else. - `globals`: same as `locals` but shared between all widgets, contains a few useful variables: - - `screen`: client screen dimensions (`{width, height}`) + - `screen`: client screen informations (`{width, height, orientation}`) - `env`: client options (ie: url query options), - `ip`: client's ip address, - `url`: server url, diff --git a/src/client/widgets/vm.js b/src/client/widgets/vm.js index 87b35e54..40b25be7 100644 --- a/src/client/widgets/vm.js +++ b/src/client/widgets/vm.js @@ -9,7 +9,13 @@ class Vm { if (!globals) { var sessionManager globals = { - screen: {width: screen.width, height: screen.height}, + screen: { + get width() {return screen.width}, + get height() {return screen.height}, + get orientation() { + return (screen.orientation || {}).type || window.orientation + } + }, env: deepCopy(ENV), ip: IP, url: document.location.host, @@ -92,7 +98,7 @@ class Vm { compile(code, defaultContext) { if (typeof code !== 'string') code = String(code) - + // var contextInit = 'var locals = locals;', var contextInit = '', contextKeys = ['__VARS'],