From d3b6b1c54e56990da14d1f547d5e2ce7249bbe85 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 22 Oct 2016 16:43:53 +0100
Subject: [PATCH] Add 'smart save' feature to page creator. Never again will
 people loose their work.

---
 build/index.php       | 24 +++++++++++++++++++++---
 module_index.json     | 22 +++++++++++-----------
 modules/page-edit.php | 18 ++++++++++++++++++
 3 files changed, 50 insertions(+), 14 deletions(-)

diff --git a/build/index.php b/build/index.php
index 9626cce..1412145 100755
--- a/build/index.php
+++ b/build/index.php
@@ -1372,7 +1372,7 @@ if(isset($_GET["revision"]) and is_numeric($_GET["revision"]))
 $env->page_filename = $env->storage_prefix;
 if($env->is_history_revision)
 	$env->page_filename .= $pageindex->{$env->page}->history[$env->history->revision_number]->filename;
-else
+else if(isset($pageindex->{$env->page}))
 	$env->page_filename .= $pageindex->{$env->page}->filename;
 
 $env->action = strtolower($_GET["action"]);
@@ -3845,6 +3845,24 @@ register_module([
 				});
 			</script>
 		</form>";
+			
+			// ~
+			
+			/// ~~~ Smart saving ~~~ ///
+			
+			$content .= <<<SMARTSAVE
+<!-- Smart saving script -->
+<script>
+	function getSmartSaveKey() { return document.querySelector("main h1").innerHTML.replace("Creating ", "").trim(); }
+	// Saving
+	document.querySelector("textarea[name=content]").addEventListener("keyup", function(event) { window.localStorage.setItem(getSmartSaveKey(), event.target.value) });
+	// Loading
+	window.addEventListener("load", function(event) {
+		document.querySelector("textarea[name=content]").value = localStorage.getItem(getSmartSaveKey());
+	});
+</script>
+SMARTSAVE;
+			
 			exit(page_renderer::render_main("$title - $settings->sitename", $content));
 		});
 		
@@ -4743,14 +4761,14 @@ register_module([
 
 register_module([
 	"name" => "Update",
-	"version" => "0.6.1",
+	"version" => "0.6.2",
 	"author" => "Starbeamrainbowlabs",
 	"description" => "Adds an update page that downloads the latest stable version of Pepperminty Wiki. This module is currently outdated as it doesn't save your module preferences.",
 	"id" => "page-update",
 	"code" => function() {
 		
 		/**
-		 * @api		{get}	?action=move[do=yes]	Update the wiki
+		 * @api		{get}	?action=update[do=yes]	Update the wiki
 		 * @apiDescription	Update the wiki by downloading  a new version of Pepperminty Wiki from the URL specified in the settings. Note that unless you change the url from it's default, all custom modules installed will be removed. **Note also that this plugin is currently out of date. Use with extreme caution!**
 		 * @apiName			Update
 		 * @apiGroup		Utility
diff --git a/module_index.json b/module_index.json
index a42949d..fcc9844 100755
--- a/module_index.json
+++ b/module_index.json
@@ -23,7 +23,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds a 'raw' action that shows you the raw source of a page.",
         "id": "action-raw",
-        "lastupdate": 1476813024,
+        "lastupdate": 1476906745,
         "optional": false
     },
     {
@@ -41,7 +41,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "The module everyone has been waiting for! Adds a web based gui that lets mods change the wiki settings.",
         "id": "feature-guiconfig",
-        "lastupdate": 1476815055,
+        "lastupdate": 1476906745,
         "optional": false
     },
     {
@@ -59,7 +59,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds recent changes. Access through the 'recent-changes' action.",
         "id": "feature-recent-changes",
-        "lastupdate": 1476815055,
+        "lastupdate": 1476906745,
         "optional": false
     },
     {
@@ -77,7 +77,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
         "id": "feature-search",
-        "lastupdate": 1476815055,
+        "lastupdate": 1476906745,
         "optional": false
     },
     {
@@ -86,7 +86,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds the ability to upload files to Pepperminty Wiki. Uploaded files act as pages and have the special 'File\/' prefix.",
         "id": "feature-upload",
-        "lastupdate": 1476815055,
+        "lastupdate": 1476906745,
         "optional": false
     },
     {
@@ -122,7 +122,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
         "id": "page-edit",
-        "lastupdate": 1476815055,
+        "lastupdate": 1477150472,
         "optional": false
     },
     {
@@ -176,16 +176,16 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds an action to allow administrators to move pages.",
         "id": "page-move",
-        "lastupdate": 1476815055,
+        "lastupdate": 1476906745,
         "optional": false
     },
     {
         "name": "Update",
-        "version": "0.6.1",
+        "version": "0.6.2",
         "author": "Starbeamrainbowlabs",
         "description": "Adds an update page that downloads the latest stable version of Pepperminty Wiki. This module is currently outdated as it doesn't save your module preferences.",
         "id": "page-update",
-        "lastupdate": 1466012454,
+        "lastupdate": 1476906745,
         "optional": false
     },
     {
@@ -194,7 +194,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Allows you to view pages. You really should include this one.",
         "id": "page-view",
-        "lastupdate": 1476815512,
+        "lastupdate": 1476906745,
         "optional": false
     },
     {
@@ -212,7 +212,7 @@
         "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.",
         "id": "parser-parsedown",
-        "lastupdate": 1476815055,
+        "lastupdate": 1476906745,
         "optional": false
     }
 ]
\ No newline at end of file
diff --git a/modules/page-edit.php b/modules/page-edit.php
index c2b8f03..4fac54a 100755
--- a/modules/page-edit.php
+++ b/modules/page-edit.php
@@ -99,6 +99,24 @@ register_module([
 				});
 			</script>
 		</form>";
+			
+			// ~
+			
+			/// ~~~ Smart saving ~~~ ///
+			
+			$content .= <<<SMARTSAVE
+<!-- Smart saving script -->
+<script>
+	function getSmartSaveKey() { return document.querySelector("main h1").innerHTML.replace("Creating ", "").trim(); }
+	// Saving
+	document.querySelector("textarea[name=content]").addEventListener("keyup", function(event) { window.localStorage.setItem(getSmartSaveKey(), event.target.value) });
+	// Loading
+	window.addEventListener("load", function(event) {
+		document.querySelector("textarea[name=content]").value = localStorage.getItem(getSmartSaveKey());
+	});
+</script>
+SMARTSAVE;
+			
 			exit(page_renderer::render_main("$title - $settings->sitename", $content));
 		});
 		
