Mercurial Hosting > freedit
comparison src/bbcode/bbcode.js @ 44:96f0c3d65698
add /bbcode
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 10 Nov 2022 23:18:58 -0700 |
parents | |
children | 2d4f00755092 |
comparison
equal
deleted
inserted
replaced
43:298c71e0c854 | 44:96f0c3d65698 |
---|---|
1 function fixTextarea(textarea) { | |
2 let height = textarea.scrollHeight; | |
3 if( height > textarea.clientHeight ) { | |
4 textarea.style.height = (height+2) + "px"; | |
5 } | |
6 } | |
7 | |
8 function fileButtonClick(button) { | |
9 button.parentNode.querySelector('input[type="file"]').click(); | |
10 } | |
11 | |
12 function upload(input,callback) { | |
13 let file = input.files[0]; | |
14 input.value = null; | |
15 let request = new XMLHttpRequest(); | |
16 let url = 'https://upload.uploadcare.com/base/'; | |
17 request.open( 'POST', url ); | |
18 request.onload = function() { | |
19 if( request.status !== 200 ) { | |
20 let err = 'ajax failed: ' + request.status; | |
21 if( request.responseText ) { | |
22 err += '\n' + request.responseText; | |
23 document.write('<pre>'+request.responseText+'</pre>'); | |
24 } | |
25 console.log(err); | |
26 ajax( '/error_log.js', 'err='+encodeURIComponent(err) ); | |
27 return; | |
28 } | |
29 let response = JSON.parse(request.responseText); | |
30 let filename = file.name; | |
31 let url = 'https://ucarecdn.com/' + response.file + '/' + filename; | |
32 callback(input,url,filename); | |
33 }; | |
34 let formData = new FormData(); | |
35 formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' ); | |
36 formData.append( 'file', file ); | |
37 request.send(formData); | |
38 } | |
39 | |
40 function getBbcodeDiv(node) { | |
41 do { | |
42 //console.log(node); | |
43 if( node.getAttribute('bbcode') !== null ) | |
44 return node; | |
45 } while( node = node.parentNode ); | |
46 } | |
47 | |
48 function uploaded(input,url,filename) { | |
49 let div = getBbcodeDiv(input); | |
50 let textarea = div.querySelector('textarea'); | |
51 textarea.focus(); | |
52 textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select'); | |
53 } | |
54 | |
55 function bbcodeCreate(div,options) { | |
56 if( typeof(div) === 'string' ) | |
57 div = document.querySelector(div); | |
58 let content = options.content || ''; | |
59 let save = options.save; | |
60 let cancel = options.cancel; | |
61 let html = `\ | |
62 <div bbcode> | |
63 <textarea oninput="fixTextarea(this)"></textarea> | |
64 <p> | |
65 <input type=file onchange="upload(this,uploaded)"> | |
66 <button type=button onclick="fileButtonClick(this)">Upload File</button> | |
67 ` ; | |
68 if(save) { | |
69 html += `\ | |
70 <button type=button save>save</button> | |
71 ` ; | |
72 } | |
73 if(cancel) { | |
74 html += `\ | |
75 <button type=button cancel>cancel</button> | |
76 ` ; | |
77 } | |
78 html += `\ | |
79 </p> | |
80 </div> | |
81 ` ; | |
82 div.innerHTML = html; | |
83 if(save) | |
84 div.querySelector('button[save]').addEventListener('click',save); | |
85 if(cancel) | |
86 div.querySelector('button[cancel]').addEventListener('click',cancel); | |
87 } |