Skip to content

Commit 33161e4

Browse files
feat(prettierOnMarkdown): Added support for prettyifing markdown as well as added hot key option. Partial Implementation of Prettier config in configuration screen. TODO Fix defaulting of prettier configuration
1 parent 7e3c662 commit 33161e4

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

browser/components/CodeEditor.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,23 @@ export default class CodeEditor extends React.Component {
233233
}
234234
return CodeMirror.Pass
235235
},
236-
'Shift-Alt-F': cm => {
237-
// console.log(prettier.format('foo ( );', { semi: false, parser: 'babel' }))
238-
// console.log('Key Combo Pressed')
236+
[translateHotkey(hotkey.prettifyMarkdown)]: cm => {
237+
// Default / User configured prettier options
238+
const currentConfig = JSON.parse(self.props.prettierConfig)
239+
// Get current cursor position.
240+
const cursorPos = cm.getCursor()
241+
currentConfig.cursorOffset = cm.doc.indexFromPos(cursorPos)
242+
243+
// Prettify contents of editor.
244+
const formattedTextDetails = prettier.formatWithCursor(cm.doc.getValue(), currentConfig)
245+
246+
const formattedText = formattedTextDetails.formatted
247+
const formattedCursorPos = formattedTextDetails.cursorOffset
248+
cm.doc.setValue(formattedText)
249+
250+
// Reset Cursor position to be at the same markdown as was before prettifying
251+
const newCursorPos = cm.doc.posFromIndex(formattedCursorPos)
252+
cm.doc.setCursor(newCursorPos)
239253
},
240254
[translateHotkey(hotkey.pasteSmartly)]: cm => {
241255
this.handlePaste(cm, true)
@@ -290,7 +304,8 @@ export default class CodeEditor extends React.Component {
290304
explode: this.props.explodingPairs,
291305
override: true
292306
},
293-
extraKeys: this.defaultKeyMap
307+
extraKeys: this.defaultKeyMap,
308+
prettierConfig: this.props.prettierConfig
294309
})
295310

296311
document.querySelector('.CodeMirror-lint-markers').style.display = enableMarkdownLint ? 'inline-block' : 'none'
@@ -1200,5 +1215,6 @@ CodeEditor.defaultProps = {
12001215
autoDetect: false,
12011216
spellCheck: false,
12021217
enableMarkdownLint: DEFAULT_CONFIG.editor.enableMarkdownLint,
1203-
customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig
1218+
customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig,
1219+
prettierConfig: DEFAULT_CONFIG.editor.prettierConfig
12041220
}

browser/components/MarkdownEditor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class MarkdownEditor extends React.Component {
321321
switchPreview={config.editor.switchPreview}
322322
enableMarkdownLint={config.editor.enableMarkdownLint}
323323
customMarkdownLintConfig={config.editor.customMarkdownLintConfig}
324+
prettierConfig={config.editor.prettierConfig}
324325
/>
325326
<MarkdownPreview styleName={this.state.status === 'PREVIEW'
326327
? 'preview'

browser/main/lib/ConfigManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const DEFAULT_CONFIG = {
3131
toggleMode: OSX ? 'Command + Alt + M' : 'Ctrl + M',
3232
deleteNote: OSX ? 'Command + Shift + Backspace' : 'Ctrl + Shift + Backspace',
3333
pasteSmartly: OSX ? 'Command + Shift + V' : 'Ctrl + Shift + V',
34+
prettifyMarkdown: 'Shift + Alt + F',
3435
toggleMenuBar: 'Alt'
3536
},
3637
ui: {

browser/main/modals/PreferencesModal/HotkeyTab.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class HotkeyTab extends React.Component {
8181
toggleMode: this.refs.toggleMode.value,
8282
deleteNote: this.refs.deleteNote.value,
8383
pasteSmartly: this.refs.pasteSmartly.value,
84+
prettifyMarkdown: this.refs.prettifyMarkdown.value,
8485
toggleMenuBar: this.refs.toggleMenuBar.value
8586
}
8687
this.setState({
@@ -173,6 +174,17 @@ class HotkeyTab extends React.Component {
173174
/>
174175
</div>
175176
</div>
177+
<div styleName='group-section'>
178+
<div styleName='group-section-label'>{i18n.__('Prettify Markdown')}</div>
179+
<div styleName='group-section-control'>
180+
<input styleName='group-section-control-input'
181+
onChange={(e) => this.handleHotkeyChange(e)}
182+
ref='prettifyMarkdown'
183+
value={config.hotkey.prettifyMarkdown}
184+
type='text'
185+
/>
186+
</div>
187+
</div>
176188
<div styleName='group-control'>
177189
<button styleName='group-control-leftButton'
178190
onClick={(e) => this.handleHintToggleButtonClick(e)}

browser/main/modals/PreferencesModal/UiTab.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ class UiTab extends React.Component {
3131
CodeMirror.autoLoadMode(this.codeMirrorInstance.getCodeMirror(), 'javascript')
3232
CodeMirror.autoLoadMode(this.customCSSCM.getCodeMirror(), 'css')
3333
CodeMirror.autoLoadMode(this.customMarkdownLintConfigCM.getCodeMirror(), 'javascript')
34+
CodeMirror.autoLoadMode(this.prettierConfigCM.getCodeMirror(), 'json')
35+
// Set CM editor Sizes
3436
this.customCSSCM.getCodeMirror().setSize('400px', '400px')
37+
this.prettierConfigCM.getCodeMirror().setSize('400px', '400px')
3538
this.customMarkdownLintConfigCM.getCodeMirror().setSize('400px', '200px')
39+
3640
this.handleSettingDone = () => {
3741
this.setState({UiAlert: {
3842
type: 'success',
@@ -105,7 +109,9 @@ class UiTab extends React.Component {
105109
spellcheck: this.refs.spellcheck.checked,
106110
enableSmartPaste: this.refs.enableSmartPaste.checked,
107111
enableMarkdownLint: this.refs.enableMarkdownLint.checked,
108-
customMarkdownLintConfig: this.customMarkdownLintConfigCM.getCodeMirror().getValue()
112+
customMarkdownLintConfig: this.customMarkdownLintConfigCM.getCodeMirror().getValue(),
113+
prettierConfig: this.prettierConfigCM.getCodeMirror().getValue()
114+
109115
},
110116
preview: {
111117
fontSize: this.refs.previewFontSize.value,
@@ -892,7 +898,26 @@ class UiTab extends React.Component {
892898
</div>
893899
</div>
894900
</div>
895-
901+
<div styleName='group-section'>
902+
<div styleName='group-section-label'>
903+
{i18n.__('Prettier Config')}
904+
</div>
905+
<div styleName='group-section-control'>
906+
<div style={{fontFamily}}>
907+
<ReactCodeMirror
908+
width='400px'
909+
height='400px'
910+
onChange={e => this.handleUIChange(e)}
911+
ref={e => (this.prettierConfigCM = e)}
912+
value={config.editor.prettierConfig}
913+
options={{
914+
lineNumbers: true,
915+
mode: 'json',
916+
theme: codemirrorTheme
917+
}} />
918+
</div>
919+
</div>
920+
</div>
896921
<div styleName='group-control'>
897922
<button styleName='group-control-rightButton'
898923
onClick={(e) => this.handleSaveUIClick(e)}>{i18n.__('Save')}

0 commit comments

Comments
 (0)