Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Suspense, useCallback, useEffect, useRef, useState } from "react";
import React, { Suspense, useCallback, useEffect, useMemo, useRef, useState } from "react";
import { default as Button } from "antd/es/button";
import Dropdown from "antd/es/dropdown";
import type { ItemType } from "antd/es/menu/interface";
Expand All @@ -8,6 +8,7 @@ import Flex from "antd/es/flex";
import styled from "styled-components";
import { trans } from "i18n";
import { CustomModal } from "lowcoder-design";
import { CaptureResolution, RESOLUTION_CONSTRAINTS } from "./fileComp";

const CustomModalStyled = styled(CustomModal)`
top: 10vh;
Expand Down Expand Up @@ -53,23 +54,32 @@ const ReactWebcam = React.lazy(() => import("react-webcam"));

export const ImageCaptureModal = (props: {
showModal: boolean;
captureResolution?: CaptureResolution;
onModalClose: () => void;
onImageCapture: (image: string) => void;
}) => {
const [errMessage, setErrMessage] = useState("");
const [videoConstraints, setVideoConstraints] = useState<MediaTrackConstraints>({
facingMode: "environment",
});
const [selectedDeviceId, setSelectedDeviceId] = useState<string | null>(null);
const [modeList, setModeList] = useState<ItemType[]>([]);
const [dropdownShow, setDropdownShow] = useState(false);
const [imgSrc, setImgSrc] = useState<string>();
const webcamRef = useRef<any>(null);

const resolution = props.captureResolution ?? "auto";
const resolutionSize = RESOLUTION_CONSTRAINTS[resolution] ?? {};

const videoConstraints = useMemo<MediaTrackConstraints>(() => {
const base: MediaTrackConstraints = selectedDeviceId
? { deviceId: { exact: selectedDeviceId } }
: { facingMode: "environment" };
return { ...base, ...resolutionSize };
}, [selectedDeviceId, resolutionSize]);

useEffect(() => {
if (props.showModal) {
setImgSrc("");
setErrMessage("");
setVideoConstraints({ facingMode: "environment" });
setSelectedDeviceId(null);
setDropdownShow(false);
}
}, [props.showModal]);
Expand Down Expand Up @@ -125,6 +135,8 @@ export const ImageCaptureModal = (props: {
ref={webcamRef}
onUserMediaError={handleMediaErr}
screenshotFormat="image/jpeg"
screenshotQuality={1}
forceScreenshotSourceSize
videoConstraints={videoConstraints}
/>
</Suspense>
Expand Down Expand Up @@ -172,7 +184,7 @@ export const ImageCaptureModal = (props: {
<Menu
items={modeList}
onClick={(value) => {
setVideoConstraints({ deviceId: { exact: value.key } });
setSelectedDeviceId(value.key);
setDropdownShow(false);
}}
/>
Expand Down
54 changes: 23 additions & 31 deletions client/packages/lowcoder/src/comps/comps/fileComp/draggerUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { default as AntdUpload } from "antd/es/upload";
import { default as Button } from "antd/es/button";
import { UploadFile, UploadChangeParam, UploadFileStatus, RcFile } from "antd/es/upload/interface";
import { useState, useEffect } from "react";
import { useState, useMemo } from "react";
import styled, { css } from "styled-components";
import { trans } from "i18n";
import _ from "lodash";
Expand All @@ -11,8 +11,7 @@ import {
multiChangeAction,
} from "lowcoder-core";
import { hasIcon } from "comps/utils";
import { messageInstance } from "lowcoder-design/src/components/GlobalInstances";
import { resolveValue, resolveParsedValue, commonProps } from "./fileComp";
import { resolveValue, resolveParsedValue, commonProps, validateFile, CaptureResolution } from "./fileComp";
import { FileStyleType, AnimationStyleType, heightCalculator, widthCalculator } from "comps/controls/styleControlConstants";
import { ImageCaptureModal } from "./ImageCaptureModal";
import { v4 as uuidv4 } from "uuid";
Expand Down Expand Up @@ -149,9 +148,11 @@ interface DraggerUploadProps {
prefixIcon: any;
suffixIcon: any;
forceCapture: boolean;
captureResolution: CaptureResolution;
minSize: number;
maxSize: number;
maxFiles: number;
fileNamePattern: string;
uploadType: "single" | "multiple" | "directory";
text: string;
dragHintText?: string;
Expand All @@ -162,25 +163,27 @@ interface DraggerUploadProps {

export const DraggerUpload = (props: DraggerUploadProps) => {
const { dispatch, files, style, autoHeight, animationStyle } = props;
const [fileList, setFileList] = useState<UploadFile[]>(
files.map((f) => ({ ...f, status: "done" })) as UploadFile[]
);
// Track only files currently being uploaded (not yet in props.files)
const [uploadingFiles, setUploadingFiles] = useState<UploadFile[]>([]);
const [showModal, setShowModal] = useState(false);
const isMobile = checkIsMobile(window.innerWidth);

useEffect(() => {
if (files.length === 0 && fileList.length !== 0) {
setFileList([]);
}
}, [files]);
// Derive fileList from props.files (source of truth) + currently uploading files
const fileList = useMemo<UploadFile[]>(() => [
...(files.map((f) => ({ ...f, status: "done" as const })) as UploadFile[]),
...uploadingFiles,
], [files, uploadingFiles]);

const handleOnChange = (param: UploadChangeParam) => {
const uploadingFiles = param.fileList.filter((f) => f.status === "uploading");
if (uploadingFiles.length !== 0) {
setFileList(param.fileList);
const currentlyUploading = param.fileList.filter((f) => f.status === "uploading");
if (currentlyUploading.length !== 0) {
setUploadingFiles(currentlyUploading);
return;
}

// Clear uploading state when all uploads complete
setUploadingFiles([]);

let maxFiles = props.maxFiles;
if (props.uploadType === "single") {
maxFiles = 1;
Expand Down Expand Up @@ -240,8 +243,6 @@ export const DraggerUpload = (props: DraggerUploadProps) => {
props.onEvent("parse");
});
}

setFileList(uploadedFiles.slice(-maxFiles));
};

return (
Expand All @@ -254,21 +255,11 @@ export const DraggerUpload = (props: DraggerUploadProps) => {
$auto={autoHeight}
capture={props.forceCapture}
openFileDialogOnClick={!(props.forceCapture && !isMobile)}
beforeUpload={(file) => {
if (!file.size || file.size <= 0) {
messageInstance.error(`${file.name} ` + trans("file.fileEmptyErrorMsg"));
return AntdUpload.LIST_IGNORE;
}

if (
(!!props.minSize && file.size < props.minSize) ||
(!!props.maxSize && file.size > props.maxSize)
) {
messageInstance.error(`${file.name} ` + trans("file.fileSizeExceedErrorMsg"));
return AntdUpload.LIST_IGNORE;
}
return true;
}}
beforeUpload={(file) => validateFile(file, {
minSize: props.minSize,
maxSize: props.maxSize,
fileNamePattern: props.fileNamePattern,
})}
onChange={handleOnChange}
>
<p className="ant-upload-drag-icon">
Expand Down Expand Up @@ -301,6 +292,7 @@ export const DraggerUpload = (props: DraggerUploadProps) => {

<ImageCaptureModal
showModal={showModal}
captureResolution={props.captureResolution as CaptureResolution}
onModalClose={() => setShowModal(false)}
onImageCapture={async (image) => {
setShowModal(false);
Expand Down
Loading
Loading