Skip to content

Commit

Permalink
fix: Encounter Builder encounters will save between reloads
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed May 9, 2023
1 parent 06b1a45 commit dca66ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/builder/stores/encounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ function createEncounter() {
monsters.set(item, count);
return monsters;
}),
setMultiple: (creatures: [item: SRDMonster, count: number][]) => update((monsters) => {
for (const [item, count] of creatures) {
monsters.set(item, count);
}
return monsters;
}),
empty: () => set(new Map())
};
}
Expand Down
15 changes: 14 additions & 1 deletion src/builder/view.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { ExperienceThreshold } from "index";
import { ItemView, WorkspaceLeaf } from "obsidian";
import { ItemView, ViewStateResult, WorkspaceLeaf } from "obsidian";
import type InitiativeTracker from "src/main";
import { BUILDER_VIEW } from "../utils";

import Builder from "./view/Builder.svelte";
import { encounter } from "./stores/encounter";
import { get } from "svelte/store";
import type { SRDMonster } from "obsidian-overload";

interface BuilderContext {
plugin: InitiativeTracker;
Expand All @@ -24,6 +27,16 @@ export default class BuilderView extends ItemView {
constructor(leaf: WorkspaceLeaf, public plugin: InitiativeTracker) {
super(leaf);
}
getState() {
return [...get(encounter).entries()];
}
async setState(
state: [SRDMonster, number][],
result: ViewStateResult
): Promise<void> {
if (state && Array.isArray(state)) encounter.setMultiple(state);
super.setState(state, result);
}
ui: Builder;
async onOpen() {
this.ui = new Builder({
Expand Down

0 comments on commit dca66ab

Please sign in to comment.