1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

fix pasting into fenced code block

This commit is contained in:
Baptiste Augrain
2018-11-25 14:55:29 +01:00
parent a6eddb5798
commit bf288fdaeb

View File

@@ -562,14 +562,17 @@ export default class CodeEditor extends React.Component {
} }
handlePaste (editor, e) { handlePaste (editor, e) {
const { storageKey, noteKey, fetchUrlTitle } = this.props
const clipboardData = e.clipboardData const clipboardData = e.clipboardData
const { storageKey, noteKey } = this.props
const dataTransferItem = clipboardData.items[0] const dataTransferItem = clipboardData.items[0]
const pastedTxt = clipboardData.getData('text') const pastedTxt = clipboardData.getData('text')
const isURL = str => { const isURL = str => {
const matcher = /^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/ const matcher = /^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/
return matcher.test(str) return matcher.test(str)
} }
const isInLinkTag = editor => { const isInLinkTag = editor => {
const startCursor = editor.getCursor('start') const startCursor = editor.getCursor('start')
const prevChar = editor.getRange( const prevChar = editor.getRange(
@@ -584,30 +587,69 @@ export default class CodeEditor extends React.Component {
return prevChar === '](' && nextChar === ')' return prevChar === '](' && nextChar === ')'
} }
const pastedHtml = clipboardData.getData('text/html') const isInFencedCodeBlock = editor => {
if (pastedHtml !== '') { const cursor = editor.getCursor()
this.handlePasteHtml(e, editor, pastedHtml)
} else if (dataTransferItem.type.match('image')) { let token = editor.getTokenAt(cursor)
attachmentManagement.handlePastImageEvent( if (token.state.fencedState) {
this, return true
storageKey, }
noteKey,
dataTransferItem let line = line = cursor.line - 1
) while (line >= 0) {
} else if ( token = editor.getTokenAt({
this.props.fetchUrlTitle && ch: 3,
isURL(pastedTxt) && line
!isInLinkTag(editor) })
) {
this.handlePasteUrl(e, editor, pastedTxt) if (token.start === token.end) {
--line
} else if (token.type === 'comment') {
if (line > 0) {
token = editor.getTokenAt({
ch: 3,
line: line - 1
})
return token.type !== 'comment'
} else {
return true
}
return true
} else {
return false
}
}
return false
} }
if (isInFencedCodeBlock(editor)) {
this.handlePasteText(e, editor, pastedTxt)
} else {
const pastedHtml = clipboardData.getData('text/html')
if (pastedHtml !== '') {
this.handlePasteHtml(e, editor, pastedHtml)
} else if (dataTransferItem.type.match('image')) {
attachmentManagement.handlePastImageEvent(
this,
storageKey,
noteKey,
dataTransferItem
)
} else if (fetchUrlTitle && isURL(pastedTxt) && !isInLinkTag(editor)) {
this.handlePasteUrl(e, editor, pastedTxt)
}
}
if (attachmentManagement.isAttachmentLink(pastedTxt)) { if (attachmentManagement.isAttachmentLink(pastedTxt)) {
e.preventDefault()
attachmentManagement attachmentManagement
.handleAttachmentLinkPaste(storageKey, noteKey, pastedTxt) .handleAttachmentLinkPaste(storageKey, noteKey, pastedTxt)
.then(modifiedText => { .then(modifiedText => {
this.editor.replaceSelection(modifiedText) this.editor.replaceSelection(modifiedText)
}) })
e.preventDefault()
} }
} }
@@ -663,6 +705,12 @@ export default class CodeEditor extends React.Component {
editor.replaceSelection(markdown) editor.replaceSelection(markdown)
} }
handlePasteText (e, editor, pastedTxt) {
e.preventDefault()
editor.replaceSelection(pastedTxt)
}
mapNormalResponse (response, pastedTxt) { mapNormalResponse (response, pastedTxt) {
return this.decodeResponse(response).then(body => { return this.decodeResponse(response).then(body => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {