word/edit: fix (vertical) scale with different aspect ratios
[sheet.git] / word / editor.js
1 document.addEventListener('DOMContentLoaded', () => {
2         document.querySelectorAll('#search').forEach(p => {
3                 let [input, button] = p.children;
4                 button.onclick = e => {
5                         if (input.value && input.offsetWidth > 50) {
6                                 return true; // bubble to submit
7                         }
8                         // make visible first
9                         input.focus();
10                         e.preventDefault();
11                         return false;
12                 };
13         });
14
15         document.querySelectorAll('.multiinput > input[id]').forEach(el => {
16                 el.oninput = e => {
17                         if (e.target.value == '') return;
18                         // insert another empty input element option
19                         let add = e.target.cloneNode(true);
20                         add.value = '';
21                         add.oninput = e.target.oninput;
22                         e.target.parentNode.appendChild(add);
23                         e.target.oninput = undefined;
24                         e.target.removeAttribute('id');
25                 };
26         });
27
28         let wpinput = document.getElementById('wptitle');
29         if (wpinput) {
30                 let wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
31                 wpbutton.type = 'button';
32                 wpbutton.append('Download');
33                 wpbutton.onclick = () => {
34                         let wptitle = wpinput.value || document.getElementById('form').value;
35                         let wplang = document.getElementById('lang').value;
36                         if (wplang == 'la') wplang = 'en'; // most likely presence of scientific names
37                         let wpapi = `https://${wplang}.wikipedia.org/w/api.php`;
38                         let wppage = wpapi+'?action=parse&format=json&origin=*&prop=text|langlinks&page='+wptitle;
39                         fetch(wppage).then(res => res.json()).then(json => {
40                                 if (json.error) throw `error returned: ${json.error.info}`;
41                                 wpinput.value = json.parse.title;
42
43                                 let wptext = json.parse.text['*'];
44                                 let transrow = document.getElementById('trans-la');
45                                 if (transrow && !transrow.value && wptext) {
46                                         const binom = wptext.match(/ class="binomial">.*?<i>(.*?)<\/i>/);
47                                         if (binom) {
48                                                 transrow.value = binom[1]
49                                         }
50                                 }
51
52                                 // translations from language links
53                                 let wplangs = json.parse.langlinks;
54                                 if (wplangs) wplangs.forEach(wptrans => {
55                                         let transrow = document.getElementById('trans-' + wptrans.lang);
56                                         if (!transrow || transrow.value) return;
57                                         transrow.value = wptrans['*'].replace(/([^,(]*).*/, (link, short) => {
58                                                 return short.toLocaleLowerCase(wptrans.lang).trimEnd() + ' [' + link + ']';
59                                         });
60                                 });
61
62                                 // copy first paragraph to story
63                                 let storyinput = document.getElementById('story');
64                                 if (storyinput && !storyinput.value && wptext) {
65                                         storyinput.value = wptext
66                                                 .replace(/<h2.*/s, '') // prefix
67                                                 .replace(/<table.*?<\/table>/sg, '') // ignore infobox
68                                                 .match(/<p>(.*?)<\/p>/s)[0] // first paragraph
69                                                 .replace(/<[^>]*>/g, '') // strip html tags
70                                 }
71
72                                 // list images in article html
73                                 let imginput = document.getElementById('source');
74                                 if (!imginput || imginput.value) return;
75                                 let wpimages = wptext.match(/<img\s[^>]+>/g);
76                                 let wpselect = wpinput.parentNode.appendChild(document.createElement('ul'));
77                                 wpselect.className = 'popup';
78                                 wpimages.forEach(img => {
79                                         let selectitem = wpselect.appendChild(document.createElement('li'));
80                                         selectitem.insertAdjacentHTML('beforeend', img);
81                                         selectitem.onclick = e => {
82                                                 let imgsrc = e.target.src
83                                                         .replace(/^(?=\/\/)/, 'https:')
84                                                         .replace(/\/thumb(\/.+)\/[^\/]+$/, '$1');
85                                                 imginput.value = imgsrc;
86                                                 wpselect.remove();
87                                                 return false;
88                                         };
89                                 });
90                         }).catch(error => alert(error));
91                         return false;
92                 };
93                 wpbutton = wpinput.parentNode.appendChild(document.createElement('button'));
94                 wpbutton.type = 'button';
95                 wpbutton.append('Visit');
96                 wpbutton.onclick = () => {
97                         let wptitle = wpinput.value || document.getElementById('form').value;
98                         let wplang = document.getElementById('lang').value;
99                         let wpurl =
100                                 wplang == 'la' ? `https://species.wikimedia.org/wiki/${wptitle}` :
101                                 `https://${wplang}.wikipedia.org/wiki/${wptitle}`;
102                         window.open(wpurl, 'sheet-wikipedia').focus();
103                         return false;
104                 };
105         }
106
107         let imgpreview = document.getElementById('sourcepreview');
108         if (imgpreview) {
109                 let imginput = document.getElementById('source');
110                 imginput.parentNode.parentNode.append(imgpreview); // separate row
111                 let previewbutton = imginput.parentNode.appendChild(document.createElement('button'));
112                 previewbutton.type = 'button';
113                 previewbutton.append('View');
114                 previewbutton.onclick = () => {
115                         previewbutton.childNodes[0].nodeValue = imgpreview.hidden ? 'Hide' : 'View';
116                         imgpreview.hidden = !imgpreview.hidden;
117                 };
118         }
119
120         let thumbpreview = document.getElementById('convertpreview');
121         if (thumbpreview && imgpreview) {
122                 thumbpreview.onclick = e => {
123                         thumbpreview.onclick = null; // setup once
124                         const cropinput = document.getElementById('crop32');
125                         const imgselect = thumbpreview.children[0];
126                         const canvas = [thumbpreview.clientWidth, thumbpreview.clientHeight];
127                         const border = [canvas[0], canvas[0] * imgpreview.height / imgpreview.width];
128                         const minscale = Math.max(1, canvas[1] / border[1]); // 100% or fit width
129                         let crop = cropinput.value.split(/[^0-9]/).map(pos => pos / 1000);
130                         let scale = 1 / (crop[2] - crop[0]) || minscale;
131                         crop.push(0); // defined y dimension
132                         crop.splice(2); // end coordinates applied to zoom
133                         crop = crop.map((rel, axis) => rel * border[axis % 2] * scale);
134
135                         let drag, pinch;
136                         function applydrag(e) {
137                                 const touch = e.touches ? e.touches[0] : e;
138                                 let pos = [touch.pageX, touch.pageY];
139                                 if (e.type === 'touchmove' && e.touches.length > 1) {
140                                         // distance to second point
141                                         pos[0] -= e.touches[1].pageX;
142                                         pos[1] -= e.touches[1].pageY;
143                                         const span = Math.sqrt(pos[0]**2 + pos[1]**2);
144                                         if (pinch) {
145                                                 cropzoom(.01 * (span - pinch));
146                                         }
147                                         pinch = span;
148                                         return;
149                                 }
150                                 if (drag) {
151                                         // apply drag delta to crop position
152                                         crop[0] += drag[0] - pos[0];
153                                         crop[1] += drag[1] - pos[1];
154                                         recrop();
155                                 }
156                                 drag = pos;
157                         }
158
159                         function recrop() {
160                                 [0, 1].forEach(axis => {
161                                         if (crop[axis] > border[axis] * scale - canvas[axis]) {
162                                                 crop[axis] = border[axis] * scale - canvas[axis]; // max bound
163                                         }
164                                         if (crop[axis] < 0) {
165                                                 crop[axis] = 0; // min bound
166                                         }
167                                 });
168                                 imgselect.style.left = -crop[0]+'px';
169                                 imgselect.style.top  = -crop[1]+'px';
170                                 imgselect.style.width = (scale * 100)+'%';
171                                 cropinput.value = [
172                                         crop[0] / border[0],
173                                         crop[1] / border[1],
174                                         (crop[0] + canvas[0]) / border[0],
175                                         (crop[1] + canvas[1]) / border[1],
176                                 ].map(pos => Math.round(1000 * pos / scale));
177                         }
178
179                         function cropzoom(delta) {
180                                 if (scale + delta < minscale) {
181                                         delta = minscale - scale; // scale = 1
182                                 }
183                                 [0, 1].forEach(axis => {
184                                         // same area center at altered scale
185                                         crop[axis] += (crop[axis] + border[axis] / 2) / scale * delta;
186                                 });
187                                 scale += delta;
188                                 recrop();
189                         }
190
191                         imgselect.src = imgpreview.src;
192                         imgselect.style.cursor = 'grab';
193                         imgselect.style.position = 'absolute';
194                         recrop();
195
196                         imgselect.ontouchstart =
197                         imgselect.onmousedown = e => {
198                                 e.preventDefault();
199                                 drag = pinch = false;
200                                 applydrag(e);
201                                 imgselect.style.cursor = 'grabbing';
202                                 window.ontouchmove =
203                                 window.onmousemove = e => {
204                                         e.preventDefault();
205                                         applydrag(e);
206                                 };
207                                 window.ontouchend =
208                                 window.onmouseup = e => {
209                                         e.preventDefault();
210                                         imgselect.style.cursor = 'grab';
211                                         window.ontouchmove = window.ontouchend =
212                                         window.onmousemove = window.onmouseup = null;
213                                 };
214                         };
215
216                         imgselect.onwheel = e => {
217                                 e.preventDefault();
218                                 let delta = (-e.deltaY || e.wheelDelta) * .001 * scale;
219                                 if (e.deltaMode == 1) { // DOM_DELTA_LINE
220                                         delta *= 18; // convert number of lines to pixels
221                                 }
222                                 cropzoom(delta);
223                         };
224                 };
225         }
226
227         let translist = document.getElementById('trans');
228         if (translist) {
229                 let langoptions = Array.prototype.filter.call(document.getElementById('lang').options, opt => {
230                         if (document.getElementById('trans-' + opt.value)) return;
231                         if (document.getElementById('lang').value == opt.value) return;
232                         return true;
233                 });
234                 if (!langoptions.length) return;
235
236                 let transadd = translist.appendChild(document.createElement('li'));
237                 let transselect = transadd.appendChild(document.createElement('select'));
238                 transselect.appendChild(document.createElement('option'));
239                 for (let langoption of langoptions) {
240                         let transoption = document.createElement('option');
241                         transoption.value = langoption.value;
242                         transoption.append(langoption.label);
243                         transselect.appendChild(transoption);
244                 }
245                 transselect.onchange = e => {
246                         let inputlang = e.target.selectedOptions[0];
247                         let transadded = translist.insertBefore(document.createElement('li'), transadd);
248                         let translabel = transadded.appendChild(document.createElement('label'));
249                         translabel.append(inputlang.label.replace(/ (.+)/, ' ')); //TODO title = $1
250                         let transinput = transadded.appendChild(document.createElement('input'));
251                         transinput.name = 'trans-'+inputlang.value;
252                         translabel.setAttribute('for', transinput.id = transinput.name);
253                         inputlang.remove();
254                         if (e.target.length <= 1) e.target.remove();
255                         transinput.focus();
256                 };
257         }
258 });