react-input-emoji 5.6.5 → 5.6.6

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.es.js CHANGED
@@ -682,35 +682,100 @@ function totalCharacters(_ref2) {
682
682
  var emojisCount = (html.match(/<img/g) || []).length;
683
683
  return textCount + emojisCount;
684
684
  }
685
+ /**
686
+ *
687
+ * @param {HTMLDivElement} inputDiv
688
+ * @return {string}
689
+ */
690
+ function removeHtmlExceptBr(inputDiv) {
691
+ var temp = inputDiv.innerHTML.replace(/<br\s*\/?>/gi, "[BR]"); // temporarily replace <br> with placeholder
692
+ var stripped = temp.replace(/<[^>]+>/g, ""); // strip all html tags
693
+ var _final = stripped.replace(/\[BR\]/gi, "</br>"); // replace placeholders with <br>
694
+ return _final;
695
+ }
685
696
 
686
697
  /**
687
- * Set caret to the end of text value
688
- * @param {React.MutableRefObject<HTMLDivElement| null>} input
698
+ *
699
+ * @param {*} range
700
+ * @returns
689
701
  */
690
- function moveCaretToEnd(input) {
691
- var range;
692
- var selection;
693
- if (document.createRange && input.current) {
694
- range = document.createRange();
695
- range.selectNodeContents(input.current);
696
- range.collapse(false);
697
- selection = window.getSelection();
698
- if (selection) {
699
- selection.removeAllRanges();
700
- selection.addRange(range);
702
+ function getSelectionStart$1(range) {
703
+ var node = range.startContainer;
704
+ var offset = range.startOffset;
705
+
706
+ // Handle cases where the selection start node is not a text node
707
+ if (node.nodeType !== Node.TEXT_NODE) {
708
+ while (node.nodeType !== Node.TEXT_NODE) {
709
+ node = node.nextSibling;
710
+ if (!node) break;
701
711
  }
712
+ if (!node) {
713
+ node = range.commonAncestorContainer;
714
+ while (node.nodeType !== Node.TEXT_NODE) {
715
+ node = node.firstChild;
716
+ }
717
+ }
718
+ offset = 0;
702
719
  }
720
+ return {
721
+ node: node,
722
+ offset: offset
723
+ };
703
724
  }
725
+
704
726
  /**
705
727
  *
706
- * @param {HTMLDivElement} inputDiv
707
- * @return {string}
728
+ * @returns {Object} cursor
708
729
  */
709
- function removeHtmlExceptBr(inputDiv) {
710
- var temp = inputDiv.innerHTML.replace(/<br\s*\/?>/gi, '[BR]'); // temporarily replace <br> with placeholder
711
- var stripped = temp.replace(/<[^>]+>/g, ''); // strip all html tags
712
- var _final = stripped.replace(/\[BR\]/gi, '</br>'); // replace placeholders with <br>
713
- return _final;
730
+ function getCursor() {
731
+ var selection = window.getSelection();
732
+ var range = selection.getRangeAt(0);
733
+ var selectionStart = getSelectionStart$1(range);
734
+ return {
735
+ selection: selection,
736
+ range: range,
737
+ selectionStart: selectionStart
738
+ };
739
+ }
740
+
741
+ /**
742
+ *
743
+ */
744
+ function addLineBreak() {
745
+ var _getCursor = getCursor(),
746
+ selection = _getCursor.selection,
747
+ range = _getCursor.range,
748
+ selectionStart = _getCursor.selectionStart;
749
+
750
+ // If cursor is at the end of the text content, add one more line break
751
+ if (selection.isCollapsed && selectionStart.offset === selectionStart.node.textContent.length) {
752
+ var br = document.createElement("br");
753
+ range.insertNode(br);
754
+ range.setStartAfter(br);
755
+ range.setEndAfter(br);
756
+ selection.removeAllRanges();
757
+ selection.addRange(range);
758
+ var br2 = document.createElement("br");
759
+ range.insertNode(br2);
760
+ range.setStartAfter(br2);
761
+ range.setEndAfter(br2);
762
+ selection.removeAllRanges();
763
+ selection.addRange(range);
764
+ } else {
765
+ var _br = document.createElement("br");
766
+ range.insertNode(_br);
767
+ range.setStartAfter(_br);
768
+ range.setEndAfter(_br);
769
+ selection.removeAllRanges();
770
+ selection.addRange(range);
771
+ // Set cursor position right before the first letter after the line break
772
+ if (selectionStart.node.nextSibling && selectionStart.node.nextSibling.nodeType === Node.TEXT_NODE) {
773
+ range.setStart(selectionStart.node.nextSibling, 1);
774
+ range.setEnd(selectionStart.node.nextSibling, 1);
775
+ }
776
+ selection.removeAllRanges();
777
+ selection.addRange(range);
778
+ }
714
779
  }
715
780
 
716
781
  // @ts-check
@@ -969,8 +1034,7 @@ var TextInput = function TextInput(_ref, ref) {
969
1034
  if (event.key === "Enter" && (event.shiftKey === true || event.ctrlKey === true) && props.shouldReturn) {
970
1035
  event.preventDefault();
971
1036
  if (textInputRef.current) {
972
- textInputRef.current.innerHTML = "".concat(textInputRef.current.innerHTML, "</br></br>");
973
- moveCaretToEnd(textInputRef);
1037
+ addLineBreak();
974
1038
  return;
975
1039
  }
976
1040
  }