save guide line

This commit is contained in:
hamid zarghami
2026-04-26 10:56:26 +03:30
parent 4feccbf1f9
commit 333955fdf3
2 changed files with 47 additions and 11 deletions
+9 -8
View File
@@ -28,12 +28,14 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
const { const {
tool, tool,
objects, objects,
guides,
selectedObjectId, selectedObjectId,
selectedObjectIds, selectedObjectIds,
selectedCellId, selectedCellId,
editingCell, editingCell,
setStageRef, setStageRef,
setLayerRef, setLayerRef,
setGuides,
zoom, zoom,
} = useEditorStore(); } = useEditorStore();
@@ -49,7 +51,6 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
useTransformHandlers(transformerRef, layerRef); useTransformHandlers(transformerRef, layerRef);
const [guides, setGuides] = useState<Guide[]>([]);
const [activeGuideIds, setActiveGuideIds] = useState<string[]>([]); const [activeGuideIds, setActiveGuideIds] = useState<string[]>([]);
const [selectedGuideId, setSelectedGuideId] = useState<string | null>(null); const [selectedGuideId, setSelectedGuideId] = useState<string | null>(null);
const [draftGuide, setDraftGuide] = useState<{ const [draftGuide, setDraftGuide] = useState<{
@@ -81,13 +82,13 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
}; };
const handleGuidePositionChange = (id: string, position: number) => { const handleGuidePositionChange = (id: string, position: number) => {
setGuides((prev) => setGuides(
prev.map((guide) => guides.map((guide) =>
guide.id === id guide.id === id
? { ? {
...guide, ...guide,
position, position,
} }
: guide, : guide,
), ),
); );
@@ -95,12 +96,12 @@ const EditorCanvas = ({ catalogSize }: EditorCanvasProps) => {
const addGuide = (orientation: Guide["orientation"], position: number) => { const addGuide = (orientation: Guide["orientation"], position: number) => {
const id = `guide-${orientation}-${crypto.randomUUID()}`; const id = `guide-${orientation}-${crypto.randomUUID()}`;
setGuides((prev) => [...prev, { id, orientation, position }]); setGuides([...guides, { id, orientation, position }]);
setSelectedGuideId(id); setSelectedGuideId(id);
}; };
const removeGuide = (id: string) => { 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)); setActiveGuideIds((prev) => prev.filter((activeId) => activeId !== id));
setSelectedGuideId((prev) => (prev === id ? null : prev)); setSelectedGuideId((prev) => (prev === id ? null : prev));
}; };
+38 -3
View File
@@ -72,13 +72,22 @@ export type Page = {
id: string; id: string;
name: string; name: string;
objects: EditorObject[]; objects: EditorObject[];
guides: PageGuide[];
};
export type PageGuide = {
id: string;
orientation: "vertical" | "horizontal";
position: number;
}; };
type EditorStoreType = { type EditorStoreType = {
tool: ToolType; tool: ToolType;
setTool: (tool: ToolType) => void; setTool: (tool: ToolType) => void;
objects: EditorObject[]; objects: EditorObject[];
guides: PageGuide[];
addObject: (object: EditorObject) => void; addObject: (object: EditorObject) => void;
setGuides: (guides: PageGuide[]) => void;
updateObject: (id: string, updates: Partial<EditorObject>) => void; updateObject: (id: string, updates: Partial<EditorObject>) => void;
deleteObject: (id: string) => void; deleteObject: (id: string) => void;
duplicateObject: (id: string) => void; duplicateObject: (id: string) => void;
@@ -177,6 +186,7 @@ const createInitialPage = (name: string): Page => ({
id: `page-${crypto.randomUUID?.()}`, id: `page-${crypto.randomUUID?.()}`,
name, name,
objects: [], objects: [],
guides: [],
}); });
export const useEditorStore = create<EditorStoreType>((set, get) => { export const useEditorStore = create<EditorStoreType>((set, get) => {
@@ -189,6 +199,7 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
selectedObjectId: tool === "select" ? state.selectedObjectId : null, selectedObjectId: tool === "select" ? state.selectedObjectId : null,
})), })),
objects: [], objects: [],
guides: [],
addObject: (object) => { addObject: (object) => {
const state = get(); const state = get();
if (state.currentPageId) { if (state.currentPageId) {
@@ -209,6 +220,19 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
} }
set((state) => ({ objects: [...state.objects, object] })); 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) => { updateObject: (id, updates) => {
const state = get(); const state = get();
if (state.currentPageId) { if (state.currentPageId) {
@@ -578,6 +602,7 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
pages: [...state.pages, newPage], pages: [...state.pages, newPage],
currentPageId: newPage.id, currentPageId: newPage.id,
objects: [], objects: [],
guides: [],
selectedObjectId: null, selectedObjectId: null,
})); }));
}, },
@@ -595,6 +620,7 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
id: `page-${crypto.randomUUID?.()}`, id: `page-${crypto.randomUUID?.()}`,
name: `${pageToDuplicate.name} (کپی)`, name: `${pageToDuplicate.name} (کپی)`,
objects: duplicatedObjects, objects: duplicatedObjects,
guides: [...pageToDuplicate.guides],
}; };
set((state) => ({ set((state) => ({
@@ -617,6 +643,7 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
pages: newPages, pages: newPages,
currentPageId: newCurrentPageId, currentPageId: newCurrentPageId,
objects: newCurrentPage?.objects || [], objects: newCurrentPage?.objects || [],
guides: newCurrentPage?.guides || [],
selectedObjectId: null, selectedObjectId: null,
}); });
}, },
@@ -628,7 +655,9 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
// Save current objects to current page before switching // Save current objects to current page before switching
const updatedPages = state.currentPageId const updatedPages = state.currentPageId
? state.pages.map((p) => ? 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; : state.pages;
@@ -639,6 +668,7 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
pages: updatedPages, pages: updatedPages,
currentPageId: pageId, currentPageId: pageId,
objects: targetPageFromUpdated.objects, objects: targetPageFromUpdated.objects,
guides: targetPageFromUpdated.guides || [],
selectedObjectId: null, selectedObjectId: null,
}); });
}, },
@@ -939,11 +969,16 @@ export const useEditorStore = create<EditorStoreType>((set, get) => {
}, },
loadPages: (pages) => { loadPages: (pages) => {
if (pages.length === 0) return; if (pages.length === 0) return;
const firstPage = pages[0]; const normalizedPages = pages.map((page) => ({
...page,
guides: page.guides || [],
}));
const firstPage = normalizedPages[0];
set({ set({
pages, pages: normalizedPages,
currentPageId: firstPage.id, currentPageId: firstPage.id,
objects: firstPage.objects, objects: firstPage.objects,
guides: firstPage.guides || [],
selectedObjectId: null, selectedObjectId: null,
selectedObjectIds: [], selectedObjectIds: [],
}); });