quantity fields in food log and recipe upload drop zone
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
(() => {
|
||||
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();
|
||||
});
|
||||
}
|
||||
})();
|
||||
@@ -40,3 +40,9 @@ body[data-theme=dark] .recipes-markdown-editor .toastui-editor-defaultUI,body[da
|
||||
.recipes-saved-badge{align-self:center;padding:5px 8px;border-radius:99px;background:#e1f2e8;color:#235943;font-size:.72rem;font-weight:700}
|
||||
.cook-later{max-width:850px;margin:0 auto;padding:20px;color:var(--recipe-ink,#25292e)}.cook-later h1{margin:15px 0 5px;font-family:Fraunces,Georgia,serif}.cook-later__list{display:grid;gap:12px;margin-top:24px}.cook-later__item{display:flex;align-items:center;justify-content:space-between;gap:20px;padding:18px;border:1px solid var(--recipe-line,#d0d8d0);border-radius:14px;background:var(--recipe-surface,#fff)}.cook-later__item h2{margin:0;font-size:1.15rem}.cook-later__item p{margin:6px 0}.cook-later__item small{color:var(--recipe-muted,#687178)}.cook-later__item button{min-height:44px;padding:8px 14px;border:1px solid #b94a55;border-radius:9px;background:transparent;color:#a12435;cursor:pointer}body[data-theme=dark] .cook-later__item button{color:#ffb3bb}body[data-theme=dark] .recipes-saved-badge{background:#294d38;color:#b7e8c5}@media(max-width:600px){.cook-later__item{align-items:flex-start;flex-direction:column}}
|
||||
body[data-theme=dark] .cook-later{--recipe-ink:#eaf0ea;--recipe-muted:#afbbb2;--recipe-surface:#202a23;--recipe-line:#435046}body[data-theme=dark] .cook-later a{color:#a7d9b6}
|
||||
/* Recipe photo upload controls */
|
||||
.recipe-photo-upload{min-width:0}.recipe-photo-upload .recipes-photo-placeholder{position:relative;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:7px;height:220px;padding:20px;border:2px dashed #719d7d;border-radius:14px;color:#24573b;background:#e5f1e8;text-align:center;cursor:pointer;font-family:inherit;transition:border-color .15s,background .15s}.recipe-photo-upload__icon{display:grid;place-items:center;width:42px;height:42px;border-radius:50%;background:#d0e6d6;font-size:1.5rem;font-weight:700}.recipe-photo-upload .recipes-photo-placeholder strong{font-size:1rem}.recipe-photo-upload .recipes-photo-placeholder span:last-of-type{max-width:220px;font-size:.77rem;line-height:1.4}.recipe-photo-upload .recipes-photo-placeholder:hover,.recipe-photo-upload .recipes-photo-placeholder.is-dragging{border-color:#235943;background:#d9ebdf}.recipe-photo-upload .recipes-photo-placeholder:focus-within,.recipe-photo-editor__zone:focus-within{outline:3px solid #a44cbe;outline-offset:3px}.recipe-photo-upload input[type=file],.recipe-photo-editor input[type=file]{position:absolute;width:1px;height:1px;opacity:0;overflow:hidden}.recipe-photo-submit{margin-top:8px;min-height:40px;padding:7px 12px;border:0;border-radius:8px;color:#fff;background:#235943;cursor:pointer}.recipe-photo-submit[hidden]{display:none}.recipe-photo-upload__status{display:block;margin-top:6px;color:var(--recipe-muted);font-size:.75rem}
|
||||
.recipes-photo-field-label{display:block;margin-bottom:6px;font-size:.81rem;font-weight:700}.recipe-photo-editor{position:relative;max-width:490px}.recipe-photo-editor__zone{position:relative;display:grid;place-items:center;min-height:225px;overflow:hidden;border:2px dashed #719d7d;border-radius:14px;background:#e5f1e8;color:#24573b;text-align:center;cursor:pointer}.recipe-photo-editor__zone.is-dragging,.recipe-photo-editor__zone:hover{border-color:#235943;background:#d9ebdf}.recipe-photo-editor__zone img{position:absolute;inset:0;width:100%;height:100%;object-fit:cover}.recipe-photo-editor__zone img[hidden]{display:none}.recipe-photo-editor__prompt{z-index:1;max-width:260px;padding:10px 16px;border-radius:10px;background:rgba(255,255,255,.88);font-size:.9rem;font-weight:700}.recipe-photo-editor__prompt small{display:block;margin-top:4px;font-weight:400}.recipe-photo-editor__remove{position:absolute;z-index:2;top:10px;right:10px;display:grid;place-items:center;width:36px;height:36px;border:0;border-radius:50%;color:#fff;background:#223029;font-size:1.45rem;line-height:1;cursor:pointer}.recipe-photo-editor__remove[hidden]{display:none}.recipe-photo-editor__remove:focus-visible{outline:3px solid #a44cbe;outline-offset:3px}.recipe-photo-editor__status{min-height:1.2em;margin:7px 0 0;color:var(--recipe-muted);font-size:.78rem}.recipe-photo-editor__checkbox[hidden]{display:none}body[data-theme=dark] .recipe-photo-upload .recipes-photo-placeholder,body[data-theme=dark] .recipe-photo-editor__zone{color:#c7e8cf;background:#314d39;border-color:#79a889}body[data-theme=dark] .recipe-photo-upload .recipes-photo-placeholder:hover,body[data-theme=dark] .recipe-photo-editor__zone:hover,body[data-theme=dark] .recipe-photo-editor__zone.is-dragging{background:#3a6246}body[data-theme=dark] .recipe-photo-upload__icon{background:#446f50}body[data-theme=dark] .recipe-photo-editor__prompt{color:#24372a}
|
||||
.recipes-field .recipe-photo-editor__zone{display:grid;margin:0;align-items:center;justify-items:center}
|
||||
.recipes-field label.recipe-photo-editor__zone{display:grid;margin:0;align-items:center;justify-items:center}
|
||||
.recipe-photo-editor__prompt small,body[data-theme=dark] .recipe-photo-editor__prompt small{color:#415c4b}
|
||||
|
||||
Reference in New Issue
Block a user