mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 17:56:25 +00:00
Create a image tag when pasting image url
This commit is contained in:
@@ -312,24 +312,57 @@ export default class CodeEditor extends React.Component {
|
|||||||
const taggedUrl = `<${pastedTxt}>`
|
const taggedUrl = `<${pastedTxt}>`
|
||||||
editor.replaceSelection(taggedUrl)
|
editor.replaceSelection(taggedUrl)
|
||||||
|
|
||||||
|
const isImageReponse = (response) => {
|
||||||
|
return response.headers.has('content-type')
|
||||||
|
&& response.headers.get('content-type').match(/^image\/.+$/)
|
||||||
|
}
|
||||||
|
const replaceTaggedUrl = (replacement) => {
|
||||||
|
const value = editor.getValue()
|
||||||
|
const cursor = editor.getCursor()
|
||||||
|
const newValue = value.replace(taggedUrl, replacement)
|
||||||
|
editor.setValue(newValue)
|
||||||
|
editor.setCursor(cursor)
|
||||||
|
}
|
||||||
|
|
||||||
fetch(pastedTxt, {
|
fetch(pastedTxt, {
|
||||||
method: 'get'
|
method: 'get'
|
||||||
}).then((response) => {
|
}).then((response) => {
|
||||||
return this.decodeResponse(response)
|
if (isImageReponse(response)) {
|
||||||
}).then((response) => {
|
return this.mapImageResponse(response, pastedTxt)
|
||||||
const parsedResponse = (new window.DOMParser()).parseFromString(response, 'text/html')
|
} else {
|
||||||
const value = editor.getValue()
|
return this.mapNormalResponse(response, pastedTxt)
|
||||||
const cursor = editor.getCursor()
|
}
|
||||||
const LinkWithTitle = `[${parsedResponse.title}](${pastedTxt})`
|
}).then((replacement) => {
|
||||||
const newValue = value.replace(taggedUrl, LinkWithTitle)
|
replaceTaggedUrl(replacement)
|
||||||
editor.setValue(newValue)
|
|
||||||
editor.setCursor(cursor)
|
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
const value = editor.getValue()
|
replaceTaggedUrl(pastedTxt)
|
||||||
const newValue = value.replace(taggedUrl, pastedTxt)
|
})
|
||||||
const cursor = editor.getCursor()
|
}
|
||||||
editor.setValue(newValue)
|
|
||||||
editor.setCursor(cursor)
|
mapNormalResponse (response, pastedTxt) {
|
||||||
|
return this.decodeResponse(response).then((body) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const parsedBody = (new window.DOMParser()).parseFromString(body, 'text/html')
|
||||||
|
const linkWithTitle = `[${parsedBody.title}](${pastedTxt})`
|
||||||
|
resolve(linkWithTitle)
|
||||||
|
} catch (e) {
|
||||||
|
reject(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
mapImageResponse (response, pastedTxt) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const url = response.url
|
||||||
|
const name = url.substring(url.lastIndexOf('/') + 1)
|
||||||
|
const imageLinkWithName = ``
|
||||||
|
resolve(imageLinkWithName)
|
||||||
|
} catch (e) {
|
||||||
|
reject(e)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user