我不知道这是我们的常见问题还是错误,但也许有人有一个想法:我们正在构建一个带有 react 和 slate 的 HTML 编辑器。用户可以选择一个文本框,然后更改属性。这适用于简单的按钮。但是,当我打开一个下拉列表(react-select)以更改字体大小时,所选文本不再被标记。Slate 保留选择以使更改生效,但这样的用户体验很糟糕。
恕我直言,这应该是保持文本标记的石板功能,但也许这是我需要自己应用的东西。
一些片段,不知道它们是否有帮助:
Editor 组件初始化字体样式插件并负责序列化。
class Editor extends React.Component {
constructor(props) {
super(props);
this.config = {
...mergePluginConfig(PLUGIN_CONFIG, props),
getEditor: () => this.editor,
getValue: () => this.state.value,
};
this.plugins = initializePlugins(this.config);
this.htmlSerializer = new HtmlSerializer({
rules: getSerializationRulesFromPlugins(this.plugins),
});
this.schema = getSchemaFromPlugins(this.plugins);
this.state = {
value: this.htmlSerializer.deserialize(props.value)
};
ref = editor => {
this.editor = editor;
};
render() {
return (
<div>
<Toolbar>
<div className="control">
{renderToolbarElementWithPlugins(this.plugins, 'font-size')}
</div>
<!--- more tools --->
<SlateEditor
autoFocus={true}
spellCheck={true}
placeholder={this.props.placeholder}
ref={this.ref}
value={this.state.value}
onChange={this.onChange}
onKeyDown={this.onKeyDown}
plugins={this.plugins}
schema={this.schema}
/>
onChange = ({ value }) => {
const {startInline, endInline, document, selection, fragment} = value;
// holds the correct information
console.log(fragment.text);
// ...
this.setState({ value });
this.props.onChange(this.htmlSerializer.serialize(value));
};
这是与其他插件一起初始化的字体大小插件,将显示在工具栏中:
export default function initializeFontSizePlugin(options) {
// this takes care of detecting the current value and applying selected change to the value.
// it does not change selection
const plugin = createStyleBasedMarkPlugin(...);
const fontSizeOptions = options.fontSizeOptions || [];
const handleFontSizeChange = ({value}) => {
plugin.reapplyFontSize({value: rendererFontSize(value)});
};
return {
...plugin,
renderToolbarElement() {
const {isMixed, fontSize} = plugin.detectFontSize();
return <Select
isCreatable={true}
name='font-size'
value={isMixed ? undefined : displayFontSize(fontSize)}
onChange={handleFontSizeChange}
options={fontSizeOptions}
/>;
}
};
}
我目前的解决方案是在选择打开后立即关注 slate,然后告诉选择打开,但这感觉很hackish并且有缺点(见下文)
const handleFontSizeChange = ({value}) => {
plugin.reapplyFontSize({value: rendererFontSize(value)});
handleMenuClose();
};
let menuIsOpen = false;
let firstOpen = false;
const handleMenuOpen = (editor) => {
firstOpen = true;
if(!menuIsOpen) {
setTimeout(() => {
if (editor) {
editor.focus();
}
menuIsOpen = true;
}, 1);
}
}
const handleMenuClose = (editor) => {
if(!firstOpen) {
setTimeout(() => {
if(menuIsOpen) {
menuIsOpen = false;
if (editor) {
editor.focus();
}
}
}, 1);
} else {
firstOpen = false;
}
}
<Select
onMenuOpen={handleMenuOpen.bind(this)}
onMenuClose={handleMenuClose.bind(this)}
menuIsOpen={menuIsOpen}
我必须使用超时来超出反应生命周期,并且我必须添加一个额外的标志,因为失去对选择组件的关注也会关闭它。就像我说的那样有缺点: - 在焦点切换期间所选文本有点闪烁 - 选择中的下拉框颜色错误(明显没有聚焦) - 切换到另一个下拉框(如对齐)不会关闭另一个,因为已经没有焦点:
附加信息:我们必须使用 slate 和0.47 版本,因为我们需要slate-react
的更高版本不支持。slate-html-serializer
也许这已经在更高版本中解决了?
所以,我有一个有点工作的版本,但我更喜欢一个解决方案,其中 slate 可以“本地”处理选择,而无需我处理焦点。我认为没有选定的文本flickering
和关闭颜色应该是可能的。