From 8cfe559fb6110f382d686da4e9a3ffc359266ae7 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Tue, 12 May 2026 09:24:35 +0330 Subject: [PATCH] editor store update --- src/pages/editor/store/editorStore.helpers.ts | 19 +++++++--- src/pages/editor/store/editorStore.ts | 37 +++++++++++++------ src/pages/editor/store/editorStore.types.ts | 4 +- 3 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/pages/editor/store/editorStore.helpers.ts b/src/pages/editor/store/editorStore.helpers.ts index be155fd..fc51273 100644 --- a/src/pages/editor/store/editorStore.helpers.ts +++ b/src/pages/editor/store/editorStore.helpers.ts @@ -1,6 +1,7 @@ import type { EditorObject, Page, TableCell } from "./editorStore.types"; -export const createTableCellId = (row: number, col: number) => `cell-${row}-${col}`; +export const createTableCellId = (row: number, col: number) => + `cell-${row}-${col}`; const createUuid = (): string => { if (typeof globalThis.crypto?.randomUUID === "function") { @@ -15,7 +16,9 @@ const createUuid = (): string => { bytes[8] = (bytes[8] & 0x3f) | 0x80; const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")); - return `${hex.slice(0, 4).join("")}-${hex.slice(4, 6).join("")}-${hex.slice(6, 8).join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10, 16).join("")}`; + return `${hex.slice(0, 4).join("")}-${hex.slice(4, 6).join("")}-${hex + .slice(6, 8) + .join("")}-${hex.slice(8, 10).join("")}-${hex.slice(10, 16).join("")}`; } return `${Date.now()}-${Math.random().toString(16).slice(2)}`; @@ -300,7 +303,9 @@ export const buildGroupResult = ( export const ungroupById = (objects: EditorObject[], groupId: string) => objects - .map((obj) => (obj.groupId === groupId ? { ...obj, groupId: undefined } : obj)) + .map((obj) => + obj.groupId === groupId ? { ...obj, groupId: undefined } : obj, + ) .filter((obj) => obj.id !== groupId); /** @@ -308,7 +313,9 @@ export const ungroupById = (objects: EditorObject[], groupId: string) => * همانند buildGroupResult یک wrapper اضافه می‌کنیم تا ادیتور گروه را یکپارچه جابه‌جا کند. * اگر فقط یک عضو با آن groupId باشد، groupId حذف می‌شود (گروه نامعتبر). */ -export const ensureMissingGroupObjects = (objects: EditorObject[]): EditorObject[] => { +export const ensureMissingGroupObjects = ( + objects: EditorObject[], +): EditorObject[] => { let next = [...objects]; const referencedIds = new Set(); @@ -322,7 +329,9 @@ export const ensureMissingGroupObjects = (objects: EditorObject[]): EditorObject const members = next.filter((o) => o.groupId === gid && o.type !== "group"); if (members.length < 2) { next = next.map((o) => - o.groupId === gid && o.type !== "group" ? { ...o, groupId: undefined } : o, + o.groupId === gid && o.type !== "group" + ? { ...o, groupId: undefined } + : o, ); } } diff --git a/src/pages/editor/store/editorStore.ts b/src/pages/editor/store/editorStore.ts index d33090a..3417c29 100644 --- a/src/pages/editor/store/editorStore.ts +++ b/src/pages/editor/store/editorStore.ts @@ -46,7 +46,10 @@ export type { type PageBackgroundSettings = Pick< Page, - "backgroundType" | "backgroundColor" | "backgroundGradient" | "backgroundImageUrl" + | "backgroundType" + | "backgroundColor" + | "backgroundGradient" + | "backgroundImageUrl" >; type EditorStoreType = { @@ -126,7 +129,10 @@ type EditorStoreType = { toggleObjectVisibility: (id: string) => void; groupObjects: (objectIds: string[]) => void; ungroupObjects: (groupId: string) => void; - loadPages: (pages: Page[], documentSettings?: Partial) => void; + loadPages: ( + pages: Page[], + documentSettings?: Partial, + ) => void; clipboardObjects: EditorObject[]; objectHistoryPast: EditorObject[][]; objectHistoryFuture: EditorObject[][]; @@ -139,7 +145,9 @@ type EditorStoreType = { sendObjectBackward: (id: string) => void; documentSettings: DocumentSettings; updateDocumentSettings: (updates: Partial) => void; - updateCurrentPageBackground: (updates: Partial) => void; + updateCurrentPageBackground: ( + updates: Partial, + ) => void; }; export const useEditorStore = create((set, get) => { @@ -185,19 +193,19 @@ export const useEditorStore = create((set, get) => { return { documentSettings: { - displayStyle: 'double', + displayStyle: "double", autoPlay: false, pageFlipSound: true, leftToRightFlip: false, smartGuide: true, - backgroundType: 'color', - backgroundColor: '#ffffff', + backgroundType: "color", + backgroundColor: "#ffffff", backgroundGradient: { from: "#ffffff", to: "#e2e8f0", angle: 135, }, - backgroundImageUrl: '', + backgroundImageUrl: "", }, updateDocumentSettings: (updates) => set((state) => ({ @@ -206,7 +214,9 @@ export const useEditorStore = create((set, get) => { updateCurrentPageBackground: (updates) => set((state) => { if (!state.currentPageId) return {}; - const currentPage = state.pages.find((p) => p.id === state.currentPageId); + const currentPage = state.pages.find( + (p) => p.id === state.currentPageId, + ); if (!currentPage) return {}; const nextPage = { ...currentPage, ...updates }; return { @@ -471,8 +481,8 @@ export const useEditorStore = create((set, get) => { state.selectedObjectIds.length > 0 ? state.selectedObjectIds : state.selectedObjectId - ? [state.selectedObjectId] - : []; + ? [state.selectedObjectId] + : []; if (selectedIds.length === 0) return; @@ -483,7 +493,8 @@ export const useEditorStore = create((set, get) => { .map((obj) => obj.id), ); const objectsToCopy = state.objects.filter( - (obj) => selectedIdSet.has(obj.id) || groupsToCopy.has(obj.groupId || ""), + (obj) => + selectedIdSet.has(obj.id) || groupsToCopy.has(obj.groupId || ""), ); if (objectsToCopy.length === 0) return; @@ -950,7 +961,9 @@ export const useEditorStore = create((set, get) => { if (!result) return; if (state.currentPageId) { - const pageToGroup = state.pages.find((p) => p.id === state.currentPageId); + const pageToGroup = state.pages.find( + (p) => p.id === state.currentPageId, + ); if (!pageToGroup) return; const pageResult = buildGroupResult( pageToGroup.objects, diff --git a/src/pages/editor/store/editorStore.types.ts b/src/pages/editor/store/editorStore.types.ts index 7646eab..39b4031 100644 --- a/src/pages/editor/store/editorStore.types.ts +++ b/src/pages/editor/store/editorStore.types.ts @@ -89,8 +89,8 @@ export type Page = { backgroundImageUrl: string; }; -export type DisplayStyle = 'single' | 'double'; -export type BackgroundType = 'color' | 'gradient' | 'image'; +export type DisplayStyle = "single" | "double"; +export type BackgroundType = "color" | "gradient" | "image"; export type DocumentSettings = { displayStyle: DisplayStyle;