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.

more work on image loading

Sara Vieirac7178aed7eef
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager.tsx
index 6dc23b7eda..8a26d00c6b 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager.tsx
@@ -1,60 +1,27 @@
import { ImageIcon } from "~/shared/icons";
-import { Button, Flex, Grid, Heading } from "~/shared/design-system";
-import { ChangeEvent, useEffect, useRef, useState } from "react";
-import { Form, useActionData, useSubmit } from "@remix-run/react";
-import type { Asset } from "@webstudio-is/prisma-client";
+import { Flex, Grid, Heading } from "~/shared/design-system";
+import { useEffect, useState } from "react";
+import { useActionData } from "@remix-run/react";
+import { Asset } from "@webstudio-is/prisma-client";
import { AssetManagerImage } from "./components/image";
-import ObjectID from "bson-objectid";
-const readImages = async (fileList: FileList) => {
- const images = [];
- for (const file of fileList) {
- const path = await new Promise((resolve) => {
- const reader = new FileReader();
- reader.addEventListener("load", (event) => {
- const dataUri = event?.target?.result;
+import { AddAnAssetForm } from "./components/add-an-asset-form";
- resolve(dataUri);
- });
- reader.readAsDataURL(file);
- });
-
- images.push({
- path: path as string,
- name: file.name,
- id: ObjectID.toString(),
- });
- }
-
- return images;
-};
-
-type ParsedFiles = {
- path: string;
- name: string;
- id: string;
-}[];
-
-export const TabContent = ({ assets }: { assets: Array<Asset> }) => {
+export const TabContent = ({
+ assets: baseAssets,
+}: {
+ assets: Array<Asset & { uploading?: boolean }>;
+}) => {
const newImages = useActionData();
- const inputRef = useRef<HTMLInputElement | null>(null);
- const submit = useSubmit();
- const [uploadedImages, setUploadedImages] = useState<ParsedFiles>([]);
-
- const onFormChange = async (event: ChangeEvent<HTMLFormElement>) => {
- const newFiles = inputRef?.current?.files;
- if (newFiles) {
- submit(event.currentTarget);
- const parsedFiles = await readImages(newFiles);
- setUploadedImages(parsedFiles);
- event.currentTarget.reset();
- }
- };
+ const [assets, setAsssets] = useState(baseAssets);
useEffect(() => {
if (newImages?.assets.length) {
- setUploadedImages([]);
+ setAsssets((currentAssets) => [
+ ...newImages.assets,
+ ...currentAssets.filter((a) => !a.uploading),
+ ]);
}
}, [newImages]);
@@ -62,38 +29,19 @@ export const TabContent = ({ assets }: { assets: Array<Asset> }) => {
<Flex gap="3" direction="column" css={{ padding: "$1" }}>
<Flex justify="between">
<Heading>Assets</Heading>
- <Form
- method="post"
- encType="multipart/form-data"
- onChange={onFormChange}
- >
- <input
- accept="image/*"
- type="file"
- name="image"
- multiple
- ref={inputRef}
- style={{ display: "none" }}
- />
- <Button type="button" onClick={() => inputRef?.current?.click()}>
- Upload Image
- </Button>
- </Form>
+ <AddAnAssetForm
+ onSubmit={(uploadedAssets: Array<Asset>) =>
+ setAsssets((assets) => [...uploadedAssets, ...assets])
+ }
+ />
</Flex>
<Grid columns={2} gap={2}>
- {uploadedImages.map((asset) => (
- <AssetManagerImage
- key={asset.id}
- path={asset.path}
- uploading={true}
- alt={asset.name}
- />
- ))}
{assets.map((asset) => (
<AssetManagerImage
key={asset.id}
path={asset.path}
alt={asset.alt || asset.name}
+ uploading={asset.uploading}
/>
))}
</Grid>
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/components/add-an-asset-form.tsx b/apps/designer/app/designer/features/sidebar-left/panels/components/add-an-asset-form.tsx
new file mode 100644
index 0000000000..1ad682cb46
--- /dev/null
+++ b/apps/designer/app/designer/features/sidebar-left/panels/components/add-an-asset-form.tsx
@@ -0,0 +1,73 @@
+import { Form, useParams, useSubmit } from "@remix-run/react";
+import { Asset, Decimal, Location } from "@webstudio-is/prisma-client";
+import ObjectID from "bson-objectid";
+import { ChangeEvent, useRef } from "react";
+import { Button } from "~/shared/design-system";
+
+const readImages = async (fileList: FileList, projectId: string) => {
+ const images = [];
+ for (const file of fileList) {
+ const path = await new Promise((resolve) => {
+ const reader = new FileReader();
+ reader.addEventListener("load", (event) => {
+ const dataUri = event?.target?.result;
+
+ resolve(dataUri);
+ });
+ reader.readAsDataURL(file);
+ });
+
+ images.push({
+ path: path as string,
+ name: file.name,
+ id: ObjectID().toString(),
+ width: 0 as unknown as Decimal,
+ height: 0 as unknown as Decimal,
+ projectId,
+ size: file.size,
+ format: file.type,
+ createdAt: new Date(),
+ location: "FS" as Location,
+ alt: "",
+ uploading: true,
+ });
+ }
+
+ return images;
+};
+
+export const AddAnAssetForm = ({
+ onSubmit,
+}: {
+ onSubmit: (uploadedAssets: Array<Asset>) => void;
+}) => {
+ const submit = useSubmit();
+ const inputRef = useRef<HTMLInputElement | null>(null);
+ const { id } = useParams();
+
+ const onFormChange = async (event: ChangeEvent<HTMLFormElement>) => {
+ const newFiles = inputRef?.current?.files;
+ if (newFiles && id) {
+ submit(event.currentTarget);
+ const parsedFiles = await readImages(newFiles, id);
+ onSubmit(parsedFiles);
+ event.currentTarget.reset();
+ }
+ };
+
+ return (
+ <Form method="post" encType="multipart/form-data" onChange={onFormChange}>
+ <input
+ accept="image/*"
+ type="file"
+ name="image"
+ multiple
+ ref={inputRef}
+ style={{ display: "none" }}
+ />
+ <Button type="button" onClick={() => inputRef?.current?.click()}>
+ Upload Image
+ </Button>
+ </Form>
+ );
+};
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/components/image.tsx b/apps/designer/app/designer/features/sidebar-left/panels/components/image.tsx
index 5b2d1e2418..475b310def 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/components/image.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/components/image.tsx
@@ -1,10 +1,10 @@
import { useEffect, useState } from "react";
import { Box, ProgressBar } from "~/shared/design-system";
-import placholderImage from "~/shared/images/image-placeholder.svg";
+import placeholderImage from "~/shared/images/image-placeholder.svg";
import brokenImage from "~/shared/images/broken-image-placeholder.svg";
const useImageWithFallback = ({ path }: { path: string }) => {
- const [src, setSrc] = useState(placholderImage);
+ const [src, setSrc] = useState(placeholderImage);
useEffect(() => {
const newImage = new Image();
@@ -29,12 +29,14 @@ export const AssetManagerImage = ({
const [progressBarPercentage, setProgressBarPercentage] = useState(0);
useEffect(() => {
- setInterval(() => {
- if (progressBarPercentage < 60) {
- setProgressBarPercentage((percentage) => percentage + 1);
- }
- }, 100);
- }, []);
+ if (uploading) {
+ setInterval(() => {
+ setProgressBarPercentage((percentage) =>
+ percentage < 60 ? percentage + 1 : percentage
+ );
+ }, 100);
+ }
+ }, [progressBarPercentage, uploading]);
return (
<Box
diff --git a/packages/prisma-client/src/prisma.ts b/packages/prisma-client/src/prisma.ts
index e56bcdb8cf..bf58739c56 100644
--- a/packages/prisma-client/src/prisma.ts
+++ b/packages/prisma-client/src/prisma.ts
@@ -1,4 +1,5 @@
import { PrismaClient, Prisma } from "@prisma/client";
+import { Decimal } from "@prisma/client/runtime";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
declare global {
@@ -14,4 +15,4 @@ export const prisma = global.prisma || new PrismaClient();
if (process.env.NODE_ENV !== "production") global.prisma = prisma;
-export { Prisma, PrismaClientKnownRequestError };
+export { Prisma, PrismaClientKnownRequestError, Decimal };