UPdate.
This commit is contained in:
@@ -50,28 +50,32 @@ class CodeEditorView extends HTMLElement {
|
||||
createUI(content) {
|
||||
this.innerHTML = `
|
||||
<div class="code-editor-overlay">
|
||||
<div class="code-editor-container">
|
||||
<div class="code-editor-header">
|
||||
<div class="header-left">
|
||||
<button class="button" id="back-btn">Back</button>
|
||||
<h2 class="editor-filename">${this.escapeHtml(this.file.name)}</h2>
|
||||
</div>
|
||||
<div class="header-right">
|
||||
<button class="button button-primary" id="save-btn">Save & Close</button>
|
||||
<div class="code-editor-header">
|
||||
<div class="header-left">
|
||||
<button class="button" id="back-btn">Back</button>
|
||||
<div class="preview-info">
|
||||
<h2 class="preview-file-name">${this.escapeHtml(this.file.name)}</h2>
|
||||
<p class="preview-file-info">${this.formatFileSize(this.file.size)} • ${new Date(this.file.created_at).toLocaleDateString()}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="code-editor-body">
|
||||
<textarea id="editor-textarea"></textarea>
|
||||
<div class="preview-actions">
|
||||
<button class="button" id="download-btn">Download</button>
|
||||
<button class="button button-primary" id="save-btn">Save & Close</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="code-editor-body">
|
||||
<textarea id="editor-textarea"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
const backBtn = this.querySelector('#back-btn');
|
||||
const saveBtn = this.querySelector('#save-btn');
|
||||
const downloadBtn = this.querySelector('#download-btn');
|
||||
|
||||
backBtn.addEventListener('click', () => this.close());
|
||||
saveBtn.addEventListener('click', () => this.save());
|
||||
downloadBtn.addEventListener('click', () => this.downloadFile());
|
||||
|
||||
document.addEventListener('keydown', this.handleKeydown.bind(this));
|
||||
}
|
||||
@@ -140,6 +144,33 @@ class CodeEditorView extends HTMLElement {
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
async downloadFile() {
|
||||
try {
|
||||
const blob = await api.downloadFile(this.file.id);
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = this.file.name;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
document.body.removeChild(a);
|
||||
} catch (error) {
|
||||
console.error('Failed to download file:', error);
|
||||
}
|
||||
}
|
||||
|
||||
formatFileSize(bytes) {
|
||||
const units = ['B', 'KB', 'MB', 'GB'];
|
||||
let size = bytes;
|
||||
let unitIndex = 0;
|
||||
while (size >= 1024 && unitIndex < units.length - 1) {
|
||||
size /= 1024;
|
||||
unitIndex++;
|
||||
}
|
||||
return `${size.toFixed(1)} ${units[unitIndex]}`;
|
||||
}
|
||||
|
||||
handleKeydown(e) {
|
||||
if (e.key === 'Escape' && !this.editor.getOption('readOnly')) {
|
||||
this.close();
|
||||
|
||||
Reference in New Issue
Block a user