Skip to content

Commit a18f1bb

Browse files
committed
Another bunch of functions converted.
1 parent 453b0d1 commit a18f1bb

29 files changed

Lines changed: 1039 additions & 671 deletions

src/components/ui/About.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*/
55

6-
import logo from "../../assets/images/animada-logo.svg";
6+
import logo from "../../assets/images/animada-logo2.svg";
77
import githubLogo from "../../assets/images/GitHub_Invertocat_Dark.svg";
88

99
import type { ComponentChild } from "preact";

src/components/ui/BananaDrumViewer.tsx

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/components/ui/ExpandingSpacer.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*/
55

6-
/* eslint-disable prefer-arrow/prefer-arrow-functions, @typescript-eslint/naming-convention, jsdoc/require-jsdoc */
6+
import type { ComponentChild } from "preact";
7+
import { ComponentBase } from "./ComponentBase/ComponentBase.js";
78

8-
import type { JSX } from "preact/jsx-runtime";
9-
10-
export function ExpandingSpacer(): JSX.Element {
11-
return <div style={{ flexGrow: 1 }} />;
9+
export class ExpandingSpacer extends ComponentBase {
10+
public override render(): ComponentChild {
11+
return <div style={{ flexGrow: 1 }} />;
12+
}
1213
}

src/components/ui/InstrumentBrowser.tsx

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,28 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*/
55

6-
/* eslint-disable prefer-arrow/prefer-arrow-functions, @typescript-eslint/naming-convention, jsdoc/require-jsdoc */
7-
8-
import { useContext } from "preact/hooks";
9-
10-
import type { JSX } from "preact/jsx-runtime";
11-
import type { ArrangementView, InstrumentMeta } from "../../core/index.js";
6+
import type { ComponentChild } from "preact";
127
import { getLibrary } from "../../core/Library.js";
13-
import { useEditCommand, type EditFunction } from "../../ui/hooks/useEditCommand.js";
14-
import { ArrangementPlayerContext } from "./arrangement/ArrangementViewer.js";
8+
import { ComponentBase, type IComponentProperties } from "./ComponentBase/ComponentBase.js";
9+
import { InstrumentChooser } from "./InstrumentChooser.js";
1510

16-
export function InstrumentBrowser({ close }: { close: () => void; }): JSX.Element {
17-
return (
18-
<div className="viewport-wrapper">
19-
<div style={{ padding: "20pt" }}>
20-
{getLibrary().instrumentMetas.map(meta => {
21-
return <InstrumentChooser key={meta.id} instrumentMeta={meta} close={close} />;
22-
})}
23-
<br />
24-
<br />
25-
<button className="push-button" onClick={close}>Back</button>
26-
</div>
27-
</div>
28-
);
11+
export interface IInstrumentBrowserProps extends IComponentProperties {
12+
close: () => void;
2913
}
3014

31-
function InstrumentChooser(
32-
{ instrumentMeta, close }: { instrumentMeta: InstrumentMeta, close: () => void; }): JSX.Element {
33-
const { id, displayName } = instrumentMeta;
34-
const arrangement: ArrangementView = useContext(ArrangementPlayerContext)!.arrangement;
35-
const edit = useEditCommand();
36-
37-
return (
38-
<button className="instrument-chooser push-button" onClick={() => {
39-
choose(id, arrangement, edit);
40-
close();
41-
}}>
42-
{displayName}
43-
</button>
44-
);
45-
}
46-
47-
function choose(id: string, arrangement: ArrangementView, edit: EditFunction) {
48-
edit({ type: "EditCommand_ArrangementAddTrack", arrangement, addTrack: getLibrary().getInstrument(id) });
15+
export class InstrumentBrowser extends ComponentBase<IInstrumentBrowserProps> {
16+
public render(): ComponentChild {
17+
return (
18+
<div className="viewport-wrapper">
19+
<div style={{ padding: "20pt" }}>
20+
{getLibrary().instrumentMetas.map((meta) => {
21+
return <InstrumentChooser key={meta.id} instrumentMeta={meta} close={close} />;
22+
})}
23+
<br />
24+
<br />
25+
<button className="push-button" onClick={close}>Back</button>
26+
</div>
27+
</div>
28+
);
29+
}
4930
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) Mike Lischke. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*/
5+
6+
import type { ComponentChild, ContextType } from "preact";
7+
8+
import type { ArrangementView, InstrumentMeta } from "../../core/index.js";
9+
import { getLibrary } from "../../core/Library.js";
10+
import { ArrangementPlayerContext } from "./arrangement/ArrangementViewer.js";
11+
import { ComponentBase, type IComponentProperties } from "./ComponentBase/ComponentBase.js";
12+
import { BananaDrumContext } from "./ScoreBookViewer.js";
13+
14+
export interface IInstrumentChooserProps extends IComponentProperties {
15+
instrumentMeta: InstrumentMeta;
16+
close: () => void;
17+
}
18+
19+
export class InstrumentChooser extends ComponentBase<IInstrumentChooserProps> {
20+
private bananaDrumContext?: ContextType<typeof BananaDrumContext>;
21+
22+
public render(): ComponentChild {
23+
const { instrumentMeta } = this.props;
24+
25+
return (
26+
<BananaDrumContext.Consumer>
27+
{(bananaDrumContext) => {
28+
this.bananaDrumContext = bananaDrumContext;
29+
30+
return (
31+
<ArrangementPlayerContext.Consumer>
32+
{(context) => {
33+
const arrangement: ArrangementView = context!.arrangement;
34+
35+
return (
36+
<button
37+
className="instrument-chooser push-button"
38+
onClick={this.buttonClick.bind(this, arrangement)}
39+
>
40+
{instrumentMeta.displayName}
41+
</button>
42+
);
43+
}}
44+
</ArrangementPlayerContext.Consumer>
45+
);
46+
}}
47+
</BananaDrumContext.Consumer>
48+
);
49+
}
50+
51+
private buttonClick(arrangement: ArrangementView) {
52+
const { instrumentMeta, close } = this.props;
53+
54+
this.bananaDrumContext?.edit({
55+
type: "EditCommand_ArrangementAddTrack", arrangement, addTrack: getLibrary()
56+
.getInstrument(instrumentMeta.id)
57+
});
58+
59+
close();
60+
}
61+
}

src/components/ui/NumberInput.tsx

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,103 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*/
55

6-
/* eslint-disable prefer-arrow/prefer-arrow-functions, @typescript-eslint/naming-convention, jsdoc/require-jsdoc */
7-
8-
import { useState } from "preact/hooks";
9-
import type { JSX } from "preact/jsx-runtime";
6+
import type { ComponentChild } from "preact";
107
import type { Subscribable } from "../../core/types/general.js";
11-
import { useSubscription } from "../../ui/hooks/useSubscription.js";
8+
import { ComponentBase, type IComponentProperties, type IComponentState } from "./ComponentBase/ComponentBase.js";
129

13-
export function NumberInput({ getValue, setValue, subscribable }: {
14-
getValue: () => string,
15-
setValue: (newValue: string) => void,
10+
export interface INumberInputProps extends IComponentProperties {
11+
getValue: () => string;
12+
setValue: (newValue: string) => void;
1613
subscribable: Subscribable;
17-
}): JSX.Element {
18-
const [visibleValue, setVisibleValue] = useState(getValue());
14+
}
15+
16+
interface INumberInputState extends IComponentState {
17+
visibleValue: string;
18+
}
19+
20+
export class NumberInput extends ComponentBase<INumberInputProps, INumberInputState> {
21+
22+
public constructor(props: INumberInputProps) {
23+
super(props);
24+
25+
this.state = {
26+
visibleValue: this.props.getValue()
27+
};
28+
}
29+
30+
public override componentDidMount(): void {
31+
const { subscribable } = this.props;
1932

20-
// If the model pushes a change to this value for some other reason, we'd better update
21-
useSubscription(subscribable, () => {
22-
setVisibleValue(getValue());
23-
});
33+
subscribable.subscribe(this.handleChange);
34+
}
35+
36+
public override componentWillUnmount(): void {
37+
const { subscribable } = this.props;
38+
39+
subscribable.unsubscribe(this.handleChange);
40+
}
41+
42+
public render(): ComponentChild {
43+
const { visibleValue } = this.state;
44+
45+
return (
46+
<input
47+
className="short"
48+
type="text"
49+
inputMode="numeric"
50+
pattern="[0-9]*"
51+
onChange={(event) => {
52+
this.attemptSetVisibleValue((event.target as HTMLInputElement).value);
53+
}}
54+
value={visibleValue}
55+
onBlur={() => {
56+
this.attemptSet();
57+
}}
58+
onKeyPress={(event) => {
59+
if (event.key === "Enter") {
60+
this.attemptSet();
61+
}
62+
}}
63+
/>
64+
);
65+
}
2466

2567
// To update the input as you type, but not update the model
26-
function attemptSetVisibleValue(inputValue: string) {
68+
private attemptSetVisibleValue(inputValue: string) {
2769
if (inputValue.length === 0) {
28-
setVisibleValue("");
70+
this.setState({ visibleValue: "" });
2971

3072
return;
3173
}
3274

3375
if (!inputValue.charAt(inputValue.length - 1).match(/[0-9]/)) {
34-
attemptSetVisibleValue(inputValue.substring(0, inputValue.length - 1));
76+
this.attemptSetVisibleValue(inputValue.substring(0, inputValue.length - 1));
3577

3678
return;
3779
}
3880

39-
setVisibleValue(inputValue);
81+
this.setState({ visibleValue: inputValue });
4082
}
4183

4284
// Try to set the model value, which may fail due to validation
43-
function attemptSet() {
85+
private attemptSet() {
86+
const { getValue, setValue } = this.props;
87+
const { visibleValue } = this.state;
88+
4489
if (visibleValue === getValue()) {
4590
return;
4691
}
4792

4893
try {
4994
setValue(visibleValue);
5095
} catch {
51-
setVisibleValue(getValue());
96+
this.setState({ visibleValue: getValue() });
5297
}
5398
}
5499

55-
return (
56-
<input
57-
className="short"
58-
type="text"
59-
inputMode="numeric"
60-
pattern="[0-9]*"
61-
onChange={event => {
62-
attemptSetVisibleValue((event.target as HTMLInputElement).value);
63-
}}
64-
value={visibleValue}
65-
onBlur={() => {
66-
attemptSet();
67-
}}
68-
onKeyPress={event => {
69-
if (event.key === "Enter") {
70-
attemptSet();
71-
}
72-
}}
73-
/>
74-
);
100+
private handleChange = () => {
101+
const { getValue } = this.props;
102+
103+
this.setState({ visibleValue: getValue() });
104+
};
75105
}

src/components/ui/PolyrhythmViewer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { JSX } from "preact/jsx-runtime";
1111
import type { PolyrhythmView } from "../../core/index.js";
1212
import { useEditCommand } from "../../ui/hooks/useEditCommand.js";
1313
import { useSubscription } from "../../ui/hooks/useSubscription.js";
14-
import { ServicesContext } from "./BananaDrumViewer.js";
14+
import { ServicesContext } from "./ScoreBookViewer.js";
1515
import { NoteViewer } from "./note/NoteViewer.js";
1616

1717
export function PolyrhythmViewer({ polyrhythm }: { polyrhythm: PolyrhythmView; }): JSX.Element {

0 commit comments

Comments
 (0)