From 090b5c32f01a8e87e8aec19113108b1a7ba918e1 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Sun, 9 Jun 2019 13:28:53 +0800 Subject: [PATCH 01/21] feat: Added Context Menu for markdown preview mode and copy url when hyperlink --- browser/components/CodeEditor.js | 2 +- browser/components/MarkdownPreview.js | 33 ++------------ browser/lib/contextMenuBuilder.js | 65 ++++++++++++++++++++++++++- tests/lib/contextMenuBuilder.test.js | 12 +++++ 4 files changed, 81 insertions(+), 31 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 1abd15a9e..59782e080 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -20,7 +20,7 @@ import styles from '../components/CodeEditor.styl' const { ipcRenderer, remote, clipboard } = require('electron') import normalizeEditorFontFamily from 'browser/lib/normalizeEditorFontFamily' const spellcheck = require('browser/lib/spellcheck') -const buildEditorContextMenu = require('browser/lib/contextMenuBuilder') +const buildEditorContextMenu = require('browser/lib/contextMenuBuilder').buildEditorContextMenu import TurndownService from 'turndown' import {languageMaps} from '../lib/CMLanguageList' import snippetManager from '../lib/SnippetManager' diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index bb663c5e3..a407651ef 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -18,15 +18,13 @@ import mdurl from 'mdurl' import exportNote from 'browser/main/lib/dataApi/exportNote' import { escapeHtmlCharacters } from 'browser/lib/utils' import yaml from 'js-yaml' -import context from 'browser/lib/context' -import i18n from 'browser/lib/i18n' -import fs from 'fs' import { render } from 'react-dom' import Carousel from 'react-image-carousel' import ConfigManager from '../main/lib/ConfigManager' const { remote, shell } = require('electron') const attachmentManagement = require('../main/lib/dataApi/attachmentManagement') +const buildMarkdownPreviewContextMenu = require('browser/lib/contextMenuBuilder').buildMarkdownPreviewContextMenu const { app } = remote const path = require('path') @@ -34,8 +32,6 @@ const fileUrl = require('file-url') const dialog = remote.dialog -const uri2path = require('file-uri-to-path') - const markdownStyle = require('!!css!stylus?sourceMap!./markdown.styl')[0][1] const appPath = fileUrl( process.env.NODE_ENV === 'production' ? app.getAppPath() : path.resolve() @@ -249,30 +245,9 @@ export default class MarkdownPreview extends React.Component { } handleContextMenu (event) { - // If a contextMenu handler was passed to us, use it instead of the self-defined one -> return - if (_.isFunction(this.props.onContextMenu)) { - this.props.onContextMenu(event) - return - } - // No contextMenu was passed to us -> execute our own link-opener - if (event.target.tagName.toLowerCase() === 'a' && event.target.getAttribute('href')) { - const href = event.target.href - const isLocalFile = href.startsWith('file:') - if (isLocalFile) { - const absPath = uri2path(href) - try { - if (fs.lstatSync(absPath).isFile()) { - context.popup([ - { - label: i18n.__('Show in explorer'), - click: (e) => shell.showItemInFolder(absPath) - } - ]) - } - } catch (e) { - console.log('Error while evaluating if the file is locally available', e) - } - } + const menu = buildMarkdownPreviewContextMenu(this, event) + if (menu != null) { + setTimeout(() => menu.popup(remote.getCurrentWindow()), 30) } } diff --git a/browser/lib/contextMenuBuilder.js b/browser/lib/contextMenuBuilder.js index cf92f52e7..c46f0dc41 100644 --- a/browser/lib/contextMenuBuilder.js +++ b/browser/lib/contextMenuBuilder.js @@ -1,6 +1,12 @@ +import i18n from 'browser/lib/i18n' +import fs from 'fs' + const {remote} = require('electron') const {Menu} = remote.require('electron') +const {clipboard} = remote.require('electron') +const {shell} = remote.require('electron') const spellcheck = require('./spellcheck') +const uri2path = require('file-uri-to-path') /** * Creates the context menu that is shown when there is a right click in the editor of a (not-snippet) note. @@ -62,4 +68,61 @@ const buildEditorContextMenu = function (editor, event) { return Menu.buildFromTemplate(template) } -module.exports = buildEditorContextMenu +/** + * Creates the context menu that is shown when there is a right click Markdown preview of a (not-snippet) note. + * @param {MarkdownPreview} markdownPreview + * @param {MouseEvent} event that has triggered the creation of the context menu + * @returns {Electron.Menu} The created electron context menu + */ +const buildMarkdownPreviewContextMenu = function (markdownPreview, event) { + if (markdownPreview == null || event == null || event.pageX == null || event.pageY == null) { + return null + } + + // Default context menu inclusions + const template = [{ + role: 'cut' + }, { + role: 'copy' + }, { + role: 'paste' + }, { + role: 'selectall' + }] + + if (event.target.tagName.toLowerCase() === 'a' && event.target.getAttribute('href')) { + // Link opener for files on the local system pointed to by href + const href = event.target.href + const isLocalFile = href.startsWith('file:') + if (isLocalFile) { + const absPath = uri2path(href) + try { + if (fs.lstatSync(absPath).isFile()) { + template.push( + { + label: i18n.__('Show in explorer'), + click: (e) => shell.showItemInFolder(absPath) + } + ) + } + } catch (e) { + console.log('Error while evaluating if the file is locally available', e) + } + } + + // Add option to context menu to copy url + template.push( + { + label: i18n.__('Copy Url'), + click: (e) => clipboard.writeText(href) + } + ) + } + return Menu.buildFromTemplate(template) +} + +module.exports = +{ + buildEditorContextMenu: buildEditorContextMenu, + buildMarkdownPreviewContextMenu: buildMarkdownPreviewContextMenu +} diff --git a/tests/lib/contextMenuBuilder.test.js b/tests/lib/contextMenuBuilder.test.js index 12ed2c321..e61d4b73a 100644 --- a/tests/lib/contextMenuBuilder.test.js +++ b/tests/lib/contextMenuBuilder.test.js @@ -5,11 +5,13 @@ jest.mock('electron', () => { const spellcheck = require('browser/lib/spellcheck') const buildEditorContextMenu = require('browser/lib/contextMenuBuilder') +const buildMarkdownPreviewContextMenu = require('browser/lib/contextMenuBuilder') beforeEach(() => { menuBuilderParameter = null }) +// Editor Context Menu it('should make sure that no context menu is build if the passed editor instance was null', function () { const event = { pageX: 12, @@ -124,3 +126,13 @@ it('should make sure that word suggestions creates a correct menu if there was a expect(menuBuilderParameter[7].role).toEqual('selectall') expect(spellcheck.getSpellingSuggestion).toHaveBeenCalledWith(wordToCorrect) }) + +// Markdown Preview Context Menu +it('should make sure that no context menu is built if the Markdown Preview instance was null', function () { + const event = { + pageX: 12, + pageY: 12 + } + buildMarkdownPreviewContextMenu(null, event) + expect(menuBuilderParameter).toEqual(null) +}) From ef1809305c2d3286b81221644830023ceeba0e44 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Tue, 11 Jun 2019 18:20:24 +0800 Subject: [PATCH 02/21] feat(prettierOnMarkdown): Added Reference To Prettier --- package.json | 3 ++- yarn.lock | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 0893e6812..2634ac148 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "boost", "productName": "Boostnote", - "version": "0.11.17", + "version": "0.11.17", "main": "index.js", "description": "Boostnote", "license": "GPL-3.0", @@ -98,6 +98,7 @@ "mousetrap": "^1.6.2", "mousetrap-global-bind": "^1.1.0", "node-ipc": "^8.1.0", + "prettier": "^1.18.2", "prop-types": "^15.7.2", "query-string": "^6.5.0", "raphael": "^2.2.7", diff --git a/yarn.lock b/yarn.lock index 30640d178..c4af1e910 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7482,6 +7482,11 @@ preserve@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b" +prettier@^1.18.2: + version "1.18.2" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea" + integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw== + pretty-bytes@^1.0.2: version "1.0.4" resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-1.0.4.tgz#0a22e8210609ad35542f8c8d5d2159aff0751c84" From 25bdaf9f00a7a9584948e5ec19940588a411c21c Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Tue, 11 Jun 2019 19:09:50 +0800 Subject: [PATCH 03/21] feat(prettierOnMarkdown): Added Reference to prettier in Code Editor and created config file --- browser/components/CodeEditor.js | 5 +++++ prettier.config | 6 ++++++ 2 files changed, 11 insertions(+) create mode 100644 prettier.config diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 1abd15a9e..c758ea07a 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -28,6 +28,7 @@ import {generateInEditor, tocExistsInEditor} from 'browser/lib/markdown-toc-gene import markdownlint from 'markdownlint' import Jsonlint from 'jsonlint-mod' import { DEFAULT_CONFIG } from '../main/lib/ConfigManager' +const prettier = require('prettier') CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' @@ -232,6 +233,10 @@ 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.pasteSmartly)]: cm => { this.handlePaste(cm, true) } diff --git a/prettier.config b/prettier.config new file mode 100644 index 000000000..8b8b7b99f --- /dev/null +++ b/prettier.config @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 4, + "semi": false, + "singleQuote": true +} \ No newline at end of file From f0380ef733bd5eabab94168e27686e9709618d0f Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Sat, 15 Jun 2019 16:23:51 +0800 Subject: [PATCH 04/21] 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 c758ea07a..d1354bb46 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 e956655c2..a7154e684 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 + }} /> +
+
+
From ed4a670f0aa57027fef70be3711ab0031b84d172 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Sun, 23 Jun 2019 13:54:17 +0800 Subject: [PATCH 08/21] feat(prettierOnMarkdown): Changed default hotkey value --- browser/main/lib/ConfigManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/lib/ConfigManager.js b/browser/main/lib/ConfigManager.js index 9e4e4d132..f2d6d85a7 100644 --- a/browser/main/lib/ConfigManager.js +++ b/browser/main/lib/ConfigManager.js @@ -31,7 +31,7 @@ export const DEFAULT_CONFIG = { toggleMode: OSX ? 'Command + Alt + M' : 'Ctrl + M', deleteNote: OSX ? 'Command + Shift + Backspace' : 'Ctrl + Shift + Backspace', pasteSmartly: OSX ? 'Command + Shift + V' : 'Ctrl + Shift + V', - prettifyMarkdown: 'Shift + Alt + F', + prettifyMarkdown: 'Shift + F', toggleMenuBar: 'Alt' }, ui: { From bde357f95200197b4bedcb44aac57136ca32569a Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Wed, 3 Jul 2019 09:03:24 +0800 Subject: [PATCH 09/21] feat(prettierOnMarkdown): Changed Prettier require to use import --- browser/components/CodeEditor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index d1354bb46..ed24c671d 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -28,7 +28,7 @@ import {generateInEditor, tocExistsInEditor} from 'browser/lib/markdown-toc-gene import markdownlint from 'markdownlint' import Jsonlint from 'jsonlint-mod' import { DEFAULT_CONFIG } from '../main/lib/ConfigManager' -const prettier = require('prettier') +import prettier from 'prettier' CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' From 1d59d89588a9b328b81d19714e7b410bc7cef8c8 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Wed, 3 Jul 2019 09:28:36 +0800 Subject: [PATCH 10/21] feat(prettierOnMarkdown): Forced prettier options to always have parser set to markdown when used. --- browser/components/CodeEditor.js | 8 ++++++-- browser/main/lib/ConfigManager.js | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index ed24c671d..8421b520a 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -236,11 +236,15 @@ export default class CodeEditor extends React.Component { [translateHotkey(hotkey.prettifyMarkdown)]: cm => { // Default / User configured prettier options const currentConfig = JSON.parse(self.props.prettierConfig) - // Get current cursor position. + + // Parser type will always need to be markdown so we override the option before use + currentConfig.parser = 'markdown' + + // Get current cursor position const cursorPos = cm.getCursor() currentConfig.cursorOffset = cm.doc.indexFromPos(cursorPos) - // Prettify contents of editor. + // Prettify contents of editor const formattedTextDetails = prettier.formatWithCursor(cm.doc.getValue(), currentConfig) const formattedText = formattedTextDetails.formatted diff --git a/browser/main/lib/ConfigManager.js b/browser/main/lib/ConfigManager.js index f2d6d85a7..d4c73b4a7 100644 --- a/browser/main/lib/ConfigManager.js +++ b/browser/main/lib/ConfigManager.js @@ -71,8 +71,7 @@ export const DEFAULT_CONFIG = { "trailingComma": "es5", "tabWidth": 4, "semi": false, - "singleQuote": true, - "parser":"markdown" + "singleQuote": true }` }, From a39e9c2da64b29602ffbad7160291078b6c32ced Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Tue, 11 Jun 2019 18:20:24 +0800 Subject: [PATCH 11/21] feat(prettierOnMarkdown): Added Reference To Prettier --- package.json | 3 ++- yarn.lock | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 0893e6812..2634ac148 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "boost", "productName": "Boostnote", - "version": "0.11.17", + "version": "0.11.17", "main": "index.js", "description": "Boostnote", "license": "GPL-3.0", @@ -98,6 +98,7 @@ "mousetrap": "^1.6.2", "mousetrap-global-bind": "^1.1.0", "node-ipc": "^8.1.0", + "prettier": "^1.18.2", "prop-types": "^15.7.2", "query-string": "^6.5.0", "raphael": "^2.2.7", diff --git a/yarn.lock b/yarn.lock index 30640d178..c4af1e910 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7482,6 +7482,11 @@ preserve@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b" +prettier@^1.18.2: + version "1.18.2" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea" + integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw== + pretty-bytes@^1.0.2: version "1.0.4" resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-1.0.4.tgz#0a22e8210609ad35542f8c8d5d2159aff0751c84" From 7e3c662374904ecbe24f5062d2bb9f4c6e143919 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Tue, 11 Jun 2019 19:09:50 +0800 Subject: [PATCH 12/21] feat(prettierOnMarkdown): Added Reference to prettier in Code Editor and created config file --- browser/components/CodeEditor.js | 5 +++++ prettier.config | 6 ++++++ 2 files changed, 11 insertions(+) create mode 100644 prettier.config diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 59782e080..a8f001697 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -28,6 +28,7 @@ import {generateInEditor, tocExistsInEditor} from 'browser/lib/markdown-toc-gene import markdownlint from 'markdownlint' import Jsonlint from 'jsonlint-mod' import { DEFAULT_CONFIG } from '../main/lib/ConfigManager' +const prettier = require('prettier') CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' @@ -232,6 +233,10 @@ 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.pasteSmartly)]: cm => { this.handlePaste(cm, true) } diff --git a/prettier.config b/prettier.config new file mode 100644 index 000000000..8b8b7b99f --- /dev/null +++ b/prettier.config @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 4, + "semi": false, + "singleQuote": true +} \ No newline at end of file From 33161e46e63e54d2bd26aceb91232b80973c0126 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Sat, 15 Jun 2019 16:23:51 +0800 Subject: [PATCH 13/21] 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 a8f001697..241a099a4 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 e956655c2..a7154e684 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 + }} /> +
+
+
From 0ad3da5bbcdcb8771bd3f3dc9a0348abdaca191f Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Sun, 23 Jun 2019 13:54:17 +0800 Subject: [PATCH 17/21] feat(prettierOnMarkdown): Changed default hotkey value --- browser/main/lib/ConfigManager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/lib/ConfigManager.js b/browser/main/lib/ConfigManager.js index 9e4e4d132..f2d6d85a7 100644 --- a/browser/main/lib/ConfigManager.js +++ b/browser/main/lib/ConfigManager.js @@ -31,7 +31,7 @@ export const DEFAULT_CONFIG = { toggleMode: OSX ? 'Command + Alt + M' : 'Ctrl + M', deleteNote: OSX ? 'Command + Shift + Backspace' : 'Ctrl + Shift + Backspace', pasteSmartly: OSX ? 'Command + Shift + V' : 'Ctrl + Shift + V', - prettifyMarkdown: 'Shift + Alt + F', + prettifyMarkdown: 'Shift + F', toggleMenuBar: 'Alt' }, ui: { From 911fd9a0042d39faf7c681815017ebe59783e7fd Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Wed, 3 Jul 2019 09:03:24 +0800 Subject: [PATCH 18/21] feat(prettierOnMarkdown): Changed Prettier require to use import --- browser/components/CodeEditor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index 241a099a4..e6debd766 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -28,7 +28,7 @@ import {generateInEditor, tocExistsInEditor} from 'browser/lib/markdown-toc-gene import markdownlint from 'markdownlint' import Jsonlint from 'jsonlint-mod' import { DEFAULT_CONFIG } from '../main/lib/ConfigManager' -const prettier = require('prettier') +import prettier from 'prettier' CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js' From 1173631255d66b40d824a3e01565997d56258603 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Wed, 3 Jul 2019 09:28:36 +0800 Subject: [PATCH 19/21] feat(prettierOnMarkdown): Forced prettier options to always have parser set to markdown when used. --- browser/components/CodeEditor.js | 8 ++++++-- browser/main/lib/ConfigManager.js | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/browser/components/CodeEditor.js b/browser/components/CodeEditor.js index e6debd766..da479fd50 100644 --- a/browser/components/CodeEditor.js +++ b/browser/components/CodeEditor.js @@ -236,11 +236,15 @@ export default class CodeEditor extends React.Component { [translateHotkey(hotkey.prettifyMarkdown)]: cm => { // Default / User configured prettier options const currentConfig = JSON.parse(self.props.prettierConfig) - // Get current cursor position. + + // Parser type will always need to be markdown so we override the option before use + currentConfig.parser = 'markdown' + + // Get current cursor position const cursorPos = cm.getCursor() currentConfig.cursorOffset = cm.doc.indexFromPos(cursorPos) - // Prettify contents of editor. + // Prettify contents of editor const formattedTextDetails = prettier.formatWithCursor(cm.doc.getValue(), currentConfig) const formattedText = formattedTextDetails.formatted diff --git a/browser/main/lib/ConfigManager.js b/browser/main/lib/ConfigManager.js index f2d6d85a7..d4c73b4a7 100644 --- a/browser/main/lib/ConfigManager.js +++ b/browser/main/lib/ConfigManager.js @@ -71,8 +71,7 @@ export const DEFAULT_CONFIG = { "trailingComma": "es5", "tabWidth": 4, "semi": false, - "singleQuote": true, - "parser":"markdown" + "singleQuote": true }` }, From eeca031c86b518223d32a8eb45f572f4d001af24 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Thu, 1 Aug 2019 19:56:38 +0800 Subject: [PATCH 20/21] Merge upstream into master --- browser/components/MarkdownPreview.js | 4 ---- browser/lib/contextMenuBuilder.js | 9 --------- tests/lib/contextMenuBuilder.test.js | 5 ----- 3 files changed, 18 deletions(-) diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index e4c88de91..0072e4037 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -248,11 +248,7 @@ export default class MarkdownPreview extends React.Component { handleContextMenu (event) { const menu = buildMarkdownPreviewContextMenu(this, event) if (menu != null) { -<<<<<<< HEAD - setTimeout(() => menu.popup(remote.getCurrentWindow()), 30) -======= menu.popup(remote.getCurrentWindow()) ->>>>>>> upstream/master } } diff --git a/browser/lib/contextMenuBuilder.js b/browser/lib/contextMenuBuilder.js index 34f847895..ff3349eb7 100644 --- a/browser/lib/contextMenuBuilder.js +++ b/browser/lib/contextMenuBuilder.js @@ -81,17 +81,8 @@ const buildMarkdownPreviewContextMenu = function (markdownPreview, event) { // Default context menu inclusions const template = [{ -<<<<<<< HEAD - role: 'cut' - }, { - role: 'copy' - }, { - role: 'paste' - }, { -======= role: 'copy' }, { ->>>>>>> upstream/master role: 'selectall' }] diff --git a/tests/lib/contextMenuBuilder.test.js b/tests/lib/contextMenuBuilder.test.js index e3753aca1..b7009bf1f 100644 --- a/tests/lib/contextMenuBuilder.test.js +++ b/tests/lib/contextMenuBuilder.test.js @@ -4,13 +4,8 @@ jest.mock('electron', () => { }) const spellcheck = require('browser/lib/spellcheck') -<<<<<<< HEAD -const buildEditorContextMenu = require('browser/lib/contextMenuBuilder') -const buildMarkdownPreviewContextMenu = require('browser/lib/contextMenuBuilder') -======= const buildEditorContextMenu = require('browser/lib/contextMenuBuilder').buildEditorContextMenu const buildMarkdownPreviewContextMenu = require('browser/lib/contextMenuBuilder').buildMarkdownPreviewContextMenu ->>>>>>> upstream/master beforeEach(() => { menuBuilderParameter = null From 2d3c69d178328b4d54ab639aac4e93d22470f000 Mon Sep 17 00:00:00 2001 From: nathan-castlehow Date: Thu, 1 Aug 2019 20:13:46 +0800 Subject: [PATCH 21/21] Fixed eslint issue --- browser/main/modals/PreferencesModal/HotkeyTab.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser/main/modals/PreferencesModal/HotkeyTab.js b/browser/main/modals/PreferencesModal/HotkeyTab.js index 54639f200..a7e42c0f6 100644 --- a/browser/main/modals/PreferencesModal/HotkeyTab.js +++ b/browser/main/modals/PreferencesModal/HotkeyTab.js @@ -181,7 +181,7 @@ class HotkeyTab extends React.Component { onChange={(e) => this.handleHotkeyChange(e)} ref='prettifyMarkdown' value={config.hotkey.prettifyMarkdown} - type='text'/> + type='text' />