Hyperlink 如何显示Draft.js数据,包括超链接?
使用draft.js创建编辑器。当包含链接标记时,在显示时遇到问题。没有链接标签,工作正常。我跟随博客实现了抓取功能 将数据保存在本地存储器中以供测试Hyperlink 如何显示Draft.js数据,包括超链接?,hyperlink,draftjs,Hyperlink,Draftjs,使用draft.js创建编辑器。当包含链接标记时,在显示时遇到问题。没有链接标签,工作正常。我跟随博客实现了抓取功能 将数据保存在本地存储器中以供测试 localStorage.setItem("testObject", JSON.stringify(editorData)); 然后像这样庆祝: render() { const contentState = convertFromRaw(localStorage.testObject); const editorState =
localStorage.setItem("testObject", JSON.stringify(editorData));
然后像这样庆祝:
render() {
const contentState = convertFromRaw(localStorage.testObject);
const editorState = EditorState.createWithContent(contentState);
return (
<div className="App">
<Editor editorState={editorState} readOnly={true} />
</div>
);
render(){
const contentState=convertFromRaw(localStorage.testObject);
const editorState=editorState.createWithContent(contentState);
返回(
);
粗体和斜体样式显示正确,但链接显示不正确。例如,单击链接,您需要让Draft.js完全管理您的链接-您还必须通过Draft API插入链接 要插入链接,请使用RichUtils.toggleLink()API,您可以找到一个示例 要显示链接,请创建您自己的构建链接元素的装饰器,并将此装饰器注册到draft.js:
const Link = (props) => {
const {url} = Entity.get(props.entityKey).getData();
return (
<a href={url}>{props.children}</a>
)
}
function findLinkEntities(contentBlock, callback) {
contentBlock.findEntityRanges(
(character) => {
const entityKey = character.getEntity();
return (
entityKey !== null &&
Entity.get(entityKey).getType() === 'LINK'
)
},
callback
)
}
const decorators = [
{ strategy: findLinkEntities, component: Link }
]
const editorState = EditorState.createWithContent(state, new CompositeDecorator(decorators))
<Editor decorators={decorators}
editorState={editorState}
... />
const Link=(道具)=>{
const{url}=Entity.get(props.entityKey.getData();
返回(
)
}
函数findLinkEntities(contentBlock,回调){
contentBlock.findEntityRanges(
(字符)=>{
const entityKey=character.getEntity();
返回(
entityKey!==null&&
Entity.get(entityKey).getType()=='LINK'
)
},
回拨
)
}
常量装饰器=[
{策略:findLinkEntities,组件:Link}
]
const editorState=editorState.createWithContent(状态,新的复合编辑器(装饰器))
您需要让Draft.js完全管理您的链接-您还必须通过Draft API插入链接
要插入链接,请使用RichUtils.toggleLink()API,您可以找到一个示例
要显示链接,请创建您自己的构建链接元素的装饰器,并将此装饰器注册到draft.js:
const Link = (props) => {
const {url} = Entity.get(props.entityKey).getData();
return (
<a href={url}>{props.children}</a>
)
}
function findLinkEntities(contentBlock, callback) {
contentBlock.findEntityRanges(
(character) => {
const entityKey = character.getEntity();
return (
entityKey !== null &&
Entity.get(entityKey).getType() === 'LINK'
)
},
callback
)
}
const decorators = [
{ strategy: findLinkEntities, component: Link }
]
const editorState = EditorState.createWithContent(state, new CompositeDecorator(decorators))
<Editor decorators={decorators}
editorState={editorState}
... />
const Link=(道具)=>{
const{url}=Entity.get(props.entityKey.getData();
返回(
)
}
函数findLinkEntities(contentBlock,回调){
contentBlock.findEntityRanges(
(字符)=>{
const entityKey=character.getEntity();
返回(
entityKey!==null&&
Entity.get(entityKey).getType()=='LINK'
)
},
回拨
)
}
常量装饰器=[
{策略:findLinkEntities,组件:Link}
]
const editorState=editorState.createWithContent(状态,新的复合编辑器(装饰器))