mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-09 05:26:24 +09:00
## Description closes #3872 - [x] - Builder support - [x] - Publish support ## Steps for reproduction Example https://add-json.staging.webstudio.is/builder/8160e16d-5ab4-4249-86de-0c98a64526a3 https://test-json-ki9yj.wstd.work/ ## 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: 5de6) - [ ] 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
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import { createContext, useContext, useMemo } from "react";
|
|
import type { ImageLoader } from "@webstudio-is/image";
|
|
import { createJsonStringifyProxy, isPlainObject } from "@webstudio-is/sdk";
|
|
|
|
export type Params = {
|
|
/**
|
|
* When rendering a published version, there is no renderer defined.
|
|
* - canvas is the builder canvas in dev mode
|
|
* - preview is the preview mode in builder
|
|
*/
|
|
renderer?: "canvas" | "preview";
|
|
/**
|
|
* Base url ir base path for images with ending slash.
|
|
* Used for configuring image with different sizes.
|
|
* Concatinated with "name?width=&quality=&format=".
|
|
*
|
|
* For example
|
|
* /asset/image/ used by default in builder
|
|
* https://image-transform.wstd.io/cdn-cgi/image/
|
|
* https://webstudio.is/cdn-cgi/image/
|
|
*/
|
|
imageBaseUrl: string;
|
|
/**
|
|
* Base url or base path for any asset with ending slash.
|
|
* Used to load assets like fonts or images in styles
|
|
* Concatinated with "name".
|
|
*
|
|
* For example
|
|
* /s/uploads/
|
|
* /asset/file/
|
|
* https://assets-dev.webstudio.is/
|
|
* https://assets.webstudio.is/
|
|
*/
|
|
assetBaseUrl: string;
|
|
};
|
|
|
|
export const ReactSdkContext = createContext<
|
|
Params & {
|
|
imageLoader: ImageLoader;
|
|
// resources need to be any to support accessing unknown fields without extra checks
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
resources: Record<string, any>;
|
|
}
|
|
>({
|
|
assetBaseUrl: "/",
|
|
imageBaseUrl: "/",
|
|
imageLoader: ({ src }) => src,
|
|
resources: {},
|
|
});
|
|
|
|
export const useResource = (name: string) => {
|
|
const { resources } = useContext(ReactSdkContext);
|
|
const resource = resources[name];
|
|
|
|
const resourceMemozied = useMemo(
|
|
() =>
|
|
isPlainObject(resource) ? createJsonStringifyProxy(resource) : resource,
|
|
[resource]
|
|
);
|
|
|
|
return resourceMemozied;
|
|
};
|