我在PrePublishCheckList
块
const title =
typeof coreEditorPostEditsTitle !== "undefined"
? coreEditorPostEditsTitle
: select("core/editor").getCurrentPost().title;
const [event, setEvent] = useState(2019);
// todo:: Find a better way to hook after gutenberg loaded and .editor-post-title__input" is available in dom
setTimeout(() => {
const editorTitleElement = document.querySelector(
".editor-post-title__input"
);
if(
typeof select("core/editor").loadedInit === "undefined" &&
!title.length
) {
pickDropdownNotice(title);
// Open right sidebar
dispatch("core/edit-post").openPublishSidebar();
// Set default event loadFromApi(event, setEvent);
select("core/editor").loadedInit = true;
}
if(title.length) {
showEditorTitle(editorTitleElement);
} else {
hideEditorTitle(editorTitleElement);
}
}, 100);
const showEditorTitle = editorTitleElement => {
editorTitleElement.style.display = "block";
editorTitleElement.readOnly = true;
editorTitleElement.style.background = "#eee";
editorTitleElement.style.color = "#32373c";
};
const hideEditorTitle = editorTitleElement => {
editorTitleElement.style.display = "none";
};
代码的目标是隐藏标题,直到从PrePublishCheckList
更新帖子标题并保存草稿。代码运行得很好,唯一的问题是我可以找到一个内置钩子来确定".editor-post-title__input"
元素在dom中可用。我知道我可以设置一个超时来反复检查,直到元素在dom中可见为止,但setTimeout感觉像是一个黑客。
还有哪种方法可以替换setTimeout,使setTimeout中的代码仅在.editor-post-title__input"
Dom中是否已就绪?
删除setTimeout时出现的错误在hideEditorTitle中TypeError: Cannot read property \'style\' of null
在…上editorTitleElement.style.display = "none";
内部hideEditorTitle()