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

23680 wp editor client screenshot url #1923

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ interface DefaultTemplates {
layoutsUrl: string;
}

interface ScreenshotUrl {
globalScreenshotUrl: string;
layoutScreenshotUrl: string;
normalScreenshotUrl: string;
savedScreenshotUrl: string;
}
interface Actions {
getMediaUid: string;
getAttachmentUid: string;
Expand All @@ -37,6 +43,7 @@ interface API {
mediaResizeUrl: string;
customFileUrl: string;
templates: DefaultTemplates;
screenshots: ScreenshotUrl;
}
export interface Config {
hash: string;
Expand All @@ -48,6 +55,27 @@ export interface Config {
l10n?: Record<string, string>;
}

const screenshotUrlReader = parseStrict<Record<string, unknown>, ScreenshotUrl>(
{
globalScreenshotUrl: pipe(
mPipe(Obj.readKey("globalScreenshotUrl"), Str.read),
throwOnNullish("Invalid API Config: globalScreenshotUrl")
),
layoutScreenshotUrl: pipe(
mPipe(Obj.readKey("layoutScreenshotUrl"), Str.read),
throwOnNullish("Invalid API Config: layoutScreenshotUrl")
),
normalScreenshotUrl: pipe(
mPipe(Obj.readKey("normalScreenshotUrl"), Str.read),
throwOnNullish("Invalid API Config: normalScreenshotUrl")
),
savedScreenshotUrl: pipe(
mPipe(Obj.readKey("savedScreenshotUrl"), Str.read),
throwOnNullish("Invalid API Config: savedScreenshotUrl")
)
}
);

const templatesReader = parseStrict<Record<string, unknown>, DefaultTemplates>({
kitsUrl: pipe(
mPipe(Obj.readKey("kitsUrl"), Str.read),
Expand Down Expand Up @@ -89,6 +117,10 @@ const apiReader = parseStrict<PLUGIN_ENV["api"], API>({
templates: pipe(
mPipe(Obj.readKey("templates"), Obj.read, templatesReader),
throwOnNullish("Invalid API: templates")
),
screenshots: pipe(
mPipe(Obj.readKey("screenshots"), Obj.read, screenshotUrlReader),
throwOnNullish("Invalid API: screenshots")
)
});

Expand Down
19 changes: 19 additions & 0 deletions public/editor-client/src/savedBlocks/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { SavedLayoutMeta } from "../types/SavedBlocks";
// Limitation API for getBlocks
export const TOTAL_COUNT = 200;
export const normalBlocks = (
data: SavedLayoutMeta[],
screenshotUrl: string
) => {
return data
.filter((item) => item.meta.type === "normal")
.map((item) => {
return {
...item,
meta: {
...item.meta,
_thumbnailSrc: `${screenshotUrl}/${item.meta._thumbnailSrc}`
}
};
});
};
4 changes: 1 addition & 3 deletions public/editor-client/src/savedBlocks/savedBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
import { SavedBlockMeta, SavedBlocks } from "../types/SavedBlocks";
import { createUpload } from "../utils/createUpload";
import { t } from "../utils/i18n";

// Limitation API for getBlocks
const TOTAL_COUNT = 200;
import { TOTAL_COUNT } from "./common";

export const savedBlocks: SavedBlocks = {
async get(res, rej) {
Expand Down
4 changes: 1 addition & 3 deletions public/editor-client/src/savedBlocks/savedLayouts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
import { SavedLayoutMeta, SavedLayouts } from "../types/SavedBlocks";
import { createUpload } from "../utils/createUpload";
import { t } from "../utils/i18n";

// Limitation API for getBlocks
const TOTAL_COUNT = 200;
import { TOTAL_COUNT } from "./common";

export const savedLayouts: SavedLayouts = {
async get(res, rej) {
Expand Down
4 changes: 1 addition & 3 deletions public/editor-client/src/savedBlocks/savedPopups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
import { SavedBlockMeta, SavedPopups } from "../types/SavedBlocks";
import { createUpload } from "../utils/createUpload";
import { t } from "../utils/i18n";

// Limitation API for getBlocks
const TOTAL_COUNT = 200;
import { TOTAL_COUNT } from "./common";

export const savedPopups: SavedPopups = {
async get(res, rej) {
Expand Down
28 changes: 28 additions & 0 deletions public/editor-client/src/screenshots/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createBlockScreenshot, updateBlockScreenshot } from "../api";
import { Screenshots } from "../types/Screenshots";
import { t } from "../utils/i18n";
import { getScreenshotUrl } from "./utils";

export const screenshots = (): Screenshots => {
return {
getScreenshotUrl,
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"));
}
}
};
};
33 changes: 33 additions & 0 deletions public/editor-client/src/screenshots/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Config, getConfig } from "../config";
import { ScreenshotType } from "../types/Screenshots";
import { MValue } from "../utils/types";
import { urlContainsQueryString } from "../utils/url/urlContainerQueryString";
export const getScreenshotUrl = ({
_thumbnailSrc,
type
}: ScreenshotType): string => {
const config = getConfig();
const siteUrl = determineScreenshotUrl(type, config);

return urlContainsQueryString(siteUrl)
? `${siteUrl}&${_thumbnailSrc}`
: `${siteUrl}?${_thumbnailSrc}`;
};

const determineScreenshotUrl = (
type: string,
config: MValue<Config>
): string => {
switch (type) {
case "normal":
return config?.api.screenshots.normalScreenshotUrl ?? "";
case "global":
return config?.api.screenshots.globalScreenshotUrl ?? "";
case "saved":
return config?.api.screenshots.savedScreenshotUrl ?? "";
case "layout":
return config?.api.screenshots.layoutScreenshotUrl ?? "";
default:
return "";
}
};
25 changes: 25 additions & 0 deletions public/editor-client/src/types/Screenshots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Response } from "./Response";

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

export type ScreenshotType = {
_thumbnailSrc: string;
type: string;
};

export interface Screenshots {
getScreenshotUrl?: ({ _thumbnailSrc, type }: ScreenshotType) => string;
create?: (
res: Response<{ id: string }>,
rej: Response<string>,
extra: ScreenshotData
) => void;
update?: (
res: Response<{ id: string }>,
rej: Response<string>,
extra: ScreenshotData & { id: string }
) => void;
}
2 changes: 2 additions & 0 deletions public/editor-client/src/utils/url/urlContainerQueryString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const urlContainsQueryString = (url: string): boolean =>
url.includes("?");