Web: Doceditor: added init for desktop editors, refactoring

This commit is contained in:
Artem Tarasov 2022-03-28 11:24:55 +03:00
parent 0e76b57cee
commit 1c25b5a27c
3 changed files with 63 additions and 59 deletions

View File

@ -21,6 +21,8 @@ import { EditorWrapper } from "./StyledEditor";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import withDialogs from "./helpers/withDialogs"; import withDialogs from "./helpers/withDialogs";
import { canConvert, convertDocumentUrl } from "./helpers/utils";
const LoaderComponent = ( const LoaderComponent = (
<Loader <Loader
type="rombs" type="rombs"
@ -59,39 +61,6 @@ const onSDKError = (event) => {
); );
}; };
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 text = "text";
const presentation = "presentation"; const presentation = "presentation";
let documentIsReady = false; // move to state? let documentIsReady = false; // move to state?
@ -124,6 +93,8 @@ function Editor({
onSDKRequestSaveAs, onSDKRequestSaveAs,
isFileDialogVisible, isFileDialogVisible,
isFolderDialogVisible, isFolderDialogVisible,
isDesktopEditor,
initDesktop,
}) { }) {
const [isLoaded, setIsLoaded] = useState(false); const [isLoaded, setIsLoaded] = useState(false);
const [documentTitle, setNewDocumentTitle] = useState("Loading..."); const [documentTitle, setNewDocumentTitle] = useState("Loading...");
@ -143,14 +114,51 @@ function Editor({
if (config) { if (config) {
document.getElementById("scripDocServiceAddress").onload = onLoad(); document.getElementById("scripDocServiceAddress").onload = onLoad();
setDocumentTitle(config?.document?.title); setDocumentTitle(config?.document?.title);
}
if (isIOS && deviceType === "tablet") { if (isIOS && deviceType === "tablet") {
const vh = window.innerHeight * 0.01; const vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty("--vh", `${vh}px`); 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) => { const getDefaultFileName = (format) => {
switch (format) { switch (format) {
case "docx": case "docx":

View File

@ -4,6 +4,7 @@ import { registerSW } from "@appserver/common/sw/helper";
import App from "../App.js"; import App from "../App.js";
import { useSSR } from "react-i18next"; import { useSSR } from "react-i18next";
import useMfScripts from "../helpers/useMfScripts"; import useMfScripts from "../helpers/useMfScripts";
import initDesktop from "../helpers/initDesktop";
const propsObj = window.__ASC_INITIAL_STATE__; const propsObj = window.__ASC_INITIAL_STATE__;
const initialI18nStore = window.initialI18nStore; const initialI18nStore = window.initialI18nStore;
@ -17,6 +18,7 @@ const stateInit = document.getElementById("__ASC_INITIAL_STATE__");
const i18nInit = document.getElementById("__ASC_I18N_INIT__"); const i18nInit = document.getElementById("__ASC_I18N_INIT__");
stateInit.parentNode.removeChild(stateInit); stateInit.parentNode.removeChild(stateInit);
i18nInit.parentNode.removeChild(i18nInit); i18nInit.parentNode.removeChild(i18nInit);
const isDesktopEditor = window["AscDesktopEditor"] !== undefined;
const AppWrapper = () => { const AppWrapper = () => {
const [isInitialized, isErrorLoading] = useMfScripts(); const [isInitialized, isErrorLoading] = useMfScripts();
@ -24,7 +26,13 @@ const AppWrapper = () => {
return ( return (
<Suspense fallback={<div />}> <Suspense fallback={<div />}>
<App {...propsObj} mfReady={isInitialized} mfFailed={isErrorLoading} /> <App
{...propsObj}
mfReady={isInitialized}
mfFailed={isErrorLoading}
isDesktopEditor={isDesktopEditor}
initDesktop={initDesktop}
/>
</Suspense> </Suspense>
); );
}; };

View File

@ -8,7 +8,11 @@ import {
getFileInfo, getFileInfo,
checkFillFormDraft, checkFillFormDraft,
openEdit, openEdit,
convertFile,
setEncryptionKeys,
getEncryptionAccess,
} from "@appserver/common/api/files"; } from "@appserver/common/api/files";
import pkg from "../../package.json"; import pkg from "../../package.json";
export const canConvert = (extension, filesSettings) => { export const canConvert = (extension, filesSettings) => {
@ -17,13 +21,17 @@ export const canConvert = (extension, filesSettings) => {
return result === -1 ? false : true; 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) => { export const initDocEditor = async (req) => {
if (!req) return false; if (!req) return false;
const { headers, url, query } = req; const { headers, url, query } = req;
const { version, desktop: isDesktop } = query; const { version, desktop: isDesktop } = query;
let error = null; let error = null;
initSSR(headers); initSSR(headers);
try { try {
@ -70,19 +78,6 @@ export const initDocEditor = async (req) => {
getFileInfo(fileId), 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; let formUrl;
if ( 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; const isSharingAccess = fileInfo && fileInfo.canShare;
if (view) { if (view) {
@ -130,7 +119,6 @@ export const initDocEditor = async (req) => {
url, url,
doc, doc,
fileId, fileId,
needInitDesktop,
}, },
}; };
} catch (err) { } catch (err) {