Skip to content

Commit

Permalink
Improve behavior of formatting buttons
Browse files Browse the repository at this point in the history
* Synthesize an input event to trigger a preview update.
* Slightly tweak the algorithm.
  • Loading branch information
gldrk authored and zorbathut committed Dec 9, 2023
1 parent 61ec760 commit 00b8e7a
Showing 1 changed file with 55 additions and 57 deletions.
112 changes: 55 additions & 57 deletions files/assets/js/formatting.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,55 @@

function makeBold(form) {
var text = document.getElementById(form);
var startIndex = text.selectionStart,
endIndex = text.selectionEnd;
var selectedText = text.value.substring(startIndex, endIndex);

var format = '**'

if (selectedText.includes('**')) {
text.value = selectedText.replace(/\*/g, '');
}
else if (selectedText.length == 0) {
text.value = text.value.substring(0, startIndex) + selectedText + text.value.substring(endIndex);
}
else {
text.value = text.value.substring(0, startIndex) + format + selectedText + format + text.value.substring(endIndex);
}
}

function makeItalics(form) {
var text = document.getElementById(form);
var startIndex = text.selectionStart,
endIndex = text.selectionEnd;
var selectedText = text.value.substring(startIndex, endIndex);

var format = '*'

if (selectedText.includes('*')) {
text.value = selectedText.replace(/\*/g, '');
}
else if (selectedText.length == 0) {
text.value = text.value.substring(0, startIndex) + selectedText + text.value.substring(endIndex);
}
else {
text.value = text.value.substring(0, startIndex) + format + selectedText + format + text.value.substring(endIndex);
}
}

function makeQuote(form) {
var text = document.getElementById(form);
var startIndex = text.selectionStart,
endIndex = text.selectionEnd;
var selectedText = text.value.substring(startIndex, endIndex);

var format = '>'

if (selectedText.includes('>')) {
text.value = text.value.substring(0, startIndex) + selectedText.replace(/\>/g, '') + text.value.substring(endIndex);
}
else if (selectedText.length == 0) {
text.value = text.value.substring(0, startIndex) + selectedText + text.value.substring(endIndex);
}
else {
text.value = text.value.substring(0, startIndex) + format + selectedText + text.value.substring(endIndex);
}
}
(function() {
var event = InputEvent
? function(type, attrs) {
return new InputEvent(type, attrs);
}
: function(type) {
e = document.createEvent('event');
e.initEvent(type, false, false);
return e;
};
var escape = function(str) {
return str.replace(/./g, '[$&]').replace(/[\\^]|]]/g, '\\$&');
};
var wrap = function(cb) {
return function(id) {
var form = document.getElementById(id);
if (cb(form)) {
var e = event('input', { inputType: 'insertReplacementText' });
form.dispatchEvent(e);
}
}
};
var select = function(cb) {
return function(form) {
var begin = form.selectionStart, end = form.selectionEnd;
if (begin == end)
return false;
form.value = form.value.substring(0, begin)
+ cb(form.value.substring(begin, end))
+ form.value.substring(end);
return true;
};
};
var enclose = function(mark) {
var re = new RegExp(escape(mark) + '(\\S.*?\\S|\\S)' + escape(mark), 'g');
return select(function(selection) {
var replacement = selection.replace(re, '$1');
if (replacement.length == selection.length)
replacement = mark + replacement + mark;
return replacement;
});
};
var quote = select(function(selection) {
var lines = selection.split('\n');
if (lines.some(function(line) { return /^\s*[^\s>]/.test(line) }))
return '>' + lines.join('\n>');
else
return lines.map(function(line) {
return line.substring(line.indexOf('>') + 1);
}).join('\n');
});
makeItalics = wrap(enclose('*'));
makeBold = wrap(enclose('**'));
makeQuote = wrap(quote);
})()

0 comments on commit 00b8e7a

Please sign in to comment.