import { FloatingWindow } from './ui/window.js';
import { logger } from './utils/logger.js';
class ObjectStudioPlugin {
constructor() {
this.win = null;
this.uploadedPath = '';
}
open() {
if (this.win) {
this.win.bringToFront();
return;
}
this.win = new FloatingWindow('studio', t('object_studio'), {
width: 700,
height: 500,
icon: 'fas fa-magic'
});
const content = `
`;
this.win.render(content);
this.initEventListeners();
}
initEventListeners() {
const el = this.win.element;
const fileInput = el.querySelector('#studio-file');
const uploadBtn = el.querySelector('#studio-upload-btn');
const saveBtn = el.querySelector('#studio-save');
const cancelBtn = el.querySelector('#studio-cancel');
cancelBtn.addEventListener('click', () => this.win.destroy());
this.win.onClose = () => { this.win = null; };
uploadBtn.addEventListener('click', () => this.handleUpload(fileInput));
saveBtn.addEventListener('click', () => this.handleSave());
}
async handleUpload(fileInput) {
const file = fileInput.files[0];
if (!file) return;
const formData = new FormData();
formData.append('file', file);
try {
const res = await fetch('/api/studio/upload', { method: 'POST', body: formData });
const data = await res.json();
if (data.success) {
this.uploadedPath = data.path;
const preview = this.win.element.querySelector('#studio-preview-icon');
preview.innerHTML = `
`;
preview.querySelector('img').style.transform = 'scale(0.8)';
setTimeout(() => { preview.querySelector('img').style.transform = 'scale(1)'; }, 10);
}
} catch (err) { logger.critical(err); }
}
async handleSave() {
const id = document.getElementById('studio-id').value.trim();
const label = document.getElementById('studio-label').value.trim();
const category = document.getElementById('studio-category').value;
if (!id || !label || !this.uploadedPath) {
alert("Please fill all required fields.");
return;
}
const newAsset = { id, type: label, label, category, path: this.uploadedPath, format: this.uploadedPath.split('.').pop() };
try {
const res = await fetch('/api/studio/add', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(newAsset)
});
const data = await res.json();
if (data.success) {
initAssets();
this.win.destroy();
}
} catch (err) { logger.critical(err); }
}
}
export const studioPlugin = new ObjectStudioPlugin();
export const openStudioModal = () => studioPlugin.open();