Snippets

Kevin Klement Smart Punctuation AutoCorrect for Quill Text Editor

Created by Kevin Klement last modified
               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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.