检测WordPress Gutenberg中的PluginSidebar打开

huangapple go评论51阅读模式
英文:

Detect opening of PluginSidebar in WordPress Gutenberg

问题

我正在寻找在重新打开我的PluginSidebar时重新初始化一些设置和状态数据,但我难以在wp.data core/editor或类似的地方找到任何有用的内容,以便我能够创建一个最佳的订阅。

古腾堡是否提供了任何此类数据,我可以检查侧边栏是打开还是关闭,以便我可以每次打开时触发我选择的函数?

目前,我已经放置了一个mutationObserver,以侦听它是否打开,这有点笨拙。

我偏好方法的伪代码。

subscribe(() => {
    if (select('core/editor').isPluginSidebarOpen()) {
        open = true
    } else {
        open = false
    }
})
英文:

I'm looking to reinitialise some settings and state data when my PluginSidebar is reopened, but I'm struggling to find anything useful in wp.data core/editor or similar that I could use to best create a subscription.

Does gutenberg provide any such data where I can check to see if the side panel is open or shut, so that I could fire a function of my choice every time it opens?

At present, I have a mutationObserver in place listening to see if it opens, which is quite clunky.

Some pseudo-code of my preferred approach.

subscribe(() => {
    if (select('core/editor').isPluginSidebarOpen()) {
        open = true
    } else {
        open = false
    }
})

答案1

得分: 2

您的伪代码非常接近了。该函数存在且称为isPluginSidebarOpened,并来自core/edit-post,例如:

import { subscribe, select } from '@wordpress/data';

subscribe(() => {
    if (select('core/edit-post').isPluginSidebarOpened()) {
        // 已打开...
    } else {
        // 已关闭...
    }
});
英文:

Your pseudo-code is so very close.. the function exists and is called isPluginSidebarOpened and comes from core/edit-post, eg:

import { subscribe, select } from '@wordpress/data';

subscribe(() => {
    if (select('core/edit-post').isPluginSidebarOpened()) {
        // Is open..
    } else {
        // Is closed..
    }
});

huangapple
  • 本文由 发表于 2023年2月16日 06:14:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定