fix theme live preview

This commit is contained in:
SethBurkart123
2024-04-03 17:23:43 +11:00
parent c4be377b51
commit 9c4a499f63
3 changed files with 42 additions and 29 deletions
+24 -24
View File
@@ -61,42 +61,42 @@ export const deleteTheme = async (themeID: string) => {
}); });
} }
export const sendThemeUpdate = (updatedTheme: CustomTheme, saveTheme?: boolean) => { export const sendThemeUpdate = (updatedTheme: CustomTheme, saveTheme?: boolean, updateImages?: boolean) => {
// Create a copy of the updatedTheme object
const updatedThemeCopy: CustomTheme = { ...updatedTheme };
saveTheme = saveTheme || false; saveTheme = saveTheme || false;
// Send the updated theme (without image data) to the content script for live preview const imageDataPromises = updatedTheme.CustomImages.map(async (image) => {
browser.runtime.sendMessage({ if (saveTheme || updateImages) {
type: 'currentTab', console.log('Saving image:', image);
info: 'UpdateThemePreview', const base64 = await blobToBase64(image.blob);
body: { return {
...updatedThemeCopy,
CustomImages: updatedThemeCopy.CustomImages.map(image => ({
id: image.id, id: image.id,
variableName: image.variableName, variableName: image.variableName,
})), url: base64,
}, };
save: saveTheme, }
return {
id: image.id,
variableName: image.variableName,
url: ''
};
}); });
// Send image data separately Promise.all(imageDataPromises).then((imageData) => {
updatedThemeCopy.CustomImages.forEach(async (image) => { // Send the updated theme with image data to the content script for live preview or saving
const base64 = await blobToBase64(image.blob);
browser.runtime.sendMessage({ browser.runtime.sendMessage({
type: 'currentTab', type: 'currentTab',
info: 'UpdateThemeImageData', info: 'UpdateThemePreview',
body: { body: {
id: image.id, ...updatedTheme,
base64, CustomImages: imageData,
}, },
save: saveTheme,
}); });
});
if (saveTheme) { if (saveTheme) {
browser.runtime.sendMessage({ type: 'currentTab', info: 'CloseThemeCreator' }); browser.runtime.sendMessage({ type: 'currentTab', info: 'CloseThemeCreator' });
} }
});
}; };
// Helper function to convert a Blob to base64 // Helper function to convert a Blob to base64
+1 -1
View File
@@ -47,7 +47,7 @@ function ThemeCreator({ themeID }: { themeID?: string }) {
CustomImages: [...theme.CustomImages, { id: imageId, blob: imageBlob, variableName }], CustomImages: [...theme.CustomImages, { id: imageId, blob: imageBlob, variableName }],
}; };
setTheme(updatedTheme); setTheme(updatedTheme);
sendThemeUpdate(updatedTheme); sendThemeUpdate(updatedTheme, false, true);
}; };
reader.readAsDataURL(file); reader.readAsDataURL(file);
} }
+17 -4
View File
@@ -95,7 +95,7 @@ export const saveTheme = async (theme: CustomThemeBase64) => {
} }
}; };
export const UpdateThemePreview = async (updatedTheme: Omit<CustomTheme, 'CustomImages'> & { CustomImages: Omit<CustomImage, 'blob'>[] }) => { export const UpdateThemePreview = async (updatedTheme: CustomThemeBase64 /* Omit<CustomTheme, 'CustomImages'> & { CustomImages: Omit<CustomImage, 'blob'>[] } */) => {
const { CustomCSS, CustomImages, defaultColour } = updatedTheme; const { CustomCSS, CustomImages, defaultColour } = updatedTheme;
// Update image data // Update image data
@@ -120,6 +120,13 @@ export const UpdateThemePreview = async (updatedTheme: Omit<CustomTheme, 'Custom
removeImageFromDocument(existingImage.variableName); removeImageFromDocument(existingImage.variableName);
} }
if (image.url) {
UpdateImageData({
id: image.id,
base64: image.url
});
}
imageData[image.id] = { imageData[image.id] = {
url: '', url: '',
variableName: image.variableName, variableName: image.variableName,
@@ -139,9 +146,9 @@ export const UpdateImageData = (imageData2: { id: string; base64: string }) => {
const { id, base64 } = imageData2; const { id, base64 } = imageData2;
if (imageData[id]) { if (imageData[id]) {
imageData[id].url = base64; imageData[id].url = updateImage({ id, url: base64, variableName: imageData[id].variableName });
const { variableName } = imageData[id]; const { variableName } = imageData[id];
document.documentElement.style.setProperty('--' + variableName, `url(${base64})`); document.documentElement.style.setProperty('--' + variableName, `url(${imageData[id].url})`);
} }
}; };
@@ -180,7 +187,13 @@ export function updateImage(image: CustomImageBase64) {
} }
const applyTheme = async (theme: CustomTheme) => { const applyTheme = async (theme: CustomTheme) => {
const { CustomCSS, CustomImages, defaultColour } = theme; let CustomCSS = '';
let CustomImages: CustomImage[] = [];
let defaultColour = '';
if (theme?.CustomCSS) CustomCSS = theme.CustomCSS;
if (theme?.CustomImages) CustomImages = theme.CustomImages;
if (theme?.defaultColour) defaultColour = theme.defaultColour;
// Apply custom CSS // Apply custom CSS
applyCustomCSS(CustomCSS); applyCustomCSS(CustomCSS);