comparison src/bbcode/bbcode.js @ 47:7ef9222474e2

add more button
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 14 Nov 2022 23:01:29 -0700
parents 289718f121e4
children 4e1a01db19ec
comparison
equal deleted inserted replaced
46:289718f121e4 47:7ef9222474e2
37 formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' ); 37 formData.append( 'UPLOADCARE_PUB_KEY', 'fe3d30f3088a50941d45' );
38 formData.append( 'file', file ); 38 formData.append( 'file', file );
39 request.send(formData); 39 request.send(formData);
40 } 40 }
41 41
42 function bb_getDiv(node) { 42 function bb_getDivBbcode(node) {
43 do { 43 do {
44 //console.log(node); 44 //console.log(node);
45 if( node.getAttribute('bbcode') !== null ) 45 if( node.getAttribute('bbcode') !== null )
46 return node; 46 return node;
47 } while( node = node.parentNode ); 47 } while( node = node.parentNode );
48 } 48 }
49 49
50 function bb_getTextarea(node) { 50 function bb_getTextarea(node) {
51 return bb_getDiv(node).querySelector('textarea'); 51 return bb_getDivBbcode(node).querySelector('textarea');
52 } 52 }
53 53
54 function bb_uploaded(input,url,filename) { 54 function bb_uploaded(input,url,filename) {
55 let textarea = bb_getTextarea(input); 55 let textarea = bb_getTextarea(input);
56 textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select'); 56 textarea.setRangeText(url,textarea.selectionStart,textarea.selectionEnd,'select');
64 textarea.setRangeText(closeTag,end,end); 64 textarea.setRangeText(closeTag,end,end);
65 textarea.setRangeText(openTag,start,start); 65 textarea.setRangeText(openTag,start,start);
66 let len = openTag.length; 66 let len = openTag.length;
67 textarea.setSelectionRange(start+len,end+len); 67 textarea.setSelectionRange(start+len,end+len);
68 textarea.focus(); 68 textarea.focus();
69 }
70
71 function bb_more(button) {
72 let checked = button.getAttribute('checked') !== null;
73 let buttons = button.parentNode.querySelectorAll('button');
74 if( checked ) {
75 button.removeAttribute('checked');
76 for( let b of buttons ) {
77 if( b === button )
78 break;
79 b.setAttribute('hidden','');
80 }
81 } else {
82 button.setAttribute('checked','');
83 for( let b of buttons ) {
84 if( b === button )
85 break;
86 b.removeAttribute('hidden');
87 }
88 }
69 } 89 }
70 90
71 function bbcodeCreate(div,options) { 91 function bbcodeCreate(div,options) {
72 if( typeof(div) === 'string' ) { 92 if( typeof(div) === 'string' ) {
73 div = document.querySelector(div); 93 div = document.querySelector(div);
85 <button type=button onclick="bb_add(this,'[i]','[/i]')" title="Italic"><img src="/bbcode/icons/format_italic.svg"></button> 105 <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> 106 <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> 107 <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> 108 <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> 109 <button type=button onclick="bb_add(this,'[sup]','[/sup]')" title="Superscript"><img src="/bbcode/icons/superscript.svg"></button>
110 <button more checked type=button onclick="bb_more(this)" title="More..."><img src="/bbcode/icons/more_horiz.svg"></button>
90 <input type=file onchange="bb_upload(this,bb_uploaded)"> 111 <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> 112 <button type=button onclick="bb_fileButtonClick(this)" title="Upload File"><img src="/bbcode/icons/file_upload.svg"></button>
92 ` ; 113 ` ;
93 if(save) { 114 if(save) {
94 html += `\ 115 html += `\
109 div.querySelector('button[save]').addEventListener('click',save); 130 div.querySelector('button[save]').addEventListener('click',save);
110 if(cancel) 131 if(cancel)
111 div.querySelector('button[cancel]').addEventListener('click',cancel); 132 div.querySelector('button[cancel]').addEventListener('click',cancel);
112 let textarea = div.querySelector('textarea') 133 let textarea = div.querySelector('textarea')
113 bb_fixTextarea(textarea); 134 bb_fixTextarea(textarea);
135 bb_more( div.querySelector('button[more]') );
114 } 136 }