Skip to content

Commit a24e8c6

Browse files
add-external
1 parent 9ba465f commit a24e8c6

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

frontend/src/pages/sdoc/sdoc-editor/external-operations.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import { Utils } from '../../../utils/utils';
66
import toaster from '../../../components/toast';
77
import InternalLinkDialog from '../../../components/dialog/internal-link-dialog';
88
import ShareDialog from '../../../components/dialog/share-dialog';
9+
import CreateFile from '../../../components/dialog/create-file-dialog';
910

1011
const propTypes = {
1112
repoID: PropTypes.string.isRequired,
1213
docPath: PropTypes.string.isRequired,
1314
docName: PropTypes.string.isRequired,
1415
docPerm: PropTypes.string.isRequired,
1516
isStarred: PropTypes.bool.isRequired,
17+
direntList: PropTypes.array.isRequired,
18+
dirPath: PropTypes.string.isRequired,
1619
toggleStar: PropTypes.func.isRequired,
1720
unmarkDraft: PropTypes.func.isRequired
1821
};
@@ -25,6 +28,7 @@ class ExternalOperations extends React.Component {
2528
isShowInternalLinkDialog: false,
2629
isShowShareDialog: false,
2730
internalLink: '',
31+
isShowCreateFileDialog: false,
2832
};
2933
}
3034

@@ -36,13 +40,15 @@ class ExternalOperations extends React.Component {
3640
this.unsubscribeShare = eventBus.subscribe(EXTERNAL_EVENT.SHARE_SDOC, this.onShareToggle);
3741
this.unsubscribeShare = eventBus.subscribe(EXTERNAL_EVENT.FREEZE_DOCUMENT, this.onFreezeDocument);
3842
this.unsubscribeShare = eventBus.subscribe(EXTERNAL_EVENT.UNFREEZE, this.unFreeze);
43+
this.unsubscribeCreateSdocFile = eventBus.subscribe(EXTERNAL_EVENT.CREATE_SDOC_FILE, this.onCreateSdocFile);
3944
}
4045

4146
componentWillUnmount() {
4247
this.unsubscribeInternalLinkEvent();
4348
this.unsubscribeStar();
4449
this.unsubscribeUnmark();
4550
this.unsubscribeShare();
51+
this.unsubscribeCreateSdocFile();
4652
}
4753

4854
onInternalLinkToggle = (options) => {
@@ -107,9 +113,31 @@ class ExternalOperations extends React.Component {
107113
});
108114
};
109115

116+
onCreateSdocFile = () => {
117+
this.setState({
118+
isShowCreateFileDialog: !this.state.isShowCreateFileDialog
119+
});
120+
};
121+
122+
checkDuplicatedName = (newName) => {
123+
let direntList = this.props.direntList;
124+
let isDuplicated = direntList.some(object => {
125+
return object.name === newName;
126+
});
127+
return isDuplicated;
128+
};
129+
130+
onAddFile = (filePath, isMarkdownDraft, isSdocDraft) => {
131+
let repoID = this.props.repoID;
132+
seafileAPI.createFile(repoID, filePath, isMarkdownDraft).catch((error) => {
133+
let errMessage = Utils.getErrorMsg(error);
134+
toaster.danger(errMessage);
135+
});
136+
};
137+
110138
render() {
111-
const { repoID, docPath, docName, docPerm } = this.props;
112-
const { isShowInternalLinkDialog, isShowShareDialog, internalLink } = this.state;
139+
const { repoID, docPath, docName, docPerm, dirPath } = this.props;
140+
const { isShowInternalLinkDialog, isShowShareDialog, internalLink, isShowCreateFileDialog } = this.state;
113141
return (
114142
<>
115143
{isShowInternalLinkDialog && (
@@ -130,6 +158,15 @@ class ExternalOperations extends React.Component {
130158
toggleDialog={this.onShareToggle}
131159
/>
132160
)}
161+
{isShowCreateFileDialog && (
162+
<CreateFile
163+
parentPath={dirPath}
164+
fileType='.sdoc'
165+
onAddFile={this.onAddFile}
166+
checkDuplicatedName={this.checkDuplicatedName}
167+
toggleDialog={this.onCreateSdocFile}
168+
/>
169+
)}
133170
</>
134171
);
135172
}

frontend/src/pages/sdoc/sdoc-editor/index.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React, { Fragment } from 'react';
22
import { SimpleEditor } from '@seafile/sdoc-editor';
33
import ExternalOperations from './external-operations';
4+
import { seafileAPI } from '../../../utils/seafile-api';
5+
import Dirent from '../../../models/dirent';
46
import { Utils } from '../../../utils/utils';
57

68
export default class SdocEditor extends React.Component {
@@ -10,11 +12,13 @@ export default class SdocEditor extends React.Component {
1012
const { isStarred, isSdocDraft } = window.app.pageOptions;
1113
this.state = {
1214
isStarred: isStarred,
13-
isDraft: isSdocDraft
15+
isDraft: isSdocDraft,
16+
direntList: []
1417
};
1518
}
1619

1720
componentDidMount() {
21+
this.getDirentList();
1822
const { docName } = window.seafile;
1923
const fileIcon = Utils.getFileIconUrl(docName, 192);
2024
document.getElementById('favicon').href = fileIcon;
@@ -28,9 +32,36 @@ export default class SdocEditor extends React.Component {
2832
this.setState({isDraft: false});
2933
};
3034

35+
getDirPath = () => {
36+
const { docPath } = window.seafile;
37+
const index = docPath.lastIndexOf('/');
38+
if (index) {
39+
return docPath.slice(0, index);
40+
}
41+
return '/';
42+
};
43+
44+
getDirentList = () => {
45+
const { repoID } = window.seafile;
46+
const path = this.getDirPath();
47+
seafileAPI.listDir(repoID, path, {'with_thumbnail': true}).then(res => {
48+
let direntList = [];
49+
res.data.dirent_list.forEach(item => {
50+
let dirent = new Dirent(item);
51+
direntList.push(dirent);
52+
});
53+
this.setState({
54+
direntList: direntList
55+
});
56+
}).catch((err) => {
57+
Utils.getErrorMsg(err, true);
58+
});
59+
};
60+
3161
render() {
3262
const { repoID, docPath, docName, docPerm } = window.seafile;
33-
const { isStarred, isDraft } = this.state;
63+
const { isStarred, isDraft, direntList } = this.state;
64+
const dirPath = this.getDirPath();
3465
return (
3566
<Fragment>
3667
<SimpleEditor isStarred={isStarred} isDraft={isDraft} />
@@ -40,6 +71,8 @@ export default class SdocEditor extends React.Component {
4071
docName={docName}
4172
docPerm={docPerm}
4273
isStarred={isStarred}
74+
direntList={direntList}
75+
dirPath={dirPath}
4376
toggleStar={this.toggleStar}
4477
unmarkDraft={this.unmarkDraft}
4578
/>

0 commit comments

Comments
 (0)