From f5090a42b1f134e62f7788bf84d21b1a6c4e118e Mon Sep 17 00:00:00 2001 From: Gregor Aisch Date: Wed, 11 Oct 2017 11:16:26 +0200 Subject: [PATCH] save draft in localstorage (solves #5) --- build/embed.js | 2 +- src/embed/index.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/build/embed.js b/build/embed.js index fa32cad..6791871 100644 --- a/build/embed.js +++ b/build/embed.js @@ -1 +1 @@ -!function(){"use strict";var n="function"==typeof fetch?fetch.bind():function(n,t){return t=t||{},new Promise(function(e,s){function a(){var n,t=[],e=[],s={};return o.getAllResponseHeaders().replace(/^(.*?):\s*([\s\S]*?)$/gm,function(a,o,c){t.push(o=o.toLowerCase()),e.push([o,c]),n=s[o],s[o]=n?n+","+c:c}),{ok:1==(o.status/200|0),status:o.status,statusText:o.statusText,url:o.responseURL,clone:a,text:function(){return Promise.resolve(o.responseText)},json:function(){return Promise.resolve(o.responseText).then(JSON.parse)},blob:function(){return Promise.resolve(new Blob([o.response]))},headers:{keys:function(){return t},entries:function(){return e},get:function(n){return s[n.toLowerCase()]},has:function(n){return n.toLowerCase()in s}}}}var o=new XMLHttpRequest;o.open(t.method||"get",n);for(var c in t.headers)o.setRequestHeader(c,t.headers[c]);o.withCredentials="include"==t.credentials,o.onload=function(){e(a())},o.onerror=s,o.send(t.body)})},t=function(n){var t,e="";return n.user?e+='\n
\n \n \n
\n \n  \n
\n (signed in as @'+(null==(t=n.user.name)?"":t)+' :: sign out)\n
\n':(e+="\nTo post a comment you need to sign in via
\n",n.auth.forEach(function(n,s){e+="\n "+(null==(t=s?" or ":"")?"":t)+'\n"}),e+="\n"),e+='\n"};!function(){function e(){n(l,{credentials:"include",headers:{"Content-Type":"application/json"}}).then(function(n){return n.json()}).then(function(a){s(u).innerHTML=t(a);var o=s(u+" textarea.schnack-body"),c=s(u+" .schnack-form blockquote.schnack-body"),r=s(u+" .schnack-button"),d=s(u+" .schnack-preview"),h=s(u+" .schnack-write");if(r&&(r.addEventListener("click",function(t){var s=o.value;n(l,{credentials:"include",method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({comment:s})}).then(function(n){return n.json()}).then(function(n){console.log(n),e()})}),d.addEventListener("click",function(t){var e=o.value;o.style.display="none",d.style.display="none",c.style.display="block",h.style.display="inline",n(i+"/markdown",{credentials:"include",method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({comment:e})}).then(function(n){return n.json()}).then(function(n){console.log(n),c.innerHTML=n.html})}),h.addEventListener("click",function(n){o.style.display="inline",d.style.display="inline",c.style.display="none",h.style.display="none"})),a.user){var p=s("a.schnack-signout");p&&p.addEventListener("click",function(t){t.preventDefault(),n(i+"/signout",{credentials:"include",headers:{"Content-Type":"application/json"}}).then(function(n){return n.json()}).then(function(n){console.log(n),e()})})}else a.auth.forEach(function(n){var t=s(u+" .schnack-signin-"+n.id);t&&t.addEventListener("click",function(t){var s=window.open(i+"/auth/"+n.id,n.name+" Sign-In","resizable,scrollbars,status,width=600,height=500");window.__schnack_wait_for_oauth=function(){s.close(),e()}})});if(a.user&&a.user.admin){var f=function(t){var s=t.target.dataset;n(i+"/"+s.class+"/"+s.target+"/"+s.action,{credentials:"include",method:"POST",headers:{"Content-Type":"application/json"},body:""}).then(function(n){return n.json()}).then(function(n){console.log(n),e()})};document.querySelectorAll(".schnack-action").forEach(function(n){n.addEventListener("click",f)})}})}var s=function(n){return document.querySelector(n)},a=s("script[data-schnack-target]");if(!a)return console.warn("schnack script tag needs some data attributes");var o=a.dataset,c=o.schnackSlug,r=new URL(a.getAttribute("src")),i=r.protocol+"//"+r.host,l=i+"/comments/"+c,u=o.schnackTarget;document.domain=r.host.split(".").slice(1).join("."),e()}()}(); +!function(){"use strict";var n="function"==typeof fetch?fetch.bind():function(n,t){return t=t||{},new Promise(function(e,s){function a(){var n,t=[],e=[],s={};return o.getAllResponseHeaders().replace(/^(.*?):\s*([\s\S]*?)$/gm,function(a,o,c){t.push(o=o.toLowerCase()),e.push([o,c]),n=s[o],s[o]=n?n+","+c:c}),{ok:1==(o.status/200|0),status:o.status,statusText:o.statusText,url:o.responseURL,clone:a,text:function(){return Promise.resolve(o.responseText)},json:function(){return Promise.resolve(o.responseText).then(JSON.parse)},blob:function(){return Promise.resolve(new Blob([o.response]))},headers:{keys:function(){return t},entries:function(){return e},get:function(n){return s[n.toLowerCase()]},has:function(n){return n.toLowerCase()in s}}}}var o=new XMLHttpRequest;o.open(t.method||"get",n);for(var c in t.headers)o.setRequestHeader(c,t.headers[c]);o.withCredentials="include"==t.credentials,o.onload=function(){e(a())},o.onerror=s,o.send(t.body)})},t=function(n){var t,e="";return n.user?e+='\n
\n \n \n
\n \n  \n
\n (signed in as @'+(null==(t=n.user.name)?"":t)+' :: sign out)\n
\n':(e+="\nTo post a comment you need to sign in via
\n",n.auth.forEach(function(n,s){e+="\n "+(null==(t=s?" or ":"")?"":t)+'\n"}),e+="\n"),e+='\n"};!function(){function e(){n(l,{credentials:"include",headers:{"Content-Type":"application/json"}}).then(function(n){return n.json()}).then(function(a){s(u).innerHTML=t(a);var o=s(u+" textarea.schnack-body"),r=s(u+" .schnack-form blockquote.schnack-body"),d=window.localStorage.getItem("schnack-draft-"+c);d&&(o.value=d);var h=s(u+" .schnack-button"),p=s(u+" .schnack-preview"),f=s(u+" .schnack-write");if(h&&(h.addEventListener("click",function(t){var s=o.value;n(l,{credentials:"include",method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({comment:s})}).then(function(n){return n.json()}).then(function(n){console.log(n),e()})}),p.addEventListener("click",function(t){var e=o.value;o.style.display="none",p.style.display="none",r.style.display="block",f.style.display="inline",n(i+"/markdown",{credentials:"include",method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({comment:e})}).then(function(n){return n.json()}).then(function(n){console.log(n),r.innerHTML=n.html})}),f.addEventListener("click",function(n){o.style.display="inline",p.style.display="inline",r.style.display="none",f.style.display="none"}),o.addEventListener("keyup",function(){window.localStorage.setItem("schnack-draft-"+c,o.value)})),a.user){var k=s("a.schnack-signout");k&&k.addEventListener("click",function(t){t.preventDefault(),n(i+"/signout",{credentials:"include",headers:{"Content-Type":"application/json"}}).then(function(n){return n.json()}).then(function(n){console.log(n),e()})})}else a.auth.forEach(function(n){var t=s(u+" .schnack-signin-"+n.id);t&&t.addEventListener("click",function(t){var s=window.open(i+"/auth/"+n.id,n.name+" Sign-In","resizable,scrollbars,status,width=600,height=500");window.__schnack_wait_for_oauth=function(){s.close(),e()}})});if(a.user&&a.user.admin){var m=function(t){var s=t.target.dataset;n(i+"/"+s.class+"/"+s.target+"/"+s.action,{credentials:"include",method:"POST",headers:{"Content-Type":"application/json"},body:""}).then(function(n){return n.json()}).then(function(n){console.log(n),e()})};document.querySelectorAll(".schnack-action").forEach(function(n){n.addEventListener("click",m)})}})}var s=function(n){return document.querySelector(n)},a=s("script[data-schnack-target]");if(!a)return console.warn("schnack script tag needs some data attributes");var o=a.dataset,c=o.schnackSlug,r=new URL(a.getAttribute("src")),i=r.protocol+"//"+r.host,l=i+"/comments/"+c,u=o.schnackTarget;document.domain=r.host.split(".").slice(1).join("."),e()}()}(); diff --git a/src/embed/index.js b/src/embed/index.js index 4d39d07..a948a53 100644 --- a/src/embed/index.js +++ b/src/embed/index.js @@ -29,6 +29,9 @@ import comments_tpl from './comments.jst.html'; const textarea = $(`${target} textarea.schnack-body`); const preview = $(`${target} .schnack-form blockquote.schnack-body`); + const draft = window.localStorage.getItem(`schnack-draft-${slug}`); + if (draft) textarea.value = draft; + const postBtn = $(target + ' .schnack-button'); const previewBtn = $(target + ' .schnack-preview'); const writeBtn = $(target + ' .schnack-write'); @@ -75,6 +78,10 @@ import comments_tpl from './comments.jst.html'; preview.style.display = 'none'; writeBtn.style.display = 'none'; }); + + textarea.addEventListener('keyup', () => { + window.localStorage.setItem(`schnack-draft-${slug}`, textarea.value); + }); } if (data.user) { const signout = $('a.schnack-signout');