Skip to content

Commit 6cefa46

Browse files
committed
Address feedback round
1 parent 12e406e commit 6cefa46

1 file changed

Lines changed: 34 additions & 20 deletions

File tree

main.ts

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Editor, Plugin, WorkspaceLeaf } from "obsidian";
2-
import { AI21Settings, getAI21Completion } from "src/models/ai21";
2+
import { getAI21Completion } from "src/models/ai21";
33
import { getCohereCompletion } from "src/models/cohere";
4-
import { getGPT3Completion, GPT3Settings } from "src/models/gpt3";
4+
import { getGPT3Completion } from "src/models/gpt3";
55
import {
66
gettingCompletionNotice,
77
errorGettingCompletionNotice,
@@ -18,7 +18,6 @@ import SettingsItemView from "src/ui/SettingsItemView";
1818

1919
export default class GPTPlugin extends Plugin {
2020
settings: GPTPluginSettings;
21-
private view: SettingsItemView;
2221

2322
getSelectedText(editor: Editor) {
2423
let selectedText: string;
@@ -136,17 +135,40 @@ export default class GPTPlugin extends Plugin {
136135
});
137136
}
138137

138+
ensureLeafExists(active: boolean = false): void {
139+
let { workspace } = this.app;
140+
141+
let preferredSidebar = "right";
142+
143+
let leaf: WorkspaceLeaf;
144+
let existingPluginLeaves = workspace.getLeavesOfType(
145+
VIEW_TYPE_MODEL_SETTINGS
146+
);
147+
148+
if (existingPluginLeaves.length > 0) {
149+
leaf = existingPluginLeaves[0];
150+
} else {
151+
leaf =
152+
preferredSidebar === "left"
153+
? workspace.getLeftLeaf(false)
154+
: workspace.getRightLeaf(false);
155+
workspace.revealLeaf(leaf);
156+
leaf.setViewState({ type: VIEW_TYPE_MODEL_SETTINGS });
157+
}
158+
159+
if (active) {
160+
workspace.setActiveLeaf(leaf);
161+
}
162+
}
163+
139164
async onload() {
140165
await this.loadSettings();
141166

142167
this.registerView(
143168
VIEW_TYPE_MODEL_SETTINGS,
144-
(leaf: WorkspaceLeaf) =>
145-
(this.view = new SettingsItemView(leaf, this.settings, this))
169+
(leaf: WorkspaceLeaf) => new SettingsItemView(leaf, this.settings, this)
146170
);
147171

148-
this.initLeaf();
149-
150172
this.addCommand({
151173
id: "get-completion",
152174
name: "Get Completion",
@@ -156,24 +178,16 @@ export default class GPTPlugin extends Plugin {
156178
this.addCommand({
157179
id: "show-model-settings",
158180
name: "Show Model Settings",
159-
checkCallback: (checking: boolean) => {
160-
if (checking) {
161-
return (
162-
this.app.workspace.getLeavesOfType(VIEW_TYPE_MODEL_SETTINGS)
163-
.length === 0
164-
);
165-
}
166-
this.initLeaf();
181+
callback: () => {
182+
this.ensureLeafExists(true);
167183
},
168184
});
169185

170186
this.addSettingTab(new GPTSettingTab(this.app, this));
171-
}
172187

173-
onunload(): void {
174-
this.app.workspace
175-
.getLeavesOfType(VIEW_TYPE_MODEL_SETTINGS)
176-
.forEach((leaf) => leaf.detach());
188+
this.app.workspace.onLayoutReady(() => {
189+
this.ensureLeafExists(false);
190+
});
177191
}
178192

179193
async loadSettings() {

0 commit comments

Comments
 (0)