Remove some comments

This commit is contained in:
hamid zarghami
2025-08-04 16:37:00 +03:30
parent 4433c4ce1b
commit af935ba2b5
15 changed files with 656 additions and 129 deletions
+136 -57
View File
@@ -8,7 +8,6 @@ import {
ButtonType,
ImageType,
SocialType,
SectionName,
HorizontalAlignment,
VerticalAlignment,
SelectedElement,
@@ -16,9 +15,9 @@ import {
export const usePersonalityStore = create<PersonalityStore>((set) => ({
data: {
header: { columnsCount: 1, items: [] },
content: { columnsCount: 1, items: [] },
footer: { columnsCount: 1, items: [] },
header: { name: "هدر", columnsCount: 1, items: [] },
content: { name: "محتوا", columnsCount: 1, items: [], isContent: true },
footer: { name: "فوتر", columnsCount: 1, items: [] },
},
activeSection: "",
@@ -27,13 +26,35 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
isEditMode: false,
setData: (data: PersonalityDataType) =>
set(() => ({
data,
})),
set(() => {
// Normalize data to ensure all sections have names
const normalizedData: PersonalityDataType = {};
setActiveSection: (section: SectionName) =>
Object.entries(data).forEach(([key, section]) => {
normalizedData[key] = {
...section,
name:
section.name ||
(key === "header"
? "هدر"
: key === "content"
? "محتوا"
: key === "footer"
? "فوتر"
: `سکشن ${key}`),
};
});
return { data: normalizedData };
}),
setActiveSection: (section: string) =>
set((state) => {
const sectionData = state.data[section as keyof PersonalityDataType];
const sectionData = state.data[section];
if (!sectionData) {
return state;
}
// اگر بخش آیتم ندارد و تعداد ستون‌ها بیشتر از صفر است، یک آیتم پیش‌فرض بساز
if (sectionData.items.length === 0 && sectionData.columnsCount > 0) {
@@ -85,10 +106,90 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
setEditMode: (isEdit: boolean) => set({ isEditMode: isEdit }),
setColumnsCount: (
sectionKey: keyof PersonalityDataType,
columnsCount: number
) =>
addSection: (afterSectionKey?: string) =>
set((state) => {
// Generate automatic section name
const existingSectionNumbers = Object.values(state.data)
.map((section) => {
if (!section.name) return 0;
const match = section.name.match(/سکشن (\d+)/);
return match ? parseInt(match[1]) : 0;
})
.filter((num) => num > 0);
const nextNumber =
existingSectionNumbers.length > 0
? Math.max(...existingSectionNumbers) + 1
: 1;
const sectionName = `سکشن ${nextNumber}`;
const sectionKey = `section_${nextNumber}`;
if (state.data[sectionKey]) {
return state; // Section already exists
}
// Create new section data
const newSection = {
name: sectionName,
columnsCount: 1,
items: [],
};
// If afterSectionKey is provided, insert after that section
if (afterSectionKey) {
const keys = Object.keys(state.data);
const afterIndex = keys.indexOf(afterSectionKey);
if (afterIndex !== -1) {
const newData: typeof state.data = {};
// Add sections before the target
for (let i = 0; i <= afterIndex; i++) {
newData[keys[i]] = state.data[keys[i]];
}
// Add new section
newData[sectionKey] = newSection;
// Add remaining sections
for (let i = afterIndex + 1; i < keys.length; i++) {
newData[keys[i]] = state.data[keys[i]];
}
return { data: newData };
}
}
// Default: add at the end
return {
data: {
...state.data,
[sectionKey]: newSection,
},
};
}),
removeSection: (sectionKey: string) =>
set((state) => {
// Cannot remove content section
if (state.data[sectionKey]?.isContent) {
return state;
}
const newData = { ...state.data };
delete newData[sectionKey];
return {
data: newData,
activeSection:
state.activeSection === sectionKey ? "" : state.activeSection,
selectedElement: null,
isEditMode: false,
};
}),
setColumnsCount: (sectionKey: string, columnsCount: number) =>
set((state) => {
const currentItems = state.data[sectionKey].items;
const currentActiveItem = currentItems[state.activeItemIndex];
@@ -124,6 +225,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
data: {
...state.data,
[sectionKey]: {
...state.data[sectionKey],
columnsCount,
items: newItems,
},
@@ -132,7 +234,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
};
}),
setItems: (sectionKey: keyof PersonalityDataType, items: SectionItemType[]) =>
setItems: (sectionKey: string, items: SectionItemType[]) =>
set((state) => ({
data: {
...state.data,
@@ -143,10 +245,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
},
})),
addItem: (
sectionKey: keyof PersonalityDataType,
item: Omit<SectionItemType, "id">
) =>
addItem: (sectionKey: string, item: Omit<SectionItemType, "id">) =>
set((state) => ({
data: {
...state.data,
@@ -161,7 +260,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
set((state) => {
if (!state.activeSection) return state;
const sectionKey = state.activeSection as keyof PersonalityDataType;
const sectionKey = state.activeSection;
const items = state.data[sectionKey].items;
const activeIndex = state.activeItemIndex;
@@ -209,7 +308,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
}),
updateItem: (
sectionKey: keyof PersonalityDataType,
sectionKey: string,
id: string,
updates: Partial<SectionItemType>
) =>
@@ -225,7 +324,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
},
})),
removeItem: (sectionKey: keyof PersonalityDataType, id: string) =>
removeItem: (sectionKey: string, id: string) =>
set((state) => ({
data: {
...state.data,
@@ -236,11 +335,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
},
})),
addText: (
sectionKey: keyof PersonalityDataType,
itemId: string,
text: Omit<TextType, "id">
) =>
addText: (sectionKey: string, itemId: string, text: Omit<TextType, "id">) =>
set((state) => ({
data: {
...state.data,
@@ -262,7 +357,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
set((state) => {
if (!state.activeSection) return state;
const sectionKey = state.activeSection as keyof PersonalityDataType;
const sectionKey = state.activeSection as string;
const items = state.data[sectionKey].items;
const activeIndex = state.activeItemIndex;
@@ -289,7 +384,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
}),
addButton: (
sectionKey: keyof PersonalityDataType,
sectionKey: string,
itemId: string,
button: Omit<ButtonType, "id">
) =>
@@ -317,7 +412,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
set((state) => {
if (!state.activeSection) return state;
const sectionKey = state.activeSection as keyof PersonalityDataType;
const sectionKey = state.activeSection as string;
const items = state.data[sectionKey].items;
const activeIndex = state.activeItemIndex;
@@ -344,7 +439,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
}),
updateText: (
sectionKey: keyof PersonalityDataType,
sectionKey: string,
itemId: string,
textId: string,
updates: Partial<TextType>
@@ -369,7 +464,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
})),
updateButton: (
sectionKey: keyof PersonalityDataType,
sectionKey: string,
itemId: string,
buttonId: string,
updates: Partial<ButtonType>
@@ -394,7 +489,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
})),
addImage: (
sectionKey: keyof PersonalityDataType,
sectionKey: string,
itemId: string,
image: Omit<ImageType, "id">
) =>
@@ -419,7 +514,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
set((state) => {
if (!state.activeSection) return state;
const sectionKey = state.activeSection as keyof PersonalityDataType;
const sectionKey = state.activeSection as string;
const items = state.data[sectionKey].items;
const activeIndex = state.activeItemIndex;
@@ -446,7 +541,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
}),
updateImage: (
sectionKey: keyof PersonalityDataType,
sectionKey: string,
itemId: string,
imageId: string,
updates: Partial<ImageType>
@@ -470,11 +565,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
},
})),
deleteText: (
sectionKey: keyof PersonalityDataType,
itemId: string,
textId: string
) =>
deleteText: (sectionKey: string, itemId: string, textId: string) =>
set((state) => ({
data: {
...state.data,
@@ -496,11 +587,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
isEditMode: false,
})),
deleteButton: (
sectionKey: keyof PersonalityDataType,
itemId: string,
buttonId: string
) =>
deleteButton: (sectionKey: string, itemId: string, buttonId: string) =>
set((state) => ({
data: {
...state.data,
@@ -522,11 +609,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
isEditMode: false,
})),
deleteImage: (
sectionKey: keyof PersonalityDataType,
itemId: string,
imageId: string
) =>
deleteImage: (sectionKey: string, itemId: string, imageId: string) =>
set((state) => ({
data: {
...state.data,
@@ -549,7 +632,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
})),
addSocial: (
sectionKey: keyof PersonalityDataType,
sectionKey: string,
itemId: string,
social: Omit<SocialType, "id">
) =>
@@ -577,7 +660,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
set((state) => {
if (!state.activeSection) return state;
const sectionKey = state.activeSection as keyof PersonalityDataType;
const sectionKey = state.activeSection as string;
const items = state.data[sectionKey].items;
const activeIndex = state.activeItemIndex;
@@ -604,7 +687,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
}),
updateSocial: (
sectionKey: keyof PersonalityDataType,
sectionKey: string,
itemId: string,
socialId: string,
updates: Partial<SocialType>
@@ -628,11 +711,7 @@ export const usePersonalityStore = create<PersonalityStore>((set) => ({
},
})),
deleteSocial: (
sectionKey: keyof PersonalityDataType,
itemId: string,
socialId: string
) =>
deleteSocial: (sectionKey: string, itemId: string, socialId: string) =>
set((state) => ({
data: {
...state.data,