Skip to content

Commit

Permalink
Merge pull request #1919 from ThemeFuse/23623-screenshots
Browse files Browse the repository at this point in the history
feat(api-client): added screenshots
  • Loading branch information
maxval1 authored Aug 9, 2023
2 parents a968935 + bed9d77 commit 2c23d21
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 2 deletions.
2 changes: 2 additions & 0 deletions public/editor-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export interface __BRZ_PLUGIN_ENV__ {
getLayoutByUid: string;
updateLayout: string;
deleteLayout: string;
createBlockScreenshot: string
updateBlockScreenshot: string
};
api: {
mediaResizeUrl: string;
Expand Down
85 changes: 85 additions & 0 deletions public/editor-client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SavedLayout,
SavedLayoutMeta
} from "../types/SavedBlocks";
import { ScreenshotData } from "../types/Screenshots";
import { t } from "../utils/i18n";
import {
GetCollections,
Expand Down Expand Up @@ -763,3 +764,87 @@ export const updatePopupRules = async (
};

//#endregion

//#region Screenshots

export const createBlockScreenshot = async ({
base64,
blockType
}: ScreenshotData): Promise<{ id: string }> => {
const config = getConfig();

if (!config) {
throw new Error(t("Invalid __BRZ_PLUGIN_ENV__"));
}

const { pageId, url, hash, editorVersion, actions } = config;
const attachment = base64.replace(/data:image\/.+;base64,/, "");
const _url = makeUrl(url, {
hash,
action: actions.createBlockScreenshot,
post: pageId,
version: editorVersion
});
const body = new URLSearchParams({
block_type: blockType,
ibsf: attachment // ibsf - image base64
});

try {
const r = await request(_url, { method: "POST", body });
const d = await r.json();

if (d?.data?.id) {
return { id: d.data.id };
}

throw new Error(t("Failed to create Screenshot"));
} catch (e) {
throw new Error(t("Failed to create Screenshot"));
}
};

interface UpdateScreenshot extends ScreenshotData {
id: string;
}

export const updateBlockScreenshot = async ({
id,
base64,
blockType
}: UpdateScreenshot): Promise<{ id: string }> => {
const config = getConfig();

if (!config) {
throw new Error(t("Invalid __BRZ_PLUGIN_ENV__"));
}

const { pageId, url, hash, editorVersion, actions } = config;
const attachment = base64.replace(/data:image\/.+;base64,/, "");
const _url = makeUrl(url, {
hash,
action: actions.updateBlockScreenshot,
post: pageId,
version: editorVersion
});
const body = new URLSearchParams({
id,
block_type: blockType,
ibsf: attachment
});

try {
const r = await request(_url, { method: "POST", body });
const d = await r.json();

if (d?.data?.id) {
return { id: d.data.id };
}

throw new Error(t("Failed to update Screenshot"));
} catch (e) {
throw new Error(t("Failed to update Screenshot"));
}
};

//#endregion
13 changes: 12 additions & 1 deletion public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ interface Actions {
getPostObjects: string;

searchPosts: string;

createBlockScreenshot: string;
updateBlockScreenshot: string;
}

interface API {
Expand Down Expand Up @@ -178,7 +181,15 @@ const actionsReader = parseStrict<PLUGIN_ENV["actions"], Actions>({
),
searchPosts: pipe(
mPipe(Obj.readKey("searchPosts"), Str.read),
throwOnNullish("INvalid actions: searchPosts")
throwOnNullish("Invalid actions: searchPosts")
),
createBlockScreenshot: pipe(
mPipe(Obj.readKey("createBlockScreenshot"), Str.read),
throwOnNullish("Invalid actions: createBlockScreenshot")
),
updateBlockScreenshot: pipe(
mPipe(Obj.readKey("updateBlockScreenshot"), Str.read),
throwOnNullish("Invalid actions: updateBlockScreenshot")
)
});

Expand Down
4 changes: 3 additions & 1 deletion public/editor-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { publish } from "./publish";
import { savedBlocks } from "./savedBlocks/savedBlocks";
import { savedLayouts } from "./savedBlocks/savedLayouts";
import { savedPopups } from "./savedBlocks/savedPopups";
import { screenshots } from "./screenshots";

const config = getConfig();

Expand Down Expand Up @@ -51,7 +52,8 @@ const api = {
},
collectionTypes: {
loadCollectionTypes
}
},
screenshots: screenshots()
};

if (window.__VISUAL_CONFIG__) {
Expand Down
26 changes: 26 additions & 0 deletions public/editor-client/src/screenshots/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createBlockScreenshot, updateBlockScreenshot } from "../api";
import { Screenshots } from "../types/Screenshots";
import { t } from "../utils/i18n";

export const screenshots = (): Screenshots => {
return {
async create(res, rej, data) {
try {
const r = await createBlockScreenshot(data);
res(r);
} catch (e) {
console.error("API Client Create Screenshots:", e);
rej(t("Failed to create screenshot"));
}
},
async update(res, rej, data) {
try {
const r = await updateBlockScreenshot(data);
res(r);
} catch (e) {
console.error("API Client Update Screenshots:", e);
rej(t("Failed to update screenshot"));
}
}
};
};
19 changes: 19 additions & 0 deletions public/editor-client/src/types/Screenshots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Response } from "./Response";

export interface ScreenshotData {
base64: string;
blockType: "normal" | "global" | "saved" | "layout";
}

export interface Screenshots {
create?: (
res: Response<{ id: string }>,
rej: Response<string>,
extra: ScreenshotData
) => void;
update?: (
res: Response<{ id: string }>,
rej: Response<string>,
extra: ScreenshotData & { id: string }
) => void;
}
4 changes: 4 additions & 0 deletions public/editor-client/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { OnChange } from "./OnChange";
import { PopupConditions } from "./PopupConditions";
import { PublishData } from "./Project";
import { SavedBlocks, SavedLayouts, SavedPopups } from "./SavedBlocks";
import { Screenshots } from "./Screenshots";

declare class WPMediaLibrary {
get: (selector: string) => import("backbone").Collection;
Expand Down Expand Up @@ -99,6 +100,9 @@ export interface VISUAL_CONFIG {
// PopupConditions
popupConditions?: PopupConditions;

// Screenshots
screenshots?: Screenshots;

defaultKits?: DefaultTemplate<Array<KitsWithThumbs>, DefaultBlock>;
defaultPopups?: DefaultTemplate<PopupsWithThumbs, DefaultBlockWithID>;
defaultLayouts?: DefaultTemplate<
Expand Down

0 comments on commit 2c23d21

Please sign in to comment.