Pass app object from plugin instance to child objects and functions

Do not reference global app object from child objects and funcs
directly.

It is only available for debugging purposes and access to it maybe
dropped in the future.
This commit is contained in:
Debanjum Singh Solanky 2023-01-26 16:58:51 -03:00
parent c203c6a3fd
commit 4070be637c
3 changed files with 10 additions and 8 deletions

View file

@ -49,11 +49,11 @@ export default class Khoj extends Plugin {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
// Load, configure khoj server settings // Load, configure khoj server settings
await configureKhojBackend(this.settings); await configureKhojBackend(this.app.vault, this.settings);
} }
async saveSettings() { async saveSettings() {
await configureKhojBackend(this.settings, false) await configureKhojBackend(this.app.vault, this.settings, false)
.then(() => this.saveData(this.settings)); .then(() => this.saveData(this.settings));
} }
} }

View file

@ -10,9 +10,11 @@ export class KhojModal extends SuggestModal<SearchResult> {
setting: KhojSetting; setting: KhojSetting;
rerank: boolean = false; rerank: boolean = false;
find_similar_notes: boolean; find_similar_notes: boolean;
app: App;
constructor(app: App, setting: KhojSetting, find_similar_notes: boolean = false) { constructor(app: App, setting: KhojSetting, find_similar_notes: boolean = false) {
super(app); super(app);
this.app = app;
this.setting = setting; this.setting = setting;
this.find_similar_notes = find_similar_notes; this.find_similar_notes = find_similar_notes;

View file

@ -1,16 +1,16 @@
import { FileSystemAdapter, Notice, RequestUrlParam, request } from 'obsidian'; import { FileSystemAdapter, Notice, RequestUrlParam, request, Vault } from 'obsidian';
import { KhojSetting } from 'src/settings' import { KhojSetting } from 'src/settings'
export function getVaultAbsolutePath(): string { export function getVaultAbsolutePath(vault: Vault): string {
let adaptor = this.app.vault.adapter; let adaptor = vault.adapter;
if (adaptor instanceof FileSystemAdapter) { if (adaptor instanceof FileSystemAdapter) {
return adaptor.getBasePath(); return adaptor.getBasePath();
} }
return ''; return '';
} }
export async function configureKhojBackend(setting: KhojSetting, notify: boolean = true) { export async function configureKhojBackend(vault: Vault, setting: KhojSetting, notify: boolean = true) {
let mdInVault = `${getVaultAbsolutePath()}/**/*.md`; let mdInVault = `${getVaultAbsolutePath(vault)}/**/*.md`;
let khojConfigUrl = `${setting.khojUrl}/api/config/data`; let khojConfigUrl = `${setting.khojUrl}/api/config/data`;
// Check if khoj backend is configured, note if cannot connect to backend // Check if khoj backend is configured, note if cannot connect to backend
@ -28,7 +28,7 @@ export async function configureKhojBackend(setting: KhojSetting, notify: boolean
if (!setting.connectedToBackend) return; if (!setting.connectedToBackend) return;
// Set index name from the path of the current vault // Set index name from the path of the current vault
let indexName = getVaultAbsolutePath().replace(/\//g, '_').replace(/ /g, '_'); let indexName = getVaultAbsolutePath(vault).replace(/\//g, '_').replace(/ /g, '_');
// Get default index directory from khoj backend // Get default index directory from khoj backend
let khojDefaultIndexDirectory = await request(`${khojConfigUrl}/default`) let khojDefaultIndexDirectory = await request(`${khojConfigUrl}/default`)
.then(response => JSON.parse(response)) .then(response => JSON.parse(response))