MetadataCache extends Events

changed事件

this.app.metadataCache.on("changed", (file,data,cache) => {})

当文件已被索引且其(更新后的)缓存已可用时调用。
注意:出于性能原因,在文件重命名时不会调用此方法。若需处理重命名事件,您必须监听文件库(vault)的重命名事件。

deleted事件

this.app.metadataCache.on("deleted",(file,prevCache) => {})

当文件被删除时调用。此时会尽力提供之前缓存的元数据的旧版本,但如果该文件之前未成功缓存,则该元数据可能为 null。

resolve事件

this.app.metadataCache.on("resolve",(file) => {})

当文件为 resolvedLinks(已解析的链接)和 unresolvedLinks(未解析的链接)完成解析时调用。
这种情况通常发生在文件已被索引之后。

resolved事件

this.app.metadataCache.on("resolved",() => {})

当所有文件均已完成解析时调用。此事件会在初始加载后每次文件发生修改时触发。
然后就可以获取当前正在活动的文件并触发一些操作。

Vault extends Events

create事件


当文件被创建时调用。
此外,在首次加载文件库(vault)时,对于每个已存在的文件也会调用此方法。
如果您不希望在文件库加载时接收到这些创建事件,请在 {@link Workspace.onLayoutReady} 内部注册您的事件处理程序。

modify事件


delete事件


rename事件


Workspace extends Events

active-leaf-change事件

this.app.workspace.on("active-leaf-change", (leaf) => {  
    console.log("workspace active-leaf-change事件触发");  
})

当活动面板(leaf,指用户界面中打开的标签页或工作区)发生变化时触发。

import {App, Plugin, PluginManifest, TFile, WorkspaceLeaf} from "obsidian";  
import {debug} from "node:util";  

interface FileCachePluginSettings {  
}  
  
const DEFAULT_SETTINGS: FileCachePluginSettings = {}  
  
export default class FileCachePlugin extends Plugin {  
    settings: FileCachePluginSettings;  
  
    constructor(app: App, manifest: PluginManifest) {  
       super(app, manifest);  
       this.settings = DEFAULT_SETTINGS;  
    }  
  
    async onload() {  
       // 添加 MetadataCache 事件监听    
this.registerEvent(  
          this.app.metadataCache.on("changed", (file, data, cache) => {  
             console.log("MetadataCache changed事件触发:", file.path);  
          })  
       );  
       this.registerEvent(  
          this.app.metadataCache.on("deleted", (file, prevCache) => {  
             console.log("MetadataCache deleted事件触发:", file.path);  
          })  
       );  
       this.registerEvent(  
          this.app.metadataCache.on("resolve", (file) => {  
             console.log("MetadataCache resolve事件触发:", file.path);  
          })  
       );  
       this.registerEvent(  
          this.app.metadataCache.on("resolved", () => {  
             console.log("MetadataCache resolved事件触发");  
          })  
       );  
  
       // 添加 Vault 事件监听  
       this.registerEvent(  
          this.app.vault.on("create", () => {  
             console.log("vault create事件触发");  
          })  
       );  
       this.registerEvent(  
          this.app.vault.on("modify", (file) => {  
             console.log("Vault modify事件触发:", file.path);  
          })  
       );  
       this.registerEvent(  
          this.app.vault.on("delete", (file) => {  
             console.log("Vault delete事件触发:", file.path);  
          })  
       );  
       this.registerEvent(  
          this.app.vault.on("rename", (file, oldPath) => {  
             console.log("Vault rename事件触发:", oldPath, "->", file.path);  
          })  
       );  
  
       // 添加 Workspace 事件监听   
this.registerEvent(  
          this.app.workspace.on("active-leaf-change", (leaf) => {  
             console.log("workspace active-leaf-change事件触发");  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("file-open", (file) => {  
             console.log("Workspace file-open事件触发:", file?.path);  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("layout-change", () => {  
             console.log("Workspace layout-change事件触发");  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("resize", () => {  
             console.log("Workspace resize事件触发");  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("editor-change", (editor, info) => {  
             console.log("Workspace editor-change事件触发");  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("editor-paste", (evt, editor, info) => {  
             console.log("Workspace editor-paste事件触发");  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("editor-drop", (evt, editor, info) => {  
             console.log("Workspace editor-drop事件触发");  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("quit", (tasks) => {  
             console.log("Workspace quit事件触发");  
          })  
       );  
  
       // 添加 Workspace 菜单相关事件监听    
this.registerEvent(  
          this.app.workspace.on("file-menu", (menu, file, source, leaf) => {  
             console.log("Workspace file-menu事件触发:", file.path);  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("files-menu", (menu, files, source, leaf) => {  
             console.log("Workspace files-menu事件触发:", files.map(f => f.path));  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("url-menu", (menu, url) => {  
             console.log("Workspace url-menu事件触发:", url);  
          })  
       );  
       this.registerEvent(  
          this.app.workspace.on("editor-menu", (menu, editor, info) => {  
             console.log("Workspace editor-menu事件触发");  
          })  
       );  
    }  
  
    // 处理用户点击已打开文件的逻辑    
private handleFileClick(file: TFile) {  
       const path = file.path;  
       console.log("处理用户点击文件:", path);  
  
       // const existingLeaf = this.findExistingLeaf(path);    
       // if (existingLeaf) {       //     this.app.workspace.revealLeaf(existingLeaf);       // }    
}  
}