From 985c79b96b699c9a5094dfca8e24ef4f9ccbd519 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Thu, 8 Jan 2026 09:02:22 +0330 Subject: [PATCH] live change value text --- .../sidebar/instructions/TextInstruction.tsx | 97 +++++++------------ .../editor/components/tools/TextShape.tsx | 11 ++- 2 files changed, 47 insertions(+), 61 deletions(-) diff --git a/src/pages/editor/components/sidebar/instructions/TextInstruction.tsx b/src/pages/editor/components/sidebar/instructions/TextInstruction.tsx index 86762de..30bc80b 100644 --- a/src/pages/editor/components/sidebar/instructions/TextInstruction.tsx +++ b/src/pages/editor/components/sidebar/instructions/TextInstruction.tsx @@ -2,7 +2,6 @@ import { type FC, useState, useEffect, useMemo } from "react"; import Input from "@/components/Input"; import Select from "@/components/Select"; import ColorPicker from "@/components/ColorPicker"; -import Button from "@/components/Button"; import { useEditorStore } from "@/pages/editor/store/editorStore"; import { useTextDefaultsStore } from "@/pages/editor/store/textDefaultsStore"; @@ -17,6 +16,7 @@ const fontWeightOptions = [ { label: "Light", value: "300" }, ]; + type TextFormState = { text: string; fontFamily: string; @@ -61,7 +61,10 @@ const TextInstruction: FC = () => { const handleChange = (field: keyof TextFormState, value: string | number) => { if (isEditing && selectedObject) { - setFormState((prev) => ({ ...prev, [field]: value })); + // اعمال تغییرات به صورت لایو + const newState = { ...formState, [field]: value }; + setFormState(newState); + updateObject(selectedObject.id, { [field]: value }); } else { // text فقط برای حالت ویرایش است و نباید در defaults ذخیره شود if (field !== "text") { @@ -70,41 +73,13 @@ const TextInstruction: FC = () => { } }; - const handleSave = () => { - if (isEditing && selectedObject) { - updateObject(selectedObject.id, formState); - } - }; - - const isDirty = isEditing && selectedObject - ? formState.text !== baseValues.text || - formState.fontFamily !== baseValues.fontFamily || - formState.fontWeight !== baseValues.fontWeight || - formState.fontSize !== baseValues.fontSize || - formState.fill !== baseValues.fill || - formState.opacity !== baseValues.opacity || - formState.letterSpacing !== baseValues.letterSpacing || - formState.wordSpacing !== baseValues.wordSpacing - : false; - - const opacityValue = formState.opacity; - const [opacityInput, setOpacityInput] = useState(`${opacityValue}`); + // State محلی برای نمایش شفافیت + const [opacityDisplay, setOpacityDisplay] = useState(`${formState.opacity}`); useEffect(() => { - setOpacityInput(`${opacityValue}`); - }, [opacityValue]); + setOpacityDisplay(`${formState.opacity}`); + }, [formState.opacity]); - const handleOpacityChange = (e: React.ChangeEvent) => { - const value = e.target.value.replace("%", "").trim(); - setOpacityInput(value); - }; - - const handleOpacityBlur = () => { - const numValue = parseInt(opacityInput) || 0; - const clampedValue = Math.min(100, Math.max(0, numValue)); - setOpacityInput(`${clampedValue}`); - handleChange("opacity", clampedValue); - }; return (
@@ -141,13 +116,14 @@ const TextInstruction: FC = () => { /> { - const value = e.target.value.replace("px", "").trim(); - const numValue = parseInt(value) || 0; + const numValue = parseInt(e.target.value) || 0; handleChange("fontSize", numValue); }} + min={1} />
@@ -160,21 +136,30 @@ const TextInstruction: FC = () => { onChange={(value) => handleChange("fill", value)} />
- -
- { - if (e.key === "Enter") { - e.currentTarget.blur(); - } - }} - /> -
+ { + const value = e.target.value; + setOpacityDisplay(value); + + // اعمال تغییرات به صورت لایو اگر مقدار معتبر است + const numValue = parseInt(value); + if (!isNaN(numValue) && numValue >= 0 && numValue <= 100) { + handleChange("opacity", numValue); + } + }} + onBlur={(e) => { + const numValue = parseInt(e.target.value) || 0; + const clampedValue = Math.min(100, Math.max(0, numValue)); + setOpacityDisplay(`${clampedValue}`); + handleChange("opacity", clampedValue); + }} + min={0} + max={100} + />
@@ -200,14 +185,6 @@ const TextInstruction: FC = () => { }} /> */} - - {isEditing && ( -
- -
- )} ); }; diff --git a/src/pages/editor/components/tools/TextShape.tsx b/src/pages/editor/components/tools/TextShape.tsx index cddbacd..a32c7a5 100644 --- a/src/pages/editor/components/tools/TextShape.tsx +++ b/src/pages/editor/components/tools/TextShape.tsx @@ -12,12 +12,21 @@ const getFontFamily = (fontFamily?: string): string => { }; const getFontWeight = (fontWeight?: string): string => { + if (!fontWeight) return "normal"; + + // اگر عدد است، مستقیماً برگردان + const numValue = parseInt(fontWeight); + if (!isNaN(numValue)) { + return numValue.toString(); + } + + // برای مقادیر string قدیمی const weightMap: Record = { bold: "bold", normal: "normal", "300": "300", }; - return weightMap[fontWeight || "normal"] || "normal"; + return weightMap[fontWeight] || "normal"; }; const getColorWithOpacity = (fill?: string, opacity?: number): string => {