Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/components/NewMessageForm/AdvancedInput/AdvancedInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import At from 'vue-at'
import VueAtReparenter from '../../../mixins/vueAtReparenter'
import { EventBus } from '../../../services/EventBus'
import { searchPossibleMentions } from '../../../services/mentionsService'
import { fetchClipboardContent } from '../../../utils/clipboard'
import Avatar from '@nextcloud/vue/dist/Components/Avatar'
import Mention from '../../MessagesList/MessagesGroup/Message/MessagePart/Mention'
import escapeHtml from 'escape-html'
Expand Down Expand Up @@ -228,9 +229,16 @@ export default {
methods: {
onPaste(e) {
e.preventDefault()
const text = e.clipboardData.getData('text/plain')
const div = document.createElement('div').innerText = escapeHtml(text)
document.execCommand('insertHtml', false, div)

const content = fetchClipboardContent(e)

if (content.kind === 'file') {
this.$emit('files-pasted', content.files)
} else if (content.kind === 'text') {
const text = content.text
const div = document.createElement('div').innerText = escapeHtml(text)
document.execCommand('insertHtml', false, div)
}
},

/**
Expand Down
13 changes: 12 additions & 1 deletion src/components/NewMessageForm/NewMessageForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
v-model="text"
:token="token"
@update:contentEditable="contentEditableToParsed"
@submit="handleSubmit" />
@submit="handleSubmit"
@files-pasted="handleFiles" />
</div>
<button
type="submit"
Expand Down Expand Up @@ -283,6 +284,16 @@ export default {

handleFileInput(event) {
const files = Object.values(event.target.files)

this.handleFiles(files)
},

/**
* Handles files pasting event.
*
* @param {File[] | FileList} files pasted files list
*/
handleFiles(files) {
// Create a unique id for the upload operation
const uploadId = new Date().getTime()
// Uploads and shares the files
Expand Down
45 changes: 45 additions & 0 deletions src/utils/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @typedef {Object} ClipboardContent
* @property {('file'|'text'|'none')} kind content type (file or text)
* @property {File[] | FileList | undefined} files files array
* @property {String | undefined} text text content
*/

/**
* Fetches the clipboard content from the event.
*
* @param {ClipboardEvent} event native event
* @returns {ClipboardContent}
*/
const fetchClipboardContent = function(event) {
const clipboardData = event.clipboardData

if (!clipboardData) {
return { kind: 'none' }
}

if (clipboardData.files && clipboardData.files.length > 0) {
return { kind: 'file', files: clipboardData.files }
}

if (clipboardData.items && clipboardData.items.length > 0) {
const files = []
for (let i = 0; i < clipboardData.items.length; i++) {
if (clipboardData.items[i].kind === 'file') {
files.push(clipboardData.items[i].getAsFile())
}
}

if (files.length > 0) {
return { kind: 'file', files: files }
}
}

const text = clipboardData.getData('text/plain')

return { kind: 'text', text: text }
}

export {
fetchClipboardContent
}