Skip to content

Commit 4de6c69

Browse files
committed
merge from upstream/master
2 parents ba34458 + 43d8ebb commit 4de6c69

File tree

102 files changed

+5117
-3782
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+5117
-3782
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"globals": {
1919
"FileReader": true,
2020
"localStorage": true,
21-
"fetch": true
21+
"fetch": true,
22+
"Image": true,
23+
"MutationObserver": true
2224
},
2325
"env": {
2426
"jest": true

browser/components/CodeEditor.js

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ import styles from '../components/CodeEditor.styl'
2020
const { ipcRenderer, remote, clipboard } = require('electron')
2121
import normalizeEditorFontFamily from 'browser/lib/normalizeEditorFontFamily'
2222
const spellcheck = require('browser/lib/spellcheck')
23-
const buildEditorContextMenu = require('browser/lib/contextMenuBuilder')
24-
import TurndownService from 'turndown'
23+
const buildEditorContextMenu = require('browser/lib/contextMenuBuilder').buildEditorContextMenu
24+
import { createTurndownService } from '../lib/turndown'
2525
import {languageMaps} from '../lib/CMLanguageList'
2626
import snippetManager from '../lib/SnippetManager'
2727
import {generateInEditor, tocExistsInEditor} from 'browser/lib/markdown-toc-generator'
2828
import markdownlint from 'markdownlint'
2929
import Jsonlint from 'jsonlint-mod'
3030
import { DEFAULT_CONFIG } from '../main/lib/ConfigManager'
31+
import prettier from 'prettier'
3132

3233
CodeMirror.modeURL = '../node_modules/codemirror/mode/%N/%N.js'
3334

@@ -53,6 +54,7 @@ export default class CodeEditor extends React.Component {
5354
this.focusHandler = () => {
5455
ipcRenderer.send('editor:focused', true)
5556
}
57+
const debouncedDeletionOfAttachments = _.debounce(attachmentManagement.deleteAttachmentsNotPresentInNote, 30000)
5658
this.blurHandler = (editor, e) => {
5759
ipcRenderer.send('editor:focused', false)
5860
if (e == null) return null
@@ -64,16 +66,13 @@ export default class CodeEditor extends React.Component {
6466
el = el.parentNode
6567
}
6668
this.props.onBlur != null && this.props.onBlur(e)
67-
6869
const {
6970
storageKey,
7071
noteKey
7172
} = this.props
72-
attachmentManagement.deleteAttachmentsNotPresentInNote(
73-
this.editor.getValue(),
74-
storageKey,
75-
noteKey
76-
)
73+
if (this.props.deleteUnusedAttachments === true) {
74+
debouncedDeletionOfAttachments(this.editor.getValue(), storageKey, noteKey)
75+
}
7776
}
7877
this.pasteHandler = (editor, e) => {
7978
e.preventDefault()
@@ -102,15 +101,15 @@ export default class CodeEditor extends React.Component {
102101

103102
this.editorActivityHandler = () => this.handleEditorActivity()
104103

105-
this.turndownService = new TurndownService()
104+
this.turndownService = createTurndownService()
106105
}
107106

108107
handleSearch (msg) {
109108
const cm = this.editor
110109
const component = this
111110

112111
if (component.searchState) cm.removeOverlay(component.searchState)
113-
if (msg.length < 3) return
112+
if (msg.length < 1) return
114113

115114
cm.operation(function () {
116115
component.searchState = makeOverlay(msg, 'searching')
@@ -205,23 +204,11 @@ export default class CodeEditor extends React.Component {
205204
'Cmd-T': function (cm) {
206205
// Do nothing
207206
},
208-
'Ctrl-/': function (cm) {
209-
if (global.process.platform === 'darwin') { return }
207+
[translateHotkey(hotkey.insertDate)]: function (cm) {
210208
const dateNow = new Date()
211209
cm.replaceSelection(dateNow.toLocaleDateString())
212210
},
213-
'Cmd-/': function (cm) {
214-
if (global.process.platform !== 'darwin') { return }
215-
const dateNow = new Date()
216-
cm.replaceSelection(dateNow.toLocaleDateString())
217-
},
218-
'Shift-Ctrl-/': function (cm) {
219-
if (global.process.platform === 'darwin') { return }
220-
const dateNow = new Date()
221-
cm.replaceSelection(dateNow.toLocaleString())
222-
},
223-
'Shift-Cmd-/': function (cm) {
224-
if (global.process.platform !== 'darwin') { return }
211+
[translateHotkey(hotkey.insertDateTime)]: function (cm) {
225212
const dateNow = new Date()
226213
cm.replaceSelection(dateNow.toLocaleString())
227214
},
@@ -232,6 +219,28 @@ export default class CodeEditor extends React.Component {
232219
}
233220
return CodeMirror.Pass
234221
},
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+
},
235244
[translateHotkey(hotkey.pasteSmartly)]: cm => {
236245
this.handlePaste(cm, true)
237246
}
@@ -267,7 +276,7 @@ export default class CodeEditor extends React.Component {
267276
value: this.props.value,
268277
linesHighlighted: this.props.linesHighlighted,
269278
lineNumbers: this.props.displayLineNumbers,
270-
lineWrapping: true,
279+
lineWrapping: this.props.lineWrapping,
271280
theme: this.props.theme,
272281
indentUnit: this.props.indentSize,
273282
tabSize: this.props.indentSize,
@@ -285,7 +294,8 @@ export default class CodeEditor extends React.Component {
285294
explode: this.props.explodingPairs,
286295
override: true
287296
},
288-
extraKeys: this.defaultKeyMap
297+
extraKeys: this.defaultKeyMap,
298+
prettierConfig: this.props.prettierConfig
289299
})
290300

291301
document.querySelector('.CodeMirror-lint-markers').style.display = enableMarkdownLint ? 'inline-block' : 'none'
@@ -566,6 +576,10 @@ export default class CodeEditor extends React.Component {
566576
this.editor.setOption('lineNumbers', this.props.displayLineNumbers)
567577
}
568578

579+
if (prevProps.lineWrapping !== this.props.lineWrapping) {
580+
this.editor.setOption('lineWrapping', this.props.lineWrapping)
581+
}
582+
569583
if (prevProps.scrollPastEnd !== this.props.scrollPastEnd) {
570584
this.editor.setOption('scrollPastEnd', this.props.scrollPastEnd)
571585
}
@@ -620,6 +634,9 @@ export default class CodeEditor extends React.Component {
620634
this.editor.addPanel(this.createSpellCheckPanel(), {position: 'bottom'})
621635
}
622636
}
637+
if (prevProps.deleteUnusedAttachments !== this.props.deleteUnusedAttachments) {
638+
this.editor.setOption('deleteUnusedAttachments', this.props.deleteUnusedAttachments)
639+
}
623640

624641
if (needRefresh) {
625642
this.editor.refresh()
@@ -848,6 +865,17 @@ export default class CodeEditor extends React.Component {
848865
this.editor.setCursor(cursor)
849866
}
850867

868+
/**
869+
* Update content of one line
870+
* @param {Number} lineNumber
871+
* @param {String} content
872+
*/
873+
setLineContent (lineNumber, content) {
874+
const prevContent = this.editor.getLine(lineNumber)
875+
const prevContentLength = prevContent ? prevContent.length : 0
876+
this.editor.replaceRange(content, { line: lineNumber, ch: 0 }, { line: lineNumber, ch: prevContentLength })
877+
}
878+
851879
handleDropImage (dropEvent) {
852880
dropEvent.preventDefault()
853881
const {
@@ -1186,7 +1214,8 @@ CodeEditor.propTypes = {
11861214
autoDetect: PropTypes.bool,
11871215
spellCheck: PropTypes.bool,
11881216
enableMarkdownLint: PropTypes.bool,
1189-
customMarkdownLintConfig: PropTypes.string
1217+
customMarkdownLintConfig: PropTypes.string,
1218+
deleteUnusedAttachments: PropTypes.bool
11901219
}
11911220

11921221
CodeEditor.defaultProps = {
@@ -1200,5 +1229,7 @@ CodeEditor.defaultProps = {
12001229
autoDetect: false,
12011230
spellCheck: false,
12021231
enableMarkdownLint: DEFAULT_CONFIG.editor.enableMarkdownLint,
1203-
customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig
1232+
customMarkdownLintConfig: DEFAULT_CONFIG.editor.customMarkdownLintConfig,
1233+
prettierConfig: DEFAULT_CONFIG.editor.prettierConfig,
1234+
deleteUnusedAttachments: DEFAULT_CONFIG.editor.deleteUnusedAttachments
12041235
}

browser/components/MarkdownEditor.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,15 @@ class MarkdownEditor extends React.Component {
169169
.split('\n')
170170

171171
const targetLine = lines[lineIndex]
172+
let newLine = targetLine
172173

173174
if (targetLine.match(checkedMatch)) {
174-
lines[lineIndex] = targetLine.replace(checkReplace, '[ ]')
175+
newLine = targetLine.replace(checkReplace, '[ ]')
175176
}
176177
if (targetLine.match(uncheckedMatch)) {
177-
lines[lineIndex] = targetLine.replace(uncheckReplace, '[x]')
178+
newLine = targetLine.replace(uncheckReplace, '[x]')
178179
}
179-
this.refs.code.setValue(lines.join('\n'))
180+
this.refs.code.setLineContent(lineIndex, newLine)
180181
}
181182
}
182183

@@ -304,6 +305,7 @@ class MarkdownEditor extends React.Component {
304305
enableRulers={config.editor.enableRulers}
305306
rulers={config.editor.rulers}
306307
displayLineNumbers={config.editor.displayLineNumbers}
308+
lineWrapping
307309
matchingPairs={config.editor.matchingPairs}
308310
matchingTriples={config.editor.matchingTriples}
309311
explodingPairs={config.editor.explodingPairs}
@@ -321,6 +323,8 @@ class MarkdownEditor extends React.Component {
321323
switchPreview={config.editor.switchPreview}
322324
enableMarkdownLint={config.editor.enableMarkdownLint}
323325
customMarkdownLintConfig={config.editor.customMarkdownLintConfig}
326+
prettierConfig={config.editor.prettierConfig}
327+
deleteUnusedAttachments={config.editor.deleteUnusedAttachments}
324328
/>
325329
<MarkdownPreview styleName={this.state.status === 'PREVIEW'
326330
? 'preview'
@@ -340,6 +344,7 @@ class MarkdownEditor extends React.Component {
340344
smartArrows={config.preview.smartArrows}
341345
breaks={config.preview.breaks}
342346
sanitize={config.preview.sanitize}
347+
mermaidHTMLLabel={config.preview.mermaidHTMLLabel}
343348
ref='preview'
344349
onContextMenu={(e) => this.handleContextMenu(e)}
345350
onDoubleClick={(e) => this.handleDoubleClick(e)}

0 commit comments

Comments
 (0)