From bcdc4a11f7919c2b33f5c83c01c0c17b3ee55a84 Mon Sep 17 00:00:00 2001 From: miyanokomiya Date: Sat, 7 Dec 2024 20:33:39 +0900 Subject: [PATCH] refactor: Split components --- src/components/floatMenu/StrokePanel.tsx | 185 ++++++++++++----------- 1 file changed, 96 insertions(+), 89 deletions(-) diff --git a/src/components/floatMenu/StrokePanel.tsx b/src/components/floatMenu/StrokePanel.tsx index bcf2632d..c0a875b5 100644 --- a/src/components/floatMenu/StrokePanel.tsx +++ b/src/components/floatMenu/StrokePanel.tsx @@ -48,92 +48,6 @@ export const StrokePanel: React.FC = ({ stroke, onChanged }) => { [onChanged], ); - const onDashChanged = useCallback( - (val: LineDash) => { - onChanged?.({ dash: val === "solid" ? undefined : val }); - }, - [onChanged], - ); - - const [customDashValue, setCustomDashValue] = useState(""); - useEffect(() => { - setCustomDashValue((currentStr) => { - const current = parseLineDashCustomValue(currentStr); - const next = stroke.dashCustom?.dash.join(",") ?? ""; - if (current.join(",") === next) { - // Keep current string when dash array doesn't change. - // Draft dash array string, such as "1,2,", can be preserved by this way. - return currentStr; - } - return next; - }); - }, [stroke]); - - const commitDashCustom = useCallback(() => { - onChanged?.({ dashCustom: stroke.dashCustom }); - }, [onChanged, stroke]); - - const onDashCustomValueChange = useCallback( - (val: string) => { - const current = parseLineDashCustomValue(customDashValue); - const dash = parseLineDashCustomValue(val); - if (current.join(",") !== dash.join(",")) { - onChanged?.({ dashCustom: { dash, valueType: "scale", offset: 0 } }, true); - } - setCustomDashValue(val); - }, - [onChanged, customDashValue], - ); - - const onDashCustomOffsetChange = useCallback( - (val: number, draft = false) => { - const dash = parseLineDashCustomValue(customDashValue); - onChanged?.({ dashCustom: { dash, valueType: "scale", offset: val } }, draft); - }, - [onChanged, customDashValue], - ); - - const lineDash = getLineDash(stroke.dash); - - const dashButtons = ( - -
- {LINE_DASH_KEYS.map((ld) => { - return ; - })} - -
- -
- -
-
- -
- -
-
-
- ); - const onCapChanged = useCallback( (val: CanvasLineCap) => { onChanged?.({ lineCap: val }); @@ -196,9 +110,9 @@ export const StrokePanel: React.FC = ({ stroke, onChanged }) => { -
{capButtons}
-
{joinButtons}
-
{dashButtons}
+ {capButtons} + {joinButtons} +
@@ -215,6 +129,99 @@ export const StrokePanel: React.FC = ({ stroke, onChanged }) => { ); }; +interface LineDashFieldProps { + stroke: Pick; + onChange?: (stroke: Partial, draft?: boolean) => void; +} + +const LineDashField: React.FC = ({ stroke, onChange }) => { + const onDashChanged = useCallback( + (val: LineDash) => { + onChange?.({ dash: val === "solid" ? undefined : val }); + }, + [onChange], + ); + + const [customDashValue, setCustomDashValue] = useState(""); + useEffect(() => { + setCustomDashValue((currentStr) => { + const current = parseLineDashCustomValue(currentStr); + const next = stroke.dashCustom?.dash.join(",") ?? ""; + if (current.join(",") === next) { + // Keep current string when dash array doesn't change. + // Draft dash array string, such as "1,2,", can be preserved by this way. + return currentStr; + } + return next; + }); + }, [stroke]); + + const commitDashCustom = useCallback(() => { + onChange?.({ dashCustom: stroke.dashCustom }); + }, [onChange, stroke]); + + const onDashCustomValueChange = useCallback( + (val: string) => { + const current = parseLineDashCustomValue(customDashValue); + const dash = parseLineDashCustomValue(val); + if (current.join(",") !== dash.join(",")) { + onChange?.({ dashCustom: { dash, valueType: "scale", offset: 0 } }, true); + } + setCustomDashValue(val); + }, + [onChange, customDashValue], + ); + + const onDashCustomOffsetChange = useCallback( + (val: number, draft = false) => { + const dash = parseLineDashCustomValue(customDashValue); + onChange?.({ dashCustom: { dash, valueType: "scale", offset: val } }, draft); + }, + [onChange, customDashValue], + ); + + const lineDash = getLineDash(stroke.dash); + + return ( + +
+ {LINE_DASH_KEYS.map((ld) => { + return ; + })} + +
+ +
+ +
+
+ +
+ +
+
+
+ ); +}; + interface LineDashButtonProps { lineDash: LineDash; highlight?: boolean;