编辑模式下,打开原先的笔记,光标怎样才会出现在正文最后一空行?

目前在编辑模式下,每次打开原先的笔记,光标会出线在第一行,
有没有办法,打开原先的笔记饿时候,光标会出现在最后一空行呢?「就是原先文本的最后一行的下一行,这样就可以直接在笔记后面新写内容」

1 个赞
  1. 安装runjs插件
  2. 新建文件,比如 runjs/load.md
  3. 在load.md中输入以下代码
点查看代码
```js RunJS="load"
// 不包含的文件或文件夹
const excluding = [
    //"demo/demo1.md",
];
let lastLeafTimer;
const onActiveLeafChange = async (activeLeaf) => {
	// 定时防止无效触发,只取最后一个触发
	if(lastLeafTimer) clearTimeout(lastLeafTimer)
	lastLeafTimer = setTimeout(async () => {
        // 排除非markdown视图类型
        const viewType = activeLeaf?.view.getViewType();
        if('markdown' !== viewType) return;
        // 获取文件路径
		let filePath = activeLeaf?.view.getState().file
		if (!filePath) return;
		// 排除文件或文件夹
		if (excluding.some(e => filePath.includes(e))) return;
        // 移动光标到最后并插入回车
		moveCursorToEndAndInsertNewlineIfNeeded();
	}, 42);
};
this.app.workspace.off("active-leaf-change", onActiveLeafChange);
this.app.workspace.on('active-leaf-change', onActiveLeafChange);
onActiveLeafChange(this.app.workspace.activeLeaf);
function moveCursorToEndAndInsertNewlineIfNeeded(cm) {
    cm = cm || app.workspace.activeLeaf.view.editor;
    // 将光标移动到文本的末尾
    cm.setCursor(cm.lineCount(), 0); // 设置光标到最后一行的开始,即末尾
    // 获取当前的编辑器内容
    const text = cm.getValue();
    // 判断文本末尾是否已有一个换行符
    const endsWithNewline = /\n$/.test(text);
    // 如果末尾没有换行符,则先插入一个换行符
    if (!endsWithNewline) {
        cm.replaceSelection("\n");
    }
}
```
  1. 配置runjs,如图

2 个赞