annotate src/bbcode/bbcode.js @ 51:78b2d6995244

add preview
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 22 Nov 2022 22:57:43 -0700
parents 0d99cec5142c
children 9f8ebc757814
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>
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
73 <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
74 <input type=file>
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
75 <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
76 <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
77 ` ;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 if(save) {
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 html += `\
45
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
80 <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
81 ` ;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 }
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 if(cancel) {
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 html += `\
45
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
85 <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
86 ` ;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 }
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 html += `\
45
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
89 </div>
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 </div>
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 ` ;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 div.innerHTML = html;
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
93
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
94 let enabledInPreview = [];
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
95
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
96 if(save) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
97 let button = div.querySelector('button[save]');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
98 button.onclick = save;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
99 enabledInPreview.push(button);
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
100 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
101 if(cancel) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
102 let button = div.querySelector('button[cancel]');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
103 button.onclick = cancel;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
104 enabledInPreview.push(button);
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
105 }
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
106
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
107 let textarea = div.querySelector('textarea');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
108 function fixTextarea() {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
109 let height = textarea.scrollHeight;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
110 if( height > textarea.clientHeight ) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
111 textarea.style.height = (height+2) + "px";
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 };
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
114 textarea.oninput = fixTextarea;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
115 fixTextarea();
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
116
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
117 let moreButton = div.querySelector('button[more]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
118 function more() {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
119 let button = moreButton;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
120 let checked = button.getAttribute('checked') !== null;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
121 let buttons = button.parentNode.querySelectorAll('button');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
122 if( checked ) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
123 button.removeAttribute('checked');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
124 for( let b of buttons ) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
125 if( b === button )
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
126 break;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
127 b.setAttribute('hidden','');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
128 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
129 } else {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
130 button.setAttribute('checked','');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
131 for( let b of buttons ) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
132 if( b === button )
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
133 break;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
134 b.removeAttribute('hidden');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
135 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
136 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
137 };
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
138 moreButton.onclick = more;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
139 more();
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
140 enabledInPreview.push(moreButton);
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
141
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
142 let divPreview = div.querySelector('div[preview]');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
143 function contextPreview(html) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
144 divPreview.innerHTML = html;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
145 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
146 let previewButton = div.querySelector('button[preview]');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
147 function preview() {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
148 let button = previewButton;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
149 let checked = button.getAttribute('checked') !== null;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
150 let buttons = button.parentNode.querySelectorAll('button');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
151 if( checked ) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
152 button.removeAttribute('checked');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
153 for( let b of buttons ) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
154 b.disabled = false;
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 textarea.style.display = 'initial';
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
157 divPreview.style.display = 'none';
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
158 textarea.focus();
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
159 } else {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
160 window.enabledInPreview = enabledInPreview;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
161 button.setAttribute('checked','');
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
162 for( let b of buttons ) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
163 if( !enabledInPreview.includes(b) )
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
164 b.disabled = true;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
165 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
166 textarea.style.display = 'none';
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
167 divPreview.style.display = 'block';
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
168 let url = '/bbcode/preview.js?text=' + encodeURIComponent(textarea.value);
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
169 ajax( url, null, {preview: contextPreview} );
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
170 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
171 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
172 previewButton.onclick = preview;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
173 enabledInPreview.push(previewButton);
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
174
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
175 function add(tag,openTag,closeTag) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
176 let button = div.querySelector('button['+tag+']');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
177 button.onclick = function() {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
178 let start = textarea.selectionStart;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
179 let end = textarea.selectionEnd;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
180 textarea.setRangeText(closeTag,end,end);
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
181 textarea.setRangeText(openTag,start,start);
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
182 let len = openTag.length;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
183 textarea.setSelectionRange(start+len,end+len);
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
184 fixTextarea();
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
185 textarea.focus();
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
186 };
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
187 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
188 add('bold','[b]','[/b]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
189 add('italic','[i]','[/i]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
190 add('underline','[u]','[/u]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
191 add('strikethrough','[s]','[/s]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
192 add('sub','[sub]','[/sub]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
193 add('sup','[sup]','[/sup]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
194 add('ul','[list]\n[item]','[/item]\n[/list]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
195 add('ol','[list=1]\n[item]','[/item]\n[/list]');
49
f225e82b2bf8 add code bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
196 add('code_block','[code]','[/code]');
f225e82b2bf8 add code bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
197 add('code_inline','[code=inline]','[/code]');
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
198
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
199 let fileInput = div.querySelector('input[type="file"]');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
200 div.querySelector('button[upload]').onclick = function(){ fileInput.click(); };
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
201 function uploaded(input,url,filename) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
202 textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
203 textarea.focus();
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
204 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
205 fileInput.onchange = function() { upload(fileInput,uploaded); };
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206 }