Feed index into options

This commit is contained in:
Jake Archibald
2020-12-05 13:31:59 +00:00
parent 0226b87670
commit 793c8b2574
2 changed files with 28 additions and 25 deletions

View File

@@ -19,13 +19,14 @@ import { Options as QuantOptionsComponent } from 'features/processors/quantize/c
import { Options as ResizeOptionsComponent } from 'features/processors/resize/client'; import { Options as ResizeOptionsComponent } from 'features/processors/resize/client';
interface Props { interface Props {
index: 0 | 1;
mobileView: boolean; mobileView: boolean;
source?: SourceImage; source?: SourceImage;
encoderState?: EncoderState; encoderState?: EncoderState;
processorState: ProcessorState; processorState: ProcessorState;
onEncoderTypeChange(newType: OutputType): void; onEncoderTypeChange(index: 0 | 1, newType: OutputType): void;
onEncoderOptionsChange(newOptions: EncoderOptions): void; onEncoderOptionsChange(index: 0 | 1, newOptions: EncoderOptions): void;
onProcessorOptionsChange(newOptions: ProcessorState): void; onProcessorOptionsChange(index: 0 | 1, newOptions: ProcessorState): void;
} }
interface State { interface State {
@@ -73,7 +74,7 @@ export default class Options extends Component<Props, State> {
// The select element only has values matching encoder types, // The select element only has values matching encoder types,
// so 'as' is safe here. // so 'as' is safe here.
const type = el.value as OutputType; const type = el.value as OutputType;
this.props.onEncoderTypeChange(type); this.props.onEncoderTypeChange(this.props.index, type);
}; };
private onProcessorEnabledChange = (event: Event) => { private onProcessorEnabledChange = (event: Event) => {
@@ -81,24 +82,31 @@ export default class Options extends Component<Props, State> {
const processor = el.name.split('.')[0] as keyof ProcessorState; const processor = el.name.split('.')[0] as keyof ProcessorState;
this.props.onProcessorOptionsChange( this.props.onProcessorOptionsChange(
this.props.index,
cleanSet(this.props.processorState, `${processor}.enabled`, el.checked), cleanSet(this.props.processorState, `${processor}.enabled`, el.checked),
); );
}; };
private onQuantizerOptionsChange = (opts: ProcessorOptions['quantize']) => { private onQuantizerOptionsChange = (opts: ProcessorOptions['quantize']) => {
this.props.onProcessorOptionsChange( this.props.onProcessorOptionsChange(
this.props.index,
cleanMerge(this.props.processorState, 'quantize', opts), cleanMerge(this.props.processorState, 'quantize', opts),
); );
}; };
private onResizeOptionsChange = (opts: ProcessorOptions['resize']) => { private onResizeOptionsChange = (opts: ProcessorOptions['resize']) => {
this.props.onProcessorOptionsChange( this.props.onProcessorOptionsChange(
this.props.index,
cleanMerge(this.props.processorState, 'resize', opts), cleanMerge(this.props.processorState, 'resize', opts),
); );
}; };
private onEncoderOptionsChange = (newOptions: EncoderOptions) => {
this.props.onEncoderOptionsChange(this.props.index, newOptions);
};
render( render(
{ source, encoderState, processorState, onEncoderOptionsChange }: Props, { source, encoderState, processorState }: Props,
{ supportedEncoderMap }: State, { supportedEncoderMap }: State,
) { ) {
const encoder = encoderState && encoderMap[encoderState.type]; const encoder = encoderState && encoderMap[encoderState.type];
@@ -180,7 +188,7 @@ export default class Options extends Component<Props, State> {
// the correct type, but typescript isn't smart enough. // the correct type, but typescript isn't smart enough.
encoderState!.options as any encoderState!.options as any
} }
onChange={onEncoderOptionsChange} onChange={this.onEncoderOptionsChange}
/> />
)} )}
</Expander> </Expander>

View File

@@ -319,7 +319,7 @@ export default class Compress extends Component<Props, State> {
this.setState({ mobileView: this.widthQuery.matches }); this.setState({ mobileView: this.widthQuery.matches });
}; };
private onEncoderTypeChange(index: 0 | 1, newType: OutputType): void { private onEncoderTypeChange = (index: 0 | 1, newType: OutputType): void => {
this.setState({ this.setState({
sides: cleanSet( sides: cleanSet(
this.state.sides, this.state.sides,
@@ -332,12 +332,12 @@ export default class Compress extends Component<Props, State> {
}, },
), ),
}); });
} };
private onProcessorOptionsChange( private onProcessorOptionsChange = (
index: 0 | 1, index: 0 | 1,
options: ProcessorState, options: ProcessorState,
): void { ): void => {
this.setState({ this.setState({
sides: cleanSet( sides: cleanSet(
this.state.sides, this.state.sides,
@@ -345,9 +345,12 @@ export default class Compress extends Component<Props, State> {
options, options,
), ),
}); });
} };
private onEncoderOptionsChange(index: 0 | 1, options: EncoderOptions): void { private onEncoderOptionsChange = (
index: 0 | 1,
options: EncoderOptions,
): void => {
this.setState({ this.setState({
sides: cleanSet( sides: cleanSet(
this.state.sides, this.state.sides,
@@ -355,7 +358,7 @@ export default class Compress extends Component<Props, State> {
options, options,
), ),
}); });
} };
componentWillReceiveProps(nextProps: Props): void { componentWillReceiveProps(nextProps: Props): void {
if (nextProps.file !== this.props.file) { if (nextProps.file !== this.props.file) {
@@ -794,22 +797,14 @@ export default class Compress extends Component<Props, State> {
const options = sides.map((side, index) => ( const options = sides.map((side, index) => (
<Options <Options
index={index as 0 | 1}
source={source} source={source}
mobileView={mobileView} mobileView={mobileView}
processorState={side.latestSettings.processorState} processorState={side.latestSettings.processorState}
encoderState={side.latestSettings.encoderState} encoderState={side.latestSettings.encoderState}
onEncoderTypeChange={this.onEncoderTypeChange.bind( onEncoderTypeChange={this.onEncoderTypeChange}
this, onEncoderOptionsChange={this.onEncoderOptionsChange}
index as 0 | 1, onProcessorOptionsChange={this.onProcessorOptionsChange}
)}
onEncoderOptionsChange={this.onEncoderOptionsChange.bind(
this,
index as 0 | 1,
)}
onProcessorOptionsChange={this.onProcessorOptionsChange.bind(
this,
index as 0 | 1,
)}
/> />
)); ));