【已解决】能不能固定某个页面是阅读视图,其余页面是编辑视图

求助,有大佬能教一下吗。我想固定一个页面做主页,希望每次打开这个主页的时候是阅读视图。然后其余页面是编辑视图,方便我立刻输入文字。

1 个赞

固定在侧栏呗,避免反复开关

  1. 安装runjs,随便建个文档,在文档中输入以下代码
```js RunJS="load" 
(() => {
    // 指定预览文件列表
    const previewFiles = [
        "demo.md",
    ];
	let leafTimer;
	const onActiveLeafChange = async (activeLeaf) => {
		// 定时防止无效触发,只取最后一个触发
		if(leafTimer) clearTimeout(leafTimer)
            leafTimer = setTimeout(async () => {
            // 排除非markdown视图类型
			const viewType = activeLeaf?.view.getViewType();
			if('markdown' !== viewType) return;
			// 获取文件路径
            const state = activeLeaf?.view.getState();
			const filePath = state.file
			if (!filePath) return;
			// 检查是否在指定文件列表中
            const isPreviewFile = previewFiles.some(item => filePath.includes(item));
            // 把文档设置为预览模式
            state.mode = isPreviewFile ? "preview" : "source";
            await activeLeaf?.setViewState({type: "markdown", state: state});
		}, 42);
	};
	this.app.workspace.on('active-leaf-change', onActiveLeafChange);
	onActiveLeafChange(this.app.workspace.activeLeaf);
})();
```
  1. 在runjs插件配置中把名为load的脚本加入到Auto start中,如图

1 个赞

你也可以使用 Force note view mode 插件,和 #3 一样监测 active-leaf-change,使用方法请查看插件说明。示例:

20240511_234625

1 个赞

好的,谢谢大佬,已经解决

感谢大佬,已经搞定了