【问题标题】:DraftJS Modifier.insertText(): Insert UnicodeDraftJS Modifier.insertText():插入 Unicode
【发布时间】:2016-10-21 03:59:12
【问题描述】:

我有表情符号列表。他们每个人都有自己的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);
  }

我希望看到????插入到编辑器,但它显示行 😄 代替。

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

有解决这个问题的想法吗?谢谢。

【问题讨论】:

  • 所以你已经在 HTML 中包含了 unicode,现在它可以正常工作了吗?这里有什么问题?,我不明白。
  • 字符集应该在 html 文档的头部指定。您使用的是不同的字符集吗?

标签: reactjs draftjs emojione


【解决方案1】:

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

您应该直接在此处输入字符,或使用String.fromCharCode

例如:

Modifier.insertText(contentState, selection, ?);

Modifier.insertText(contentState, selection, String.fromCharCode(0xd83d, 0xde04));

原因

'?'.charCodeAt(0).toString(16); //d83d
'?'.charCodeAt(1).toString(16); //de04

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 2012-10-17
    • 2013-07-05
    • 2023-03-26
    • 2011-03-21
    • 2014-01-08
    • 1970-01-01
    相关资源
    最近更新 更多