From 1db489a0aa2b85558d793d2b2716cc7a78331a31 Mon Sep 17 00:00:00 2001 From: ed Date: Sat, 7 Aug 2021 21:35:24 +0200 Subject: [PATCH] port changes to mde --- copyparty/web/mde.html | 1 + copyparty/web/mde.js | 74 +++++++++++++++++++----------------------- copyparty/web/ui.css | 2 +- 3 files changed, 35 insertions(+), 42 deletions(-) diff --git a/copyparty/web/mde.html b/copyparty/web/mde.html index 77f0cb50..5170865f 100644 --- a/copyparty/web/mde.html +++ b/copyparty/web/mde.html @@ -3,6 +3,7 @@ 📝🎉 {{ title }} + diff --git a/copyparty/web/mde.js b/copyparty/web/mde.js index f3a91744..5a2dc98a 100644 --- a/copyparty/web/mde.js +++ b/copyparty/web/mde.js @@ -110,25 +110,31 @@ function save(mde) { return toast.inf(2, 'no changes'); var force = save_btn.classList.contains('force-save'); - if (force && !confirm('confirm that you wish to lose the changes made on the server since you opened this document')) - return toast.inf(3, 'aborted'); + function save2() { + var txt = mde.value(); - var txt = mde.value(); + var fd = new FormData(); + fd.append("act", "tput"); + fd.append("lastmod", (force ? -1 : last_modified)); + fd.append("body", txt); - var fd = new FormData(); - fd.append("act", "tput"); - fd.append("lastmod", (force ? -1 : last_modified)); - fd.append("body", txt); + var url = (document.location + '').split('?')[0]; + var xhr = new XMLHttpRequest(); + xhr.open('POST', url, true); + xhr.responseType = 'text'; + xhr.onreadystatechange = save_cb; + xhr.btn = save_btn; + xhr.mde = mde; + xhr.txt = txt; + xhr.send(fd); + } - var url = (document.location + '').split('?')[0]; - var xhr = new XMLHttpRequest(); - xhr.open('POST', url, true); - xhr.responseType = 'text'; - xhr.onreadystatechange = save_cb; - xhr.btn = save_btn; - xhr.mde = mde; - xhr.txt = txt; - xhr.send(fd); + if (!force) + save2(); + else + modal.confirm('confirm that you wish to lose the changes made on the server since you opened this document', save2, function () { + toast.inf(3, 'aborted'); + }); } function save_cb() { @@ -136,14 +142,14 @@ function save_cb() { return; if (this.status !== 200) - return alert('Error! The file was NOT saved.\n\n' + this.status + ": " + (this.responseText + '').replace(/^
/, ""));
+        return toast.err(0, 'Error!  The file was NOT saved.\n\n' + this.status + ": " + (this.responseText + '').replace(/^
/, ""));
 
     var r;
     try {
         r = JSON.parse(this.responseText);
     }
     catch (ex) {
-        return alert('Failed to parse reply from server:\n\n' + this.responseText);
+        return toast.err(0, 'Failed to parse reply from server:\n\n' + this.responseText);
     }
 
     if (!r.ok) {
@@ -158,12 +164,10 @@ function save_cb() {
                 r.lastmod + ' lastmod on the server now,',
                 r.now + ' server time now,\n',
             ];
-            alert(msg.join('\n'));
+            return toast.err(0, msg.join('\n'));
         }
-        else {
-            alert('Error! Save failed.  Maybe this JSON explains why:\n\n' + this.responseText);
-        }
-        return;
+        else
+            return toast.err(0, 'Error! Save failed.  Maybe this JSON explains why:\n\n' + this.responseText);
     }
 
     this.btn.classList.remove('force-save');
@@ -186,35 +190,23 @@ function save_chk() {
     if (this.readyState != XMLHttpRequest.DONE)
         return;
 
-    if (this.status !== 200) {
-        alert('Error!  The file was NOT saved.\n\n' + this.status + ": " + (this.responseText + '').replace(/^
/, ""));
-        return;
-    }
+    if (this.status !== 200)
+        return toast.err(0, 'Error!  The file was NOT saved.\n\n' + this.status + ": " + (this.responseText + '').replace(/^
/, ""));
 
     var doc1 = this.txt.replace(/\r\n/g, "\n");
     var doc2 = this.responseText.replace(/\r\n/g, "\n");
     if (doc1 != doc2) {
-        alert(
+        modal.alert(
             'Error! The document on the server does not appear to have saved correctly (your editor contents and the server copy is not identical). Place the document on your clipboard for now and check the server logs for hints\n\n' +
             'Length: yours=' + doc1.length + ', server=' + doc2.length
         );
-        alert('yours, ' + doc1.length + ' byte:\n[' + doc1 + ']');
-        alert('server, ' + doc2.length + ' byte:\n[' + doc2 + ']');
+        modal.alert('yours, ' + doc1.length + ' byte:\n[' + doc1 + ']');
+        modal.alert('server, ' + doc2.length + ' byte:\n[' + doc2 + ']');
         return;
     }
 
     last_modified = this.lastmod;
     md_changed(this.mde, true);
 
-    var ok = mknod('div');
-    ok.setAttribute('style', 'font-size:6em;font-family:serif;font-weight:bold;color:#cf6;background:#444;border-radius:.3em;padding:.6em 0;position:fixed;top:30%;left:calc(50% - 2em);width:4em;text-align:center;z-index:9001;transition:opacity 0.2s ease-in-out;opacity:1');
-    ok.innerHTML = 'OK✔️';
-    var parent = ebi('m');
-    document.documentElement.appendChild(ok);
-    setTimeout(function () {
-        ok.style.opacity = 0;
-    }, 500);
-    setTimeout(function () {
-        ok.parentNode.removeChild(ok);
-    }, 750);
+    toast.ok(2, 'save OK' + (this.ntry ? '\nattempt ' + this.ntry : ''));
 }
diff --git a/copyparty/web/ui.css b/copyparty/web/ui.css
index c9ac2f67..56fc3d02 100644
--- a/copyparty/web/ui.css
+++ b/copyparty/web/ui.css
@@ -66,7 +66,7 @@
 	background: #0be;
 }
 #toast.ok {
-	background: #4a0;
+	background: #380;
 	border-color: #8e4;
 }
 #toast.ok #toastc {