Composer: Switch to using a dispatch table for buttons

Note: for now, it looks a little messy, but once I merge wrapSelectionInTextareaWith and insertIntoTextarea, it won't be. Promise.
v1.18.x
Micheil Smith 11 years ago
parent 016ea8dc32
commit bf7de0d051

@ -414,90 +414,101 @@ define(['taskbar'], function(taskbar) {
thumbForm.submit(); thumbForm.submit();
} }
function handleFormattingBarClick() {
var iconClass = $(this).find('i').attr('class');
var textarea = $(this).parents('.composer').find('textarea')[0];
var textareaValue = $(textarea).val();
var selectionStart = textarea.selectionStart,
selectionEnd = textarea.selectionEnd,
selectionLength = selectionEnd - selectionStart,
isSelectionAtEnd = selectionStart === selectionEnd;
function updateSelection(start, end){
textarea.setSelectionRange(start, end);
textarea.focus();
}
function insertIntoInput(value) { /*************************************************/
$(textarea).val(textareaValue.slice(0, selectionStart) + value + textareaValue.slice(selectionStart)); /* Rich Textarea Controls */
} /*************************************************/
function insertIntoTextarea(textarea, value) {
var $textarea = $(textarea);
var currentVal = $textarea.val();
$textarea.val(
currentVal.slice(0, textarea.selectionStart) +
value +
currentVal.slice(textarea.selectionStart)
);
}
function wrapSelectedWith(leading, trailing){ function wrapSelectionInTextareaWith(textarea, leading, trailing){
if(trailing === undefined){ if(trailing === undefined){
trailing = leading; trailing = leading;
} }
$(textarea).val(textareaValue.slice(0, selectionStart) + leading + textareaValue.slice(selectionStart, selectionEnd) + trailing + textareaValue.slice(selectionEnd)); var $textarea = $(textarea);
var currentVal = $textarea.val()
$textarea.val(
currentVal.slice(0, textarea.selectionStart) +
leading +
currentVal.slice(textarea.selectionStart, textarea.selectionEnd) +
trailing +
currentVal.slice(textarea.selectionEnd)
);
} }
if(iconClass === 'fa fa-bold') { function updateTextareaSelection(textarea, start, end){
if (isSelectionAtEnd) { textarea.setSelectionRange(start, end);
insertIntoInput("**bolded text**"); $(textarea).focus();
}
updateSelection(selectionStart + 2, selectionStart + 13); var formattingDispatchTable = {
'fa fa-bold': function(textarea, selectionStart, selectionEnd){
if(selectionStart === selectionEnd){
insertIntoTextarea(textarea, '**bolded text**');
updateTextareaSelection(textarea, selectionStart + 2, selectionStart + 13);
} else { } else {
wrapSelectedWith('**'); wrapSelectionInTextareaWith(textarea, '**');
updateTextareaSelection(textarea, selectionStart + 2, selectionEnd + 2);
// Highlight selection
updateSelection(selectionStart + 2, selectionEnd + 2);
} }
} },
if(iconClass === 'fa fa-italic') {
if (isSelectionAtEnd) {
insertIntoInput("*italicised text*");
// Highlight selection 'fa fa-italic': function(textarea, selectionStart, selectionEnd){
updateSelection(selectionStart + 1, selectionStart + 16); if(selectionStart === selectionEnd){
insertIntoTextarea(textarea, "*italicised text*");
updateTextareaSelection(textarea, selectionStart + 1, selectionStart + 16);
} else { } else {
wrapSelectedWith('*'); wrapSelectionInTextareaWith(textarea, '*');
updateTextareaSelection(textarea, selectionStart + 1, selectionEnd + 1);
// Highlight selection
updateSelection(selectionStart + 1, selectionEnd + 1);
} }
} },
if (iconClass === 'fa fa-list'){ 'fa fa-list': function(textarea, selectionStart, selectionEnd){
if(isSelectionAtEnd){ if(selectionStart === selectionEnd){
insertIntoInput("\n* list item"); insertIntoTextarea(textarea, "\n* list item");
// Highlight "list item" // Highlight "list item"
updateSelection(selectionStart + 3, selectionStart + 12); updateTextareaSelection(textarea, selectionStart + 3, selectionStart + 12);
} else { } else {
wrapSelectedWith('\n* ', ''); wrapSelectionInTextareaWith(textarea, '\n* ', '');
updateTextareaSelection(textarea, selectionStart + 3, selectionEnd + 3);
// Maintain selection:
updateSelection(selectionStart + 3, selectionEnd + 3);
} }
} },
if (iconClass === 'fa fa-link') { 'fa fa-link': function(textarea, selectionStart, selectionEnd){
if (isSelectionAtEnd) { if(selectionStart === selectionEnd){
insertIntoInput("[link text](link url)"); insertIntoTextarea(textarea, "[link text](link url)");
// Highlight "link url" // Highlight "link url"
updateSelection(selectionStart + 12, selectionEnd + 20); updateTextareaSelection(textarea, selectionStart + 12, selectionEnd + 20);
} else { } else {
wrapSelectedWith('[', '](link url)'); wrapSelectionInTextareaWith(textarea, '[', '](link url)');
// Highlight "link url" // Highlight "link url"
updateSelection(selectionStart + selectionLength + 3, selectionEnd + 11); updateTextareaSelection(textarea, selectionEnd + 3, selectionEnd + 11);
} }
} }
};
function handleFormattingBarClick() {
var iconClass = $(this).find('i').attr('class');
var textarea = $(this).parents('.composer').find('textarea')[0];
if(formattingDispatchTable.hasOwnProperty(iconClass)){
formattingDispatchTable[iconClass](textarea, textarea.selectionStart, textarea.selectionEnd);
}
} }
composer.newTopic = function(cid) { composer.newTopic = function(cid) {
if(!allowed()) { if(!allowed()) {
return; return;

Loading…
Cancel
Save