react-input-emoji 5.6.4 → 5.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -600,6 +600,24 @@ function replaceAllTextEmojiToString(html) {
600
600
 
601
601
  // @ts-check
602
602
 
603
+ /**
604
+ * Handle copy of current selected text
605
+ * @param {React.ClipboardEvent} event
606
+ */
607
+ function handleCopy(event) {
608
+ var selectedText = window.getSelection();
609
+ if (selectedText === null) {
610
+ return;
611
+ }
612
+ var container = document.createElement("div");
613
+ for (var i = 0, len = selectedText.rangeCount; i < len; ++i) {
614
+ container.appendChild(selectedText.getRangeAt(i).cloneContents());
615
+ }
616
+ container = replaceEmojiToString(container);
617
+ event.clipboardData.setData("text", container.innerText);
618
+ event.preventDefault();
619
+ }
620
+
603
621
  /**
604
622
  *
605
623
  * @param {string} html
@@ -639,6 +657,19 @@ function handlePasteHtmlAtCaret(html) {
639
657
  }
640
658
  }
641
659
 
660
+ /**
661
+ * Replace emoji img to its string value
662
+ * @param {HTMLDivElement} container
663
+ * @return {HTMLDivElement}
664
+ */
665
+ function replaceEmojiToString(container) {
666
+ var images = Array.prototype.slice.call(container.querySelectorAll("img"));
667
+ images.forEach(function (image) {
668
+ image.outerHTML = image.dataset.emoji;
669
+ });
670
+ return container;
671
+ }
672
+
642
673
  /**
643
674
  *
644
675
  * @param {{text: string, html: string}} props
@@ -651,35 +682,100 @@ function totalCharacters(_ref2) {
651
682
  var emojisCount = (html.match(/<img/g) || []).length;
652
683
  return textCount + emojisCount;
653
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
+ }
654
696
 
655
697
  /**
656
- * Set caret to the end of text value
657
- * @param {React.MutableRefObject<HTMLDivElement| null>} input
698
+ *
699
+ * @param {*} range
700
+ * @returns
658
701
  */
659
- function moveCaretToEnd(input) {
660
- var range;
661
- var selection;
662
- if (document.createRange && input.current) {
663
- range = document.createRange();
664
- range.selectNodeContents(input.current);
665
- range.collapse(false);
666
- selection = window.getSelection();
667
- if (selection) {
668
- selection.removeAllRanges();
669
- 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;
670
711
  }
712
+ if (!node) {
713
+ node = range.commonAncestorContainer;
714
+ while (node.nodeType !== Node.TEXT_NODE) {
715
+ node = node.firstChild;
716
+ }
717
+ }
718
+ offset = 0;
671
719
  }
720
+ return {
721
+ node: node,
722
+ offset: offset
723
+ };
672
724
  }
725
+
673
726
  /**
674
727
  *
675
- * @param {HTMLDivElement} inputDiv
676
- * @return {string}
728
+ * @returns {Object} cursor
677
729
  */
678
- function removeHtmlExceptBr(inputDiv) {
679
- var temp = inputDiv.innerHTML.replace(/<br\s*\/?>/gi, '[BR]'); // temporarily replace <br> with placeholder
680
- var stripped = temp.replace(/<[^>]+>/g, ''); // strip all html tags
681
- var _final = stripped.replace(/\[BR\]/gi, '</br>'); // replace placeholders with <br>
682
- 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
+ }
683
779
  }
684
780
 
685
781
  // @ts-check
@@ -938,8 +1034,7 @@ var TextInput = function TextInput(_ref, ref) {
938
1034
  if (event.key === "Enter" && (event.shiftKey === true || event.ctrlKey === true) && props.shouldReturn) {
939
1035
  event.preventDefault();
940
1036
  if (textInputRef.current) {
941
- textInputRef.current.innerHTML = "".concat(textInputRef.current.innerHTML, "</br></br>");
942
- moveCaretToEnd(textInputRef);
1037
+ addLineBreak();
943
1038
  return;
944
1039
  }
945
1040
  }
@@ -4515,27 +4610,6 @@ function InputEmoji(props, ref) {
4515
4610
  }
4516
4611
  }
4517
4612
 
4518
- /**
4519
- * Handle copy of current selected text
4520
- * @param {React.ClipboardEvent} event
4521
- */
4522
- function handleCopy(event) {
4523
- var selection = window.getSelection();
4524
- if (selection !== null) {
4525
- var selectedText = '';
4526
- if (selection.anchorNode && selection.anchorNode.nodeType === Node.ELEMENT_NODE) {
4527
- // @ts-ignore
4528
- selectedText = selection.anchorNode.innerHTML;
4529
- } else if (selection.anchorNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {
4530
- var _selection$anchorNode;
4531
- selectedText = (_selection$anchorNode = selection.anchorNode.textContent) !== null && _selection$anchorNode !== void 0 ? _selection$anchorNode : '';
4532
- }
4533
- var text = replaceAllTextEmojiToString(selectedText);
4534
- event.clipboardData.setData("text", text);
4535
- event.preventDefault();
4536
- }
4537
- }
4538
-
4539
4613
  /**
4540
4614
  * Handle past on input
4541
4615
  * @param {React.ClipboardEvent} event