@chayns-components/emoji-input 5.0.0-beta.90 → 5.0.0-beta.92
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/lib/components/emoji-input/EmojiInput.js +34 -42
- package/lib/components/emoji-input/EmojiInput.js.map +1 -1
- package/lib/components/emoji-input/EmojiInput.styles.js +1 -0
- package/lib/components/emoji-input/EmojiInput.styles.js.map +1 -1
- package/lib/utils/insert.d.ts +19 -0
- package/lib/utils/insert.js +57 -0
- package/lib/utils/insert.js.map +1 -0
- package/package.json +2 -2
|
@@ -8,6 +8,7 @@ var _react = _interopRequireWildcard(require("react"));
|
|
|
8
8
|
var _emoji = require("../../utils/emoji");
|
|
9
9
|
var _environment = require("../../utils/environment");
|
|
10
10
|
var _font = require("../../utils/font");
|
|
11
|
+
var _insert = require("../../utils/insert");
|
|
11
12
|
var _selection = require("../../utils/selection");
|
|
12
13
|
var _EmojiPickerPopup = _interopRequireDefault(require("../emoji-picker-popup/EmojiPickerPopup"));
|
|
13
14
|
var _EmojiInput = require("./EmojiInput.styles");
|
|
@@ -66,55 +67,45 @@ const EmojiInput = _ref => {
|
|
|
66
67
|
}, [handleUpdateText, onInput]);
|
|
67
68
|
|
|
68
69
|
/**
|
|
69
|
-
* This function
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
70
|
+
* This function prevents formatting from being adopted when texts are inserted. To do this, the
|
|
71
|
+
* plain text is read from the event after the default behavior has been prevented. The plain
|
|
72
|
+
* text is then inserted at the correct position in the input field using the
|
|
73
|
+
* 'insertTextAtCursorPosition' function.
|
|
74
|
+
*/
|
|
75
|
+
const handlePaste = (0, _react.useCallback)(event => {
|
|
76
|
+
if (editorRef.current) {
|
|
77
|
+
event.preventDefault();
|
|
78
|
+
const text = event.clipboardData.getData('text/plain');
|
|
79
|
+
(0, _insert.insertTextAtCursorPosition)({
|
|
80
|
+
editorElement: editorRef.current,
|
|
81
|
+
text
|
|
82
|
+
});
|
|
83
|
+
const newEvent = new Event('input', {
|
|
84
|
+
bubbles: true
|
|
85
|
+
});
|
|
86
|
+
editorRef.current.dispatchEvent(newEvent);
|
|
87
|
+
}
|
|
88
|
+
}, []);
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* This function uses the 'insertTextAtCursorPosition' function to insert the emoji at the
|
|
92
|
+
* correct position in the editor element.
|
|
76
93
|
*
|
|
77
94
|
* At the end an 'input' event is dispatched, so that the function 'handleInput' is triggered,
|
|
78
95
|
* which in turn executes the 'onInput' function from the props. So this serves to ensure that
|
|
79
96
|
* the event is also passed through to the top when inserting via the popup.
|
|
80
97
|
*/
|
|
81
98
|
const handlePopupSelect = (0, _react.useCallback)(emoji => {
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const rangeDistance = endOffset - startOffset;
|
|
92
|
-
let offset = endOffset + emoji.length - rangeDistance;
|
|
93
|
-
let {
|
|
94
|
-
anchorNode
|
|
95
|
-
} = selection;
|
|
96
|
-
if (anchorNode.nodeValue) {
|
|
97
|
-
anchorNode.nodeValue = anchorNode.nodeValue.substring(0, startOffset) + emoji + anchorNode.nodeValue.substring(endOffset);
|
|
98
|
-
} else if (anchorNode === editorRef.current) {
|
|
99
|
-
const newTextNode = document.createTextNode(emoji);
|
|
100
|
-
editorRef.current.appendChild(newTextNode);
|
|
101
|
-
anchorNode = newTextNode;
|
|
102
|
-
}
|
|
103
|
-
const newRange = document.createRange();
|
|
104
|
-
if (anchorNode.nodeValue) {
|
|
105
|
-
offset = Math.min(offset, anchorNode.nodeValue.length);
|
|
106
|
-
}
|
|
107
|
-
newRange.setStart(anchorNode, offset);
|
|
108
|
-
newRange.setEnd(anchorNode, offset);
|
|
109
|
-
selection.removeAllRanges();
|
|
110
|
-
selection.addRange(newRange);
|
|
111
|
-
} else {
|
|
112
|
-
editorRef.current.innerText += emoji;
|
|
99
|
+
if (editorRef.current) {
|
|
100
|
+
(0, _insert.insertTextAtCursorPosition)({
|
|
101
|
+
editorElement: editorRef.current,
|
|
102
|
+
text: emoji
|
|
103
|
+
});
|
|
104
|
+
const event = new Event('input', {
|
|
105
|
+
bubbles: true
|
|
106
|
+
});
|
|
107
|
+
editorRef.current.dispatchEvent(event);
|
|
113
108
|
}
|
|
114
|
-
const event = new Event('input', {
|
|
115
|
-
bubbles: true
|
|
116
|
-
});
|
|
117
|
-
editorRef.current.dispatchEvent(event);
|
|
118
109
|
}, []);
|
|
119
110
|
|
|
120
111
|
/**
|
|
@@ -151,6 +142,7 @@ const EmojiInput = _ref => {
|
|
|
151
142
|
isMobile: isMobile,
|
|
152
143
|
onInput: handleInput,
|
|
153
144
|
onKeyDown: onKeyDown,
|
|
145
|
+
onPaste: handlePaste,
|
|
154
146
|
placeholder: placeholder,
|
|
155
147
|
ref: editorRef,
|
|
156
148
|
rootFontFamily: rootFontFamily
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EmojiInput.js","names":["_react","_interopRequireWildcard","require","_emoji","_environment","_font","_selection","_EmojiPickerPopup","_interopRequireDefault","_EmojiInput","obj","__esModule","default","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","EmojiInput","_ref","accessToken","isDisabled","onInput","onKeyDown","onPopupVisibilityChange","personId","placeholder","popupAlignment","rightElement","value","isMobile","useState","getIsMobile","rootFontFamily","getRootFontFamily","editorRef","useRef","handleUpdateText","useCallback","text","current","newInnerText","convertAsciiToUnicode","innerText","saveSelection","restoreSelection","handleInput","event","handlePopupSelect","emoji","selection","window","getSelection","anchorNode","contains","endOffset","startOffset","getRangeAt","rangeDistance","offset","length","nodeValue","substring","newTextNode","document","createTextNode","appendChild","newRange","createRange","Math","min","setStart","setEnd","removeAllRanges","addRange","Event","bubbles","dispatchEvent","handlePreventLoseFocus","_element$parentElemen","element","target","classList","parentElement","preventDefault","stopPropagation","useEffect","body","addEventListener","removeEventListener","createElement","StyledEmojiInput","StyledEmojiInputContent","isRightElementGiven","StyledEmojiInputEditor","contentEditable","ref","alignment","onSelect","StyledEmojiInputRightWrapper","displayName","_default","exports"],"sources":["../../../src/components/emoji-input/EmojiInput.tsx"],"sourcesContent":["import React, {\n ChangeEvent,\n ChangeEventHandler,\n FC,\n KeyboardEventHandler,\n ReactNode,\n useCallback,\n useEffect,\n useRef,\n useState,\n} from 'react';\nimport type { PopupAlignment } from '../../constants/alignment';\nimport { convertAsciiToUnicode } from '../../utils/emoji';\nimport { getIsMobile } from '../../utils/environment';\nimport { getRootFontFamily } from '../../utils/font';\nimport { restoreSelection, saveSelection } from '../../utils/selection';\nimport EmojiPickerPopup from '../emoji-picker-popup/EmojiPickerPopup';\nimport {\n StyledEmojiInput,\n StyledEmojiInputContent,\n StyledEmojiInputEditor,\n StyledEmojiInputRightWrapper,\n} from './EmojiInput.styles';\n\nexport type EmojiInputProps = {\n /**\n * Access token of the logged-in user. Is needed to load and save the history of the emojis.\n */\n accessToken?: string;\n /**\n * Disables the input so that it cannot be changed anymore\n */\n isDisabled?: boolean;\n /**\n * Function that is executed when the text of the input changes\n */\n onInput?: ChangeEventHandler<HTMLDivElement>;\n /**\n * Function that is executed when a key is pressed down.\n */\n onKeyDown?: KeyboardEventHandler<HTMLDivElement>;\n /**\n * Function that is executed when the visibility of the popup changes.\n * @param {boolean} isVisible - Whether the popup is visible or not\n */\n onPopupVisibilityChange?: (isVisible: boolean) => void;\n /**\n * Person id of the logged-in user. Is needed to load and save the history of the emojis.\n */\n personId?: string;\n /**\n * Placeholder for the input field\n */\n placeholder?: string;\n /**\n * Sets the alignment of the popup to a fixed value. If this value is not set, the component\n * calculates the best position on its own. Use the imported 'PopupAlignment' enum to set this\n * value.\n */\n popupAlignment?: PopupAlignment;\n /**\n * Element that is rendered inside the EmojiInput on the right side.\n */\n rightElement?: ReactNode;\n /**\n * Value of the input field\n */\n value: string;\n};\n\nconst EmojiInput: FC<EmojiInputProps> = ({\n accessToken,\n isDisabled,\n onInput,\n onKeyDown,\n onPopupVisibilityChange,\n personId,\n placeholder,\n popupAlignment,\n rightElement,\n value,\n}) => {\n const [isMobile] = useState(getIsMobile());\n const [rootFontFamily] = useState(getRootFontFamily());\n\n const editorRef = useRef<HTMLDivElement>(null);\n\n /**\n * This function updates the content of the 'contentEditable' element if the new text is\n * different from the previous content. So this is only true if, for example, a text like \":-)\"\n * has been replaced to the corresponding emoji.\n *\n * When updating the HTML, the current cursor position is saved before replacing the content, so\n * that it can be set again afterward.\n */\n const handleUpdateText = useCallback((text: string) => {\n if (!editorRef.current) {\n return;\n }\n\n const newInnerText = convertAsciiToUnicode(text);\n\n if (newInnerText !== editorRef.current.innerText) {\n saveSelection(editorRef.current);\n\n editorRef.current.innerText = newInnerText;\n\n restoreSelection(editorRef.current);\n }\n }, []);\n\n /**\n * This function handles the 'input' events of the 'contentEditable' element and also passes the\n * respective event up accordingly if the 'onInput' property is a function.\n */\n const handleInput = useCallback(\n (event: ChangeEvent<HTMLDivElement>) => {\n if (!editorRef.current) {\n return;\n }\n\n handleUpdateText(editorRef.current.innerText);\n\n if (typeof onInput === 'function') {\n onInput(event);\n }\n },\n [handleUpdateText, onInput]\n );\n\n /**\n * This function processes the selection of an emoji via the popup. If the 'contentEditable'\n * element has the focus, the new emoji is inserted at the cursor position. If not, the emoji\n * will be appended to the back of the input field content.\n *\n * In addition, this function also sets the cursor to the correct position when the input field\n * has the focus. For this purpose, the current position of the cursor or a selection is read to\n * calculate the cursor position after inserting the emoji.\n *\n * At the end an 'input' event is dispatched, so that the function 'handleInput' is triggered,\n * which in turn executes the 'onInput' function from the props. So this serves to ensure that\n * the event is also passed through to the top when inserting via the popup.\n */\n const handlePopupSelect = useCallback((emoji: string) => {\n if (!editorRef.current) {\n return;\n }\n\n const selection = window.getSelection();\n\n if (selection?.anchorNode && editorRef.current.contains(selection.anchorNode)) {\n const { endOffset, startOffset } = selection.getRangeAt(0);\n\n const rangeDistance = endOffset - startOffset;\n\n let offset = endOffset + emoji.length - rangeDistance;\n\n let { anchorNode } = selection;\n\n if (anchorNode.nodeValue) {\n anchorNode.nodeValue =\n anchorNode.nodeValue.substring(0, startOffset) +\n emoji +\n anchorNode.nodeValue.substring(endOffset);\n } else if (anchorNode === editorRef.current) {\n const newTextNode = document.createTextNode(emoji);\n\n editorRef.current.appendChild(newTextNode);\n\n anchorNode = newTextNode;\n }\n\n const newRange = document.createRange();\n\n if (anchorNode.nodeValue) {\n offset = Math.min(offset, anchorNode.nodeValue.length);\n }\n\n newRange.setStart(anchorNode, offset);\n newRange.setEnd(anchorNode, offset);\n\n selection.removeAllRanges();\n selection.addRange(newRange);\n } else {\n editorRef.current.innerText += emoji;\n }\n\n const event = new Event('input', { bubbles: true });\n\n editorRef.current.dispatchEvent(event);\n }, []);\n\n /**\n * This function ensures that the input field does not lose focus when the popup is opened or an\n * emoji is selected in it. For this purpose the corresponding elements get the class\n * 'prevent-lose-focus'.\n *\n * The class can also be set to any other elements that should also not cause the input field to\n * lose focus.\n */\n const handlePreventLoseFocus = useCallback((event: MouseEvent) => {\n const element = event.target as Element;\n\n if (\n element.classList.contains('prevent-lose-focus') ||\n element.parentElement?.classList.contains('prevent-lose-focus')\n ) {\n event.preventDefault();\n event.stopPropagation();\n }\n }, []);\n\n useEffect(() => {\n handleUpdateText(value);\n }, [handleUpdateText, value]);\n\n useEffect(() => {\n document.body.addEventListener('mousedown', handlePreventLoseFocus);\n\n return () => {\n document.body.removeEventListener('mousedown', handlePreventLoseFocus);\n };\n }, [handlePreventLoseFocus]);\n\n return (\n <StyledEmojiInput isDisabled={isDisabled}>\n <StyledEmojiInputContent isRightElementGiven={!!rightElement}>\n <StyledEmojiInputEditor\n contentEditable={!isDisabled}\n isMobile={isMobile}\n onInput={handleInput}\n onKeyDown={onKeyDown}\n placeholder={placeholder}\n ref={editorRef}\n rootFontFamily={rootFontFamily}\n />\n {!isMobile && (\n <EmojiPickerPopup\n accessToken={accessToken}\n alignment={popupAlignment}\n onSelect={handlePopupSelect}\n onPopupVisibilityChange={onPopupVisibilityChange}\n personId={personId}\n />\n )}\n </StyledEmojiInputContent>\n {rightElement && (\n <StyledEmojiInputRightWrapper>{rightElement}</StyledEmojiInputRightWrapper>\n )}\n </StyledEmojiInput>\n );\n};\n\nEmojiInput.displayName = 'EmojiInput';\n\nexport default EmojiInput;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAYA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;AACA,IAAAI,UAAA,GAAAJ,OAAA;AACA,IAAAK,iBAAA,GAAAC,sBAAA,CAAAN,OAAA;AACA,IAAAO,WAAA,GAAAP,OAAA;AAK6B,SAAAM,uBAAAE,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAAA,SAAAG,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAb,wBAAAS,GAAA,EAAAI,WAAA,SAAAA,WAAA,IAAAJ,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAQ,KAAA,GAAAL,wBAAA,CAAAC,WAAA,OAAAI,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAT,GAAA,YAAAQ,KAAA,CAAAE,GAAA,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ACH,QAAQ,iBACN5C,MAAA,CAAAY,OAAA,CAAA4F,aAAA,CAACjG,iBAAA,CAAAK,OAAgB;IACbsB,WAAW,EAAEA,WAAY;IACzB6E,SAAS,EAAEtE,cAAe;IAC1BuE,QAAQ,EAAElD,iBAAkB;IAC5BxB,uBAAuB,EAAEA,uBAAwB;IACjDC,QAAQ,EAAEA;EAAS,EAE1B,CACqB,EACzBG,YAAY,iBACT1C,MAAA,CAAAY,OAAA,CAAA4F,aAAA,CAAC/F,WAAA,CAAAwG,4BAA4B,QAAEvE,YAAY,CAC9C,CACc;AAE3B,CAAC;AAEDV,UAAU,CAACkF,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvBnF,UAAU;AAAAoF,OAAA,CAAAxG,OAAA,GAAAuG,QAAA"}
|
|
1
|
+
{"version":3,"file":"EmojiInput.js","names":["_react","_interopRequireWildcard","require","_emoji","_environment","_font","_insert","_selection","_EmojiPickerPopup","_interopRequireDefault","_EmojiInput","obj","__esModule","default","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","EmojiInput","_ref","accessToken","isDisabled","onInput","onKeyDown","onPopupVisibilityChange","personId","placeholder","popupAlignment","rightElement","value","isMobile","useState","getIsMobile","rootFontFamily","getRootFontFamily","editorRef","useRef","handleUpdateText","useCallback","text","current","newInnerText","convertAsciiToUnicode","innerText","saveSelection","restoreSelection","handleInput","event","handlePaste","preventDefault","clipboardData","getData","insertTextAtCursorPosition","editorElement","newEvent","Event","bubbles","dispatchEvent","handlePopupSelect","emoji","handlePreventLoseFocus","_element$parentElemen","element","target","classList","contains","parentElement","stopPropagation","useEffect","document","body","addEventListener","removeEventListener","createElement","StyledEmojiInput","StyledEmojiInputContent","isRightElementGiven","StyledEmojiInputEditor","contentEditable","onPaste","ref","alignment","onSelect","StyledEmojiInputRightWrapper","displayName","_default","exports"],"sources":["../../../src/components/emoji-input/EmojiInput.tsx"],"sourcesContent":["import React, {\n ChangeEvent,\n ChangeEventHandler,\n ClipboardEvent,\n FC,\n KeyboardEventHandler,\n ReactNode,\n useCallback,\n useEffect,\n useRef,\n useState,\n} from 'react';\nimport type { PopupAlignment } from '../../constants/alignment';\nimport { convertAsciiToUnicode } from '../../utils/emoji';\nimport { getIsMobile } from '../../utils/environment';\nimport { getRootFontFamily } from '../../utils/font';\nimport { insertTextAtCursorPosition } from '../../utils/insert';\nimport { restoreSelection, saveSelection } from '../../utils/selection';\nimport EmojiPickerPopup from '../emoji-picker-popup/EmojiPickerPopup';\nimport {\n StyledEmojiInput,\n StyledEmojiInputContent,\n StyledEmojiInputEditor,\n StyledEmojiInputRightWrapper,\n} from './EmojiInput.styles';\n\nexport type EmojiInputProps = {\n /**\n * Access token of the logged-in user. Is needed to load and save the history of the emojis.\n */\n accessToken?: string;\n /**\n * Disables the input so that it cannot be changed anymore\n */\n isDisabled?: boolean;\n /**\n * Function that is executed when the text of the input changes\n */\n onInput?: ChangeEventHandler<HTMLDivElement>;\n /**\n * Function that is executed when a key is pressed down.\n */\n onKeyDown?: KeyboardEventHandler<HTMLDivElement>;\n /**\n * Function that is executed when the visibility of the popup changes.\n * @param {boolean} isVisible - Whether the popup is visible or not\n */\n onPopupVisibilityChange?: (isVisible: boolean) => void;\n /**\n * Person id of the logged-in user. Is needed to load and save the history of the emojis.\n */\n personId?: string;\n /**\n * Placeholder for the input field\n */\n placeholder?: string;\n /**\n * Sets the alignment of the popup to a fixed value. If this value is not set, the component\n * calculates the best position on its own. Use the imported 'PopupAlignment' enum to set this\n * value.\n */\n popupAlignment?: PopupAlignment;\n /**\n * Element that is rendered inside the EmojiInput on the right side.\n */\n rightElement?: ReactNode;\n /**\n * Value of the input field\n */\n value: string;\n};\n\nconst EmojiInput: FC<EmojiInputProps> = ({\n accessToken,\n isDisabled,\n onInput,\n onKeyDown,\n onPopupVisibilityChange,\n personId,\n placeholder,\n popupAlignment,\n rightElement,\n value,\n}) => {\n const [isMobile] = useState(getIsMobile());\n const [rootFontFamily] = useState(getRootFontFamily());\n\n const editorRef = useRef<HTMLDivElement>(null);\n\n /**\n * This function updates the content of the 'contentEditable' element if the new text is\n * different from the previous content. So this is only true if, for example, a text like \":-)\"\n * has been replaced to the corresponding emoji.\n *\n * When updating the HTML, the current cursor position is saved before replacing the content, so\n * that it can be set again afterward.\n */\n const handleUpdateText = useCallback((text: string) => {\n if (!editorRef.current) {\n return;\n }\n\n const newInnerText = convertAsciiToUnicode(text);\n\n if (newInnerText !== editorRef.current.innerText) {\n saveSelection(editorRef.current);\n\n editorRef.current.innerText = newInnerText;\n\n restoreSelection(editorRef.current);\n }\n }, []);\n\n /**\n * This function handles the 'input' events of the 'contentEditable' element and also passes the\n * respective event up accordingly if the 'onInput' property is a function.\n */\n const handleInput = useCallback(\n (event: ChangeEvent<HTMLDivElement>) => {\n if (!editorRef.current) {\n return;\n }\n\n handleUpdateText(editorRef.current.innerText);\n\n if (typeof onInput === 'function') {\n onInput(event);\n }\n },\n [handleUpdateText, onInput]\n );\n\n /**\n * This function prevents formatting from being adopted when texts are inserted. To do this, the\n * plain text is read from the event after the default behavior has been prevented. The plain\n * text is then inserted at the correct position in the input field using the\n * 'insertTextAtCursorPosition' function.\n */\n const handlePaste = useCallback((event: ClipboardEvent<HTMLDivElement>) => {\n if (editorRef.current) {\n event.preventDefault();\n\n const text = event.clipboardData.getData('text/plain');\n\n insertTextAtCursorPosition({ editorElement: editorRef.current, text });\n\n const newEvent = new Event('input', { bubbles: true });\n\n editorRef.current.dispatchEvent(newEvent);\n }\n }, []);\n\n /**\n * This function uses the 'insertTextAtCursorPosition' function to insert the emoji at the\n * correct position in the editor element.\n *\n * At the end an 'input' event is dispatched, so that the function 'handleInput' is triggered,\n * which in turn executes the 'onInput' function from the props. So this serves to ensure that\n * the event is also passed through to the top when inserting via the popup.\n */\n const handlePopupSelect = useCallback((emoji: string) => {\n if (editorRef.current) {\n insertTextAtCursorPosition({ editorElement: editorRef.current, text: emoji });\n\n const event = new Event('input', { bubbles: true });\n\n editorRef.current.dispatchEvent(event);\n }\n }, []);\n\n /**\n * This function ensures that the input field does not lose focus when the popup is opened or an\n * emoji is selected in it. For this purpose the corresponding elements get the class\n * 'prevent-lose-focus'.\n *\n * The class can also be set to any other elements that should also not cause the input field to\n * lose focus.\n */\n const handlePreventLoseFocus = useCallback((event: MouseEvent) => {\n const element = event.target as Element;\n\n if (\n element.classList.contains('prevent-lose-focus') ||\n element.parentElement?.classList.contains('prevent-lose-focus')\n ) {\n event.preventDefault();\n event.stopPropagation();\n }\n }, []);\n\n useEffect(() => {\n handleUpdateText(value);\n }, [handleUpdateText, value]);\n\n useEffect(() => {\n document.body.addEventListener('mousedown', handlePreventLoseFocus);\n\n return () => {\n document.body.removeEventListener('mousedown', handlePreventLoseFocus);\n };\n }, [handlePreventLoseFocus]);\n\n return (\n <StyledEmojiInput isDisabled={isDisabled}>\n <StyledEmojiInputContent isRightElementGiven={!!rightElement}>\n <StyledEmojiInputEditor\n contentEditable={!isDisabled}\n isMobile={isMobile}\n onInput={handleInput}\n onKeyDown={onKeyDown}\n onPaste={handlePaste}\n placeholder={placeholder}\n ref={editorRef}\n rootFontFamily={rootFontFamily}\n />\n {!isMobile && (\n <EmojiPickerPopup\n accessToken={accessToken}\n alignment={popupAlignment}\n onSelect={handlePopupSelect}\n onPopupVisibilityChange={onPopupVisibilityChange}\n personId={personId}\n />\n )}\n </StyledEmojiInputContent>\n {rightElement && (\n <StyledEmojiInputRightWrapper>{rightElement}</StyledEmojiInputRightWrapper>\n )}\n </StyledEmojiInput>\n );\n};\n\nEmojiInput.displayName = 'EmojiInput';\n\nexport default EmojiInput;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAaA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OA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EACjC,EACD,CAACH,QAAQ,iBACN7C,MAAA,CAAAa,OAAA,CAAA2E,aAAA,CAAChF,iBAAA,CAAAK,OAAgB;IACbsB,WAAW,EAAEA,WAAY;IACzB6D,SAAS,EAAEtD,cAAe;IAC1BuD,QAAQ,EAAExB,iBAAkB;IAC5BlC,uBAAuB,EAAEA,uBAAwB;IACjDC,QAAQ,EAAEA;EAAS,EAE1B,CACqB,EACzBG,YAAY,iBACT3C,MAAA,CAAAa,OAAA,CAAA2E,aAAA,CAAC9E,WAAA,CAAAwF,4BAA4B,QAAEvD,YAAY,CAC9C,CACc;AAE3B,CAAC;AAEDV,UAAU,CAACkE,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvBnE,UAAU;AAAAoE,OAAA,CAAAxF,OAAA,GAAAuF,QAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EmojiInput.styles.js","names":["_styledComponents","_interopRequireWildcard","require","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","StyledEmojiInput","styled","div","_ref","theme","_ref2","isDisabled","_ref3","exports","StyledEmojiInputContent","_ref4","isRightElementGiven","css","StyledEmojiInputEditor","_ref5","text","_ref6","isMobile","rootFontFamily","_ref7","placeholder","_ref8","_ref9","_ref10","StyledEmojiInputRightWrapper"],"sources":["../../../src/components/emoji-input/EmojiInput.styles.ts"],"sourcesContent":["import type { WithTheme } from '@chayns-components/core';\nimport styled, { css } from 'styled-components';\nimport type { EmojiInputProps } from './EmojiInput';\n\ntype StyledEmojiInputProps = WithTheme<Pick<EmojiInputProps, 'isDisabled'>>;\n\nexport const StyledEmojiInput = styled.div<StyledEmojiInputProps>`\n align-items: center;\n background-color: ${({ theme }: StyledEmojiInputProps) => theme['100']};\n border-radius: 3px;\n display: flex;\n min-height: 42px;\n opacity: ${({ isDisabled }) => (isDisabled ? 0.5 : 1)};\n pointer-events: ${({ isDisabled }) => (isDisabled ? 'none' : 'initial')};\n position: relative;\n transition: opacity 0.3s ease;\n`;\n\ntype StyledEmojiInputContentProps = {\n isRightElementGiven: boolean;\n};\n\nexport const StyledEmojiInputContent = styled.div<StyledEmojiInputContentProps>`\n align-items: end;\n border: 1px solid rgba(160, 160, 160, 0.3);\n border-radius: 3px;\n display: flex;\n flex: 1 1 auto;\n gap: 10px;\n padding: 8px 10px;\n\n ${({ isRightElementGiven }) =>\n isRightElementGiven &&\n css`\n border-top-right-radius: 0;\n border-bottom-right-radius: 0;\n border-right-width: 0;\n `}\n`;\n\ntype StyledEmojiInputEditorProps = WithTheme<Pick<EmojiInputProps, 'placeholder'>> & {\n isMobile: boolean;\n rootFontFamily: string;\n};\n\nexport const StyledEmojiInputEditor = styled.div<StyledEmojiInputEditorProps>`\n color: ${({ theme }: StyledEmojiInputEditorProps) => theme.text};\n flex: 1 1 auto;\n max-height: 210px;\n overflow-y: scroll;\n word-break: break-word;\n\n ${({ isMobile, rootFontFamily }) => {\n if (isMobile) {\n return css`\n font-family: ${rootFontFamily};\n `;\n }\n\n return css`\n font-family: ${rootFontFamily}, 'Noto Color Emoji';\n `;\n }}\n\n &:empty:not(:focus):before {\n content: '${({ placeholder }) => placeholder}';\n color: ${({ theme }: StyledEmojiInputEditorProps) => theme['006']};\n }\n\n // Styles for custom scrollbar\n ::-webkit-scrollbar {\n width: 5px;\n }\n\n ::-webkit-scrollbar-track {\n background-color: transparent;\n }\n\n ::-webkit-scrollbar-button {\n background-color: transparent;\n height: 5px;\n }\n\n ::-webkit-scrollbar-thumb {\n background-color: rgba(\n ${({ theme }: StyledEmojiInputEditorProps) => theme['text-rgb']},\n 0.15\n );\n border-radius: 20px;\n }\n\n // Scrollbar styles for Firefox. The above styles are not supported in Firefox, these styles are\n // only supported in Firefox:\n * {\n scrollbar-color: rgba(\n ${({ theme }: StyledEmojiInputEditorProps) => theme['text-rgb']},\n 0.15\n )\n transparent;\n scrollbar-width: thin;\n }\n`;\n\nexport const StyledEmojiInputRightWrapper = styled.div`\n align-self: stretch;\n border-bottom-right-radius: 3px;\n border-top-right-radius: 3px;\n flex: 0 0 auto;\n overflow: hidden;\n`;\n"],"mappings":";;;;;;AACA,IAAAA,iBAAA,GAAAC,uBAAA,CAAAC,OAAA;AAAgD,SAAAC,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAH,wBAAAO,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAG,KAAA,GAAAR,wBAAA,CAAAC,WAAA,OAAAO,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAJ,GAAA,YAAAG,KAAA,CAAAE,GAAA,CAAAL,GAAA,SAAAM,MAAA,WAAAC,qBAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,GAAA,IAAAX,GAAA,QAAAW,GAAA,kBAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAd,GAAA,EAAAW,GAAA,SAAAI,IAAA,GAAAR,qBAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAV,GAAA,EAAAW,GAAA,cAAAI,IAAA,KAAAA,IAAA,CAAAV,GAAA,IAAAU,IAAA,CAAAC,GAAA,KAAAR,MAAA,CAAAC,cAAA,CAAAH,MAAA,EAAAK,GAAA,EAAAI,IAAA,YAAAT,MAAA,CAAAK,GAAA,IAAAX,GAAA,CAAAW,GAAA,SAAAL,MAAA,CAAAJ,OAAA,GAA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C,aAAa;EACL;EAEA,OAAO,IAAAN,qBAAG,CAAC;AACnB,2BAA2BM,cAAe;AAC1C,SAAS;AACL,CAAE;AACN;AACA;AACA,oBAAoBC,KAAA;EAAA,IAAC;IAAEC;EAAY,CAAC,GAAAD,KAAA;EAAA,OAAKC,WAAW;AAAA,CAAC;AACrD,iBAAiBC,KAAA;EAAA,IAAC;IAAEjB;EAAmC,CAAC,GAAAiB,KAAA;EAAA,OAAKjB,KAAK,CAAC,KAAK,CAAC;AAAA,CAAC;AAC1E;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,cAAckB,KAAA;EAAA,IAAC;IAAElB;EAAmC,CAAC,GAAAkB,KAAA;EAAA,OAAKlB,KAAK,CAAC,UAAU,CAAC;AAAA,CAAC;AAC5E;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,kBAAkBmB,MAAA;EAAA,IAAC;IAAEnB;EAAmC,CAAC,GAAAmB,MAAA;EAAA,OAAKnB,KAAK,CAAC,UAAU,CAAC;AAAA,CAAC;AAChF;AACA;AACA;AACA;AACA;AACA,CAAC;AAACI,OAAA,CAAAK,sBAAA,GAAAA,sBAAA;AAEK,MAAMW,4BAA4B,GAAGvB,yBAAM,CAACC,GAAI;AACvD;AACA;AACA;AACA;AACA;AACA,CAAC;AAACM,OAAA,CAAAgB,4BAAA,GAAAA,4BAAA"}
|
|
1
|
+
{"version":3,"file":"EmojiInput.styles.js","names":["_styledComponents","_interopRequireWildcard","require","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","StyledEmojiInput","styled","div","_ref","theme","_ref2","isDisabled","_ref3","exports","StyledEmojiInputContent","_ref4","isRightElementGiven","css","StyledEmojiInputEditor","_ref5","text","_ref6","isMobile","rootFontFamily","_ref7","placeholder","_ref8","_ref9","_ref10","StyledEmojiInputRightWrapper"],"sources":["../../../src/components/emoji-input/EmojiInput.styles.ts"],"sourcesContent":["import type { WithTheme } from '@chayns-components/core';\nimport styled, { css } from 'styled-components';\nimport type { EmojiInputProps } from './EmojiInput';\n\ntype StyledEmojiInputProps = WithTheme<Pick<EmojiInputProps, 'isDisabled'>>;\n\nexport const StyledEmojiInput = styled.div<StyledEmojiInputProps>`\n align-items: center;\n background-color: ${({ theme }: StyledEmojiInputProps) => theme['100']};\n border-radius: 3px;\n display: flex;\n min-height: 42px;\n opacity: ${({ isDisabled }) => (isDisabled ? 0.5 : 1)};\n pointer-events: ${({ isDisabled }) => (isDisabled ? 'none' : 'initial')};\n position: relative;\n transition: opacity 0.3s ease;\n`;\n\ntype StyledEmojiInputContentProps = {\n isRightElementGiven: boolean;\n};\n\nexport const StyledEmojiInputContent = styled.div<StyledEmojiInputContentProps>`\n align-items: end;\n border: 1px solid rgba(160, 160, 160, 0.3);\n border-radius: 3px;\n display: flex;\n flex: 1 1 auto;\n gap: 10px;\n padding: 8px 10px;\n\n ${({ isRightElementGiven }) =>\n isRightElementGiven &&\n css`\n border-top-right-radius: 0;\n border-bottom-right-radius: 0;\n border-right-width: 0;\n `}\n`;\n\ntype StyledEmojiInputEditorProps = WithTheme<Pick<EmojiInputProps, 'placeholder'>> & {\n isMobile: boolean;\n rootFontFamily: string;\n};\n\nexport const StyledEmojiInputEditor = styled.div<StyledEmojiInputEditorProps>`\n color: ${({ theme }: StyledEmojiInputEditorProps) => theme.text};\n flex: 1 1 auto;\n max-height: 210px;\n overflow-y: scroll;\n word-break: break-word;\n\n ${({ isMobile, rootFontFamily }) => {\n if (isMobile) {\n return css`\n font-family: ${rootFontFamily};\n `;\n }\n\n return css`\n font-family: ${rootFontFamily}, 'Noto Color Emoji';\n `;\n }}\n\n &:empty:not(:focus):before {\n content: '${({ placeholder }) => placeholder}';\n color: ${({ theme }: StyledEmojiInputEditorProps) => theme['006']};\n pointer-events: none;\n }\n\n // Styles for custom scrollbar\n ::-webkit-scrollbar {\n width: 5px;\n }\n\n ::-webkit-scrollbar-track {\n background-color: transparent;\n }\n\n ::-webkit-scrollbar-button {\n background-color: transparent;\n height: 5px;\n }\n\n ::-webkit-scrollbar-thumb {\n background-color: rgba(\n ${({ theme }: StyledEmojiInputEditorProps) => theme['text-rgb']},\n 0.15\n );\n border-radius: 20px;\n }\n\n // Scrollbar styles for Firefox. The above styles are not supported in Firefox, these styles are\n // only supported in Firefox:\n * {\n scrollbar-color: rgba(\n ${({ theme }: StyledEmojiInputEditorProps) => theme['text-rgb']},\n 0.15\n )\n transparent;\n scrollbar-width: thin;\n }\n`;\n\nexport const StyledEmojiInputRightWrapper = styled.div`\n align-self: stretch;\n border-bottom-right-radius: 3px;\n border-top-right-radius: 3px;\n flex: 0 0 auto;\n overflow: hidden;\n`;\n"],"mappings":";;;;;;AACA,IAAAA,iBAAA,GAAAC,uBAAA,CAAAC,OAAA;AAAgD,SAAAC,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAH,wBAAAO,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAG,KAAA,GAAAR,wBAAA,CAAAC,WAAA,OAAAO,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAJ,GAAA,YAAAG,KAAA,CAAAE,GAAA,CAAAL,GAAA,SAAAM,MAAA,WAAAC,qBAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,GAAA,IAAAX,GAAA,QAAAW,GA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CAAC;AAACM,OAAA,CAAAgB,4BAAA,GAAAA,4BAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface InsertTextAtCursorPositionOptions {
|
|
2
|
+
editorElement: HTMLDivElement;
|
|
3
|
+
text: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* This function inserts the passed text at the correct position in the editor element. If the
|
|
7
|
+
* element has the focus, the new emoji is inserted at the cursor position. If not, the emoji
|
|
8
|
+
* will be appended to the back of the input field content.
|
|
9
|
+
*
|
|
10
|
+
* In addition, this function also sets the cursor to the correct position when the input field
|
|
11
|
+
* has the focus. For this purpose, the current position of the cursor or a selection is read to
|
|
12
|
+
* calculate the cursor position after inserting the text.
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} options - Object with element and text to insert
|
|
15
|
+
* @param {HTMLDivElement} options.editorElement - Element to insert text into
|
|
16
|
+
* @param {string} options.text - Text to insert into element
|
|
17
|
+
*/
|
|
18
|
+
export declare const insertTextAtCursorPosition: ({ editorElement, text, }: InsertTextAtCursorPositionOptions) => void;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.insertTextAtCursorPosition = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* This function inserts the passed text at the correct position in the editor element. If the
|
|
9
|
+
* element has the focus, the new emoji is inserted at the cursor position. If not, the emoji
|
|
10
|
+
* will be appended to the back of the input field content.
|
|
11
|
+
*
|
|
12
|
+
* In addition, this function also sets the cursor to the correct position when the input field
|
|
13
|
+
* has the focus. For this purpose, the current position of the cursor or a selection is read to
|
|
14
|
+
* calculate the cursor position after inserting the text.
|
|
15
|
+
*
|
|
16
|
+
* @param {Object} options - Object with element and text to insert
|
|
17
|
+
* @param {HTMLDivElement} options.editorElement - Element to insert text into
|
|
18
|
+
* @param {string} options.text - Text to insert into element
|
|
19
|
+
*/
|
|
20
|
+
const insertTextAtCursorPosition = _ref => {
|
|
21
|
+
let {
|
|
22
|
+
editorElement,
|
|
23
|
+
text
|
|
24
|
+
} = _ref;
|
|
25
|
+
const selection = window.getSelection();
|
|
26
|
+
if (selection !== null && selection !== void 0 && selection.anchorNode && editorElement.contains(selection.anchorNode)) {
|
|
27
|
+
const {
|
|
28
|
+
endOffset,
|
|
29
|
+
startOffset
|
|
30
|
+
} = selection.getRangeAt(0);
|
|
31
|
+
const rangeDistance = endOffset - startOffset;
|
|
32
|
+
let offset = endOffset + text.length - rangeDistance;
|
|
33
|
+
let {
|
|
34
|
+
anchorNode
|
|
35
|
+
} = selection;
|
|
36
|
+
if (anchorNode.nodeValue) {
|
|
37
|
+
anchorNode.nodeValue = anchorNode.nodeValue.substring(0, startOffset) + text + anchorNode.nodeValue.substring(endOffset);
|
|
38
|
+
} else if (anchorNode === editorElement) {
|
|
39
|
+
const newTextNode = document.createTextNode(text);
|
|
40
|
+
editorElement.appendChild(newTextNode);
|
|
41
|
+
anchorNode = newTextNode;
|
|
42
|
+
}
|
|
43
|
+
const newRange = document.createRange();
|
|
44
|
+
if (anchorNode.nodeValue) {
|
|
45
|
+
offset = Math.min(offset, anchorNode.nodeValue.length);
|
|
46
|
+
}
|
|
47
|
+
newRange.setStart(anchorNode, offset);
|
|
48
|
+
newRange.setEnd(anchorNode, offset);
|
|
49
|
+
selection.removeAllRanges();
|
|
50
|
+
selection.addRange(newRange);
|
|
51
|
+
} else {
|
|
52
|
+
// eslint-disable-next-line no-param-reassign
|
|
53
|
+
editorElement.innerText += text;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.insertTextAtCursorPosition = insertTextAtCursorPosition;
|
|
57
|
+
//# sourceMappingURL=insert.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"insert.js","names":["insertTextAtCursorPosition","_ref","editorElement","text","selection","window","getSelection","anchorNode","contains","endOffset","startOffset","getRangeAt","rangeDistance","offset","length","nodeValue","substring","newTextNode","document","createTextNode","appendChild","newRange","createRange","Math","min","setStart","setEnd","removeAllRanges","addRange","innerText","exports"],"sources":["../../src/utils/insert.ts"],"sourcesContent":["interface InsertTextAtCursorPositionOptions {\n editorElement: HTMLDivElement;\n text: string;\n}\n\n/**\n * This function inserts the passed text at the correct position in the editor element. If the\n * element has the focus, the new emoji is inserted at the cursor position. If not, the emoji\n * will be appended to the back of the input field content.\n *\n * In addition, this function also sets the cursor to the correct position when the input field\n * has the focus. For this purpose, the current position of the cursor or a selection is read to\n * calculate the cursor position after inserting the text.\n *\n * @param {Object} options - Object with element and text to insert\n * @param {HTMLDivElement} options.editorElement - Element to insert text into\n * @param {string} options.text - Text to insert into element\n */\nexport const insertTextAtCursorPosition = ({\n editorElement,\n text,\n}: InsertTextAtCursorPositionOptions) => {\n const selection = window.getSelection();\n\n if (selection?.anchorNode && editorElement.contains(selection.anchorNode)) {\n const { endOffset, startOffset } = selection.getRangeAt(0);\n\n const rangeDistance = endOffset - startOffset;\n\n let offset = endOffset + text.length - rangeDistance;\n\n let { anchorNode } = selection;\n\n if (anchorNode.nodeValue) {\n anchorNode.nodeValue =\n anchorNode.nodeValue.substring(0, startOffset) +\n text +\n anchorNode.nodeValue.substring(endOffset);\n } else if (anchorNode === editorElement) {\n const newTextNode = document.createTextNode(text);\n\n editorElement.appendChild(newTextNode);\n\n anchorNode = newTextNode;\n }\n\n const newRange = document.createRange();\n\n if (anchorNode.nodeValue) {\n offset = Math.min(offset, anchorNode.nodeValue.length);\n }\n\n newRange.setStart(anchorNode, offset);\n newRange.setEnd(anchorNode, offset);\n\n selection.removeAllRanges();\n selection.addRange(newRange);\n } else {\n // eslint-disable-next-line no-param-reassign\n editorElement.innerText += text;\n }\n};\n"],"mappings":";;;;;;AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,0BAA0B,GAAGC,IAAA,IAGD;EAAA,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3BvB,SAAS,CAACwB,QAAQ,CAACP,QAAQ,CAAC;EAChC,CAAC,MAAM;IACH;IACAnB,aAAa,CAAC2B,SAAS,IAAI1B,IAAI;EACnC;AACJ,CAAC;AAAC2B,OAAA,CAAA9B,0BAAA,GAAAA,0BAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chayns-components/emoji-input",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.92",
|
|
4
4
|
"description": "Input field that supports HTML elements and emojis",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chayns",
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"publishConfig": {
|
|
69
69
|
"access": "public"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "6340563929c3638a0a8148c93f4cfb26c9dfd522"
|
|
72
72
|
}
|