Skip to content

Commit

Permalink
feat: Can now sort initiatives by descendig or ascending (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Aug 16, 2023
1 parent fefe218 commit 079b59c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export interface InitiativeTrackerData {
diplayPlayerHPValues: boolean;
rollHP: boolean;
builder: BuilderState;

descending: boolean;
version: number[];
}

Expand Down
11 changes: 9 additions & 2 deletions src/tracker/stores/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ function createTracker() {
const $party = writable<string | null>();

const data = writable<InitiativeTrackerData>();
const descending = derived(data, (data) => {
return data.descending;
});
let _settings: InitiativeTrackerData;

const condensed = derived(creatures, (values) => {
Expand All @@ -96,10 +99,12 @@ function createTracker() {
});

let current_order: Creature[] = [];
const ordered = derived(condensed, (values) => {
const ordered = derived([condensed, data], ([values, data]) => {
const sort = [...values];
sort.sort((a, b) => {
return b.initiative - a.initiative;
return data.descending
? b.initiative - a.initiative
: a.initiative - b.initiative;
});
current_order = sort;
return sort;
Expand Down Expand Up @@ -504,6 +509,8 @@ function createTracker() {

name: $name,

sort: descending,

party: $party,
setParty: (party: string, plugin: InitiativeTracker) =>
updateAndSave((creatures) => {
Expand Down
15 changes: 14 additions & 1 deletion src/tracker/ui/Controls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { tracker } from "../stores/tracker";
const { state, data, logFile } = tracker;
const { state, data, logFile, sort } = tracker;
const desktop = Platform.isDesktop;
Expand Down Expand Up @@ -116,6 +116,19 @@
);
});
});
menu.addItem((item) => {
item.setTitle($sort ? "Sort Ascending" : "Sort Descending").onClick(
async () => {
plugin.data.descending = !plugin.data.descending;
await plugin.saveSettings();
item.setTitle(
plugin.data.descending
? "Sort Ascending"
: "Sort Descending"
);
}
);
});
menu.addSeparator();
menu.addItem((item) => {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export const DEFAULT_SETTINGS: InitiativeTrackerData = {
useLegacy: false,
diplayPlayerHPValues: true,
rollHP: false,
descending: true,
builder: {
showParty: true,
showXP: true,
sidebarIcon: true
},
}
};


export const OVERFLOW_TYPE: { [key: string]: string } = {
ignore: "ignore",
current: "current",
Expand All @@ -77,5 +77,5 @@ export const DECIMAL_TO_VULGAR_FRACTION: Record<string, string> = {
0.5: "½",
0.625: "⅝",
0.75: "¾",
0.875: "⅞",
0.875: "⅞"
} as const;

0 comments on commit 079b59c

Please sign in to comment.