Skip to content

Commit

Permalink
fix: bug failed to load on first install due to attempting to copy a …
Browse files Browse the repository at this point in the history
…function (fix #728, #746, #749)
  • Loading branch information
chhoumann committed Nov 1, 2024
1 parent 32bcef2 commit ad97d1e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
34 changes: 14 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ export default class QuickAdd extends Plugin {
return this.settings.devMode;
}

const id: string = this.manifest.id,
plugins = this.app.plugins;
void plugins
.disablePlugin(id)
.then(() => plugins.enablePlugin(id));
const id: string = this.manifest.id;
const plugins = this.app.plugins;
void plugins.disablePlugin(id).then(() => plugins.enablePlugin(id));
},
});

Expand All @@ -71,7 +69,7 @@ export default class QuickAdd extends Plugin {
return this.settings.devMode;
}

console.log(`Test QuickAdd (dev)`);
console.log("Test QuickAdd (dev)");

const fn = () => {
new InfiniteAIAssistantCommandSettingsModal({
Expand Down Expand Up @@ -100,16 +98,16 @@ export default class QuickAdd extends Plugin {
ChoiceSuggester.Open(this, this.settings.choices);
});
}

this.addSettingTab(new QuickAddSettingsTab(this.app, this));

this.app.workspace.onLayoutReady(() =>
new StartupMacroEngine(
this.app,
this,
this.settings.macros,
new ChoiceExecutor(this.app, this)
).run()
new ChoiceExecutor(this.app, this),
).run(),
);
this.addCommandsForChoices(this.settings.choices);

Expand All @@ -124,19 +122,17 @@ export default class QuickAdd extends Plugin {

async loadSettings() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}

private addCommandsForChoices(choices: IChoice[]) {
choices.forEach((choice) => this.addCommandForChoice(choice));
for (const choice of choices) {
this.addCommandForChoice(choice);
}
}

public addCommandForChoice(choice: IChoice) {
Expand Down Expand Up @@ -178,7 +174,7 @@ export default class QuickAdd extends Plugin {
private getChoice(
by: "name" | "id",
targetPropertyValue: string,
choices: IChoice[] = this.settings.choices
choices: IChoice[] = this.settings.choices,
): IChoice | null {
for (const choice of choices) {
if (choice[by] === targetPropertyValue) {
Expand All @@ -188,7 +184,7 @@ export default class QuickAdd extends Plugin {
const subChoice = this.getChoice(
by,
targetPropertyValue,
(choice as IMultiChoice).choices
(choice as IMultiChoice).choices,
);
if (subChoice) {
return subChoice;
Expand All @@ -208,9 +204,7 @@ export default class QuickAdd extends Plugin {

return this.app.vault
.getFiles()
.filter((file) =>
file.path.startsWith(this.settings.templateFolderPath)
);
.filter((file) => file.path.startsWith(this.settings.templateFolderPath));
}

private announceUpdate() {
Expand Down
13 changes: 9 additions & 4 deletions src/migrations/setVersionAfterUpdateModalRelease.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { settingsStore } from "src/settingsStore";
import type { Migration } from "./Migrations";

/**
* This was used with v. 0.14.0, which was the release version prior to the update modal release.
* Previously, it set the version to 0.14.0, but now we want to set it to the current version.
* It would otherwise break the plugin for new users.
*/

const setVersionAfterUpdateModalRelease: Migration = {
description:
"Set version to 0.14.0, which is the release version prior to the update modal release.",
description: "Set version to the current plugin version.",
// eslint-disable-next-line @typescript-eslint/require-await
migrate: async (_) => {
settingsStore.setState({ version: "0.14.0" });
migrate: async (plugin) => {
settingsStore.setState({ version: plugin.manifest.version });
},
};

Expand Down
19 changes: 6 additions & 13 deletions src/settingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ import { DEFAULT_SETTINGS } from "./quickAddSettingsTab";
import type { IMacro } from "./types/macros/IMacro";
import { QuickAddMacro } from "./types/macros/QuickAddMacro";

// Define the state shape and actions for your store.
type SettingsState = QuickAddSettings & {
setSettings: (settings: Partial<QuickAddSettings>) => void;
};

export const settingsStore = (function () {
const useSettingsStore = createStore<SettingsState>((set, get) => ({
...DEFAULT_SETTINGS,
setSettings: (settings: Partial<QuickAddSettings>) =>
set((state) => ({ ...state, ...settings })),
type SettingsState = QuickAddSettings;

export const settingsStore = (() => {
const useSettingsStore = createStore<SettingsState>((set, _get) => ({
...structuredClone(DEFAULT_SETTINGS),
}));

const { getState, setState, subscribe } = useSettingsStore;
Expand All @@ -24,9 +19,7 @@ export const settingsStore = (function () {
subscribe,
setMacro: (macroId: IMacro["id"], macro: IMacro) => {
setState((state) => {
const macroIdx = state.macros.findIndex(
(m) => m.id === macroId
);
const macroIdx = state.macros.findIndex((m) => m.id === macroId);
if (macroIdx === -1) {
throw new Error("Macro not found");
}
Expand Down

0 comments on commit ad97d1e

Please sign in to comment.