45
|
1 // https://fonts.google.com/icons
|
|
2
|
|
3 function bb_fixTextarea(textarea) {
|
44
|
4 let height = textarea.scrollHeight;
|
|
5 if( height > textarea.clientHeight ) {
|
|
6 textarea.style.height = (height+2) + "px";
|
|
7 }
|
|
8 }
|
|
9
|
45
|
10 function bb_fileButtonClick(button) {
|
44
|
11 button.parentNode.querySelector('input[type="file"]').click();
|
|
12 }
|
|
13
|
45
|
14 function bb_upload(input,callback) {
|
44
|
15 let file = input.files[0];
|
|
16 input.value = null;
|
|
17 let request = new XMLHttpRequest();
|
|
18 let url = 'https://upload.uploadcare.com/base/';
|
|
19 request.open( 'POST', url );
|
|
20 request.onload = function() {
|
|
21 if( request.status !== 200 ) {
|
|
22 let err = 'ajax failed: ' + request.status;
|
|
23 if( request.responseText ) {
|
|
24 err += '\n' + request.responseText;
|
|
25 document.write('<pre>'+request.responseText+'</pre>');
|
|
26 }
|
|
27 console.log(err);
|
|
28 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
|
|
29 return;
|
|
30 }
|
|
31 let response = JSON.parse(request.responseText);
|
|
32 let filename = file.name;
|
|
33 let url = 'https://ucarecdn.com/' + response.file + '/' + filename;
|
|
34 callback(input,url,filename);
|
|
35 };
|
|
36 let formData = new FormData();
|
|
37 formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' );
|
|
38 formData.append( 'file', file );
|
|
39 request.send(formData);
|
|
40 }
|
|
41
|
46
|
42 function bb_getDiv(node) {
|
44
|
43 do {
|
|
44 //console.log(node);
|
|
45 if( node.getAttribute('bbcode') !== null )
|
|
46 return node;
|
|
47 } while( node = node.parentNode );
|
|
48 }
|
|
49
|
46
|
50 function bb_getTextarea(node) {
|
|
51 return bb_getDiv(node).querySelector('textarea');
|
|
52 }
|
|
53
|
45
|
54 function bb_uploaded(input,url,filename) {
|
46
|
55 let textarea = bb_getTextarea(input);
|
45
|
56 textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select');
|
44
|
57 textarea.focus();
|
45
|
58 }
|
|
59
|
|
60 function bb_add(input,openTag,closeTag) {
|
46
|
61 let textarea = bb_getTextarea(input);
|
45
|
62 let start = textarea.selectionStart;
|
|
63 let end = textarea.selectionEnd;
|
|
64 textarea.setRangeText(closeTag,end,end);
|
|
65 textarea.setRangeText(openTag,start,start);
|
|
66 let len = openTag.length;
|
|
67 textarea.setSelectionRange(start+len,end+len);
|
|
68 textarea.focus();
|
44
|
69 }
|
|
70
|
|
71 function bbcodeCreate(div,options) {
|
46
|
72 if( typeof(div) === 'string' ) {
|
44
|
73 div = document.querySelector(div);
|
46
|
74 if(!div) throw 'selector not found';
|
|
75 }
|
|
76 options = options || {};
|
44
|
77 let content = options.content || '';
|
|
78 let save = options.save;
|
|
79 let cancel = options.cancel;
|
|
80 let html = `\
|
|
81 <div bbcode>
|
46
|
82 <textarea name=bbcode oninput="bb_fixTextarea(this)">${content}</textarea>
|
45
|
83 <div buttons>
|
|
84 <button type=button onclick="bb_add(this,'[b]','[/b]')" title="Bold"><img src="/bbcode/icons/format_bold.svg"></button>
|
|
85 <button type=button onclick="bb_add(this,'[i]','[/i]')" title="Italic"><img src="/bbcode/icons/format_italic.svg"></button>
|
|
86 <button type=button onclick="bb_add(this,'[u]','[/u]')" title="Underline"><img src="/bbcode/icons/format_underlined.svg"></button>
|
|
87 <button type=button onclick="bb_add(this,'[s]','[/s]')" title="Strikethrough"><img src="/bbcode/icons/format_strikethrough.svg"></button>
|
|
88 <button type=button onclick="bb_add(this,'[sub]','[/sub]')" title="Subscript"><img src="/bbcode/icons/subscript.svg"></button>
|
|
89 <button type=button onclick="bb_add(this,'[sup]','[/sup]')" title="Superscript"><img src="/bbcode/icons/superscript.svg"></button>
|
|
90 <input type=file onchange="bb_upload(this,bb_uploaded)">
|
|
91 <button type=button onclick="bb_fileButtonClick(this)" title="Upload File"><img src="/bbcode/icons/file_upload.svg"></button>
|
44
|
92 ` ;
|
|
93 if(save) {
|
|
94 html += `\
|
45
|
95 <button type=button save title="Send message"><img src="/bbcode/icons/send.svg"></button>
|
44
|
96 ` ;
|
|
97 }
|
|
98 if(cancel) {
|
|
99 html += `\
|
45
|
100 <button type=button cancel title="Cancel"><img src="/bbcode/icons/cancel.svg"></button>
|
44
|
101 ` ;
|
|
102 }
|
|
103 html += `\
|
45
|
104 </div>
|
44
|
105 </div>
|
|
106 ` ;
|
|
107 div.innerHTML = html;
|
|
108 if(save)
|
|
109 div.querySelector('button[save]').addEventListener('click',save);
|
|
110 if(cancel)
|
|
111 div.querySelector('button[cancel]').addEventListener('click',cancel);
|
46
|
112 let textarea = div.querySelector('textarea')
|
|
113 bb_fixTextarea(textarea);
|
44
|
114 }
|