From 23876844e9db212257f89c22d755a12b93370c20 Mon Sep 17 00:00:00 2001 From: norman-andrians Date: Sun, 17 Mar 2024 22:01:38 +0700 Subject: [PATCH] Add export import piezo music editor system --- .../landing/ControlPiezoMusicEditor.tsx | 100 +++++++++++++----- 1 file changed, 72 insertions(+), 28 deletions(-) diff --git a/src/components/landing/ControlPiezoMusicEditor.tsx b/src/components/landing/ControlPiezoMusicEditor.tsx index 7a36e47..7a60d53 100644 --- a/src/components/landing/ControlPiezoMusicEditor.tsx +++ b/src/components/landing/ControlPiezoMusicEditor.tsx @@ -1,4 +1,4 @@ -import { ChangeEvent, MouseEventHandler, useEffect, useState } from "react"; +import { ChangeEvent, useState } from "react"; import { usePiezoMusic } from "../../hooks"; import { PiezoMusic } from "../../types/board"; import EvoInput from "../forms/EvoInput"; @@ -48,7 +48,7 @@ function NoteItem ({note, index, parentIndex}: { note: string, index: number, pa return ( <> {dropAppear == true && item.value == piezo.beats); const exportJson = () => { + const filename = "piezo-music_" + piezo.name + .toLocaleLowerCase() + .split("") + .map(char => char == " " ? "-" : char) + .join("") + ".json" + + const json = JSON.stringify(piezo, null, 4); + const blob = new Blob([json], { + type: "application/json; charset=utf-8" + }); + const url = URL.createObjectURL(blob); + const link = document.createElement('a'); + + link.setAttribute("href", url); + link.setAttribute("download", filename); + + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); } const handle = { @@ -230,37 +249,62 @@ function PiezoEditor ({ piezo, index }: { piezo: PiezoMusic, index: number }) { ) } -function BarPlus ({ onClick }: { onClick?: MouseEventHandler }) { - return ( - - ) -} - -function ControlPiezoMusicEditor () { - const { piezeNotes, addPiezo } = usePiezoMusic(); +function PiezoBarPlus () { + const { addPiezo } = usePiezoMusic(); const handleAdd = (): void => { - let anopin = 13; - for (let i = 0; i < piezeNotes.length; i++) { - if (piezeNotes.filter(piezo => piezo.pin == anopin).length > 0) { - anopin--; - } - else break; - } addPiezo({ - name: "test", - pin: anopin, + name: "", + pin: 11, notes: [], beats: 1/4, tempo: 100 }); } + const handleImport = (e: ChangeEvent): void => { + const file: File | null = e.target.files ? e.target.files[0] : null; + + if (!file) return; + + const reader = new FileReader(); + reader.onload = (event: ProgressEvent) => { + const result = event.target ? event.target.result : null; + + if (result && typeof result == "string") { + const data: PiezoMusic = JSON.parse(result); + const isValidFormat: boolean = data.name != null && data.pin != null && data.notes != null && data.beats != null && data.tempo != null; + + if (isValidFormat) addPiezo(data); + } + } + reader.readAsText(file); + } + + return ( +
+ + +
+ ) +} + +function ControlPiezoMusicEditor () { + const { piezeNotes } = usePiezoMusic(); + return (
@@ -278,7 +322,7 @@ function ControlPiezoMusicEditor () { key={i} /> ))} - +