From 1c25b5a27ce7cbcb1069abde6a9bda99bdfe6e97 Mon Sep 17 00:00:00 2001 From: Artem Tarasov Date: Mon, 28 Mar 2022 11:24:55 +0300 Subject: [PATCH] Web: Doceditor: added init for desktop editors, refactoring --- web/ASC.Web.Editor/src/Editor.js | 82 ++++++++++++++----------- web/ASC.Web.Editor/src/client/index.js | 10 ++- web/ASC.Web.Editor/src/helpers/utils.js | 30 +++------ 3 files changed, 63 insertions(+), 59 deletions(-) diff --git a/web/ASC.Web.Editor/src/Editor.js b/web/ASC.Web.Editor/src/Editor.js index 708953d80a..29fca186c4 100644 --- a/web/ASC.Web.Editor/src/Editor.js +++ b/web/ASC.Web.Editor/src/Editor.js @@ -21,6 +21,8 @@ import { EditorWrapper } from "./StyledEditor"; import { useTranslation } from "react-i18next"; import withDialogs from "./helpers/withDialogs"; +import { canConvert, convertDocumentUrl } from "./helpers/utils"; + const LoaderComponent = ( { ); }; -const initDesktop = (cfg) => { - const encryptionKeys = cfg?.editorConfig?.encryptionKeys; - - regDesktop( - user, - !!encryptionKeys, - encryptionKeys, - (keys) => { - setEncryptionKeys(keys); - }, - true, - (callback) => { - getEncryptionAccess(fileId) - .then((keys) => { - var data = { - keys, - }; - - callback(data); - }) - .catch((error) => { - window.toastr.error( - typeof error === "string" ? error : error.message, - null, - 0, - true - ); - }); - }, - t - ); -}; - const text = "text"; const presentation = "presentation"; let documentIsReady = false; // move to state? @@ -124,6 +93,8 @@ function Editor({ onSDKRequestSaveAs, isFileDialogVisible, isFolderDialogVisible, + isDesktopEditor, + initDesktop, }) { const [isLoaded, setIsLoaded] = useState(false); const [documentTitle, setNewDocumentTitle] = useState("Loading..."); @@ -143,14 +114,51 @@ function Editor({ if (config) { document.getElementById("scripDocServiceAddress").onload = onLoad(); setDocumentTitle(config?.document?.title); - } - if (isIOS && deviceType === "tablet") { - const vh = window.innerHeight * 0.01; - document.documentElement.style.setProperty("--vh", `${vh}px`); + if (isIOS && deviceType === "tablet") { + const vh = window.innerHeight * 0.01; + document.documentElement.style.setProperty("--vh", `${vh}px`); + } } }, []); + useEffect(() => { + if (config) { + console.log(isDesktopEditor, config); + if (isDesktopEditor) { + initDesktop(config, user, fileId, t); + } + } + }, [isDesktopEditor]); + + // useEffect(async () => { + // try { + // if ( + // url.indexOf("#message/") > -1 && + // fileInfo && + // fileInfo?.fileExst && + // canConvert(fileInfo.fileExst) + // ) { + // const result = await convertDocumentUrl(); + + // const splitUrl = url.split("#message/"); + + // if (result) { + // const newUrl = `${result.webUrl}#message/${splitUrl[1]}`; + + // history.pushState({}, null, newUrl); + + // // fileInfo = result; + // // url = newUrl; + // // fileId = result.id; + // // version = result.version; + // } + // } + // } catch (err) { + // console.error(err); + // } + // }, [url, fileInfo?.fileExst]); + const getDefaultFileName = (format) => { switch (format) { case "docx": diff --git a/web/ASC.Web.Editor/src/client/index.js b/web/ASC.Web.Editor/src/client/index.js index c9bff4ea90..820750d255 100644 --- a/web/ASC.Web.Editor/src/client/index.js +++ b/web/ASC.Web.Editor/src/client/index.js @@ -4,6 +4,7 @@ import { registerSW } from "@appserver/common/sw/helper"; import App from "../App.js"; import { useSSR } from "react-i18next"; import useMfScripts from "../helpers/useMfScripts"; +import initDesktop from "../helpers/initDesktop"; const propsObj = window.__ASC_INITIAL_STATE__; const initialI18nStore = window.initialI18nStore; @@ -17,6 +18,7 @@ const stateInit = document.getElementById("__ASC_INITIAL_STATE__"); const i18nInit = document.getElementById("__ASC_I18N_INIT__"); stateInit.parentNode.removeChild(stateInit); i18nInit.parentNode.removeChild(i18nInit); +const isDesktopEditor = window["AscDesktopEditor"] !== undefined; const AppWrapper = () => { const [isInitialized, isErrorLoading] = useMfScripts(); @@ -24,7 +26,13 @@ const AppWrapper = () => { return ( }> - + ); }; diff --git a/web/ASC.Web.Editor/src/helpers/utils.js b/web/ASC.Web.Editor/src/helpers/utils.js index 96211c8ffd..5b51892e36 100644 --- a/web/ASC.Web.Editor/src/helpers/utils.js +++ b/web/ASC.Web.Editor/src/helpers/utils.js @@ -8,7 +8,11 @@ import { getFileInfo, checkFillFormDraft, openEdit, + convertFile, + setEncryptionKeys, + getEncryptionAccess, } from "@appserver/common/api/files"; + import pkg from "../../package.json"; export const canConvert = (extension, filesSettings) => { @@ -17,13 +21,17 @@ export const canConvert = (extension, filesSettings) => { return result === -1 ? false : true; }; +export const convertDocumentUrl = async () => { + const convert = await convertFile(fileId, null, true); + return convert && convert[0]?.result; +}; + export const initDocEditor = async (req) => { if (!req) return false; const { headers, url, query } = req; const { version, desktop: isDesktop } = query; let error = null; - initSSR(headers); try { @@ -70,19 +78,6 @@ export const initDocEditor = async (req) => { getFileInfo(fileId), ]); - if (successAuth) { - try { - // if (url.indexOf("#message/") > -1) { - // if (canConvert(fileInfo.fileExst)) { - // const url = await convertDocumentUrl(); - // history.pushState({}, null, url); - // } - // } TODO: move to hook? - } catch (err) { - error = { errorMessage: typeof err === "string" ? err : err.message }; - } - } - let formUrl; if ( @@ -102,12 +97,6 @@ export const initDocEditor = async (req) => { } } - const needInitDesktop = false; - if (isDesktop) { - // initDesktop(config); TODO: move to hook - needInitDesktop = true; - } - const isSharingAccess = fileInfo && fileInfo.canShare; if (view) { @@ -130,7 +119,6 @@ export const initDocEditor = async (req) => { url, doc, fileId, - needInitDesktop, }, }; } catch (err) {