add custom cover images to themes.

This commit is contained in:
SethBurkart123
2024-04-07 15:05:11 +10:00
parent 7305b0da67
commit b7e7c24727
7 changed files with 57 additions and 17 deletions
+3 -6
View File
@@ -54,16 +54,13 @@ export const ThemeCover: React.FC<ThemeCoverProps> = ({
}
<div className="relative top-0 z-10 flex justify-center w-full h-full overflow-hidden transition dark:text-white rounded-xl group place-items-center bg-zinc-100 dark:bg-zinc-900">
{/* Render theme cover image or placeholder */}
{/* {theme.CustomImages.length > 0 ? (
{theme.coverImage &&
<img
src={URL.createObjectURL(theme.CustomImages[0].blob)}
src={theme.coverImage as string}
alt={theme.name}
className="absolute inset-0 z-0 object-cover"
/>
) : (
<div className="absolute inset-0 z-0 bg-gray-300 rounded-lg"></div>
)} */}
}
<div className="z-10">{theme.name}</div>
</div>
</button>
+25 -7
View File
@@ -30,8 +30,19 @@ export const listThemes = async (): Promise<ThemeList> => {
browser.runtime.sendMessage({
type: 'currentTab',
info: 'ListThemes'
}).then((response) => {
}).then(async (response) => {
if (response) {
// convert the response themes coverImage to a bloburl
response.themes = await Promise.all(
response.themes.map(async (theme: Omit<CustomTheme, 'CustomImages'>) => {
if (theme.coverImage) {
const blob = await fetch(theme.coverImage as string).then((res) => res.blob());
theme.coverImage = URL.createObjectURL(blob);
}
return theme;
})
);
resolve(response);
} else {
reject(new Error('Failed to get response'));
@@ -81,15 +92,22 @@ export const sendThemeUpdate = (updatedTheme: CustomTheme, saveTheme?: boolean,
};
});
Promise.all(imageDataPromises).then((imageData) => {
// Send the updated theme with image data to the content script for live preview or saving
Promise.all(imageDataPromises).then(async (imageData) => {
const themeData = {
...updatedTheme,
CustomImages: imageData,
};
if (saveTheme) {
themeData.coverImage = await blobToBase64(updatedTheme.coverImage as Blob);
} else {
themeData.coverImage = null;
}
browser.runtime.sendMessage({
type: 'currentTab',
info: 'UpdateThemePreview',
body: {
...updatedTheme,
CustomImages: imageData,
},
body: themeData,
save: saveTheme,
});
+20 -2
View File
@@ -33,7 +33,7 @@ function ThemeCreator() {
}
}) as CustomThemeBase64 | undefined;
if (theme) {
if (theme) {
// base64toblob to convert it to a blob url
const CustomImages = theme.CustomImages.map((image) => {
const base64Index = image.url.indexOf(',') + 1;
@@ -55,9 +55,27 @@ function ThemeCreator() {
};
});
const coverImageBase64 = theme.coverImage;
let coverImageBlob = null;
if (coverImageBase64) {
const base64Index = coverImageBase64.indexOf(',') + 1;
const imageBase64 = coverImageBase64.substring(base64Index);
// Convert base64 to blob
const byteCharacters = atob(imageBase64);
const byteNumbers = new Uint8Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
coverImageBlob = new Blob([byteArray], { type: 'image/png' });
}
setTheme({
...theme,
CustomImages,
coverImage: coverImageBlob
});
sendThemeUpdate({
@@ -189,7 +207,7 @@ function ThemeCreator() {
theme.coverImage && (
<>
<div className="absolute z-20 w-full h-full transition-opacity opacity-0 pointer-events-none group-hover:opacity-100 bg-black/20"></div>
<img src={URL.createObjectURL(theme.coverImage)} alt='Cover Image' className="absolute z-0 object-cover w-full h-full rounded" />
<img src={URL.createObjectURL(theme.coverImage as Blob)} alt='Cover Image' className="absolute z-0 object-cover w-full h-full rounded" />
</>
)
}
+1 -1
View File
@@ -7,7 +7,7 @@ export type CustomTheme = {
allowBackgrounds: boolean;
CustomCSS: string;
CustomImages: CustomImage[];
coverImage: Blob | null;
coverImage: Blob | string | null;
isEditable: boolean;
}