writing: egyptian hieroglyphs
[sheet.git] / writer.js
index 6470a3957c0b1051b6dd95d0532d858ce4ccd94a..5d3ad5e782977c43e58213c03db6058f667a2015 100644 (file)
--- a/writer.js
+++ b/writer.js
@@ -2,6 +2,7 @@ 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;
@@ -19,6 +20,7 @@ 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|langlinks&page='+wptitle;
                        fetch(wppage).then(res => res.json()).then(json => {
@@ -35,10 +37,21 @@ document.addEventListener('DOMContentLoaded', () => {
                                        });
                                });
 
+                               // copy first paragraph to story
+                               let wptext = json.parse.text['*'];
+                               let storyinput = document.getElementById('story');
+                               if (storyinput && wptext) {
+                                       storyinput.innerHTML = wptext
+                                               .replace(/<h2.*/s, '') // prefix
+                                               .replace(/<table.*?<\/table>/sg, '') // ignore infobox
+                                               .match(/<p>(.*?)<\/p>/s)[0] // first paragraph
+                                               .replace(/<[^>]*>/g, '') // strip html tags
+                               }
+
                                // list images in article html
                                let imginput = document.getElementById('source');
                                if (!imginput || imginput.value) return;
-                               let wpimages = json.parse.text['*'].match(/<img\s[^>]+>/g);
+                               let wpimages = wptext.match(/<img\s[^>]+>/g);
                                let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
                                wpselect.className = 'popup';
                                wpimages.forEach(img => {
@@ -62,7 +75,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;
                };
@@ -81,6 +96,29 @@ document.addEventListener('DOMContentLoaded', () => {
                };
        }
 
+       let thumbpreview = document.getElementById('thumbpreview');
+       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('thumb');
+                               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 => {