Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 263 #289

Merged
merged 3 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Expand Down Expand Up @@ -67,10 +69,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 @@ -243,14 +247,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