|
@@ -0,0 +1,401 @@
|
|
1
|
+import React from 'react'
|
|
2
|
+import { translate } from 'react-i18next'
|
|
3
|
+import i18n from '../i18n.js'
|
|
4
|
+import {
|
|
5
|
+ addAllResourceI18n,
|
|
6
|
+ handleFetchResult,
|
|
7
|
+ PopinFixed,
|
|
8
|
+ PopinFixedHeader,
|
|
9
|
+ PopinFixedOption,
|
|
10
|
+ PopinFixedContent,
|
|
11
|
+ Timeline,
|
|
12
|
+ NewVersionBtn,
|
|
13
|
+ ArchiveDeleteContent,
|
|
14
|
+ SelectStatus
|
|
15
|
+} from 'tracim_frontend_lib'
|
|
16
|
+import { MODE, debug } from '../helper.js'
|
|
17
|
+import {
|
|
18
|
+ getHtmlDocContent,
|
|
19
|
+ getHtmlDocComment,
|
|
20
|
+ getHtmlDocRevision,
|
|
21
|
+ postHtmlDocNewComment,
|
|
22
|
+ putHtmlDocContent,
|
|
23
|
+ putHtmlDocStatus
|
|
24
|
+} from '../action.async.js'
|
|
25
|
+
|
|
26
|
+class Workspace extends React.Component {
|
|
27
|
+ constructor (props) {
|
|
28
|
+ super(props)
|
|
29
|
+ this.state = {
|
|
30
|
+ appName: 'html-document',
|
|
31
|
+ isVisible: true,
|
|
32
|
+ config: props.data ? props.data.config : debug.config,
|
|
33
|
+ loggedUser: props.data ? props.data.loggedUser : debug.loggedUser,
|
|
34
|
+ content: props.data ? props.data.content : debug.content,
|
|
35
|
+ rawContentBeforeEdit: '',
|
|
36
|
+ timeline: props.data ? [] : [], // debug.timeline,
|
|
37
|
+ newComment: '',
|
|
38
|
+ timelineWysiwyg: false,
|
|
39
|
+ mode: MODE.VIEW
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ // i18n has been init, add resources from frontend
|
|
43
|
+ addAllResourceI18n(i18n, this.state.config.translation)
|
|
44
|
+ i18n.changeLanguage(this.state.loggedUser.lang)
|
|
45
|
+
|
|
46
|
+ document.addEventListener('appCustomEvent', this.customEventReducer)
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ customEventReducer = ({ detail: { type, data } }) => { // action: { type: '', data: {} }
|
|
50
|
+ switch (type) {
|
|
51
|
+ case 'html-document_showApp':
|
|
52
|
+ console.log('%c<Workspace> Custom event', 'color: #28a745', type, data)
|
|
53
|
+ this.setState({isVisible: true})
|
|
54
|
+ break
|
|
55
|
+ case 'html-document_hideApp':
|
|
56
|
+ console.log('%c<Workspace> Custom event', 'color: #28a745', type, data)
|
|
57
|
+ this.setState({isVisible: false})
|
|
58
|
+ break
|
|
59
|
+ case 'html-document_reloadContent':
|
|
60
|
+ console.log('%c<Workspace> Custom event', 'color: #28a745', type, data)
|
|
61
|
+ this.setState(prev => ({content: {...prev.content, ...data}, isVisible: true}))
|
|
62
|
+ break
|
|
63
|
+ case 'allApp_changeLang':
|
|
64
|
+ console.log('%c<Workspace> Custom event', 'color: #28a745', type, data)
|
|
65
|
+ this.setState(prev => ({
|
|
66
|
+ loggedUser: {
|
|
67
|
+ ...prev.loggedUser,
|
|
68
|
+ lang: data
|
|
69
|
+ }
|
|
70
|
+ }))
|
|
71
|
+ i18n.changeLanguage(data)
|
|
72
|
+ break
|
|
73
|
+ }
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ componentDidMount () {
|
|
77
|
+ console.log('%c<Workspace> did mount', `color: ${this.state.config.hexcolor}`)
|
|
78
|
+
|
|
79
|
+ this.loadContent()
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ componentDidUpdate (prevProps, prevState) {
|
|
83
|
+ const { state } = this
|
|
84
|
+
|
|
85
|
+ console.log('%c<Workspace> did update', `color: ${this.state.config.hexcolor}`, prevState, state)
|
|
86
|
+
|
|
87
|
+ if (!prevState.content || !state.content) return
|
|
88
|
+
|
|
89
|
+ if (prevState.content.content_id !== state.content.content_id) this.loadContent()
|
|
90
|
+
|
|
91
|
+ if (state.mode === MODE.EDIT && prevState.mode !== state.mode) {
|
|
92
|
+ tinymce.remove('#wysiwygNewVersion')
|
|
93
|
+ wysiwyg('#wysiwygNewVersion', this.handleChangeText)
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ if (!prevState.timelineWysiwyg && state.timelineWysiwyg) wysiwyg('#wysiwygTimelineComment', this.handleChangeNewComment)
|
|
97
|
+ else if (prevState.timelineWysiwyg && !state.timelineWysiwyg) tinymce.remove('#wysiwygTimelineComment')
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ componentWillUnmount () {
|
|
101
|
+ console.log('%c<Workspace> will Unmount', `color: ${this.state.config.hexcolor}`)
|
|
102
|
+ document.removeEventListener('appCustomEvent', this.customEventReducer)
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ loadContent = async () => {
|
|
106
|
+ const { loggedUser, content, config } = this.state
|
|
107
|
+
|
|
108
|
+ const fetchResultHtmlDocument = getHtmlDocContent(loggedUser, config.apiUrl, content.workspace_id, content.content_id)
|
|
109
|
+ const fetchResultComment = getHtmlDocComment(loggedUser, config.apiUrl, content.workspace_id, content.content_id)
|
|
110
|
+ const fetchResultRevision = getHtmlDocRevision(loggedUser, config.apiUrl, content.workspace_id, content.content_id)
|
|
111
|
+
|
|
112
|
+ handleFetchResult(await fetchResultHtmlDocument)
|
|
113
|
+ .then(resHtmlDocument => this.setState({content: resHtmlDocument.body}))
|
|
114
|
+ .catch(e => console.log('Error loading content.', e))
|
|
115
|
+
|
|
116
|
+ Promise.all([
|
|
117
|
+ handleFetchResult(await fetchResultComment),
|
|
118
|
+ handleFetchResult(await fetchResultRevision)
|
|
119
|
+ ])
|
|
120
|
+ .then(([resComment, resRevision]) => {
|
|
121
|
+ const resCommentWithProperDate = resComment.body.map(c => ({...c, created: (new Date(c.created)).toLocaleString()}))
|
|
122
|
+
|
|
123
|
+ const revisionWithComment = resRevision.body
|
|
124
|
+ .map((r, i) => ({
|
|
125
|
+ ...r,
|
|
126
|
+ created: (new Date(r.created)).toLocaleString(),
|
|
127
|
+ timelineType: 'revision',
|
|
128
|
+ commentList: r.comment_ids.map(ci => ({
|
|
129
|
+ timelineType: 'comment',
|
|
130
|
+ ...resCommentWithProperDate.find(c => c.content_id === ci)
|
|
131
|
+ })),
|
|
132
|
+ number: i + 1
|
|
133
|
+ }))
|
|
134
|
+ .reduce((acc, rev) => [
|
|
135
|
+ ...acc,
|
|
136
|
+ rev,
|
|
137
|
+ ...rev.commentList.map(comment => ({
|
|
138
|
+ ...comment,
|
|
139
|
+ customClass: '',
|
|
140
|
+ loggedUser: this.state.config.loggedUser
|
|
141
|
+ }))
|
|
142
|
+ ], [])
|
|
143
|
+
|
|
144
|
+ this.setState({
|
|
145
|
+ timeline: revisionWithComment,
|
|
146
|
+ mode: resRevision.body.length === 1 ? MODE.EDIT : MODE.VIEW // first time editing the doc, open in edit mode
|
|
147
|
+ })
|
|
148
|
+ })
|
|
149
|
+ .catch(e => {
|
|
150
|
+ console.log('Error loading Timeline.', e)
|
|
151
|
+ this.setState({timeline: []})
|
|
152
|
+ })
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ handleClickBtnCloseApp = () => {
|
|
156
|
+ this.setState({ isVisible: false })
|
|
157
|
+ GLOBAL_dispatchEvent({type: 'appClosed', data: {}}) // handled by tracim_front::src/container/WorkspaceContent.jsx
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ handleSaveEditTitle = async newTitle => {
|
|
161
|
+ const { loggedUser, config, content } = this.state
|
|
162
|
+
|
|
163
|
+ const fetchResultSaveHtmlDoc = putHtmlDocContent(loggedUser, config.apiUrl, content.workspace_id, content.content_id, newTitle, content.raw_content)
|
|
164
|
+
|
|
165
|
+ handleFetchResult(await fetchResultSaveHtmlDoc)
|
|
166
|
+ .then(resSave => {
|
|
167
|
+ if (resSave.apiResponse.status === 200) {
|
|
168
|
+ this.loadContent()
|
|
169
|
+ GLOBAL_dispatchEvent({ type: 'refreshContentList', data: {} })
|
|
170
|
+ } else {
|
|
171
|
+ console.warn('Error saving html-document. Result:', resSave, 'content:', content, 'config:', config)
|
|
172
|
+ }
|
|
173
|
+ })
|
|
174
|
+ }
|
|
175
|
+
|
|
176
|
+ handleClickNewVersion = () => this.setState(prev => ({
|
|
177
|
+ rawContentBeforeEdit: prev.content.raw_content,
|
|
178
|
+ mode: MODE.EDIT
|
|
179
|
+ }))
|
|
180
|
+
|
|
181
|
+ handleCloseNewVersion = () => {
|
|
182
|
+ tinymce.remove('#wysiwygNewVersion')
|
|
183
|
+ this.setState(prev => ({
|
|
184
|
+ content: {
|
|
185
|
+ ...prev.content,
|
|
186
|
+ raw_content: prev.rawContentBeforeEdit
|
|
187
|
+ },
|
|
188
|
+ mode: MODE.VIEW
|
|
189
|
+ }))
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ handleSaveHtmlDocument = async () => {
|
|
193
|
+ const { loggedUser, content, config } = this.state
|
|
194
|
+
|
|
195
|
+ const fetchResultSaveHtmlDoc = putHtmlDocContent(loggedUser, config.apiUrl, content.workspace_id, content.content_id, content.label, content.raw_content)
|
|
196
|
+
|
|
197
|
+ handleFetchResult(await fetchResultSaveHtmlDoc)
|
|
198
|
+ .then(resSave => {
|
|
199
|
+ if (resSave.apiResponse.status === 200) {
|
|
200
|
+ this.handleCloseNewVersion()
|
|
201
|
+ this.loadContent()
|
|
202
|
+ } else {
|
|
203
|
+ console.warn('Error saving html-document. Result:', resSave, 'content:', content, 'config:', config)
|
|
204
|
+ }
|
|
205
|
+ })
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ handleChangeText = e => {
|
|
209
|
+ const newText = e.target.value // because SyntheticEvent is pooled (react specificity)
|
|
210
|
+ this.setState(prev => ({content: {...prev.content, raw_content: newText}}))
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ handleChangeNewComment = e => {
|
|
214
|
+ const newComment = e.target.value
|
|
215
|
+ this.setState({newComment})
|
|
216
|
+ }
|
|
217
|
+
|
|
218
|
+ handleClickValidateNewCommentBtn = async () => {
|
|
219
|
+ const { loggedUser, config, content, newComment } = this.state
|
|
220
|
+
|
|
221
|
+ const fetchResultSaveNewComment = await postHtmlDocNewComment(loggedUser, config.apiUrl, content.workspace_id, content.content_id, newComment)
|
|
222
|
+
|
|
223
|
+ handleFetchResult(await fetchResultSaveNewComment)
|
|
224
|
+ .then(resSave => {
|
|
225
|
+ if (resSave.apiResponse.status === 200) {
|
|
226
|
+ this.setState({newComment: ''})
|
|
227
|
+ if (this.state.timelineWysiwyg) tinymce.get('wysiwygTimelineComment').setContent('')
|
|
228
|
+ this.loadContent()
|
|
229
|
+ } else {
|
|
230
|
+ console.warn('Error saving html-document comment. Result:', resSave, 'content:', content, 'config:', config)
|
|
231
|
+ }
|
|
232
|
+ })
|
|
233
|
+ }
|
|
234
|
+
|
|
235
|
+ handleToggleWysiwyg = () => this.setState(prev => ({timelineWysiwyg: !prev.timelineWysiwyg}))
|
|
236
|
+
|
|
237
|
+ handleChangeStatus = async newStatus => {
|
|
238
|
+ const { loggedUser, config, content } = this.state
|
|
239
|
+
|
|
240
|
+ const fetchResultSaveEditStatus = putHtmlDocStatus(loggedUser, config.apiUrl, content.workspace_id, content.content_id, newStatus)
|
|
241
|
+
|
|
242
|
+ handleFetchResult(await fetchResultSaveEditStatus)
|
|
243
|
+ .then(resSave => {
|
|
244
|
+ if (resSave.status !== 204) { // 204 no content so dont take status from resSave.apiResponse.status
|
|
245
|
+ console.warn('Error saving html-document comment. Result:', resSave, 'content:', content, 'config:', config)
|
|
246
|
+ } else {
|
|
247
|
+ this.loadContent()
|
|
248
|
+ }
|
|
249
|
+ })
|
|
250
|
+ }
|
|
251
|
+
|
|
252
|
+ handleClickArchive = async () => {
|
|
253
|
+ console.log('archive')
|
|
254
|
+ // const { config, content } = this.state
|
|
255
|
+ //
|
|
256
|
+ // const fetchResultArchive = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/contents/${content.content_id}/archive`, {
|
|
257
|
+ // ...FETCH_CONFIG,
|
|
258
|
+ // method: 'PUT'
|
|
259
|
+ // })
|
|
260
|
+ }
|
|
261
|
+
|
|
262
|
+ handleClickDelete = async () => {
|
|
263
|
+ console.log('delete')
|
|
264
|
+ // const { config, content } = this.state
|
|
265
|
+ // const fetchResultDelete = await fetch(`${config.apiUrl}/workspaces/${content.workspace_id}/contents/${content.content_id}/delete`, {
|
|
266
|
+ // ...FETCH_CONFIG,
|
|
267
|
+ // method: 'PUT'
|
|
268
|
+ // })
|
|
269
|
+ }
|
|
270
|
+
|
|
271
|
+ handleClickShowRevision = revision => {
|
|
272
|
+ const { mode, timeline } = this.state
|
|
273
|
+
|
|
274
|
+ const revisionArray = timeline.filter(t => t.timelineType === 'revision')
|
|
275
|
+ const isLastRevision = revision.revision_id === revisionArray[revisionArray.length - 1].revision_id
|
|
276
|
+
|
|
277
|
+ if (mode === MODE.REVISION && isLastRevision) {
|
|
278
|
+ this.handleClickLastVersion()
|
|
279
|
+ return
|
|
280
|
+ }
|
|
281
|
+
|
|
282
|
+ if (mode === MODE.VIEW && isLastRevision) return
|
|
283
|
+
|
|
284
|
+ this.setState(prev => ({
|
|
285
|
+ content: {
|
|
286
|
+ ...prev.content,
|
|
287
|
+ label: revision.label,
|
|
288
|
+ raw_content: revision.raw_content,
|
|
289
|
+ number: revision.number,
|
|
290
|
+ status: revision.status
|
|
291
|
+ },
|
|
292
|
+ mode: MODE.REVISION
|
|
293
|
+ }))
|
|
294
|
+ }
|
|
295
|
+
|
|
296
|
+ handleClickLastVersion = () => {
|
|
297
|
+ this.loadContent()
|
|
298
|
+ this.setState({mode: MODE.VIEW})
|
|
299
|
+ }
|
|
300
|
+
|
|
301
|
+ render () {
|
|
302
|
+ const { isVisible, loggedUser, content, timeline, newComment, timelineWysiwyg, config, mode } = this.state
|
|
303
|
+ const { t } = this.props
|
|
304
|
+
|
|
305
|
+ if (!isVisible) return null
|
|
306
|
+
|
|
307
|
+ return (
|
|
308
|
+ <PopinFixed
|
|
309
|
+ customClass={`${config.slug}`}
|
|
310
|
+ customColor={config.hexcolor}
|
|
311
|
+ >
|
|
312
|
+ <PopinFixedHeader
|
|
313
|
+ customClass={`${config.slug}`}
|
|
314
|
+ customColor={config.hexcolor}
|
|
315
|
+ faIcon={config.faIcon}
|
|
316
|
+ title={content.label}
|
|
317
|
+ onClickCloseBtn={this.handleClickBtnCloseApp}
|
|
318
|
+ onValidateChangeTitle={this.handleSaveEditTitle}
|
|
319
|
+ />
|
|
320
|
+
|
|
321
|
+ <PopinFixedOption
|
|
322
|
+ customColor={config.hexcolor}
|
|
323
|
+ customClass={`${config.slug}`}
|
|
324
|
+ i18n={i18n}
|
|
325
|
+ >
|
|
326
|
+ <div /* this div in display flex, justify-content space-between */>
|
|
327
|
+ <div className='d-flex'>
|
|
328
|
+ <NewVersionBtn
|
|
329
|
+ customColor={config.hexcolor}
|
|
330
|
+ onClickNewVersionBtn={this.handleClickNewVersion}
|
|
331
|
+ disabled={mode !== MODE.VIEW}
|
|
332
|
+ />
|
|
333
|
+
|
|
334
|
+ {mode === MODE.REVISION &&
|
|
335
|
+ <button
|
|
336
|
+ className='wsContentGeneric__option__menu__lastversion html-document__lastversionbtn btn'
|
|
337
|
+ onClick={this.handleClickLastVersion}
|
|
338
|
+ style={{backgroundColor: config.hexcolor, color: '#fdfdfd'}}
|
|
339
|
+ >
|
|
340
|
+ <i className='fa fa-code-fork' />
|
|
341
|
+ {t('Last version')}
|
|
342
|
+ </button>
|
|
343
|
+ }
|
|
344
|
+ </div>
|
|
345
|
+
|
|
346
|
+ <div className='d-flex'>
|
|
347
|
+ <SelectStatus
|
|
348
|
+ selectedStatus={config.availableStatuses.find(s => s.slug === content.status)}
|
|
349
|
+ availableStatus={config.availableStatuses}
|
|
350
|
+ onChangeStatus={this.handleChangeStatus}
|
|
351
|
+ disabled={mode === MODE.REVISION}
|
|
352
|
+ />
|
|
353
|
+
|
|
354
|
+ <ArchiveDeleteContent
|
|
355
|
+ customColor={config.hexcolor}
|
|
356
|
+ onClickArchiveBtn={this.handleClickArchive}
|
|
357
|
+ onClickDeleteBtn={this.handleClickDelete}
|
|
358
|
+ disabled={mode === MODE.REVISION}
|
|
359
|
+ />
|
|
360
|
+ </div>
|
|
361
|
+ </div>
|
|
362
|
+ </PopinFixedOption>
|
|
363
|
+
|
|
364
|
+ <PopinFixedContent
|
|
365
|
+ customClass={`${config.slug}__contentpage`}
|
|
366
|
+ showRightPartOnLoad={mode === MODE.VIEW}
|
|
367
|
+ >
|
|
368
|
+ <HtmlDocumentComponent
|
|
369
|
+ mode={mode}
|
|
370
|
+ customColor={config.hexcolor}
|
|
371
|
+ wysiwygNewVersion={'wysiwygNewVersion'}
|
|
372
|
+ onClickCloseEditMode={this.handleCloseNewVersion}
|
|
373
|
+ onClickValidateBtn={this.handleSaveHtmlDocument}
|
|
374
|
+ version={content.number}
|
|
375
|
+ lastVersion={timeline.filter(t => t.timelineType === 'revision').length}
|
|
376
|
+ text={content.raw_content}
|
|
377
|
+ onChangeText={this.handleChangeText}
|
|
378
|
+ key={'html-document'}
|
|
379
|
+ />
|
|
380
|
+
|
|
381
|
+ <Timeline
|
|
382
|
+ customClass={`${config.slug}__contentpage`}
|
|
383
|
+ customColor={config.hexcolor}
|
|
384
|
+ loggedUser={loggedUser}
|
|
385
|
+ timelineData={timeline}
|
|
386
|
+ newComment={newComment}
|
|
387
|
+ disableComment={mode === MODE.REVISION}
|
|
388
|
+ wysiwyg={timelineWysiwyg}
|
|
389
|
+ onChangeNewComment={this.handleChangeNewComment}
|
|
390
|
+ onClickValidateNewCommentBtn={this.handleClickValidateNewCommentBtn}
|
|
391
|
+ onClickWysiwygBtn={this.handleToggleWysiwyg}
|
|
392
|
+ onClickRevisionBtn={this.handleClickShowRevision}
|
|
393
|
+ shouldScrollToBottom={mode !== MODE.REVISION}
|
|
394
|
+ />
|
|
395
|
+ </PopinFixedContent>
|
|
396
|
+ </PopinFixed>
|
|
397
|
+ )
|
|
398
|
+ }
|
|
399
|
+}
|
|
400
|
+
|
|
401
|
+export default translate()(Workspace)
|