90 lines
3.5 KiB
JavaScript
90 lines
3.5 KiB
JavaScript
(() => {
|
|
const supported = new Set(['image/jpeg', 'image/png', 'image/webp']);
|
|
const check = (file, status) => {
|
|
if (!file) return false;
|
|
if (!supported.has(file.type)) {
|
|
status.textContent = 'Choose a JPEG, PNG or WebP image.';
|
|
return false;
|
|
}
|
|
if (file.size > 5 * 1024 * 1024) {
|
|
status.textContent = 'Choose an image smaller than 5 MB.';
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
const drop = (zone, input, onFile) => {
|
|
if (!zone || !input) return;
|
|
zone.addEventListener('dragover', event => {
|
|
if (!Array.from(event.dataTransfer?.types || []).includes('Files')) return;
|
|
event.preventDefault();
|
|
zone.classList.add('is-dragging');
|
|
});
|
|
zone.addEventListener('dragleave', () => zone.classList.remove('is-dragging'));
|
|
zone.addEventListener('drop', event => {
|
|
event.preventDefault();
|
|
zone.classList.remove('is-dragging');
|
|
if (!event.dataTransfer?.files.length) return;
|
|
input.files = event.dataTransfer.files;
|
|
onFile();
|
|
});
|
|
};
|
|
|
|
const detail = document.querySelector('[data-photo-upload]');
|
|
if (detail) {
|
|
const input = detail.querySelector('input[type=file]');
|
|
const status = detail.querySelector('[data-photo-status]');
|
|
const zone = detail.querySelector('.recipes-photo-placeholder');
|
|
const submit = detail.querySelector('.recipe-photo-submit');
|
|
if (submit) submit.hidden = true;
|
|
const upload = () => {
|
|
const file = input.files[0];
|
|
if (!check(file, status)) return;
|
|
status.textContent = `Uploading ${file.name}…`;
|
|
detail.requestSubmit();
|
|
};
|
|
input.addEventListener('change', upload);
|
|
drop(zone, input, upload);
|
|
}
|
|
|
|
const editor = document.querySelector('[data-photo-editor]');
|
|
if (editor) {
|
|
const input = editor.querySelector('input[type=file]');
|
|
const zone = editor.querySelector('.recipe-photo-editor__zone');
|
|
const preview = editor.querySelector('[data-photo-preview]');
|
|
const prompt = editor.querySelector('[data-photo-prompt]');
|
|
const remove = editor.querySelector('[data-photo-remove]');
|
|
const checkbox = editor.parentElement.querySelector('input[name=remove_photo]');
|
|
const status = editor.parentElement.querySelector('[data-photo-edit-status]');
|
|
const hasPhoto = !preview.hidden;
|
|
let previewUrl;
|
|
const selected = () => {
|
|
const file = input.files[0];
|
|
if (!file) return;
|
|
if (!check(file, status)) { input.value = ''; return; }
|
|
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
|
previewUrl = URL.createObjectURL(file);
|
|
preview.src = previewUrl;
|
|
preview.hidden = false;
|
|
checkbox.checked = false;
|
|
remove.hidden = false;
|
|
prompt.firstChild.textContent = 'Click or drop to change photo';
|
|
status.textContent = `${file.name} selected. Save the recipe to upload it.`;
|
|
};
|
|
input.addEventListener('change', selected);
|
|
drop(zone, input, () => input.dispatchEvent(new Event('change', {bubbles: true})));
|
|
remove.addEventListener('click', () => {
|
|
input.value = '';
|
|
if (previewUrl) URL.revokeObjectURL(previewUrl);
|
|
previewUrl = null;
|
|
preview.removeAttribute('src');
|
|
preview.hidden = true;
|
|
checkbox.checked = hasPhoto;
|
|
remove.hidden = true;
|
|
prompt.firstChild.textContent = 'Click or drop a photo here';
|
|
status.textContent = hasPhoto ? 'Current photo will be removed when you save.' : 'Selected photo cleared.';
|
|
checkbox.dispatchEvent(new Event('change', {bubbles: true}));
|
|
input.focus();
|
|
});
|
|
}
|
|
})();
|