Deduplicate code

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2021-06-09 22:07:36 +02:00
parent baa4af7f30
commit 1c5c3c3258

View file

@ -21,28 +21,16 @@ export const handleUpload = (file: File, editor: Editor): void => {
const cursor = editor.getCursor()
const uploadPlaceholder = `![${i18n.t('editor.upload.uploadFile', { fileName: file.name })}]()`
const noteId = store.getState().noteDetails.id
const insertCode = (replacement: string) => {
editor.replaceRange(replacement, cursor, { line: cursor.line, ch: cursor.ch + uploadPlaceholder.length }, '+input')
}
editor.replaceRange(uploadPlaceholder, cursor, cursor, '+input')
uploadFile(noteId, file)
.then(({ link }) => {
editor.replaceRange(
`![](${link})`,
cursor,
{
line: cursor.line,
ch: cursor.ch + uploadPlaceholder.length
},
'+input'
)
insertCode(`![](${link})`)
})
.catch(() => {
editor.replaceRange(
'',
cursor,
{
line: cursor.line,
ch: cursor.ch + uploadPlaceholder.length
},
'+input'
)
.catch((error) => {
console.error('error while uploading file', error)
insertCode('')
})
}