obsidian插件代码-左右拆分的智能处理

个人常用的场景是想左右拆分后实现如下固定模式:

  1. 关闭所有其他面板
  2. 左侧编辑模式
  3. 右侧预览模式
  4. 焦点移回左侧

代码已做详细说明,有其他需求可自行调整。

splitRight() {
    var leftIsSource = this.app.workspace.activeLeaf.view.getMode() === "source";
    this.app.commands.executeCommandById("workspace:close-others"); //关闭所有其他面板
    this.app.commands.executeCommandById('workspace:split-vertical'); //左右拆分
    var ot = setInterval(function() {
        if (document.querySelector(".mod-active").previousElementSibling.className === "workspace-leaf") {
            //已激活右边面板
            clearInterval(ot);
            //处理逻辑
            if (leftIsSource) {
                this.app.commands.executeCommandById("markdown:toggle-preview"); //右边转成预览模式
                setTimeout(function() {
                    this.app.commands.executeCommandById("editor:focus-left"); //焦点切换到左边
                    console.log("left");
                }, 200); //NOTE 必须,时间可优化
            } else {
                this.app.commands.executeCommandById("editor:focus-left"); //焦点切换到左边
                this.app.commands.executeCommandById("markdown:toggle-preview"); //转成编辑模式
            }
        }
    }, 100);
}
posted @ 2022-05-24 00:19  火冷  阅读(1089)  评论(0编辑  收藏  举报