-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add drag&drop zone to upload FS drafts
- Loading branch information
1 parent
abfe888
commit 6195bb1
Showing
4 changed files
with
74 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
app/javascript/controllers/fs_message_drafts_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
connect() { | ||
const dropzone = document.getElementById('dropzone'); | ||
const fileInput = document.getElementById('content[]'); | ||
const fileList = document.getElementById('fileList'); | ||
|
||
dropzone.addEventListener('dragover', (e) => { | ||
e.preventDefault(); | ||
dropzone.classList.add('border-blue-500', 'border-2'); | ||
}); | ||
|
||
dropzone.addEventListener('dragleave', () => { | ||
dropzone.classList.remove('border-blue-500', 'border-2'); | ||
}); | ||
|
||
dropzone.addEventListener('drop', (e) => { | ||
e.preventDefault(); | ||
dropzone.classList.remove('border-blue-500', 'border-2'); | ||
|
||
const files = e.dataTransfer.files; | ||
this.handleFiles(files); | ||
}); | ||
|
||
fileInput.addEventListener('change', (e) => { | ||
const files = e.target.files; | ||
this.handleFiles(files); | ||
}); | ||
} | ||
|
||
handleFiles(files) { | ||
fileList.innerHTML = ''; | ||
|
||
for (const file of files) { | ||
const listItem = document.createElement('div'); | ||
listItem.textContent = `${file.name} (${this.formatBytes(file.size)})`; | ||
fileList.appendChild(listItem); | ||
} | ||
} | ||
|
||
formatBytes(bytes) { | ||
if (bytes === 0) return '0 Bytes'; | ||
|
||
const k = 1024; | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | ||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters