28
loading...
This website collects cookies to deliver better user experience
editing.addEventListener('keydown', function (event) {
// Other older stuff...
if (event.key === 'Enter') {
// Code...
}
}
function looksLikeBullet(text, caretPos) {
let line = text.substring(0, caretPos).split(/\r?\n|\r/).pop();
}
let bulletRegex = /^([ \t]*[\*\-\+]\s*).*/gim;
if (bulletRegex.test(line)) {
return {
bullet: line.replace(bulletRegex, '$1')
};
}
return false;
/^([ \t]*[\*\-\+]\s*).*/gim
/ // Start of regex
^ // Start of line
( // Capturing group
[ \t]* // Any number of spaces or tabs
[\*\-\+] // Any of the bullet symbols +, -, or *
\s* // The whitespace after the bullet. We capture this so that we can use it later
) // End of capturing group
.* // Anything, ie. the rest of the bullet
/gim // End of regex, and tags global, case insensitive, and multiline
let bullet = looksLikeBullet(editing.value, editing.selectionStart);
if (bullet) {
// Code...
}
// Prevent the newline from being added:
event.preventDefault();
// Store the text after the cursor, so it can be added to the next line:
let addition = editing.value.substring(editing.selectionStart);
// Remove the text after the cursor:
editing.value = editing.value.substring(0, editing.selectionStart);
// Insert the bullet in the textarea
editing.value += ('\n' + bullet.bullet + addition);
looksLikeBullet
function, and check for numbers....
let numberedListRegex = /^[ \t]*(\d+\.\s*).*/gim;
if (...) {
...
} else if (numberedListRegex.test(line)) {
return {
bullet: line
.replace(numberedListRegex, "$1")
}
}
return false;
bullet: line
.replace(numberedListRegex, "$1")
.replace((/\d+/, (number) => +number + 1)
function resize() {
editing.style.height = '24px';
editing.style.height = this.scrollHeight + 12 + 'px';
}
input
event:editing.addEventListener('input', resize)
keydown
listener also. If we do that, we get the perfect auto-resize