From b8f659f3a48d4c6e523a5884ba00643df51346b5 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 17 Mar 2022 19:27:17 +0300 Subject: add "Edit" & "Download" buttons --- assets/css/main.css | 8 +++++++ assets/js/main.js | 67 +++++++++++++++++++++++++++++++++++++++++------------ index.html | 2 ++ 3 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 assets/css/main.css diff --git a/assets/css/main.css b/assets/css/main.css new file mode 100644 index 0000000..ef61a26 --- /dev/null +++ b/assets/css/main.css @@ -0,0 +1,8 @@ +.pre_container { + position: relative; +} +.pre_buttons { + position: absolute; + top: 5px; + right: 5px; +} diff --git a/assets/js/main.js b/assets/js/main.js index 89551da..0d203dd 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -341,33 +341,70 @@ Data.prototype.hide_advanced = function() { }); } -function format_pre_text(text) { - return $('
').text(text);
+function edit_btn_on_click(btn, pre) {
+    var editable = pre.prop('isContentEditable');
+    pre.prop('contentEditable', !editable);
+    if (editable) {
+        btn.text('Edit');
+        btn.blur(); // a.k.a. unfocus
+    } else {
+        btn.text('Save');
+        pre.focus();
+    }
 }
 
-var ConfigFile = function(name, text) {
-    this.name = name;
-    this.text = text;
+function basename(path) {
+    return path.substring(path.lastIndexOf('/') + 1);
 }
 
-ConfigFile.prototype.toString = function() {
-    return this.text;
+function dload_btn_on_click(btn, path, pre) {
+    var blob = new Blob([pre.text()], {type: 'text/plain'});
+    var url = URL.createObjectURL(blob);
+
+    var link = $('');
+    link.prop('href', url);
+    link.prop('download', basename(path));
+
+    // Whoever thought of this [0] is fucking crazy:
+    // https://stackoverflow.com/a/36483380/514684
+    // It literally silently does nothing if this is omitted.
+    link[0].click();
 }
 
-ConfigFile.prototype.format = function() {
-    return format_pre_text(this.toString());
+function make_pre_buttons(path, pre) {
+    var edit_btn = $('