mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 09:45:37 -04:00
improve: Refactor use-code-mirror-file-insert-extension
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
8245a519e0
commit
d4493aed95
1 changed files with 9 additions and 9 deletions
|
@ -9,7 +9,6 @@ import { EditorView } from '@codemirror/view'
|
||||||
import type { Extension } from '@codemirror/state'
|
import type { Extension } from '@codemirror/state'
|
||||||
import { handleUpload } from '../use-handle-upload'
|
import { handleUpload } from '../use-handle-upload'
|
||||||
import { Optional } from '@mrdrogdrog/optional'
|
import { Optional } from '@mrdrogdrog/optional'
|
||||||
import type { CursorSelection } from '../../tool-bar/formatters/types/cursor-selection'
|
|
||||||
|
|
||||||
const calculateCursorPositionInEditor = (view: EditorView, event: MouseEvent): number => {
|
const calculateCursorPositionInEditor = (view: EditorView, event: MouseEvent): number => {
|
||||||
return Optional.ofNullable(event.pageX)
|
return Optional.ofNullable(event.pageX)
|
||||||
|
@ -21,14 +20,10 @@ const calculateCursorPositionInEditor = (view: EditorView, event: MouseEvent): n
|
||||||
.orElse(view.state.selection.main.head)
|
.orElse(view.state.selection.main.head)
|
||||||
}
|
}
|
||||||
|
|
||||||
const processFileList = (view: EditorView, fileList?: FileList, cursorSelection?: CursorSelection): boolean => {
|
const extractFirstFile = (fileList?: FileList): Optional<File> => {
|
||||||
return Optional.ofNullable(fileList)
|
return Optional.ofNullable(fileList)
|
||||||
.filter((files) => files.length > 0)
|
.filter((files) => files.length > 0)
|
||||||
.map((files) => {
|
.map((files) => files[0])
|
||||||
handleUpload(view, files[0], cursorSelection)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
.orElse(false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,11 +35,16 @@ export const useCodeMirrorFileInsertExtension = (): Extension => {
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
return EditorView.domEventHandlers({
|
return EditorView.domEventHandlers({
|
||||||
drop: (event, view) => {
|
drop: (event, view) => {
|
||||||
processFileList(view, event.dataTransfer?.files, { from: calculateCursorPositionInEditor(view, event) }) &&
|
extractFirstFile(event.dataTransfer?.files).ifPresent((file) => {
|
||||||
|
handleUpload(view, file, { from: calculateCursorPositionInEditor(view, event) })
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
})
|
||||||
},
|
},
|
||||||
paste: (event, view) => {
|
paste: (event, view) => {
|
||||||
processFileList(view, event.clipboardData?.files) && event.preventDefault()
|
extractFirstFile(event.clipboardData?.files).ifPresent((file) => {
|
||||||
|
handleUpload(view, file)
|
||||||
|
event.preventDefault()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}, [])
|
}, [])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue