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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
import {
forwardRef,
useEffect,
useRef,
type ComponentProps,
type ElementRef,
} from "react";
import { useStore } from "@nanostores/react";
import { mergeRefs } from "@react-aria/utils";
import { selectorIdAttribute } from "@webstudio-is/react-sdk";
import { ResourceForm } from "@webstudio-is/sdk-components-react/components";
import type { submitManagedForm } from "@webstudio-is/sdk-components-react";
import type { UploadProgress } from "@webstudio-is/sdk/form/browser";
import type { ManagedFormResponse } from "@webstudio-is/sdk/runtime";
import { $selectedPageHash } from "~/shared/nano-states/pages";
import { $currentSystem } from "~/shared/system";
import { publish } from "~/shared/pubsub";
import { readPreviewFormValues } from "~/shared/preview-form-values";
import { submitPreviewForm } from "~/shared/preview-form-bridge";
import { switchPageAndUpdateSystem } from "~/canvas/interceptor";
import { navigatePreviewFormSuccess } from "./form-success-redirect";
import { getPreviewCurrentUrl } from "./preview-current-url";
export const submitManagedFormFromPreview = (
managedFormId: string,
values: Parameters<typeof submitManagedForm>[0]["values"],
signal: AbortSignal,
onUploadProgress?: (progress: UploadProgress) => void
) => {
const currentUrl = getPreviewCurrentUrl(
$currentSystem.get(),
$selectedPageHash.get().hash
);
// The authenticated parent waits for durable saves before posting the draft.
return submitPreviewForm({
values,
managedFormId,
path: currentUrl.pathname + currentUrl.search,
signal,
onUploadProgress,
});
};
export const PreviewResourceForm = forwardRef<
ElementRef<typeof ResourceForm>,
ComponentProps<typeof ResourceForm>
>((props, ref) => {
const system = useStore($currentSystem);
const { hash } = useStore($selectedPageHash);
const formRef = useRef<HTMLFormElement>(null);
const onStateChangeRef = useRef(props.onStateChange);
onStateChangeRef.current = props.onStateChange;
const onProgressChangeRef = useRef(props.onProgressChange);
onProgressChangeRef.current = props.onProgressChange;
const formSelector = (props as Record<string, unknown>)[selectorIdAttribute];
useEffect(
() => () => {
onStateChangeRef.current?.("initial");
onProgressChangeRef.current?.({
status: "idle",
loaded: 0,
total: 0,
files: [],
});
},
[]
);
const formId = props["data-ws-managed-form-id"];
useEffect(() => {
const form = formRef.current;
if (!form || !formId) {
return;
}
let active = true;
const send = () =>
queueMicrotask(() => {
if (active) {
publish({
type: "previewFormValues",
payload: {
selector: String(formSelector),
values: readPreviewFormValues(form),
},
});
}
});
form.addEventListener("input", send);
form.addEventListener("change", send);
const observer = new MutationObserver(send);
observer.observe(form, {
subtree: true,
childList: true,
attributes: true,
attributeFilter: [
"value",
"checked",
"selected",
"disabled",
"name",
"type",
],
});
send();
return () => {
active = false;
observer.disconnect();
form.removeEventListener("input", send);
form.removeEventListener("change", send);
publish({
type: "previewFormValues",
payload: { selector: String(formSelector), values: null },
});
};
}, [formId, formSelector]);
return (
<ResourceForm
{...props}
ref={mergeRefs(ref, formRef)}
navigationToken={getPreviewCurrentUrl(system, hash).href}
onQueryNavigate={(url) => {
// The Form element's relative action resolves against the Builder
// canvas URL. Apply its submitted query to the previewed page instead.
const submittedUrl = new URL(url, window.location.href);
const previewUrl = getPreviewCurrentUrl(system, hash);
switchPageAndUpdateSystem(
`${previewUrl.pathname}${submittedUrl.search}${submittedUrl.hash}`
);
}}
/>
);
});
export const getPreviewResourceFormProps = (formId: string) => {
const getPreviewUrl = () =>
getPreviewCurrentUrl($currentSystem.get(), $selectedPageHash.get().hash);
return {
"data-ws-managed-form-id": formId,
getRedirectBaseUrl: () => getPreviewUrl().href,
onManagedSubmit: (
values: Parameters<typeof submitManagedForm>[0]["values"],
signal: AbortSignal,
onUploadProgress?: (progress: UploadProgress) => void
): Promise<ManagedFormResponse> =>
submitManagedFormFromPreview(formId, values, signal, onUploadProgress),
onSuccessRedirect: (destination: string) =>
navigatePreviewFormSuccess(
destination,
getPreviewUrl().href,
(path) => switchPageAndUpdateSystem(path, { includeFallback: false }),
(href) => window.location.assign(href)
),
};
};