import Quill from 'quill';
const BlockEmbed = Quill.import('blots/block/embed');
const Parchment = Quill.import('parchment');
class FileBlot extends BlockEmbed {
static blotName = 'fileCard';
static tagName = 'div';
static className = 'file-card';
static scope = Parchment.Scope.BLOCK_BLOT;
static create(value) {
const node = super.create();
//node.setAttribute('contenteditable', 'false');
node.setAttribute('data-file-name', value.name);
node.setAttribute('data-file-size', value.size);
node.setAttribute('draggable', 'true');
node.innerHTML = `
π ${value.name} (${(value.size / 1024 / 1024).toFixed(2)}MB)
`;
// ν΄λ¦ μ μ΄λ²€νΈ λ°μ
node.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
const quill = FileBlot.getQuillInstance();
if (quill) {
const uploader = quill?.getModule('fileUploader');
// 첨λΆνμΌ νμ±ν ν΄μ
uploader.clearActiveCards();
// μλν° μ»€μ νμ ν΄μ
quill.root.blur();
}
node.classList.add('active');
});
// μμ λ²νΌ
node.querySelector('.file-card-delete')?.addEventListener('click', () => {
node.remove();
});
// μ λ ¬ λ²νΌ
node.querySelectorAll('button[data-align]').forEach(btn => {
btn.addEventListener('click', (e) => {
const target = e.currentTarget;
const align = target.dataset.align;
node.querySelectorAll('button[data-align]').forEach(el => el.classList.remove('active'));
node.style.justifySelf = align || 'start';
target.classList.add('active');
});
});
// λλκ·Έ μμ
node.addEventListener('dragstart', (e) => {
const value = {
name: node.getAttribute('data-file-name'),
size: node.getAttribute('data-file-size')
};
e.dataTransfer?.setData('text/plain', JSON.stringify(value));
e.dataTransfer.effectAllowed = 'move';
node.classList.add('dragging');
});
node.addEventListener('dragend', () => {
node.classList.remove('dragging');
});
return node;
}
static value(node) {
return {
name: node.getAttribute('data-file-name'),
size: node.getAttribute('data-file-size')
};
}
static getQuillInstance() {
return Quill.quillInstance || null;
}
static setQuillInstance(quill) {
Quill.quillInstance = quill;
}
}
export default FileBlot;