【问题标题】:Text selection in React Slate no longer marked when focus changes to React Select当焦点更改为 React Select 时,React Slate 中的文本选择不再被标记
【发布时间】:2020-08-28 04:14:06
【问题描述】:

我不知道这是一个常见问题还是我们的错误,但也许有人有一个想法: 我们正在构建一个带有 react 和 slate 的 HTML 编辑器。 用户可以选择一个文本框,然后更改属性。这适用于简单的按钮。但是,当我打开一个下拉列表(react-select)以更改字体大小时,所选文本不再被标记。 Slate 保留选择以使更改生效,但这样的用户体验很糟糕。

恕我直言,这应该是保持文本标记的石板功能,但也许这是我需要自己应用的东西。

一些sn-ps,不知道有没有帮助:

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,然后告诉选择打开,但这感觉很老套并且有缺点(见下文)

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}

我必须使用超时来超出反应生命周期,并且我必须添加一个额外的标志,因为失去对选择组件的关注也会关闭它。 就像我说的那样有缺点: - 焦点切换期间所选文本会稍微闪烁 - 选择中的下拉框有错误的颜色(明显不集中) - 切换到另一个下拉菜单(如对齐)不会关闭另一个,因为它已经没有焦点:

附加信息: 我们必须在 0.47 版本中使用 slate 和 slate-react,因为我们需要的 slate-html-serializer 不支持更高版本。也许这已经在更高版本中解决了?

所以,我有一个有点工作的版本,但我更喜欢一个解决方案,其中 slate 可以“本地”处理选择,而无需我处理焦点。我认为没有选定的文本flickering 和关闭颜色应该是可能的。

【问题讨论】:

  • 您能否创建一个沙盒来重现该问题,这将有助于解决您的问题。
  • @Pete,即使是 slateEditor 的官方演示也无法处理下拉菜单中的选择。我在codeSandbox中添加了:codesandbox.io/s/serene-sunset-ilc44?file=/src/App.js
  • 顺便说一句,你使用哪个 slate-editor 库
  • 哦,连官方都不支持?但是,当他们想要应用更改的选定文本不再突出显示时,任何用户应该如何获得良好的体验?!我们正在使用“slate”:“0.47.4”和“slate-react”:“0.22.4”,
  • 嗯,这听起来很头疼。你有我的同情。我会考虑禁用标准文本选择(或覆盖 css 以隐藏通常发生的情况),然后放入您自己的文本选择功能。这将让您绕过箍跳,让您以更精细的方式控制突出显示的文本样式。这样,您可以在反应状态下维护“样式为突出显示”的文本。像这样的东西,但修改为反应? :stackoverflow.com/a/987376/5544119

标签: javascript reactjs slate


【解决方案1】:

由于打开了下拉菜单,当您将注意力移出编辑器时,Slate 不会保留选择。现在有了按钮,它就不同了,因为它重新应用了选择

由于您现在必须手动应用和获取选择,因此一种方法是在您尝试从选择中打开菜单时将编辑器选择存储在状态中。当菜单打开时,使用Transforms.setSelection 重新应用选择并获取可以再次存储在状态中的字体大小以在下拉列表中显示焦点值

现在应用更改后,您需要再次应用选择

您可以遵循this PR中使用的概念

const [selection, setSelection] = useState();
const [menuIsOpen, setMenuIsOpen] = useState(false);
const [fontSize, setFontSize] = useState(plugins.detectFontSize());
const handleFontSizeChange = ({value}) => {
    plugin.reapplyFontSize({value: rendererFontSize(value)});
    handleMenuClose();
  };
}
const handleMenuOpen = (editor) => {
    setSelection(editor.selection);
    setMenuIsOpen(true);
    Transforms.setSelection() // pass in the required params here
    setFontSize(plugins.detectFontSize());
}
const handleMenuClose = (editor) => {
    setMenuIsOpen(false);
    Transforms.setSelection() // pass in the required params here based on selection state
}

<Select
    onMenuOpen={handleMenuOpen.bind(this)}
    onMenuClose={handleMenuClose.bind(this)}
    menuIsOpen={menuIsOpen}
    value={fontSize}
    options={options}
/>

还可以查看 this github issue 关于焦点和选择的信息

【讨论】:

  • 感谢您的意见。就像我说的那样,我一直在尝试这些方法并且已经成功了。只是在菜单打开期间选择闪烁的小缺点,但这仍然比没有看到选择要好。
  • @Pete Well 编辑,无论您选择哪一个,都有一些或其他缺失的场景。同样在内部,它们还通过在工具栏按钮上保留和应用选择来以相同的方式处理事情。不过,工具栏按钮的情况对他们来说有点简单。如果您仍然在探索编辑器,您也可以查看 CKEditor
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 2022-12-15
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
相关资源
最近更新 更多