Skip to content

Commit

Permalink
Move optional prompt into oracle node
Browse files Browse the repository at this point in the history
  • Loading branch information
cwegrzyn committed May 22, 2024
1 parent b6619a4 commit 8fe88de
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
34 changes: 14 additions & 20 deletions src/mechanics/node-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,19 @@ export function createClockNode(
});
}

export function createOracleNodes(
rootRoll: RollWrapper,
prompt?: string,
): kdl.Node[] {
function oracleNode(roll: RollWrapper): kdl.Node {
return node("oracle", {
properties: {
name: `[${oracleNameWithParents(roll.oracle)}](oracle:${roll.oracle.id})`,
// TODO: this is preposterous
roll: roll.roll.roll,
result: roll.ownResult,
},
children: Object.values(roll.subrolls)
export function createOracleNode(roll: RollWrapper, prompt?: string): kdl.Node {
return node("oracle", {
properties: {
name: `[${oracleNameWithParents(roll.oracle)}](oracle:${roll.oracle.id})`,
// TODO: this is preposterous
roll: roll.roll.roll,
result: roll.ownResult,
},
children: [
...(prompt ? [node("-", { values: [prompt] })] : []),
...Object.values(roll.subrolls)
.flatMap((subroll) => subroll.rolls)
.map((subroll) => oracleNode(subroll)),
});
}
return [
...(prompt ? [node("-", { values: [prompt] })] : []),
oracleNode(rootRoll),
];
.map((subroll) => createOracleNode(subroll)),
],
});
}
20 changes: 2 additions & 18 deletions src/oracles/command.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { createOrAppendMechanics } from "mechanics/editor";
import { createOracleNodes } from "mechanics/node-builders";
import { createOracleNode } from "mechanics/node-builders";
import {
EditorSelection,
stringifyYaml,
type App,
type Editor,
type MarkdownView,
Expand All @@ -13,21 +12,6 @@ import { RollWrapper } from "../model/rolls";
import { CustomSuggestModal } from "../utils/suggest";
import { OracleRollerModal } from "./modal";
import { OracleRoller } from "./roller";
import { type OracleSchema } from "./schema";

export function formatOracleBlock({
question,
roll,
}: {
question?: string;
roll: RollWrapper;
}): string {
const oracle: OracleSchema = {
question,
roll: roll.dehydrate(),
};
return `\`\`\`oracle\n${stringifyYaml(oracle)}\`\`\`\n\n`;
}

export function formatOraclePath(oracle: Oracle): string {
let current = oracle.parent;
Expand Down Expand Up @@ -127,7 +111,7 @@ export async function runOracleCommand(
// Delete the prompt and then inject the oracle node to a mechanics block
editor.setSelection(replaceSelection.anchor, replaceSelection.head);
editor.replaceSelection("");
createOrAppendMechanics(editor, createOracleNodes(roll, prompt));
createOrAppendMechanics(editor, [createOracleNode(roll, prompt)]);
},
() => {},
).open();
Expand Down
16 changes: 15 additions & 1 deletion src/oracles/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
MarkdownRenderer,
MarkdownView,
parseYaml,
stringifyYaml,
type App,
type MarkdownPostProcessorContext,
type Plugin,
} from "obsidian";
import { Oracle, OracleGroupingType } from "../model/oracle";
import { RollWrapper } from "../model/rolls";
import { formatOracleBlock } from "./command";
import { OracleRoller } from "./roller";
import { oracleSchema, type OracleSchema, type RollSchema } from "./schema";

Expand Down Expand Up @@ -183,3 +183,17 @@ class OracleMarkdownRenderChild extends MarkdownRenderChild {
);
}
}

export function formatOracleBlock({
question,
roll,
}: {
question?: string;
roll: RollWrapper;
}): string {
const oracle: OracleSchema = {
question,
roll: roll.dehydrate(),
};
return `\`\`\`oracle\n${stringifyYaml(oracle)}\`\`\`\n\n`;
}

0 comments on commit 8fe88de

Please sign in to comment.