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