From e61b8980b27bc6abd5490bf20313e116ff0b0622 Mon Sep 17 00:00:00 2001 From: rizky Date: Sat, 10 Aug 2024 00:20:28 -0700 Subject: [PATCH] fix file preview --- comps/form/field/type/FilePreview.tsx | 162 +++++++++++++++------- comps/form/field/type/TypeUploadMulti.tsx | 84 ++++++----- comps/form/utils/init.tsx | 2 + 3 files changed, 157 insertions(+), 91 deletions(-) diff --git a/comps/form/field/type/FilePreview.tsx b/comps/form/field/type/FilePreview.tsx index 8d88d4b..aaf333e 100755 --- a/comps/form/field/type/FilePreview.tsx +++ b/comps/form/field/type/FilePreview.tsx @@ -1,12 +1,102 @@ import { ExternalLink } from "lucide-react"; +import { ReactElement } from "react"; -export const FilePreview = ({ +export const ThumbPreview = ({ url, - variant, + del, }: { url: string; - variant?: "thumb"; + del: ReactElement; }) => { + const file = getFileName(url); + if (typeof file === "string") return; + + const color = darkenColor(generateRandomColor(file.extension)); + let content = ( +
+ {file.extension} + +
+ +
+
+ ); + + let is_image = false; + if (url.startsWith("_file/")) { + if ([".png", ".jpeg", ".jpg", ".webp"].find((e) => url.endsWith(e))) { + is_image = true; + content = ( + + ); + } + } + + return ( + <> + {file.extension && ( +
{ + let _url = siteurl(url || ""); + window.open(_url, "_blank"); + }} + > + {content} + {del} +
+ )} + + ); +}; + +export const FilePreview = ({ url }: { url: string }) => { const file = getFileName(url); if (typeof file === "string") return ( @@ -49,62 +139,40 @@ export const FilePreview = ({ ); - if (variant === "thumb") { - content = ( -
- {file.extension} - -
- -
-
- ); - } - if (url.startsWith("_file/")) { if ([".png", ".jpeg", ".jpg", ".webp"].find((e) => url.endsWith(e))) { content = ( ); } } + return ( <> {file.extension && (
{content} - {variant !== "thumb" && ( -
- -
- )} +
+ +
)} diff --git a/comps/form/field/type/TypeUploadMulti.tsx b/comps/form/field/type/TypeUploadMulti.tsx index d70df46..132fc13 100755 --- a/comps/form/field/type/TypeUploadMulti.tsx +++ b/comps/form/field/type/TypeUploadMulti.tsx @@ -4,7 +4,7 @@ import { Trash2, Upload } from "lucide-react"; import { ChangeEvent, FC } from "react"; import { FMLocal, FieldLocal, FieldProp } from "../../typings"; import { PropTypeInput } from "./TypeInput"; -import { FilePreview } from "./FilePreview"; +import { FilePreview, ThumbPreview } from "./FilePreview"; import { Spinner } from "lib/comps/ui/field-loading"; const w = window as unknown as { serverurl: string; @@ -123,50 +123,46 @@ export const FieldUploadMulti: FC<{ e.preventDefault(); }} > -
- -
{ - e.preventDefault(); - e.stopPropagation(); - if (confirm("Remove this file ?")) { - list.splice(idx, 1); - fm.data[field.name] = JSON.stringify(list); - fm.render(); - } - }} - className={cx( - "c-flex c-flex-row c-items-center c-px-1 c-rounded c-bg-white c-cursor-pointer hover:c-bg-red-100 c-absolute c-top-0 c-right-0 del transition-all", - css` - border: 1px solid red; - width: 25px; - height: 25px; - margin: 5px; - ` - )} - > - -
+
+ { + e.preventDefault(); + e.stopPropagation(); + if (confirm("Remove this file ?")) { + list.splice(idx, 1); + fm.data[field.name] = JSON.stringify(list); + fm.render(); + } + }} + className={cx( + "c-flex c-flex-row c-items-center c-px-1 c-rounded c-bg-white c-cursor-pointer hover:c-bg-red-100 transition-all", + css` + border: 1px solid red; + width: 25px; + height: 25px; + ` + )} + > + +
+ } + />
); })} {input.uploading.size > 0 && ( -
+
Uploading
)} @@ -175,8 +171,10 @@ export const FieldUploadMulti: FC<{
)} -
+
- Upload File + Upload Multiple Files
diff --git a/comps/form/utils/init.tsx b/comps/form/utils/init.tsx index c749f79..a6fff78 100755 --- a/comps/form/utils/init.tsx +++ b/comps/form/utils/init.tsx @@ -81,6 +81,8 @@ export const formInit = (fm: FMLocal, props: FMProps) => { className={cx( css` color: green; + border: 1px solid green !important; + background: #f1fff6 !important; `, "c-cursor-pointer" )}