Skip to content

Commit b1d2c25

Browse files
committed
when dropping an image, switch to editor and add it at the end of the file
1 parent fdb1ef5 commit b1d2c25

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) {
@@ -219,6 +220,28 @@ class MarkdownEditor extends React.Component {
219220
this.refs.code.editor.replaceSelection(`${mdElement}${this.refs.code.editor.getSelection()}${mdElement}`)
220221
}
221222

223+
handleDropImage (dropEvent) {
224+
dropEvent.preventDefault()
225+
const { storageKey, noteKey } = this.props
226+
227+
this.setState({
228+
status: 'CODE'
229+
}, () => {
230+
this.refs.code.focus()
231+
232+
this.refs.code.editor.execCommand('goDocEnd')
233+
this.refs.code.editor.execCommand('goLineEnd')
234+
this.refs.code.editor.execCommand('newlineAndIndent')
235+
236+
attachmentManagement.handleAttachmentDrop(
237+
this.refs.code,
238+
storageKey,
239+
noteKey,
240+
dropEvent
241+
)
242+
})
243+
}
244+
222245
handleKeyUp (e) {
223246
const keyPressed = this.state.keyPressed
224247
keyPressed.delete(e.keyCode)
@@ -308,6 +331,7 @@ class MarkdownEditor extends React.Component {
308331
customCSS={config.preview.customCSS}
309332
allowCustomCSS={config.preview.allowCustomCSS}
310333
lineThroughCheckbox={config.preview.lineThroughCheckbox}
334+
onDrop={(e) => this.handleDropImage(e)}
311335
/>
312336
</div>
313337
)

browser/components/MarkdownPreview.js

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

209-
this.linkClickHandler = this.handlelinkClick.bind(this)
209+
this.linkClickHandler = this.handleLinkClick.bind(this)
210210
this.initMarkdown = this.initMarkdown.bind(this)
211211
this.initMarkdown()
212212
}
@@ -438,6 +438,8 @@ export default class MarkdownPreview extends React.Component {
438438
}
439439

440440
componentDidMount () {
441+
const { onDrop } = this.props
442+
441443
this.refs.root.setAttribute('sandbox', 'allow-scripts')
442444
this.refs.root.contentWindow.document.body.addEventListener(
443445
'contextmenu',
@@ -475,7 +477,7 @@ export default class MarkdownPreview extends React.Component {
475477
)
476478
this.refs.root.contentWindow.document.addEventListener(
477479
'drop',
478-
this.preventImageDroppedHandler
480+
onDrop || this.preventImageDroppedHandler
479481
)
480482
this.refs.root.contentWindow.document.addEventListener(
481483
'dragover',
@@ -492,6 +494,8 @@ export default class MarkdownPreview extends React.Component {
492494
}
493495

494496
componentWillUnmount () {
497+
const { onDrop } = this.props
498+
495499
this.refs.root.contentWindow.document.body.removeEventListener(
496500
'contextmenu',
497501
this.contextMenuHandler
@@ -510,7 +514,7 @@ export default class MarkdownPreview extends React.Component {
510514
)
511515
this.refs.root.contentWindow.document.removeEventListener(
512516
'drop',
513-
this.preventImageDroppedHandler
517+
onDrop || this.preventImageDroppedHandler
514518
)
515519
this.refs.root.contentWindow.document.removeEventListener(
516520
'dragover',
@@ -837,7 +841,7 @@ export default class MarkdownPreview extends React.Component {
837841
return new window.Notification(title, options)
838842
}
839843

840-
handlelinkClick (e) {
844+
handleLinkClick (e) {
841845
e.preventDefault()
842846
e.stopPropagation()
843847

0 commit comments

Comments
 (0)