diff --git a/index.html b/index.html index 85e2b54..8326768 100644 --- a/index.html +++ b/index.html @@ -14,12 +14,7 @@ 鸡音盒 -
- - - - -
+
diff --git a/public/beautiful.opus b/public/one/beautiful.opus similarity index 100% rename from public/beautiful.opus rename to public/one/beautiful.opus diff --git a/public/chicken.opus b/public/one/chicken.opus similarity index 100% rename from public/chicken.opus rename to public/one/chicken.opus diff --git a/public/so.opus b/public/one/so.opus similarity index 100% rename from public/so.opus rename to public/one/so.opus diff --git a/public/you.opus b/public/one/you.opus similarity index 100% rename from public/you.opus rename to public/one/you.opus diff --git a/public/two/basketball.opus b/public/two/basketball.opus new file mode 100644 index 0000000..90095d1 Binary files /dev/null and b/public/two/basketball.opus differ diff --git a/public/two/jump.opus b/public/two/jump.opus new file mode 100644 index 0000000..b9813c6 Binary files /dev/null and b/public/two/jump.opus differ diff --git a/public/two/rap.opus b/public/two/rap.opus new file mode 100644 index 0000000..4ae4d6b Binary files /dev/null and b/public/two/rap.opus differ diff --git a/public/two/sing.opus b/public/two/sing.opus new file mode 100644 index 0000000..9aa80e0 Binary files /dev/null and b/public/two/sing.opus differ diff --git a/src/main.ts b/src/main.ts index ef0f981..02d2e74 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,11 @@ +type Button = { + id: string; + text: string; + key: string; + code: string; + file: string; +}; + const audioCache: Record = {}; const audioPlay = async (url: string) => { @@ -17,29 +25,93 @@ const audioPlay = async (url: string) => { source.start(); }; -const chicken = document.querySelector("#chicken")! as HTMLButtonElement; -const you = document.querySelector("#you")! as HTMLButtonElement; -const so = document.querySelector("#so")! as HTMLButtonElement; -const beautiful = document.querySelector("#beautiful")! as HTMLButtonElement; +const buttons: Button[][] = [ + [ + { + id: "chicken", + text: "鸡", + key: "1", + code: "Digit1", + file: "/one/chicken.opus", + }, + { + id: "you", + text: "你", + key: "2", + code: "Digit2", + file: "/one/you.opus", + }, + { + id: "so", + text: "太", + key: "3", + code: "Digit3", + file: "/one/so.opus", + }, + { + id: "beautiful", + text: "美", + key: "4", + code: "Digit4", + file: "/one/beautiful.opus", + }, + ], + [ + { + id: "sing", + text: "唱", + key: "Q", + code: "KeyQ", + file: "/two/sing.opus", + }, + { + id: "jump", + text: "跳", + key: "W", + code: "KeyW", + file: "/two/jump.opus", + }, + { + id: "rap", + text: "rap", + key: "E", + code: "KeyE", + file: "/two/rap.opus", + }, + { + id: "basketball", + text: "篮球", + key: "R", + code: "KeyR", + file: "/two/basketball.opus", + }, + ], +]; -chicken.onclick = () => audioPlay("/chicken.opus"); -you.onclick = () => audioPlay("/you.opus"); -so.onclick = () => audioPlay("/so.opus"); -beautiful.onclick = () => audioPlay("/beautiful.opus"); +buttons.forEach((row) => { + const container = document.querySelector(`#cxk-container`) as HTMLDivElement; + const buttonGroup = document.createElement("div"); -document.onkeydown = function (e) { - switch (e.key) { - case "1": - audioPlay("/chicken.opus"); - break; - case "2": - audioPlay("/you.opus"); - break; - case "3": - audioPlay("/so.opus"); - break; - case "4": - audioPlay("/beautiful.opus"); - break; + container.appendChild(buttonGroup); + + row.forEach(({ id, text, key, file }) => { + const button = document.createElement("button"); + button.type = "button"; + button.id = id; + button.textContent = text; + button.onclick = () => audioPlay(file); + + const buttonKey = document.createElement("p"); + buttonKey.textContent = key; + + buttonGroup.appendChild(button); + button.appendChild(buttonKey); + }); +}); + +document.onkeydown = (e) => { + const button = buttons.flat().find((b) => b.code === e.code); + if (button) { + audioPlay(button.file); } }; diff --git a/src/style.css b/src/style.css index 4708f28..ae06b25 100644 --- a/src/style.css +++ b/src/style.css @@ -3,13 +3,19 @@ @tailwind utilities; @layer components { - .chicken-button { - @apply relative py-3 px-4 inline-flex items-center gap-x-2 -ms-px first:rounded-s-lg first:ms-0 last:rounded-e-lg text-sm font-medium focus:z-10 border border-gray-200 bg-white text-gray-800 shadow-sm hover:bg-gray-50 focus:outline-none disabled:opacity-50 disabled:pointer-events-none dark:bg-neutral-900 dark:border-neutral-700 dark:text-white dark:hover:bg-neutral-800; - counter-increment: index; + #cxk-container { + @apply flex flex-col gap-2; } - .chicken-button:before { + #cxk-container > * { + @apply inline-flex rounded-lg shadow-sm; + } + + #cxk-container > * > * { + @apply relative h-12 w-16 justify-center inline-flex items-center gap-x-2 -ms-px first:rounded-s-lg first:ms-0 last:rounded-e-lg text-sm font-medium focus:z-10 border border-gray-200 bg-white text-gray-800 shadow-sm hover:bg-gray-50 focus:outline-none disabled:opacity-50 disabled:pointer-events-none dark:bg-neutral-900 dark:border-neutral-700 dark:text-white dark:hover:bg-neutral-800; + } + + #cxk-container > * > * > * { @apply absolute top-1 left-1 text-xs text-neutral-500; - content: counter(index); } }