@@ -21,13 +21,14 @@ const { ipcRenderer, remote, clipboard } = require('electron')
21
21
import normalizeEditorFontFamily from 'browser/lib/normalizeEditorFontFamily'
22
22
const spellcheck = require ( 'browser/lib/spellcheck' )
23
23
const buildEditorContextMenu = require ( 'browser/lib/contextMenuBuilder' ) . buildEditorContextMenu
24
- import TurndownService from 'turndown'
24
+ import { createTurndownService } from '../lib/ turndown'
25
25
import { languageMaps } from '../lib/CMLanguageList'
26
26
import snippetManager from '../lib/SnippetManager'
27
27
import { generateInEditor , tocExistsInEditor } from 'browser/lib/markdown-toc-generator'
28
28
import markdownlint from 'markdownlint'
29
29
import Jsonlint from 'jsonlint-mod'
30
30
import { DEFAULT_CONFIG } from '../main/lib/ConfigManager'
31
+ import prettier from 'prettier'
31
32
32
33
CodeMirror . modeURL = '../node_modules/codemirror/mode/%N/%N.js'
33
34
@@ -69,7 +70,9 @@ export default class CodeEditor extends React.Component {
69
70
storageKey,
70
71
noteKey
71
72
} = this . props
72
- debouncedDeletionOfAttachments ( this . editor . getValue ( ) , storageKey , noteKey )
73
+ if ( this . props . deleteUnusedAttachments === true ) {
74
+ debouncedDeletionOfAttachments ( this . editor . getValue ( ) , storageKey , noteKey )
75
+ }
73
76
}
74
77
this . pasteHandler = ( editor , e ) => {
75
78
e . preventDefault ( )
@@ -98,15 +101,15 @@ export default class CodeEditor extends React.Component {
98
101
99
102
this . editorActivityHandler = ( ) => this . handleEditorActivity ( )
100
103
101
- this . turndownService = new TurndownService ( )
104
+ this . turndownService = createTurndownService ( )
102
105
}
103
106
104
107
handleSearch ( msg ) {
105
108
const cm = this . editor
106
109
const component = this
107
110
108
111
if ( component . searchState ) cm . removeOverlay ( component . searchState )
109
- if ( msg . length < 3 ) return
112
+ if ( msg . length < 1 ) return
110
113
111
114
cm . operation ( function ( ) {
112
115
component . searchState = makeOverlay ( msg , 'searching' )
@@ -216,6 +219,37 @@ export default class CodeEditor extends React.Component {
216
219
}
217
220
return CodeMirror . Pass
218
221
} ,
222
+ [ translateHotkey ( hotkey . prettifyMarkdown ) ] : cm => {
223
+ // Default / User configured prettier options
224
+ const currentConfig = JSON . parse ( self . props . prettierConfig )
225
+
226
+ // Parser type will always need to be markdown so we override the option before use
227
+ currentConfig . parser = 'markdown'
228
+
229
+ // Get current cursor position
230
+ const cursorPos = cm . getCursor ( )
231
+ currentConfig . cursorOffset = cm . doc . indexFromPos ( cursorPos )
232
+
233
+ // Prettify contents of editor
234
+ const formattedTextDetails = prettier . formatWithCursor ( cm . doc . getValue ( ) , currentConfig )
235
+
236
+ const formattedText = formattedTextDetails . formatted
237
+ const formattedCursorPos = formattedTextDetails . cursorOffset
238
+ cm . doc . setValue ( formattedText )
239
+
240
+ // Reset Cursor position to be at the same markdown as was before prettifying
241
+ const newCursorPos = cm . doc . posFromIndex ( formattedCursorPos )
242
+ cm . doc . setCursor ( newCursorPos )
243
+ } ,
244
+ [ translateHotkey ( hotkey . sortLines ) ] : cm => {
245
+ const selection = cm . doc . getSelection ( )
246
+ const appendLineBreak = / \n $ / . test ( selection )
247
+
248
+ const sorted = _ . split ( selection . trim ( ) , '\n' ) . sort ( )
249
+ const sortedString = _ . join ( sorted , '\n' ) + ( appendLineBreak ? '\n' : '' )
250
+
251
+ cm . doc . replaceSelection ( sortedString )
252
+ } ,
219
253
[ translateHotkey ( hotkey . pasteSmartly ) ] : cm => {
220
254
this . handlePaste ( cm , true )
221
255
}
@@ -269,7 +303,8 @@ export default class CodeEditor extends React.Component {
269
303
explode : this . props . explodingPairs ,
270
304
override : true
271
305
} ,
272
- extraKeys : this . defaultKeyMap
306
+ extraKeys : this . defaultKeyMap ,
307
+ prettierConfig : this . props . prettierConfig
273
308
} )
274
309
275
310
document . querySelector ( '.CodeMirror-lint-markers' ) . style . display = enableMarkdownLint ? 'inline-block' : 'none'
@@ -608,6 +643,9 @@ export default class CodeEditor extends React.Component {
608
643
this . editor . addPanel ( this . createSpellCheckPanel ( ) , { position : 'bottom' } )
609
644
}
610
645
}
646
+ if ( prevProps . deleteUnusedAttachments !== this . props . deleteUnusedAttachments ) {
647
+ this . editor . setOption ( 'deleteUnusedAttachments' , this . props . deleteUnusedAttachments )
648
+ }
611
649
612
650
if ( needRefresh ) {
613
651
this . editor . refresh ( )
@@ -836,6 +874,17 @@ export default class CodeEditor extends React.Component {
836
874
this . editor . setCursor ( cursor )
837
875
}
838
876
877
+ /**
878
+ * Update content of one line
879
+ * @param {Number } lineNumber
880
+ * @param {String } content
881
+ */
882
+ setLineContent ( lineNumber , content ) {
883
+ const prevContent = this . editor . getLine ( lineNumber )
884
+ const prevContentLength = prevContent ? prevContent . length : 0
885
+ this . editor . replaceRange ( content , { line : lineNumber , ch : 0 } , { line : lineNumber , ch : prevContentLength } )
886
+ }
887
+
839
888
handleDropImage ( dropEvent ) {
840
889
dropEvent . preventDefault ( )
841
890
const {
@@ -1169,7 +1218,8 @@ CodeEditor.propTypes = {
1169
1218
autoDetect : PropTypes . bool ,
1170
1219
spellCheck : PropTypes . bool ,
1171
1220
enableMarkdownLint : PropTypes . bool ,
1172
- customMarkdownLintConfig : PropTypes . string
1221
+ customMarkdownLintConfig : PropTypes . string ,
1222
+ deleteUnusedAttachments : PropTypes . bool
1173
1223
}
1174
1224
1175
1225
CodeEditor . defaultProps = {
@@ -1183,5 +1233,7 @@ CodeEditor.defaultProps = {
1183
1233
autoDetect : false ,
1184
1234
spellCheck : false ,
1185
1235
enableMarkdownLint : DEFAULT_CONFIG . editor . enableMarkdownLint ,
1186
- customMarkdownLintConfig : DEFAULT_CONFIG . editor . customMarkdownLintConfig
1236
+ customMarkdownLintConfig : DEFAULT_CONFIG . editor . customMarkdownLintConfig ,
1237
+ prettierConfig : DEFAULT_CONFIG . editor . prettierConfig ,
1238
+ deleteUnusedAttachments : DEFAULT_CONFIG . editor . deleteUnusedAttachments
1187
1239
}
0 commit comments