From 7bd74e3b59dd1f4c552eb0d5b591928afff5f4fb Mon Sep 17 00:00:00 2001 From: SArpnt Date: Mon, 11 Mar 2024 20:56:28 -0400 Subject: [PATCH] a bit of typing --- src/bytebeat.ts | 16 ++++++++++------ src/library/load.ts | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/bytebeat.ts b/src/bytebeat.ts index 7db83ce..0c73af4 100644 --- a/src/bytebeat.ts +++ b/src/bytebeat.ts @@ -1,6 +1,7 @@ -import { f32ToRound10, isPlainObject, Song } from "./common.ts"; +import { f32ToRound10, isPlainObject, Song, SongMode } from "./common.ts"; import { elements as elementsPromise } from "./dom.ts"; import { fromUrlData, setUrlData } from "./url/mod.ts"; +import type { EditorView } from "@codemirror/view"; const searchParams = new URLSearchParams(location.search); @@ -26,7 +27,10 @@ let byteSample = 0; let isPlaying = false; -let songData = { +let songData: { + sampleRate: number, + mode: SongMode, +} = { sampleRate: 8000, // float32 mode: "Bytebeat", }; @@ -37,7 +41,7 @@ let timeUnit = null; let animationFrameId = null; -let codeEditor = null; +let codeEditor: HTMLTextAreaElement | EditorView | null = null; let elements = null; // TODO remove @@ -236,9 +240,9 @@ function getCodeEditorText(): string { return codeEditor.state.doc.toString(); } } -function setCodeEditorText(value: string) { +function setCodeEditorText(value?: string) { if (codeEditor instanceof Element) { - codeEditor.value = value; + codeEditor.value = value as string; } else { codeEditor.dispatch({ changes: { from: 0, to: codeEditor.state.doc.length, insert: value }, @@ -296,7 +300,7 @@ function getSong(): Song { return { code: getCodeEditorText(), ...songData }; } -function setSong(songData?: Song, play = true) { +function setSong(songData?: Partial, play = true) { let code, sampleRate, mode; if (songData) { ({ code, sampleRate, mode } = songData); diff --git a/src/library/load.ts b/src/library/load.ts index 5b934b3..a170dec 100644 --- a/src/library/load.ts +++ b/src/library/load.ts @@ -10,12 +10,12 @@ function stripInlineEntryToSong(entry: Readonly, codeTy const { sampleRate, mode } = entry; return { code: entry[codeType], sampleRate, mode }; } -function createByteSnippet(text: string, onclick: () => void) { +function createByteSnippet(text: string | undefined, onclick: () => void) { const interactElem = document.createElement("button"); const codeElem = document.createElement("code"); interactElem.title = "Click to play this code"; interactElem.className = "code-button"; - codeElem.innerText = text; + codeElem.innerText = text as string; interactElem.addEventListener("click", onclick); interactElem.append(codeElem); return interactElem;