From d66dd93b85ce9bd346bc07a2099c28b5cce5ec59 Mon Sep 17 00:00:00 2001 From: Akmal Isomadinov Date: Wed, 17 Jul 2024 12:55:40 +0500 Subject: [PATCH] Doceditor:Components: Added download as --- packages/doceditor/src/components/Editor.tsx | 2 + packages/doceditor/src/components/Root.tsx | 2 + .../src/hooks/useStartFillingSelectDialog.ts | 81 +++++++++++++++---- packages/doceditor/src/types/index.ts | 1 + 4 files changed, 69 insertions(+), 17 deletions(-) diff --git a/packages/doceditor/src/components/Editor.tsx b/packages/doceditor/src/components/Editor.tsx index a0365598d1..7117ada08c 100644 --- a/packages/doceditor/src/components/Editor.tsx +++ b/packages/doceditor/src/components/Editor.tsx @@ -72,6 +72,7 @@ const Editor = ({ errorMessage, isSkipError, + onDownloadAs, onSDKRequestSharingSettings, onSDKRequestSaveAs, onSDKRequestInsertImage, @@ -235,6 +236,7 @@ const Editor = ({ onMetaChange, onMakeActionLink, onOutdatedVersion, + onDownloadAs, }; if (successAuth) { diff --git a/packages/doceditor/src/components/Root.tsx b/packages/doceditor/src/components/Root.tsx index 23f5eb16f1..7928e29e0e 100644 --- a/packages/doceditor/src/components/Root.tsx +++ b/packages/doceditor/src/components/Root.tsx @@ -135,6 +135,7 @@ const Root = ({ onSDKRequestStartFilling, conflictDataDialog, headerLabelSFSDialog, + onDownloadAs, } = useStartFillingSelectDialog(fileInfo); const { @@ -211,6 +212,7 @@ const Root = ({ fileInfo={fileInfo} errorMessage={error?.message} isSkipError={!!isSkipError} + onDownloadAs={onDownloadAs} onSDKRequestSharingSettings={onSDKRequestSharingSettings} onSDKRequestSaveAs={onSDKRequestSaveAs} onSDKRequestInsertImage={onSDKRequestInsertImage} diff --git a/packages/doceditor/src/hooks/useStartFillingSelectDialog.ts b/packages/doceditor/src/hooks/useStartFillingSelectDialog.ts index a72ea9dc2d..9dbccfad16 100644 --- a/packages/doceditor/src/hooks/useStartFillingSelectDialog.ts +++ b/packages/doceditor/src/hooks/useStartFillingSelectDialog.ts @@ -34,6 +34,7 @@ import { } from "@docspace/shared/api/files"; // import { getOperationProgress } from "@docspace/shared/utils/getOperationProgress"; import { toastr } from "@docspace/shared/components/toast"; +import { EDITOR_ID } from "@docspace/shared/constants"; import type { TFile, @@ -48,7 +49,7 @@ import type { TSelectedFileInfo } from "@docspace/shared/selectors/Files/FilesSe import type { TData } from "@docspace/shared/components/toast/Toast.type"; // import { useTranslation } from "react-i18next"; - +import { saveAs } from "@/utils"; import type { ConflictStateType } from "@/types"; const DefaultConflictDataDialogState: ConflictStateType = { @@ -59,8 +60,20 @@ const DefaultConflictDataDialogState: ConflictStateType = { folderName: "", }; +const hasFileUrl = (arg: object): arg is { data: { url: string } } => { + return ( + "data" in arg && + typeof arg.data === "object" && + arg.data !== null && + "url" in arg.data && + typeof arg.data.url === "string" + ); +}; + const useStartFillingSelectDialog = (fileInfo: TFile | undefined) => { // const { t } = useTranslation(["Common"]); + const resolveRef = useRef<(value: string | PromiseLike) => void>(); + const [headerLabelSFSDialog, setHeaderLabelSFSDialog] = useState(""); const [isVisible, setIsVisible] = useState(false); @@ -105,6 +118,26 @@ const useStartFillingSelectDialog = (fileInfo: TFile | undefined) => { } }; + const onDownloadAs = (obj: object) => { + if (hasFileUrl(obj)) { + resolveRef.current?.(obj.data.url); + resolveRef.current = undefined; + } + }; + + const getFileUrl = async () => { + const docEditor = + typeof window !== "undefined" && window.DocEditor?.instances[EDITOR_ID]; + + docEditor?.downloadAs("pdf"); + + const url = await new Promise((resolve) => { + resolveRef.current = resolve; + }); + + return url; + }; + const onSubmit = async ( selectedItemId: string | number | undefined, folderTitle: string, @@ -144,27 +177,40 @@ const useStartFillingSelectDialog = (fileInfo: TFile | undefined) => { } } - await copyToFolder( - Number(selectedItemId), - [], - [fileInfo.id], - conflictResolve, + const fileUrl = await getFileUrl(); + + const response = await saveAs( + fileInfo.title, + fileUrl, + selectedItemId, false, ); - const error = await new Promise((resolve) => { - const interval = setInterval(async () => { - const [progress] = await getProgress(); + const [key, value] = response?.split(":") ?? []; - if (progress?.finished) { - clearInterval(interval); - resolve(progress.error); - } - }, 1000); - }); + console.log({ key, value }); - if (error) { - toastr.error(error); + // await copyToFolder( + // Number(selectedItemId), + // [], + // [fileInfo.id], + // conflictResolve, + // false, + // ); + + // const error = await new Promise((resolve) => { + // const interval = setInterval(async () => { + // const [progress] = await getProgress(); + + // if (progress?.finished) { + // clearInterval(interval); + // resolve(progress.error); + // } + // }, 1000); + // }); + + if (key === "error") { + toastr.error(value); } else { window.location.replace(url.toString()); onClose(); @@ -208,6 +254,7 @@ const useStartFillingSelectDialog = (fileInfo: TFile | undefined) => { onSubmitStartFillingSelectDialog: onSubmit, onCloseStartFillingSelectDialog: onClose, getIsDisabledStartFillingSelectDialog: getIsDisabled, + onDownloadAs, isVisibleStartFillingSelectDialog: isVisible, conflictDataDialog, headerLabelSFSDialog, diff --git a/packages/doceditor/src/types/index.ts b/packages/doceditor/src/types/index.ts index ccdf60007f..96ae2ee7cc 100644 --- a/packages/doceditor/src/types/index.ts +++ b/packages/doceditor/src/types/index.ts @@ -228,6 +228,7 @@ export type EditorProps = { errorMessage?: string; isSkipError?: boolean; + onDownloadAs: (obj: object) => void; onSDKRequestSharingSettings?: () => void; onSDKRequestSaveAs?: (event: object) => void; onSDKRequestInsertImage?: (event: object) => void;