Skip to content

Commit aec79c4

Browse files
authored
Merge pull request #2591 from daiyam/drop-image-preview
drag image into preview
2 parents 5f385e4 + 3679fbe commit aec79c4

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

browser/components/MarkdownEditor.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import MarkdownPreview from 'browser/components/MarkdownPreview'
77
import eventEmitter from 'browser/main/lib/eventEmitter'
88
import { findStorage } from 'browser/lib/findStorage'
99
import ConfigManager from 'browser/main/lib/ConfigManager'
10+
import attachmentManagement from 'browser/main/lib/dataApi/attachmentManagement'
1011

1112
class MarkdownEditor extends React.Component {
1213
constructor (props) {
@@ -221,6 +222,28 @@ class MarkdownEditor extends React.Component {
221222
this.refs.code.editor.replaceSelection(`${mdElement}${this.refs.code.editor.getSelection()}${mdElement}`)
222223
}
223224

225+
handleDropImage (dropEvent) {
226+
dropEvent.preventDefault()
227+
const { storageKey, noteKey } = this.props
228+
229+
this.setState({
230+
status: 'CODE'
231+
}, () => {
232+
this.refs.code.focus()
233+
234+
this.refs.code.editor.execCommand('goDocEnd')
235+
this.refs.code.editor.execCommand('goLineEnd')
236+
this.refs.code.editor.execCommand('newlineAndIndent')
237+
238+
attachmentManagement.handleAttachmentDrop(
239+
this.refs.code,
240+
storageKey,
241+
noteKey,
242+
dropEvent
243+
)
244+
})
245+
}
246+
224247
handleKeyUp (e) {
225248
const keyPressed = this.state.keyPressed
226249
keyPressed.delete(e.keyCode)
@@ -315,6 +338,7 @@ class MarkdownEditor extends React.Component {
315338
customCSS={config.preview.customCSS}
316339
allowCustomCSS={config.preview.allowCustomCSS}
317340
lineThroughCheckbox={config.preview.lineThroughCheckbox}
341+
onDrop={(e) => this.handleDropImage(e)}
318342
/>
319343
</div>
320344
)

browser/components/MarkdownPreview.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export default class MarkdownPreview extends React.Component {
210210
this.saveAsHtmlHandler = () => this.handleSaveAsHtml()
211211
this.printHandler = () => this.handlePrint()
212212

213-
this.linkClickHandler = this.handlelinkClick.bind(this)
213+
this.linkClickHandler = this.handleLinkClick.bind(this)
214214
this.initMarkdown = this.initMarkdown.bind(this)
215215
this.initMarkdown()
216216
}
@@ -413,6 +413,8 @@ export default class MarkdownPreview extends React.Component {
413413
}
414414

415415
componentDidMount () {
416+
const { onDrop } = this.props
417+
416418
this.refs.root.setAttribute('sandbox', 'allow-scripts')
417419
this.refs.root.contentWindow.document.body.addEventListener(
418420
'contextmenu',
@@ -450,7 +452,7 @@ export default class MarkdownPreview extends React.Component {
450452
)
451453
this.refs.root.contentWindow.document.addEventListener(
452454
'drop',
453-
this.preventImageDroppedHandler
455+
onDrop || this.preventImageDroppedHandler
454456
)
455457
this.refs.root.contentWindow.document.addEventListener(
456458
'dragover',
@@ -467,6 +469,8 @@ export default class MarkdownPreview extends React.Component {
467469
}
468470

469471
componentWillUnmount () {
472+
const { onDrop } = this.props
473+
470474
this.refs.root.contentWindow.document.body.removeEventListener(
471475
'contextmenu',
472476
this.contextMenuHandler
@@ -485,7 +489,7 @@ export default class MarkdownPreview extends React.Component {
485489
)
486490
this.refs.root.contentWindow.document.removeEventListener(
487491
'drop',
488-
this.preventImageDroppedHandler
492+
onDrop || this.preventImageDroppedHandler
489493
)
490494
this.refs.root.contentWindow.document.removeEventListener(
491495
'dragover',
@@ -840,7 +844,7 @@ export default class MarkdownPreview extends React.Component {
840844
return new window.Notification(title, options)
841845
}
842846

843-
handlelinkClick (e) {
847+
handleLinkClick (e) {
844848
e.preventDefault()
845849
e.stopPropagation()
846850

0 commit comments

Comments
 (0)