Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: read theme from document.__.theme #174

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 10 additions & 28 deletions src/utils/applyTheme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@ describe("buildCssOverridesStyle", () => {
it("should generate correct CSS with one override in c_theme", () => {
const document: Document = {
siteId: 123,
_customDataOverrides: {
pagesTheme: [
{
themeConfiguration: {
data: JSON.stringify({ "--colors-palette-text": "red" }),
siteId: 123,
},
},
],
__: {
theme: JSON.stringify({ "--colors-palette-text": "red" }),
},
};
const result = applyTheme(document, themeConfig);
Expand All @@ -34,19 +27,12 @@ describe("buildCssOverridesStyle", () => {
it("should generate correct CSS with multiple overrides in c_theme", () => {
const document: Document = {
siteId: 123,
_customDataOverrides: {
pagesTheme: [
{
themeConfiguration: {
data: JSON.stringify({
"--colors-palette-primary-DEFAULT": "hsl(0 68% 51%)",
"--colors-palette-primary-foreground": "hsl(0 0% 100%)",
"--borderRadius-border-lg": "20px",
}),
siteId: 123,
},
},
],
__: {
theme: JSON.stringify({
"--colors-palette-primary-DEFAULT": "hsl(0 68% 51%)",
"--colors-palette-primary-foreground": "hsl(0 0% 100%)",
"--borderRadius-border-lg": "20px",
}),
},
};

Expand Down Expand Up @@ -88,12 +74,8 @@ describe("buildCssOverridesStyle", () => {

it("should ignore saved values that are no longer in the themeConfig", () => {
const document: Document = {
_customDataOverrides: {
pagesTheme: [
{
themeConfiguration: { data: JSON.stringify({ "--absdag": "red" }) },
},
],
__: {
theme: JSON.stringify({ "--absdag": "red" }),
},
};
const result = applyTheme(document, themeConfig);
Expand Down
26 changes: 8 additions & 18 deletions src/utils/applyTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { ThemeConfig } from "./themeResolver.ts";

export type Document = {
[key: string]: any;
__?: {
layout?: string;
theme?: string;
codeTemplate?: string;
name?: string;
};
};

export const THEME_STYLE_TAG_ID = "visual-editor-theme";
Expand All @@ -19,24 +25,8 @@ export const applyTheme = (
): string => {
devLogger.logFunc("applyTheme");

const savedThemes: Record<string, any>[] =
document?._customDataOverrides?.pagesTheme ?? document?._site?.pagesTheme;

let savedTheme;
if (savedThemes?.length > 0) {
savedTheme =
document.siteId !== 0
? savedThemes.find(
(theme) =>
Number(theme.themeConfiguration.siteId) === document.siteId
)
: savedThemes[0]; // siteId is not set on local data documents, so default to the first one
}

let overrides;
if (savedTheme?.themeConfiguration) {
overrides = JSON.parse(savedTheme.themeConfiguration?.data);
}
const publishedTheme = document?.__?.theme;
const overrides = publishedTheme ? JSON.parse(publishedTheme) : undefined;

if (Object.keys(themeConfig).length > 0) {
return `${base ?? ""}${googleFontLinkTags}<style id="${THEME_STYLE_TAG_ID}" type="text/css">${internalApplyTheme(overrides, themeConfig)}</style>`;
Expand Down
Loading