Skip to content

Commit

Permalink
fix: Fixes issue where sometimes table lookup rollers wouldn't resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Sep 26, 2024
1 parent 5700981 commit d8df63d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ class APIInstance {
}
if (content.includes("|lookup=")) {
[, lookup] = content.match(/\|lookup=(.+?)(?:\||$)/) ?? [];
console.log("🚀 ~ file: api.ts:178 ~ lookup:", lookup);
}

content = decode(
Expand Down
14 changes: 4 additions & 10 deletions src/processor/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ export default class DiceProcessor extends Component {
continue;

//build result map;
const maybeRoller = await API.getRoller(
content,
ctx.sourcePath
);
const maybeRoller = API.getRoller(content, ctx.sourcePath);
if (maybeRoller.isNone()) {
return;
}
Expand Down Expand Up @@ -147,22 +144,19 @@ export default class DiceProcessor extends Component {
}
try {
//build result map;
const maybeRoller = await API.getRoller(
content,
ctx.sourcePath
);
const maybeRoller = API.getRoller(content, ctx.sourcePath);
if (maybeRoller.isNone()) {
return;
}
const roller = maybeRoller.unwrap();
/** Add the roller to the child context, so it can be unloaded with the context. */
roller.addContexts(ctx, this.plugin);
const roller = maybeRoller.unwrap();

roller.onLoad(async () => {
await roller.roll();

node.replaceWith(roller.containerEl);
});
roller.addContexts(ctx, this.plugin);

if (!file || !(file instanceof TFile)) continue;

Expand Down
12 changes: 9 additions & 3 deletions src/roller/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {

import { TABLE_REGEX } from "src/utils/constants";
import { StackRoller } from ".";
import { GenericFileRoller } from "./roller";
import { GenericFileRoller, type ComponentLike } from "./roller";
import { API } from "src/api/api";
import type { DiceRollerSettings } from "src/settings/settings.types";
import type { LexicalToken } from "src/lexer/lexer";
Expand All @@ -30,6 +30,11 @@ export class TableRoller extends GenericFileRoller<string> {
lookupRanges: [range: [min: number, max: number], option: string][];
combinedTooltip: string = "";
prettyTooltip: string = "";
#components: ComponentLike[] = [];
override addContexts(...components: ComponentLike[]): void {
this.#components = components;
super.addContexts(...components);
}
constructor(
data: DiceRollerSettings,
original: string,
Expand Down Expand Up @@ -159,6 +164,7 @@ export class TableRoller extends GenericFileRoller<string> {
continue;
}
const subRoller = maybeRoller.unwrap();
subRoller.addContexts(...this.#components);
// Roll it
await subRoller.roll();
// Get sub result
Expand Down Expand Up @@ -209,6 +215,7 @@ export class TableRoller extends GenericFileRoller<string> {
return "ERROR";
}
const rollsRoller = roller as StackRoller;
rollsRoller.addContexts(...this.#components);
await rollsRoller.roll();
this.rolls = rollsRoller.result;
if (!rollsRoller.isStatic) {
Expand Down Expand Up @@ -353,10 +360,9 @@ export class TableRoller extends GenericFileRoller<string> {
);
if (maybeRoller.isSome()) {
const roller = maybeRoller.unwrap();
roller.addContexts(...this.#components);
if (roller instanceof StackRoller) {
this.lookupRoller = roller;
// TODO: useless roll I think
// let result = await this.lookupRoller.roll();

this.lookupRanges = table.rows.map((row) => {
const [range, option] = row
Expand Down

0 comments on commit d8df63d

Please sign in to comment.