I’m trying to make a plugin to operate on some custom file types but I don’t really understand what the process flow is for getting a handle to the opened file when the user clicks on it in the sidebar. For example this should open a simple text file:
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
async onload() {
this.registerExtensions(["txt"], "text-view");
this.registerView("text-view", (leaf) => new TextView(leaf));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
export class TextView extends FileView {
filePath: string | null;
constructor(leaf: WorkspaceLeaf){
super(leaf);
}
getViewType(): string {
return "text-view";
}
getDisplayText(): string {
return "Text View";
}
protected async onOpen(): Promise<void> {
const container = this.containerEl.children[1];
container.empty();
//how do I get a file reference?
const file = ???
if(file){
const content = await this.app.vault.read(file);
container.createEl("pre", { text: content });
} else {
container.createEl("p", { text: "File not found." });
}
}
async onClose(){
}
}
FileView supposedly has a “file” property but it doesn’t exist when onOpen is called and I can’t find anything that says how this information might be forwarded to the view so I’m not sure how to wire the content up for display.