装饰器策略功能仅被调用一次

时间:2019-03-29 14:46:16

标签: reactjs typescript decorator draftjs

我的装饰器策略函数仅在安装情况下不被调用。 我的代码有什么问题?

我正在尝试关注本文https://medium.com/@mshabrikov/draft-js-rich-text-editor-framework-for-react-from-facebook-f236d02576f0

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
    );
}

在许多示例中,每次编辑器状态更改时都会调用策略函数

0 个答案:

没有答案