web-apps/apps/presentationeditor/mobile/src/controller/ContextMenu.jsx

223 lines
7.6 KiB
React
Raw Normal View History

2021-03-23 11:05:45 +00:00
import React, { useContext } from 'react';
import { f7 } from 'framework7-react';
import { inject, observer } from "mobx-react";
import { withTranslation} from 'react-i18next';
import { LocalStorage } from '../../../../common/mobile/utils/LocalStorage';
import ContextMenuController from '../../../../common/mobile/lib/controller/ContextMenu';
import { idContextMenuElement } from '../../../../common/mobile/lib/view/ContextMenu';
import { Device } from '../../../../common/mobile/utils/device';
2021-04-16 10:34:05 +00:00
import EditorUIController from '../lib/patch';
2021-03-23 11:05:45 +00:00
@inject ( stores => ({
isEdit: stores.storeAppOptions.isEdit,
canViewComments: stores.storeAppOptions.canViewComments,
users: stores.users,
isDisconnected: stores.users.isDisconnected
}))
class ContextMenu extends ContextMenuController {
constructor(props) {
super(props);
// console.log('context menu controller created');
this.onApiShowComment = this.onApiShowComment.bind(this);
this.onApiHideComment = this.onApiHideComment.bind(this);
this.getUserName = this.getUserName.bind(this);
}
static closeContextMenu() {
f7.popover.close(idContextMenuElement, false);
}
getUserName(id) {
const user = this.props.users.searchUserByCurrentId(id);
return Common.Utils.UserInfoParser.getParsedName(user.asc_getUserName());
}
componentWillUnmount() {
super.componentWillUnmount();
const api = Common.EditorApi.get();
api.asc_unregisterCallback('asc_onShowComment', this.onApiShowComment);
api.asc_unregisterCallback('asc_onHideComment', this.onApiHideComment);
}
onApiShowComment(comments) {
this.isComments = comments && comments.length > 0;
}
onApiHideComment() {
this.isComments = false;
}
// onMenuClosed() {
// super.onMenuClosed();
// }
onMenuItemClick(action) {
super.onMenuItemClick(action);
if ( EditorUIController.ContextMenu && EditorUIController.ContextMenu.handleMenuItemClick(this, action) )
2021-04-16 10:34:05 +00:00
return;
2021-03-23 11:05:45 +00:00
const api = Common.EditorApi.get();
switch (action) {
case 'cut':
2021-04-16 10:34:05 +00:00
if ( !LocalStorage.getBool("pe-hide-copy-cut-paste-warning")) {
2021-03-23 11:05:45 +00:00
this.showCopyCutPasteModal();
}
break;
case 'copy':
if (!api.Copy() && !LocalStorage.getBool("pe-hide-copy-cut-paste-warning")) {
this.showCopyCutPasteModal();
}
break;
case 'paste':
2021-04-16 10:34:05 +00:00
if ( !LocalStorage.getBool("pe-hide-copy-cut-paste-warning")) {
2021-03-23 11:05:45 +00:00
this.showCopyCutPasteModal();
}
break;
case 'viewcomment':
Common.Notifications.trigger('viewcomment');
break;
case 'openlink':
const stack = Common.EditorApi.get().getSelectedElements();
let value;
stack.forEach((item) => {
if (item.get_ObjectType() == Asc.c_oAscTypeSelectElement.Hyperlink) {
value = item.get_ObjectValue().get_Value();
}
});
value && this.openLink(value);
break;
}
}
showCopyCutPasteModal() {
const { t } = this.props;
const _t = t("ContextMenu", { returnObjects: true });
f7.dialog.create({
title: _t.textCopyCutPasteActions,
text: _t.errorCopyCutPaste,
content: `<div class="checkbox-in-modal">
<label class="checkbox">
<input type="checkbox" name="checkbox-show" />
<i class="icon-checkbox"></i>
</label>
<span class="right-text">${_t.textDoNotShowAgain}</span>
</div>`,
buttons: [{
text: 'OK',
onClick: () => {
const dontShow = $$('input[name="checkbox-show"]').prop('checked');
if (dontShow) LocalStorage.setItem("de-hide-copy-cut-paste-warning", 1);
}
}]
}).open();
}
openLink(url) {
const api = Common.EditorApi.get();
if (api.asc_getUrlType(url) > 0) {
const newDocumentPage = window.open(url, '_blank');
if (newDocumentPage) {
newDocumentPage.focus();
}
} else {
api.asc_GoToInternalHyperlink(url);
}
}
onDocumentReady() {
super.onDocumentReady();
const api = Common.EditorApi.get();
api.asc_registerCallback('asc_onShowComment', this.onApiShowComment);
api.asc_registerCallback('asc_onHideComment', this.onApiHideComment);
}
initMenuItems() {
if ( !Common.EditorApi ) return [];
2021-04-16 10:34:05 +00:00
const { isEdit } = this.props;
2021-03-23 11:05:45 +00:00
if (isEdit && EditorUIController.ContextMenu) {
2021-04-16 10:34:05 +00:00
return EditorUIController.ContextMenu.mapMenuItems(this);
} else {
const { t } = this.props;
const _t = t("ContextMenu", { returnObjects: true });
const { canViewComments, isDisconnected } = this.props;
const api = Common.EditorApi.get();
const stack = api.getSelectedElements();
const canCopy = api.can_CopyCut();
let itemsIcon = [],
itemsText = [];
let isText = false,
isTable = false,
isImage = false,
isChart = false,
isShape = false,
isLink = false,
isSlide = false,
isObject = false;
stack.forEach(item => {
const objectType = item.get_ObjectType(),
objectValue = item.get_ObjectValue();
if (objectType == Asc.c_oAscTypeSelectElement.Paragraph) {
isText = true;
} else if (objectType == Asc.c_oAscTypeSelectElement.Image) {
isImage = true;
} else if (objectType == Asc.c_oAscTypeSelectElement.Chart) {
isChart = true;
} else if (objectType == Asc.c_oAscTypeSelectElement.Shape) {
isShape = true;
} else if (objectType == Asc.c_oAscTypeSelectElement.Table) {
isTable = true;
} else if (objectType == Asc.c_oAscTypeSelectElement.Hyperlink) {
isLink = true;
} else if (objectType == Asc.c_oAscTypeSelectElement.Slide) {
isSlide = true;
}
2021-03-23 11:05:45 +00:00
});
2021-04-16 10:34:05 +00:00
isObject = isText || isImage || isChart || isShape || isTable;
2021-03-23 11:05:45 +00:00
2021-04-16 10:34:05 +00:00
if (canCopy && isObject) {
2021-03-23 11:05:45 +00:00
itemsIcon.push({
2021-04-16 10:34:05 +00:00
event: 'copy',
icon: 'icon-copy'
2021-03-23 11:05:45 +00:00
});
2021-04-16 10:34:05 +00:00
}
if (canViewComments && this.isComments && !isEdit) {
2021-03-23 11:05:45 +00:00
itemsText.push({
2021-04-16 10:34:05 +00:00
caption: _t.menuViewComment,
event: 'viewcomment'
2021-03-23 11:05:45 +00:00
});
}
2021-04-16 10:34:05 +00:00
if (isLink) {
itemsText.push({
caption: _t.menuOpenLink,
event: 'openlink'
});
}
2021-03-23 11:05:45 +00:00
2021-04-16 10:34:05 +00:00
return itemsIcon.concat(itemsText);
2021-03-23 11:05:45 +00:00
}
}
initExtraItems () {
return (this.extraItems && this.extraItems.length > 0 ? this.extraItems : []);
}
}
const _ContextMenu = withTranslation()(ContextMenu);
_ContextMenu.closeContextMenu = ContextMenu.closeContextMenu;
export { _ContextMenu as default };