word edit: group reference input with category
[sheet.git] / writer.plp
index b64fea08e3f8f2fb5dbab85e807dc070eb813e7a..bae010cf19aa89e20ec950652848b3293ee987af 100644 (file)
@@ -40,9 +40,6 @@ input:not([type]) {
 select {
        padding: .3rem .2rem; /* TODO: input */
 }
-form > ul li img {
-       max-width: 300px;
-}
 
 ul.popup {
        display: flex;
@@ -85,42 +82,7 @@ h1 {
 }
 </style>
 
-<script>
-document.addEventListener('DOMContentLoaded', () => {
-       var wpinput = document.getElementById('wptitle');
-       var wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
-       wpbutton.type = 'button';
-       wpbutton.append('Copy');
-       wpbutton.onclick = () => {
-               let wptitle = wpinput.value || document.getElementById('form').value;
-               let wplang = document.getElementById('lang').value.substr(0, 2); // crude iso-639-3→2
-               let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
-               let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text&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 imginput = document.getElementById('source');
-                       if (imginput.value) return;
-                       let wpimages = json.parse.text['*'].match(/<img\s[^>]+>/g);
-                       let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
-                       wpselect.className = 'popup';
-                       wpimages.forEach(img => {
-                               let selectitem = wpselect.appendChild(document.createElement('li'));
-                               selectitem.insertAdjacentHTML('beforeend', img);
-                               selectitem.onclick = e => {
-                                       let imgsrc = e.target.src
-                                               .replace(/^(?=\/\/)/, 'https:')
-                                               .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
-                                       imginput.value = imgsrc;
-                                       wpselect.remove();
-                                       return false;
-                               };
-                       });
-               }).catch(error => alert(error));
-               return false;
-       };
-});
-</script>
+<script src="/writer.js"></script>
 EOT
 });
 
@@ -140,14 +102,14 @@ my $db = eval {
 my @wordcols = (
        lang    => 'Language',
        cat     => 'Category',
+       ref     => undef, # included with cat
+       prio    => 'Level',
+       cover   => undef, # included with prio
        form    => 'Translation',
        alt     => 'Synonyms',
        wptitle => 'Wikipedia',
-       source  => 'Image URL',
+       source  => 'Image',
        thumb   => 'Convert options',
-       prio    => 'Level',
-       cover   => undef, # included with prio
-       ref     => 'Reference',
 );
 my @prioenum = qw( essential basic common distinctive rare invisible );
 my ($find) = map {{id => $_}} $fields{id} || $Request || ();
@@ -266,12 +228,16 @@ for my $colinfo (pairs @wordcols) {
        }
        else {
                printf '<input id="%s" name="%1$s" value="%s" />', $col, Entity($val);
-               -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form} for
-                       $col eq 'source' ? "data/word/org/$row->{id}.jpg" :
-                       $col eq 'thumb'  ? "data/word/eng/$row->{form}.jpg" :
-                       ();
+               -e and printf '<img src="/%s" alt="%s" />', $_, $row->{form}
+                       for $col eq 'thumb'  ? "data/word/eng/$row->{form}.jpg" : ();
+               printf('<label for="%s">%s</label><input id="%1$s" name="%1$s" value="%s" />',
+                       'ref', 'Reference', Entity($row->{ref})
+               ) if $col eq 'cat';
        }
                print '</span>';
+       -e and printf('<img id="%spreview" src="/%s" alt="%s" hidden />',
+               $col, $_, $row->{form}
+       ) for $col eq 'source' ? "data/word/org/$row->{id}.jpg" : ();
        say '</p></li>';
 }
 :>