Feature/image upload ux #97

Merged
gnilebein merged 3 commits from feature/image-upload-ux into main 2026-11-17 14:23:45 +01:00
5 changed files with 242 additions and 77 deletions
Showing only changes of commit c6a92e347f - Show all commits

No files matched your search

+4 -1
View File
@@ -9,7 +9,10 @@ Ein Bild pro Folie: Upload, Verarbeitung, Auslieferung und Löschregeln.
Ablage `{Plenario.images.path}/{poll_id}/{24 Hex}.webp` (Standard `data/uploads`, Docker `/data/uploads`) **außerhalb von `webroot/`**, Ablage `{Plenario.images.path}/{poll_id}/{24 Hex}.webp` (Standard `data/uploads`, Docker `/data/uploads`) **außerhalb von `webroot/`**,
ausgeliefert über `participate:image` (`/p/{code}/img/{file}`, auch für Presenter und Einbettung; nur Dateien dieser Umfrage, `immutable` ausgeliefert über `participate:image` (`/p/{code}/img/{file}`, auch für Presenter und Einbettung; nur Dateien dieser Umfrage, `immutable`
gecacht, kein Cookie). Jedes neue Bild bekommt einen neuen Namen → neue URL, ersetzte Bilder sind sofort 404. gecacht, kein Cookie). Jedes neue Bild bekommt einen neuen Namen → neue URL, ersetzte Bilder sind sofort 404.
Eine Bildbeschreibung wird bewusst nicht abgefragt (`image_alt` bleibt `""`, die Spalte ist ungenutzt): Bilder sind Beamer-Dekoration. Nie Pflichtfelder zum Bild einführen, die den Upload verwerfen: das Dateifeld lässt sich nach einem Fehler nicht neu füllen. Die Frage nach der Platzierung (`data-image-layout`) blendet `initImageFields()` nur ein, wenn ein Bild vorhanden (und nicht zum Entfernen markiert) oder eine Datei gewählt ist; ohne JavaScript bleibt sie sichtbar. Layout `image_layout` (`top`|`side`|`full`, `full` nur Eine Bildbeschreibung wird bewusst nicht abgefragt (`image_alt` bleibt `""`, die Spalte ist ungenutzt): Bilder sind Beamer-Dekoration. Nie Pflichtfelder zum Bild einführen, die den Upload verwerfen: das Dateifeld lässt sich nach einem Fehler nicht neu füllen. Der Formularbereich (`element/slide_image_fields`) ist ein eigener Abschnitt: ohne Bild eine Dropzone (Label auf dem versteckten
Dateifeld `#image-file`, Drag & Drop), mit Bild eine Zeile aus Vorschau, „Ersetzen“ und „Entfernen“; eine gewählte Datei wird sofort als
Vorschau gezeigt (`initImageFields()`, ersetzt `remove_image` per Button, die Checkbox bleibt für den Betrieb ohne JavaScript). Die Platzierung
(`data-image-layout`, Kacheln mit Mini-Skizze, echte Radios) blendet `initImageFields()` nur ein, wenn ein Bild vorhanden (und nicht zum Entfernen markiert) oder eine Datei gewählt ist; ohne JavaScript bleibt sie sichtbar. Layout `image_layout` (`top`|`side`|`full`, `full` nur
Inhaltsfolien, `null` = Standard je Typ: Inhaltsfolie `side`, sonst `top`; `Slide::imageLayout()`); `side` gilt nur für die Bühne Inhaltsfolien, `null` = Standard je Typ: Inhaltsfolie `side`, sonst `top`; `Slide::imageLayout()`); `side` gilt nur für die Bühne
(CSS `stage--img-*`), Teilnehmende sehen das Bild immer oben (`element/slide_image`). Kontingent `Plenario.images.maxPerPoll` Folien mit Bild. (CSS `stage--img-*`), Teilnehmende sehen das Bild immer oben (`element/slide_image`). Kontingent `Plenario.images.maxPerPoll` Folien mit Bild.
Im State steht `slide.image` = `{url, alt, layout, width, height}` (`PollStateService::slidePayload()`). Im State steht `slide.image` = `{url, alt, layout, width, height}` (`PollStateService::slidePayload()`).
+68 -30
View File
@@ -1,7 +1,8 @@
<?php <?php
/** /**
* Image fields of the slide form: upload, alternative text, layout, remove. * Image section of the slide form: dropzone or current image (replace / remove), placement tiles.
* Not for slide types without image support (YouTube). * Works without JavaScript (label opens the file dialog, checkbox removes); JavaScript adds
* preview, drag and drop and the remove toggle. Not for slide types without image support (YouTube).
* *
* @var \App\View\AppView $this * @var \App\View\AppView $this
* @var \App\Model\Entity\Slide $slide * @var \App\Model\Entity\Slide $slide
@@ -23,25 +24,55 @@ $layout = $posted && in_array($request->getData('image_layout'), Slide::IMAGE_LA
? (string)$request->getData('image_layout') ? (string)$request->getData('image_layout')
: $slide->imageLayout(); : $slide->imageLayout();
$layouts = [ $layouts = [
Slide::IMAGE_TOP => __('Oben (über den Antworten bzw. Ergebnissen)'), Slide::IMAGE_TOP => [__('Oben'), '<rect class="img" x="4" y="3" width="40" height="13" rx="2"/><rect x="4" y="20" width="30" height="3" rx="1.5"/><rect x="4" y="26" width="22" height="3" rx="1.5"/>'],
Slide::IMAGE_SIDE => __('Daneben (Bild links, Text und Ergebnis rechts)'), Slide::IMAGE_SIDE => [__('Daneben'), '<rect class="img" x="4" y="3" width="18" height="26" rx="2"/><rect x="27" y="6" width="17" height="3" rx="1.5"/><rect x="27" y="12" width="13" height="3" rx="1.5"/><rect x="27" y="18" width="17" height="3" rx="1.5"/>'],
]; ];
if ($slide->type === Slide::TYPE_CONTENT) { if ($slide->type === Slide::TYPE_CONTENT) {
$layouts[Slide::IMAGE_FULL] = __('Bildschirm füllen (Bild so groß wie möglich)'); $layouts[Slide::IMAGE_FULL] = [__('Vollbild'), '<rect class="img" x="4" y="3" width="40" height="26" rx="2"/>'];
} }
$imageError = $slide->getError('image'); $imageError = $slide->getError('image');
$maxMb = (int)round((int)Configure::read('Plenario.images.maxUploadBytes', 10 * 1024 * 1024) / 1024 / 1024); $maxMb = (int)round((int)Configure::read('Plenario.images.maxUploadBytes', 10 * 1024 * 1024) / 1024 / 1024);
$imageUrl = $hasImage
? $this->Url->build(['_name' => 'participate:image', 'code' => $poll->join_code, 'file' => $slide->image])
: '';
?> ?>
<fieldset class="image-fields" data-image-fields<?= $hasImage ? ' data-has-image' : '' ?>> <div class="image-section" data-image-fields<?= $hasImage ? ' data-has-image' : '' ?>>
<legend><?= __('Bild (optional)') ?></legend> <fieldset class="image-fields">
<legend><?= __('Bild') ?> <span class="image-fields__optional"><?= __('(optional)') ?></span></legend>
<?php if (!ImageService::isAvailable()) : ?> <?php if (!ImageService::isAvailable()) : ?>
<p class="hint"><?= __('Bilder sind auf diesem Server nicht verfügbar (PHP-Erweiterung GD mit WebP fehlt).') ?></p> <p class="hint"><?= __('Bilder sind auf diesem Server nicht verfügbar (PHP-Erweiterung GD mit WebP fehlt).') ?></p>
<?php else : ?> <?php else : ?>
<?php if ($hasImage) : ?> <?php if ($imageError) : ?>
<figure class="image-fields__current"> <div class="error-message" id="image-error" role="alert"><?= h(implode(' ', (array)$imageError)) ?></div>
<img src="<?= h($this->Url->build(['_name' => 'participate:image', 'code' => $poll->join_code, 'file' => $slide->image])) ?>" <?php endif; ?>
alt="<?= h((string)$slide->image_alt) ?>" width="<?= (int)$slide->image_width ?>" height="<?= (int)$slide->image_height ?>">
<?php // One file input for both states; the labels below open it, so it can stay visually hidden. ?>
<input type="file" id="image-file" name="image" class="sr-only" accept="image/jpeg,image/png,image/webp,image/gif"
aria-describedby="image-hint<?= $imageError ? ' image-error' : '' ?>" <?= $imageError ? 'aria-invalid="true"' : '' ?>>
<label class="image-drop" for="image-file" data-image-drop<?= $hasImage ? ' hidden' : '' ?>>
<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6"
stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<rect x="3" y="4" width="18" height="16" rx="2"/><circle cx="9" cy="10" r="1.5"/><path d="M21 16l-5-5-8 9"/>
</svg>
<span class="image-drop__title"><?= __('Bild auswählen') ?><span data-image-drop-extra hidden> <?= __('oder hierher ziehen') ?></span></span>
<span class="image-drop__hint"><?= __('JPEG, PNG, WebP oder GIF, bis {0} MB', $maxMb) ?></span>
</label>
<div class="image-fields__row" data-image-row<?= $hasImage ? '' : ' hidden' ?>>
<figure class="image-fields__current" data-image-figure>
<img src="<?= h($imageUrl) ?>" alt="" data-image-original="<?= h($imageUrl) ?>"
<?= $hasImage ? 'width="' . (int)$slide->image_width . '" height="' . (int)$slide->image_height . '"' : '' ?>>
</figure> </figure>
<div class="image-fields__side">
<p class="image-fields__info" role="status" aria-live="polite" data-image-info
data-text-current="<?= h(__('Aktuelles Bild')) ?><?= $hasImage ? h(' · ' . __('{0} × {1} Pixel', (int)$slide->image_width, (int)$slide->image_height)) : '' ?>"
data-text-new="<?= h(__('Neues Bild wird beim Speichern übernommen:')) ?>"
data-text-removed="<?= h(__('Das Bild wird beim Speichern entfernt.')) ?>"><?= $hasImage ? h(__('Aktuelles Bild') . ' · ' . __('{0} × {1} Pixel', (int)$slide->image_width, (int)$slide->image_height)) : '' ?></p>
<div class="image-fields__buttons">
<label class="button button--small" for="image-file" data-image-pick
data-label-replace="<?= h(__('Ersetzen')) ?>" data-label-choose="<?= h(__('Anderes Bild wählen')) ?>"><?= __('Ersetzen') ?></label>
<?php if ($hasImage) : ?>
<?php // Without JavaScript the checkbox does the job; with it, the button toggles the same checkbox. ?> <?php // Without JavaScript the checkbox does the job; with it, the button toggles the same checkbox. ?>
<div class="image-fields__remove" data-image-remove> <div class="image-fields__remove" data-image-remove>
<?= $this->Form->control('remove_image', [ <?= $this->Form->control('remove_image', [
@@ -51,34 +82,41 @@ $maxMb = (int)round((int)Configure::read('Plenario.images.maxUploadBytes', 10 *
'hiddenField' => false, 'hiddenField' => false,
'checked' => false, 'checked' => false,
]) ?> ]) ?>
<button type="button" class="button button--danger button--small" hidden aria-pressed="false" data-image-remove-button </div>
data-label-remove="<?= h(__('Bild entfernen')) ?>" data-label-undo="<?= h(__('Entfernen rückgängig machen')) ?>"> <?php endif; ?>
<button type="button" class="button button--danger button--small" hidden data-image-remove-button
data-label-remove="<?= h(__('Entfernen')) ?>" data-label-undo="<?= h(__('Rückgängig')) ?>"
data-label-discard="<?= h(__('Auswahl verwerfen')) ?>">
<svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" <svg width="1em" height="1em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M3 6h18M8 6V4h8v2M6 6l1 14h10l1-14M10 11v5M14 11v5"/> <path d="M3 6h18M8 6V4h8v2M6 6l1 14h10l1-14M10 11v5M14 11v5"/>
</svg> </svg>
<span data-label><?= __('Bild entfernen') ?></span> <span data-label><?= __('Entfernen') ?></span>
</button> </button>
<span class="hint" role="status" aria-live="polite" data-image-remove-status></span>
</div> </div>
<?php endif; ?>
<div class="input file<?= $imageError ? ' error' : '' ?>">
<label for="image-file"><?= $hasImage ? __('Anderes Bild hochladen') : __('Bild hochladen') ?></label>
<input type="file" id="image-file" name="image" accept="image/jpeg,image/png,image/webp,image/gif"
aria-describedby="image-hint<?= $imageError ? ' image-error' : '' ?>" <?= $imageError ? 'aria-invalid="true"' : '' ?>>
<p class="hint" id="image-hint"><?= __('JPEG, PNG, WebP oder GIF (nicht animiert), bis {0} MB. Das Bild wird automatisch auf höchstens 1600 Pixel verkleinert und als WebP gespeichert, Kameradaten wie der Aufnahmeort werden dabei entfernt.', $maxMb) ?></p>
<?php if ($imageError) : ?>
<div class="error-message" id="image-error"><?= h(implode(' ', (array)$imageError)) ?></div>
<?php endif; ?>
</div> </div>
<fieldset class="scale-style image-fields__layout" data-image-layout> </div>
<div class="image-fields__help" id="image-hint">
<details>
<summary><?= __('Was passiert mit dem Bild?') ?></summary>
<p class="hint"><?= __('JPEG, PNG, WebP oder GIF (nicht animiert), bis {0} MB. Das Bild wird automatisch auf höchstens 1600 Pixel verkleinert und als WebP gespeichert, Kameradaten wie der Aufnahmeort werden dabei entfernt.', $maxMb) ?></p>
</details>
</div>
<fieldset class="image-layout" data-image-layout>
<legend><?= __('Platzierung in der Präsentation') ?></legend> <legend><?= __('Platzierung in der Präsentation') ?></legend>
<?= $this->Form->radio('image_layout', array_map( <div class="image-layout__tiles">
fn($value, $text) => ['value' => $value, 'text' => $text], <?php foreach ($layouts as $value => [$text, $shapes]) : ?>
array_keys($layouts), <label class="image-layout__tile">
$layouts, <input type="radio" name="image_layout" value="<?= h($value) ?>" class="sr-only" <?= $layout === $value ? 'checked' : '' ?>>
), ['value' => $layout]) ?> <svg viewBox="0 0 48 32" width="72" height="48" fill="currentColor" aria-hidden="true"><?= $shapes ?></svg>
<span><?= h($text) ?></span>
</label>
<?php endforeach; ?>
</div>
<p class="hint"><?= __('Auf dem Smartphone der Teilnehmenden steht das Bild immer oben.') ?></p> <p class="hint"><?= __('Auf dem Smartphone der Teilnehmenden steht das Bild immer oben.') ?></p>
</fieldset> </fieldset>
<?php endif; ?> <?php endif; ?>
</fieldset> </fieldset>
</div>
+1 -1
View File
@@ -328,6 +328,6 @@ class SlideImageTest extends TestCase
$this->assertResponseOk(); $this->assertResponseOk();
$this->assertResponseContains('enctype="multipart/form-data"'); $this->assertResponseContains('enctype="multipart/form-data"');
$this->assertResponseContains('name="remove_image"'); $this->assertResponseContains('name="remove_image"');
$this->assertResponseContains('Bildschirm füllen'); $this->assertResponseContains('value="full"');
} }
} }
+51 -7
View File
@@ -816,6 +816,8 @@ body.present {
.modal::backdrop { background: rgba(10, 12, 30, .55); } .modal::backdrop { background: rgba(10, 12, 30, .55); }
.modal__panel { .modal__panel {
max-height: calc(100dvh - 24px); overflow: auto; max-height: calc(100dvh - 24px); overflow: auto;
/* slim scrollbar in the theme colours instead of the bright system one */
scrollbar-width: thin; scrollbar-color: color-mix(in srgb, var(--muted) 55%, transparent) transparent;
border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
box-shadow: 0 20px 60px rgba(0, 0, 0, .35); box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
} }
@@ -836,12 +838,54 @@ body:has(dialog[open]) { overflow: hidden; }
/* slide images: participant page and slide form */ /* slide images: participant page and slide form */
.slide-image { margin: 1em 0; } .slide-image { margin: 1em 0; }
.slide-image img { display: block; max-width: 100%; height: auto; margin: 0 auto; border-radius: 8px; } .slide-image img { display: block; max-width: 100%; height: auto; margin: 0 auto; border-radius: 8px; }
.image-fields, .image-fields__layout { border: 0; padding: 0; margin: 0 0 16px; min-width: 0; } /* Full width dividers: reach through the 24px padding of .card. */
.image-section { margin: 24px -24px; padding: 16px 24px; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.image-fields, .image-layout { border: 0; padding: 0; margin: 0; min-width: 0; }
.image-fields > legend { font-weight: 600; padding: 0; margin-bottom: .5em; } .image-fields > legend { font-weight: 600; padding: 0; margin-bottom: .5em; }
.image-fields__layout > legend { padding: 0; margin-bottom: .25em; } .image-fields__optional { font-weight: 400; color: var(--muted); }
.image-fields__current { margin: 0 0 .75em; } .image-layout { margin-top: 16px; }
.image-layout > legend { font-weight: 600; padding: 0; margin-bottom: .5em; }
.image-layout .hint { margin-bottom: 0; }
/* dropzone (no image yet) */
.image-drop {
display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center;
padding: 20px 16px; border: 2px dashed var(--border); border-radius: 10px;
color: var(--muted); cursor: pointer;
}
.image-drop[hidden] { display: none; }
.image-drop:hover, .image-drop.is-dragover, .image-fields:has(input[type="file"]:focus-visible) .image-drop {
border-color: var(--primary); background: var(--primary-soft); color: var(--text);
}
.image-fields:has(input[type="file"]:focus-visible) .image-drop { outline: 3px solid var(--primary); outline-offset: 2px; }
.image-drop__title { font-weight: 600; color: var(--text); }
.image-drop__hint { font-size: .875rem; }
/* current or chosen image: thumbnail, info, buttons */
.image-fields__row { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 16px; padding: 12px; border: 1px solid var(--border); border-radius: 10px; }
.image-fields__row[hidden] { display: none; }
.image-fields__row.is-dragover { border-color: var(--primary); background: var(--primary-soft); }
.image-fields__current { margin: 0; flex: none; }
.image-fields__current img { display: block; max-width: 160px; max-height: 110px; width: auto; height: auto; border-radius: 6px; background: var(--surface-2); }
.image-fields__current.is-removed img { opacity: .3; filter: grayscale(1); } .image-fields__current.is-removed img { opacity: .3; filter: grayscale(1); }
.image-fields__remove { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; margin: 0 0 1em; } .image-fields__side { flex: 1 1 200px; min-width: 0; }
.image-fields__remove .button { display: inline-flex; align-items: center; gap: 6px; } .image-fields__info { margin: 0 0 8px; font-size: .875rem; color: var(--muted); overflow-wrap: anywhere; }
.image-fields__remove .hint { margin: 0; } .image-fields__buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.image-fields__current img { display: block; max-width: 100%; max-height: 180px; width: auto; height: auto; border-radius: 6px; } .image-fields__buttons .button { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.image-fields:has(input[type="file"]:focus-visible) .image-fields__buttons [data-image-pick] { outline: 3px solid var(--primary); outline-offset: 2px; }
.image-fields__remove { margin: 0; }
.image-fields__help { margin-top: 8px; font-size: .875rem; color: var(--muted); }
.image-fields__help summary { cursor: pointer; width: fit-content; }
.image-fields__help .hint { margin: .5em 0 0; }
/* placement tiles (radios) */
.image-layout__tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; max-width: 420px; }
.image-layout__tile {
display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 8px;
border: 2px solid var(--border); border-radius: 8px; cursor: pointer; color: var(--muted); font-size: .9rem;
}
.image-layout__tile svg { max-width: 100%; height: auto; }
.image-layout__tile svg .img { fill: var(--primary); opacity: .45; }
.image-layout__tile:hover { border-color: var(--primary); }
.image-layout__tile:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); color: var(--text); font-weight: 600; }
.image-layout__tile:has(input:focus-visible) { outline: 3px solid var(--primary); outline-offset: 2px; }
+118 -38
View File
@@ -2279,51 +2279,132 @@
// ------------------------------------------------------------------ slide form // ------------------------------------------------------------------ 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 * Slide image section: dropzone (also drag and drop) while there is no image, otherwise a row with
* marked for removal, or a newly selected file. Without JavaScript it stays visible. * 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) { function initImageFields(box) {
var layout = box.querySelector('[data-image-layout]');
var file = box.querySelector('input[type="file"]'); var file = box.querySelector('input[type="file"]');
var remove = box.querySelector('input[name="remove_image"]'); var layout = box.querySelector('[data-image-layout]');
if (!layout || !file) { 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; return;
} }
var hasImage = box.hasAttribute('data-has-image'); var hasImage = box.hasAttribute('data-has-image');
function update() { var original = img.getAttribute('data-image-original') || '';
var removed = !!remove && remove.checked; var width = img.getAttribute('width');
layout.hidden = !(file.files && file.files.length > 0) && !(hasImage && !removed); 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); var extra = box.querySelector('[data-image-drop-extra]');
box.addEventListener('click', update); 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(); update();
} }
@@ -2967,7 +3048,6 @@
initCopy(root); initCopy(root);
root.querySelectorAll('[data-youtube-form]').forEach(initYouTubeForm); root.querySelectorAll('[data-youtube-form]').forEach(initYouTubeForm);
root.querySelectorAll('[data-quiz-options]').forEach(initQuizOptions); root.querySelectorAll('[data-quiz-options]').forEach(initQuizOptions);
root.querySelectorAll('[data-image-remove]').forEach(initImageRemove);
root.querySelectorAll('[data-image-fields]').forEach(initImageFields); root.querySelectorAll('[data-image-fields]').forEach(initImageFields);
root.querySelectorAll('[data-sortable]').forEach(initSortable); root.querySelectorAll('[data-sortable]').forEach(initSortable);
root.querySelectorAll('[data-order-options]').forEach(initOrderOptions); root.querySelectorAll('[data-order-options]').forEach(initOrderOptions);