chore: clean up

This commit is contained in:
hypercross 2026-02-28 17:44:38 +08:00
parent 80471e8dd2
commit 1f5ee24a0f
6 changed files with 60 additions and 89 deletions

View File

@ -18,11 +18,3 @@ export { FileTreeNode, HeadingNode } from './FileTree';
export type { DiceProps } from './md-dice'; export type { DiceProps } from './md-dice';
export type { TableProps } from './md-table'; export type { TableProps } from './md-table';
export type { BgProps } from './md-bg'; export type { BgProps } from './md-bg';
export type {
MdCommanderProps,
MdCommanderCommand,
MdCommanderOption,
MdCommanderOptionType,
CommanderEntry,
CompletionItem,
} from './md-commander';

View File

@ -1,4 +1,4 @@
export { useCommander, defaultCommands, parseInput, getCompletions, getResultClass } from './useCommander'; export { useCommander, defaultCommands, parseInput, getCompletions, getResultClass } from './useCommander';
export type { UseCommanderReturn } from './useCommander'; export type { UseCommanderReturn } from './useCommander';
export { useDiceRoller } from './useDiceRoller'; export { rollFormula, rollSimple } from './useDiceRoller';
export * from './dice-engine'; export * from './dice-engine';

View File

@ -4,7 +4,7 @@ import {
CommanderEntry, CommanderEntry,
CompletionItem, CompletionItem,
} from "../types"; } from "../types";
import { useDiceRoller } from "./useDiceRoller"; import { rollSimple } from "./useDiceRoller";
// ==================== 默认命令 ==================== // ==================== 默认命令 ====================
@ -57,7 +57,6 @@ export const defaultCommands: Record<string, MdCommanderCommand> = {
], ],
handler: (args) => { handler: (args) => {
const formula = args.params.formula || "1d6"; const formula = args.params.formula || "1d6";
const { rollSimple } = useDiceRoller();
const result = rollSimple(formula); const result = rollSimple(formula);
return { return {
message: result.text, message: result.text,

View File

@ -19,69 +19,62 @@ export interface DiceRollerResult {
* Hook * Hook
* *
*/ */
export function useDiceRoller() { /**
/** *
* * @param formula
* @param formula * - 3d6: 标准骰子
* - 3d6: 标准骰子 * - {d4, d8, 2d6}:
* - {d4, d8, 2d6}: * - kh2/dl1: 修饰符
* - kh2/dl1: 修饰符 * - +:
* - +: * - -:
* - -: * - 5: 固定数字
* - 5: 固定数字 */
*/ export function rollFormula(formula: string): DiceRollerResult {
function roll(formula: string): DiceRollerResult { try {
try { const result = rollDice(formula);
const result = rollDice(formula);
return { return {
formula, formula,
result: { result: {
total: result.total, total: result.total,
detail: result.detail, detail: result.detail,
pools: result.pools.map((pool) => ({ pools: result.pools.map((pool) => ({
rolls: pool.rolls.map((r) => r.value), rolls: pool.rolls.map((r) => r.value),
keptRolls: pool.keptRolls.map((r) => r.value), keptRolls: pool.keptRolls.map((r) => r.value),
subtotal: pool.subtotal, subtotal: pool.subtotal,
})), })),
}, },
success: true, success: true,
}; };
} catch (e) { } catch (e) {
return { return {
formula, formula,
result: { result: {
total: 0, total: 0,
detail: "", detail: "",
pools: [], pools: [],
}, },
success: false, success: false,
error: e instanceof Error ? e.message : String(e), error: e instanceof Error ? e.message : String(e),
}; };
} }
}
/**
* HTML
* [1] [2] [3] = <strong>6</strong>
*/
export function rollSimple(formula: string): { text: string; isHtml?: boolean } {
try {
const result = rollDice(formula);
return {
text: result.detail,
isHtml: true, // detail 包含 HTML
};
} catch (e) {
return {
text: `错误:${e instanceof Error ? e.message : String(e)}`,
isHtml: false,
};
} }
/**
* HTML
* [1] [2] [3] = <strong>6</strong>
*/
function rollSimple(formula: string): { text: string; isHtml?: boolean } {
try {
const result = rollDice(formula);
return {
text: result.detail,
isHtml: true, // detail 包含 HTML
};
} catch (e) {
return {
text: `错误:${e instanceof Error ? e.message : String(e)}`,
isHtml: false,
};
}
}
return {
roll,
rollSimple,
};
} }

View File

@ -1,13 +0,0 @@
export { CommanderInput } from './CommanderInput';
export type { CommanderInputProps } from './CommanderInput';
export { CommanderEntries } from './CommanderEntries';
export type { CommanderEntriesProps } from './CommanderEntries';
export type {
MdCommanderProps,
MdCommanderCommand,
MdCommanderOption,
MdCommanderParameter,
MdCommanderOptionType,
CommanderEntry,
CompletionItem,
} from './types';

View File

@ -1,6 +1,6 @@
import { customElement, noShadowDOM } from "solid-element"; import { customElement, noShadowDOM } from "solid-element";
import { onMount, onCleanup } from "solid-js"; import { onMount, onCleanup } from "solid-js";
import { useCommander } from "./hooks/useCommander"; import { useCommander } from "./hooks";
import { CommanderInput } from "./CommanderInput"; import { CommanderInput } from "./CommanderInput";
import { CommanderEntries } from "./CommanderEntries"; import { CommanderEntries } from "./CommanderEntries";
import type { MdCommanderProps } from "./types"; import type { MdCommanderProps } from "./types";