Skip to content

Commit

Permalink
WP Ai Global Styles
Browse files Browse the repository at this point in the history
  • Loading branch information
GunkaArtur committed May 2, 2024
1 parent 695ac3c commit 7d60fd6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Brizy_Config {
const CLOUD_SCREENSHOT = '/screenshot/%s';
const CLOUD_SCREENSHOTS = '/api/screenshots';
const CLOUD_CUSTOM_FILES = '/api/custom_files';
const GENERATE_GLOBAL_STYLES_ENDPOINT = 'https://phplaravel-1109775-4163586.cloudwaysapps.com';
const WP_HTTP_TIMEOUT = 600;


Expand Down
1 change: 1 addition & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Brizy_Config {
const CLOUD_SCREENSHOT = '/screenshot/%s';
const CLOUD_SCREENSHOTS = '/api/screenshots';
const CLOUD_CUSTOM_FILES = '/api/custom_files';
const GENERATE_GLOBAL_STYLES_ENDPOINT = 'https://phplaravel-1109775-4163586.cloudwaysapps.com';
const WP_HTTP_TIMEOUT = 30;

// this file will be stored in uploads/brizy/
Expand Down
1 change: 1 addition & 0 deletions editor/editor/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function getClientConfig($context)
'project' => array(
'status' => $this->getProjectStatus(),
),
'aiGlobalStyleUrl' => Brizy_Config::GENERATE_GLOBAL_STYLES_ENDPOINT
];

$config = $this->getApiConfigFields($config, $context);
Expand Down
30 changes: 30 additions & 0 deletions public/editor-client/src/aiGlobalStyles/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { getStyles, getTypography } from "@/api";
import { Config } from "@/config";
import { t } from "@/utils/i18n";
import { AiGlobalStyles, FontStyle, Palette } from "../types/DefaultTemplate";

export const getAIGlobalStyles = (
config: Config
): AiGlobalStyles<Palette, FontStyle> => {
return {
async regenerateColors(res, rej) {
try {
const styles = await getStyles(config);

res(styles.colorPalette);
} catch (e) {
rej(t("Failed to load meta.json"));
}
},
async regenerateTypography(res, rej) {
try {
const data = await getTypography(config);

res(data.fontStyles);
} catch (e) {
rej(t("Failed to load resolves for selected DefaultTemplate"));
}
},
label: t("Regenerate with AI")
};
};
20 changes: 20 additions & 0 deletions public/editor-client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1336,3 +1336,23 @@ export const deleteIcon = async (uid: string): Promise<Response> => {
throw new Error(t("Failed to delete icon"));
};
//#endregion

//#region AI Global Styles

export const getStyles = async (config: Config) => {
const { aiGlobalStyleUrl } = config;

return await fetch(`${aiGlobalStyleUrl}/api/template/style`).then((r) =>
r.json()
);
};

export const getTypography = async (config: Config) => {
const { aiGlobalStyleUrl } = config;

return await fetch(`${aiGlobalStyleUrl}/api/template/typography`).then((r) =>
r.json()
);
};

//#endregion
5 changes: 5 additions & 0 deletions public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface Config {
api: API;
l10n?: Record<string, string>;
collectionTypes: CollectionType[];
aiGlobalStyleUrl: string;
}

const templatesReader = parseStrict<Record<string, unknown>, DefaultTemplates>({
Expand Down Expand Up @@ -355,6 +356,10 @@ const reader = parseStrict<PLUGIN_ENV, Config>({
})
),
throwOnNullish("Invalid: project")
),
aiGlobalStyleUrl: pipe(
mPipe(Obj.readKey("aiGlobalStyleUrl"), Str.read),
throwOnNullish("Invalid: aiGlobalStyleUrl")
)
});

Expand Down
6 changes: 6 additions & 0 deletions public/editor-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import merge from "lodash/merge";
import set from "lodash/set";
import { getAIGlobalStyles } from "./aiGlobalStyles";
import { doAiRequest } from "./aiText";
import { autoSave } from "./autoSave";
import { getCollectionItemsIds } from "./collectionItems/getCollectionItemsIds";
Expand Down Expand Up @@ -90,6 +91,11 @@ if (window.__VISUAL_CONFIG__) {
// UI
if (window.__VISUAL_CONFIG__.ui) {
window.__VISUAL_CONFIG__.ui.publish = publish;
set(
window.__VISUAL_CONFIG__.ui,
["leftSidebar", "styles"],
getAIGlobalStyles(config)
);
}

// Elements
Expand Down
13 changes: 11 additions & 2 deletions public/editor-client/src/types/DefaultTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ export interface DefaultBlock {
value: Record<string, unknown>;
}

export interface AiGlobalStyles<T1, T2> {
regenerateColors: (res: Response<T1>, rej: Response<string>) => void;
regenerateTypography: (
res: Response<Promise<T2>>,
rej: Response<string>
) => void;
label: string;
}

export interface DefaultTemplate<T1, T2> {
label?: string;
getMeta: (res: Response<T1>, rej: Response<string>) => void;
Expand Down Expand Up @@ -101,14 +110,14 @@ export interface Style {
fontStyles: Array<FontStyle>;
}

interface Palette {
export interface Palette {
id: string;
hex: string;
}

type fontSizeUnits = "px" | "%";

interface FontStyle {
export interface FontStyle {
id: number | string;
title: string;
deletable: "on" | "off";
Expand Down

0 comments on commit 7d60fd6

Please sign in to comment.