|
5 | 5 | /* |
6 | 6 |
|
7 | 7 | codeit.js |
8 | | - v3.0.5 |
| 8 | + v3.0.6 |
9 | 9 | MIT License |
10 | 10 |
|
11 | 11 | https://codeit.codes |
@@ -282,6 +282,8 @@ class CodeitElement extends HTMLElement { |
282 | 282 | && event.key !== 'Control' |
283 | 283 | && event.key !== 'Alt' |
284 | 284 | && event.key !== 'Shift' |
| 285 | + && event.key !== 'CapsLock' |
| 286 | + && event.key !== 'Escape' |
285 | 287 |
|
286 | 288 | && !event.key.startsWith('Arrow') |
287 | 289 | && !isCtrl(event); |
@@ -352,14 +354,14 @@ class CodeitElement extends HTMLElement { |
352 | 354 |
|
353 | 355 | if (cd.options.preserveIdent) handleDelNewLine(event); |
354 | 356 |
|
355 | | - if (cd.options.addClosing) handleSelfClosingCharacters(event); |
356 | | - |
357 | 357 | if (cd.options.preserveIdent) alignBracket(event); |
358 | 358 |
|
359 | 359 | } |
360 | 360 |
|
361 | 361 | if (cd.options.catchTab) handleTabCharacters(event); |
362 | 362 |
|
| 363 | + if (cd.options.addClosing) handleSelfClosingCharacters(event); |
| 364 | + |
363 | 365 | if (cd.options.history) { |
364 | 366 |
|
365 | 367 | handleUndoRedo(event); |
@@ -637,13 +639,35 @@ class CodeitElement extends HTMLElement { |
637 | 639 | // wrap the text with matching opening and closing chars |
638 | 640 | const wrappedText = event.key + textToWrap + close[open.indexOf(event.key)]; |
639 | 641 |
|
| 642 | + // delete current selection |
| 643 | + cd.deleteCurrentSelection(); |
| 644 | + |
640 | 645 | // 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)); |
642 | 653 |
|
643 | 654 | } 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 { |
644 | 666 |
|
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 | + } |
647 | 671 |
|
648 | 672 | } |
649 | 673 |
|
@@ -1414,6 +1438,45 @@ class CodeitElement extends HTMLElement { |
1414 | 1438 |
|
1415 | 1439 | function getCaretTextNode(caretPosInText) { |
1416 | 1440 |
|
| 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 | + |
1417 | 1480 | let overallLength = 0, |
1418 | 1481 | lastNode; |
1419 | 1482 |
|
@@ -1503,62 +1566,17 @@ class CodeitElement extends HTMLElement { |
1503 | 1566 | }; |
1504 | 1567 |
|
1505 | 1568 | } 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; |
1521 | 1569 |
|
1522 | | - } else { // continue recursive call |
| 1570 | + // get start nodes |
| 1571 | + const [startNode, startOffset] = getCaretTextNode(startPos); |
1523 | 1572 |
|
1524 | | - return getLastNode(lastNode); |
| 1573 | + return { |
| 1574 | + startNode: startNode, |
| 1575 | + startOffset: startOffset, |
| 1576 | + endNode: startNode, |
| 1577 | + endOffset: startOffset |
| 1578 | + }; |
1525 | 1579 |
|
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 | | - |
1562 | 1580 | } |
1563 | 1581 |
|
1564 | 1582 | } |
|
0 commit comments