-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathindex.js
392 lines (357 loc) · 11.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import React, { Component } from 'react'
import { ipcRenderer } from 'electron'
import { connect } from 'react-redux'
import cx from 'classnames'
import pathModule from 'path'
import SplitPane from 'react-split-pane'
import FaFolder from 'react-icons/fa/folder'
import IconClose from 'react-icons/md/close'
import IconEdit from 'react-icons/md/mode-edit'
import { openModal } from 'reducers/modals'
import { readDir, sortFiles, fsRename } from 'helpers/fs'
import { setPreview } from 'actions/preview'
import { updateSettings } from 'actions/settings'
import FileEditor from 'components/FileEditor'
import FilePreview from './FilePreview'
import OldSyntaxDetected from './OldSyntaxDetected'
import './styles.scss'
function renameFile(path, oldName, newName, files) {
if (oldName === newName) {
return
}
const filesWithoutOld = files.filter(f => f.name !== oldName)
const fileExists = filesWithoutOld.some(f => f.name === newName)
if (fileExists) {
throw new Error('File already exists')
}
const oldFullName = pathModule.join(path, oldName)
const newFullName = pathModule.join(path, newName)
return fsRename(oldFullName, newFullName)
}
@connect(
state => ({
previewSize: state.settings.get('previewSize'),
}),
{
setPreview,
openModal,
updateSettings,
},
null,
{ withRef: true },
)
class FilesList extends Component {
state = {
isAdding: false,
files: [],
isDragging: false,
renamedFile: null,
newName: '',
isOldSyntaxDetected: false,
}
componentWillMount() {
this._hasFocused = false
}
componentDidMount() {
this.refresh()
ipcRenderer.on('browser-window-focus', this.refresh)
}
componentDidUpdate(prevProps, prevState) {
if (prevProps.path !== this.props.path) {
this.refresh()
}
if (!prevState.isAdding && this.state.isAdding) {
this._inputName.focus()
}
if (this.state.files.length > prevState.files.length && this._lastCreated) {
this._refs[this._lastCreated].focus()
}
if (!prevState.renamedFile && this.state.renamedFile) {
this._renameInput.select()
}
}
componentWillUnmount() {
this._unmounted = true
ipcRenderer.removeListener('browser-window-focus', this.refresh)
}
handleDetectOldSyntax = () => this.setState({ isOldSyntaxDetected: true })
handleSubmit = e => {
e.preventDefault()
const { path, onAddFile, onActiveFileChange } = this.props
let name = this._inputName.value
if (!name) {
return
}
name = `${name}.mjml`
const fileName = pathModule.join(path, name)
onAddFile(fileName)
onActiveFileChange({
isFolder: false,
name,
})
this._lastCreated = name
this.toggleAdding()
}
handleClickFactory = f => () => {
const p = pathModule.join(this.props.path, f.name)
if (f.isFolder) {
return this.props.onPathChange(p)
}
if (this.props.onFileClick) {
this.props.onFileClick(p)
}
// preview will be set by the onChange on editor
// no need to trigger it here
if (!p.endsWith('.mjml')) {
this.props.setPreview(p)
}
this.props.onActiveFileChange(f)
}
handleClickDirectFactory = (items, i) => () => {
const sub = items.slice(0, i + 1)
const relativePath = sub.join(pathModule.sep)
const path = pathModule.join(this.props.rootPath, relativePath)
this.props.onPathChange(path)
}
handleRemoveFileFactory = f => e => {
if (e) {
e.preventDefault()
}
const p = pathModule.join(this.props.path, f.name)
this.props.onRemoveFile(p)
}
handleNavigateUp = () => {
this.props.onPathChange(pathModule.dirname(this.props.path))
}
handlePreviewPanelStopDrag = size => {
this.setCurrentSize(size)
this.stopDrag()
}
handleChangeNewName = e => {
this.setState({ newName: e.target.value })
}
handleCancelRename = () =>
this.setState({
renamedFile: null,
newName: '',
})
handleRenameInputKeyDown = async e => {
switch (e.which) { // eslint-disable-line
case 27:
this.handleCancelRename()
break
case 13:
try {
const { path } = this.props
const { newName, renamedFile, files } = this.state
await renameFile(path, renamedFile.name, newName, files)
const newFile = { name: newName, isFolder: false }
const newFiles = files.map(f => {
if (f === renamedFile) {
return newFile
}
return f
})
sortFiles(newFiles)
this.setState({ files: newFiles })
this.props.onActiveFileChange(newFile)
this.handleCancelRename()
} catch (e) {} // eslint-disable-line
break
}
}
handleMigrate = () => {
this._editor.migrateToMJML4()
this.setState({ isOldSyntaxDetected: false })
}
setCurrentSize = size => {
this.props.updateSettings(settings => {
return settings.setIn(['previewSize', 'current'], size)
})
}
refsFactory = () => {
this._refs = {}
return refName => node => {
this._refs[refName] = node
}
}
refresh = () => {
const { path } = this.props
readDir(path).then(files => {
if (this._unmounted) {
return
}
sortFiles(files)
this.setState({
files,
})
window.requestIdleCallback(() => {
if (files.length && !this._hasFocused) {
const indexOfIndexFile = files.findIndex(f => f.name === 'index.mjml')
const indexOfFirstMJMLFile = files.findIndex(f => f.name.endsWith('.mjml'))
const activeIndex =
indexOfIndexFile > -1
? indexOfIndexFile
: indexOfFirstMJMLFile > -1 ? indexOfFirstMJMLFile : 0
this.props.onActiveFileChange(files[activeIndex])
this._hasFocused = true
}
})
})
}
startDrag = () => this.setState({ isDragging: true })
stopDrag = () => {
this.setState({ isDragging: false })
if (!this._editor) {
return
}
this._editor.refresh()
this._editor.focus()
}
toggleAdding = e => {
if (e) {
e.preventDefault()
}
this.setState(s => ({ isAdding: !s.isAdding }))
}
cancelAdd = e => {
if (e) {
e.preventDefault()
}
this.setState(
s => ({ isAdding: !s.isAdding }),
() => {
this._addBtn.focus()
},
)
}
render() {
const { files, isDragging, renamedFile, newName, isOldSyntaxDetected } = this.state
const { onRef, onEditorRef, activeFile, path, rootPath, openModal, previewSize } = this.props
onRef(this)
const setRef = this.refsFactory()
const rootPathItems = rootPath.split(pathModule.sep)
const pathItems = path.split(pathModule.sep).slice(rootPathItems.length)
const fullActiveFile = pathModule.join(path, (activeFile && activeFile.name) || '')
return (
<div className="fg-1 d-f fd-c">
<div className="rel fg-1">
<SplitPane
split="vertical"
defaultSize={180}
minSize={2}
maxSize={250}
onDragStarted={this.startDrag}
onDragFinished={this.stopDrag}
>
<div className="sticky o-y-a bg-dark">
<div className="rel FilesList--list anim-enter-fade-left">
{!!pathItems.length && (
<button
className="FilesList--file d-f ai-c"
tabIndex={0}
onClick={this.handleNavigateUp}
>
<div className="fg-1 FilesList--item-name-container">
<div className="FilesList--item-name">{'..'}</div>
</div>
</button>
)}
{files.map(
f =>
renamedFile === f ? (
<div key={f.name} className="FilesList--file renaming active">
<input
ref={n => (this._renameInput = n)}
autoFocus
type="text"
value={newName}
onKeyDown={this.handleRenameInputKeyDown}
onChange={this.handleChangeNewName}
onBlur={this.handleCancelRename}
/>
</div>
) : (
<button
ref={setRef(f.name)}
key={f.name}
className={cx('FilesList--file d-f ai-c', {
active: activeFile && activeFile.name === f.name,
})}
tabIndex={0}
onClick={this.handleClickFactory(f)}
>
{f.isFolder && (
<div className="fs-0 pr-10">
<FaFolder />
</div>
)}
<div className="fg-1 FilesList--item-name-container">
<div className="FilesList--item-name">{f.name}</div>
</div>
<div className="FilesList--item-actions">
<div
tabIndex={0}
onClick={() =>
this.setState({
renamedFile: f,
newName: f.name,
})
}
className="action action-rename"
>
<IconEdit />
</div>
<div
tabIndex={0}
onClick={() => openModal('removeFile', f)}
className="action action-remove"
>
<IconClose />
</div>
</div>
</button>
),
)}
</div>
</div>
<SplitPane
ref={n => (this._previewSplitPane = n)}
split="vertical"
size={previewSize.get('current')}
maxSize={previewSize.get('desktop')}
minSize={previewSize.get('mobile')}
primary="second"
onDragStarted={this.startDrag}
onDragFinished={this.handlePreviewPanelStopDrag}
>
<div className="d-f fd-c sticky anim-enter-fade">
{isOldSyntaxDetected && <OldSyntaxDetected onMigrate={this.handleMigrate} />}
{activeFile &&
activeFile.name.endsWith('.mjml') && (
<FileEditor
onRef={n => {
this._editor = n
onEditorRef(n)
}}
fileName={fullActiveFile}
disablePointer={isDragging}
onDetectOldSyntax={this.handleDetectOldSyntax}
/>
)}
</div>
<div className="sticky fs-0 ml-5 rel FilesList--preview-container">
<FilePreview
disablePointer={isDragging}
onSetSize={this.setCurrentSize}
iframeBase={path}
/>
</div>
</SplitPane>
</SplitPane>
</div>
</div>
)
}
}
export default FilesList