Skip to content

Commit

Permalink
Support multiple navigation keys in the generic plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Jul 28, 2023
1 parent 9ec0b4f commit f54ed5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 20 additions & 12 deletions plugins/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);

Expand All @@ -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;
}

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit f54ed5a

Please sign in to comment.