feat: 右键点击每一边的 LED 增减按钮时,按增减十个计。

This commit is contained in:
Ivan Li 2023-01-29 00:41:11 +08:00
parent 45f3f79a49
commit bd025d6d71

View File

@ -1,4 +1,4 @@
import { HTMLAttributes, useCallback } from 'react'; import { HTMLAttributes, MouseEventHandler, useCallback } from 'react';
import { FC } from 'react'; import { FC } from 'react';
import { LedStripConfig } from '../models/led-strip-config'; import { LedStripConfig } from '../models/led-strip-config';
import tw, { styled } from 'twin.macro'; import tw, { styled } from 'twin.macro';
@ -26,36 +26,54 @@ export const LedStripEditor: FC<LedStripEditorProps> = ({
onChange, onChange,
...htmlAttrs ...htmlAttrs
}) => { }) => {
const addLed = useCallback(() => { const addLed: MouseEventHandler = useCallback(
if (config) { (ev) => {
if (config.global_start_position <= config.global_end_position) { ev.preventDefault();
onChange?.({ ...config, global_end_position: config.global_end_position + 1 }); const delta = ev.button === 2 ? 10 : 1;
if (config) {
if (config.global_start_position <= config.global_end_position) {
onChange?.({
...config,
global_end_position: config.global_end_position + delta,
});
} else {
onChange?.({
...config,
global_start_position: config.global_start_position + delta,
});
}
} else { } else {
onChange?.({ onChange?.(new LedStripConfig(0, 0, 0));
...config,
global_start_position: config.global_start_position + 1,
});
} }
} else { },
onChange?.(new LedStripConfig(0, 0, 0)); [config, onChange],
} );
}, [config, onChange]); const removeLed: MouseEventHandler = useCallback(
const removeLed = useCallback(() => { (ev) => {
if (!config) { ev.preventDefault();
onChange?.(null); const delta = ev.button === 2 ? 10 : 1;
} else if (Math.abs(config.global_start_position - config.global_end_position) <= 1) { if (!config) {
onChange?.(null); onChange?.(null);
} else { } else if (
if (config.global_start_position <= config.global_end_position) { Math.abs(config.global_start_position - config.global_end_position) <= delta
onChange?.({ ...config, global_end_position: config.global_end_position - 1 }); ) {
onChange?.(null);
} else { } else {
onChange?.({ if (config.global_start_position <= config.global_end_position) {
...config, onChange?.({
global_start_position: config.global_start_position - 1, ...config,
}); global_end_position: config.global_end_position - delta,
});
} else {
onChange?.({
...config,
global_start_position: config.global_start_position - delta,
});
}
} }
} },
}, [config, onChange]); [config, onChange],
);
const reverse = useCallback(() => { const reverse = useCallback(() => {
if (!config) { if (!config) {
return; return;
@ -69,10 +87,10 @@ export const LedStripEditor: FC<LedStripEditorProps> = ({
return ( return (
<StyledContainer {...htmlAttrs}> <StyledContainer {...htmlAttrs}>
<StyledButton title="Add LED" onClick={addLed}> <StyledButton title="Add LED" onClick={addLed} onContextMenu={addLed}>
<FontAwesomeIcon icon={faPlus} /> <FontAwesomeIcon icon={faPlus} />
</StyledButton> </StyledButton>
<StyledButton title="Remove LED" onClick={removeLed}> <StyledButton title="Remove LED" onClick={removeLed} onContextMenu={removeLed}>
<FontAwesomeIcon icon={faMinus} /> <FontAwesomeIcon icon={faMinus} />
</StyledButton> </StyledButton>
<StyledButton title="Reverse" onClick={reverse}> <StyledButton title="Reverse" onClick={reverse}>