mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-23 11:37:02 -04: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 cursor = editor.getCursor()
|
||||||
const uploadPlaceholder = `![${i18n.t('editor.upload.uploadFile', { fileName: file.name })}]()`
|
const uploadPlaceholder = `![${i18n.t('editor.upload.uploadFile', { fileName: file.name })}]()`
|
||||||
const noteId = store.getState().noteDetails.id
|
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')
|
editor.replaceRange(uploadPlaceholder, cursor, cursor, '+input')
|
||||||
uploadFile(noteId, file)
|
uploadFile(noteId, file)
|
||||||
.then(({ link }) => {
|
.then(({ link }) => {
|
||||||
editor.replaceRange(
|
insertCode(``)
|
||||||
``,
|
|
||||||
cursor,
|
|
||||||
{
|
|
||||||
line: cursor.line,
|
|
||||||
ch: cursor.ch + uploadPlaceholder.length
|
|
||||||
},
|
|
||||||
'+input'
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch((error) => {
|
||||||
editor.replaceRange(
|
console.error('error while uploading file', error)
|
||||||
'',
|
insertCode('')
|
||||||
cursor,
|
|
||||||
{
|
|
||||||
line: cursor.line,
|
|
||||||
ch: cursor.ch + uploadPlaceholder.length
|
|
||||||
},
|
|
||||||
'+input'
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue