Redesign the image section of the slide form

Show a dropzone (with drag and drop) while a slide has no image and a
row with preview, replace and remove buttons otherwise. A newly chosen
file is previewed right away. Placement is picked from tiles with a
small sketch instead of long radio labels, technical details move into
a collapsible help. The section gets full-width dividers, and the modal
a slim scrollbar.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Sonnet 5.5 committed 2026-11-17 14:21:27 +01:00
1 parent 0ad29aa484
commit c6a92e347f
5 files changed
+257 -92

No files matched your search

+118 -38
View File
@@ -2279,51 +2279,132 @@
// ------------------------------------------------------------------ slide form
/** Slide image: a trash button toggles the "remove image" checkbox (the checkbox stays for use without JavaScript). */
function initImageRemove(box) {
var checkbox = box.querySelector('input[type="checkbox"]');
var button = box.querySelector('[data-image-remove-button]');
var status = box.querySelector('[data-image-remove-status]');
var figure = box.parentNode.querySelector('.image-fields__current');
if (!checkbox || !button) {
return;
}
var field = checkbox.closest('.input');
if (field) {
field.hidden = true;
}
button.hidden = false;
button.addEventListener('click', function () {
checkbox.checked = !checkbox.checked;
button.setAttribute('aria-pressed', checkbox.checked ? 'true' : 'false');
button.querySelector('[data-label]').textContent = button.getAttribute(checkbox.checked ? 'data-label-undo' : 'data-label-remove');
if (figure) {
figure.classList.toggle('is-removed', checkbox.checked);
}
if (status) {
status.textContent = checkbox.checked ? 'Das Bild wird beim Speichern entfernt.' : '';
}
});
}
/**
* Slide image: the placement question only makes sense with an image, i.e. an existing one that is not
* marked for removal, or a newly selected file. Without JavaScript it stays visible.
* Slide image section: dropzone (also drag and drop) while there is no image, otherwise a row with
* preview and replace/remove buttons. A newly chosen file is previewed right away. The placement
* question only shows with an image (existing and not marked for removal, or a chosen file).
* Without JavaScript the label opens the file dialog, the checkbox removes and the placement stays visible.
*/
function initImageFields(box) {
var layout = box.querySelector('[data-image-layout]');
var file = box.querySelector('input[type="file"]');
var remove = box.querySelector('input[name="remove_image"]');
if (!layout || !file) {
var layout = box.querySelector('[data-image-layout]');
var drop = box.querySelector('[data-image-drop]');
var row = box.querySelector('[data-image-row]');
var img = box.querySelector('[data-image-figure] img');
var figure = box.querySelector('[data-image-figure]');
var info = box.querySelector('[data-image-info]');
var pick = box.querySelector('[data-image-pick]');
var button = box.querySelector('[data-image-remove-button]');
var checkbox = box.querySelector('input[name="remove_image"]');
if (!file || !layout || !drop || !row || !img || !button) {
return;
}
var hasImage = box.hasAttribute('data-has-image');
function update() {
var removed = !!remove && remove.checked;
layout.hidden = !(file.files && file.files.length > 0) && !(hasImage && !removed);
var original = img.getAttribute('data-image-original') || '';
var width = img.getAttribute('width');
var height = img.getAttribute('height');
var objectUrl = null;
// The checkbox is replaced by the button (it stays in the form and is toggled by it).
var removeBox = box.querySelector('[data-image-remove]');
if (removeBox) {
removeBox.hidden = true;
}
box.addEventListener('change', update);
box.addEventListener('click', update);
var extra = box.querySelector('[data-image-drop-extra]');
if (extra) {
extra.hidden = false;
}
button.hidden = false;
function formatSize(bytes) {
return bytes < 1048576 ? Math.max(1, Math.round(bytes / 1024)) + ' KB' : (bytes / 1048576).toFixed(1).replace('.', ',') + ' MB';
}
function update() {
var chosen = !!file.files && file.files.length > 0;
var removed = !!checkbox && checkbox.checked && !chosen;
if (objectUrl) {
URL.revokeObjectURL(objectUrl);
objectUrl = null;
}
if (chosen) {
objectUrl = URL.createObjectURL(file.files[0]);
img.src = objectUrl;
img.removeAttribute('width');
img.removeAttribute('height');
} else {
img.src = original;
if (width && height) {
img.setAttribute('width', width);
img.setAttribute('height', height);
}
}
var showRow = chosen || hasImage;
var focusLost = row.hidden === false && !showRow;
row.hidden = !showRow;
drop.hidden = showRow;
figure.classList.toggle('is-removed', removed);
if (info) {
info.textContent = chosen
? info.getAttribute('data-text-new') + ' ' + file.files[0].name + ' (' + formatSize(file.files[0].size) + ')'
: (removed ? info.getAttribute('data-text-removed') : (hasImage ? info.getAttribute('data-text-current') : ''));
}
if (pick) {
pick.textContent = pick.getAttribute(chosen || !hasImage ? 'data-label-choose' : 'data-label-replace');
}
button.querySelector('[data-label]').textContent = button.getAttribute(chosen ? 'data-label-discard' : (removed ? 'data-label-undo' : 'data-label-remove'));
if (!chosen && checkbox) {
button.setAttribute('aria-pressed', removed ? 'true' : 'false');
} else {
button.removeAttribute('aria-pressed');
}
layout.hidden = !chosen && !(hasImage && !removed);
if (focusLost) {
file.focus();
}
}
file.addEventListener('change', function () {
if (file.files && file.files.length > 0 && checkbox) {
// A new image replaces the old one, so "remove" no longer applies.
checkbox.checked = false;
}
update();
});
button.addEventListener('click', function () {
if (file.files && file.files.length > 0) {
file.value = '';
} else if (checkbox) {
checkbox.checked = !checkbox.checked;
}
update();
});
[drop, row].forEach(function (zone) {
zone.addEventListener('dragover', function (event) {
event.preventDefault();
zone.classList.add('is-dragover');
});
zone.addEventListener('dragleave', function () {
zone.classList.remove('is-dragover');
});
zone.addEventListener('drop', function (event) {
event.preventDefault();
zone.classList.remove('is-dragover');
var dropped = event.dataTransfer && event.dataTransfer.files;
if (!dropped || dropped.length === 0 || typeof DataTransfer === 'undefined') {
return;
}
try {
var transfer = new DataTransfer();
transfer.items.add(dropped[0]);
file.files = transfer.files;
file.dispatchEvent(new Event('change', {bubbles: true}));
} catch (e) {
// Browser without assignable file lists: the file dialog still works.
}
});
});
update();
}
@@ -2967,7 +3048,6 @@
initCopy(root);
root.querySelectorAll('[data-youtube-form]').forEach(initYouTubeForm);
root.querySelectorAll('[data-quiz-options]').forEach(initQuizOptions);
root.querySelectorAll('[data-image-remove]').forEach(initImageRemove);
root.querySelectorAll('[data-image-fields]').forEach(initImageFields);
root.querySelectorAll('[data-sortable]').forEach(initSortable);
root.querySelectorAll('[data-order-options]').forEach(initOrderOptions);