diff options
Diffstat (limited to 'pub/pmwiki-utils.js')
-rw-r--r-- | pub/pmwiki-utils.js | 175 |
1 files changed, 166 insertions, 9 deletions
diff --git a/pub/pmwiki-utils.js b/pub/pmwiki-utils.js index 5b68101b..c15dab6e 100644 --- a/pub/pmwiki-utils.js +++ b/pub/pmwiki-utils.js @@ -10,7 +10,7 @@ -(function(__script__){ +(async function(__script__){ try { var Config = JSON.parse(__script__.dataset.config); Config.fullname = __script__.dataset.fullname; @@ -19,6 +19,13 @@ var Config = {}; } + if(Config.rediquiet) { + var url = location.href.replace(/\?from=[^?&#]*[&]quiet=1/, ''); + if(url != location.href) + history.replaceState(null, null, url); + } + + function aE(el, ev, fn) { if(typeof el == 'string') el = dqsa(el); for(var i=0; i<el.length; i++) el[i].addEventListener(ev, fn); @@ -59,7 +66,7 @@ function PHSC(x) { return x.replace(/[&]/g, '&').replace(/[<]/g, '<').replace(/[>]/g, '>'); } var wikitext; - var log = console.log; + var echo = console.log; function PmXMail() { var els = document.querySelectorAll('span._pmXmail'); @@ -566,7 +573,7 @@ } if(out) adjae(plus, out); }) - .catch(log); + .catch(echo); }); if(dqs('form[name="authform"]') || location.href.match(/action=/)) return; seenstamp[pagename] = Math.floor(Now.getTime()/1000); @@ -590,16 +597,166 @@ } }); } - - if(Config.rediquiet) { - var url = location.href.replace(/\?from=[^?&#]*[&]quiet=1/, ''); - if(url != location.href) - history.replaceState(null, null, url); + + var Dropzone = false; + var UploadQueue = []; + function init_dropzone(){ + if(!Config.updrop) return; + var form = dqs('form[action$="action=postupload"], form[action$="action=edit"]'); + if(!form) return; + Dropzone = dce('div'); + Dropzone.className = "frame pmdropzone"; + var label = dce('span'); + label.textContent = Config.updrop.label; + adjbe(label, ' '); + Dropzone.appendChild(label); + if(Config.updrop.areq) { + var areq = form.querySelector('input[name="author"]') + Dropzone.pmareq = areq; + } + + form.insertAdjacentElement('afterbegin', Dropzone); + aE([Dropzone], 'dragenter', dragenter); + aE([Dropzone], 'dragover', dragenter); + aE([Dropzone], 'dragleave', dragleave); + aE([Dropzone], 'drop', dragdrop); + tap([Dropzone], clickzone); + } + + async function postUpload(file) { + var url = Config.updrop.action; + var f = new FormData; + f.append('n', Config.fullname); + f.append('action', 'postupload'); + f.append('uploadfile', file); + f.append('pmdrop', 1); + f.append(Config.updrop.token[0], Config.updrop.token[1]); + if(Dropzone.pmareq) + f.append('author', Dropzone.pmareq.value); + try { + var response = await fetch(url, { method: 'POST', body: f }); + if (!response.ok) { + var msg = `HTTP error! Status: ${response.status}`; + throw new Error(msg); + return { error: 1, msg: msg}; + } + return await response.json(); + } + catch(err) { + throw new Error(err); + return {error: 1, msg: 'Error posting form data: ' + err.message }; + } + } + + async function processUploads() { + while(true) { + var a = dqs('.pmdropzone a.queued'); + if(!a) return; + a.className = 'uploading'; + var result = await postUpload(a.pmfile); + if(result.error) { + a.className = 'error'; + a.title = result.msg; + } + else { + a.className = 'success'; + a.href = result.href; + a.textContent = result.uprname; + a.title = result.msg; + delete a.pmfile; + } + } + } + + function clickzone(e){ + var a = e.target.closest('a'); + if(!a) return; + e.preventDefault(); + e.stopPropagation(); + if(a.className.match(/uploading|queued|deleting/)) return; + else if(a.className == 'success' && typeof insMarkup == 'function') { + var pn = e.ctrlKey? Config.fullname + '/': ''; + var text = "Attach:"+pn+a.textContent; + if(text.match(/\s/)) text = '[['+text+']]'; + insMarkup(function(x){ + if(x.length) return text; + return { + mtext: text, + unselect: true + }; + }); + return; + } + else if(a.className == 'error' || typeof insMarkup == 'undefined') { + a.classList.add('deleting'); + setTimeout(function(){a.remove();}, 700); + return; + } + // this shouldn't happen + } + + function dragenter(e) { + e.preventDefault(); + Dropzone.classList.add('over'); + } + + function dragleave(e) { + e.preventDefault(); + Dropzone.classList.remove('over'); } + async function dragdrop(e) { + e.preventDefault(); + Dropzone.classList.remove('over'); + if(Dropzone.pmareq) { + var v = Dropzone.pmareq.value; + if(!v.length) { + appendUpload({size:-500,name:Config.updrop.areq}); + Dropzone.pmareq.focus(); + return; + } + } + var files = event.dataTransfer.files; + for (var i = 0; i < files.length; i++) appendUpload(files[i]); + var pending = dqs('.pmdropzone a.uploading'); + if(!pending) await processUploads(); + } + + function appendUpload(file) { + if(file.size==0) { return; } + var sizes = Config.updrop.sizes; + var ext = ''; + var m = file.name.match(/\.([^\.]+)$/); + if(m) ext = m[1].toLowerCase(); + + var a = dce('a'); + a.href = '#'; + a.textContent = file.name; + + if(file.size==-500) { + a.className = 'error'; + } + else if(typeof sizes[ext] == 'undefined') { + a.className = 'error'; + a.title = Config.updrop.badtype.replace(/\#upext/, ext); + } + else if(file.size > sizes[ext]) { + a.className = 'error'; + a.title = Config.updrop.toobig + .replace(/\#upmax/, sizes[ext]).replace(/\#upext/, ext); + } + else { + a.className = 'queued'; + a.pmfile = file; + } + Dropzone.appendChild(a); + } + + function ready(){ wikitext = document.getElementById('wikitext'); - var fn = [autotoc, inittoggle, PmXMail, localTimes, highlight_pre, copy_pre, confirmForms]; + var fn = [autotoc, inittoggle, PmXMail, localTimes, + highlight_pre, copy_pre, confirmForms, init_dropzone]; fn.forEach(function(a){a();}); makesortable(); } |