browser: append partial match of next cell versions
[sheet.git] / clipboard.js
1 copyhint = true;
2 function clipboardcopy(val) {
3         if (window && window.clipboardData) {
4                 // msie
5                 return window.clipboardData.setData('text', val);
6         }
7
8         if (document.queryCommandSupported('copy')) {
9                 var textArea = document.createElement('textarea');
10
11                 // minimise styling in case it is (briefly) rendered
12                 textArea.style.position = 'fixed';
13                 textArea.style.top = 0;
14                 textArea.style.left = 0;
15                 textArea.style.width = '2em';
16                 textArea.style.height = '2em';
17                 textArea.style.padding = 0;
18                 textArea.style.border = 'none';
19                 textArea.style.outline = 'none';
20                 textArea.style.boxShadow = 'none';
21                 textArea.style.background = 'transparent';
22
23                 // temporarily add input field to copy text from
24                 textArea.value = val;
25                 document.body.appendChild(textArea);
26                 try {
27                         textArea.select();
28                         document.execCommand('copy');
29                 } catch (e) {
30                         console.log('could not copy "'+val+'" to clipboard');
31                 }
32                 document.body.removeChild(textArea);
33         }
34 }
35
36 charmatch = /^U\+([0-9A-F]{4,})/;
37 function copycellchars() {
38         var unicode = charmatch.exec(this.title);
39         var str = String.fromCharCode(parseInt(unicode[1], 16));
40         return clipboardcopy(str);
41 }
42
43 var cells = document.getElementsByTagName('TD');
44 for (var i = 0; i < cells.length; i++) {
45         if (!cells[i].title) continue;
46         if (!charmatch.test(cells[i].title)) continue;
47         cells[i].onclick = copycellchars;
48 }
49