Wilson@思源

目 录

获取当前文档对象

获取当前文档对象
js
// pc siyuan.layout.centerLayout.children.map(item=>item.children.find(item=>item.headElement?.classList.contains('item--focus') && (item.panelElement.closest('.layout__wnd--active')||item.panelElement.closest('[data-type="wnd"]')))).find(item=>item)
获取当前文档的protyle
js
function getProtyle() { try { if(document.getElementById("sidebar")) return siyuan.mobile.editor.protyle; const currDoc = siyuan?.layout?.centerLayout?.children.map(item=>item.children.find(item=>item.headElement?.classList.contains('item--focus') && (item.panelElement.closest('.layout__wnd--active')||item.panelElement.closest('[data-type="wnd"]')))).find(item=>item); return currDoc?.model.editor.protyle; } catch(e) { console.error(e); return null; } }
获取当前文档元素
js
document.querySelector('[data-type="wnd"].layout__wnd--active .protyle:not(.fn__none)')||document.querySelector('[data-type="wnd"] .protyle:not(.fn__none)'); //或 getProtyle().wysiwyg.element;
获取当前元素文档id
js
getProtyle().element?.querySelector('.protyle-title')?.dataset?.nodeId; //或 (document.querySelector('[data-type="wnd"].layout__wnd--active .protyle:not(.fn__none)')||document.querySelector('[data-type="wnd"] .protyle:not(.fn__none)'))?.querySelector('.protyle-title')?.dataset?.nodeId;
获取当前文档编辑器元素
js
document.querySelector('[data-type="wnd"].layout__wnd--active .protyle:not(.fn__none) .protyle-wysiwyg.protyle-wysiwyg--attr')
注意:这种方法不适合分屏情况,这种情况可参考 js获取鼠标所在元素