react-input-emoji 5.8.1 → 5.9.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.es.js CHANGED
@@ -619,9 +619,22 @@ function handleCopy(event) {
619
619
  event.preventDefault();
620
620
  }
621
621
 
622
+ /** @type {Range|undefined} */
623
+ /** @type {Range|undefined} */
624
+ var currentRangeCached;
625
+
622
626
  /**
623
- *
624
- * @param {string} html
627
+ * Caches the current text selection range
628
+ */
629
+ function cacheCurrentRange() {
630
+ var selection = window.getSelection();
631
+ if (!selection.rangeCount || (selection === null || selection === void 0 ? void 0 : selection.anchorNode['className']) !== 'react-input-emoji--input' && selection.anchorNode.parentNode['className'] !== 'react-input-emoji--input') return;
632
+ var range = selection.getRangeAt(0);
633
+ currentRangeCached = range.cloneRange();
634
+ }
635
+
636
+ /**
637
+ * @param {string} html - HTML string to be pasted at the caret position
625
638
  */
626
639
  function handlePasteHtmlAtCaret(html) {
627
640
  var sel;
@@ -631,7 +644,8 @@ function handlePasteHtmlAtCaret(html) {
631
644
  sel = window.getSelection();
632
645
  if (sel === null) return;
633
646
  if (sel.getRangeAt && sel.rangeCount) {
634
- range = sel.getRangeAt(0);
647
+ var _currentRangeCached;
648
+ range = (_currentRangeCached = currentRangeCached) !== null && _currentRangeCached !== void 0 ? _currentRangeCached : sel.getRangeAt(0);
635
649
  range.deleteContents();
636
650
 
637
651
  // Range.createContextualFragment() would be useful here but is
@@ -649,6 +663,7 @@ function handlePasteHtmlAtCaret(html) {
649
663
  // Preserve the selection
650
664
  if (lastNode) {
651
665
  range = range.cloneRange();
666
+ currentRangeCached = range;
652
667
  range.setStartAfter(lastNode);
653
668
  range.collapse(true);
654
669
  sel.removeAllRanges();
@@ -4055,6 +4070,11 @@ var EmojiPickerWrapper = function EmojiPickerWrapper(props) {
4055
4070
  _useState6 = _slicedToArray(_useState5, 2),
4056
4071
  emojiPickerPosition = _useState6[0],
4057
4072
  setEmojiPickerPosition = _useState6[1];
4073
+ useEffect(function () {
4074
+ if (showPicker) {
4075
+ cacheCurrentRange();
4076
+ }
4077
+ }, [showPicker]);
4058
4078
  useEffect(function () {
4059
4079
  addSanitizeFn(replaceAllTextEmojiToString);
4060
4080
  }, [addSanitizeFn]);