Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

3.6 KB
Raw Blame
import {
  TemplateValidationError,
  ActionValue,
  AssetValue,
  PageValue,
  Parameter,
  PlaceholderValue,
  ResourceValue,
  Variable,
  expression,
  parseTemplateCss,
  token,
  ws,
} from "@webstudio-is/template";
import { hasUnsupportedCssTemplateRules } from "@webstudio-is/css-data";
import { getComponentByJsxName, getComponentJsxName } from "@webstudio-is/sdk";
import { componentsById } from "@webstudio-is/sdk-components-registry/components";
import { isMdxTemplateComponentName } from "@webstudio-is/content-engine/mdx";

const css = (strings: TemplateStringsArray, ...values: string[]) => {
  const source = String.raw({ raw: strings }, ...values);
  if (hasUnsupportedCssTemplateRules(source)) {
    throw new TemplateValidationError(
      "css templates do not support selectors or at-rules such as @keyframes. Use declarations and @media rules only. Use Webstudio Animation components for editable animations."
    );
  }
  return parseTemplateCss(source);
};

const directComponentBindings = Object.fromEntries(
  Array.from(componentsById.keys()).flatMap((candidate) => {
    const jsxName = getComponentJsxName({
      component: candidate,
      components: componentsById.keys(),
    });
    if (
      jsxName === "Fragment" ||
      isMdxTemplateComponentName(jsxName) === false
    ) {
      return [];
    }
    const component = getComponentByJsxName({
      name: jsxName,
      components: componentsById.keys(),
    });
    const componentObject =
      component === undefined ? undefined : componentsById.get(component);
    return componentObject === undefined ? [] : [[jsxName, componentObject]];
  })
);

const componentBindings = {
  ...directComponentBindings,
  ws,
};

const helperBindings = {
  css,
  token,
  expression,
  Variable,
  Parameter,
  ResourceValue,
  ActionValue,
  AssetValue,
  PageValue,
  PlaceholderValue,
};

export const webstudioJsxRuntimeBindings = {
  ...componentBindings,
  ...helperBindings,
};

export const webstudioJsxRootNames = new Set(Object.keys(componentBindings));

export const webstudioJsxCompilerPrimitiveNames = new Set(Object.keys(ws));

const listFormatter = new Intl.ListFormat("en", {
  style: "long",
  type: "conjunction",
});

const webstudioJsxHelperNames = listFormatter.format(
  Object.keys(helperBindings)
);

export const webstudioJsxBindingGuidance = `lowercase HTML elements, unique direct component identifiers, compiler primitives from ws, and value helpers ${webstudioJsxHelperNames}`;

export const webstudioJsxFragmentInputDescription =
  `Webstudio JSX fragment. Use ${webstudioJsxBindingGuidance}. ` +
  'Do not access host globals or dynamic code APIs such as process, globalThis, eval, Function, or constructor. Use Webstudio prop names such as class and for, not React aliases className or htmlFor. Use ws:style={css`...`} for Webstudio-native styles, or style={{ padding: 24 }} for React-style object syntax that is converted into editable Webstudio styles. Use Webstudio actions such as onClick={new ActionValue(["event"], expression`console.log(event)`)}; do not pass JavaScript functions such as onClick={() => ...}. Plain prop values must be JSON-compatible: use null, strings, booleans, finite numbers, arrays, and plain objects; do not pass undefined, Symbol, BigInt, NaN, Infinity, Date, Map, Set, class instances, or circular objects. Template-backed components used in JSX must include required child/part components explicitly under the same parent structure as the template; use insert-component when you want Webstudio to apply one registered component template automatically. Do not set ws:id; ids are generated by the runtime. The JSX is converted locally before the mutation request is sent.';