-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Add full help screen to help plugin #714
base: master
Are you sure you want to change the base?
Changes from all commits
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,11 +1,6 @@ | ||
/** | ||
* Help popup plugin | ||
* | ||
* Example: | ||
* | ||
* <!-- Show a help popup at start, or if user presses "H" --> | ||
* <div id="impress-help"></div> | ||
* | ||
* For developers: | ||
* | ||
* Typical use for this plugin, is for plugins that support some keypress, to add a line | ||
|
@@ -21,46 +16,88 @@ | |
var rows = []; | ||
var timeoutHandle; | ||
|
||
var triggerEvent = function( el, eventName, detail ) { | ||
var event = document.createEvent( "CustomEvent" ); | ||
event.initCustomEvent( eventName, true, true, detail ); | ||
el.dispatchEvent( event ); | ||
var body = document.querySelector( "body" ); | ||
|
||
// This querySelector is used to keep compatibility with templates using div#impress-help | ||
var helpDiv = document.querySelector( "#impress-help" ) || document.createElement( "div" ); | ||
body.appendChild( helpDiv ); | ||
|
||
var fullHelpDiv = document.createElement( "div" ); | ||
var fullHelpStyle = { | ||
display: "none", | ||
position: "fixed", | ||
top: 0, | ||
bottom: 0, | ||
left: 0, | ||
right: 0, | ||
backgroundColor: "#000", | ||
color: "#FFF", | ||
opacity: 0.8, | ||
textAlign: "center", | ||
padding: "3em" | ||
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 philosophy of impress.js is that we don't want to force any colors or other styling for the user. So instead of setting the style here, you should set it in css/impress-demo.css. Unfortunately this means you also need to set this in every other css file under presentations. (Related: #681) So what is left for the plugin to do? Maybe nothing? Do you need different CSS for the full help? At most, you could set a class that indicates short help vs full help:
Presentation authors can then choose to use this class for styling if they want. |
||
}; | ||
|
||
var renderHelpDiv = function() { | ||
var helpDiv = document.getElementById( "impress-help" ); | ||
if ( helpDiv ) { | ||
body.appendChild( fullHelpDiv ); | ||
|
||
var css = function( elem, style ) { | ||
for ( var p in style ) { | ||
elem.style[ p ] = style[ p ]; | ||
} | ||
}; | ||
css( fullHelpDiv, fullHelpStyle ); | ||
|
||
var renderDiv = function( elem, shortOnly ) { | ||
if ( elem ) { | ||
var html = []; | ||
for ( var row in rows ) { | ||
for ( var arrayItem in row ) { | ||
html.push( rows[ row ][ arrayItem ] ); | ||
var item = rows[ row ][ arrayItem ]; | ||
if ( item ) { | ||
if ( ( shortOnly && item.short ) || !shortOnly ) { | ||
html.push( item.row ); | ||
} | ||
} | ||
} | ||
} | ||
if ( html ) { | ||
helpDiv.innerHTML = "<table>\n" + html.join( "\n" ) + "</table>\n"; | ||
elem.innerHTML = "<table style='width: 100%'>\n" + html.join( "\n" ) + "</table>\n"; | ||
} | ||
} | ||
}; | ||
|
||
var toggleHelp = function() { | ||
var helpDiv = document.getElementById( "impress-help" ); | ||
if ( !helpDiv ) { | ||
var renderHelpDiv = function() { | ||
renderDiv( helpDiv, true ); | ||
renderDiv( fullHelpDiv, false ); | ||
}; | ||
|
||
var toggle = function( elem ) { | ||
if ( !elem ) { | ||
return; | ||
} | ||
|
||
if ( helpDiv.style.display === "block" ) { | ||
helpDiv.style.display = "none"; | ||
if ( elem.style.display === "block" ) { | ||
elem.style.display = "none"; | ||
} else { | ||
helpDiv.style.display = "block"; | ||
window.clearTimeout( timeoutHandle ); | ||
elem.style.display = "block"; | ||
if ( elem === helpDiv ) { | ||
window.clearTimeout( timeoutHandle ); | ||
} else { | ||
|
||
// Hides helpDiv when fullHelpDiv is shown | ||
helpDiv.style.display = "none"; | ||
window.clearTimeout( timeoutHandle ); | ||
} | ||
} | ||
}; | ||
|
||
document.addEventListener( "keyup", function( event ) { | ||
|
||
if ( event.keyCode === 72 || event.keyCode === 191 ) { // "h" || "?" | ||
event.preventDefault(); | ||
toggleHelp(); | ||
toggle( helpDiv ); | ||
} else if ( event.key === "F1" ) { | ||
event.preventDefault(); | ||
toggle( fullHelpDiv ); | ||
} | ||
}, false ); | ||
|
||
|
@@ -72,6 +109,7 @@ | |
* :param: e.detail.command Example: "H" | ||
* :param: e.detail.text Example: "Show this help." | ||
* :param: e.detail.row Row index from 0 to 9 where to place this help text. Example: 0 | ||
* :param: e.detail.short If this help must appear in the short help. Example: true | ||
*/ | ||
document.addEventListener( "impress:help:add", function( e ) { | ||
|
||
|
@@ -80,39 +118,44 @@ | |
// its own array. If there are more than one entry for the same index, they are shown in | ||
// first come, first serve ordering. | ||
var rowIndex = e.detail.row; | ||
if ( typeof rows[ rowIndex ] !== "object" || !rows[ rowIndex ].isArray ) { | ||
rows[ rowIndex ] = []; | ||
} | ||
rows[ e.detail.row ].push( "<tr><td><strong>" + e.detail.command + "</strong></td><td>" + | ||
e.detail.text + "</td></tr>" ); | ||
var short = e.detail.short; | ||
rows[ rowIndex ] = rows[ rowIndex ] || []; | ||
rows[ e.detail.row ].push( { | ||
row: "<tr><td><strong>" + e.detail.command + "</strong></td><td>" + | ||
e.detail.text + "</td></tr>", | ||
short: !!short | ||
} ); | ||
renderHelpDiv(); | ||
} ); | ||
|
||
document.addEventListener( "impress:init", function( e ) { | ||
var api = e.detail.api; | ||
var gc = api.lib.gc; | ||
var util = api.lib.util; | ||
|
||
renderHelpDiv(); | ||
|
||
// At start, show the help for 7 seconds. | ||
var helpDiv = document.getElementById( "impress-help" ); | ||
if ( helpDiv ) { | ||
helpDiv.style.display = "block"; | ||
timeoutHandle = window.setTimeout( function() { | ||
var helpDiv = document.getElementById( "impress-help" ); | ||
helpDiv.style.display = "none"; | ||
}, 7000 ); | ||
|
||
// Regster callback to empty the help div on teardown | ||
var api = e.detail.api; | ||
api.lib.gc.pushCallback( function() { | ||
gc.pushCallback( function() { | ||
window.clearTimeout( timeoutHandle ); | ||
helpDiv.style.display = ""; | ||
helpDiv.innerHTML = ""; | ||
helpDiv.remove(); | ||
fullHelpDiv.remove(); | ||
rows = []; | ||
} ); | ||
} | ||
|
||
// Use our own API to register the help text for "h" | ||
triggerEvent( document, "impress:help:add", | ||
{ command: "H", text: "Show this help", row: 0 } ); | ||
util.triggerEvent( document, "impress:help:add", | ||
{ command: "H", text: "Show/hide this help", row: 0, short: true } ); | ||
util.triggerEvent( document, "impress:help:add", | ||
{ command: "F1", text: "Show/hide full help", row: 999, short: true } ); | ||
} ); | ||
|
||
} )( document, window ); | ||
|
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.
This needs to stay. Plugins that display something on the screen are required to be optional. One way to make them optional is that the presentation author must explicitly add an empty div for them to appear.
In the case of the help plugin, note that it is shown automatically when the presentation is loaded. Due to this, the div must stay.