annotate src/bbcode/bbcode.js @ 61:389e5d8e5f8a default tip

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Dec 2022 13:37:25 -0700
parents 8b5b1bce7d6b
children
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 || {};
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
55 options.buttons = options.buttons || {};
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 let content = options.content || '';
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 let html = `\
50
Franklin Schmidt <fschmidt@gmail.com>
parents: 49
diff changeset
58 <div bbcode_editor>
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
59 <div buttons=top>
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
60 <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
61 <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
62 <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
63 <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
64 <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
65 <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
66 <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
67 <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
68 <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
69 <button type=button code_inline title="Inline code"><img src="/bbcode/icons/code.svg"></button>
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
70 <button type=button convert_urls><input type=checkbox name=convert_urls checked> Convert URLs</button>
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
71 </div>
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
72 <textarea name=bbcode>${content}</textarea>
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
73 <div preview from_bbcode></div>
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
74 <div buttons=bottom>
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
75 <button type=button more><input type=checkbox> BBCode menu</button>
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
76 <input type=file>
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
77 <button type=button upload>Upload file</button>
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
78 <button type=button preview><input type=checkbox> Preview</button>
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 ` ;
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
80 for( let key of Object.keys(options.buttons) ) {
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 html += `\
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
82 <button type=button key="${key}">${key}</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 html += `\
45
2d4f00755092 bcode editor work
Franklin Schmidt <fschmidt@gmail.com>
parents: 44
diff changeset
86 </div>
44
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 </div>
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 ` ;
96f0c3d65698 add /bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 div.innerHTML = html;
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
90
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
91 let enabledInPreview = [];
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
92 let convertUrls = true;
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
93
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
94 for( let entry of Object.entries(options.buttons) ) {
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
95 console.log(entry);
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
96 let button = div.querySelector('button[key="'+entry[0]+'"]');
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
97 let fn = entry[1];
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
98 button.onclick = function(event) {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
99 event.convertUrls = convertUrls;
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
100 fn(event);
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
101 };
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
102 enabledInPreview.push(button);
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
103 }
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
104
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
105 let textarea = div.querySelector('textarea');
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
106 function fixTextarea() {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
107 let height = textarea.scrollHeight;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
108 if( height > textarea.clientHeight ) {
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
109 textarea.style.height = (height+2) + "px";
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
110 }
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
111 };
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
112 textarea.oninput = fixTextarea;
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
113 fixTextarea();
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
114
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
115 function checkboxButtonClick(event) {
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
116 event.target.querySelector('input').click();
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
117 };
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
118
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
119 let topButtons = div.querySelector('[buttons="top"]');
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
120 topButtons.style.display = 'none';
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
121 let moreButton = div.querySelector('button[more]');
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
122 moreButton.onclick = checkboxButtonClick;
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
123 let moreCheckbox = moreButton.querySelector('input');
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
124 moreCheckbox.onclick = function() {
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
125 if( moreCheckbox.checked ) {
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
126 topButtons.style.display = 'flex';
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
127 } else {
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
128 topButtons.style.display = 'none';
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
129 }
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
130 event.stopPropagation();
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
131 };
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
132
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
133 let divPreview = div.querySelector('div[preview]');
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
134 function contextPreview(bbcode,html) {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
135 textarea.value = bbcode;
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
136 divPreview.innerHTML = html;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
137 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
138 let previewButton = div.querySelector('button[preview]');
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
139 previewButton.onclick = checkboxButtonClick;
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
140 let previewCheckbox = previewButton.querySelector('input');
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
141 previewCheckbox.onclick = function() {
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
142 let buttons = previewButton.parentNode.querySelectorAll('button');
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
143 if( previewCheckbox.checked ) {
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
144 for( let b of buttons ) {
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
145 if( !enabledInPreview.includes(b) )
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
146 b.disabled = true;
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
147 }
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
148 textarea.style.display = 'none';
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
149 divPreview.style.display = 'block';
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
150 topButtons.style.display = 'none';
53
cac477dd1f82 convert image and video urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
151 let postData = 'text=' + encodeURIComponent(textarea.value) + '&convert_urls=' + convertUrls;
cac477dd1f82 convert image and video urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 52
diff changeset
152 ajax( '/bbcode/preview.js', postData, {preview: contextPreview} );
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
153 } else {
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
154 for( let b of buttons ) {
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
155 b.disabled = false;
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
156 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
157 textarea.style.display = 'initial';
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
158 divPreview.style.display = 'none';
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
159 if( moreCheckbox.checked )
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
160 topButtons.style.display = 'flex';
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
161 textarea.focus();
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
162 }
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
163 event.stopPropagation();
51
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
164 }
78b2d6995244 add preview
Franklin Schmidt <fschmidt@gmail.com>
parents: 50
diff changeset
165 enabledInPreview.push(previewButton);
48
4e1a01db19ec add lists
Franklin Schmidt <fschmidt@gmail.com>
parents: 47
diff changeset
166
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
167 let convertUrlsButton = div.querySelector('button[convert_urls]');
60
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
168 convertUrlsButton.onclick = checkboxButtonClick;
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
169 let convertUrlsCheckbox = convertUrlsButton.querySelector('input');
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
170 convertUrlsCheckbox.onclick = function(event) {
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
171 convertUrls = convertUrlsCheckbox.checked;
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
172 event.stopPropagation();
8b5b1bce7d6b bcode menus
Franklin Schmidt <fschmidt@gmail.com>
parents: 53
diff changeset
173 };
52
9f8ebc757814 add convert urls
Franklin Schmidt <fschmidt@gmail.com>
parents: 51
diff changeset
174
48
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 }