diff --git a/build/embed.js b/build/embed.js index f5e23a8..1036f61 100644 --- a/build/embed.js +++ b/build/embed.js @@ -1,373 +1 @@ -(function () { -'use strict'; - -var index = typeof fetch=='function' ? fetch.bind() : function(url, options) { - options = options || {}; - return new Promise( function (resolve, reject) { - var request = new XMLHttpRequest(); - - request.open(options.method || 'get', url); - - for (var i in options.headers) { - request.setRequestHeader(i, options.headers[i]); - } - - request.withCredentials = options.credentials=='include'; - - request.onload = function () { - resolve(response()); - }; - - request.onerror = reject; - - request.send(options.body); - - function response() { - var keys = [], - all = [], - headers = {}, - header; - - request.getAllResponseHeaders().replace(/^(.*?):\s*([\s\S]*?)$/gm, function (m, key, value) { - keys.push(key = key.toLowerCase()); - all.push([key, value]); - header = headers[key]; - headers[key] = header ? (header + "," + value) : value; - }); - - return { - ok: (request.status/200|0) == 1, // 200-299 - status: request.status, - statusText: request.statusText, - url: request.responseURL, - clone: response, - text: function () { return Promise.resolve(request.responseText); }, - json: function () { return Promise.resolve(request.responseText).then(JSON.parse); }, - blob: function () { return Promise.resolve(new Blob([request.response])); }, - headers: { - keys: function () { return keys; }, - entries: function () { return all; }, - get: function (n) { return headers[n.toLowerCase()]; }, - has: function (n) { return n.toLowerCase() in headers; } - } - }; - } - }); -}; - -var schnack_tpl = function(data) { -var __t, __p = ''; -if (data.user) { -__p += '\n '; - if (data.user.admin) { -__p += '\n
\n \n \n
\n '; - } -__p += '\n
\n (signed in as @' + -((__t = ( data.user.name )) == null ? '' : __t) + -' :: sign out)\n
\n
\n
\n \n \n
\n \n  \n  \n \n
\n
\n'; - } else { -__p += '\nTo post a comment you need to sign in via
\n'; - data.auth.forEach(function (provider, i) { -__p += '\n ' + -((__t = ( i ? ' or ' : '' )) == null ? '' : __t) + -'\n'; - }) ; -__p += '\n'; - } -__p += '\n'; - -var comments = []; -data.replies = {}; -data.comments.forEach(function (comment) { - if (comment.reply_to) { - if (!data.replies[comment.reply_to]) { data.replies[comment.reply_to] = []; } - data.replies[comment.reply_to].push(comment); - } else { - comments.push(comment); - } -}); -data.comments = comments; - -__p += '\n' + -((__t = ( data.comments_tpl(data) )) == null ? '' : __t) + -'\n\n'; -return __p -}; - -var comments_tpl = function(data) { -var __t, __p = ''; -console.log('comments tpl', data); - -__p += '\n\n'; -return __p -}; - -(function() { - var initialized = false; - var firstLoad = true; - var $ = function (sel) { return document.querySelector(sel); }; - var $$ = function (sel) { return document.querySelectorAll(sel); }; - var script = $('script[data-schnack-target]'); - - if (!script) { return console.warn('schnack script tag needs some data attributes'); } - - var opts = script.dataset; - var slug = opts.schnackSlug; - var url = new URL(script.getAttribute('src')); - var host = (url.protocol) + "//" + (url.host); - var endpoint = host + "/comments/" + slug; - var target = opts.schnackTarget; - - if (url.hostname != 'localhost') { - document.domain = url.hostname.split('.').slice(1).join('.'); - } - - function refresh() { - - index(endpoint, { - credentials: 'include', - headers: { 'Content-Type': 'application/json' } - }) - .then( function (r) { return r.json(); } ) - .then(function (data) { - data.comments_tpl = comments_tpl; - $(target).innerHTML = schnack_tpl(data); - - var above = $((target + " div.schnack-above")); - var form = $((target + " div.schnack-form")); - var textarea = $((target + " textarea.schnack-body")); - var preview = $((target + " .schnack-form blockquote.schnack-body")); - - var draft = window.localStorage.getItem(("schnack-draft-" + slug)); - if (draft && textarea) { textarea.value = draft; } - - var postBtn = $(target + ' .schnack-button'); - var previewBtn = $(target + ' .schnack-preview'); - var writeBtn = $(target + ' .schnack-write'); - var cancelReplyBtn = $(target + ' .schnack-cancel-reply'); - var replyBtns = $$(target + ' .schnack-reply'); - - if (postBtn) { - postBtn.addEventListener('click', function (d) { - var body = textarea.value; - index(endpoint, { - credentials: 'include', - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - comment: body, - replyTo: form.dataset.reply - }) - }) - .then( function (r) { return r.json(); } ) - .then(function (res) { - console.log(res); - textarea.value = ''; - window.localStorage.setItem(("schnack-draft-" + slug), textarea.value); - if (res.id) { - firstLoad = true; - window.location.hash = '#comment-'+res.id; - } - refresh(); - }); - }); - - previewBtn.addEventListener('click', function (d) { - var body = textarea.value; - textarea.style.display = 'none'; - previewBtn.style.display = 'none'; - preview.style.display = 'block'; - writeBtn.style.display = 'inline'; - index((host + "/markdown"), { - credentials: 'include', - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ comment: body }) - }) - .then( function (r) { return r.json(); } ) - .then(function (res) { - console.log(res); - preview.innerHTML = res.html; - // refresh(); - }); - }); - - writeBtn.addEventListener('click', function (d) { - textarea.style.display = 'inline'; - previewBtn.style.display = 'inline'; - preview.style.display = 'none'; - writeBtn.style.display = 'none'; - }); - - textarea.addEventListener('keyup', function () { - window.localStorage.setItem(("schnack-draft-" + slug), textarea.value); - }); - - replyBtns.forEach(function (btn) { - btn.addEventListener('click', function () { - form.dataset.reply = btn.dataset.replyTo; - cancelReplyBtn.style.display = 'inline-block'; - btn.parentElement.appendChild(form); - }); - }); - - cancelReplyBtn.addEventListener('click', function () { - above.appendChild(form); - delete form.dataset.reply; - cancelReplyBtn.style.display = 'none'; - }); - } - if (data.user) { - var signout = $('a.schnack-signout'); - if (signout) { signout.addEventListener('click', function (e) { - e.preventDefault(); - index((host + "/signout"), { - credentials: 'include', - headers: { 'Content-Type': 'application/json' }, - }) - .then( function (r) { return r.json(); } ) - .then(function (res) { - console.log(res); - refresh(); - }); - }); } - } else { - data.auth.forEach(function (provider) { - var btn = $(target + ' .schnack-signin-'+provider.id); - if (btn) { btn.addEventListener('click', function (d) { - var windowRef = window.open( - (host + "/auth/" + (provider.id)), provider.name+' Sign-In', 'resizable,scrollbars,status,width=600,height=500' - ); - window.__schnack_wait_for_oauth = function () { - windowRef.close(); - refresh(); - }; - }); } - }); - } - - if (data.user && data.user.admin) { - if (!initialized) { - var push = document.createElement('script'); - push.setAttribute('src', (host + "/push.js")); - document.head.appendChild(push); - initialized = true; - } - - var action = function (evt) { - var btn = evt.target; - var data = btn.dataset; - index((host + "/" + (data.class) + "/" + (data.target) + "/" + (data.action)), { - credentials: 'include', - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: '' - }) - .then( function (r) { return r.json(); } ) - .then(function (res) { - console.log(res); - refresh(); - }); - }; - document.querySelectorAll('.schnack-action').forEach(function (btn) { - btn.addEventListener('click', action); - }); - } - - if (firstLoad && window.location.hash.match(/^#comment-\d+$/)) { - var hl = document.querySelector(window.location.hash); - hl.scrollIntoView(); - hl.classList.add('schnack-highlight'); - firstLoad = false; - } - }); - } - - refresh(); - -})(); - -}()); +!function(){"use strict";var n="function"==typeof fetch?fetch.bind():function(n,t){return t=t||{},new Promise(function(e,a){function s(){var n,t=[],e=[],a={};return c.getAllResponseHeaders().replace(/^(.*?):\s*([\s\S]*?)$/gm,function(s,c,o){t.push(c=c.toLowerCase()),e.push([c,o]),n=a[c],a[c]=n?n+","+o:o}),{ok:1==(c.status/200|0),status:c.status,statusText:c.statusText,url:c.responseURL,clone:s,text:function(){return Promise.resolve(c.responseText)},json:function(){return Promise.resolve(c.responseText).then(JSON.parse)},blob:function(){return Promise.resolve(new Blob([c.response]))},headers:{keys:function(){return t},entries:function(){return e},get:function(n){return a[n.toLowerCase()]},has:function(n){return n.toLowerCase()in a}}}}var c=new XMLHttpRequest;c.open(t.method||"get",n);for(var o in t.headers)c.setRequestHeader(o,t.headers[o]);c.withCredentials="include"==t.credentials,c.onload=function(){e(s())},c.onerror=a,c.send(t.body)})},t=function(n){var t,e="";n.user?(e+="\n ",n.user.admin&&(e+='\n
\n \n \n
\n '),e+='\n
\n (signed in as @'+(null==(t=n.user.name)?"":t)+' :: sign out)\n
\n
\n
\n \n \n
\n \n  \n  \n \n
\n
\n'):(e+="\nTo post a comment you need to sign in via
\n",n.auth.forEach(function(n,a){e+="\n "+(null==(t=a?" or ":"")?"":t)+'\n"}),e+="\n"),e+="\n";var a=[];return n.replies={},n.comments.forEach(function(t){t.reply_to?(n.replies[t.reply_to]||(n.replies[t.reply_to]=[]),n.replies[t.reply_to].push(t)):a.push(t)}),n.comments=a,e+="\n"+(null==(t=n.comments_tpl(n))?"":t)+'\n\n'},e=function(n){var t,e="";return e+='\n"};!function(){function a(){n(p,{credentials:"include",headers:{"Content-Type":"application/json"}}).then(function(n){return n.json()}).then(function(l){l.comments_tpl=e,o(m).innerHTML=t(l);var r=o(m+" div.schnack-above"),d=o(m+" div.schnack-form"),f=o(m+" textarea.schnack-body"),k=o(m+" .schnack-form blockquote.schnack-body"),y=window.localStorage.getItem("schnack-draft-"+u);y&&f&&(f.value=y);var v=o(m+" .schnack-button"),b=o(m+" .schnack-preview"),g=o(m+" .schnack-write"),w=o(m+" .schnack-cancel-reply"),E=i(m+" .schnack-reply");if(v&&(v.addEventListener("click",function(t){var e=f.value;n(p,{credentials:"include",method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({comment:e,replyTo:d.dataset.reply})}).then(function(n){return n.json()}).then(function(n){f.value="",window.localStorage.setItem("schnack-draft-"+u,f.value),n.id&&(c=!0,window.location.hash="#comment-"+n.id),a()})}),b.addEventListener("click",function(t){var e=f.value;f.style.display="none",b.style.display="none",k.style.display="block",g.style.display="inline",n(h+"/markdown",{credentials:"include",method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({comment:e})}).then(function(n){return n.json()}).then(function(n){k.innerHTML=n.html})}),g.addEventListener("click",function(n){f.style.display="inline",b.style.display="inline",k.style.display="none",g.style.display="none"}),f.addEventListener("keyup",function(){window.localStorage.setItem("schnack-draft-"+u,f.value)}),E.forEach(function(n){n.addEventListener("click",function(){d.dataset.reply=n.dataset.replyTo,w.style.display="inline-block",n.parentElement.appendChild(d)})}),w.addEventListener("click",function(){r.appendChild(d),delete d.dataset.reply,w.style.display="none"})),l.user){var _=o("a.schnack-signout");_&&_.addEventListener("click",function(t){t.preventDefault(),n(h+"/signout",{credentials:"include",headers:{"Content-Type":"application/json"}}).then(function(n){return n.json()}).then(function(n){a()})})}else l.auth.forEach(function(n){var t=o(m+" .schnack-signin-"+n.id);t&&t.addEventListener("click",function(t){var e=window.open(h+"/auth/"+n.id,n.name+" Sign-In","resizable,scrollbars,status,width=600,height=500");window.__schnack_wait_for_oauth=function(){e.close(),a()}})});if(l.user&&l.user.admin){if(!s){var T=document.createElement("script");T.setAttribute("src",h+"/push.js"),document.head.appendChild(T),s=!0}var L=function(t){var e=t.target.dataset;n(h+"/"+e.class+"/"+e.target+"/"+e.action,{credentials:"include",method:"POST",headers:{"Content-Type":"application/json"},body:""}).then(function(n){return n.json()}).then(function(n){a()})};document.querySelectorAll(".schnack-action").forEach(function(n){n.addEventListener("click",L)})}if(c&&window.location.hash.match(/^#comment-\d+$/)){var S=document.querySelector(window.location.hash);S.scrollIntoView(),S.classList.add("schnack-highlight"),c=!1}})}var s=!1,c=!0,o=function(n){return document.querySelector(n)},i=function(n){return document.querySelectorAll(n)},l=o("script[data-schnack-target]");if(!l)return console.warn("schnack script tag needs some data attributes");var r=l.dataset,u=r.schnackSlug,d=new URL(l.getAttribute("src")),h=d.protocol+"//"+d.host,p=h+"/comments/"+u,m=r.schnackTarget;"localhost"!=d.hostname&&(document.domain=d.hostname.split(".").slice(1).join(".")),a()}()}(); diff --git a/rollup.config.js b/rollup.config.js index b892649..484f38d 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -19,6 +19,6 @@ export default { commonjs(), resolve(), buble(), - // uglify() + uglify() ] -}; \ No newline at end of file +}; diff --git a/src/embed/comments.jst.html b/src/embed/comments.jst.html index 7ed4ba9..c68fa19 100644 --- a/src/embed/comments.jst.html +++ b/src/embed/comments.jst.html @@ -1,6 +1,3 @@ -<% -console.log('comments tpl', data); -%>