reactjs - 如何在草稿 js 中更新文本并同时切换新的内联样式?
问题描述
当我单击特定按钮时,handleMouseDown
函数会触发onMouseDown
事件:
handleMouseDown(e) {
e.preventDefault();
this.props.handleMouseDown(this.props.symbol);
}
它触发了handleMouseDown
实际编辑器类的函数,如下所示:
handleMouseDown(symbol) {
let newState = this.insertText(symbol);
this.setState({ editorState: newState }); // First action
this.toggleInlineStyle("SUBSCRIPT"); // Second action
}
问题是我一次只能执行一项操作 - 如果我注释掉文本插入,则切换下标内联样式,如果我注释掉切换插入新文本的内联样式,但是如果我离开它喜欢这样,当内联样式更改时,符号不会被插入,最后,如果我更改顺序,符号会被插入,但内联样式不会改变。如何同时正确执行这两项操作?
插入文本函数:
insertText(characterToInsert) {
let editorState = this.state.editorState;
const currentContent = editorState.getCurrentContent(),
currentSelection = editorState.getSelection();
let newContent = Modifier.replaceText(
currentContent,
currentSelection,
characterToInsert
);
let newEditorState = EditorState.push(editorState, newContent, 'insert-characters');
return newEditorState;
}
解决方案
您可以使用以下命令直接将内联样式添加到新的编辑器状态:EditorState.setInlineStyleOverride
。
所以它看起来像这样:
handleMouseDown(symbol) {
let newState = this.insertText(symbol);
EditorState.setInlineStyleOverride(newState, OrderedSet.of('SUBSCRIPT'));
this.setState({ editorState: newState });
}
推荐阅读
- python - 将列中的每个记录名称更改为与 Pandas 中的条件匹配的另一个记录名称
- c# - 我的 Page_Load 没有在 ASP.NET 中隐藏我的链接
- javascript - 仅当源字段格式设置为具有名称时才将 Amazon SES 电子邮件标记为垃圾邮件
- c# - 通过 JsonProperty 属性值将对象列表投影到另一种类型
- python - 查询 SQLAlchemy 时间到 Pandas 时间(Python)
- javascript - 提交后如何清除FormData()
- postgresql - 如何正确删除 postresql 中的索引唯一性?
- r - 如何从 R 中的列表创建多个矩阵或数据框?
- python - Django 如何在不刷新或重新加载页面的情况下插入数据
- java - 在单选按钮可绘制的左侧添加填充