scripting: add screen.orientation and make screen.height/width dynamic

This commit is contained in:
jean-emmanuel 2022-02-05 13:50:28 +01:00
parent 29e18ef23c
commit bcb74dc0b6
4 changed files with 16 additions and 5 deletions

View file

@ -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

View file

@ -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.

View file

@ -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,

View file

@ -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'],