古腾堡在保存之前进行后期验证

时间:2020-10-23 15:35:18

标签: javascript wordpress wordpress-gutenberg

如果帖子摘录为空,我想在Gutenberg编辑器中添加帖子保存验证。我现在拥有的是:

const { subscribe } = wp.data;

const unsubscribe = subscribe(() => {
    const editor = wp.data.select('core/editor');
    const notices = wp.data.select('core/notices');

    const isSavingPost = editor.isSavingPost();
    const isAutosavingPost = editor.isAutosavingPost();
    const didPostSaveRequestSucceed = editor.didPostSaveRequestSucceed();

    if (isSavingPost && !isAutosavingPost && didPostSaveRequestSucceed) {
        const postExcerpt = editor.getEditedPostAttribute('excerpt');

        if (postExcerpt.length !== 0) {
            editor.unlockPostSaving('excerpt-lock');
            notices.removeNotice('excerpt-lock');

            editor.savePost();
        }

        editor.lockPostSaving('excerpt-lock');
        notices.createNotice(
            'error',
            'Please add an excerpt',
            { id: 'excerpt-lock', isDismissible: false }
        );

        unsubscribe();
    }
});

但是在帖子中保存我会得到

未捕获(承诺)TypeError:editor.lockPostSaving不是函数

我不确定我缺少什么。我在各种教程中查看了GB存储库中的大量示例,但没有任何效果。

0 个答案:

没有答案