Skip to content

Instantly share code, notes, and snippets.

@trey
Last active March 20, 2021 13:40

Revisions

  1. trey revised this gist Mar 20, 2021. No changes.
  2. trey renamed this gist Mar 20, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. trey created this gist Mar 20, 2021.
    44 changes: 44 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    const $textarea = $('textarea');
    const ulRegex = /^- .+/;
    const olRegex = /^\d+\. .+/;
    const olNumberRegex = /^\d+/;
    const emptyLiRegex = /^(-|(\d+\.)) $/;
    const emptyOlLiRegex = /^\d+\. $/;

    $textarea.on('keyup', e => {
    const $this = $(e.target);

    if (e.which === 13) {
    // `13` is the return key.
    const cursorPosition = $this.prop('selectionStart');
    const everything = $this.val();
    const beforeCursor = everything.slice(0, cursorPosition);
    const afterCursor = everything.slice(cursorPosition);
    let lines = beforeCursor.split('\n');
    const lastLine = lines[lines.length - 2];

    if (lastLine.match(ulRegex)) {
    $this.val(beforeCursor + '- ' + afterCursor);
    $this[0].setSelectionRange(cursorPosition + 2, cursorPosition + 2);
    }

    if (lastLine.match(olRegex)) {
    let nextNumber = Number(olNumberRegex.exec(lastLine)[0]) + 1;
    let numberLength = nextNumber.toString().length + 2;
    $this.val(beforeCursor + `${nextNumber}. ` + afterCursor);
    $this[0].setSelectionRange(cursorPosition + numberLength, cursorPosition + numberLength);
    }

    if (lastLine.match(emptyLiRegex)) {
    let offset = 3;

    if (lastLine.match(emptyOlLiRegex)) {
    offset += Number(olNumberRegex.exec(lastLine)[0].length);
    }

    lines.splice(-2, 1);
    $this.val(lines.join('\n') + '' + afterCursor);
    $this[0].setSelectionRange(cursorPosition - offset, cursorPosition - offset);
    }
    }
    });