Skip to content

Commit

Permalink
Delete Pinyin & Pitch labeler (#2)
Browse files Browse the repository at this point in the history
* Delete PinYin

* Delete More
  • Loading branch information
AnyaCoder authored Apr 27, 2024
1 parent 7ad3536 commit a4ca976
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 438 deletions.
59 changes: 11 additions & 48 deletions src/components/AudioLabeler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import AudioPlayer from "react-h5-audio-player";
import "react-h5-audio-player/lib/styles.css";
import PaperWithPadding from "./PaperWithPadding";
import { useEffect, useState } from "react";
import pinyin from "pinyin";

type AudioLabelerProps = {
audio: string;
text: string;
pinYin: string;
onPrev: (text: string, pinYin: string) => void;
onNext: (text: string, pinYin: string) => void;
onSave: (text: string, pinYin: string) => void;
onPrev: (text: string) => void;
onNext: (text: string) => void;
onSave: (text: string) => void;
onDelete: () => void;
};

Expand All @@ -38,24 +36,20 @@ const AudioLabeler = ({
onSave,
onDelete,
text,
pinYin,
}: AudioLabelerProps) => {
const [autoPlay, setAutoPlay] = useState(true);
const [autoPinYin, setAutoPinYin] = useState(true);
const [autoNext, setAutoNext] = useState(true);
const [tempText, setTempText] = useState(text);
const [tempPinYin, setTempPinYin] = useState(pinYin);

useEffect(() => {
setTempText(text);
setTempPinYin(pinYin);
}, [text, pinYin]);
}, [text]);

useEffect(() => {
const handleKeyPress = (event: any) => {
if (autoNext && event.keyCode === 13 && !!tempPinYin) {
if (autoNext && event.keyCode === 13) {
event.preventDefault();
onNext(tempText, tempPinYin);
onNext(tempText);
}

// Ignore Enter
Expand All @@ -66,7 +60,7 @@ const AudioLabeler = ({
// If Command + S is pressed
if (event.keyCode === 83 && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
onSave(tempText, tempPinYin);
onSave(tempText);
}
};

Expand All @@ -77,12 +71,11 @@ const AudioLabeler = ({
return () => {
document.removeEventListener("keydown", handleKeyPress);
};
}, [tempText, tempPinYin, autoNext, onSave, onNext]);
}, [tempText, autoNext, onSave, onNext]);

useEffect(() => {
setAutoNext(getBooleanFromLocalStorage("autoNext", true));
setAutoPlay(getBooleanFromLocalStorage("autoPlay", true));
setAutoPinYin(getBooleanFromLocalStorage("autoPinYin", true));
}, []);

const onUpdateAutoPlay = (e: any) => {
Expand All @@ -92,13 +85,6 @@ const AudioLabeler = ({
setAutoPlay(e.target.checked);
};

const onUpdateAutoPinYin = (e: any) => {
if (typeof window !== "undefined") {
localStorage.setItem("autoPinYin", e.target.checked);
}
setAutoPinYin(e.target.checked);
};

const onUpdateAutoNext = (e: any) => {
if (typeof window !== "undefined") {
localStorage.setItem("autoNext", e.target.checked);
Expand All @@ -109,13 +95,6 @@ const AudioLabeler = ({
const onUpdateText = (e: any) => {
setTempText(e.target.value);

if (autoPinYin) {
const pinyinResult = pinyin(e.target.value, {
style: pinyin.STYLE_NORMAL,
});

setTempPinYin(pinyinResult.map((item) => item[0]).join(" "));
}
};

const disabled = !audio;
Expand All @@ -137,12 +116,6 @@ const AudioLabeler = ({
onChange={onUpdateAutoPlay}
disabled={disabled}
/>
<FormControlLabel
control={<Switch checked={autoPinYin} />}
label="更新拼音"
onChange={onUpdateAutoPinYin}
disabled={disabled}
/>
<FormControlLabel
control={<Switch checked={autoNext} />}
label="回车自动下一个"
Expand All @@ -162,38 +135,28 @@ const AudioLabeler = ({
value={tempText}
disabled={disabled}
/>

<TextField
label="拼音 (自动转换)"
multiline
fullWidth
rows={4}
value={tempPinYin}
onChange={(e) => setTempPinYin(e.target.value)}
disabled={disabled}
/>
</Stack>
</PaperWithPadding>
<PaperWithPadding>
<Stack spacing={2} direction="row">
<Button
variant="outlined"
fullWidth
onClick={() => onPrev(tempText, tempPinYin)}
onClick={() => onPrev(tempText)}
>
上一个
</Button>
<Button
variant="outlined"
fullWidth
onClick={() => onNext(tempText, tempPinYin)}
onClick={() => onNext(tempText)}
>
下一个 {autoNext ? "(Enter)" : ""}
</Button>
<Button
variant="outlined"
fullWidth
onClick={() => onSave(tempText, tempPinYin)}
onClick={() => onSave(tempText)}
>
保存 (Ctrl + S)
</Button>
Expand Down
15 changes: 6 additions & 9 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export default function Home() {
setSelected(file);
};

const onPrev = async (text: string, pinYin: string) => {
await onSave(text, pinYin);
const onPrev = async (text: string) => {
await onSave(text);

const index = files.findIndex((f) => f.name === selected?.name);

Expand All @@ -48,8 +48,8 @@ export default function Home() {
}
};

const onNext = async (text: string, pinYin: string) => {
await onSave(text, pinYin);
const onNext = async (text: string) => {
await onSave(text);

const index = files.findIndex((f) => f.name === selected?.name);

Expand All @@ -58,21 +58,20 @@ export default function Home() {
}
};

const onSave = async (text: string, pinYin: string) => {
const onSave = async (text: string) => {
if (!selected) {
return;
}

selected.text = text;
selected.pinYin = pinYin;
selected.labeled = true;

const fileHandle: any = await selected.directoryHandle.getFileHandle(
selected.labelFileName,
{ create: true }
);
const writable = await fileHandle.createWritable();
await writable.write(`${text}\n${pinYin}`);
await writable.write(`${text}`);
await writable.close();

setFiles([...files]);
Expand Down Expand Up @@ -127,7 +126,6 @@ export default function Home() {
<IconButton sx={{ color: "#000" }} onClick={openRepo}>
<GitHubIcon />
</IconButton>
<Link href="/pitch">Pitch Labeler</Link>
</Header>
</Grid>
<Grid xs={12} md={4}>
Expand All @@ -142,7 +140,6 @@ export default function Home() {
<AudioLabeler
audio={audio}
text={selected?.text || ""}
pinYin={selected?.pinYin || ""}
onPrev={onPrev}
onNext={onNext}
onSave={onSave}
Expand Down
Loading

0 comments on commit a4ca976

Please sign in to comment.