-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathindex.js
269 lines (231 loc) · 7.89 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
import React, { Component } from 'react'
import pathModule from 'path'
import trash from 'trash'
import { connect } from 'react-redux'
import FaCog from 'react-icons/fa/cog'
import FaFolderOpen from 'react-icons/fa/arrow-up'
import IconCopy from 'react-icons/md/content-copy'
import IconCode from 'react-icons/md/code'
import IconCamera from 'react-icons/md/camera-alt'
import IconEmail from 'react-icons/md/email'
import IconAdd from 'react-icons/md/note-add'
import IconBeautify from 'react-icons/md/autorenew'
import fs from 'fs'
import { shell, clipboard } from 'electron'
import beautifyJS from 'js-beautify'
import defaultMJML from 'data/defaultMJML'
import { openModal } from 'reducers/modals'
import { addAlert } from 'reducers/alerts'
import { setPreview } from 'actions/preview'
import { fileDialog, saveDialog, fsWriteFile } from 'helpers/fs'
import Button from 'components/Button'
import ButtonDropdown from 'components/Button/ButtonDropdown'
import FilesList from 'components/FilesList'
import NotifBtn from 'components/Notifs/NotifBtn'
import BackButton from './BackButton'
import SendModal from './SendModal'
import AddFileModal from './AddFileModal'
import RemoveFileModal from './RemoveFileModal'
import takeScreenshot from 'helpers/takeScreenshot'
@connect(
state => ({
preview: state.preview,
previewSize: state.settings.get('previewSize'),
beautifyOutput: state.settings.getIn(['mjml', 'beautify']),
}),
{
openModal,
addAlert,
setPreview,
},
)
class ProjectPage extends Component {
state = {
path: this.props.location.query.path,
activeFile: null,
}
componentDidMount() {
this._page.focus()
}
componentWillUnmount() {
this.props.setPreview(null)
}
handleBeautify = () => this._editor.beautify()
handlePathChange = path => this.setState({ path, activeFile: null })
handleClickImport = () => {
const p = fileDialog({
defaultPath: this.props.location.query.path,
properties: ['openFile'],
filters: [{ name: 'All Files', extensions: ['mjml'] }],
})
if (!p) {
return
}
fs.readFile(p, { encoding: 'utf8' }, (err, res) => {
if (err) {
return
}
this._content = res
})
}
handleAddFile = fileName => {
fs.writeFile(fileName, defaultMJML, err => {
if (err) {
return
}
this._filelist.refresh()
})
}
handleRemoveFile = async fileName => {
await trash(fileName)
this._filelist.refresh()
this.setState({ activeFile: null })
}
handleOpenInBrowser = () => {
if (process.platform === 'darwin') {
shell.showItemInFolder(this.state.path)
} else {
shell.openItem(this.state.path)
}
}
handleActiveFileChange = activeFile => this.setState({ activeFile })
handleCopyHTML = () => {
const htmlContent = this.getHTMLOutput()
clipboard.writeText(htmlContent)
this.props.addAlert('Copied!', 'success')
}
handleExportToHTML = async () => {
const p = saveDialog({
title: 'Export to HTML file',
defaultPath: this.props.location.query.path,
filters: [{ name: 'All Files', extensions: ['html'] }],
})
if (!p) {
return
}
const { addAlert } = this.props
const htmlContent = this.getHTMLOutput()
await fsWriteFile(p, htmlContent)
addAlert('Successfully exported HTML', 'success')
this._filelist.refresh()
}
handleScreenshot = async () => {
const { preview, previewSize, addAlert, location } = this.props
const filename = pathModule.basename(this.state.activeFile.name, '.mjml')
const [mobileWidth, desktopWidth] = [previewSize.get('mobile'), previewSize.get('desktop')]
const [mobileScreenshot, desktopScreenshot] = await Promise.all([
takeScreenshot(preview.content, mobileWidth),
takeScreenshot(preview.content, desktopWidth),
])
await Promise.all([
fsWriteFile(pathModule.join(location.query.path, `${filename}-mobile.png`), mobileScreenshot),
fsWriteFile(
pathModule.join(location.query.path, `${filename}-desktop.png`),
desktopScreenshot,
),
])
addAlert('Successfully saved mobile and desktop screenshots', 'success')
this._filelist.refresh()
}
openSettingsModal = () => this.props.openModal('settings')
openSendModal = () => this.props.openModal('send')
openAddFileModal = () => this.props.openModal('addFile')
getHTMLOutput() {
const { preview, beautifyOutput } = this.props
return beautifyOutput ? beautifyJS.html(preview.content) : preview.content
}
render() {
const { preview } = this.props
const { path, activeFile } = this.state
const rootPath = this.props.location.query.path
const projectName = pathModule.basename(rootPath)
const isMJMLFile = activeFile && activeFile.name.endsWith('.mjml')
return (
<div className="fg-1 d-f fd-c o-n" tabIndex={0} ref={n => (this._page = n)}>
<div className="d-f p-10 r" style={{ zIndex: 2 }}>
<div className="fg-1 flow-h-10">
<BackButton projectName={projectName} />
<Button ghost onClick={this.openAddFileModal}>
<IconAdd className="mr-5" />
{'New file'}
</Button>
</div>
<div className="d-f flow-h-10">
{isMJMLFile && [
<Button key="beautify" transparent onClick={this.handleBeautify}>
<IconBeautify style={{ marginRight: 5 }} />
{'Beautify'}
</Button>,
]}
<Button transparent onClick={this.handleOpenInBrowser}>
<FaFolderOpen style={{ marginRight: 5 }} />
{'Open'}
</Button>
{preview &&
preview.type === 'html' && [
<Button key={'send'} transparent onClick={this.openSendModal}>
<IconEmail style={{ marginRight: 5 }} />
{'Send'}
</Button>,
<ButtonDropdown
ghost
key={'export'}
dropdownWidth={300}
actions={[
{
icon: <IconCopy />,
label: 'Copy HTML',
desc: 'Copy the result HTML to clipboard',
onClick: this.handleCopyHTML,
},
{
icon: <IconCode />,
label: 'Export to HTML file',
desc: 'Save the result HTML file to disk',
onClick: this.handleExportToHTML,
},
{
icon: <IconCamera />,
label: 'Screenshot',
desc: 'Save a screenshot of mobile & desktop result',
onClick: this.handleScreenshot,
},
]}
/>,
]}
</div>
<Button
className="ml-10"
ghost
onClick={this.openSettingsModal}
ref={n => (this._btnSettings = n)}
>
<FaCog />
</Button>
<NotifBtn />
</div>
<div className="fg-1 d-f fd-c r" style={{ zIndex: 1 }}>
<FilesList
onRef={n => (this._filelist = n)}
onEditorRef={n => (this._editor = n)}
withPreview
withHome
rootPath={rootPath}
path={path}
activeFile={activeFile}
onActiveFileChange={this.handleActiveFileChange}
onPathChange={this.handlePathChange}
onAddClick={this.openAddModal}
onAddFile={this.handleAddFile}
onRemoveFile={this.handleRemoveFile}
focusHome
/>
</div>
<SendModal />
<AddFileModal rootPath={rootPath} onAdd={this.handleAddFile} />
<RemoveFileModal rootPath={rootPath} onRemove={this.handleRemoveFile} />
</div>
)
}
}
export default ProjectPage