From 333955fdf3624896868a1b835900c0e05433cfab Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 26 Apr 2026 10:56:26 +0330 Subject: [PATCH] save guide line --- src/pages/editor/components/EditorCanvas.tsx | 17 ++++---- src/pages/editor/store/editorStore.ts | 41 ++++++++++++++++++-- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/pages/editor/components/EditorCanvas.tsx b/src/pages/editor/components/EditorCanvas.tsx index 3c25139..ed83d85 100644 --- a/src/pages/editor/components/EditorCanvas.tsx +++ b/src/pages/editor/components/EditorCanvas.tsx @@ -28,12 +28,14 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => { const { tool, objects, + guides, selectedObjectId, selectedObjectIds, selectedCellId, editingCell, setStageRef, setLayerRef, + setGuides, zoom, } = useEditorStore(); @@ -49,7 +51,6 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => { useTransformHandlers(transformerRef, layerRef); - const [guides, setGuides] = useState([]); const [activeGuideIds, setActiveGuideIds] = useState([]); const [selectedGuideId, setSelectedGuideId] = useState(null); const [draftGuide, setDraftGuide] = useState<{ @@ -81,13 +82,13 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => { }; const handleGuidePositionChange = (id: string, position: number) => { - setGuides((prev) => - prev.map((guide) => + setGuides( + guides.map((guide) => guide.id === id ? { - ...guide, - position, - } + ...guide, + position, + } : guide, ), ); @@ -95,12 +96,12 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => { const addGuide = (orientation: Guide["orientation"], position: number) => { const id = `guide-${orientation}-${crypto.randomUUID()}`; - setGuides((prev) => [...prev, { id, orientation, position }]); + setGuides([...guides, { id, orientation, position }]); setSelectedGuideId(id); }; const removeGuide = (id: string) => { - setGuides((prev) => prev.filter((guide) => guide.id !== id)); + setGuides(guides.filter((guide) => guide.id !== id)); setActiveGuideIds((prev) => prev.filter((activeId) => activeId !== id)); setSelectedGuideId((prev) => (prev === id ? null : prev)); }; diff --git a/src/pages/editor/store/editorStore.ts b/src/pages/editor/store/editorStore.ts index c1a675f..894255d 100644 --- a/src/pages/editor/store/editorStore.ts +++ b/src/pages/editor/store/editorStore.ts @@ -72,13 +72,22 @@ export type Page = { id: string; name: string; objects: EditorObject[]; + guides: PageGuide[]; +}; + +export type PageGuide = { + id: string; + orientation: "vertical" | "horizontal"; + position: number; }; type EditorStoreType = { tool: ToolType; setTool: (tool: ToolType) => void; objects: EditorObject[]; + guides: PageGuide[]; addObject: (object: EditorObject) => void; + setGuides: (guides: PageGuide[]) => void; updateObject: (id: string, updates: Partial) => void; deleteObject: (id: string) => void; duplicateObject: (id: string) => void; @@ -177,6 +186,7 @@ const createInitialPage = (name: string): Page => ({ id: `page-${crypto.randomUUID?.()}`, name, objects: [], + guides: [], }); export const useEditorStore = create((set, get) => { @@ -189,6 +199,7 @@ export const useEditorStore = create((set, get) => { selectedObjectId: tool === "select" ? state.selectedObjectId : null, })), objects: [], + guides: [], addObject: (object) => { const state = get(); if (state.currentPageId) { @@ -209,6 +220,19 @@ export const useEditorStore = create((set, get) => { } set((state) => ({ objects: [...state.objects, object] })); }, + setGuides: (guides) => { + const state = get(); + if (state.currentPageId) { + set((state) => ({ + guides, + pages: state.pages.map((p) => + p.id === state.currentPageId ? { ...p, guides } : p, + ), + })); + return; + } + set({ guides }); + }, updateObject: (id, updates) => { const state = get(); if (state.currentPageId) { @@ -578,6 +602,7 @@ export const useEditorStore = create((set, get) => { pages: [...state.pages, newPage], currentPageId: newPage.id, objects: [], + guides: [], selectedObjectId: null, })); }, @@ -595,6 +620,7 @@ export const useEditorStore = create((set, get) => { id: `page-${crypto.randomUUID?.()}`, name: `${pageToDuplicate.name} (کپی)`, objects: duplicatedObjects, + guides: [...pageToDuplicate.guides], }; set((state) => ({ @@ -617,6 +643,7 @@ export const useEditorStore = create((set, get) => { pages: newPages, currentPageId: newCurrentPageId, objects: newCurrentPage?.objects || [], + guides: newCurrentPage?.guides || [], selectedObjectId: null, }); }, @@ -628,7 +655,9 @@ export const useEditorStore = create((set, get) => { // Save current objects to current page before switching const updatedPages = state.currentPageId ? state.pages.map((p) => - p.id === state.currentPageId ? { ...p, objects: state.objects } : p, + p.id === state.currentPageId + ? { ...p, objects: state.objects, guides: state.guides } + : p, ) : state.pages; @@ -639,6 +668,7 @@ export const useEditorStore = create((set, get) => { pages: updatedPages, currentPageId: pageId, objects: targetPageFromUpdated.objects, + guides: targetPageFromUpdated.guides || [], selectedObjectId: null, }); }, @@ -939,11 +969,16 @@ export const useEditorStore = create((set, get) => { }, loadPages: (pages) => { if (pages.length === 0) return; - const firstPage = pages[0]; + const normalizedPages = pages.map((page) => ({ + ...page, + guides: page.guides || [], + })); + const firstPage = normalizedPages[0]; set({ - pages, + pages: normalizedPages, currentPageId: firstPage.id, objects: firstPage.objects, + guides: firstPage.guides || [], selectedObjectId: null, selectedObjectIds: [], });