简体   繁体   English

emoji-mart(Picker组件)的window.getSelection()问题

[英]problem with window.getSelection() with emoji-mart (Picker component)

My code: 我的代码:

addEmoji = () => {
    document.getElementById('description-text').focus();

    let element = $("#span").clone()[0];
    let sel = window.getSelection();

    if (sel.getRangeAt && sel.rangeCount) {
        let range = sel.getRangeAt(0);
        range.deleteContents();
        range.insertNode(element);

        let textNode = document.createTextNode('\u00A0');
        range.setStartAfter(element);
        range.insertNode(textNode);
        range.setStartAfter(textNode);
        range.collapse(true);

        sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
    }
}

render() {
    return (
        <div>
            <form onSubmit={this.onSubmit}>
                <div id="description-text" ref="description" suppressContentEditableWarning="true" contentEditable={true} />                    
                <div>
                    <button type="submit">Guardar Cambios</button>
                </div>
            </form>
            <div>
                {this.state.emojiPanel ? 
                    <div>
                        <button type="button" onClick={this.switchEmojiPanel}>Hide Emojis</button>
                        <Picker onClick={this.addEmoji} set='emojione' emojiSize={26} title="Select Emoji" color="#000" /> 

                        <span id="span">INSERT</span>
                    </div>
                    :
                    <div>
                        <button type="button" onClick={this.switchEmojiPanel}>Select Emojis</button>
                    </div>
                }
            </div>
        </div>
    )
}

The problem is that when trying to insert the span in the div when the user clicks an emoji, window.getSelection() does not acquire the value of my position in the div since I am calling the function from another component, which is the Picker, then the span is always inserted at the beginning of the div. 问题是,当用户单击表情符号时尝试在div中插入跨度时,window.getSelection()不会获取我在div中的位置值,因为我是从另一个组件(即Picker)调用该函数,则跨度总是插入到div的开头。

If someone could help me with this, I would really appreciate it !! 如果有人可以帮助我,我将不胜感激!

You can use the css user-select property to achieve this. 您可以使用css user-select属性来实现此目的。 Style the elements you are clicking on inside the Picker component with user-select: none; 使用user-select: none;样式来设置在Picker组件中单击的元素的样式user-select: none; property. 属性。

This way when user clicks an emoji inside the Picker component, the caret position or text selection are going to stay where you left it at your contentEditable div. 这样,当用户单击Picker组件内的表情符号时,插入标记的位置或文本选择将保持在您将其保留在contentEditable div处的位置。

Be sure to check compatability to make this work properly across different browsers: https://developer.mozilla.org/en-US/docs/Web/CSS/user-select 确保检查兼容性,以使其在不同的浏览器中正常运行: https : //developer.mozilla.org/en-US/docs/Web/CSS/user-select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM