Commit 25b417d4 authored by jan.koester's avatar jan.koester
Browse files

test

parent 7b091001
Loading
Loading
Loading
Loading
+63 −1
Changes for data/template/default/editor.html: 63 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -1472,6 +1472,16 @@
                form.addEventListener('input', scheduleAutoSave);
                form.addEventListener('change', scheduleAutoSave);
            }

            // Image: attach aspect ratio auto-calc and load original dimensions
            if (widgetName === 'Image') {
                _attachSizeAutoCalc();
                const mid = properties['media_id'] || '';
                const ext = properties['media_ext'] || 'webp';
                if (mid) {
                    _loadImageNaturalSize(PREFIX + '/media/getimage/' + mid + '.' + ext);
                }
            }
        }

        /**
@@ -2666,6 +2676,54 @@
            content.innerHTML = html;
        }

        // --- Aspect ratio auto-calculation for Image ---
        let _imageNaturalWidth = 0;
        let _imageNaturalHeight = 0;

        function _getImageAspectRatio() {
            if (_imageNaturalWidth > 0 && _imageNaturalHeight > 0)
                return _imageNaturalWidth / _imageNaturalHeight;
            return 0;
        }

        function _attachSizeAutoCalc() {
            const rwEl = document.getElementById('prop-real_width');
            const rhEl = document.getElementById('prop-real_height');
            if (!rwEl || !rhEl) return;

            // Remove old listeners by replacing elements
            rwEl.addEventListener('change', function() {
                const ratio = _getImageAspectRatio();
                if (ratio <= 0) return;
                const w = parseInt(this.value, 10) || 0;
                if (w > 0) {
                    rhEl.value = Math.round(w / ratio);
                    rhEl.dispatchEvent(new Event('input', { bubbles: true }));
                }
            });
            rhEl.addEventListener('change', function() {
                const ratio = _getImageAspectRatio();
                if (ratio <= 0) return;
                const h = parseInt(this.value, 10) || 0;
                if (h > 0) {
                    rwEl.value = Math.round(h * ratio);
                    rwEl.dispatchEvent(new Event('input', { bubbles: true }));
                }
            });
        }

        function _loadImageNaturalSize(url) {
            _imageNaturalWidth = 0;
            _imageNaturalHeight = 0;
            if (!url) return;
            const img = new Image();
            img.onload = function() {
                _imageNaturalWidth = img.naturalWidth;
                _imageNaturalHeight = img.naturalHeight;
            };
            img.src = url;
        }

        function selectMediaItem(mediaId, ext, filename) {
            // Set the media_id input
            const input = document.getElementById(_mediaBrowserTargetInput);
@@ -2679,6 +2737,10 @@
                extEl.value = ext;
                extEl.dispatchEvent(new Event('change', { bubbles: true }));
            }
            // Load original image to get natural dimensions for aspect ratio
            const origUrl = PREFIX + '/media/getimage/' + mediaId + '.' + ext;
            _loadImageNaturalSize(origUrl);

            // Build src URL with PREFIX
            const srcEl = document.getElementById('prop-src');
            if (srcEl) {
@@ -2686,7 +2748,7 @@
                const rh = document.getElementById('prop-real_height');
                const w = rw ? parseInt(rw.value, 10) || 0 : 0;
                const h = rh ? parseInt(rh.value, 10) || 0 : 0;
                let url = PREFIX + '/media/getimage/' + mediaId + '.' + ext;
                let url = origUrl;
                if (w > 0 && h > 0) url += '?' + w + 'x' + h;
                srcEl.value = url;
                srcEl.dispatchEvent(new Event('input', { bubbles: true }));