mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-14 02:06:29 +00:00
feat(editor): extract pasting url method
This commit is contained in:
@@ -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,33 +252,37 @@ export default class CodeEditor extends React.Component {
|
|||||||
const imageMd = `})`
|
const imageMd = `})`
|
||||||
this.insertImageMd(imageMd)
|
this.insertImageMd(imageMd)
|
||||||
}
|
}
|
||||||
} else if (this.props.fetchUrlTitle && isURL(pasteTxt)) {
|
} else if (this.props.fetchUrlTitle && isURL(pastedTxt)) {
|
||||||
e.preventDefault()
|
this.handlePasteUrl(e, editor, pastedTxt)
|
||||||
const taggedUrl = '[' + pasteTxt + ']'
|
|
||||||
editor.replaceSelection(taggedUrl)
|
|
||||||
|
|
||||||
fetch(pasteTxt, {
|
|
||||||
method: 'get'
|
|
||||||
}).then((response) => {
|
|
||||||
return (response.text())
|
|
||||||
}).then((response) => {
|
|
||||||
const parsedResponse = (new window.DOMParser()).parseFromString(response, 'text/html')
|
|
||||||
const value = editor.getValue()
|
|
||||||
const cursor = editor.getCursor()
|
|
||||||
const LinkWithTitle = `[${parsedResponse.title}](${pasteTxt})`
|
|
||||||
const newValue = value.replace(taggedUrl, LinkWithTitle)
|
|
||||||
editor.setValue(newValue)
|
|
||||||
editor.setCursor(cursor)
|
|
||||||
}).catch((e) => {
|
|
||||||
const value = editor.getValue()
|
|
||||||
const newValue = value.replace(taggedUrl, pasteTxt)
|
|
||||||
const cursor = editor.getCursor()
|
|
||||||
editor.setValue(newValue)
|
|
||||||
editor.setCursor(cursor)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handlePasteUrl(e, editor, pastedTxt){
|
||||||
|
e.preventDefault()
|
||||||
|
const taggedUrl = '[' + pastedTxt + ']'
|
||||||
|
editor.replaceSelection(taggedUrl)
|
||||||
|
|
||||||
|
fetch(pastedTxt, {
|
||||||
|
method: 'get'
|
||||||
|
}).then((response) => {
|
||||||
|
return (response.text())
|
||||||
|
}).then((response) => {
|
||||||
|
const parsedResponse = (new window.DOMParser()).parseFromString(response, 'text/html')
|
||||||
|
const value = editor.getValue()
|
||||||
|
const cursor = editor.getCursor()
|
||||||
|
const LinkWithTitle = `[${parsedResponse.title}](${pastedTxt})`
|
||||||
|
const newValue = value.replace(taggedUrl, LinkWithTitle)
|
||||||
|
editor.setValue(newValue)
|
||||||
|
editor.setCursor(cursor)
|
||||||
|
}).catch((e) => {
|
||||||
|
const value = editor.getValue()
|
||||||
|
const newValue = value.replace(taggedUrl, pastedTxt)
|
||||||
|
const cursor = editor.getCursor()
|
||||||
|
editor.setValue(newValue)
|
||||||
|
editor.setCursor(cursor)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
const { className, fontSize } = this.props
|
const { className, fontSize } = this.props
|
||||||
let fontFamily = this.props.fontFamily
|
let fontFamily = this.props.fontFamily
|
||||||
|
|||||||
Reference in New Issue
Block a user