make ribbon create a right sidebar view

This commit is contained in:
Dian 2025-01-31 21:37:06 +08:00
parent 22b82a129a
commit 49bef1ce37
2 changed files with 57 additions and 0 deletions

30
CustomView.ts Normal file
View file

@ -0,0 +1,30 @@
import { ItemView, WorkspaceLeaf } from "obsidian";
// Each view is uniquely identified by a text string
// and several operations require that you specify the view you'd like to use.
// Extracting it to a constant, VIEW_TYPE_EXAMPLE, is a good idea—as you will see later in this guide.
export const VIEW_TYPE_EXAMPLE = "example-view";
export class ExampleView extends ItemView {
constructor(leaf: WorkspaceLeaf) {
super(leaf);
}
getViewType() {
return VIEW_TYPE_EXAMPLE;
}
getDisplayText() {
return "Example view";
}
async onOpen() {
const container = this.containerEl.children[1];
container.empty();
container.createEl("h4", { text: "Example view" });
}
async onClose() {
// Nothing to clean up.
}
}

27
main.ts
View file

@ -1,3 +1,4 @@
import { ExampleView, VIEW_TYPE_EXAMPLE } from "CustomView";
import { import {
addIcon, addIcon,
App, App,
@ -8,9 +9,13 @@ import {
Plugin, Plugin,
PluginSettingTab, PluginSettingTab,
Setting, Setting,
WorkspaceLeaf,
} from "obsidian"; } from "obsidian";
// Remember to rename these classes and interfaces! // Remember to rename these classes and interfaces!
//
// Custom views need to be registered when the plugin is enabled,
// and cleaned up when the plugin is disabled:
interface MyPluginSettings { interface MyPluginSettings {
mySetting: string; mySetting: string;
@ -25,6 +30,25 @@ const DEFAULT_SETTINGS: MyPluginSettings = {
export default class NemesisPlugin extends Plugin { export default class NemesisPlugin extends Plugin {
settings: MyPluginSettings; settings: MyPluginSettings;
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_EXAMPLE);
if (leaves.length > 0) {
// A leaf with our view already exists, use that
leaf = leaves[0];
} else {
// Create a new leaf in the right sidebar
leaf = workspace.getRightLeaf(false);
if (leaf) {
await leaf.setViewState({
type: VIEW_TYPE_EXAMPLE,
active: true,
});
}
}
}
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
@ -42,10 +66,13 @@ export default class NemesisPlugin extends Plugin {
// Called when the user clicks the icon. // Called when the user clicks the icon.
new Notice("This is a notice!"); new Notice("This is a notice!");
console.log("click"); console.log("click");
this.activateView();
// console.log(`My openai key: ${this.settings.openAiKey}`); // console.log(`My openai key: ${this.settings.openAiKey}`);
} }
); );
// todo create and open new view to display the open ai nemesis result // todo create and open new view to display the open ai nemesis result
// it currently does not pop open the view
this.registerView(VIEW_TYPE_EXAMPLE, (leaf) => new ExampleView(leaf));
// use this class to perform additional things with the ribbon // use this class to perform additional things with the ribbon
// ribbonIconEl.addClass("my-plugin-ribbon-class"); // ribbonIconEl.addClass("my-plugin-ribbon-class");