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
Add insertTemplate helper (#1683)
Giuseppe6d1e9019503c
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex ef4fd426f9..36bb54dc53 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -1,43 +1,44 @@-import store from "immerhin";import { findTreeInstanceIdsExcludingSlotDescendants } from "@webstudio-is/project-build";import {generateDataFromEmbedTemplate,getComponentMeta,+ type WsEmbedTemplate,} from "@webstudio-is/react-sdk";+import store from "immerhin";+import { removeByMutable } from "./array-utils";+import { isBaseBreakpoint } from "./breakpoints";import {+ breakpointsStore,+ instancesStore,propsStore,- stylesStore,selectedInstanceSelectorStore,+ selectedStyleSourceSelectorStore,styleSourceSelectionsStore,styleSourcesStore,- instancesStore,- selectedStyleSourceSelectorStore,+ stylesStore,textEditingInstanceSelectorStore,- breakpointsStore,} from "./nano-states";import {- type DroppableTarget,- type InstanceSelector,findLocalStyleSourcesWithinInstances,- insertInstancesMutable,- reparentInstanceMutable,getAncestorInstanceSelector,+ insertInstancesMutable,insertPropsCopyMutable,+ insertStylesCopyMutable,insertStyleSourcesCopyMutable,insertStyleSourceSelectionsCopyMutable,- insertStylesCopyMutable,+ reparentInstanceMutable,+ type DroppableTarget,+ type InstanceSelector,} from "./tree-utils";-import { removeByMutable } from "./array-utils";-import { isBaseBreakpoint } from "./breakpoints";-export const insertNewComponentInstance = (- component: string,+export const insertTemplate = (+ template: WsEmbedTemplate,dropTarget: DroppableTarget) => {const breakpoints = breakpointsStore.get();const breakpointValues = Array.from(breakpoints.values());const baseBreakpoint = breakpointValues.find(isBaseBreakpoint);- const componentMeta = getComponentMeta(component);+if (baseBreakpoint === undefined) {return;}@@ -48,17 +49,7 @@ export const insertNewComponentInstance = (styleSourceSelections: insertedStyleSourceSelections,styleSources: insertedStyleSources,styles: insertedStyles,- } = generateDataFromEmbedTemplate(- // when template not specified fallback to template with the component- componentMeta?.template ?? [- {- type: "instance",- component,- children: [],- },- ],- baseBreakpoint.id- );+ } = generateDataFromEmbedTemplate(template, baseBreakpoint.id);const rootInstanceId = insertedInstances[0].id;store.createTransaction([@@ -98,6 +89,22 @@ export const insertNewComponentInstance = (selectedStyleSourceSelectorStore.set(undefined);};+export const insertNewComponentInstance = (+ component: string,+ dropTarget: DroppableTarget+) => {+ const componentMeta = getComponentMeta(component);+ // when template not specified fallback to template with the component+ const template = componentMeta?.template ?? [+ {+ type: "instance",+ component,+ children: [],+ },+ ];+ insertTemplate(template, dropTarget);+};+export const reparentInstance = (targetInstanceSelector: InstanceSelector,dropTarget: DroppableTarget