Skip to content

Commit b1355cb

Browse files
committed
Update codeit.js
1 parent 21c227e commit b1355cb

File tree

1 file changed

+61
-66
lines changed

1 file changed

+61
-66
lines changed

lib/codeit.js

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -733,97 +733,92 @@ class CodeitElement extends HTMLElement {
733733
function handleSelfClosingCharacters(event) {
734734

735735
const cursor = cd.dropper.cursor();
736-
const inStringOrComment = (cursor.in('string') || cursor.in('comment'));
736+
//const inStringOrComment = (cursor.in('string') || cursor.in('comment'));
737737

738-
// if cursor is not in string or comment
739-
if (!inStringOrComment) {
738+
// join brackets and quotation marks
739+
// to get chars to autocomplete
740+
const open = cd.options.openBrackets.join('') + cd.options.quot.join('');
741+
const close = cd.options.closeBrackets.join('') + cd.options.quot.join('');
740742

741-
// join brackets and quotation marks
742-
// to get chars to autocomplete
743-
const open = cd.options.openBrackets.join('') + cd.options.quot.join('');
744-
const close = cd.options.closeBrackets.join('') + cd.options.quot.join('');
743+
// get code before and after cursor
744+
const codeAfter = cd.afterCursor();
745+
const codeBefore = cd.beforeCursor();
745746

746-
// get code before and after cursor
747-
const codeAfter = cd.afterCursor();
748-
const codeBefore = cd.beforeCursor();
747+
const charBefore = codeBefore.slice(-1);
748+
const charAfter = codeAfter.charAt(0);
749749

750-
const charBefore = codeBefore.slice(-1);
751-
const charAfter = codeAfter.charAt(0);
750+
// check if typed an opening or closing char
751+
const typedOpeningChar = open.includes(event.key);
752+
const typedClosingChar = close.includes(event.key);
752753

753-
// check if typed an opening or closing char
754-
const typedOpeningChar = open.includes(event.key);
755-
const typedClosingChar = close.includes(event.key);
754+
// closing char is next to cursor if
755+
// the chars before and after the cursor are
756+
// matching opening and closing chars
757+
const closingCharNextToCursor = (charBefore === open[close.indexOf(event.key)]
758+
&& charAfter === event.key);
756759

757-
// closing char is next to cursor if
758-
// the chars before and after the cursor are
759-
// matching opening and closing chars
760-
const closingCharNextToCursor = (charBefore === open[close.indexOf(event.key)]
761-
&& charAfter === event.key);
760+
// if typed opening char
761+
if (typedOpeningChar) {
762762

763-
// if typed opening char
764-
if (typedOpeningChar) {
763+
// if selection exists
764+
if (!cursor.collapsed) {
765765

766-
// if selection exists
767-
if (!cursor.collapsed) {
766+
// prevent default behavior
767+
event.preventDefault();
768768

769-
// prevent default behavior
770-
event.preventDefault();
769+
// get the text to wrap
770+
const textToWrap = window.getSelection().toString();
771771

772-
// get the text to wrap
773-
const textToWrap = window.getSelection().toString();
772+
// wrap the text with matching opening and closing chars
773+
const wrappedText = event.key + textToWrap + close[open.indexOf(event.key)];
774774

775-
// wrap the text with matching opening and closing chars
776-
const wrappedText = event.key + textToWrap + close[open.indexOf(event.key)];
775+
// delete current selection
776+
cd.deleteCurrentSelection();
777777

778-
// delete current selection
779-
cd.deleteCurrentSelection();
778+
// insert wrapped text
779+
cd.insert(wrappedText, { moveToEnd: false });
780+
781+
// get caret pos in text
782+
const pos = cd.getSelection();
783+
784+
// restore pos in text
785+
cd.setSelection(pos.start, (pos.start + wrappedText.length));
780786

781-
// insert wrapped text
782-
cd.insert(wrappedText, { moveToEnd: false });
787+
} else {
788+
789+
// get caret pos in text
790+
const pos = cd.getSelection();
791+
792+
// if cursor is on last line
793+
if (pos.start === cd.textContent.length) {
783794

784-
// get caret pos in text
785-
const pos = cd.getSelection();
795+
// insert newline
796+
cd.insert((close[open.indexOf(event.key)] + '\n'), { moveToEnd: false });
786797

787-
// restore pos in text
788-
cd.setSelection(pos.start, (pos.start + wrappedText.length));
789-
790798
} else {
791-
792-
// get caret pos in text
793-
const pos = cd.getSelection();
794-
795-
// if cursor is on last line
796-
if (pos.start === cd.textContent.length) {
797-
798-
// insert newline
799-
cd.insert((close[open.indexOf(event.key)] + '\n'), { moveToEnd: false });
800-
801-
} else {
802-
803-
// insert matching closing char
804-
cd.insert(close[open.indexOf(event.key)], { moveToEnd: false });
805-
806-
}
807799

800+
// insert matching closing char
801+
cd.insert(close[open.indexOf(event.key)], { moveToEnd: false });
802+
808803
}
809804

810805
}
811806

812-
// if typed closing char but closing char
813-
// is already next to cursor
814-
if (typedClosingChar && closingCharNextToCursor) {
807+
}
815808

816-
// prevent default behavior
817-
event.preventDefault();
809+
// if typed closing char but closing char
810+
// is already next to cursor
811+
if (typedClosingChar && closingCharNextToCursor) {
818812

819-
// get caret pos in text
820-
const pos = cd.getSelection();
813+
// prevent default behavior
814+
event.preventDefault();
821815

822-
// move caret one char right
823-
pos.start++;
824-
cd.setSelection(pos.start);
816+
// get caret pos in text
817+
const pos = cd.getSelection();
825818

826-
}
819+
// move caret one char right
820+
pos.start++;
821+
cd.setSelection(pos.start);
827822

828823
}
829824

0 commit comments

Comments
 (0)