feat(editor): add uploading an image on save
This commit is contained in:
parent
76d31c84ad
commit
4f2d7b3ce2
@ -79,7 +79,7 @@ const tiptapRegex = new RegExp(`${TIPTAP_TEXT_VALUE_PREFIX}`, 's')
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import {ref, watch, onBeforeUnmount, nextTick} from 'vue'
|
import {ref, watch, onBeforeUnmount, nextTick, onMounted} from 'vue'
|
||||||
import {marked} from 'marked'
|
import {marked} from 'marked'
|
||||||
import {refDebounced} from '@vueuse/core'
|
import {refDebounced} from '@vueuse/core'
|
||||||
|
|
||||||
@ -281,6 +281,19 @@ onBeforeUnmount(() => editor.value?.destroy())
|
|||||||
|
|
||||||
const uploadInputRef = ref<HTMLInputElement | null>(null)
|
const uploadInputRef = ref<HTMLInputElement | null>(null)
|
||||||
|
|
||||||
|
function uploadAndInsertFiles(files: File[] | FileList) {
|
||||||
|
uploadCallback(files).then(urls => {
|
||||||
|
urls?.forEach(url => {
|
||||||
|
editor.value
|
||||||
|
?.chain()
|
||||||
|
.focus()
|
||||||
|
.setImage({src: url})
|
||||||
|
.run()
|
||||||
|
})
|
||||||
|
onImageAdded()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function addImage() {
|
function addImage() {
|
||||||
|
|
||||||
if (typeof uploadCallback !== 'undefined') {
|
if (typeof uploadCallback !== 'undefined') {
|
||||||
@ -290,16 +303,7 @@ function addImage() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadCallback(files).then(urls => {
|
uploadAndInsertFiles(files)
|
||||||
urls.forEach(url => {
|
|
||||||
editor.value
|
|
||||||
?.chain()
|
|
||||||
.focus()
|
|
||||||
.setImage({src: url})
|
|
||||||
.run()
|
|
||||||
})
|
|
||||||
onImageAdded()
|
|
||||||
})
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -341,6 +345,23 @@ function setLink() {
|
|||||||
.setLink({href: url, target: '_blank'})
|
.setLink({href: url, target: '_blank'})
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
document.addEventListener('paste', handleImagePaste)
|
||||||
|
})
|
||||||
|
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
document.removeEventListener('paste', handleImagePaste)
|
||||||
|
})
|
||||||
|
|
||||||
|
function handleImagePaste(event) {
|
||||||
|
event.preventDefault()
|
||||||
|
event?.clipboardData?.items?.forEach(i => {
|
||||||
|
if (i.kind === 'file' && i.type.startsWith('image/')) {
|
||||||
|
uploadAndInsertFiles([i.getAsFile()])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user