export html

This commit is contained in:
hamid zarghami
2025-07-20 14:47:57 +03:30
parent 6f640a80a7
commit b76be3815d
2 changed files with 11 additions and 75 deletions
+1 -1
View File
@@ -73,7 +73,7 @@
},
"setting": {
"title": "تنظیمات",
"personality": "شخصی سازی",
"personality": "قالب",
"mail_server": "تنظیمات میل سرور",
"domain": "تنظیمات دامنه",
"address": "نشانی ها",
@@ -10,79 +10,9 @@ import {
SectionType,
} from "../types/Types";
// تابع برای تبدیل تصویر به base64
const imageToBase64 = async (imageUrl: string): Promise<string> => {
try {
const response = await fetch(imageUrl);
const blob = await response.blob();
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
reader.onerror = reject;
reader.readAsDataURL(blob);
});
} catch (error) {
console.error("Error converting image to base64:", error);
return imageUrl; // در صورت خطا، URL اصلی را برگردان
}
};
// تابع برای تبدیل همه تصاویر در داده‌ها به base64
const convertImagesToBase64 = async (
data: PersonalityDataType
): Promise<PersonalityDataType> => {
const convertSectionImages = async (
section: SectionType
): Promise<SectionType> => {
const items = await Promise.all(
section.items.map(async (item) => {
// تبدیل تصاویر در images array
const images = await Promise.all(
item.images.map(async (image) => ({
...image,
src: await imageToBase64(image.src),
}))
);
// تبدیل background image
let backgroundImage = item.backgroundImage;
if (backgroundImage) {
backgroundImage = await imageToBase64(backgroundImage);
}
return {
...item,
images,
backgroundImage,
};
})
);
return {
...section,
items,
};
};
const [header, content, footer] = await Promise.all([
convertSectionImages(data.header),
convertSectionImages(data.content),
convertSectionImages(data.footer),
]);
return {
header,
content,
footer,
};
};
export const exportPersonalityToHTML = async (
data: PersonalityDataType
): Promise<string> => {
// تبدیل تصاویر به base64
const dataWithBase64Images = await convertImagesToBase64(data);
// Helper functions
const getHorizontalAlign = (alignment?: HorizontalAlignment) => {
switch (alignment) {
@@ -338,7 +268,8 @@ export const exportPersonalityToHTML = async (
const renderSection = (
section: SectionType,
sectionHeight: string,
paddingTop = ""
paddingTop = "",
isContentSection = false
) => {
if (section.columnsCount > 0) {
return `<tr>
@@ -418,6 +349,11 @@ export const exportPersonalityToHTML = async (
}" style="padding: 8px; font-size: 14px; line-height: 1.4;">
${renderTexts(item?.texts || [])}
${renderImages(item?.images || [])}
${
isContentSection
? '<div style="margin: 16px 0; text-align: center;">{{content}}</div>'
: ""
}
</td>
</tr>`;
@@ -490,9 +426,9 @@ export const exportPersonalityToHTML = async (
const html = `<div style="max-width: 600px; margin: 0 auto; direction: rtl;">
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse: collapse;">
<tbody>
${renderSection(dataWithBase64Images.header, "123px")}
${renderSection(dataWithBase64Images.content, "246px", "16px")}
${renderSection(dataWithBase64Images.footer, "123px", "16px")}
${renderSection(data.header, "123px")}
${renderSection(data.content, "246px", "16px", true)}
${renderSection(data.footer, "123px", "16px")}
</tbody>
</table>
</div>`;