-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New "bookmark" plugin allows hotkey fast-travel to specific steps #852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d8b8391
2c43ff3
366c127
9743979
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,17 @@ | ||
| /** | ||
| * Bookmark Plugin | ||
| * | ||
| * The bookmark plugin consists of a pre-init plugin, a keyup listener, and a pre-stepleave plugin. | ||
| * The bookmark plugin consists of | ||
| * a pre-init plugin, | ||
| * a keyup listener, and | ||
| * a pre-stepleave plugin. | ||
| * | ||
| * The pre-init plugin surveys all step divs to set up bookmark keybindings. | ||
| * The pre-stepleave plugin alters the destination when a bookmark hotkey is pressed. | ||
| * | ||
| * Example: | ||
| * | ||
| * <!-- data-bookmark-key-list allows an "inbound"-oriented style of non-linear navigation. --> | ||
| * <!-- data-bookmark-key-list allows an "inbound" style of non-linear navigation. --> | ||
| * <div id="..." class="step" data-bookmark-key-list="Digit1 KeyA 1 2 3 a b c"> | ||
| * | ||
| * See https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values for a table | ||
|
|
@@ -18,48 +22,51 @@ | |
| * Copyright 2016-2017 Henrik Ingo (@henrikingo) | ||
| * Released under the MIT license. | ||
| */ | ||
| /* global window, document, impress */ | ||
| /* global document, impress */ | ||
|
|
||
| ( function( document, window ) { | ||
| ( function( document ) { | ||
| "use strict"; | ||
| var api; | ||
| var hotkeys = {}; | ||
| function hotkeyDest(event) { | ||
| return (hotkeys.hasOwnProperty(event.key) ? hotkeys[event.key] : | ||
| hotkeys.hasOwnProperty(event.code) ? hotkeys[event.code] : null) } | ||
| function hotkeyDest( event ) { | ||
| return ( hotkeys.hasOwnProperty( event.key ) ? hotkeys[ event.key ] : | ||
| hotkeys.hasOwnProperty( event.code ) ? hotkeys[ event.code ] : null ); } | ||
|
|
||
| // In pre-init phase, build a map of bookmark hotkey to div id, by reviewing all steps | ||
| impress.addPreInitPlugin( function( root, api ) { | ||
| root.querySelectorAll( ".step" ).forEach(function(div) { | ||
| if ( div.dataset.bookmarkKeyList !== undefined && div.id != undefined) { | ||
| div.dataset.bookmarkKeyList.split( " " ).forEach( (k) => { | ||
| if (hotkeys.hasOwnProperty(k)) hotkeys[k].push( div.id ); // if hotkeys collide, we'll cycle through the targets | ||
| else hotkeys[k] = [div.id]; } ); } }); | ||
| root.querySelectorAll( ".step" ).forEach( function( div ) { | ||
| if ( div.dataset.bookmarkKeyList !== undefined && div.id !== undefined ) { | ||
| div.dataset.bookmarkKeyList.split( " " ).forEach( ( k ) => { | ||
| if ( hotkeys.hasOwnProperty( k ) ) { | ||
| hotkeys[ k ].push( div.id ); | ||
| } else { hotkeys[ k ] = [ div.id ]; } } ); } } ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move those closing brackets to separate lines? This makes the code more legible for beginners, imo. Also some explanation might be needed for the code here, if someone with less coding (well js, anyway) knowledge than you have reads this code.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol i'm surprised the linter even let me do that … haha I don't really think there's anything to be gained from adding a bunch of punctuation over multiple lines… my preference is to keep things as short as possible so you can see everything on one screen. They used to say "you can write Fortran in any language", now I suppose they should say "you can write Python in any language" 😺
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The linter is not properly configured for that. I have an issue open to talk about moving to eslint and how to configure it |
||
|
|
||
| api.lib.gc.addEventListener( document, "keyup", function( event ) { | ||
| if (hotkeyDest(event) != undefined) { | ||
| if ( hotkeyDest( event ) !== undefined ) { | ||
| event.stopImmediatePropagation(); | ||
| api.next(event); | ||
| // event.preventDefault(); | ||
| api.next( event ); | ||
|
|
||
| // Event.preventDefault(); | ||
| } | ||
| }); | ||
| }); | ||
| } ); | ||
| } ); | ||
|
|
||
| // In pre-stepleave phase, match a hotkey and reset destination accordingly. | ||
| impress.addPreStepLeavePlugin( function( event ) { | ||
| // window.console.log(`bookmark: running as PreStepLeavePlugin; event=`); window.console.log(event) | ||
|
|
||
| // Window.console.log(`bookmark: running as PreStepLeavePlugin; event=`); | ||
| // window.console.log(event) | ||
| if ( ( !event || !event.origEvent ) ) { return; } | ||
| var dest = hotkeyDest(event.origEvent) | ||
| // window.console.log(`bookmark: recognizing hotkey ${event.origEvent.code} goes to ${dest}`) | ||
| var dest = hotkeyDest( event.origEvent ); | ||
| if ( dest ) { | ||
| // window.console.log(`bookmark: recognizing hotkey ${event.code} goes to ${dest}`) | ||
| var newTarget = document.getElementById( dest[0] ); // jshint ignore:line | ||
|
|
||
| // Window.console.log(`bookmark: recognizing hotkey ${event.code} goes to ${dest}`) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you really want to leave the debug code commented out in here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i have taken it out, will push a new rev. Still working on the test failure. |
||
| var newTarget = document.getElementById( dest[ 0 ] ); // jshint ignore:line | ||
| if ( newTarget ) { | ||
| event.detail.next = newTarget; | ||
| dest.push(dest.shift()); // repeated hotkey presses cycle through each dest. | ||
| dest.push( dest.shift() ); // Repeated hotkey presses cycle through each dest. | ||
| } | ||
| } | ||
| }); | ||
| } ); | ||
|
|
||
| } )( document, window ); | ||
| } )( document ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can credit yourself here as well!