From 33161e46e63e54d2bd26aceb91232b80973c0126 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Sat, 15 Jun 2019 16:23:51 +0800 Subject: [PATCH] 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 --- browser/components/CodeEditor.js | 26 +++++++++++++---- browser/components/MarkdownEditor.js | 1 + browser/main/lib/ConfigManager.js | 1 + .../main/modals/PreferencesModal/HotkeyTab.js | 12 ++++++++ browser/main/modals/PreferencesModal/UiTab.js | 29 +++++++++++++++++-- 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index a8f00169..241a099a 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -233,9 +233,23 @@ export default class CodeEditor extends React.Component { } return CodeMirror.Pass }, - 'Shift-Alt-F': cm => { - // console.log(prettier.format('foo ( );', { semi: false, parser: 'babel' })) - // console.log('Key Combo Pressed') + [translateHotkey(hotkey.prettifyMarkdown)]: cm => { + // Default / User configured prettier options + const currentConfig = JSON.parse(self.props.prettierConfig) + // Get current cursor position. + const cursorPos = cm.getCursor() + currentConfig.cursorOffset = cm.doc.indexFromPos(cursorPos) + + // Prettify contents of editor. + const formattedTextDetails = prettier.formatWithCursor(cm.doc.getValue(), currentConfig) + + const formattedText = formattedTextDetails.formatted + const formattedCursorPos = formattedTextDetails.cursorOffset + cm.doc.setValue(formattedText) + + // Reset Cursor position to be at the same markdown as was before prettifying + const newCursorPos = cm.doc.posFromIndex(formattedCursorPos) + cm.doc.setCursor(newCursorPos) }, [translateHotkey(hotkey.pasteSmartly)]: cm => { this.handlePaste(cm, true) @@ -290,7 +304,8 @@ export default class CodeEditor extends React.Component { explode: this.props.explodingPairs, override: true }, - extraKeys: this.defaultKeyMap + extraKeys: this.defaultKeyMap, + prettierConfig: this.props.prettierConfig }) document.querySelector('.CodeMirror-lint-markers').style.display = enableMarkdownLint ? 'inline-block' : 'none' @@ -1200,5 +1215,6 @@ CodeEditor.defaultProps = { autoDetect: false, spellCheck: false, enableMarkdownLint: DEFAULT_CONFIG.editor.enableMarkdownLint, - customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig + customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig, + prettierConfig: DEFAULT_CONFIG.editor.prettierConfig } diff --git a/browser/components/MarkdownEditor.js b/browser/components/MarkdownEditor.js index e956655c..a7154e68 100644 --- a/browser/components/MarkdownEditor.js +++ b/browser/components/MarkdownEditor.js @@ -321,6 +321,7 @@ class MarkdownEditor extends React.Component { switchPreview={config.editor.switchPreview} enableMarkdownLint={config.editor.enableMarkdownLint} customMarkdownLintConfig={config.editor.customMarkdownLintConfig} + prettierConfig={config.editor.prettierConfig} /> +
+
{i18n.__('Prettify Markdown')}
+
+ this.handleHotkeyChange(e)} + ref='prettifyMarkdown' + value={config.hotkey.prettifyMarkdown} + type='text' + /> +
+
- +
+
+ {i18n.__('Prettier Config')} +
+
+
+ this.handleUIChange(e)} + ref={e => (this.prettierConfigCM = e)} + value={config.editor.prettierConfig} + options={{ + lineNumbers: true, + mode: 'json', + theme: codemirrorTheme + }} /> +
+
+