Bugfx: Correct new JS api.

This commit is contained in:
Starbeamrainbowlabs 2016-11-05 14:08:50 +00:00
parent 9d25376d82
commit 0c625cd3da
4 changed files with 57 additions and 47 deletions

View File

@ -1245,7 +1245,7 @@ class page_renderer
$result .= self::getJS(); $result .= self::getJS();
if(module_exists("feature-search")) if(module_exists("feature-search"))
$result .= "\t\t<link type='application/opensearchdescription+xml' rel='search' href='?action=opensearch-description' />"; $result .= "\t\t<link type='application/opensearchdescription+xml' rel='search' href='?action=opensearch-description' />\n";
if(!empty($settings->enable_math_rendering)) if(!empty($settings->enable_math_rendering))
{ {
@ -1303,18 +1303,18 @@ class page_renderer
private static $jsLinks = []; private static $jsLinks = [];
public function AddJSLink(string $scriptUrl) public function AddJSLink(string $scriptUrl)
{ {
$jsLinks[] = $scriptUrl; static::$jsLinks[] = $scriptUrl;
} }
public function AddJSSnippet(string $script) public function AddJSSnippet(string $script)
{ {
$jsSnippets[] = $script; static::$jsSnippets[] = $script;
} }
private static function getJS() private static function getJS()
{ {
$result = ""; $result = "<!-- Javascript -->\n";
foreach(static::$jsSnippets as $snippet) foreach(static::$jsSnippets as $snippet)
$result .= "<script defer>$snippet</script>\n"; $result .= "<script defer>\n$snippet\n</script>\n";
foreach(static::$jsLinks as $link) foreach(static::$jsLinks as $link)
$result .= "<script src='" . $link . "' defer></script>\n"; $result .= "<script src='" . $link . "' defer></script>\n";
return $result; return $result;
@ -3941,29 +3941,34 @@ register_module([
<p class='editing-message'>$settings->editing_message</p> <p class='editing-message'>$settings->editing_message</p>
<input name='submit-edit' type='submit' value='Save Page' tabindex='3' /> <input name='submit-edit' type='submit' value='Save Page' tabindex='3' />
</form>"; </form>";
page_renderer::AddJSSnippet("// Adapted from https://jsfiddle.net/2wAzx/13/ // Allow tab characters in the page editor
document.querySelector(\"[name=content]\").addEventListener(\"keydown\", (event) => { page_renderer::AddJSSnippet("window.addEventListener('load', function(event) {
if(event.keyCode !== 9) return true; // Adapted from https://jsfiddle.net/2wAzx/13/
var currentValue = event.target.value, startPos = event.target.selectionStart, endPos = event.target.selectionEnd; document.querySelector(\"[name=content]\").addEventListener(\"keydown\", (event) => {
event.target.value = currentValue.substring(0, startPos) + \"\\t\" + currentValue.substring(endPos); if(event.keyCode !== 9) return true;
event.target.selectionStart = event.target.selectionEnd = startPos + 1; var currentValue = event.target.value, startPos = event.target.selectionStart, endPos = event.target.selectionEnd;
event.stopPropagation(); event.preventDefault(); event.target.value = currentValue.substring(0, startPos) + \"\\t\" + currentValue.substring(endPos);
return false; event.target.selectionStart = event.target.selectionEnd = startPos + 1;
event.stopPropagation(); event.preventDefault();
return false;
});
});"); });");
// ~ // ~
/// ~~~ Smart saving ~~~ /// /// ~~~ Smart saving ~~~ ///
page_renderer::AddJSSnippet('// Smart saving page_renderer::AddJSSnippet('document.addEventListener("load", function(event) {
function getSmartSaveKey() { return document.querySelector("main h1").innerHTML.replace("Creating ", "").replace("Editing ", "").trim(); } // Smart saving
// Saving function getSmartSaveKey() { return document.querySelector("main h1").innerHTML.replace("Creating ", "").replace("Editing ", "").trim(); }
document.querySelector("textarea[name=content]").addEventListener("keyup", function(event) { window.localStorage.setItem(getSmartSaveKey(), event.target.value) }); // Saving
// Loading document.querySelector("textarea[name=content]").addEventListener("keyup", function(event) { window.localStorage.setItem(getSmartSaveKey(), event.target.value) });
window.addEventListener("load", function(event) { // Loading
var editor = document.querySelector("textarea[name=content]"); window.addEventListener("load", function(event) {
if(editor.value.length > 0) return; // Don\'t restore if there\'s data in the editor already var editor = document.querySelector("textarea[name=content]");
editor.value = localStorage.getItem(getSmartSaveKey()); if(editor.value.length > 0) return; // Don\'t restore if there\'s data in the editor already
editor.value = localStorage.getItem(getSmartSaveKey());
});
});'); });');
exit(page_renderer::render_main("$title - $settings->sitename", $content)); exit(page_renderer::render_main("$title - $settings->sitename", $content));
@ -5174,7 +5179,7 @@ register_module([
<tr><th>Type this</th><th>To get this</th></tr> <tr><th>Type this</th><th>To get this</th></tr>
<tr><td><code>{{{@}}}</code></td><td>Lists all variables and their values in a table.</td></tr> <tr><td><code>{{{@}}}</code></td><td>Lists all variables and their values in a table.</td></tr>
<tr><td><code>{{{#}}}</code></td><td>Shows a 'stack trace', outlining all the parent includes of the current page being parsed.</td></tr> <tr><td><code>{{{#}}}</code></td><td>Shows a 'stack trace', outlining all the parent includes of the current page being parsed.</td></tr>
<tr><td><code>{{{~}}}</code></td><td>Outputs the requested pagee's name.</td></tr> <tr><td><code>{{{~}}}</code></td><td>Outputs the requested page's name.</td></tr>
<tr><td><code>{{{*}}}</code></td><td>Outputs a comma separated list of all the subpages of the current page.</td></tr> <tr><td><code>{{{*}}}</code></td><td>Outputs a comma separated list of all the subpages of the current page.</td></tr>
<tr><td><code>{{{+}}}</code></td><td>Shows a gallery containing all the files that are sub pages of the current page.</td></tr> <tr><td><code>{{{+}}}</code></td><td>Shows a gallery containing all the files that are sub pages of the current page.</td></tr>
</table>"); </table>");

View File

@ -936,7 +936,7 @@ class page_renderer
$result .= self::getJS(); $result .= self::getJS();
if(module_exists("feature-search")) if(module_exists("feature-search"))
$result .= "\t\t<link type='application/opensearchdescription+xml' rel='search' href='?action=opensearch-description' />"; $result .= "\t\t<link type='application/opensearchdescription+xml' rel='search' href='?action=opensearch-description' />\n";
if(!empty($settings->enable_math_rendering)) if(!empty($settings->enable_math_rendering))
{ {
@ -994,18 +994,18 @@ class page_renderer
private static $jsLinks = []; private static $jsLinks = [];
public function AddJSLink(string $scriptUrl) public function AddJSLink(string $scriptUrl)
{ {
$jsLinks[] = $scriptUrl; static::$jsLinks[] = $scriptUrl;
} }
public function AddJSSnippet(string $script) public function AddJSSnippet(string $script)
{ {
$jsSnippets[] = $script; static::$jsSnippets[] = $script;
} }
private static function getJS() private static function getJS()
{ {
$result = ""; $result = "<!-- Javascript -->\n";
foreach(static::$jsSnippets as $snippet) foreach(static::$jsSnippets as $snippet)
$result .= "<script defer>$snippet</script>\n"; $result .= "<script defer>\n$snippet\n</script>\n";
foreach(static::$jsLinks as $link) foreach(static::$jsLinks as $link)
$result .= "<script src='" . $link . "' defer></script>\n"; $result .= "<script src='" . $link . "' defer></script>\n";
return $result; return $result;

View File

@ -122,7 +122,7 @@
"author": "Starbeamrainbowlabs", "author": "Starbeamrainbowlabs",
"description": "Allows you to edit pages by adding the edit and save actions. You should probably include this one.", "description": "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
"id": "page-edit", "id": "page-edit",
"lastupdate": 1477821977, "lastupdate": 1478354728,
"optional": false "optional": false
}, },
{ {
@ -212,7 +212,7 @@
"author": "Emanuil Rusev & Starbeamrainbowlabs", "author": "Emanuil Rusev & Starbeamrainbowlabs",
"description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation, and also *requires* write access to the disk on first load.", "description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation, and also *requires* write access to the disk on first load.",
"id": "parser-parsedown", "id": "parser-parsedown",
"lastupdate": 1476906745, "lastupdate": 1478204933,
"optional": false "optional": false
} }
] ]

View File

@ -88,29 +88,34 @@ register_module([
<p class='editing-message'>$settings->editing_message</p> <p class='editing-message'>$settings->editing_message</p>
<input name='submit-edit' type='submit' value='Save Page' tabindex='3' /> <input name='submit-edit' type='submit' value='Save Page' tabindex='3' />
</form>"; </form>";
page_renderer::AddJSSnippet("// Adapted from https://jsfiddle.net/2wAzx/13/ // Allow tab characters in the page editor
document.querySelector(\"[name=content]\").addEventListener(\"keydown\", (event) => { page_renderer::AddJSSnippet("window.addEventListener('load', function(event) {
if(event.keyCode !== 9) return true; // Adapted from https://jsfiddle.net/2wAzx/13/
var currentValue = event.target.value, startPos = event.target.selectionStart, endPos = event.target.selectionEnd; document.querySelector(\"[name=content]\").addEventListener(\"keydown\", (event) => {
event.target.value = currentValue.substring(0, startPos) + \"\\t\" + currentValue.substring(endPos); if(event.keyCode !== 9) return true;
event.target.selectionStart = event.target.selectionEnd = startPos + 1; var currentValue = event.target.value, startPos = event.target.selectionStart, endPos = event.target.selectionEnd;
event.stopPropagation(); event.preventDefault(); event.target.value = currentValue.substring(0, startPos) + \"\\t\" + currentValue.substring(endPos);
return false; event.target.selectionStart = event.target.selectionEnd = startPos + 1;
event.stopPropagation(); event.preventDefault();
return false;
});
});"); });");
// ~ // ~
/// ~~~ Smart saving ~~~ /// /// ~~~ Smart saving ~~~ ///
page_renderer::AddJSSnippet('// Smart saving page_renderer::AddJSSnippet('document.addEventListener("load", function(event) {
function getSmartSaveKey() { return document.querySelector("main h1").innerHTML.replace("Creating ", "").replace("Editing ", "").trim(); } // Smart saving
// Saving function getSmartSaveKey() { return document.querySelector("main h1").innerHTML.replace("Creating ", "").replace("Editing ", "").trim(); }
document.querySelector("textarea[name=content]").addEventListener("keyup", function(event) { window.localStorage.setItem(getSmartSaveKey(), event.target.value) }); // Saving
// Loading document.querySelector("textarea[name=content]").addEventListener("keyup", function(event) { window.localStorage.setItem(getSmartSaveKey(), event.target.value) });
window.addEventListener("load", function(event) { // Loading
var editor = document.querySelector("textarea[name=content]"); window.addEventListener("load", function(event) {
if(editor.value.length > 0) return; // Don\'t restore if there\'s data in the editor already var editor = document.querySelector("textarea[name=content]");
editor.value = localStorage.getItem(getSmartSaveKey()); if(editor.value.length > 0) return; // Don\'t restore if there\'s data in the editor already
editor.value = localStorage.getItem(getSmartSaveKey());
});
});'); });');
exit(page_renderer::render_main("$title - $settings->sitename", $content)); exit(page_renderer::render_main("$title - $settings->sitename", $content));