刷新文档

刷新文档

see https://github.com/siyuan-note/siyuan/issues/10243

protyle.reload

this.protyleSlash = [{
      filter: ["insert emoji \u{1F60A}", "\u63D2\u5165\u8868\u60C5 \u{1F60A}", "crbqwx"],
      html: `<div class="b3-list-item__first"><span class="b3-list-item__text">${this.i18n.insertEmoji}</span><span class="b3-list-item__meta">\u{1F60A}</span></div>`,
      id: "insertEmoji",
      callback(protyle) {
        protyle.insert("\u{1F60A}");
        debugger;
        protyle.reload(false);
      }
    }];

(siyuan?.mobile||protyle.model).editor.reload()

参考 获取当前文档的protyle

改进版

先检查所有块是否已全部在目标文档中显示,如果有未显示再调用protyle刷新,从而改善用户体验

function reloadProtyle(ids = [], node = null) {
    node = node || document;
    if(ids.length > 0) {
        // 检查id的元素是否存在
        let hasNotExistEl = false;
        for(const id of ids){
            const el = node.querySelector(`[data-node-id="${id}"]`);
            if(!el) {
                hasNotExistEl = true;
                break;
            }
        }
        if(hasNotExistEl) (siyuan?.mobile||getProtyle()?.model)?.editor?.reload();
    } else {
        (siyuan?.mobile||getProtyle()?.model)?.editor?.reload();
    }
}
image.png

留下你的脚步
推荐阅读