Files
Oleg Isonen a1b2123f6a chore: Refactor assets without any changes in functionality (#5560)
pure code organizational refactoring

## Description

1. What is this PR about (link the issue and add a short description)

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
0000)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2026-01-08 16:53:43 +01:00

111 lines
2.7 KiB
TypeScript

import { createRecursiveProxy } from "@trpc/server/shared";
import invariant from "tiny-invariant";
import { toast } from "@webstudio-is/design-system";
import { uploadAssets } from "~/builder/shared/assets/upload-assets";
import { showTokenConflictDialog } from "./token-conflict-dialog";
const apiWindowNamespace = "__webstudio__$__builderApi";
type ToastHandler = (message: string) => void;
const _builderApi = {
isInitialized: () => true,
toast: {
info: toast.info as ToastHandler,
warn: toast.warn as ToastHandler,
error: toast.error as ToastHandler,
success: toast.success as ToastHandler,
},
uploadImages: async (srcs: string[]) => {
const urlToIds = await uploadAssets(
"image",
srcs.map((src) => new URL(src))
);
return new Map([...urlToIds.entries()].map(([url, id]) => [url.href, id]));
},
showTokenConflictDialog,
};
declare global {
interface Window {
[apiWindowNamespace]: typeof _builderApi;
}
}
const isInTop = () => {
try {
return window.self === window.top;
} catch {
return true;
}
};
const getTopApi = () => {
if (isInTop()) {
// Inside the iframe, use the local window.api
return _builderApi;
} else {
// Find first iframe with the API
invariant(window.top);
return window.top[apiWindowNamespace];
}
};
const isKeyOf = <T>(key: unknown, obj: T): key is keyof T => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return key in obj;
};
/**
* Forwards the call from the builder to the iframe, invoking the original API in the iframe.
*/
export const builderApi = createRecursiveProxy((options) => {
const api = getTopApi();
if (api == null) {
if (
options.path.join(".") ===
("isInitialized" satisfies keyof typeof _builderApi)
) {
return false;
}
console.warn(
`API not found in the iframe, skipping ${options.path.join(".")} call, iframe probably not loaded yet`
);
return null;
}
let currentMethod = api as unknown;
for (const key of options.path) {
invariant(
isKeyOf(key, currentMethod),
`API method ${options.path.join(".")} not found`
);
invariant(typeof currentMethod === "object");
invariant(currentMethod != null);
currentMethod = currentMethod[key];
}
invariant(
typeof currentMethod === "function",
`API method ${options.path.join(".")} is not a function`
);
return currentMethod.call(null, ...options.args);
}) as typeof _builderApi;
/**
* Initializes the builder API in the window. Must be called in the builder context.
*/
export const initBuilderApi = () => {
if (isInTop()) {
window[apiWindowNamespace] = _builderApi;
}
return () => {};
};