我有一系列的表情符号。它们中的每一个都有自己的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,而不是表情符号。
有什么想法来解决这个问题吗?谢谢。
发布于 2017-03-10 01:00:50
因为insertText会将你的&解析成带有超文本标记语言编码的&。
您应该直接在那里键入字符,或者使用String.fromCharCode。
例如:
Modifier.insertText(contentState, selection, );或
Modifier.insertText(contentState, selection, String.fromCharCode(0xd83d, 0xde04));原因
''.charCodeAt(0).toString(16); //d83d
''.charCodeAt(1).toString(16); //de04https://stackoverflow.com/questions/40168161
复制相似问题