DocSpace-client/web/ASC.Web.Editor/src/Editor.jsx

523 lines
13 KiB
React
Raw Normal View History

2021-03-06 15:50:06 +00:00
import React, { useEffect, useState } from "react";
2021-03-05 18:15:48 +00:00
import Toast from "@appserver/components/toast";
import toastr from "studio/toastr";
import { toast } from "react-toastify";
2021-03-05 18:15:48 +00:00
import Box from "@appserver/components/box";
import { regDesktop } from "@appserver/common/desktop";
import Loaders from "@appserver/common/components/Loaders";
import {
2021-03-22 09:12:34 +00:00
combineUrl,
2021-03-05 18:15:48 +00:00
getObjectByLocation,
//showLoader,
//hideLoader,
} from "@appserver/common/utils";
import {
getDocServiceUrl,
openEdit,
setEncryptionKeys,
getEncryptionAccess,
getFileInfo,
2021-07-27 06:51:56 +00:00
getRecentFolderList,
getFolderInfo,
2021-07-27 07:36:21 +00:00
updateFile,
removeFromFavorite,
markAsFavorite,
2021-03-05 18:15:48 +00:00
} from "@appserver/common/api/files";
import { checkIsAuthenticated } from "@appserver/common/api/user";
import { getUser } from "@appserver/common/api/people";
import FilesFilter from "@appserver/common/api/files/filter";
2021-03-05 18:15:48 +00:00
import throttle from "lodash/throttle";
2021-03-06 15:50:06 +00:00
import { isIOS, deviceType } from "react-device-detect";
import { homepage } from "../package.json";
2021-03-22 09:12:34 +00:00
import { AppServerConfig } from "@appserver/common/constants";
import SharingDialog from "files/SharingDialog";
import { createNewFile, getDefaultFileName } from "files/utils";
import i18n from "./i18n";
import { FolderType } from "@appserver/common/constants";
2021-03-05 18:15:48 +00:00
let documentIsReady = false;
const text = "text";
const spreadSheet = "spreadsheet";
const presentation = "presentation";
2021-03-05 18:15:48 +00:00
let docTitle = null;
let fileType = null;
2021-03-26 12:05:41 +00:00
let config;
2021-03-05 18:15:48 +00:00
let docSaved = null;
2021-03-26 12:05:41 +00:00
let docEditor;
let fileInfo;
const url = window.location.href;
const filesUrl = url.substring(0, url.indexOf("/doceditor"));
toast.configure();
2021-04-01 08:50:30 +00:00
const Editor = () => {
2021-03-06 15:50:06 +00:00
const urlParams = getObjectByLocation(window.location);
const fileId = urlParams
? urlParams.fileId || urlParams.fileid || null
: null;
2021-04-23 15:00:05 +00:00
const version = urlParams ? urlParams.version || null : null;
2021-03-06 15:50:06 +00:00
const doc = urlParams ? urlParams.doc || null : null;
const isDesktop = window["AscDesktopEditor"] !== undefined;
const [isLoading, setIsLoading] = useState(true);
const [isAuthenticated, setIsAuthenticated] = useState(true);
2021-03-05 18:15:48 +00:00
2021-03-06 15:50:06 +00:00
const throttledChangeTitle = throttle(
() => changeTitle(docSaved, docTitle),
500
);
2021-03-26 12:05:41 +00:00
useEffect(() => {
init();
2021-03-26 12:05:41 +00:00
}, []);
2021-03-28 16:21:08 +00:00
const updateUsersRightsList = () => {
SharingDialog.getSharingSettings(fileId).then((sharingSettings) => {
docEditor.setSharingSettings({
sharingSettings,
});
});
};
const updateFavorite = (favorite) => {
docEditor.setFavorite(favorite);
};
2021-03-06 15:50:06 +00:00
const init = async () => {
try {
2021-03-05 18:15:48 +00:00
if (!fileId) return;
2021-04-23 15:00:05 +00:00
console.log(
`Editor componentDidMount fileId=${fileId}, version=${version}, doc=${doc}`
);
2021-03-05 18:15:48 +00:00
if (isIPad()) {
const vh = window.innerHeight * 0.01;
document.documentElement.style.setProperty("--vh", `${vh}px`);
}
//showLoader();
const docApiUrl = await getDocServiceUrl();
const success = await checkIsAuthenticated();
2021-03-05 18:15:48 +00:00
if (!doc && !success) {
window.open(
combineUrl(AppServerConfig.proxyURL, "/login"),
"_self",
"",
true
);
return;
}
2021-03-05 18:15:48 +00:00
if (success) {
try {
fileInfo = await getFileInfo(fileId);
} catch (err) {
console.error(err);
2021-03-06 15:50:06 +00:00
}
setIsAuthenticated(success);
2021-03-05 18:15:48 +00:00
}
2021-04-23 15:00:05 +00:00
config = await openEdit(fileId, version, doc);
2021-03-05 18:15:48 +00:00
if (isDesktop) {
const isEncryption =
config.editorConfig["encryptionKeys"] !== undefined;
const user = await getUser();
regDesktop(
user,
isEncryption,
config.editorConfig.encryptionKeys,
(keys) => {
setEncryptionKeys(keys);
},
true,
(callback) => {
getEncryptionAccess(fileId)
.then((keys) => {
var data = {
keys,
};
callback(data);
})
.catch((error) => {
console.log(error);
toastr.error(
typeof error === "string" ? error : error.message,
null,
0,
true
);
});
},
i18n.t
2021-03-05 18:15:48 +00:00
);
}
2021-07-27 06:51:56 +00:00
//console.log("config.document.info", config, "fileInfo", fileInfo);
if (fileInfo) {
const recentFolderList = await getRecentFolderList();
let recentFilesArray = [];
const filesArray = recentFolderList.files;
for (let i = 0; i < filesArray.length; i++) {
if (
config.documentType === text &&
filesArray[i].fileType === 7 &&
filesArray[i].rootFolderType !== FolderType.SHARE
) {
2021-07-27 06:51:56 +00:00
const folderInfo = await getFolderInfo(filesArray[i].folderId);
const convertedData = convertRecentData(filesArray[i], folderInfo);
if (Object.keys(convertedData).length !== 0)
recentFilesArray.push(convertedData);
}
if (
config.documentType === spreadSheet &&
filesArray[i].fileType === 5 &&
filesArray[i].rootFolderType !== FolderType.SHARE
2021-07-27 06:51:56 +00:00
) {
const folderInfo = await getFolderInfo(filesArray[i].folderId);
const convertedData = convertRecentData(filesArray[i], folderInfo);
if (Object.keys(convertedData).length !== 0)
recentFilesArray.push(convertedData);
}
if (
config.documentType === presentation &&
filesArray[i].fileType === 6 &&
filesArray[i].rootFolderType !== FolderType.SHARE
2021-07-27 06:51:56 +00:00
) {
const folderInfo = await getFolderInfo(filesArray[i].folderId);
const convertedData = convertRecentData(filesArray[i], folderInfo);
if (Object.keys(convertedData).length !== 0)
recentFilesArray.push(convertedData);
}
}
config.editorConfig = {
...config.editorConfig,
recent: recentFilesArray,
};
}
2021-03-05 18:15:48 +00:00
if (
config &&
config.document.permissions.edit &&
config.document.permissions.modifyFilter &&
fileInfo
) {
const sharingSettings = await SharingDialog.getSharingSettings(fileId);
config.document.info = {
...config.document.info,
sharingSettings,
};
}
2021-03-06 15:50:06 +00:00
setIsLoading(false);
loadDocApi(docApiUrl, () => onLoad(config));
2021-03-05 18:15:48 +00:00
} catch (error) {
console.log(error);
toastr.error(
typeof error === "string" ? error : error.message,
null,
0,
true
);
}
2021-03-06 15:50:06 +00:00
};
2021-07-27 06:51:56 +00:00
const convertRecentData = (file, folder) => {
let obj = {};
const folderName = folder.title;
const fileName = file.title;
const url = file.webUrl;
if (fileInfo.id !== file.id)
obj = {
folder: folderName,
title: fileName,
url: url,
};
return obj;
};
2021-03-06 15:50:06 +00:00
const isIPad = () => {
return isIOS && deviceType === "tablet";
};
const setFavicon = (fileType) => {
const favicon = document.getElementById("favicon");
if (!favicon) return;
let icon = null;
switch (fileType) {
case "docx":
icon = "text.ico";
break;
case "pptx":
icon = "presentation.ico";
break;
case "xlsx":
icon = "spreadsheet.ico";
break;
default:
break;
}
if (icon) favicon.href = `${homepage}/images/${icon}`;
};
const changeTitle = (docSaved, docTitle) => {
docSaved ? setDocumentTitle(docTitle) : setDocumentTitle(`*${docTitle}`);
};
const setDocumentTitle = (subTitle = null) => {
//const { isAuthenticated, settingsStore, product: currentModule } = auth;
//const { organizationName } = settingsStore;
const organizationName = "ONLYOFFICE"; //TODO: Replace to API variant
const moduleTitle = "Documents"; //TODO: Replace to API variant
let title;
if (subTitle) {
if (isAuthenticated && moduleTitle) {
title = subTitle + " - " + moduleTitle;
} else {
title = subTitle + " - " + organizationName;
}
} else if (moduleTitle && organizationName) {
title = moduleTitle + " - " + organizationName;
} else {
title = organizationName;
}
document.title = title;
};
const loadDocApi = (docApiUrl, onLoadCallback) => {
2021-03-05 18:15:48 +00:00
const script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("id", "scripDocServiceAddress");
script.onload = onLoadCallback;
script.src = docApiUrl;
script.async = true;
console.log("PureEditor componentDidMount: added script");
document.body.appendChild(script);
};
2021-03-06 15:50:06 +00:00
const onLoad = (config) => {
2021-03-05 18:15:48 +00:00
console.log("Editor config: ", config);
2021-03-26 12:05:41 +00:00
2021-03-05 18:15:48 +00:00
try {
console.log(config);
docTitle = config.document.title;
fileType = config.document.fileType;
setFavicon(fileType);
setDocumentTitle(docTitle);
if (window.innerWidth < 720) {
config.type = "mobile";
}
let goback;
if (fileInfo) {
const filterObj = FilesFilter.getDefault();
filterObj.folder = fileInfo.folderId;
const urlFilter = filterObj.toUrlParams();
goback = {
blank: true,
requestClose: false,
text: i18n.t("FileLocation"),
url: `${combineUrl(filesUrl, `/filter?${urlFilter}`)}`,
};
}
config.editorConfig.customization = {
...config.editorConfig.customization,
goback,
};
let onRequestSharingSettings;
2021-07-27 07:36:21 +00:00
let onRequestRename;
let onRequestCreateNew;
if (
fileInfo &&
config.document.permissions.edit &&
config.document.permissions.modifyFilter
) {
onRequestSharingSettings = onSDKRequestSharingSettings;
2021-07-27 07:36:21 +00:00
onRequestRename = onSDKRequestRename;
}
if (fileInfo) {
onRequestCreateNew = onSDKRequestCreateNew;
}
2021-03-05 18:15:48 +00:00
const events = {
events: {
2021-03-06 15:50:06 +00:00
onAppReady: onSDKAppReady,
onDocumentStateChange: onDocumentStateChange,
onMetaChange: onMetaChange,
onDocumentReady: onDocumentReady,
onInfo: onSDKInfo,
onWarning: onSDKWarning,
onError: onSDKError,
onRequestSharingSettings,
2021-07-27 07:36:21 +00:00
onRequestRename,
onRequestCreateNew,
2021-03-05 18:15:48 +00:00
},
};
const newConfig = Object.assign(config, events);
if (!window.DocsAPI) throw new Error("DocsAPI is not defined");
2021-03-26 12:05:41 +00:00
docEditor = window.DocsAPI.DocEditor("editor", newConfig);
2021-03-05 18:15:48 +00:00
} catch (error) {
console.log(error);
toastr.error(error.message, null, 0, true);
}
};
2021-03-06 15:50:06 +00:00
const onSDKAppReady = () => {
2021-03-26 14:35:44 +00:00
console.log("ONLYOFFICE Document Editor is ready");
2021-03-05 18:15:48 +00:00
};
2021-03-06 15:50:06 +00:00
const onSDKInfo = (event) => {
2021-03-05 18:15:48 +00:00
console.log(
"ONLYOFFICE Document Editor is opened in mode " + event.data.mode
);
};
const [isVisible, setIsVisible] = useState(false);
2021-03-26 12:05:41 +00:00
const onSDKRequestSharingSettings = () => {
setIsVisible(true);
};
const onSDKRequestCreateNew = () => {
const documentType = config.documentType;
const fileExst =
documentType === text
? "docx"
: documentType === presentation
? "pptx"
: "xlsx";
const defaultFileName = getDefaultFileName(fileExst);
createNewFile(fileInfo.folderId, `${defaultFileName}.${fileExst}`);
};
2021-07-27 07:36:21 +00:00
const onSDKRequestRename = (event) => {
const title = event.data;
updateFile(fileInfo.id, title);
};
const onCancel = () => {
setIsVisible(false);
2021-03-26 12:05:41 +00:00
};
2021-03-06 15:50:06 +00:00
const onSDKWarning = (event) => {
2021-03-05 18:15:48 +00:00
console.log(
"ONLYOFFICE Document Editor reports a warning: code " +
event.data.warningCode +
", description " +
event.data.warningDescription
);
};
2021-03-06 15:50:06 +00:00
const onSDKError = (event) => {
2021-03-05 18:15:48 +00:00
console.log(
"ONLYOFFICE Document Editor reports an error: code " +
event.data.errorCode +
", description " +
event.data.errorDescription
);
};
2021-03-06 15:50:06 +00:00
const onDocumentStateChange = (event) => {
2021-03-05 18:15:48 +00:00
if (!documentIsReady) return;
docSaved = !event.data;
throttledChangeTitle();
};
2021-03-06 15:50:06 +00:00
const onDocumentReady = () => {
2021-03-05 18:15:48 +00:00
documentIsReady = true;
};
2021-03-06 15:50:06 +00:00
const onMetaChange = (event) => {
2021-03-05 18:15:48 +00:00
const newTitle = event.data.title;
const favorite = event.data.favorite;
2021-03-05 18:15:48 +00:00
if (newTitle && newTitle !== docTitle) {
setDocumentTitle(newTitle);
docTitle = newTitle;
}
if (!newTitle)
favorite
? markAsFavorite([+fileId])
.then(() => updateFavorite(favorite))
.catch((error) => console.log("error", error))
: removeFromFavorite([+fileId])
.then(() => updateFavorite(favorite))
.catch((error) => console.log("error", error));
2021-03-05 18:15:48 +00:00
};
2021-03-06 15:50:06 +00:00
return (
<Box
widthProp="100vw"
heightProp={isIPad() ? "calc(var(--vh, 1vh) * 100)" : "100vh"}
>
<Toast />
2021-03-06 15:50:06 +00:00
{!isLoading ? (
<>
<div id="editor"></div>
{fileInfo && (
<SharingDialog
isVisible={isVisible}
sharingObject={fileInfo}
onCancel={onCancel}
onSuccess={updateUsersRightsList}
/>
)}
</>
2021-03-06 15:50:06 +00:00
) : (
<Box paddingProp="16px">
<Loaders.Rectangle height="96vh" />
</Box>
)}
</Box>
);
};
2021-03-05 18:15:48 +00:00
export default Editor;