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.

feat: insert marketplace pages (#3425)

Bogdan Chadkined9fc0088371
diff --git a/apps/builder/app/builder/features/sidebar-left/panels/marketplace/marketplace.tsx b/apps/builder/app/builder/features/sidebar-left/panels/marketplace/marketplace.tsx
index 8cdcf5f25f..479f722786 100644
--- a/apps/builder/app/builder/features/sidebar-left/panels/marketplace/marketplace.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/panels/marketplace/marketplace.tsx
@@ -58,6 +58,7 @@ export const TabContent = ({ onSetActiveTab }: TabContentProps) => {
<Templates
projectId={activeOverviewItem.projectId}
name={activeOverviewItem.name}
+ productCategory={activeOverviewItem.category}
data={toWebstudioData(buildData)}
onOpenChange={(isOpen: boolean) => {
if (isOpen === false) {
diff --git a/apps/builder/app/builder/features/sidebar-left/panels/marketplace/templates.tsx b/apps/builder/app/builder/features/sidebar-left/panels/marketplace/templates.tsx
index 3079a8488c..84dd9d2f59 100644
--- a/apps/builder/app/builder/features/sidebar-left/panels/marketplace/templates.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/panels/marketplace/templates.tsx
@@ -1,3 +1,4 @@
+import { useMemo } from "react";
import {
Button,
Flex,
@@ -9,17 +10,67 @@ import {
Link,
} from "@webstudio-is/design-system";
import { ChevronLeftIcon, ExternalLinkIcon } from "@webstudio-is/icons";
-import { insert } from "./utils";
+import {
+ ROOT_FOLDER_ID,
+ type Asset,
+ type Page,
+ type WebstudioData,
+} from "@webstudio-is/sdk";
+import type { MarketplaceProduct } from "@webstudio-is/project-build";
import { computeExpression } from "~/shared/nano-states";
import { CollapsibleSection } from "~/builder/shared/collapsible-section";
-import type { Asset, WebstudioData } from "@webstudio-is/sdk";
-import { useMemo } from "react";
import { builderUrl } from "~/shared/router-utils";
+import {
+ extractWebstudioFragment,
+ findTargetAndInsertFragment,
+ updateWebstudioData,
+} from "~/shared/instance-utils";
+import { insertPageCopyMutable } from "~/shared/page-utils";
+import { switchPage } from "~/shared/pages";
import { Card } from "./card";
+/**
+ * Insert page as a template.
+ * - Currently only supports inserting everything from the body
+ * - Could be extended to support children of some other instance e.g. Marketplace Item
+ */
+const insertSection = ({
+ data,
+ instanceId,
+}: {
+ data: WebstudioData;
+ instanceId: string;
+}) => {
+ const fragment = extractWebstudioFragment(data, instanceId);
+ fragment.instances = fragment.instances.filter(
+ (instance) => instance.component !== "Body"
+ );
+ findTargetAndInsertFragment(fragment);
+};
+
+const insertPage = ({
+ data: sourceData,
+ pageId,
+}: {
+ data: WebstudioData;
+ pageId: Page["id"];
+}) => {
+ let newPageId: undefined | Page["id"];
+ updateWebstudioData((targetData) => {
+ newPageId = insertPageCopyMutable({
+ source: { data: sourceData, pageId },
+ target: { data: targetData, folderId: ROOT_FOLDER_ID },
+ });
+ });
+ if (newPageId) {
+ switchPage(newPageId);
+ }
+};
+
type TemplateData = {
title?: string;
thumbnailAsset?: Asset;
+ pageId: string;
rootInstanceId: string;
};
@@ -71,6 +122,7 @@ const getTemplatesDataByCategory = (data?: WebstudioData) => {
templates.push({
title: page.name,
thumbnailAsset: thumbnailImageAsset ?? socialImageAsset,
+ pageId: page.id,
rootInstanceId: page.rootInstanceId,
});
}
@@ -80,11 +132,13 @@ const getTemplatesDataByCategory = (data?: WebstudioData) => {
export const Templates = ({
name,
projectId,
+ productCategory,
data,
onOpenChange,
}: {
name: string;
projectId: string;
+ productCategory: MarketplaceProduct["category"];
data: WebstudioData;
onOpenChange: (isOpen: boolean) => void;
}) => {
@@ -145,10 +199,18 @@ export const Templates = ({
key={templateData.rootInstanceId}
index={index}
onSelect={() => {
- insert({
- instanceId: templateData.rootInstanceId,
- data,
- });
+ if (productCategory === "sectionTemplates") {
+ insertSection({
+ data,
+ instanceId: templateData.rootInstanceId,
+ });
+ }
+ if (productCategory === "pageTemplates") {
+ insertPage({
+ data,
+ pageId: templateData.pageId,
+ });
+ }
}}
>
<Card
diff --git a/apps/builder/app/builder/features/sidebar-left/panels/marketplace/utils.ts b/apps/builder/app/builder/features/sidebar-left/panels/marketplace/utils.ts
index 27d3ddba75..cb66aecb2d 100644
--- a/apps/builder/app/builder/features/sidebar-left/panels/marketplace/utils.ts
+++ b/apps/builder/app/builder/features/sidebar-left/panels/marketplace/utils.ts
@@ -1,13 +1,6 @@
-import { atom } from "nanostores";
-import {
- extractWebstudioFragment,
- findTargetAndInsertFragment,
-} from "~/shared/instance-utils";
import type { WebstudioData } from "@webstudio-is/sdk";
import type { BuildData } from "~/shared/marketplace/types";
-export const $activeProductData = atom<WebstudioData | undefined>();
-
export const toWebstudioData = (data: BuildData): WebstudioData => ({
pages: data.build.pages,
assets: new Map(data.assets),
@@ -20,22 +13,3 @@ export const toWebstudioData = (data: BuildData): WebstudioData => ({
breakpoints: new Map(data.build.breakpoints),
styles: new Map(data.build.styles),
});
-
-/**
- * Insert page as a template.
- * - Currently only supports inserting everything from the body
- * - Could be extended to support children of some other instance e.g. Marketplace Item
- */
-export const insert = ({
- instanceId,
- data,
-}: {
- instanceId: string;
- data: WebstudioData;
-}) => {
- const fragment = extractWebstudioFragment(data, instanceId);
- fragment.instances = fragment.instances.filter(
- (instance) => instance.component !== "Body"
- );
- findTargetAndInsertFragment(fragment);
-};