Draft.js使用applyEntity添加超链接似乎无效

问题描述

我已经在这个问题上研究了一段时间了,希望它不是一个错误。

我正在使用Draft.js测试文本编辑器,我只是希望用户向其文章添加超链接,因此我通过修改编辑器状态的内容来创建一个函数来实现这一点。

const addLlink = (e) => {
    e.preventDefault();
    const contentState = editorState.getCurrentContent();
    const contentStateWithEntity = contentState.createEntity(
        'LINK','MUTABLE',{url: 'https://www.amazon.com'} // link for testing only
    );
    const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
    const contentStateWithLink = Modifier.applyEntity(
        contentStateWithEntity,editorState.getSelection(),entityKey
    );
    // tried with and without styling
    const styledContentStateWithLink = Modifier.applyInlineStyle(
        contentStateWithLink,'HYPERLINK'
    );
    const newState = EditorState.set(editorState,{
        currentContent: styledContentStateWithLink
    });

    setEditorState(newState);
    // Also tried: setEditorState(RichUtils.toggleLink(newState,newState.getSelection(),entityKey));
}

当我触发它时,我只使用一个Evergreen-ui按钮:

<Button onMouseDown={addLlink} appearance="minimal">Link</Button>

我使用Modifier对象实现的样式有效,但是我似乎无法获得实际起作用的链接。应该注意的是,链接插件作为一个软件包,虽然效果很好,但是仅用于检测输入/粘贴的URL(未嵌入文本中)。

对于使用React函数编程的链接,有没有人有实际的工作示例或对我可能做错了的建议?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)