Snippets
Created by
Kevin Klement
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | quill.leftStartChars = ['(','{','[',' ','&','=',' ',' ','“','‘',"\n"];
quill.replaceTextJustTyped = function (newText, index, length) {
if (index < length) {
return;
}
quill.insertText(index, newText);
quill.deleteText((index - length), length);
}
// SMART PUNCTUATION
quill.on('text-change', function(delta, old, source) {
if (source != 'user') {
return;
}
var sel = quill.getSelection();
if (sel.index == 0) {
return;
}
var ch = quill.getText((sel.index - 1), 1);
//double quotes
if (ch == '"') {
if (sel.index == 1) {
quill.replaceTextJustTyped('“',sel.index,1);
return;
}
prevCh = quill.getText((sel.index -2),1);
if (quill.leftStartChars.includes(prevCh)) {
quill.replaceTextJustTyped('“',sel.index,1);
return;
} else {
quill.replaceTextJustTyped('”',sel.index,1);
return;
}
}
//single quotes/apostrophes
if (ch == "'") {
if (sel.index == 1) {
quill.replaceTextJustTyped('‘',sel.index,1);
return;
}
prevCh = quill.getText((sel.index -2),1);
if (quill.leftStartChars.includes(prevCh)) {
quill.replaceTextJustTyped('‘',sel.index,1);
return;
} else {
quill.replaceTextJustTyped('’',sel.index,1);
return;
}
}
// elipses
if (ch == '.') {
if (sel.index < 3) {
return;
}
prevTwo = quill.getText((sel.index -3 ), 2);
if (prevTwo == '..') {
quill.replaceTextJustTyped('…',sel.index,3);
return;
}
}
// en-dash for numerical ranges
if (/[0-9]/.test(ch)) {
if (sel.index < 3) {
return;
}
prevCh = quill.getText((sel.index -2),1);
ppCh = quill.getText((sel.index -3),1);
if ((prevCh == '-') && (/[0-9]/.test(ppCh))) {
quill.replaceTextJustTyped('–',(sel.index-1),1);
return;
}
if ((sel.index > 3) && (prevCh == '-') && (ppCh == '-') && (/[0-9]/.test(quill.getText((sel.index -4) , 1)))) {
quill.replaceTextJustTyped('–',(sel.index-1),2);
return;
}
}
// em-dashes for ' - ', ' – ', ' -- ', '---', 'a--b'
if ((ch == ' ') && (sel.index > 2)) {
prevTwo = quill.getText((sel.index -3),2);
if ((prevTwo == ' -') || (prevTwo == ' –') || (prevTwo == ' —')) {
// except when starting with a number; then give minus sign
if ((sel.index>3) && (prevTwo == ' -') && (/[0-9]/.test( quill.getText((sel.index - 4), 1) ))) {
quill.replaceTextJustTyped('−', (sel.index - 1), 1);
return;
}
quill.replaceTextJustTyped('—',sel.index,3);
return;
}
if ((prevTwo == '--') && (quill.getText((sel.index -4),1) == ' ')) {
quill.replaceTextJustTyped('—',sel.index,4);
return;
}
}
if ((ch == '-') && (sel.index > 2)) {
prevTwo = quill.getText((sel.index -3),2);
if (prevTwo == '--') {
quill.replaceTextJustTyped('—',sel.index,3);
return;
}
}
if ((/[a-z]/.test(ch)) && (sel.index > 3)) {
prevTwo = quill.getText((sel.index -3),2);
if ((prevTwo == '--') && (/[a-z]/.test( quill.getText((sel.index -4),1)))) {
quill.replaceTextJustTyped('—',(sel.index -1),2);
return;
}
}
});
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.