如何基于纯字符串值(序列化)设置板岩值

问题描述 投票:0回答:1

我正在React上使用slate js构建富文本编辑器,我需要从内容中解析URL(例如:www.website.me应该转换为www.website.me。

我已经实现了从纯文本解析URL,然后将内容包装在正确标记中的功能。

但是,问题是我需要在用户在编辑器上键入时(在onChange事件上)立即显示已解析的值。这是我所拥有的:

onChange = ({ value }) => {
    const { isFocused } = value.selection
    if (isFocused !== this.state.isFocused) {
      this.setState({ isFocused })
    }

    const string = this.state.html.serialize(value)
    const linkifiedString = linkifyHtml(string) || ''
    if (value.document !== this.state.value.document) {
      const { onChange } = this.props
      onChange && onChange(linkifiedString)
    }

    // this won't work and will call the onChange method infinitely
    this.setState({ value: this.state.html.deserialize(linkifiedString) })
}

感谢您的帮助。

reactjs rich-text-editor slatejs
1个回答
0
投票

最后,我找到了解决方案,您必须创建自己的slate插件或使用以下插件:https://github.com/enzoferey/slate-instant-replace

© www.soinside.com 2019 - 2024. All rights reserved.