mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-12-24 17:52:50 +00:00
Deduplicate code
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
baa4af7f30
commit
1c5c3c3258
1 changed files with 7 additions and 19 deletions
|
@ -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('')
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue