我的装饰器策略函数仅在安装情况下不被调用。 我的代码有什么问题?
interface ILinkProps {
contentState: ContentState,
entityKey: string,
children: any
}
class ReactEditor extends React.Component<any, any> {
constructor(props: any) {
super(props);
const decorator = new CompositeDecorator([
{
component: Link,
strategy: findLinkEntities
},
]);
this.state = {
editorState: EditorState.createEmpty(decorator),
};
}
public render() {
// ...
}
}
const Link = (props: ILinkProps) => {
const { url } = props.contentState.getEntity(props.entityKey).getData();
console.log(url);
return (
<a href={url} className='link'>
{props.children}
</a>
);
};
function findLinkEntities(contentBlock: ContentBlock, callback: any, contentState: ContentState) {
console.log("decorator Function");
contentBlock.findEntityRanges(
(character) => {
const entityKey = character.getEntity();
return (
entityKey !== null &&
contentState.getEntity(entityKey).getType() === 'LINK'
);
},
callback
);
}
在许多示例中,每次编辑器状态更改时都会调用策略函数