annotate src/bbcode/bbcode.js @ 52:9f8ebc757814

add convert urls
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 23 Nov 2022 23:29:16 -0700
parents 78b2d6995244
children cac477dd1f82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
1 // https://fonts.google.com/icons
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
2
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
3 function ajax(url,postData,context) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
4 let request = new XMLHttpRequest();
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
5 let method = postData ? 'POST' : 'GET';
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
6 request.open( method, url );
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
7 if( postData )
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
8 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
9 request.onload = function() {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
10 if( request.status !== 200 ) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
11 window.console && console.log( 'ajax failed: ' + request.status );
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
12 if( request.responseText )
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
13 document.write('<pre>'+request.responseText+'</pre>');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
14 return;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
15 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
16 eval( request.responseText );
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
17 };
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
18 request.send(postData);
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
19 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
20
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
21 function upload(input,callback) {
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 let file = input.files[0];
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 input.value = null;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 let request = new XMLHttpRequest();
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 let url = 'https://upload.uploadcare.com/base/';
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 request.open( 'POST', url );
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 request.onload = function() {
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 if( request.status !== 200 ) {
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 let err = 'ajax failed: ' + request.status;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 if( request.responseText ) {
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 err += '\n' + request.responseText;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 document.write('<pre>'+request.responseText+'</pre>');
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 console.log(err);
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 return;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 }
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 let response = JSON.parse(request.responseText);
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 let filename = file.name;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 let url = 'https://ucarecdn.com/' + response.file + '/' + filename;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 callback(input,url,filename);
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 };
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 let formData = new FormData();
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' );
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 formData.append( 'file', file );
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 request.send(formData);
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 }
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 function bbcodeCreate(div,options) {
46
289718f121e4 use bbcode editor
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
50 if( typeof(div) === 'string' ) {
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 div = document.querySelector(div);
46
289718f121e4 use bbcode editor
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
52 if(!div) throw 'selector not found';
289718f121e4 use bbcode editor
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
53 }
289718f121e4 use bbcode editor
Franklin Schmidt <fschmidt@gmail.com>
parents: 45
diff changeset
54 options = options || {};
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 let content = options.content || '';
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 let save = options.save;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 let cancel = options.cancel;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 let html = `\
50
Franklin Schmidt <fschmidt@gmail.com>
parents: 49
diff changeset
59 <div bbcode_editor>
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
60 <textarea name=bbcode>${content}</textarea>
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
61 <div preview from_bbcode></div>
45
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
62 <div buttons>
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
63 <button type=button bold title="Bold"><img src="/bbcode/icons/format_bold.svg"></button>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
64 <button type=button italic title="Italic"><img src="/bbcode/icons/format_italic.svg"></button>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
65 <button type=button underline title="Underline"><img src="/bbcode/icons/format_underlined.svg"></button>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
66 <button type=button strikethrough title="Strikethrough"><img src="/bbcode/icons/format_strikethrough.svg"></button>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
67 <button type=button sub title="Subscript"><img src="/bbcode/icons/subscript.svg"></button>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
68 <button type=button sup title="Superscript"><img src="/bbcode/icons/superscript.svg"></button>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
69 <button type=button ul title="Bullet list"><img src="/bbcode/icons/format_list_bulleted.svg"></button>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
70 <button type=button ol title="Numbered list"><img src="/bbcode/icons/format_list_numbered.svg"></button>
49
f225e82b2bf8 add code bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
71 <button type=button code_block title="Code block"><img src="/bbcode/icons/code_blocks.svg"></button>
f225e82b2bf8 add code bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
72 <button type=button code_inline title="Inline code"><img src="/bbcode/icons/code.svg"></button>
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
73 <button type=button convert_urls checked title="Convert URLs"><img src="/bbcode/icons/media_link.svg"></button>
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
74 <input type=hidden name=convert_urls value=true>
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
75 <button type=button more checked title="More..."><img src="/bbcode/icons/more_horiz.svg"></button>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
76 <input type=file>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
77 <button type=button upload title="Upload File"><img src="/bbcode/icons/file_upload.svg"></button>
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
78 <button type=button preview title="Preview"><img src="/bbcode/icons/preview.svg"></button>
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 ` ;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 if(save) {
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 html += `\
45
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
82 <button type=button save title="Send message"><img src="/bbcode/icons/send.svg"></button>
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 ` ;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 }
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 if(cancel) {
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 html += `\
45
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
87 <button type=button cancel title="Cancel"><img src="/bbcode/icons/cancel.svg"></button>
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 ` ;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 }
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 html += `\
45
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
91 </div>
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 </div>
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 ` ;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 div.innerHTML = html;
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
95
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
96 let enabledInPreview = [];
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
97 let convertUrls = true;
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
98
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
99 if(save) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
100 let button = div.querySelector('button[save]');
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
101 button.onclick = function(event) {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
102 event.convertUrls = convertUrls;
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
103 save(event);
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
104 };
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
105 enabledInPreview.push(button);
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
106 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
107 if(cancel) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
108 let button = div.querySelector('button[cancel]');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
109 button.onclick = cancel;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
110 enabledInPreview.push(button);
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
111 }
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
112
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
113 let textarea = div.querySelector('textarea');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
114 function fixTextarea() {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
115 let height = textarea.scrollHeight;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
116 if( height > textarea.clientHeight ) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
117 textarea.style.height = (height+2) + "px";
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
118 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
119 };
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
120 textarea.oninput = fixTextarea;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
121 fixTextarea();
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
122
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
123 function toggle(button) {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
124 let checked = button.getAttribute('checked') !== null;
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
125 checked = !checked; // toggle
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
126 if( checked ) {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
127 button.setAttribute('checked','');
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
128 } else {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
129 button.removeAttribute('checked');
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
130 }
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
131 return checked;
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
132 }
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
133
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
134 let moreButton = div.querySelector('button[more]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
135 function more() {
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
136 let checked = toggle(moreButton);
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
137 let buttons = moreButton.parentNode.querySelectorAll('button');
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
138 if( checked ) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
139 for( let b of buttons ) {
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
140 if( b === moreButton )
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
141 break;
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
142 b.removeAttribute('hidden');
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
143 }
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
144 } else {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
145 for( let b of buttons ) {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
146 if( b === moreButton )
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
147 break;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
148 b.setAttribute('hidden','');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
149 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
150 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
151 };
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
152 moreButton.onclick = more;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
153 more();
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
154 enabledInPreview.push(moreButton);
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
155
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
156 let divPreview = div.querySelector('div[preview]');
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
157 function contextPreview(bbcode,html) {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
158 textarea.value = bbcode;
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
159 divPreview.innerHTML = html;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
160 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
161 let previewButton = div.querySelector('button[preview]');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
162 function preview() {
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
163 let checked = toggle(previewButton);
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
164 let buttons = previewButton.parentNode.querySelectorAll('button');
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
165 if( checked ) {
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
166 for( let b of buttons ) {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
167 if( !enabledInPreview.includes(b) )
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
168 b.disabled = true;
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
169 }
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
170 textarea.style.display = 'none';
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
171 divPreview.style.display = 'block';
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
172 let url = '/bbcode/preview.js?text=' + encodeURIComponent(textarea.value) + '&convert_urls=' + convertUrls;
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
173 ajax( url, null, {preview: contextPreview} );
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
174 } else {
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
175 for( let b of buttons ) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
176 b.disabled = false;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
177 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
178 textarea.style.display = 'initial';
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
179 divPreview.style.display = 'none';
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
180 textarea.focus();
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
181 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
182 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
183 previewButton.onclick = preview;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
184 enabledInPreview.push(previewButton);
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
185
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
186 let convertUrlsButton = div.querySelector('button[convert_urls]');
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
187 let convertUrlsInput = div.querySelector('input[name="convert_urls"]');
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
188 function toggleConvertUrls() {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
189 convertUrls = toggle(convertUrlsButton);
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
190 convertUrlsInput.value = convertUrls;
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
191 }
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
192 convertUrlsButton.onclick = toggleConvertUrls;
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
193
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
194 function add(tag,openTag,closeTag) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
195 let button = div.querySelector('button['+tag+']');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
196 button.onclick = function() {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
197 let start = textarea.selectionStart;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
198 let end = textarea.selectionEnd;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
199 textarea.setRangeText(closeTag,end,end);
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
200 textarea.setRangeText(openTag,start,start);
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
201 let len = openTag.length;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
202 textarea.setSelectionRange(start+len,end+len);
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
203 fixTextarea();
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
204 textarea.focus();
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
205 };
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
206 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
207 add('bold','[b]','[/b]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
208 add('italic','[i]','[/i]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
209 add('underline','[u]','[/u]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
210 add('strikethrough','[s]','[/s]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
211 add('sub','[sub]','[/sub]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
212 add('sup','[sup]','[/sup]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
213 add('ul','[list]\n[item]','[/item]\n[/list]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
214 add('ol','[list=1]\n[item]','[/item]\n[/list]');
49
f225e82b2bf8 add code bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
215 add('code_block','[code]','[/code]');
f225e82b2bf8 add code bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
216 add('code_inline','[code=inline]','[/code]');
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
217
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
218 let fileInput = div.querySelector('input[type="file"]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
219 div.querySelector('button[upload]').onclick = function(){ fileInput.click(); };
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
220 function uploaded(input,url,filename) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
221 textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
222 textarea.focus();
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
223 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
224 fileInput.onchange = function() { upload(fileInput,uploaded); };
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
225 }