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: Upload images on copy-paste (#3569)

Ivan Starkov8665ba202c9c
diff --git a/apps/builder/app/builder/shared/assets/asset-utils.ts b/apps/builder/app/builder/shared/assets/asset-utils.ts
index 403450bfad..939ad2c907 100644
--- a/apps/builder/app/builder/shared/assets/asset-utils.ts
+++ b/apps/builder/app/builder/shared/assets/asset-utils.ts
@@ -1,4 +1,6 @@
+import type { Asset, FontAsset, ImageAsset } from "@webstudio-is/sdk";
import { nanoid } from "nanoid";
+import type { UploadingFileData } from "~/shared/nano-states";
const extensionToMime = new Map([
[".gif", "image/gif"],
@@ -91,3 +93,50 @@ export const getFileName = (file: File | URL) => {
return extractImageNameAndMimeTypeFromUrl(file)[1];
};
+
+export const uploadingFileDataToAsset = (
+ fileData: UploadingFileData
+): Asset => {
+ const mimeType = getMimeType(
+ fileData.source === "file" ? fileData.file : new URL(fileData.url)
+ );
+ const format = mimeType.split("/")[1];
+
+ if (mimeType.startsWith("image/")) {
+ const asset: ImageAsset = {
+ id: fileData.assetId,
+ name: fileData.objectURL,
+ format,
+ type: "image",
+ description: "",
+ createdAt: "",
+ projectId: "",
+ size: 0,
+
+ meta: {
+ width: Number.NaN,
+ height: Number.NaN,
+ },
+ };
+
+ return asset;
+ }
+
+ const asset: FontAsset = {
+ id: fileData.assetId,
+ name: fileData.objectURL,
+ format: "woff2",
+ type: "font",
+ description: "",
+ createdAt: "",
+ projectId: "",
+ size: 0,
+ meta: {
+ family: "system",
+ style: "normal",
+ weight: 400,
+ },
+ };
+
+ return asset;
+};
diff --git a/apps/builder/app/builder/shared/assets/assets-shell.tsx b/apps/builder/app/builder/shared/assets/assets-shell.tsx
index b097fa2af0..2b12f63ca6 100644
--- a/apps/builder/app/builder/shared/assets/assets-shell.tsx
+++ b/apps/builder/app/builder/shared/assets/assets-shell.tsx
@@ -139,7 +139,8 @@ export const AssetsShell = ({
return false;
});
- uploadAssets(type, files, droppedUrls);
+ uploadAssets(type, files);
+ uploadAssets(type, droppedUrls);
},
})
);
diff --git a/apps/builder/app/builder/shared/assets/use-assets.tsx b/apps/builder/app/builder/shared/assets/use-assets.tsx
index db3adab91c..31804d9503 100644
--- a/apps/builder/app/builder/shared/assets/use-assets.tsx
+++ b/apps/builder/app/builder/shared/assets/use-assets.tsx
@@ -27,8 +27,10 @@ import {
getMimeType,
getSha256Hash,
getSha256HashOfFile,
+ uploadingFileDataToAsset,
} from "./asset-utils";
import { Image } from "../image-manager/image";
+import invariant from "tiny-invariant";
export const deleteAssets = (assetIds: Asset["id"][]) => {
serverSyncStore.createTransaction([$assets], (assets) => {
@@ -38,7 +40,27 @@ export const deleteAssets = (assetIds: Asset["id"][]) => {
});
};
-const setAsset = (asset: Asset) => {
+const safeDeleteAssets = (assetIds: Asset["id"][], projectId: string) => {
+ const currentProjectId = $project.get()?.id;
+
+ if (currentProjectId !== projectId) {
+ toast.error("Project has been changed, files will not be uploaded");
+ // Can cause data corruption
+ return;
+ }
+
+ deleteAssets(assetIds);
+};
+
+const safeSetAsset = (asset: Asset, projectId: string) => {
+ const currentProjectId = $project.get()?.id;
+
+ if (currentProjectId !== projectId) {
+ toast.error("Project has been changed, files will not be uploaded");
+ // Can cause data corrupiton
+ return;
+ }
+
serverSyncStore.createTransaction([$assets], (assets) => {
assets.set(asset.id, asset);
});
@@ -46,29 +68,29 @@ const setAsset = (asset: Asset) => {
const getFilesData = async (
type: AssetType,
- files: File[],
- urls: URL[]
+ filesOrUrls: File[] | URL[]
): Promise<UploadingFileData[]> => {
const filesData: UploadingFileData[] = [];
- for (const file of files) {
- const assetId = await getSha256HashOfFile(file);
- filesData.push({
- source: "file" as const,
- assetId: assetId,
- type,
- file,
- objectURL: URL.createObjectURL(file),
- });
- }
+ for (const fileOrUrl of filesOrUrls) {
+ if (fileOrUrl instanceof File) {
+ const assetId = await getSha256HashOfFile(fileOrUrl);
+ filesData.push({
+ source: "file" as const,
+ assetId: assetId,
+ type,
+ file: fileOrUrl,
+ objectURL: URL.createObjectURL(fileOrUrl),
+ });
+ continue;
+ }
- for (const url of urls) {
- const assetId = await getSha256Hash(url.href);
+ const assetId = await getSha256Hash(fileOrUrl.href);
filesData.push({
source: "url" as const,
assetId,
type,
- url: url.href,
- objectURL: url.href,
+ url: fileOrUrl.href,
+ objectURL: fileOrUrl.href,
});
}
@@ -93,27 +115,10 @@ const $assetContainers = computed(
const uploadingContainers: UploadingAssetContainer[] = [];
for (const uploadingFile of uploadingFilesData) {
- const name =
- uploadingFile.source === "file"
- ? uploadingFile.file.name
- : uploadingFile.url;
-
- const format = getMimeType(
- uploadingFile.source === "file"
- ? uploadingFile.file
- : new URL(uploadingFile.url)
- ).split("/")[1];
-
uploadingContainers.push({
status: "uploading",
objectURL: uploadingFile.objectURL,
- asset: {
- id: uploadingFile.assetId,
- type: uploadingFile.type,
- format,
- name: name,
- description: name,
- },
+ asset: uploadingFileDataToAsset(uploadingFile),
});
}
@@ -210,7 +215,11 @@ const uploadAsset = async ({
}
};
-const handleAfterSubmit = (assetId: string, data: UploadData) => {
+const handleAfterSubmit = (
+ assetId: string,
+ data: UploadData,
+ projectId: string
+) => {
warnOnce(
data.uploadedAssets?.length !== 1,
"Expected exactly 1 uploaded asset"
@@ -221,12 +230,12 @@ const handleAfterSubmit = (assetId: string, data: UploadData) => {
if (uploadedAsset === undefined) {
warnOnce(true, "An uploaded asset is undefined");
toast.error("Could not upload an asset");
- deleteAssets([assetId]);
+ safeDeleteAssets([assetId], projectId);
return;
}
// update store with new asset and set current id
- setAsset({ ...uploadedAsset, id: assetId });
+ safeSetAsset({ ...uploadedAsset, id: assetId }, projectId);
};
const ToastMessage = ({
@@ -269,65 +278,164 @@ const ToastMessage = ({
);
};
-export const uploadAssets = async (
- type: AssetType,
- files: File[],
- urls: URL[] = []
+const processingQueue: [
+ filesData: UploadingFileData[],
+ projectId: string,
+ authToken: string | undefined,
+][] = [];
+
+const processUpload = async (
+ filesData: UploadingFileData[],
+ projectId: string,
+ authToken: string | undefined
) => {
+ processingQueue.push([filesData, projectId, authToken]);
+
+ if (processingQueue.length > 1) {
+ return;
+ }
+
+ while (processingQueue.length > 0) {
+ const [filesData, projectId, authToken] = processingQueue.shift()!;
+
+ const currentProjectId = $project.get()?.id;
+ if (currentProjectId !== projectId) {
+ toast.error("Project has been changed, files will not be uploaded");
+ // Can cause data corrupiton
+ continue;
+ }
+
+ for (const fileData of filesData) {
+ const assetId = fileData.assetId;
+
+ if ($assets.get().has(assetId)) {
+ toast.warn(
+ <ToastMessage
+ title="Info"
+ assetId={assetId}
+ objectURL={fileData.objectURL}
+ >
+ Asset already exists
+ </ToastMessage>
+ );
+
+ deleteUploadingFileData(assetId);
+ continue;
+ }
+
+ await uploadAsset({
+ authToken,
+ projectId,
+ fileOrUrl:
+ fileData.source === "file" ? fileData.file : new URL(fileData.url),
+ onCompleted: (data) => {
+ URL.revokeObjectURL(fileData.objectURL);
+ deleteUploadingFileData(assetId);
+ handleAfterSubmit(assetId, data, projectId);
+ },
+ onError: (error) => {
+ deleteUploadingFileData(assetId);
+ toast.error(
+ <ToastMessage
+ title="Error"
+ assetId={assetId}
+ objectURL={fileData.objectURL}
+ >
+ {error}
+ </ToastMessage>
+ );
+
+ safeDeleteAssets([assetId], projectId);
+ },
+ });
+ }
+ }
+};
+
+export async function uploadAssets(
+ type: AssetType,
+ files: File[]
+): Promise<Map<File, string>>;
+
+export async function uploadAssets(
+ type: AssetType,
+ urls: URL[]
+): Promise<Map<URL, string>>;
+
+/**
+ * returns a list of asset ids that are being uploaded
+ */
+// eslint-disable-next-line func-style
+export async function uploadAssets(
+ type: AssetType,
+ filesOrUrls: File[] | URL[]
+): Promise<Map<URL | File, string>> {
const projectId = $project.get()?.id;
const authToken = $authToken.get();
if (projectId === undefined) {
- return;
+ return new Map();
}
- const filesData = await getFilesData(type, files, urls);
+ const filesData = await getFilesData(type, filesOrUrls);
+
+ // Filter out duplicates inside filesData
+ const uniqFilesDataMap = new Map(
+ filesData.map((fileData) => [fileData.assetId, fileData])
+ );
- addUploadingFilesData(filesData);
+ // Filter out duplicates existing in assets or uploading files
+ const existingIds = [
+ ...$assets.get().keys(),
+ ...$uploadingFilesDataStore.get().map((fileData) => fileData.assetId),
+ ];
- for (const fileData of filesData) {
- const assetId = fileData.assetId;
+ for (const existingAssetId of existingIds) {
+ if (uniqFilesDataMap.has(existingAssetId)) {
+ const fileData = uniqFilesDataMap.get(existingAssetId)!;
+ uniqFilesDataMap.delete(existingAssetId);
- if ($assets.get().has(assetId)) {
toast.warn(
<ToastMessage
- title="Info"
- assetId={assetId}
+ title="Warning"
+ assetId={fileData.assetId}
objectURL={fileData.objectURL}
>
- Asset is already exists
+ Asset already exists
</ToastMessage>
);
-
- deleteUploadingFileData(assetId);
- continue;
}
+ }
- await uploadAsset({
- authToken,
- projectId,
- fileOrUrl:
- fileData.source === "file" ? fileData.file : new URL(fileData.url),
- onCompleted: (data) => {
- URL.revokeObjectURL(fileData.objectURL);
- deleteUploadingFileData(assetId);
- handleAfterSubmit(assetId, data);
- },
- onError: (error) => {
- deleteAssets([assetId]);
- deleteUploadingFileData(assetId);
- toast.error(
- <ToastMessage
- title="Error"
- assetId={assetId}
- objectURL={fileData.objectURL}
- >
- {error}
- </ToastMessage>
- );
- },
- });
+ const uniqFilesData = [...uniqFilesDataMap.values()];
+
+ addUploadingFilesData(uniqFilesData);
+
+ processUpload(uniqFilesData, projectId, authToken);
+
+ const res = new Map();
+
+ for (let i = 0; i < filesData.length; ++i) {
+ const fileOrUrl = filesOrUrls[i];
+ const fileData = filesData[i];
+
+ invariant(
+ fileOrUrl instanceof URL ||
+ (fileOrUrl instanceof File &&
+ fileData.source === "file" &&
+ fileData.file === fileOrUrl)
+ );
+ invariant(
+ fileOrUrl instanceof File ||
+ (fileOrUrl instanceof URL &&
+ fileData.source === "url" &&
+ fileData.url === fileOrUrl.href)
+ );
+
+ res.set(filesOrUrls[i], filesData[i].assetId);
}
-};
+
+ return res;
+}
const filterByType = (assetContainers: AssetContainer[], type: AssetType) => {
return assetContainers.filter((assetContainer) => {
diff --git a/apps/builder/app/shared/builder-api.ts b/apps/builder/app/shared/builder-api.ts
index 1246af1abc..334e5a4999 100644
--- a/apps/builder/app/shared/builder-api.ts
+++ b/apps/builder/app/shared/builder-api.ts
@@ -1,12 +1,21 @@
import { createRecursiveProxy } from "@trpc/server/shared";
import invariant from "tiny-invariant";
import { toast } from "@webstudio-is/design-system";
+import { uploadAssets } from "~/builder/shared/assets/use-assets";
const apiWindowNamespace = "__webstudio__$__builderApi";
const _builderApi = {
isInitialized: () => true,
toast,
+ uploadImages: async (srcs: string[]) => {
+ const urlToIds = await uploadAssets(
+ "image",
+ srcs.map((src) => new URL(src))
+ );
+
+ return new Map([...urlToIds.entries()].map(([url, id]) => [url.href, id]));
+ },
};
declare global {
diff --git a/apps/builder/app/shared/copy-paste/asset-upload.test.tsx b/apps/builder/app/shared/copy-paste/asset-upload.test.tsx
new file mode 100644
index 0000000000..4ca4e3a4f2
--- /dev/null
+++ b/apps/builder/app/shared/copy-paste/asset-upload.test.tsx
@@ -0,0 +1,68 @@
+import { expect, test } from "@jest/globals";
+import { $, renderJsx, AssetValue } from "@webstudio-is/sdk/testing";
+import { denormalizeSrcProps } from "./asset-upload";
+import type { WebstudioFragment } from "@webstudio-is/sdk";
+
+test("extractSrcProps works well", async () => {
+ const inputData = renderJsx(
+ <$.Body ws:id="boxA">
+ <$.Image ws:id="imageA" src="https://src-a/"></$.Image>
+ <$.Box ws:id="boxB">
+ <$.Image ws:id="imageB" src="https://src-b/"></$.Image>
+ </$.Box>
+ </$.Body>
+ );
+
+ const data: WebstudioFragment = {
+ assets: [],
+ breakpoints: [],
+ children: [],
+ dataSources: [],
+ instances: [...inputData.instances.values()],
+ props: [...inputData.props.values()],
+ resources: [],
+ styles: [],
+ styleSources: [],
+ styleSourceSelections: [],
+ };
+
+ const src2AssetId = (src: string) => `${src}asset-id`;
+
+ const uploadImages = async (srcs: string[]) => {
+ return new Map(srcs.map((src) => [src, `${src2AssetId(src)}`]));
+ };
+
+ const denormalizedData = await denormalizeSrcProps(
+ data,
+ uploadImages,
+ (instanceId, propName) => `${instanceId}:${propName}`
+ );
+
+ const assetA = new AssetValue(src2AssetId("https://src-a/"));
+ const assetB = new AssetValue(src2AssetId("https://src-b/"));
+
+ const desiredOutcome = renderJsx(
+ <$.Body ws:id="boxA">
+ <$.Image
+ ws:id="imageA"
+ src={assetA}
+ width={assetA}
+ height={assetA}
+ ></$.Image>
+ <$.Box ws:id="boxB">
+ <$.Image
+ ws:id="imageB"
+ src={assetB}
+ width={assetB}
+ height={assetB}
+ ></$.Image>
+ </$.Box>
+ </$.Body>
+ );
+
+ expect(denormalizedData.instances).toEqual([
+ ...desiredOutcome.instances.values(),
+ ]);
+
+ expect(denormalizedData.props).toEqual([...desiredOutcome.props.values()]);
+});
diff --git a/apps/builder/app/shared/copy-paste/asset-upload.ts b/apps/builder/app/shared/copy-paste/asset-upload.ts
new file mode 100644
index 0000000000..9eaf4d4b6b
--- /dev/null
+++ b/apps/builder/app/shared/copy-paste/asset-upload.ts
@@ -0,0 +1,96 @@
+import type { WebstudioFragment } from "@webstudio-is/sdk";
+import { builderApi } from "../builder-api";
+import { nanoid } from "nanoid";
+
+type StringProp = Extract<
+ WebstudioFragment["props"][number],
+ { type: "string" }
+>;
+
+const extractSrcProps = (
+ data: WebstudioFragment
+): [propId: string, href: string][] => {
+ const imageComponentsSet = new Set(
+ data.instances
+ .filter(
+ (instance) =>
+ instance.type === "instance" && instance.component === "Image"
+ )
+ .map((instance) => instance.id)
+ );
+
+ const srcProps = data.props
+ .filter(
+ (prop): prop is StringProp =>
+ prop.type === "string" &&
+ prop.name === "src" &&
+ imageComponentsSet.has(prop.instanceId)
+ )
+ .map(
+ (prop) =>
+ [prop.id, new URL(prop.value).href] as [propId: string, href: string]
+ );
+
+ return srcProps;
+};
+
+/**
+ *
+ * Similar to normalizeProps, where asset properties are replaced with values,
+ * here we replace the image src string property with the asset.
+ */
+export const denormalizeSrcProps = async (
+ data: WebstudioFragment,
+ uploadImages = builderApi.uploadImages,
+ generateId: (instanceId: string, propName: string) => string = () => nanoid()
+): Promise<WebstudioFragment> => {
+ const srcProps = extractSrcProps(data);
+
+ const assetUrlToIds = await uploadImages(srcProps.map(([, value]) => value));
+
+ const srcPropIdToAssetId = new Map(
+ srcProps.map(([id, url]) => [id, assetUrlToIds.get(url)] as const)
+ );
+
+ const result = { ...data };
+
+ result.props = result.props
+ .map((prop) => {
+ if (prop.type === "string" && prop.name === "src") {
+ const assetId = srcPropIdToAssetId.get(prop.id);
+
+ if (assetId === undefined) {
+ return prop;
+ }
+
+ // @todo: add width and height props
+ const assetWithSizeProps: WebstudioFragment["props"] = [
+ {
+ ...prop,
+ type: "asset",
+ value: assetId,
+ },
+ {
+ id: generateId(prop.instanceId, "width"),
+ name: "width",
+ instanceId: prop.instanceId,
+ type: "asset",
+ value: assetId,
+ },
+ {
+ id: generateId(prop.instanceId, "height"),
+ name: "height",
+ instanceId: prop.instanceId,
+ type: "asset",
+ value: assetId,
+ },
+ ];
+ return assetWithSizeProps;
+ }
+
+ return prop;
+ })
+ .flat();
+
+ return result;
+};
diff --git a/apps/builder/app/shared/copy-paste/plugin-markdown.ts b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
index 99d756383c..c2993b1939 100644
--- a/apps/builder/app/shared/copy-paste/plugin-markdown.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-markdown.ts
@@ -20,6 +20,7 @@ import {
$selectedPage,
} from "../nano-states";
import { isBaseBreakpoint } from "../breakpoints";
+import { denormalizeSrcProps } from "./asset-upload";
const micromarkOptions = {
extensions: [gfm()],
@@ -220,11 +221,15 @@ const parse = (clipboardData: string, options?: Options) => {
};
export const onPaste = async (clipboardData: string) => {
- const data = parse(clipboardData);
+ let data = parse(clipboardData);
+
const selectedPage = $selectedPage.get();
if (data === undefined || selectedPage === undefined) {
return false;
}
+
+ data = await denormalizeSrcProps(data);
+
const metas = $registeredComponentMetas.get();
const newInstances = new Map(
data.instances.map((instance) => [instance.id, instance])
diff --git a/apps/builder/app/shared/nano-states/props.ts b/apps/builder/app/shared/nano-states/props.ts
index d6ee16b5c9..183fae9244 100644
--- a/apps/builder/app/shared/nano-states/props.ts
+++ b/apps/builder/app/shared/nano-states/props.ts
@@ -39,7 +39,7 @@ import {
$selectedPageDefaultSystem,
mergeSystem,
} from "./variables";
-import { getMimeType } from "~/builder/shared/assets/asset-utils";
+import { uploadingFileDataToAsset } from "~/builder/shared/assets/asset-utils";
export const getIndexedInstanceId = (
instanceId: Instance["id"],
@@ -262,30 +262,9 @@ export const $propValuesByInstanceSelector = computed(
// ignore asset and page props when params is not provided
if (params && pages) {
const uploadingImageAssets = uploadingFilesDataStore
- .map((fileData) => {
- const format = getMimeType(
- fileData.source === "file" ? fileData.file : new URL(fileData.url)
- ).split("/")[1];
-
- const uploadingAsset: ImageAsset = {
- id: fileData.assetId,
- name: fileData.objectURL,
- format,
- type: "image",
- description: "",
- createdAt: "",
- projectId: "",
- size: 0,
-
- meta: {
- width: Number.NaN,
- height: Number.NaN,
- },
- };
-
- return uploadingAsset;
- })
- .filter(<T>(value: T): value is NonNullable<T> => value !== undefined);
+ .map(uploadingFileDataToAsset)
+ .filter(<T>(value: T): value is NonNullable<T> => value !== undefined)
+ .filter((asset): asset is ImageAsset => asset.type === "image");
// use whole props list to let access hash props from other pages and instances
propsList = normalizeProps({
diff --git a/apps/builder/jest.config.js b/apps/builder/jest.config.js
index 4f29fda3ad..3ae762a3af 100644
--- a/apps/builder/jest.config.js
+++ b/apps/builder/jest.config.js
@@ -6,7 +6,7 @@ import baseConfig from "@webstudio-is/jest-config";
*/
export default {
...baseConfig,
- testMatch: ["<rootDir>/app/**/*.test.ts"],
+ testMatch: ["<rootDir>/app/**/*.test.(ts|tsx)"],
moduleNameMapper: {
"^~/(.*)$": "<rootDir>/app/$1",
},