fix: dice url key

This commit is contained in:
hypercross 2026-02-26 09:18:05 +08:00
parent 0a2c756213
commit f596e66983
2 changed files with 18 additions and 5 deletions

View File

@ -10,7 +10,7 @@
:md-dice[2d6]
:md-dice[1d20+d8]
:md-dice[3d6kh1] :md-dice[3d6dl2-1]
:md-dice[3d6+5]{key="attack"}

View File

@ -128,8 +128,17 @@ function getDiceResultFromUrl(key: string): number | null {
return value ? parseInt(value) : null;
}
// TODO:使用history.pushState
function setDiceResultToUrl(key: string, value: number) {}
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);
}
customElement("md-dice", { key: "" }, (props, { element }) => {
noShadowDOM();
@ -160,7 +169,9 @@ customElement("md-dice", { key: "" }, (props, { element }) => {
setResult(rollResult.total);
setRollDetail(rollResult.detail);
setIsRolled(true);
// TODO:保存结果到url
if (effectiveKey()) {
setDiceResultToUrl(effectiveKey(), rollResult.total);
}
};
const handleTextClick = () => {
@ -168,7 +179,9 @@ customElement("md-dice", { key: "" }, (props, { element }) => {
setResult(null);
setIsRolled(false);
setRollDetail("");
// TODO:从url中移除结果
if (effectiveKey()) {
setDiceResultToUrl(effectiveKey(), null);
}
};
const displayText = () => (isRolled() ? `${result()}` : formula);