@chayns-components/emoji-input 5.0.0-beta.153 → 5.0.0-beta.155
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.d.ts +2 -1
- package/lib/components/emoji-input/EmojiInput.js +36 -34
- package/lib/components/emoji-input/EmojiInput.js.map +1 -1
- package/lib/utils/insert.js +7 -2
- package/lib/utils/insert.js.map +1 -1
- package/lib/utils/selection.js +0 -27
- package/lib/utils/selection.js.map +1 -1
- package/lib/utils/text.js +2 -0
- package/lib/utils/text.js.map +1 -1
- package/package.json +3 -3
|
@@ -47,7 +47,8 @@ export type EmojiInputProps = {
|
|
|
47
47
|
*/
|
|
48
48
|
rightElement?: ReactNode;
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
50
|
+
* The plain text value of the input field. Instead of HTML elements BB codes must be used at
|
|
51
|
+
* this point. These are then converted by the input field into corresponding HTML elements.
|
|
51
52
|
*/
|
|
52
53
|
value: string;
|
|
53
54
|
};
|
|
@@ -30,6 +30,7 @@ const EmojiInput = _ref => {
|
|
|
30
30
|
value
|
|
31
31
|
} = _ref;
|
|
32
32
|
const [isMobile] = (0, _react.useState)((0, _environment.getIsMobile)());
|
|
33
|
+
const [plainTextValue, setPlainTextValue] = (0, _react.useState)(value);
|
|
33
34
|
const editorRef = (0, _react.useRef)(null);
|
|
34
35
|
|
|
35
36
|
/**
|
|
@@ -46,11 +47,6 @@ const EmojiInput = _ref => {
|
|
|
46
47
|
}
|
|
47
48
|
let newInnerHTML = (0, _emoji.convertEmojisToUnicode)(html);
|
|
48
49
|
newInnerHTML = (0, _text.convertTextToHTML)(newInnerHTML);
|
|
49
|
-
console.debug('handleUpdateHTML', {
|
|
50
|
-
html,
|
|
51
|
-
newInnerHTML,
|
|
52
|
-
isDifferent: newInnerHTML !== editorRef.current.innerHTML
|
|
53
|
-
});
|
|
54
50
|
if (newInnerHTML !== editorRef.current.innerHTML) {
|
|
55
51
|
(0, _selection.saveSelection)(editorRef.current, {
|
|
56
52
|
shouldIgnoreEmptyTextNodes: true
|
|
@@ -69,14 +65,21 @@ const EmojiInput = _ref => {
|
|
|
69
65
|
return;
|
|
70
66
|
}
|
|
71
67
|
handleUpdateHTML(editorRef.current.innerHTML);
|
|
68
|
+
const text = (0, _text.convertHTMLToText)(editorRef.current.innerHTML);
|
|
69
|
+
setPlainTextValue(text);
|
|
72
70
|
if (typeof onInput === 'function') {
|
|
73
|
-
|
|
74
|
-
innerHTML: editorRef.current.innerHTML,
|
|
75
|
-
text: (0, _text.convertHTMLToText)(editorRef.current.innerHTML)
|
|
76
|
-
});
|
|
77
|
-
onInput(event, (0, _text.convertHTMLToText)(editorRef.current.innerHTML));
|
|
71
|
+
onInput(event, text);
|
|
78
72
|
}
|
|
79
73
|
}, [handleUpdateHTML, onInput]);
|
|
74
|
+
const handleKeyDown = (0, _react.useCallback)(event => {
|
|
75
|
+
if (typeof onKeyDown === 'function') {
|
|
76
|
+
onKeyDown(event);
|
|
77
|
+
}
|
|
78
|
+
if (event.key === 'Enter' && !event.shiftKey && !event.isPropagationStopped() && editorRef.current) {
|
|
79
|
+
event.preventDefault();
|
|
80
|
+
document.execCommand('insertLineBreak', false);
|
|
81
|
+
}
|
|
82
|
+
}, [onKeyDown]);
|
|
80
83
|
|
|
81
84
|
/**
|
|
82
85
|
* This function prevents formatting from being adopted when texts are inserted. To do this, the
|
|
@@ -120,35 +123,34 @@ const EmojiInput = _ref => {
|
|
|
120
123
|
editorRef.current.dispatchEvent(event);
|
|
121
124
|
}
|
|
122
125
|
}, []);
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* This function ensures that the input field does not lose focus when the popup is opened or an
|
|
126
|
-
* emoji is selected in it. For this purpose the corresponding elements get the class
|
|
127
|
-
* 'prevent-lose-focus'.
|
|
128
|
-
*
|
|
129
|
-
* The class can also be set to any other elements that should also not cause the input field to
|
|
130
|
-
* lose focus.
|
|
131
|
-
*/
|
|
132
|
-
const handlePreventLoseFocus = (0, _react.useCallback)(event => {
|
|
133
|
-
var _element$parentElemen, _element$parentElemen2, _element$parentElemen3;
|
|
134
|
-
const element = event.target;
|
|
135
|
-
if (element.classList.contains('prevent-lose-focus') || (_element$parentElemen = element.parentElement) !== null && _element$parentElemen !== void 0 && _element$parentElemen.classList.contains('prevent-lose-focus') || (_element$parentElemen2 = element.parentElement) !== null && _element$parentElemen2 !== void 0 && (_element$parentElemen3 = _element$parentElemen2.parentElement) !== null && _element$parentElemen3 !== void 0 && _element$parentElemen3.classList.contains('prevent-lose-focus')) {
|
|
136
|
-
event.preventDefault();
|
|
137
|
-
event.stopPropagation();
|
|
138
|
-
}
|
|
139
|
-
}, []);
|
|
140
126
|
(0, _react.useEffect)(() => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}, [handleUpdateHTML, value]);
|
|
127
|
+
if (value !== plainTextValue) {
|
|
128
|
+
setPlainTextValue(value);
|
|
129
|
+
handleUpdateHTML(value);
|
|
130
|
+
}
|
|
131
|
+
}, [handleUpdateHTML, plainTextValue, value]);
|
|
146
132
|
(0, _react.useEffect)(() => {
|
|
133
|
+
/**
|
|
134
|
+
* This function ensures that the input field does not lose focus when the popup is opened
|
|
135
|
+
* or an emoji is selected in it. For this purpose the corresponding elements get the class
|
|
136
|
+
* 'prevent-lose-focus'.
|
|
137
|
+
*
|
|
138
|
+
* The class can also be set to any other elements that should also not cause the input
|
|
139
|
+
* field to lose focus.
|
|
140
|
+
*/
|
|
141
|
+
const handlePreventLoseFocus = event => {
|
|
142
|
+
var _element$parentElemen, _element$parentElemen2, _element$parentElemen3;
|
|
143
|
+
const element = event.target;
|
|
144
|
+
if (element.classList.contains('prevent-lose-focus') || (_element$parentElemen = element.parentElement) !== null && _element$parentElemen !== void 0 && _element$parentElemen.classList.contains('prevent-lose-focus') || (_element$parentElemen2 = element.parentElement) !== null && _element$parentElemen2 !== void 0 && (_element$parentElemen3 = _element$parentElemen2.parentElement) !== null && _element$parentElemen3 !== void 0 && _element$parentElemen3.classList.contains('prevent-lose-focus')) {
|
|
145
|
+
event.preventDefault();
|
|
146
|
+
event.stopPropagation();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
147
149
|
document.body.addEventListener('mousedown', handlePreventLoseFocus);
|
|
148
150
|
return () => {
|
|
149
151
|
document.body.removeEventListener('mousedown', handlePreventLoseFocus);
|
|
150
152
|
};
|
|
151
|
-
}, [
|
|
153
|
+
}, []);
|
|
152
154
|
return /*#__PURE__*/_react.default.createElement(_EmojiInput.StyledEmojiInput, {
|
|
153
155
|
isDisabled: isDisabled
|
|
154
156
|
}, /*#__PURE__*/_react.default.createElement(_EmojiInput.StyledEmojiInputContent, {
|
|
@@ -157,7 +159,7 @@ const EmojiInput = _ref => {
|
|
|
157
159
|
contentEditable: !isDisabled,
|
|
158
160
|
id: inputId,
|
|
159
161
|
onInput: handleInput,
|
|
160
|
-
onKeyDown:
|
|
162
|
+
onKeyDown: handleKeyDown,
|
|
161
163
|
onPaste: handlePaste,
|
|
162
164
|
placeholder: placeholder,
|
|
163
165
|
ref: editorRef
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EmojiInput.js","names":["_react","_interopRequireWildcard","require","_emoji","_environment","_insert","_selection","_text","_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","inputId","isDisabled","onInput","onKeyDown","onPopupVisibilityChange","personId","placeholder","popupAlignment","rightElement","value","isMobile","useState","getIsMobile","editorRef","useRef","handleUpdateHTML","useCallback","html","current","newInnerHTML","convertEmojisToUnicode","convertTextToHTML","console","debug","isDifferent","innerHTML","saveSelection","shouldIgnoreEmptyTextNodes","restoreSelection","handleInput","event","text","convertHTMLToText","handlePaste","preventDefault","clipboardData","getData","insertTextAtCursorPosition","editorElement","newEvent","Event","bubbles","dispatchEvent","handlePopupSelect","emoji","handlePreventLoseFocus","_element$parentElemen","_element$parentElemen2","_element$parentElemen3","element","target","classList","contains","parentElement","stopPropagation","useEffect","document","body","addEventListener","removeEventListener","createElement","StyledEmojiInput","StyledEmojiInputContent","isRightElementGiven","StyledEmojiInputEditor","contentEditable","id","onPaste","ref","alignment","onSelect","StyledEmojiInputRightWrapper","displayName","_default","exports"],"sources":["../../../src/components/emoji-input/EmojiInput.tsx"],"sourcesContent":["import React, {\n ChangeEvent,\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 { convertEmojisToUnicode } from '../../utils/emoji';\nimport { getIsMobile } from '../../utils/environment';\nimport { insertTextAtCursorPosition } from '../../utils/insert';\nimport { restoreSelection, saveSelection } from '../../utils/selection';\nimport { convertHTMLToText, convertTextToHTML } from '../../utils/text';\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 * HTML id of the input element\n */\n inputId?: 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. In addition to the original\n * event, the original text is returned as second parameter, in which the internally used HTML\n * elements have been converted back to BB codes.\n */\n onInput?: (event: ChangeEvent<HTMLDivElement>, originalText: string) => void;\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 inputId,\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\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 handleUpdateHTML = useCallback((html: string) => {\n if (!editorRef.current) {\n return;\n }\n\n let newInnerHTML = convertEmojisToUnicode(html);\n\n newInnerHTML = convertTextToHTML(newInnerHTML);\n\n console.debug('handleUpdateHTML', {\n html,\n newInnerHTML,\n isDifferent: newInnerHTML !== editorRef.current.innerHTML,\n });\n\n if (newInnerHTML !== editorRef.current.innerHTML) {\n saveSelection(editorRef.current, { shouldIgnoreEmptyTextNodes: true });\n\n editorRef.current.innerHTML = newInnerHTML;\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 handleUpdateHTML(editorRef.current.innerHTML);\n\n if (typeof onInput === 'function') {\n console.debug('handleInput', {\n innerHTML: editorRef.current.innerHTML,\n text: convertHTMLToText(editorRef.current.innerHTML),\n });\n\n onInput(event, convertHTMLToText(editorRef.current.innerHTML));\n }\n },\n [handleUpdateHTML, 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 let text = event.clipboardData.getData('text/plain');\n\n text = convertEmojisToUnicode(text);\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 element.parentElement?.parentElement?.classList.contains('prevent-lose-focus')\n ) {\n event.preventDefault();\n event.stopPropagation();\n }\n }, []);\n\n useEffect(() => {\n console.debug('useEffect', { text: value });\n\n handleUpdateHTML(value);\n }, [handleUpdateHTML, 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 id={inputId}\n onInput={handleInput}\n onKeyDown={onKeyDown}\n onPaste={handlePaste}\n placeholder={placeholder}\n ref={editorRef}\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,OAAA,GAAAH,OAAA;AACA,IAAAI,UAAA,GAAAJ,OAAA;AACA,IAAAK,KAAA,GAAAL,OAAA;AACA,IAAAM,iBAAA,GAAAC,sBAAA,CAAAP,OAAA;AACA,IAAAQ,WAAA,GAAAR,OAAA;AAK6B,SAAAO,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,SAAAd,wBAAAU,GAAA,EAAAI,WAAA,SAAAA,WAAA,IAAAJ,GAAA,IAA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AAa,OAAA,CAAAmF,aAAA,CAACtF,WAAA,CAAA0F,sBAAsB;IACnBC,eAAe,EAAE,CAAChE,UAAW;IAC7BiE,EAAE,EAAElE,OAAQ;IACZE,OAAO,EAAE2B,WAAY;IACrB1B,SAAS,EAAEA,SAAU;IACrBgE,OAAO,EAAElC,WAAY;IACrB3B,WAAW,EAAEA,WAAY;IACzB8D,GAAG,EAAEvD;EAAU,CAClB,CAAC,EACD,CAACH,QAAQ,iBACN9C,MAAA,CAAAa,OAAA,CAAAmF,aAAA,CAACxF,iBAAA,CAAAK,OAAgB;IACbsB,WAAW,EAAEA,WAAY;IACzBsE,SAAS,EAAE9D,cAAe;IAC1B+D,QAAQ,EAAE3B,iBAAkB;IAC5BvC,uBAAuB,EAAEA,uBAAwB;IACjDC,QAAQ,EAAEA;EAAS,CACtB,CAEgB,CAAC,EACzBG,YAAY,iBACT5C,MAAA,CAAAa,OAAA,CAAAmF,aAAA,CAACtF,WAAA,CAAAiG,4BAA4B,QAAE/D,YAA2C,CAEhE,CAAC;AAE3B,CAAC;AAEDX,UAAU,CAAC2E,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvB5E,UAAU;AAAA6E,OAAA,CAAAjG,OAAA,GAAAgG,QAAA"}
|
|
1
|
+
{"version":3,"file":"EmojiInput.js","names":["_react","_interopRequireWildcard","require","_emoji","_environment","_insert","_selection","_text","_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","inputId","isDisabled","onInput","onKeyDown","onPopupVisibilityChange","personId","placeholder","popupAlignment","rightElement","value","isMobile","useState","getIsMobile","plainTextValue","setPlainTextValue","editorRef","useRef","handleUpdateHTML","useCallback","html","current","newInnerHTML","convertEmojisToUnicode","convertTextToHTML","innerHTML","saveSelection","shouldIgnoreEmptyTextNodes","restoreSelection","handleInput","event","text","convertHTMLToText","handleKeyDown","shiftKey","isPropagationStopped","preventDefault","document","execCommand","handlePaste","clipboardData","getData","insertTextAtCursorPosition","editorElement","newEvent","Event","bubbles","dispatchEvent","handlePopupSelect","emoji","useEffect","handlePreventLoseFocus","_element$parentElemen","_element$parentElemen2","_element$parentElemen3","element","target","classList","contains","parentElement","stopPropagation","body","addEventListener","removeEventListener","createElement","StyledEmojiInput","StyledEmojiInputContent","isRightElementGiven","StyledEmojiInputEditor","contentEditable","id","onPaste","ref","alignment","onSelect","StyledEmojiInputRightWrapper","displayName","_default","exports"],"sources":["../../../src/components/emoji-input/EmojiInput.tsx"],"sourcesContent":["import React, {\n ChangeEvent,\n ClipboardEvent,\n FC,\n KeyboardEvent,\n KeyboardEventHandler,\n ReactNode,\n useCallback,\n useEffect,\n useRef,\n useState,\n} from 'react';\nimport type { PopupAlignment } from '../../constants/alignment';\nimport { convertEmojisToUnicode } from '../../utils/emoji';\nimport { getIsMobile } from '../../utils/environment';\nimport { insertTextAtCursorPosition } from '../../utils/insert';\nimport { restoreSelection, saveSelection } from '../../utils/selection';\nimport { convertHTMLToText, convertTextToHTML } from '../../utils/text';\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 * HTML id of the input element\n */\n inputId?: 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. In addition to the original\n * event, the original text is returned as second parameter, in which the internally used HTML\n * elements have been converted back to BB codes.\n */\n onInput?: (event: ChangeEvent<HTMLDivElement>, originalText: string) => void;\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 * The plain text value of the input field. Instead of HTML elements BB codes must be used at\n * this point. These are then converted by the input field into corresponding HTML elements.\n */\n value: string;\n};\n\nconst EmojiInput: FC<EmojiInputProps> = ({\n accessToken,\n inputId,\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 [plainTextValue, setPlainTextValue] = useState(value);\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 handleUpdateHTML = useCallback((html: string) => {\n if (!editorRef.current) {\n return;\n }\n\n let newInnerHTML = convertEmojisToUnicode(html);\n\n newInnerHTML = convertTextToHTML(newInnerHTML);\n\n if (newInnerHTML !== editorRef.current.innerHTML) {\n saveSelection(editorRef.current, { shouldIgnoreEmptyTextNodes: true });\n\n editorRef.current.innerHTML = newInnerHTML;\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 handleUpdateHTML(editorRef.current.innerHTML);\n\n const text = convertHTMLToText(editorRef.current.innerHTML);\n\n setPlainTextValue(text);\n\n if (typeof onInput === 'function') {\n onInput(event, text);\n }\n },\n [handleUpdateHTML, onInput]\n );\n\n const handleKeyDown = useCallback(\n (event: KeyboardEvent<HTMLDivElement>) => {\n if (typeof onKeyDown === 'function') {\n onKeyDown(event);\n }\n\n if (\n event.key === 'Enter' &&\n !event.shiftKey &&\n !event.isPropagationStopped() &&\n editorRef.current\n ) {\n event.preventDefault();\n\n document.execCommand('insertLineBreak', false);\n }\n },\n [onKeyDown]\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 let text = event.clipboardData.getData('text/plain');\n\n text = convertEmojisToUnicode(text);\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 useEffect(() => {\n if (value !== plainTextValue) {\n setPlainTextValue(value);\n\n handleUpdateHTML(value);\n }\n }, [handleUpdateHTML, plainTextValue, value]);\n\n useEffect(() => {\n /**\n * This function ensures that the input field does not lose focus when the popup is opened\n * or an 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\n * field to lose focus.\n */\n const handlePreventLoseFocus = (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 element.parentElement?.parentElement?.classList.contains('prevent-lose-focus')\n ) {\n event.preventDefault();\n event.stopPropagation();\n }\n };\n\n document.body.addEventListener('mousedown', handlePreventLoseFocus);\n\n return () => {\n document.body.removeEventListener('mousedown', handlePreventLoseFocus);\n };\n }, []);\n\n return (\n <StyledEmojiInput isDisabled={isDisabled}>\n <StyledEmojiInputContent isRightElementGiven={!!rightElement}>\n <StyledEmojiInputEditor\n contentEditable={!isDisabled}\n id={inputId}\n onInput={handleInput}\n onKeyDown={handleKeyDown}\n onPaste={handlePaste}\n placeholder={placeholder}\n ref={editorRef}\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,OAAA,GAAAH,OAAA;AACA,IAAAI,UAAA,GAAAJ,OAAA;AACA,IAAAK,KAAA,GAAAL,OAAA;AACA,IAAAM,iBAAA,GAAAC,sBAAA,CAAAP,OAAA;AACA,IAAAQ,WAAA,GAAAR,OAAA;AAK6B,SAAAO,uBAAAE,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAA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,QAAA,GAEvB/E,UAAU;AAAAgF,OAAA,CAAApG,OAAA,GAAAmG,QAAA"}
|
package/lib/utils/insert.js
CHANGED
|
@@ -33,8 +33,13 @@ const insertTextAtCursorPosition = _ref => {
|
|
|
33
33
|
range.deleteContents();
|
|
34
34
|
if (firstPart) {
|
|
35
35
|
if (selection.anchorNode.nodeType === Node.TEXT_NODE) {
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
const {
|
|
37
|
+
nodeValue
|
|
38
|
+
} = selection.anchorNode;
|
|
39
|
+
if (typeof nodeValue === 'string') {
|
|
40
|
+
selection.anchorNode.nodeValue = nodeValue.slice(0, range.startOffset) + firstPart + nodeValue.slice(range.startOffset);
|
|
41
|
+
(0, _selection.moveSelectionOffset)(firstPart.length);
|
|
42
|
+
}
|
|
38
43
|
} else if (selection.anchorNode === editorElement) {
|
|
39
44
|
const textNode = document.createTextNode(firstPart);
|
|
40
45
|
editorElement.appendChild(textNode);
|
package/lib/utils/insert.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insert.js","names":["_selection","require","insertTextAtCursorPosition","_ref","editorElement","text","selection","window","getSelection","saveSelection","anchorNode","contains","range","getRangeAt","parts","split","firstPart","shift","textNodes","map","part","document","createTextNode","deleteContents","nodeType","Node","TEXT_NODE","nodeValue","moveSelectionOffset","length","textNode","appendChild","textNodeIndex","Array","from","childNodes","indexOf","setChildIndex","restoreSelection","brElement","createElement","insertNode","setEndAfter","setStartAfter","forEach","index","collapse","removeAllRanges","addRange","innerText","exports"],"sources":["../../src/utils/insert.ts"],"sourcesContent":["import { moveSelectionOffset, restoreSelection, saveSelection, setChildIndex } from './selection';\n\ninterface 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 saveSelection(editorElement);\n\n if (selection?.anchorNode && editorElement.contains(selection.anchorNode)) {\n let range = selection.getRangeAt(0);\n\n const parts = text.split(/\\r\\n|\\r|\\n/);\n\n const firstPart = parts.shift();\n\n const textNodes = parts.map((part) => document.createTextNode(part));\n\n range.deleteContents();\n\n if (firstPart) {\n if (selection.anchorNode.nodeType === Node.TEXT_NODE) {\n selection.anchorNode.nodeValue
|
|
1
|
+
{"version":3,"file":"insert.js","names":["_selection","require","insertTextAtCursorPosition","_ref","editorElement","text","selection","window","getSelection","saveSelection","anchorNode","contains","range","getRangeAt","parts","split","firstPart","shift","textNodes","map","part","document","createTextNode","deleteContents","nodeType","Node","TEXT_NODE","nodeValue","slice","startOffset","moveSelectionOffset","length","textNode","appendChild","textNodeIndex","Array","from","childNodes","indexOf","setChildIndex","restoreSelection","brElement","createElement","insertNode","setEndAfter","setStartAfter","forEach","index","collapse","removeAllRanges","addRange","innerText","exports"],"sources":["../../src/utils/insert.ts"],"sourcesContent":["import { moveSelectionOffset, restoreSelection, saveSelection, setChildIndex } from './selection';\n\ninterface 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 saveSelection(editorElement);\n\n if (selection?.anchorNode && editorElement.contains(selection.anchorNode)) {\n let range = selection.getRangeAt(0);\n\n const parts = text.split(/\\r\\n|\\r|\\n/);\n\n const firstPart = parts.shift();\n\n const textNodes = parts.map((part) => document.createTextNode(part));\n\n range.deleteContents();\n\n if (firstPart) {\n if (selection.anchorNode.nodeType === Node.TEXT_NODE) {\n const { nodeValue } = selection.anchorNode;\n\n if (typeof nodeValue === 'string') {\n selection.anchorNode.nodeValue =\n nodeValue.slice(0, range.startOffset) +\n firstPart +\n nodeValue.slice(range.startOffset);\n\n moveSelectionOffset(firstPart.length);\n }\n } else if (selection.anchorNode === editorElement) {\n const textNode = document.createTextNode(firstPart);\n\n editorElement.appendChild(textNode);\n\n const textNodeIndex = Array.from(editorElement.childNodes).indexOf(textNode);\n\n moveSelectionOffset(firstPart.length);\n setChildIndex(textNodeIndex);\n }\n }\n\n restoreSelection(editorElement);\n\n if (textNodes.length > 0) {\n range = selection.getRangeAt(0);\n\n let brElement = document.createElement('br');\n\n range.insertNode(brElement);\n range.setEndAfter(brElement);\n range.setStartAfter(brElement);\n\n textNodes.forEach((textNode, index) => {\n range.insertNode(textNode);\n range.setEndAfter(textNode);\n range.setStartAfter(textNode);\n\n if (index !== textNodes.length - 1) {\n brElement = document.createElement('br');\n\n range.insertNode(brElement);\n range.setEndAfter(brElement);\n range.setStartAfter(brElement);\n }\n });\n\n range.collapse(false);\n\n selection.removeAllRanges();\n selection.addRange(range);\n }\n } else {\n // eslint-disable-next-line no-param-reassign\n editorElement.innerText += text;\n }\n};\n"],"mappings":";;;;;;AAAA,IAAAA,UAAA,GAAAC,OAAA;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,0BAA0B,GAAGC,IAAA,IAGD;EAAA,IAHE;IACvCC,aAAa;IACbC;EAC+B,CAAC,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IAAI9C,IAAI;EACnC;AACJ,CAAC;AAAC+C,OAAA,CAAAlD,0BAAA,GAAAA,0BAAA"}
|
package/lib/utils/selection.js
CHANGED
|
@@ -12,11 +12,6 @@ const saveSelection = function (element) {
|
|
|
12
12
|
shouldIgnoreEmptyTextNodes
|
|
13
13
|
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
14
14
|
const selection = window.getSelection();
|
|
15
|
-
console.debug('saveSelection 1', {
|
|
16
|
-
anchorNode: selection === null || selection === void 0 ? void 0 : selection.anchorNode,
|
|
17
|
-
selection,
|
|
18
|
-
shouldIgnoreEmptyTextNodes
|
|
19
|
-
});
|
|
20
15
|
if (!selection) {
|
|
21
16
|
return;
|
|
22
17
|
}
|
|
@@ -40,27 +35,11 @@ const saveSelection = function (element) {
|
|
|
40
35
|
childIndex = childNodesArray.indexOf(anchorNode);
|
|
41
36
|
endOffset = range.endOffset;
|
|
42
37
|
startOffset = range.startOffset;
|
|
43
|
-
console.debug('saveSelection 2', {
|
|
44
|
-
childIndex,
|
|
45
|
-
endOffset,
|
|
46
|
-
startOffset
|
|
47
|
-
});
|
|
48
38
|
};
|
|
49
39
|
exports.saveSelection = saveSelection;
|
|
50
40
|
const restoreSelection = element => {
|
|
51
|
-
var _childNode;
|
|
52
41
|
let childNode = element.childNodes[childIndex];
|
|
53
42
|
const selection = window.getSelection();
|
|
54
|
-
console.debug('restoreSelection 1', {
|
|
55
|
-
childIndex,
|
|
56
|
-
childNode,
|
|
57
|
-
childNodes: element.childNodes,
|
|
58
|
-
element,
|
|
59
|
-
endOffset,
|
|
60
|
-
nodeValue: (_childNode = childNode) === null || _childNode === void 0 ? void 0 : _childNode.nodeValue,
|
|
61
|
-
selection,
|
|
62
|
-
startOffset
|
|
63
|
-
});
|
|
64
43
|
if (!childNode || !element || !selection) {
|
|
65
44
|
return;
|
|
66
45
|
}
|
|
@@ -91,12 +70,6 @@ const restoreSelection = element => {
|
|
|
91
70
|
}
|
|
92
71
|
}
|
|
93
72
|
const range = document.createRange();
|
|
94
|
-
console.debug('restoreSelection 2', {
|
|
95
|
-
childNode,
|
|
96
|
-
endOffset,
|
|
97
|
-
range,
|
|
98
|
-
startOffset
|
|
99
|
-
});
|
|
100
73
|
range.setStart(childNode, startOffset);
|
|
101
74
|
range.setEnd(childNode, endOffset);
|
|
102
75
|
selection.removeAllRanges();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selection.js","names":["childIndex","endOffset","startOffset","saveSelection","element","shouldIgnoreEmptyTextNodes","arguments","length","undefined","selection","window","getSelection","
|
|
1
|
+
{"version":3,"file":"selection.js","names":["childIndex","endOffset","startOffset","saveSelection","element","shouldIgnoreEmptyTextNodes","arguments","length","undefined","selection","window","getSelection","anchorNode","range","getRangeAt","childNodesArray","Array","from","childNodes","filter","_ref","nodeType","nodeValue","Node","TEXT_NODE","indexOf","exports","restoreSelection","childNode","_childNode$parentNode","textNode","document","createTextNode","parentNode","insertBefore","nextSibling","_childNode$parentNode2","createRange","setStart","setEnd","removeAllRanges","addRange","collapse","moveSelectionOffset","distance","setChildIndex","index"],"sources":["../../src/utils/selection.ts"],"sourcesContent":["let childIndex = -1;\nlet endOffset = -1;\nlet startOffset = -1;\n\ninterface SaveSelectionOptions {\n shouldIgnoreEmptyTextNodes?: boolean;\n}\n\nexport const saveSelection = (\n element: HTMLDivElement,\n { shouldIgnoreEmptyTextNodes }: SaveSelectionOptions = {}\n) => {\n const selection = window.getSelection();\n\n if (!selection) {\n return;\n }\n\n const { anchorNode } = selection;\n\n if (!anchorNode) {\n return;\n }\n\n const range = selection.getRangeAt(0);\n\n let childNodesArray = Array.from(element.childNodes);\n\n if (shouldIgnoreEmptyTextNodes) {\n childNodesArray = childNodesArray.filter(\n ({ nodeType, nodeValue }) =>\n nodeType !== Node.TEXT_NODE || (nodeValue !== '' && nodeValue !== '\\u200B')\n );\n }\n\n childIndex = childNodesArray.indexOf(anchorNode as ChildNode);\n\n endOffset = range.endOffset;\n startOffset = range.startOffset;\n};\n\nexport const restoreSelection = (element: HTMLDivElement) => {\n let childNode = element.childNodes[childIndex];\n\n const selection = window.getSelection();\n\n if (!childNode || !element || !selection) {\n return;\n }\n\n if (typeof childNode.nodeValue !== 'string') {\n const textNode = document.createTextNode('\\u200B');\n\n childNode.parentNode?.insertBefore(textNode, childNode.nextSibling);\n\n childNode = textNode;\n\n endOffset = 0;\n startOffset = 0;\n } else if (childNode.nodeValue && endOffset > childNode.nodeValue.length) {\n if (childNode.nextSibling) {\n childNode = childNode.nextSibling;\n\n if (childNode.nodeType === Node.TEXT_NODE && childNode.nodeValue) {\n endOffset = childNode.nodeValue.length;\n startOffset = childNode.nodeValue.length;\n } else {\n const textNode = document.createTextNode('\\u200B');\n\n childNode.parentNode?.insertBefore(textNode, childNode.nextSibling);\n\n childNode = textNode;\n\n endOffset = 0;\n startOffset = 0;\n }\n } else {\n endOffset = childNode.nodeValue.length;\n startOffset = childNode.nodeValue.length;\n }\n }\n\n const range = document.createRange();\n\n range.setStart(childNode, startOffset);\n range.setEnd(childNode, endOffset);\n\n selection.removeAllRanges();\n selection.addRange(range);\n\n range.collapse(true);\n};\n\nexport const moveSelectionOffset = (distance: number) => {\n endOffset += distance;\n startOffset += distance;\n};\n\nexport const setChildIndex = (index: number) => {\n childIndex = index;\n};\n"],"mappings":";;;;;;AAAA,IAAIA,UAAU,GAAG,CAAC,CAAC;AACnB,IAAIC,SAAS,GAAG,CAAC,CAAC;AAClB,IAAIC,WAAW,GAAG,CAAC,CAAC;AAMb,MAAMC,aAAa,GAAG,SAAAA,CACzBC,OAAuB,EAEtB;EAAA,IADD;IAAEC;EAAiD,CAAC,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAEzD,MAAMG,SAAS,GAAGC,MAAM,CAACC,YAAY,CAAC,CAAC;EAEvC,IAAI,CAACF,SAAS,EAAE;IACZ;EACJ;EAEA,MAAM;IAAEG;E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AA,GAAAA,gBAAA;AAEK,MAAMgB,mBAAmB,GAAIC,QAAgB,IAAK;EACrD3C,SAAS,IAAI2C,QAAQ;EACrB1C,WAAW,IAAI0C,QAAQ;AAC3B,CAAC;AAAClB,OAAA,CAAAiB,mBAAA,GAAAA,mBAAA;AAEK,MAAME,aAAa,GAAIC,KAAa,IAAK;EAC5C9C,UAAU,GAAG8C,KAAK;AACtB,CAAC;AAACpB,OAAA,CAAAmB,aAAA,GAAAA,aAAA"}
|
package/lib/utils/text.js
CHANGED
|
@@ -10,6 +10,7 @@ const convertTextToHTML = text => {
|
|
|
10
10
|
const element = document.createElement('div');
|
|
11
11
|
element.style.position = 'absolute';
|
|
12
12
|
element.style.opacity = '0';
|
|
13
|
+
element.contentEditable = 'true';
|
|
13
14
|
element.innerText = text;
|
|
14
15
|
document.body.appendChild(element);
|
|
15
16
|
let result = element.innerHTML;
|
|
@@ -26,6 +27,7 @@ const convertHTMLToText = text => {
|
|
|
26
27
|
const element = document.createElement('div');
|
|
27
28
|
element.style.position = 'absolute';
|
|
28
29
|
element.style.opacity = '0';
|
|
30
|
+
element.contentEditable = 'true';
|
|
29
31
|
element.innerHTML = result;
|
|
30
32
|
document.body.appendChild(element);
|
|
31
33
|
result = element.innerText;
|
package/lib/utils/text.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","names":["_regex","require","_emoji","convertTextToHTML","text","element","document","createElement","style","position","opacity","innerText","body","appendChild","result","innerHTML","removeChild","unescapeHTML","replace","BB_LC_MENTION_REGEX","exports","convertHTMLToText","HTML_LC_MENTION_REGEX"],"sources":["../../src/utils/text.ts"],"sourcesContent":["import { BB_LC_MENTION_REGEX, HTML_LC_MENTION_REGEX } from '../constants/regex';\nimport { unescapeHTML } from './emoji';\n\nexport const convertTextToHTML = (text: string) => {\n const element = document.createElement('div');\n\n element.style.position = 'absolute';\n element.style.opacity = '0';\n\n element.innerText = text;\n\n document.body.appendChild(element);\n\n let result = element.innerHTML;\n\n document.body.removeChild(element);\n\n result = unescapeHTML(result);\n\n result = result.replace(\n BB_LC_MENTION_REGEX,\n '<lc_mention contenteditable=\"false\" id=\"$1\"><span>@</span>$2</lc_mention>'\n );\n\n return result;\n};\n\nexport const convertHTMLToText = (text: string) => {\n let result = text;\n\n result = result.replace(HTML_LC_MENTION_REGEX, '[lc_mention id=\"$1\"]$2[/lc_mention]');\n\n result = unescapeHTML(result);\n\n const element = document.createElement('div');\n\n element.style.position = 'absolute';\n element.style.opacity = '0';\n\n element.innerHTML = result;\n\n document.body.appendChild(element);\n\n result = element.innerText;\n\n document.body.removeChild(element);\n\n return result;\n};\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAEO,MAAME,iBAAiB,GAAIC,IAAY,IAAK;EAC/C,MAAMC,OAAO,GAAGC,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;EAE7CF,OAAO,CAACG,KAAK,CAACC,QAAQ,GAAG,UAAU;EACnCJ,OAAO,CAACG,KAAK,CAACE,OAAO,GAAG,GAAG;EAE3BL,OAAO,CAACM,SAAS,
|
|
1
|
+
{"version":3,"file":"text.js","names":["_regex","require","_emoji","convertTextToHTML","text","element","document","createElement","style","position","opacity","contentEditable","innerText","body","appendChild","result","innerHTML","removeChild","unescapeHTML","replace","BB_LC_MENTION_REGEX","exports","convertHTMLToText","HTML_LC_MENTION_REGEX"],"sources":["../../src/utils/text.ts"],"sourcesContent":["import { BB_LC_MENTION_REGEX, HTML_LC_MENTION_REGEX } from '../constants/regex';\nimport { unescapeHTML } from './emoji';\n\nexport const convertTextToHTML = (text: string) => {\n const element = document.createElement('div');\n\n element.style.position = 'absolute';\n element.style.opacity = '0';\n\n element.contentEditable = 'true';\n element.innerText = text;\n\n document.body.appendChild(element);\n\n let result = element.innerHTML;\n\n document.body.removeChild(element);\n\n result = unescapeHTML(result);\n\n result = result.replace(\n BB_LC_MENTION_REGEX,\n '<lc_mention contenteditable=\"false\" id=\"$1\"><span>@</span>$2</lc_mention>'\n );\n\n return result;\n};\n\nexport const convertHTMLToText = (text: string) => {\n let result = text;\n\n result = result.replace(HTML_LC_MENTION_REGEX, '[lc_mention id=\"$1\"]$2[/lc_mention]');\n\n result = unescapeHTML(result);\n\n const element = document.createElement('div');\n\n element.style.position = 'absolute';\n element.style.opacity = '0';\n\n element.contentEditable = 'true';\n element.innerHTML = result;\n\n document.body.appendChild(element);\n\n result = element.innerText;\n\n document.body.removeChild(element);\n\n return result;\n};\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AAEO,MAAME,iBAAiB,GAAIC,IAAY,IAAK;EAC/C,MAAMC,OAAO,GAAGC,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;EAE7CF,OAAO,CAACG,KAAK,CAACC,QAAQ,GAAG,UAAU;EACnCJ,OAAO,CAACG,KAAK,CAACE,OAAO,GAAG,GAAG;EAE3BL,OAAO,CAACM,eAAe,GAAG,MAAM;EAChCN,OAAO,CAACO,SAAS,GAAGR,IAAI;EAExBE,QAAQ,CAACO,IAAI,CAACC,WAAW,CAACT,OAAO,CAAC;EAElC,IAAIU,MAAM,GAAGV,OAAO,CAACW,SAAS;EAE9BV,QAAQ,CAACO,IAAI,CAACI,WAAW,CAACZ,OAAO,CAAC;EAElCU,MAAM,GAAG,IAAAG,mBAAY,EAACH,MAAM,CAAC;EAE7BA,MAAM,GAAGA,MAAM,CAACI,OAAO,CACnBC,0BAAmB,EACnB,2EACJ,CAAC;EAED,OAAOL,MAAM;AACjB,CAAC;AAACM,OAAA,CAAAlB,iBAAA,GAAAA,iBAAA;AAEK,MAAMmB,iBAAiB,GAAIlB,IAAY,IAAK;EAC/C,IAAIW,MAAM,GAAGX,IAAI;EAEjBW,MAAM,GAAGA,MAAM,CAACI,OAAO,CAACI,4BAAqB,EAAE,qCAAqC,CAAC;EAErFR,MAAM,GAAG,IAAAG,mBAAY,EAACH,MAAM,CAAC;EAE7B,MAAMV,OAAO,GAAGC,QAAQ,CAACC,aAAa,CAAC,KAAK,CAAC;EAE7CF,OAAO,CAACG,KAAK,CAACC,QAAQ,GAAG,UAAU;EACnCJ,OAAO,CAACG,KAAK,CAACE,OAAO,GAAG,GAAG;EAE3BL,OAAO,CAACM,eAAe,GAAG,MAAM;EAChCN,OAAO,CAACW,SAAS,GAAGD,MAAM;EAE1BT,QAAQ,CAACO,IAAI,CAACC,WAAW,CAACT,OAAO,CAAC;EAElCU,MAAM,GAAGV,OAAO,CAACO,SAAS;EAE1BN,QAAQ,CAACO,IAAI,CAACI,WAAW,CAACZ,OAAO,CAAC;EAElC,OAAOU,MAAM;AACjB,CAAC;AAACM,OAAA,CAAAC,iBAAA,GAAAA,iBAAA"}
|
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.155",
|
|
4
4
|
"description": "Input field that supports HTML elements and emojis",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chayns",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"typescript": "^4.9.5"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@chayns-components/core": "^5.0.0-beta.
|
|
55
|
+
"@chayns-components/core": "^5.0.0-beta.155",
|
|
56
56
|
"@chayns/colors": "^2.0.0",
|
|
57
57
|
"clsx": "^1.2.1",
|
|
58
58
|
"emojilib": "^3.0.10",
|
|
@@ -68,5 +68,5 @@
|
|
|
68
68
|
"publishConfig": {
|
|
69
69
|
"access": "public"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "97105b5babfa509f5f3ba49f520aff72cfbcb35c"
|
|
72
72
|
}
|