X-Git-Url: http://git.shiar.net/sheet.git/blobdiff_plain/cf905894dcf3c4c9cdff7732f8d69c1e893bc1da..2c02dbfe7e0bc1ce3619489adcb79aca128142cf:/writer.js diff --git a/writer.js b/writer.js index 80ba7b8..da403b2 100644 --- a/writer.js +++ b/writer.js @@ -1,4 +1,17 @@ document.addEventListener('DOMContentLoaded', () => { + document.querySelectorAll('.multiinput > input[id]').forEach(el => { + el.oninput = e => { + if (e.target.value == '') return; + // insert another empty input element option + let add = e.target.cloneNode(true); + add.value = ''; + add.oninput = e.target.oninput; + e.target.parentNode.appendChild(add); + e.target.oninput = undefined; + e.target.removeAttribute('id'); + }; + }); + let wpinput = document.getElementById('wptitle'); if (wpinput) { let wpbutton = wpinput.parentNode.appendChild(document.createElement('button')); @@ -7,14 +20,44 @@ document.addEventListener('DOMContentLoaded', () => { wpbutton.onclick = () => { let wptitle = wpinput.value || document.getElementById('form').value; let wplang = document.getElementById('lang').value; + if (wplang == 'la') wplang = 'en'; // most likely presence of scientific names let wpapi = `https://${wplang}.wikipedia.org/w/api.php`; - let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text&page='+wptitle; + let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text|langlinks&page='+wptitle; fetch(wppage).then(res => res.json()).then(json => { if (json.error) throw `error returned: ${json.error.info}`; wpinput.value = json.parse.title; + + let wptext = json.parse.text['*']; + let transrow = document.getElementById('trans-la'); + if (transrow && !transrow.value && wptext) { + const binom = wptext.match(/ class="binomial">.*?(.*?)<\/i>/); + transrow.value = binom[1] + } + + // translations from language links + let wplangs = json.parse.langlinks; + if (wplangs) wplangs.forEach(wptrans => { + let transrow = document.getElementById('trans-' + wptrans.lang); + if (!transrow || transrow.value) return; + transrow.value = wptrans['*'].replace(/([^,(]*).*/, (link, short) => { + return short.toLocaleLowerCase(wptrans.lang).trimEnd() + ' [' + link + ']'; + }); + }); + + // copy first paragraph to story + let storyinput = document.getElementById('story'); + if (storyinput && !storyinput.value && wptext) { + storyinput.value = wptext + .replace(//sg, '') // ignore infobox + .match(/

(.*?)<\/p>/s)[0] // first paragraph + .replace(/<[^>]*>/g, '') // strip html tags + } + + // list images in article html let imginput = document.getElementById('source'); - if (imginput.value) return; - let wpimages = json.parse.text['*'].match(/]+>/g); + if (!imginput || imginput.value) return; + let wpimages = wptext.match(/]+>/g); let wpselect = wpinput.parentNode.appendChild(document.createElement('ul')); wpselect.className = 'popup'; wpimages.forEach(img => { @@ -38,7 +81,9 @@ document.addEventListener('DOMContentLoaded', () => { wpbutton.onclick = () => { let wptitle = wpinput.value || document.getElementById('form').value; let wplang = document.getElementById('lang').value; - let wpurl = `https://${wplang}.wikipedia.org/wiki/${wptitle}`; + let wpurl = + wplang == 'la' ? `https://species.wikimedia.org/wiki/${wptitle}` : + `https://${wplang}.wikipedia.org/wiki/${wptitle}`; window.open(wpurl, 'sheet-wikipedia').focus(); return false; }; @@ -57,13 +102,42 @@ document.addEventListener('DOMContentLoaded', () => { }; } + let thumbpreview = document.getElementById('convertpreview'); + if (thumbpreview && imgpreview) { + thumbpreview.onclick = e => { + let imgselect = imgpreview; /* TODO clone */ + imgselect.hidden = false; + imgselect.classList.add('popup'); + imgselect.onmousemove = e => { + let border = imgselect.getBoundingClientRect(); + let pos = [ + Math.round(1000 * (e.clientX - border.x) / border.width), + Math.round(1000 * (e.clientY - border.y) / border.height) + ]; + return pos; + }; + imgselect.onclick = e => { + let imgoption = document.getElementById('convert'); + imgoption.value += (imgoption.value && '-') + imgselect.onmousemove(e); + imgselect.hidden = true; + imgselect.classList.remove('popup'); + }; + }; + } + let translist = document.getElementById('trans'); if (translist) { + let langoptions = Array.prototype.filter.call(document.getElementById('lang').options, opt => { + if (document.getElementById('trans-' + opt.value)) return; + if (document.getElementById('lang').value == opt.value) return; + return true; + }); + if (!langoptions.length) return; + let transadd = translist.appendChild(document.createElement('li')); let transselect = transadd.appendChild(document.createElement('select')); transselect.appendChild(document.createElement('option')); - for (let langoption of document.getElementById('lang').options) { - if (document.getElementById('trans-'+langoption.value)) continue; + for (let langoption of langoptions) { let transoption = document.createElement('option'); transoption.value = langoption.value; transoption.append(langoption.label);