Skip to content

Commit

Permalink
0.11 small code fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TfTHacker committed Oct 3, 2021
1 parent 503a643 commit 443c403
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
7 changes: 4 additions & 3 deletions src/AddNewPluginModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import BetaPlugins from './BetaPlugins';
import ThePlugin from './main';
import { existBetaPluginInList } from './settings';

// Generic class for capturing a line of text

/**
* Add a beta plugin to the list of plugins being tracked and updated
*/
export default class AddNewPluginModal extends Modal {
plugin: ThePlugin;
betaPlugins: BetaPlugins;
Expand All @@ -20,7 +21,7 @@ export default class AddNewPluginModal extends Modal {
async submitForm(): Promise<void> {
if (this.address === "") return;
if (await existBetaPluginInList(this.plugin, this.address)) {
new Notice(`This plugin is already in the list for beta testing`, 20000);
new Notice(`BRAT\nThis plugin is already in the list for beta testing`, 20000);
return;
}
const result = await this.betaPlugins.addPlugin(this.address);
Expand Down
23 changes: 13 additions & 10 deletions src/BetaPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ interface ReleaseFiles {
styles: string;
}

/**
* Primary handler for adding, updating, deleting beta plugins tracked by this plugin
*/
export default class BetaPlugins {
plugin: ThePlugin;

Expand Down Expand Up @@ -40,7 +43,7 @@ export default class BetaPlugins {
*
* @return {Promise<PluginManifest>} the manifest file if found, or null if its incomplete
*/
async validateRepository(repositoryPath: string, getBetaManifest: boolean = false, reportIsues: boolean = false): Promise<PluginManifest> {
async validateRepository(repositoryPath: string, getBetaManifest = false, reportIsues = false): Promise<PluginManifest> {
const noticeTimeout = 60000;
const manifestJson = await grabManifestJsonFromRepository(repositoryPath, !getBetaManifest);
if (!manifestJson) { // this is a plugin with a manifest json, try to see if there is a beta version
Expand Down Expand Up @@ -104,7 +107,7 @@ export default class BetaPlugins {
*
* @return {Promise<boolean>} true if succeeds
*/
async addPlugin(repositoryPath: string, updatePluginFiles: boolean = false): Promise<boolean> {
async addPlugin(repositoryPath: string, updatePluginFiles = false): Promise<boolean> {
const manifestJson = await this.validateRepository(repositoryPath, false, true);
const noticeTimeout = 60000;
if (manifestJson === null) return false;
Expand Down Expand Up @@ -135,12 +138,12 @@ export default class BetaPlugins {
try {
localManifestContents = await this.plugin.app.vault.adapter.read(pluginTargetFolderPath + "manifest.json")
} catch (e) {
if (e.errno = -4058) { // file does not exist, try installing the plugin
if (e.errno === -4058) { // file does not exist, try installing the plugin
await this.addPlugin(repositoryPath, false);
return true; // even though failed, return true since install will be attempted
}
}
else
console.log("BRAT - Local Manifest Load", remoteManifestJSON.id, JSON.stringify(e, null, 2));
console.log("BRAT - Local Manifest Load", remoteManifestJSON.id, JSON.stringify(e, null, 2));
}
const localManifestJSON = await JSON.parse(localManifestContents);
if (localManifestJSON.version !== remoteManifestJSON.version) { //manifest files are not the same, do an update
Expand Down Expand Up @@ -188,12 +191,12 @@ export default class BetaPlugins {
* @param {boolean} showInfo should this with a started/completed message - useful when ran from CP
* @return {Promise<void>}
*/
async checkForUpdates(showInfo: boolean = false): Promise<void> {
if (showInfo) new Notice(`BRAT\nChecking for plugin updates STARTED`);
async checkForUpdates(showInfo = false): Promise<void> {
if (showInfo) new Notice(`BRAT\nChecking for plugin updates STARTED`, 30000);
for (const bp of this.plugin.settings.pluginList) {
await this.updatePlugin(bp)
}
if (showInfo) new Notice(`BRAT\nChecking for plugin updates COMPLETED`);
if (showInfo) new Notice(`BRAT\nChecking for plugin updates COMPLETED`, 10000);
}

/**
Expand All @@ -203,8 +206,8 @@ export default class BetaPlugins {
*
* @return {Promise<void>} [return description]
*/
async deletePlugin(repositoryPath:string): Promise<void> {
this.plugin.settings.pluginList = this.plugin.settings.pluginList.filter( (b)=> b != repositoryPath); ;
async deletePlugin(repositoryPath: string): Promise<void> {
this.plugin.settings.pluginList = this.plugin.settings.pluginList.filter((b) => b != repositoryPath);
this.plugin.saveSettings();
}

Expand Down
18 changes: 12 additions & 6 deletions src/GenericFuzzySuggester.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { FuzzySuggestModal, FuzzyMatch } from 'obsidian';
import ThePlugin from '../main';
import ThePlugin from './main';

/**
* Simple interface for what should be displayed and stored for suggester
*/
export interface SuggesterItem {
display: string, // displayed to user
info: any // supplmental info for the callback
}

/**
* Generic suggester for quick reuse
*/
export class GenericFuzzySuggester extends FuzzySuggestModal<SuggesterItem>{
data: SuggesterItem[];
callbackFunction: any;

constructor(plugin: ThePlugin) {
super(plugin.app);
this.scope.register(["Shift"],"Enter", evt => this.enterTrigger(evt));
this.scope.register(["Ctrl"],"Enter", evt => this.enterTrigger(evt));
this.scope.register(["Shift"], "Enter", evt => this.enterTrigger(evt));
this.scope.register(["Ctrl"], "Enter", evt => this.enterTrigger(evt));
}

setSuggesterData(suggesterData: Array<SuggesterItem>): void { this.data = suggesterData }
Expand All @@ -23,7 +29,7 @@ export class GenericFuzzySuggester extends FuzzySuggestModal<SuggesterItem>{
this.open();
}

getItems(): SuggesterItem[] { return this.data }
getItems(): SuggesterItem[] { return this.data }

getItemText(item: SuggesterItem): string { return item.display }

Expand All @@ -33,8 +39,8 @@ export class GenericFuzzySuggester extends FuzzySuggestModal<SuggesterItem>{

enterTrigger(evt: KeyboardEvent): void {
const selectedText = document.querySelector(".suggestion-item.is-selected div").textContent;
const item = this.data.find( i => i.display === selectedText );
if(item) {
const item = this.data.find(i => i.display === selectedText);
if (item) {
this.invokeCallback(item, evt);
this.close();
}
Expand Down
18 changes: 8 additions & 10 deletions src/SettingsTab.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { App, PluginSettingTab, Setting, ToggleComponent, Platform, ButtonComponent } from 'obsidian';
import { App, PluginSettingTab, Setting, ToggleComponent, ButtonComponent } from 'obsidian';
import ThePlugin from './main';
import { Settings, DEFAULT_SETTINGS } from './settings';


export class SettingsTab extends PluginSettingTab {
plugin: ThePlugin;
Expand Down Expand Up @@ -35,18 +33,18 @@ export class SettingsTab extends PluginSettingTab {
containerEl.createEl("div", { text: `Click the x button next to a plugin to remove it from the list.` });
containerEl.createEl("p");
containerEl.createEl("span")
.createEl("b", {text: "Note: "})
containerEl.createSpan( { text: "This does not delete the plugin, this should be done from the Community Plugins tab in Settings." });
for(const bp of this.plugin.settings.pluginList) {
.createEl("b", { text: "Note: " })
containerEl.createSpan({ text: "This does not delete the plugin, this should be done from the Community Plugins tab in Settings." });

for (const bp of this.plugin.settings.pluginList) {
new Setting(containerEl)
.setName(bp)
.addButton( (btn: ButtonComponent)=>{
.addButton((btn: ButtonComponent) => {
btn.setIcon("cross");
btn.setTooltip("Delete this beta plugin");
btn.onClick( async ()=>{
btn.onClick(async () => {
// await this.plugin.betaPlugins.deletePlugin(bp);
if(btn.buttonEl.textContent==="")
if (btn.buttonEl.textContent === "")
btn.setButtonText("Click once more to confirm removal");
else {
btn.buttonEl.parentElement.parentElement.remove();
Expand Down
14 changes: 7 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Notice, Plugin, request } from "obsidian";
import { Notice, Plugin } from "obsidian";
import { SettingsTab } from "./SettingsTab";
import { Settings, DEFAULT_SETTINGS, existBetaPluginInList } from "./settings";
import { Settings, DEFAULT_SETTINGS } from "./settings";
import BetaPlugins from "./BetaPlugins";
import { GenericFuzzySuggester, SuggesterItem } from "./GenericFuzzySuggester";

Expand Down Expand Up @@ -32,21 +32,21 @@ export default class ThePlugin extends Plugin {
this.addCommand({
id: "BRAT-restart plugin",
name: "Restart a plugin that is already installed",
callback: async () => {
callback: async () => {
// @ts-ignore
const pluginList: SuggesterItem[] = Object.values(this.app.plugins.manifests).map( (m)=> { return { display: m.id, info: m.id }} );
const pluginList: SuggesterItem[] = Object.values(this.app.plugins.manifests).map((m) => { return { display: m.id, info: m.id } });
const gfs = new GenericFuzzySuggester(this);
gfs.setSuggesterData(pluginList);
await gfs.display( async (results) => {
await gfs.display(async (results) => {
new Notice(`${results.info}\nPlugin reloading .....`, 5000);
await this.betaPlugins.reloadPlugin(results.info);
});
}
});

this.app.workspace.onLayoutReady((): void => {
if(this.settings.updateAtStartup) // let obsidian load and calm down before check
setTimeout( async () => { await this.betaPlugins.checkForUpdates(false) }, 60000);
if (this.settings.updateAtStartup) // let obsidian load and calm down before check
setTimeout(async () => { await this.betaPlugins.checkForUpdates(false) }, 60000);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function addBetaPluginToList(plugin: ThePlugin, repositoryPath: str
}

/**
* tests if a plugin is in data.json
* Tests if a plugin is in data.json
*
* @param {ThePlugin} plugin
* @param {string<boolean>} repositoryPath path to the GitHub repository
Expand Down

0 comments on commit 443c403

Please sign in to comment.