DraftJS Modifier.insertText():插入 Unicode

asu*_*sky 6 reactjs emojione draftjs

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

_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);
  }
Run Code Online (Sandbox Code Playgroud)

我希望看到

ste*_*fan 4

因为 insertText 会使用 HTML 编码解析您的&内容&

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

例如:

Modifier.insertText(contentState, selection, );
Run Code Online (Sandbox Code Playgroud)

或者

Modifier.insertText(contentState, selection, String.fromCharCode(0xd83d, 0xde04));
Run Code Online (Sandbox Code Playgroud)

原因

''.charCodeAt(0).toString(16); //d83d
''.charCodeAt(1).toString(16); //de04
Run Code Online (Sandbox Code Playgroud)