Skip to content

Commit

Permalink
Merge pull request #289 from mjmlio/fix-263
Browse files Browse the repository at this point in the history
Fix 263
  • Loading branch information
iRyusa authored Jul 2, 2019
2 parents 0547044 + 9100a31 commit 1807e12
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
29 changes: 25 additions & 4 deletions src/components/FileEditor/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react'
import { connect } from 'react-redux'
import debounce from 'lodash/debounce'
import get from 'lodash/get'
import { get } from 'lodash'

import beautifyJS from 'js-beautify'

Expand All @@ -25,6 +25,8 @@ import 'codemirror/addon/lint/lint'

import 'helpers/codemirror-util-autoformat'

import { addAlert } from 'reducers/alerts'

import isOldSyntax from 'helpers/detectOldMJMLSyntax'

import { codeMirrorCtrlD, codeMirrorDuplicate } from 'helpers/codemirror-shortcuts'
Expand Down Expand Up @@ -69,10 +71,12 @@ function beautify(content) {
useTab: settings.getIn(['editor', 'useTab'], false),
tabSize: settings.getIn(['editor', 'tabSize'], 2),
indentSize: settings.getIn(['editor', 'indentSize'], 2),
preventAutoSave: settings.getIn(['editor', 'preventAutoSave'], false),
}
},
{
setPreview,
addAlert,
},
)
class FileEditor extends Component {
Expand Down Expand Up @@ -257,14 +261,31 @@ class FileEditor extends Component {
}
}

async handleSave() {
const { fileName, addAlert } = this.props
const mjml = this._codeMirror.getValue()

try {
await fsWriteFile(fileName, mjml)
addAlert('File successfully saved', 'success')
} catch (e) {
addAlert('Could not save file', 'error')
console.log(e) // eslint-disable-line no-console
}
}

handleChange = debounce(async () => {
const { setPreview, fileName, mjmlEngine } = this.props
const { setPreview, fileName, mjmlEngine, preventAutoSave } = this.props
const mjml = this._codeMirror.getValue()
if (mjmlEngine === 'auto') {
setPreview(fileName, mjml)
this.debounceWrite(fileName, mjml)

if (!preventAutoSave) this.debounceWrite(fileName, mjml)
} else {
await fsWriteFile(fileName, mjml)
if (!preventAutoSave) {
await fsWriteFile(fileName, mjml)
}

setPreview(fileName, mjml)
}

Expand Down
1 change: 0 additions & 1 deletion src/components/ProjectsList/ProjectItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ProjectItem extends Component {

render() {
const { onRemove, onOpen, onDuplicate, onEditName, onToggleSelect, isSelected, p } = this.props

const { isOver } = this.state

return (
Expand Down
7 changes: 7 additions & 0 deletions src/components/SettingsModal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class SettingsModal extends Component {
const editorUseTab = settings.getIn(['editor', 'useTab'], false)
const editorTabSize = settings.getIn(['editor', 'tabSize'], 2)
const editorIndentSize = settings.getIn(['editor', 'indentSize'], 2)
const preventAutoSave = settings.getIn(['editor', 'preventAutoSave'], false)

return (
<Modal
Expand Down Expand Up @@ -194,6 +195,12 @@ class SettingsModal extends Component {
onChange={e => this.changeEditorSetting('indentSize')(Number(e.target.value))}
/>
</div>
<CheckBox
value={preventAutoSave}
onChange={this.changeEditorSetting('preventAutoSave')}
>
{'Don’t auto-save on change'}
</CheckBox>
</TabItem>

<TabItem title="Preview" className="flow-v-10" icon={IconPreview}>
Expand Down
1 change: 1 addition & 0 deletions src/helpers/takeScreenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function takeScreenshot(html, deviceWidth, workingDirectory) {
})

const tmpFileName = path.join(workingDirectory, TMP_FILE)

await fsWriteFile(tmpFileName, html)

win.loadURL(`file://${tmpFileName}`)
Expand Down
10 changes: 9 additions & 1 deletion src/pages/Project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 IconSave from 'react-icons/md/save'
import fs from 'fs'
import { shell, clipboard } from 'electron'
import beautifyJS from 'js-beautify'
Expand Down Expand Up @@ -38,6 +39,7 @@ import { takeScreenshot, cleanUp } from 'helpers/takeScreenshot'
preview: state.preview,
previewSize: state.settings.get('previewSize'),
beautifyOutput: state.settings.getIn(['mjml', 'beautify']),
preventAutoSave: state.settings.getIn(['editor', 'preventAutoSave']),
}),
{
openModal,
Expand Down Expand Up @@ -185,7 +187,7 @@ class ProjectPage extends Component {
}

render() {
const { preview } = this.props
const { preview, preventAutoSave } = this.props
const { path, activeFile } = this.state

const rootPath = this.props.location.query.path
Expand All @@ -203,6 +205,12 @@ class ProjectPage extends Component {
</Button>
</div>
<div className="d-f flow-h-10">
{preventAutoSave && [
<Button key="save" transparent onClick={() => this._editor.handleSave()}>
<IconSave style={{ marginRight: 5 }} />
{'Save'}
</Button>,
]}
{isMJMLFile && [
<Button key="beautify" transparent onClick={this.handleBeautify}>
<IconBeautify style={{ marginRight: 5 }} />
Expand Down

0 comments on commit 1807e12

Please sign in to comment.