2023-05-12 06:12:28 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { EventBus, EXTERNAL_EVENT } from '@seafile/sdoc-editor';
|
2023-10-26 09:15:42 +00:00
|
|
|
import { seafileAPI } from '../../../utils/seafile-api';
|
|
|
|
import { Utils } from '../../../utils/utils';
|
|
|
|
import toaster from '../../../components/toast';
|
|
|
|
import InternalLinkDialog from '../../../components/dialog/internal-link-dialog';
|
|
|
|
import ShareDialog from '../../../components/dialog/share-dialog';
|
2024-01-06 06:17:43 +00:00
|
|
|
import CreateFile from '../../../components/dialog/create-file-dialog';
|
2023-05-12 06:12:28 +00:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
repoID: PropTypes.string.isRequired,
|
|
|
|
docPath: PropTypes.string.isRequired,
|
2023-08-10 10:05:01 +00:00
|
|
|
docName: PropTypes.string.isRequired,
|
|
|
|
docPerm: PropTypes.string.isRequired,
|
2023-06-30 13:51:27 +00:00
|
|
|
isStarred: PropTypes.bool.isRequired,
|
2024-01-06 06:17:43 +00:00
|
|
|
direntList: PropTypes.array.isRequired,
|
|
|
|
dirPath: PropTypes.string.isRequired,
|
2023-07-22 07:54:25 +00:00
|
|
|
toggleStar: PropTypes.func.isRequired,
|
2024-03-01 07:22:47 +00:00
|
|
|
onNewNotification: PropTypes.func.isRequired,
|
2024-03-01 10:15:01 +00:00
|
|
|
onClearNotification: PropTypes.func.isRequired
|
2023-05-12 06:12:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ExternalOperations extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
isShowInternalLinkDialog: false,
|
2023-08-10 10:05:01 +00:00
|
|
|
isShowShareDialog: false,
|
2023-12-25 08:06:36 +00:00
|
|
|
internalLink: '',
|
2024-01-06 06:17:43 +00:00
|
|
|
isShowCreateFileDialog: false,
|
2024-01-08 02:55:34 +00:00
|
|
|
fileType: '.sdoc',
|
2023-05-12 06:12:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const eventBus = EventBus.getInstance();
|
|
|
|
this.unsubscribeInternalLinkEvent = eventBus.subscribe(EXTERNAL_EVENT.INTERNAL_LINK_CLICK, this.onInternalLinkToggle);
|
2023-06-30 13:51:27 +00:00
|
|
|
this.unsubscribeStar = eventBus.subscribe(EXTERNAL_EVENT.TOGGLE_STAR, this.toggleStar);
|
2023-08-10 10:05:01 +00:00
|
|
|
this.unsubscribeShare = eventBus.subscribe(EXTERNAL_EVENT.SHARE_SDOC, this.onShareToggle);
|
2024-01-05 03:49:34 +00:00
|
|
|
this.unsubscribeFreezeDocument = eventBus.subscribe(EXTERNAL_EVENT.FREEZE_DOCUMENT, this.onFreezeDocument);
|
|
|
|
this.unsubscribeUnfreeze = eventBus.subscribe(EXTERNAL_EVENT.UNFREEZE, this.unFreeze);
|
|
|
|
this.unsubscribeNewNotification = eventBus.subscribe(EXTERNAL_EVENT.NEW_NOTIFICATION, this.onNewNotification);
|
2024-03-01 10:15:01 +00:00
|
|
|
this.unsubscribeClearNotification = eventBus.subscribe(EXTERNAL_EVENT.CLEAR_NOTIFICATION, this.onClearNotification);
|
2024-01-06 06:17:43 +00:00
|
|
|
this.unsubscribeCreateSdocFile = eventBus.subscribe(EXTERNAL_EVENT.CREATE_SDOC_FILE, this.onCreateSdocFile);
|
2023-05-12 06:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.unsubscribeInternalLinkEvent();
|
2023-06-30 13:51:27 +00:00
|
|
|
this.unsubscribeStar();
|
2023-07-22 07:54:25 +00:00
|
|
|
this.unsubscribeUnmark();
|
2023-08-10 10:05:01 +00:00
|
|
|
this.unsubscribeShare();
|
2024-01-05 03:49:34 +00:00
|
|
|
this.unsubscribeFreezeDocument();
|
|
|
|
this.unsubscribeUnfreeze();
|
|
|
|
this.unsubscribeNewNotification();
|
2024-01-06 06:17:43 +00:00
|
|
|
this.unsubscribeCreateSdocFile();
|
2024-03-01 07:22:47 +00:00
|
|
|
this.unsubscribeClearNotification();
|
2023-05-12 06:12:28 +00:00
|
|
|
}
|
|
|
|
|
2023-12-28 07:29:35 +00:00
|
|
|
onInternalLinkToggle = (options) => {
|
|
|
|
if (options && options.internalLink) {
|
2024-03-01 10:15:01 +00:00
|
|
|
this.setState({ internalLink: options.internalLink });
|
2023-12-25 08:06:36 +00:00
|
|
|
}
|
2024-03-01 10:15:01 +00:00
|
|
|
this.setState({ isShowInternalLinkDialog: !this.state.isShowInternalLinkDialog });
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-05-12 06:12:28 +00:00
|
|
|
|
2023-06-30 13:51:27 +00:00
|
|
|
toggleStar = () => {
|
2024-03-01 10:15:01 +00:00
|
|
|
const { isStarred, repoID, docPath } = this.props;
|
2023-06-30 13:51:27 +00:00
|
|
|
if (isStarred) {
|
|
|
|
seafileAPI.unstarItem(repoID, docPath).then((res) => {
|
|
|
|
this.props.toggleStar(false);
|
|
|
|
}).catch((error) => {
|
|
|
|
const errorMsg = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errorMsg);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
seafileAPI.starItem(repoID, docPath).then((res) => {
|
|
|
|
this.props.toggleStar(true);
|
|
|
|
}).catch((error) => {
|
|
|
|
const errorMsg = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errorMsg);
|
|
|
|
});
|
|
|
|
}
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-06-30 13:51:27 +00:00
|
|
|
|
2023-08-10 10:05:01 +00:00
|
|
|
onShareToggle = () => {
|
2024-03-01 10:15:01 +00:00
|
|
|
this.setState({ isShowShareDialog: !this.state.isShowShareDialog });
|
2023-09-13 00:40:50 +00:00
|
|
|
};
|
2023-08-10 10:05:01 +00:00
|
|
|
|
2023-12-12 23:56:54 +00:00
|
|
|
onFreezeDocument = () => {
|
|
|
|
const { repoID, docPath } = this.props;
|
|
|
|
seafileAPI.lockfile(repoID, docPath, -1).then((res) => {
|
|
|
|
const eventBus = EventBus.getInstance();
|
|
|
|
eventBus.dispatch(EXTERNAL_EVENT.REFRESH_DOCUMENT);
|
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
unFreeze = () => {
|
|
|
|
const { repoID, docPath } = this.props;
|
|
|
|
seafileAPI.unlockfile(repoID, docPath).then((res) => {
|
|
|
|
const eventBus = EventBus.getInstance();
|
|
|
|
eventBus.dispatch(EXTERNAL_EVENT.REFRESH_DOCUMENT);
|
|
|
|
}).catch(error => {
|
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-01-05 03:49:34 +00:00
|
|
|
onNewNotification = () => {
|
|
|
|
this.props.onNewNotification();
|
|
|
|
};
|
|
|
|
|
2024-03-01 10:15:01 +00:00
|
|
|
onClearNotification = () => {
|
|
|
|
this.props.onClearNotification();
|
2024-03-01 07:22:47 +00:00
|
|
|
};
|
|
|
|
|
2024-01-08 02:55:34 +00:00
|
|
|
onCreateSdocFile = (params) => {
|
|
|
|
if (params?.newFileName) {
|
2024-03-01 10:15:01 +00:00
|
|
|
this.setState({ fileType: `${params.newFileName}.sdoc` });
|
2024-01-08 02:55:34 +00:00
|
|
|
}
|
2024-01-06 06:17:43 +00:00
|
|
|
this.setState({
|
|
|
|
isShowCreateFileDialog: !this.state.isShowCreateFileDialog
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
checkDuplicatedName = (newName) => {
|
|
|
|
let direntList = this.props.direntList;
|
|
|
|
let isDuplicated = direntList.some(object => {
|
|
|
|
return object.name === newName;
|
|
|
|
});
|
|
|
|
return isDuplicated;
|
|
|
|
};
|
|
|
|
|
2024-03-25 09:22:01 +00:00
|
|
|
onAddFile = (filePath) => {
|
2024-01-06 06:17:43 +00:00
|
|
|
let repoID = this.props.repoID;
|
2024-03-25 09:22:01 +00:00
|
|
|
seafileAPI.createFile(repoID, filePath).then((res) => {
|
2024-01-09 07:56:20 +00:00
|
|
|
const eventBus = EventBus.getInstance();
|
2024-03-01 10:15:01 +00:00
|
|
|
eventBus.dispatch(EXTERNAL_EVENT.INSERT_LINK, { data: res.data });
|
2024-01-09 07:56:20 +00:00
|
|
|
}).catch((error) => {
|
2024-01-06 06:17:43 +00:00
|
|
|
let errMessage = Utils.getErrorMsg(error);
|
|
|
|
toaster.danger(errMessage);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-05-12 06:12:28 +00:00
|
|
|
render() {
|
2024-01-06 06:17:43 +00:00
|
|
|
const { repoID, docPath, docName, docPerm, dirPath } = this.props;
|
2024-01-08 02:55:34 +00:00
|
|
|
const { isShowInternalLinkDialog, isShowShareDialog, internalLink, isShowCreateFileDialog, fileType } = this.state;
|
2023-05-12 06:12:28 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{isShowInternalLinkDialog && (
|
|
|
|
<InternalLinkDialog
|
|
|
|
repoID={repoID}
|
|
|
|
path={docPath}
|
2023-12-25 08:06:36 +00:00
|
|
|
internalLink={internalLink}
|
2023-05-12 06:12:28 +00:00
|
|
|
onInternalLinkDialogToggle={this.onInternalLinkToggle}
|
|
|
|
/>
|
|
|
|
)}
|
2023-08-10 10:05:01 +00:00
|
|
|
{isShowShareDialog && (
|
|
|
|
<ShareDialog
|
|
|
|
itemType={'file'}
|
|
|
|
itemPath={docPath}
|
|
|
|
itemName={docName}
|
|
|
|
repoID={repoID}
|
|
|
|
userPerm={docPerm}
|
|
|
|
toggleDialog={this.onShareToggle}
|
|
|
|
/>
|
|
|
|
)}
|
2024-01-06 06:17:43 +00:00
|
|
|
{isShowCreateFileDialog && (
|
|
|
|
<CreateFile
|
|
|
|
parentPath={dirPath}
|
2024-01-08 02:55:34 +00:00
|
|
|
fileType={fileType}
|
2024-01-06 06:17:43 +00:00
|
|
|
onAddFile={this.onAddFile}
|
|
|
|
checkDuplicatedName={this.checkDuplicatedName}
|
|
|
|
toggleDialog={this.onCreateSdocFile}
|
|
|
|
/>
|
|
|
|
)}
|
2023-05-12 06:12:28 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExternalOperations.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default ExternalOperations;
|