1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-14 10:16:26 +00:00

feat(editor): extract pasting url method

This commit is contained in:
Georges Indrianjafy
2018-02-05 10:59:36 +02:00
parent 3e2b876c59
commit d9d46cda1c

View File

@@ -228,7 +228,7 @@ export default class CodeEditor extends React.Component {
handlePaste (editor, e) { handlePaste (editor, e) {
const clipboardData = e.clipboardData const clipboardData = e.clipboardData
const dataTransferItem = clipboardData.items[0] const dataTransferItem = clipboardData.items[0]
const pasteTxt = 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)
@@ -252,12 +252,17 @@ export default class CodeEditor extends React.Component {
const imageMd = `![${imageName}](${path.join('/:storage', `${imageName}.png`)})` const imageMd = `![${imageName}](${path.join('/:storage', `${imageName}.png`)})`
this.insertImageMd(imageMd) this.insertImageMd(imageMd)
} }
} else if (this.props.fetchUrlTitle && isURL(pasteTxt)) { } else if (this.props.fetchUrlTitle && isURL(pastedTxt)) {
this.handlePasteUrl(e, editor, pastedTxt)
}
}
handlePasteUrl(e, editor, pastedTxt){
e.preventDefault() e.preventDefault()
const taggedUrl = '[' + pasteTxt + ']' const taggedUrl = '[' + pastedTxt + ']'
editor.replaceSelection(taggedUrl) editor.replaceSelection(taggedUrl)
fetch(pasteTxt, { fetch(pastedTxt, {
method: 'get' method: 'get'
}).then((response) => { }).then((response) => {
return (response.text()) return (response.text())
@@ -265,19 +270,18 @@ export default class CodeEditor extends React.Component {
const parsedResponse = (new window.DOMParser()).parseFromString(response, 'text/html') const parsedResponse = (new window.DOMParser()).parseFromString(response, 'text/html')
const value = editor.getValue() const value = editor.getValue()
const cursor = editor.getCursor() const cursor = editor.getCursor()
const LinkWithTitle = `[${parsedResponse.title}](${pasteTxt})` const LinkWithTitle = `[${parsedResponse.title}](${pastedTxt})`
const newValue = value.replace(taggedUrl, LinkWithTitle) const newValue = value.replace(taggedUrl, LinkWithTitle)
editor.setValue(newValue) editor.setValue(newValue)
editor.setCursor(cursor) editor.setCursor(cursor)
}).catch((e) => { }).catch((e) => {
const value = editor.getValue() const value = editor.getValue()
const newValue = value.replace(taggedUrl, pasteTxt) const newValue = value.replace(taggedUrl, pastedTxt)
const cursor = editor.getCursor() const cursor = editor.getCursor()
editor.setValue(newValue) editor.setValue(newValue)
editor.setCursor(cursor) editor.setCursor(cursor)
}) })
} }
}
render () { render () {
const { className, fontSize } = this.props const { className, fontSize } = this.props