英文:
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..
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论