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
feat: support copy pasting resources (#2796)
Bogdan Chadkin3fb1368023c7
diff --git a/apps/builder/app/shared/instance-utils.test.ts b/apps/builder/app/shared/instance-utils.test.tsindex 6a64e53194..5f6cb0b302 100644--- a/apps/builder/app/shared/instance-utils.test.ts+++ b/apps/builder/app/shared/instance-utils.test.ts@@ -45,6 +45,7 @@ import {$styleSources,$styles,$registeredComponentMetas,+ $resources,} from "./nano-states";import { registerContainers } from "./sync";import type { Project } from "@webstudio-is/project";@@ -1316,6 +1317,220 @@ describe("get instances slice", () => {},]);});++ test("collect resources within instances", () => {+ // body+ // box1+ // box2+ $instances.set(+ toMap([+ createInstance("body", "Body", [+ { type: "id", value: "box1" },+ { type: "id", value: "box2" },+ ]),+ createInstance("box1", "Box", []),+ createInstance("box2", "Box", []),+ ])+ );+ $resources.set(+ toMap([+ {+ id: "resource1",+ name: "resource1",+ url: `""`,+ method: "get",+ headers: [],+ },+ {+ id: "resource2",+ name: "resource2",+ url: `""`,+ method: "get",+ headers: [],+ },+ {+ id: "resource3",+ name: "resource3",+ url: `""`,+ method: "get",+ headers: [],+ },+ ])+ );+ $dataSources.set(+ toMap([+ {+ id: "body$state",+ scopeInstanceId: "box1",+ name: "data1",+ type: "resource",+ resourceId: "resource1",+ },+ {+ id: "box1$state",+ scopeInstanceId: "box1",+ name: "data2",+ type: "resource",+ resourceId: "resource2",+ },+ {+ id: "box2$state",+ scopeInstanceId: "box2",+ type: "resource",+ name: "data3",+ resourceId: "resource3",+ },+ ])+ );+ const { resources, dataSources } = getInstancesSlice("box1");++ expect(resources).toEqual([+ {+ id: "resource1",+ name: "resource1",+ url: `""`,+ method: "get",+ headers: [],+ },+ {+ id: "resource2",+ name: "resource2",+ url: `""`,+ method: "get",+ headers: [],+ },+ ]);+ expect(dataSources).toEqual([+ {+ id: "body$state",+ scopeInstanceId: "box1",+ name: "data1",+ type: "resource",+ resourceId: "resource1",+ },+ {+ id: "box1$state",+ scopeInstanceId: "box1",+ name: "data2",+ type: "resource",+ resourceId: "resource2",+ },+ ]);+ });++ test("collect data sources used in resources", () => {+ // body+ // box+ $instances.set(+ toMap([+ createInstance("body", "Body", [{ type: "id", value: "box" }]),+ createInstance("box", "Box", []),+ ])+ );+ $resources.set(+ toMap([+ {+ id: "resourceId",+ name: "resourceName",+ url: `$ws$dataSource$bodyUrl`,+ method: "post",+ headers: [+ {+ name: "Authorization",+ value: `"Token " + $ws$dataSource$bodyToken`,+ },+ ],+ body: `$ws$dataSource$boxBody`,+ },+ ])+ );+ $dataSources.set(+ toMap([+ {+ id: "boxData",+ scopeInstanceId: "box",+ name: "data",+ type: "resource",+ resourceId: "resourceId",+ },+ {+ id: "boxBody",+ scopeInstanceId: "box",+ name: "token",+ type: "variable",+ value: { type: "string", value: "body" },+ },+ {+ id: "bodyUrl",+ scopeInstanceId: "body",+ name: "url",+ type: "variable",+ value: { type: "string", value: "url" },+ },+ {+ id: "bodyToken",+ scopeInstanceId: "body",+ name: "token",+ type: "variable",+ value: { type: "string", value: "token" },+ },+ {+ id: "bodyAnotherUrl",+ scopeInstanceId: "body",+ name: "anotherUrl",+ type: "variable",+ value: { type: "string", value: "anotherUrl" },+ },+ ])+ );+ const { resources, dataSources } = getInstancesSlice("box");++ expect(resources).toEqual([+ {+ id: "resourceId",+ name: "resourceName",+ url: `$ws$dataSource$bodyUrl`,+ method: "post",+ headers: [+ {+ name: "Authorization",+ value: `"Token " + $ws$dataSource$bodyToken`,+ },+ ],+ body: `$ws$dataSource$boxBody`,+ },+ ]);+ expect(dataSources).toEqual([+ {+ id: "boxData",+ scopeInstanceId: "box",+ name: "data",+ type: "resource",+ resourceId: "resourceId",+ },+ {+ id: "boxBody",+ scopeInstanceId: "box",+ name: "token",+ type: "variable",+ value: { type: "string", value: "body" },+ },+ {+ id: "bodyUrl",+ scopeInstanceId: "body",+ name: "url",+ type: "variable",+ value: { type: "string", value: "url" },+ },+ {+ id: "bodyToken",+ scopeInstanceId: "body",+ name: "token",+ type: "variable",+ value: { type: "string", value: "token" },+ },+ ]);+ });});describe("insert instances slice copy", () => {@@ -2184,4 +2399,286 @@ describe("insert instances slice copy", () => {},]);});++ test("insert resources with new ids", () => {+ const data = getWebstudioData();+ insertInstancesSliceCopy({+ data,+ slice: {+ ...emptySlice,+ resources: [+ {+ id: "resourceId",+ name: "",+ url: `$ws$dataSource$paramVariableId`,+ method: "post",+ headers: [+ { name: "auth", value: "$ws$dataSource$paramVariableId" },+ ],+ body: `$ws$dataSource$paramVariableId`,+ },+ ],+ dataSources: [+ {+ id: "paramVariableId",+ scopeInstanceId: "body",+ name: "myParam",+ type: "variable",+ value: { type: "string", value: "myParam" },+ },+ {+ id: "resourceVariableId",+ scopeInstanceId: "body",+ name: "myResource",+ type: "resource",+ resourceId: "resourceId",+ },+ ],+ },+ availableDataSources: new Set(),+ });+ const [newInstanceId] = data.instances.keys();+ const [newParamId, newResourceVariableId] = data.dataSources.keys();+ const [newResourceId] = data.resources.keys();+ expect(newResourceId).not.toEqual("resourceId");+ expect(Array.from(data.dataSources.values())).toEqual([+ {+ id: newParamId,+ scopeInstanceId: newInstanceId,+ name: "myParam",+ type: "variable",+ value: { type: "string", value: "myParam" },+ },+ {+ id: newResourceVariableId,+ scopeInstanceId: newInstanceId,+ name: "myResource",+ type: "resource",+ resourceId: newResourceId,+ },+ ]);+ expect(Array.from(data.resources.values())).toEqual([+ {+ id: newResourceId,+ name: "",+ url: encodeDataSourceVariable(newParamId),+ method: "post",+ headers: [+ { name: "auth", value: encodeDataSourceVariable(newParamId) },+ ],+ body: encodeDataSourceVariable(newParamId),+ },+ ]);+ });++ test("inline data sources into resources when not available in the scope", () => {+ const data = getWebstudioData();+ insertInstancesSliceCopy({+ data,+ slice: {+ ...emptySlice,+ resources: [+ {+ id: "resourceId",+ name: "",+ url: `$ws$dataSource$paramVariableId`,+ method: "post",+ headers: [+ { name: "auth", value: "$ws$dataSource$paramVariableId" },+ ],+ body: `$ws$dataSource$paramVariableId`,+ },+ ],+ dataSources: [+ {+ id: "paramVariableId",+ scopeInstanceId: "outside",+ name: "myParam",+ type: "variable",+ value: { type: "string", value: "myParam" },+ },+ {+ id: "variableId",+ scopeInstanceId: "body",+ name: "myResource",+ type: "resource",+ resourceId: "resourceId",+ },+ ],+ },+ availableDataSources: new Set(),+ });+ const [newResourceId] = data.resources.keys();+ expect(Array.from(data.resources.values())).toEqual([+ {+ id: newResourceId,+ name: "",+ url: `"myParam"`,+ method: "post",+ headers: [{ name: "auth", value: `"myParam"` }],+ body: `"myParam"`,+ },+ ]);+ });++ test("inline resource variables when not available in scope", () => {+ const data = getWebstudioData();+ insertInstancesSliceCopy({+ data,+ slice: {+ ...emptySlice,+ resources: [+ {+ id: "resourceId",+ name: "",+ url: `""`,+ method: "get",+ headers: [],+ },+ ],+ dataSources: [+ {+ id: "variableId",+ scopeInstanceId: "outside",+ name: "myResource",+ type: "resource",+ resourceId: "resourceId",+ },+ ],+ props: [+ {+ id: "expressionId",+ instanceId: "body",+ name: "myProp",+ type: "expression",+ value: "$ws$dataSource$variableId",+ },+ ],+ },+ availableDataSources: new Set(),+ });+ const [newInstanceId] = data.instances.keys();+ expect(Array.from(data.props.values())).toEqual([+ {+ id: expect.not.stringMatching("expressionId"),+ instanceId: newInstanceId,+ name: "myProp",+ type: "expression",+ value: "{}",+ },+ ]);+ });++ test("insert resources from portals with old ids", () => {+ const data = getWebstudioData();+ insertInstancesSliceCopy({+ data,+ slice: {+ ...emptySlice,+ // portal+ // fragment+ instances: [+ createInstance("portal", portalComponent, [+ { type: "id", value: "fragment" },+ ]),+ createInstance("fragment", "Fragment", []),+ ],+ resources: [+ {+ id: "resourceId",+ name: "",+ url: `""`,+ method: "get",+ headers: [],+ },+ ],+ dataSources: [+ {+ id: "variableId",+ scopeInstanceId: "fragment",+ name: "myVariable",+ type: "resource",+ resourceId: "resourceId",+ },+ ],+ },+ availableDataSources: new Set(),+ });+ expect(Array.from(data.dataSources.values())).toEqual([+ {+ id: "variableId",+ scopeInstanceId: "fragment",+ name: "myVariable",+ type: "resource",+ resourceId: "resourceId",+ },+ ]);+ expect(Array.from(data.resources.values())).toEqual([+ {+ id: "resourceId",+ name: "",+ url: `""`,+ method: "get",+ headers: [],+ },+ ]);+ });++ test("inline data sources into resources from portals when not available in scope", () => {+ const data = getWebstudioData();+ insertInstancesSliceCopy({+ data,+ slice: {+ ...emptySlice,+ // portal+ // fragment+ instances: [+ createInstance("portal", portalComponent, [+ { type: "id", value: "fragment" },+ ]),+ createInstance("fragment", "Fragment", []),+ ],+ resources: [+ {+ id: "resourceId",+ name: "",+ url: `$ws$dataSource$paramVariableId`,+ method: "post",+ headers: [+ { name: "auth", value: "$ws$dataSource$paramVariableId" },+ ],+ body: `$ws$dataSource$paramVariableId`,+ },+ ],+ dataSources: [+ {+ id: "paramVariableId",+ scopeInstanceId: "outside",+ name: "myParam",+ type: "variable",+ value: { type: "string", value: "myParam" },+ },+ {+ id: "variableId",+ scopeInstanceId: "fragment",+ name: "myResource",+ type: "resource",+ resourceId: "resourceId",+ },+ ],+ },+ availableDataSources: new Set(),+ });+ const [newResourceId] = data.resources.keys();+ expect(Array.from(data.resources.values())).toEqual([+ {+ id: newResourceId,+ name: "",+ url: `"myParam"`,+ method: "post",+ headers: [{ name: "auth", value: `"myParam"` }],+ body: `"myParam"`,+ },+ ]);+ });});diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.tsindex 6f439d9163..0845c1a225 100644--- a/apps/builder/app/shared/instance-utils.ts+++ b/apps/builder/app/shared/instance-utils.ts@@ -18,6 +18,7 @@ import {Breakpoint,type WebstudioFragment,type WebstudioData,+ type Resource,} from "@webstudio-is/sdk";import { findTreeInstanceIdsExcludingSlotDescendants } from "@webstudio-is/sdk";import {@@ -648,6 +649,7 @@ export const getInstancesSlice = (const assets = $assets.get();const instances = $instances.get();const dataSources = $dataSources.get();+ const resources = $resources.get();const props = $props.get();const styleSourceSelections = $styleSourceSelections.get();const styleSources = $styleSources.get();@@ -754,6 +756,7 @@ export const getInstancesSlice = (// or used by expressions or actions even outside of the tree// such variables can be bound to sliced root on pasteconst slicedDataSources: DataSources = new Map();+ const slicedResourceIds = new Set<Resource["id"]>();for (const dataSource of dataSources.values()) {if (// check if data source itself can be copied@@ -762,6 +765,36 @@ export const getInstancesSlice = (usedDataSourceIds.has(dataSource.id)) {slicedDataSources.set(dataSource.id, dataSource);+ if (dataSource.type === "resource") {+ slicedResourceIds.add(dataSource.resourceId);+ }+ }+ }++ // collect resources bound to all sliced data sources+ // and then collect data sources used in these resources+ // it creates some recursive behavior but since resources+ // cannot depend on other resources all left data sources+ // can be collected just once+ const slicedResources: Resource[] = [];+ const dataSourceIdsUsedInResources = new Set<DataSource["id"]>();+ for (const resourceId of slicedResourceIds) {+ const resource = resources.get(resourceId);+ if (resource === undefined) {+ continue;+ }+ slicedResources.push(resource);+ collectUsedDataSources(resource.url, dataSourceIdsUsedInResources);+ for (const { value } of resource.headers) {+ collectUsedDataSources(value, dataSourceIdsUsedInResources);+ }+ if (resource.body) {+ collectUsedDataSources(resource.body, dataSourceIdsUsedInResources);+ }+ }+ for (const dataSource of dataSources.values()) {+ if (dataSourceIdsUsedInResources.has(dataSource.id)) {+ slicedDataSources.set(dataSource.id, dataSource);}}@@ -783,7 +816,7 @@ export const getInstancesSlice = (breakpoints: Array.from(slicedBreapoints.values()),styles: slicedStyles,dataSources: Array.from(slicedDataSources.values()),- resources: [],+ resources: slicedResources,props: Array.from(slicedProps.values()),assets: slicedAssets,};@@ -841,7 +874,7 @@ const inlineUnavailableDataSources = ({if (dataSource?.type === "variable") {return JSON.stringify(dataSource.value.value);}- return "";+ return "{}";},});return { code: newCode, isDiscarded };@@ -900,6 +933,7 @@ export const insertInstancesSliceCopy = ({const {assets,instances,+ resources,dataSources,props,breakpoints,@@ -987,6 +1021,7 @@ export const insertInstancesSliceCopy = ({);const availablePortalDataSources = new Set(availableDataSources);+ const usedResourceIds = new Set<Resource["id"]>();for (const dataSource of slice.dataSources) {// insert only data sources within portal contentif (@@ -995,9 +1030,45 @@ export const insertInstancesSliceCopy = ({) {availablePortalDataSources.add(dataSource.id);dataSources.set(dataSource.id, dataSource);+ if (dataSource.type === "resource") {+ usedResourceIds.add(dataSource.resourceId);+ }}}+ for (const resource of slice.resources) {+ if (usedResourceIds.has(resource.id) === false) {+ continue;+ }+ const newUrl = inlineUnavailableDataSources({+ code: resource.url,+ availableDataSources: availablePortalDataSources,+ dataSources: sliceDataSources,+ }).code;+ const newHeaders = resource.headers.map((header) => ({+ name: header.name,+ value: inlineUnavailableDataSources({+ code: header.value,+ availableDataSources: availablePortalDataSources,+ dataSources: sliceDataSources,+ }).code,+ }));+ const newBody =+ resource.body === undefined+ ? undefined+ : inlineUnavailableDataSources({+ code: resource.body,+ availableDataSources: availablePortalDataSources,+ dataSources: sliceDataSources,+ }).code;+ resources.set(resource.id, {+ ...resource,+ url: newUrl,+ headers: newHeaders,+ body: newBody,+ });+ }+for (const instance of slice.instances) {if (instanceIds.has(instance.id)) {instances.set(instance.id, {@@ -1111,19 +1182,78 @@ export const insertInstancesSliceCopy = ({const availableFragmentDataSources = new Set(availableDataSources);const newDataSourceIds = new Map<DataSource["id"], DataSource["id"]>();+ const newResourceIds = new Map<Resource["id"], Resource["id"]>();+ const usedResourceIds = new Set<Resource["id"]>();for (const dataSource of slice.dataSources) {const { scopeInstanceId } = dataSource;// insert only data sources within portal contentif (scopeInstanceId && sliceInstanceIds.has(scopeInstanceId)) {availableFragmentDataSources.add(dataSource.id);- const newId = nanoid();- newDataSourceIds.set(dataSource.id, newId);- dataSources.set(newId, {- ...dataSource,- id: newId,- scopeInstanceId: newInstanceIds.get(scopeInstanceId),- });+ const newDataSourceId = nanoid();+ newDataSourceIds.set(dataSource.id, newDataSourceId);+ if (dataSource.type === "resource") {+ const newResourceId = nanoid();+ newResourceIds.set(dataSource.resourceId, newResourceId);+ usedResourceIds.add(dataSource.resourceId);+ dataSources.set(newDataSourceId, {+ ...dataSource,+ id: newDataSourceId,+ scopeInstanceId: newInstanceIds.get(scopeInstanceId),+ resourceId: newResourceId,+ });+ } else {+ dataSources.set(newDataSourceId, {+ ...dataSource,+ id: newDataSourceId,+ scopeInstanceId: newInstanceIds.get(scopeInstanceId),+ });+ }+ }+ }++ for (const resource of slice.resources) {+ if (usedResourceIds.has(resource.id) === false) {+ continue;}+ const newResourceId = newResourceIds.get(resource.id) ?? resource.id;++ const newUrl = replaceDataSources(+ inlineUnavailableDataSources({+ code: resource.url,+ availableDataSources: availableFragmentDataSources,+ dataSources: sliceDataSources,+ }).code,+ newDataSourceIds+ );+ const newHeaders = resource.headers.map((header) => ({+ name: header.name,+ value: replaceDataSources(+ inlineUnavailableDataSources({+ code: header.value,+ availableDataSources: availableFragmentDataSources,+ dataSources: sliceDataSources,+ }).code,+ newDataSourceIds+ ),+ }));+ const newBody =+ resource.body === undefined+ ? undefined+ : replaceDataSources(+ inlineUnavailableDataSources({+ code: resource.body,+ availableDataSources: availableFragmentDataSources,+ dataSources: sliceDataSources,+ }).code,+ newDataSourceIds+ );+ resources.set(newResourceId, {+ ...resource,+ id: newResourceId,+ url: newUrl,+ headers: newHeaders,+ body: newBody,+ });}for (const instance of slice.instances) {