Support multiple navigation keys in the generic plugin

This commit is contained in:
Antonin Stefanutti 2023-07-28 17:21:00 +02:00 committed by Antonin Stefanutti
parent 9ec0b4f33b
commit 5d83d4345e
2 changed files with 25 additions and 17 deletions

View File

@ -160,15 +160,15 @@ Iterates over the available link:plugins[], picks the compatible one for present
[#generic]
=== `generic`
Emulates the end-user interaction by pressing the key with the specified `--key` option and iterates over the presentation as long as:
Emulates the end-user interaction by pressing the key with the specified `--key` option, and iterates over the presentation as long as:
[loweralpha]
. Any change to the DOM is detected by observing mutation events targeting the body element and its subtree nor
. the number of slides exported has reached the specified `--max-slides` option.
. Any change to the DOM is detected by observing mutation events targeting the body element and its subtree,
. Nor the number of slides exported has reached the specified `--max-slides` option.
The `--key` value must be one of the {uri-w3c-uievents-key}[UI events `KeyboardEvent` key values] and defaults to `ArrowRight`, e.g.:
The `--key` option must be a list of {uri-w3c-uievents-key}[UI events `KeyboardEvent` key values], and defaults to `['ArrowRight']`, e.g.:
$ decktape generic --key=ArrowDown
$ decktape generic --key=ArrowDown --key=ArrowRight
== Options

View File

@ -5,10 +5,12 @@
import { pause } from '../libs/util.js';
export const options = {
key : {
default : 'ArrowRight',
keys : {
full : 'key',
metavar : '<key>',
help : 'Key pressed to navigate to next slide',
list : true,
default : ['ArrowRight'],
help : 'Key pressed to navigate to next slide, can be repeated',
},
maxSlides : {
full : 'max-slides',
@ -24,12 +26,13 @@ export const options = {
};
export const help =
`Emulates the end-user interaction by pressing the key with the specified --key option
`Emulates the end-user interaction by pressing the key with the specified --key option,
and iterates over the presentation as long as:
- Any change to the DOM is detected by observing mutation events targeting the body element
and its subtree,
- Nor the number of slides exported has reached the specified --max-slides option.
The --key option must be one of the 'KeyboardEvent' keys and defaults to [${options.key.default}].`;
The --key option must be a list of 'KeyboardEvent' keys, and defaults to [${options.keys.default}].`;
export const create = (page, opts) => new Generic(page, opts);
@ -39,7 +42,7 @@ class Generic {
this.options = opts;
this.currentSlide = 1;
this.isNextSlideDetected = false;
this.key = this.options.key || options.key.default;
this.keys = this.options.keys || options.keys.default;
this.media = this.options.media || options.media.default;
}
@ -74,12 +77,17 @@ class Generic {
if (this.options.maxSlides && this.currentSlide >= this.options.maxSlides) {
return false;
}
await this.page.keyboard.press(this.key);
// TODO: use mutation event directly instead of relying on a timeout
// TODO: detect cycle to avoid infinite navigation for frameworks
// that support loopable presentations like impress.js and flowtime.js
await pause(1000);
return this.isNextSlideDetected;
for (let key of this.keys) {
await this.page.keyboard.press(key);
// TODO: use mutation event directly instead of relying on a timeout
// TODO: detect cycle to avoid infinite navigation for frameworks
// that support loopable presentations like impress.js and flowtime.js
await pause(1000);
if (this.isNextSlideDetected) {
return true;
}
}
return false;
}
nextSlide() {