首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DraftJS Unicode ():插入Unicode

DraftJS Unicode ():插入Unicode
EN

Stack Overflow用户
提问于 2016-10-21 11:59:13
回答 1查看 5K关注 0票数 4

我有一系列的表情符号。它们中的每一个都有自己的unicode。使用Modifier.insertText(),我想将它们插入到文本中。

代码语言:javascript
复制
_addEmoji(text) {
    const { editorState } = this.state;
    const selection = editorState.getSelection();
    const contentState = editorState.getCurrentContent();
    const txt = '&#x' + text + ';';
    let nextEditorState = EditorState.createEmpty();
    if (selection.isCollapsed()) {
      const nextContentState = Modifier.insertText(contentState, selection, txt);
      nextEditorState = EditorState.push(
        editorState,
        nextContentState,
        'insert-characters'
      );
    } else {
      const nextContentState = Modifier.replaceText(contentState, selection, text);
      nextEditorState = EditorState.push(
        editorState,
        nextContentState,
        'insert-characters'
      );
    }
    this.onChange(nextEditorState);
  }

我希望看到在编辑器中插入行,但它显示的是行😄

我已经将<meta charset="utf-8" />插入到<head />中,并且表情符号列表呈现得非常完美。主要问题是draftjs编辑器显示的是原始unicode,而不是表情符号。

有什么想法来解决这个问题吗?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-03-10 01:00:50

因为insertText会将你的&解析成带有超文本标记语言编码的&amp;

您应该直接在那里键入字符,或者使用String.fromCharCode

例如:

代码语言:javascript
复制
Modifier.insertText(contentState, selection, );

代码语言:javascript
复制
Modifier.insertText(contentState, selection, String.fromCharCode(0xd83d, 0xde04));

原因

代码语言:javascript
复制
''.charCodeAt(0).toString(16); //d83d
''.charCodeAt(1).toString(16); //de04
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40168161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档