-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: You can now create custom encounter builder headers
- Loading branch information
1 parent
921034a
commit 2c42744
Showing
17 changed files
with
1,353 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import { Updater, derived, get, writable } from "svelte/store"; | ||
import { | ||
type SRDMonster, | ||
SortFunctions, | ||
type TableHeaderState | ||
} from "../../../index"; | ||
import { convertFraction } from "../../utils"; | ||
import type InitiativeTracker from "../../main"; | ||
import { Modal } from "obsidian"; | ||
import copy from "fast-copy"; | ||
import Headers from "./table/Headers.svelte"; | ||
import { getId } from "src/utils/creature"; | ||
|
||
export const playerCount = writable(0); | ||
|
||
export class TableHeader { | ||
public active: boolean; | ||
id = getId(); | ||
constructor( | ||
public text: string, | ||
public field: string, | ||
public type: SortFunctions, | ||
public func?: string | ||
) {} | ||
private _func: (a: SRDMonster, b: SRDMonster) => number = | ||
this.getSortByType(); | ||
private getSortByType() { | ||
switch (this.type) { | ||
case SortFunctions.LOCAL_COMPARE: { | ||
return (a: Record<string, any>, b: Record<string, any>) => | ||
(a[this.field] ?? "").localeCompare(b[this.field] ?? ""); | ||
} | ||
case SortFunctions.CONVERT_FRACTION: { | ||
return (a: Record<string, any>, b: Record<string, any>) => | ||
convertFraction(a[this.field] ?? 0) - | ||
convertFraction(b[this.field] ?? 0); | ||
} | ||
case SortFunctions.CUSTOM: { | ||
return new Function("a", "b", this.func) as ( | ||
a: SRDMonster, | ||
b: SRDMonster | ||
) => number; | ||
} | ||
} | ||
} | ||
|
||
public sortAsc(a: SRDMonster, b: SRDMonster) { | ||
console.log(this._func(a, b)); | ||
return this._func(a, b); | ||
} | ||
public sortDesc(a: SRDMonster, b: SRDMonster) { | ||
console.log(this._func(b, a)); | ||
return this._func(b, a); | ||
} | ||
|
||
public toState(): TableHeaderState { | ||
return { | ||
text: this.text, | ||
field: this.field, | ||
type: this.type, | ||
...(this.func && this.func.length ? { func: this.func } : {}) | ||
}; | ||
} | ||
|
||
public static fromState(state: TableHeaderState): TableHeader { | ||
return new TableHeader(state.text, state.field, state.type, state.func); | ||
} | ||
public static get defaultState() { | ||
return { | ||
text: "", | ||
field: "", | ||
type: SortFunctions.LOCAL_COMPARE | ||
}; | ||
} | ||
} | ||
|
||
export const DEFAULT_HEADERS: TableHeaderState[] = [ | ||
{ | ||
text: "CR", | ||
field: "cr", | ||
type: SortFunctions.CONVERT_FRACTION | ||
}, | ||
{ | ||
text: "Type", | ||
field: "type", | ||
type: SortFunctions.LOCAL_COMPARE | ||
}, | ||
{ | ||
text: "Size", | ||
field: "size", | ||
type: SortFunctions.LOCAL_COMPARE | ||
}, | ||
{ | ||
text: "Alignment", | ||
field: "alignment", | ||
type: SortFunctions.LOCAL_COMPARE | ||
} | ||
]; | ||
|
||
export const NAME_HEADER = TableHeader.fromState({ | ||
text: "Name", | ||
field: "name", | ||
type: SortFunctions.LOCAL_COMPARE | ||
}); | ||
|
||
export function createTable(plugin: InitiativeTracker, monsters: SRDMonster[]) { | ||
if (!plugin.data.builder) { | ||
plugin.data.builder = { | ||
sidebarIcon: true, | ||
showParty: true, | ||
showXP: true | ||
}; | ||
} | ||
if (!plugin.data.builder.headers) { | ||
plugin.data.builder.headers = copy(DEFAULT_HEADERS); | ||
plugin.saveSettings(); | ||
} | ||
|
||
const store = writable<TableHeader[]>( | ||
plugin.data.builder.headers.map((h) => TableHeader.fromState(h)) | ||
); | ||
const { subscribe, set, update } = store; | ||
const creatures = writable<SRDMonster[]>(monsters); | ||
|
||
let sortDir = writable(true); //true == asc, false == des | ||
const allHeaders = derived(store, (headers) => { | ||
return [NAME_HEADER, ...headers]; | ||
}); | ||
const sort = (field: TableHeader) => | ||
update((headers) => { | ||
if (field.active) { | ||
sortDir.update((v) => !v); | ||
} else { | ||
const curr = get(allHeaders).find((h) => h.active); | ||
if (curr) curr.active = false; | ||
field.active = true; | ||
sortDir.set(true); | ||
} | ||
|
||
creatures.update((creatures) => { | ||
creatures.sort( | ||
get(sortDir) | ||
? field.sortAsc.bind(field) | ||
: field.sortDesc.bind(field) | ||
); | ||
return creatures; | ||
}); | ||
return headers; | ||
}); | ||
function updateAndSave(updater: Updater<TableHeader[]>): void { | ||
update(updater); | ||
plugin.saveSettings(); | ||
} | ||
return { | ||
allHeaders, | ||
subscribe, | ||
set, | ||
update, | ||
sort, | ||
sortDir, | ||
creatures, | ||
setHeadersFromState: (headers: TableHeaderState[]) => | ||
updateAndSave((_) => { | ||
plugin.data.builder.headers = headers; | ||
const newHeaders = headers.map((h) => TableHeader.fromState(h)); | ||
const active = _.findIndex((h) => h.active); | ||
if (active >= 0 && active < newHeaders.length) { | ||
newHeaders[active].active = true; | ||
} | ||
return newHeaders; | ||
}), | ||
resetToDefault: () => | ||
updateAndSave((_) => { | ||
plugin.data.builder.headers = copy(DEFAULT_HEADERS); | ||
return plugin.data.builder.headers.map((h) => | ||
TableHeader.fromState(h) | ||
); | ||
}) | ||
}; | ||
} | ||
|
||
export class SettingsModal extends Modal { | ||
canceled: boolean = false; | ||
reset = false; | ||
constructor(public headers: TableHeaderState[]) { | ||
super(app); | ||
} | ||
onOpen() { | ||
this.titleEl.setText("Edit Headers"); | ||
const app = new Headers({ | ||
target: this.contentEl, | ||
props: { | ||
headers: copy(this.headers) | ||
} | ||
}); | ||
app.$on("update", (evt) => { | ||
console.log("🚀 ~ file: table.ts:169 ~ evt:", evt); | ||
this.headers = copy(evt.detail); | ||
}); | ||
app.$on("cancel", () => { | ||
this.canceled = true; | ||
this.close(); | ||
}); | ||
app.$on("reset", () => { | ||
this.reset = true; | ||
this.close(); | ||
}); | ||
} | ||
} |
Oops, something went wrong.