1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| async function listenPaste(e) { let clipboardData = e.clipboardData if (clipboardData) { const arr = Array.from(clipboardData.files) for (let i = 0; i < arr.length; i++) { const item = arr[i] let blob = await fileToBlob(item) fileReader({ type: item.type }, blob) } if(arr.length > 0) return
e.stopPropagation(); e.preventDefault(); var text = '', event = (e.originalEvent || e); if (event.clipboardData && event.clipboardData.getData) { text = event.clipboardData.getData('text/plain'); } else if (window.clipboardData && window.clipboardData.getData) { text = window.clipboardData.getData('Text'); } if (document.queryCommandSupported('insertText')) { document.execCommand('insertText', false, text); } else { document.execCommand('paste', false, text); } } }
function fileReader(item, blob) { console.log('fileReader----', item, blob) let type = 'file' if(item.type.match(/^image\//i)) { type = 'img' }else if(item.type === 'text/plain') { type = 'txt' }else if(item.type.includes('sheet')) { type = 'excel' }else if(item.type.split('.')[item.type.split('.').length - 1] === 'document') { type = 'doc' }else if(item.type === 'application/pdf') { type = 'pdf' } const id = Math.random() fileList.push({ id: id, file: blob, type: type }) let blobUrl = URL.createObjectURL(blob) let img = new Image() const url = type === 'img' ? blobUrl : blobUrl img.src = url img.setAttribute('data-id', id) img.style.width = '50px' img.style.height = '50px' img.style.objectFit = 'contain' img.onclick = function() {} const box = document.createElement('span') box.appendChild(img) document.getElementById('editor').appendChild(box) }
const imgs = document.querySelectorAll('#editor img') const imgIds = imgs.map(item => Number(item.getAttribute('data-id'))) const fileData = fileList.filter(item => imgIds.includes(Number(item.id))).map(item => item.file)
|