Skip to content

Commit b4e0bc8

Browse files
authored
Merge pull request #110 from codeitcodes/dev
Dev
2 parents 1b08c41 + ca2b6e2 commit b4e0bc8

File tree

3 files changed

+87
-63
lines changed

3 files changed

+87
-63
lines changed

filebrowser.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ const fileSizeText = '<this file is too big to view>';
833833

834834
// push file to Git from HTML element
835835
async function pushFileFromHTML(fileEl, commitMessage) {
836-
836+
837837
// disable pushing file in HTML
838838
fileEl.classList.remove('modified');
839839
bottomFloat.classList.remove('modified');
@@ -2439,11 +2439,17 @@ function setupEditor() {
24392439
// compare current code with new code
24402440
// if the code is different, swap it
24412441
if (hashCode(selText) !== hashCode(beautifiedText)) {
2442-
2442+
24432443
// replace selection contents
24442444
// with beautified text
24452445
cd.deleteCurrentSelection();
2446-
cd.insert(beautifiedText);
2446+
cd.insert(beautifiedText, { moveToEnd: false });
2447+
2448+
// get caret pos in text
2449+
const pos = cd.getSelection();
2450+
2451+
// select beautified text
2452+
cd.setSelection(pos.start, (pos.start + beautifiedText.length));
24472453

24482454
// dispatch type event (simulate typing)
24492455
cd.dispatchTypeEvent();

lib/codeit.js

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/*
66
77
codeit.js
8-
v3.0.5
8+
v3.0.6
99
MIT License
1010
1111
https://codeit.codes
@@ -282,6 +282,8 @@ class CodeitElement extends HTMLElement {
282282
&& event.key !== 'Control'
283283
&& event.key !== 'Alt'
284284
&& event.key !== 'Shift'
285+
&& event.key !== 'CapsLock'
286+
&& event.key !== 'Escape'
285287

286288
&& !event.key.startsWith('Arrow')
287289
&& !isCtrl(event);
@@ -352,14 +354,14 @@ class CodeitElement extends HTMLElement {
352354

353355
if (cd.options.preserveIdent) handleDelNewLine(event);
354356

355-
if (cd.options.addClosing) handleSelfClosingCharacters(event);
356-
357357
if (cd.options.preserveIdent) alignBracket(event);
358358

359359
}
360360

361361
if (cd.options.catchTab) handleTabCharacters(event);
362362

363+
if (cd.options.addClosing) handleSelfClosingCharacters(event);
364+
363365
if (cd.options.history) {
364366

365367
handleUndoRedo(event);
@@ -637,13 +639,35 @@ class CodeitElement extends HTMLElement {
637639
// wrap the text with matching opening and closing chars
638640
const wrappedText = event.key + textToWrap + close[open.indexOf(event.key)];
639641

642+
// delete current selection
643+
cd.deleteCurrentSelection();
644+
640645
// insert wrapped text
641-
cd.insert(wrappedText);
646+
cd.insert(wrappedText, { moveToEnd: false });
647+
648+
// get caret pos in text
649+
const pos = cd.getSelection();
650+
651+
// restore pos in text
652+
cd.setSelection(pos.start, (pos.start + wrappedText.length));
642653

643654
} else {
655+
656+
// get caret pos in text
657+
const pos = cd.getSelection();
658+
659+
// if cursor is on last line
660+
if (pos.start === cd.textContent.length) {
661+
662+
// insert newline
663+
cd.insert((close[open.indexOf(event.key)] + '\n'), { moveToEnd: false });
664+
665+
} else {
644666

645-
// insert matching closing char
646-
cd.insert(close[open.indexOf(event.key)], { moveToEnd: false });
667+
// insert matching closing char
668+
cd.insert(close[open.indexOf(event.key)], { moveToEnd: false });
669+
670+
}
647671

648672
}
649673

@@ -1414,6 +1438,45 @@ class CodeitElement extends HTMLElement {
14141438

14151439
function getCaretTextNode(caretPosInText) {
14161440

1441+
if (caretPosInText === cd.textContent.length) {
1442+
1443+
function getLastNode(parentNode) {
1444+
1445+
// if parent has child nodes
1446+
if (parentNode.childNodes.length > 0) {
1447+
1448+
const lastNode = parentNode.childNodes[parentNode.childNodes.length-1];
1449+
1450+
// if found a text node
1451+
if (lastNode.nodeType === 3) {
1452+
1453+
// return
1454+
return lastNode;
1455+
1456+
} else { // continue recursive call
1457+
1458+
return getLastNode(lastNode);
1459+
1460+
}
1461+
1462+
} else {
1463+
1464+
// return
1465+
return parentNode;
1466+
1467+
}
1468+
1469+
}
1470+
1471+
// get end node
1472+
const endNode = getLastNode(cd);
1473+
const endOffset = (endNode.nodeValue || endNode.textContent).length;
1474+
1475+
return [endNode, endOffset];
1476+
1477+
}
1478+
1479+
14171480
let overallLength = 0,
14181481
lastNode;
14191482

@@ -1503,62 +1566,17 @@ class CodeitElement extends HTMLElement {
15031566
};
15041567

15051568
} else {
1506-
1507-
if (startPos === cd.textContent.length) {
1508-
1509-
function getLastNode(parentNode) {
1510-
1511-
// if parent has child nodes
1512-
if (parentNode.childNodes.length > 0) {
1513-
1514-
const lastNode = parentNode.childNodes[parentNode.childNodes.length-1];
1515-
1516-
// if found a text node
1517-
if (lastNode.nodeType === 3) {
1518-
1519-
// return
1520-
return lastNode;
15211569

1522-
} else { // continue recursive call
1570+
// get start nodes
1571+
const [startNode, startOffset] = getCaretTextNode(startPos);
15231572

1524-
return getLastNode(lastNode);
1573+
return {
1574+
startNode: startNode,
1575+
startOffset: startOffset,
1576+
endNode: startNode,
1577+
endOffset: startOffset
1578+
};
15251579

1526-
}
1527-
1528-
} else {
1529-
1530-
// return
1531-
return parentNode;
1532-
1533-
}
1534-
1535-
}
1536-
1537-
// get end node
1538-
const endNode = getLastNode(cd);
1539-
const endOffset = (endNode.nodeValue || endNode.textContent).length;
1540-
1541-
return {
1542-
startNode: endNode,
1543-
startOffset: endOffset,
1544-
endNode: endNode,
1545-
endOffset: endOffset
1546-
};
1547-
1548-
} else {
1549-
1550-
// get start nodes
1551-
const [startNode, startOffset] = getCaretTextNode(startPos);
1552-
1553-
return {
1554-
startNode: startNode,
1555-
startOffset: startOffset,
1556-
endNode: startNode,
1557-
endOffset: startOffset
1558-
};
1559-
1560-
}
1561-
15621580
}
15631581

15641582
}

worker/client-channel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
// update worker name when updating worker
7-
const WORKER_NAME = 'codeit-worker-v473';
7+
const WORKER_NAME = 'codeit-worker-v476';
88

99

1010
// internal paths

0 commit comments

Comments
 (0)