Improve behavior of formatting buttons

* Synthesize an input event to trigger a preview update.
* Slightly tweak the algorithm.
This commit is contained in:
gldrk 2023-11-29 08:18:10 +00:00
parent 61ec760536
commit 09c3bd9e42

View file

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