-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathimage_upload_controller.js
More file actions
213 lines (176 loc) · 7.33 KB
/
Copy pathimage_upload_controller.js
File metadata and controls
213 lines (176 loc) · 7.33 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = [ "file", "picker", "content", "previewList", "previewTemplate", "fileInputs" ]
connect() {
if (!this.hasFileTarget || !this.hasContentTarget) {
console.log("image-upload controller is skipping initialization because a target is missing")
return
}
this.entries = []
this.nextEntryId = 0
this.nextCloneIndex = 1
this.dragCounter = 0
this.fileTarget.addEventListener("change", this.boundPickerChanged)
this.pickerTarget.addEventListener("change", this.boundPickerChanged)
this.element.addEventListener("drop", this.boundDropped)
this.contentTarget.addEventListener("paste", this.boundPasted)
this.element.addEventListener("dragenter", this.boundDragEnter)
this.element.addEventListener("dragover", this.boundDragOver)
this.element.addEventListener("dragleave", this.boundDragLeave)
}
disconnect() {
if (!this.hasFileTarget || !this.hasContentTarget) return
this.fileTarget.removeEventListener("change", this.boundPickerChanged)
this.pickerTarget.removeEventListener("change", this.boundPickerChanged)
this.element.removeEventListener("drop", this.boundDropped)
this.contentTarget.removeEventListener("paste", this.boundPasted)
this.element.removeEventListener("dragenter", this.boundDragEnter)
this.element.removeEventListener("dragover", this.boundDragOver)
this.element.removeEventListener("dragleave", this.boundDragLeave)
}
// The picker is a plain, unnamed <input multiple> used only to trigger the OS file dialog —
// it's never part of the submitted form. Each file it returns is handed off to its own
// single-file submission input (fileTarget for the first, a clone for each additional one),
// so a real Rails-nested-attributes input is never left both "multiple" and empty, which
// browsers submit as a blank value instead of omitting entirely. fileTarget itself is also
// watched directly so attaching a file straight to it (e.g. via Capybara) still works.
boundPickerChanged = (event) => { this.pickerChanged(event) }
pickerChanged(event) {
const input = event.target
const files = Array.from(input.files || [])
if (input === this.pickerTarget) input.value = ''
files.forEach((file) => this.addFile(file))
}
addFile(file) {
const entryId = this.nextEntryId++
const usingFileTarget = !this.entries.some((entry) => entry.input === this.fileTarget)
let input
if (usingFileTarget) {
input = this.fileTarget
} else {
const cloneIndex = this.nextCloneIndex++
input = this.fileTarget.cloneNode(false)
input.removeAttribute("id")
input.name = this.fileTarget.name.replace(/\[documents_attributes\]\[\d+\]/, `[documents_attributes][${cloneIndex}]`)
this.fileInputsTarget.appendChild(input)
}
const dataTransfer = new DataTransfer()
dataTransfer.items.add(file)
input.files = dataTransfer.files
const preview = this.buildPreview(file, entryId)
this.entries.push({ entryId, file, input, preview })
}
buildPreview(file, entryId) {
const clone = this.previewTemplateTarget.content.firstElementChild.cloneNode(true)
clone.dataset.entryId = entryId
const removeBtn = clone.querySelector("[data-role='preview-remove']")
if (removeBtn) removeBtn.dataset.imageUploadIndexParam = entryId
const img = clone.querySelector("img")
const fileIcon = clone.querySelector("[data-role='file-icon']")
if (file.type.startsWith('image/')) {
const reader = new FileReader()
reader.onload = (e) => { img.src = e.target.result }
reader.readAsDataURL(file)
} else if (file.type === 'application/pdf') {
img.style.display = 'none'
if (fileIcon) {
fileIcon.style.display = 'flex'
} else {
const pdfIcon = document.createElement('div')
pdfIcon.setAttribute('data-role', 'file-icon')
pdfIcon.className = 'w-full h-full flex items-center justify-center bg-red-100 dark:bg-red-900'
pdfIcon.innerHTML = `
<svg class="w-8 h-8 text-red-600 dark:text-red-400" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M4 4a2 2 0 012-2h4.586A2 2 0 0112 2.586L15.414 6A2 2 0 0116 7.414V16a2 2 0 01-2 2H6a2 2 0 01-2-2V4zm2 6a1 1 0 011-1h6a1 1 0 110 2H7a1 1 0 01-1-1zm1 3a1 1 0 100 2h6a1 1 0 100-2H7z" clip-rule="evenodd"></path>
</svg>
`
clone.appendChild(pdfIcon)
}
}
this.previewListTarget.appendChild(clone)
this.element.classList.add("show-previews")
this.contentTarget.focus()
window.dispatchEvent(new CustomEvent('main-column-changed'))
return clone
}
removeFile(event) {
const entryId = parseInt(event.params.index, 10)
const index = this.entries.findIndex((entry) => entry.entryId === entryId)
if (index === -1) return
const entry = this.entries[index]
entry.preview.remove()
if (entry.input === this.fileTarget) {
this.fileTarget.value = ''
} else {
entry.input.remove()
}
this.entries.splice(index, 1)
if (this.entries.length === 0) this.element.classList.remove("show-previews")
if (this.hasContentTarget) this.contentTarget.focus()
window.dispatchEvent(new CustomEvent('main-column-changed'))
}
boundDropped = (event) => { this.dropped(event) }
dropped(event) {
if (!this.hasFileTarget) return
event.preventDefault()
this.dragCounter = 0
const shade = this.element.querySelector("#drag-n-drop-shade")
if (shade) shade.remove()
const files = Array.from(event.dataTransfer.files || []).filter((file) => this.isAllowedType(file))
files.forEach((file) => this.addFile(file))
}
boundDragOver = (event) => this.dragOver(event)
dragOver(event) {
event.preventDefault()
this.displayDragnDropShade()
}
boundDragLeave = (event) => this.dragLeave(event)
dragLeave(event) {
event.preventDefault()
this.dragCounter--
if (this.dragCounter <= 0) {
this.dragCounter = 0
const shade = this.element.querySelector("#drag-n-drop-shade")
if (shade) shade.remove()
}
}
boundDragEnter = (event) => this.dragEnter(event)
dragEnter(event) {
event.preventDefault()
this.dragCounter++
this.displayDragnDropShade()
}
boundPasted = async (event) => { this.pasted(event) }
async pasted(event) {
if (!this.hasFileTarget) return
const clipboardData =
event.clipboardData || event.originalEvent.clipboardData
for (const item of clipboardData.items) {
if (item.kind === "file") {
const blob = item.getAsFile()
if (!blob) continue
if (!this.isAllowedType(blob)) continue
const file = new File([blob], blob.name || this.defaultFileName(blob.type), { type: blob.type })
this.addFile(file)
}
}
}
isAllowedType(file) {
return file.type.startsWith('image/') || file.type === 'application/pdf'
}
defaultFileName(fileType) {
return fileType === 'application/pdf' ? 'pasted-document.pdf' : 'pasted-image.png'
}
displayDragnDropShade() {
const existing = this.element.querySelector("#drag-n-drop-shade")
if (existing) return
this.element.insertAdjacentHTML(
'beforeend',
'<div id="drag-n-drop-shade"></div>'
);
}
choose() {
if (!this.hasPickerTarget) return
this.pickerTarget.click()
}
}