From e0aeda8b170ed5850c48641f4614cb115655dcdc Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 12 Dec 2017 23:00:29 +0000
Subject: [PATCH] Add short markdown embed code input box to file pages

---
 Changelog.md               |  3 +++
 build/index.php            | 16 ++++++++++++++--
 module_index.json          |  6 +++---
 modules/feature-upload.php | 13 ++++++++++++-
 4 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/Changelog.md b/Changelog.md
index 8c8d2b0..60c8b74 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -3,6 +3,9 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
 
 ## v0.16-dev
 
+### Added
+ - Added an input box with auto-generated short markdown embed code with copy button
+
 ### Fixed
  - Fix saving edits to pages with an ampersand in their name (#99)
 
diff --git a/build/index.php b/build/index.php
index 61d3520..1416ce2 100644
--- a/build/index.php
+++ b/build/index.php
@@ -5348,7 +5348,7 @@ register_module([
 				}
 				
 				$fileInfo = [];
-				$fileInfo["Name"] = str_replace("File/", "", $filepath);
+				$fileInfo["Name"] = str_replace("Files/", "", $filepath);
 				$fileInfo["Type"] = $mime_type;
 				$fileInfo["Size"] = human_filesize(filesize($env->storage_prefix . $filepath));
 				switch($fileTypeDisplay)
@@ -5359,6 +5359,7 @@ register_module([
 						break;
 				}
 				$fileInfo["Uploaded by"] = $pageindex->{$env->page}->lasteditor;
+				$fileInfo["Short markdown embed code"] = "<input type='text' class='short-embed-markdown-code' value='![{$fileInfo["Name"]}]($filepath | right | 350x350)' readonly /> <button class='short-embed-markdown-button'>Copy</button>";
 				
 				$preview_html .= "\t\t\t<h2>File Information</h2>
 			<table>";
@@ -5372,6 +5373,17 @@ register_module([
 			}
 		});
 		
+		// Add the snippet that copies the embed markdown code to the clipboard
+		page_renderer::AddJSSnippet('window.addEventListener("load", function(event) {
+	console.log("initialising copy driver");
+	let button = document.querySelector(".short-embed-markdown-button");
+	button.addEventListener("click", function(inner_event) {
+		let input = document.querySelector(".short-embed-markdown-code");
+		input.select();
+		button.innerHTML = document.execCommand("copy") ? "Copied!" : "Failed to copy :-(";
+	});
+});');
+		
 		// Register a section on the help page on uploading files
 		add_help_section("28-uploading-files", "Uploading Files", "<p>$settings->sitename supports the uploading of files, though it is up to " . $settings->admindetails_name . ", $settings->sitename's administrator as to whether it is enabled or not (uploads are currently " . (($settings->upload_enabled) ? "enabled" : "disabled") . ").</p>
 		<p>Currently Pepperminty Wiki (the software that $settings->sitename uses) only supports the uploading of images, although more file types should be supported in the future (<a href='//github.com/sbrl/Pepperminty-Wiki/issues'>open an issue on GitHub</a> if you are interested in support for more file types).</p>
@@ -6084,7 +6096,7 @@ register_module([
 
 register_module([
 	"name" => "Page editor",
-	"version" => "0.17",
+	"version" => "0.17.1",
 	"author" => "Starbeamrainbowlabs",
 	"description" => "Allows you to edit pages by adding the edit and save actions. You should probably include this one.",
 	"id" => "page-edit",
diff --git a/module_index.json b/module_index.json
index aa12c34..a93c893 100755
--- a/module_index.json
+++ b/module_index.json
@@ -122,7 +122,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": 1505512979,
+        "lastupdate": 1513119449,
         "optional": false
     },
     {
@@ -163,11 +163,11 @@
     },
     {
         "name": "Page editor",
-        "version": "0.17",
+        "version": "0.17.1",
         "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": 1512908662,
+        "lastupdate": 1512919152,
         "optional": false
     },
     {
diff --git a/modules/feature-upload.php b/modules/feature-upload.php
index e7c3309..ebb686a 100644
--- a/modules/feature-upload.php
+++ b/modules/feature-upload.php
@@ -504,7 +504,7 @@ register_module([
 				}
 				
 				$fileInfo = [];
-				$fileInfo["Name"] = str_replace("File/", "", $filepath);
+				$fileInfo["Name"] = str_replace("Files/", "", $filepath);
 				$fileInfo["Type"] = $mime_type;
 				$fileInfo["Size"] = human_filesize(filesize($env->storage_prefix . $filepath));
 				switch($fileTypeDisplay)
@@ -515,6 +515,7 @@ register_module([
 						break;
 				}
 				$fileInfo["Uploaded by"] = $pageindex->{$env->page}->lasteditor;
+				$fileInfo["Short markdown embed code"] = "<input type='text' class='short-embed-markdown-code' value='![{$fileInfo["Name"]}]($filepath | right | 350x350)' readonly /> <button class='short-embed-markdown-button'>Copy</button>";
 				
 				$preview_html .= "\t\t\t<h2>File Information</h2>
 			<table>";
@@ -528,6 +529,16 @@ register_module([
 			}
 		});
 		
+		// Add the snippet that copies the embed markdown code to the clipboard
+		page_renderer::AddJSSnippet('window.addEventListener("load", function(event) {
+	let button = document.querySelector(".short-embed-markdown-button");
+	button.addEventListener("click", function(inner_event) {
+		let input = document.querySelector(".short-embed-markdown-code");
+		input.select();
+		button.innerHTML = document.execCommand("copy") ? "Copied!" : "Failed to copy :-(";
+	});
+});');
+		
 		// Register a section on the help page on uploading files
 		add_help_section("28-uploading-files", "Uploading Files", "<p>$settings->sitename supports the uploading of files, though it is up to " . $settings->admindetails_name . ", $settings->sitename's administrator as to whether it is enabled or not (uploads are currently " . (($settings->upload_enabled) ? "enabled" : "disabled") . ").</p>
 		<p>Currently Pepperminty Wiki (the software that $settings->sitename uses) only supports the uploading of images, although more file types should be supported in the future (<a href='//github.com/sbrl/Pepperminty-Wiki/issues'>open an issue on GitHub</a> if you are interested in support for more file types).</p>
