ttrpg-tools/src/components/md-dice.tsx

102 lines
2.9 KiB
TypeScript
Raw Normal View History

2026-02-26 09:08:05 +08:00
import { customElement, noShadowDOM } from "solid-element";
import { createSignal, onMount } from "solid-js";
2026-02-28 17:46:41 +08:00
import {rollFormula} from "./md-commander/hooks";
2026-02-26 00:17:23 +08:00
2026-02-27 12:34:55 +08:00
export interface DiceProps {
key?: string;
}
2026-02-26 09:01:47 +08:00
// 从 URL 参数获取骰子结果
function getDiceResultFromUrl(key: string): number | null {
2026-02-26 09:08:05 +08:00
if (typeof window === "undefined") return null;
2026-02-26 09:01:47 +08:00
const params = new URLSearchParams(window.location.search);
const value = params.get(`dice-${key}`);
return value ? parseInt(value) : null;
2026-02-26 00:17:23 +08:00
}
2026-02-26 09:18:05 +08:00
function setDiceResultToUrl(key: string, value: number | null) {
if (typeof window === "undefined") return;
const params = new URLSearchParams(window.location.search);
if (value === null) {
params.delete(`dice-${key}`);
} else {
params.set(`dice-${key}`, value.toString());
}
const newUrl = `${window.location.pathname}${params.toString() ? `?${params.toString()}` : ""}${window.location.hash}`;
window.history.pushState({}, "", newUrl);
}
2026-02-26 09:08:05 +08:00
customElement("md-dice", { key: "" }, (props, { element }) => {
2026-02-26 00:47:26 +08:00
noShadowDOM();
2026-02-26 00:17:23 +08:00
const [result, setResult] = createSignal<number | null>(null);
const [isRolled, setIsRolled] = createSignal(false);
2026-02-26 09:08:05 +08:00
const [rollDetail, setRollDetail] = createSignal<string>("");
2026-02-26 00:47:26 +08:00
// 从 element 的 textContent 获取骰子公式
2026-02-26 09:08:05 +08:00
const formula = element?.textContent?.trim() || "";
2026-02-26 09:47:21 +08:00
// 隐藏原始文本内容
if (element) {
element.textContent = "";
}
2026-02-26 09:08:05 +08:00
2026-02-26 09:01:47 +08:00
// 使用的 key如果没有提供则使用生成的 ID
2026-02-26 09:08:05 +08:00
const effectiveKey = () => props.key;
2026-02-26 00:47:26 +08:00
2026-02-26 09:01:47 +08:00
onMount(() => {
// 初始化时检查 URL 参数
if (effectiveKey()) {
const urlResult = getDiceResultFromUrl(effectiveKey());
if (urlResult !== null) {
setResult(urlResult);
setIsRolled(true);
}
2026-02-26 00:17:23 +08:00
}
2026-02-26 09:01:47 +08:00
});
const handleRoll = () => {
// 点击骰子图标:总是重 roll
2026-02-28 17:46:41 +08:00
const rollResult = rollFormula(formula).result;
2026-02-26 09:01:47 +08:00
setResult(rollResult.total);
setRollDetail(rollResult.detail);
setIsRolled(true);
2026-02-26 09:18:05 +08:00
if (effectiveKey()) {
setDiceResultToUrl(effectiveKey(), rollResult.total);
}
2026-02-26 09:01:47 +08:00
};
const handleTextClick = () => {
// 点击文本:总是重置为公式
setResult(null);
setIsRolled(false);
2026-02-26 09:08:05 +08:00
setRollDetail("");
2026-02-26 09:18:05 +08:00
if (effectiveKey()) {
setDiceResultToUrl(effectiveKey(), null);
}
2026-02-26 00:17:23 +08:00
};
2026-02-26 00:47:26 +08:00
const displayText = () => (isRolled() ? `${result()}` : formula);
2026-02-26 00:17:23 +08:00
return (
2026-02-26 09:59:45 +08:00
<span class="inline-flex items-center px-1">
<span
2026-02-26 09:01:47 +08:00
onClick={(e) => {
e.preventDefault();
handleRoll();
}}
class="text-blue-600 hover:text-blue-800 cursor-pointer"
2026-02-26 09:08:05 +08:00
title={rollDetail() || "掷骰子"}
2026-02-26 09:01:47 +08:00
>
🎲
2026-02-26 09:59:45 +08:00
</span>
<a
2026-02-26 09:01:47 +08:00
onClick={handleTextClick}
2026-02-26 09:59:45 +08:00
class="cursor-pointer text-gray-700 hover:text-gray-900"
2026-02-26 09:08:05 +08:00
title={isRolled() ? "点击重置为公式" : formula}
2026-02-26 09:01:47 +08:00
>
{displayText()}
2026-02-26 09:59:45 +08:00
</a>
2026-02-26 09:01:47 +08:00
</span>
2026-02-26 00:17:23 +08:00
);
2026-02-26 00:47:26 +08:00
});