Commit 7b091001 authored by jan.koester's avatar jan.koester
Browse files

test

parent ad43b360
Loading
Loading
Loading
Loading
+225 −3
Changes for data/template/default/editor.html: 225 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -1270,10 +1270,14 @@
                { key: 'hidden', label: 'Hidden', type: 'checkbox', responsive: true }
            ],
            'Image': [
                { key: 'src', label: 'Image Path', type: 'text', placeholder: '/path/to/image.jpg' },
                { key: 'media_id', label: 'Media (from MediaDB)', type: 'media_browse' },
                { key: 'media_ext', label: 'Format', type: 'select', options: ['webp','png','jpg','jpeg','gif','avif','svg'] },
                { key: 'real_width', label: 'Preview Width (px)', type: 'number', min: 0 },
                { key: 'real_height', label: 'Preview Height (px)', type: 'number', min: 0 },
                { key: 'src', label: 'Image Path (manual)', type: 'text', placeholder: '/path/to/image.jpg' },
                { key: 'alt', label: 'Alt Text', type: 'text' },
                { key: 'width', label: 'Width', type: 'text', placeholder: '100%', responsive: true },
                { key: 'height', label: 'Height', type: 'text', placeholder: 'auto', responsive: true },
                { key: 'width', label: 'CSS Width', type: 'text', placeholder: '100%', responsive: true },
                { key: 'height', label: 'CSS Height', type: 'text', placeholder: 'auto', responsive: true },
                { key: 'object_fit', label: 'Object Fit', type: 'select', options: ['cover','contain','fill','none','scale-down'] },
                { key: 'border_radius', label: 'Border Radius', type: 'text', placeholder: '0' },
                { key: 'link_url', label: 'Link URL', type: 'text', placeholder: '/page or https://...' },
@@ -1774,6 +1778,12 @@
                    // Async-populate section list after render
                    setTimeout(() => populateSectionSelect(inputId, String(val)), 0);
                    break;
                case 'media_browse':
                    html += `<div style="display:flex;gap:6px;align-items:center;">`;
                    html += `<input class="prop-input" type="text" id="${inputId}" name="${field.key}" value="${escapeHtml(String(val))}" placeholder="media UUID" style="flex:1;">`;
                    html += `<button type="button" class="prop-save-btn" style="white-space:nowrap;padding:6px 10px;" onclick="openMediaBrowser('${inputId}')">Browse</button>`;
                    html += `</div>`;
                    break;
                case 'textarea':
                    html += `<textarea class="prop-textarea" id="${inputId}" name="${field.key}" placeholder="${field.placeholder || ''}">${escapeHtml(String(val))}</textarea>`;
                    break;
@@ -2529,6 +2539,218 @@
            }
        }

        // --- Media Browser ---
        let _mediaBrowserTargetInput = null;

        async function openMediaBrowser(targetInputId) {
            _mediaBrowserTargetInput = targetInputId;
            // Remove existing modal if any
            let old = document.getElementById('media-browser-modal');
            if (old) old.remove();

            const overlay = document.createElement('div');
            overlay.id = 'media-browser-modal';
            overlay.className = 'modal-overlay';
            overlay.onclick = (e) => { if (e.target === overlay) overlay.remove(); };

            const modal = document.createElement('div');
            modal.className = 'modal-content';
            modal.style.width = '600px';
            modal.style.maxHeight = '80vh';
            modal.style.display = 'flex';
            modal.style.flexDirection = 'column';
            modal.innerHTML = `
                <h3 style="margin:0 0 12px 0; color:#1f2937; font-size:1.1rem;">Select Media from Album</h3>
                <div id="media-browser-nav" style="margin-bottom:8px;"></div>
                <div id="media-browser-content" style="flex:1; overflow-y:auto; min-height:200px;">
                    <p style="color:#6b7280;">Loading albums...</p>
                </div>
                <div style="margin-top:12px; text-align:right;">
                    <button type="button" onclick="document.getElementById('media-browser-modal').remove()" style="padding:6px 16px; background:#6b7280; color:#fff; border:none; border-radius:6px; cursor:pointer;">Cancel</button>
                </div>
            `;
            overlay.appendChild(modal);
            document.body.appendChild(overlay);

            // Load albums
            try {
                const resp = await sendApiRequest([{ command: 'media_list_albums' }]);
                const result = resp.find(r => r.albums);
                if (!result || !result.albums) {
                    document.getElementById('media-browser-content').innerHTML = '<p style="color:#dc2626;">Failed to load albums.</p>';
                    return;
                }
                renderAlbumList(result.albums);
            } catch (e) {
                document.getElementById('media-browser-content').innerHTML = `<p style="color:#dc2626;">Error: ${e.message}</p>`;
            }
        }

        function renderAlbumList(albums) {
            const content = document.getElementById('media-browser-content');
            const nav = document.getElementById('media-browser-nav');
            if (nav) nav.innerHTML = '';

            if (!albums.length) {
                content.innerHTML = '<p style="color:#6b7280;">No albums found. Create albums in Settings &rarr; Media.</p>';
                return;
            }

            let html = '<div style="display:flex; flex-direction:column; gap:4px;">';
            for (const a of albums) {
                html += `<button type="button" onclick="loadAlbumMedia('${escapeHtml(a.id)}', '${escapeHtml(a.name)}')" 
                    style="display:flex; justify-content:space-between; align-items:center; padding:10px 14px; background:#e5e7eb; border:1px solid #d1d5db; border-radius:8px; cursor:pointer; transition:background 0.15s;">
                    <span style="font-weight:600; color:#1f2937;">${escapeHtml(a.name)}</span>
                    <span style="font-size:0.85rem; color:#6b7280;">${a.media_count} items</span>
                </button>`;
            }
            html += '</div>';
            content.innerHTML = html;
        }

        async function loadAlbumMedia(albumId, albumName) {
            const content = document.getElementById('media-browser-content');
            const nav = document.getElementById('media-browser-nav');

            // Back button
            if (nav) {
                nav.innerHTML = `<button type="button" onclick="openMediaBrowser('${_mediaBrowserTargetInput}')" 
                    style="padding:4px 10px; background:#374151; color:#e5e7eb; border:none; border-radius:6px; cursor:pointer; font-size:0.85rem;">&larr; Back to Albums</button>
                    <span style="margin-left:8px; font-weight:600; color:#1f2937;">${escapeHtml(albumName)}</span>`;
            }

            content.innerHTML = '<p style="color:#6b7280;">Loading media...</p>';

            try {
                const resp = await sendApiRequest([{ command: 'media_list_media', album_id: albumId }]);
                const result = resp.find(r => r.media);
                if (!result || !result.media) {
                    content.innerHTML = '<p style="color:#dc2626;">Failed to load media.</p>';
                    return;
                }
                renderMediaGrid(result.media);
            } catch (e) {
                content.innerHTML = `<p style="color:#dc2626;">Error: ${e.message}</p>`;
            }
        }

        function renderMediaGrid(mediaList) {
            const content = document.getElementById('media-browser-content');

            if (!mediaList.length) {
                content.innerHTML = '<p style="color:#6b7280;">Album is empty.</p>';
                return;
            }

            let html = '<div style="display:grid; grid-template-columns:repeat(auto-fill, minmax(120px, 1fr)); gap:8px;">';
            for (const m of mediaList) {
                const thumbUrl = m.url;
                const ext = m.filename.includes('.') ? m.filename.split('.').pop() : 'webp';
                if (m.kind === 'image') {
                    html += `<div onclick="selectMediaItem('${escapeHtml(m.id)}', '${escapeHtml(ext)}', '${escapeHtml(m.filename)}')" 
                        style="cursor:pointer; border:2px solid transparent; border-radius:8px; padding:4px; transition: border-color 0.15s; background:#f3f4f6; text-align:center;"
                        onmouseover="this.style.borderColor='#3b82f6'" onmouseout="this.style.borderColor='transparent'">
                        <img src="${escapeHtml(thumbUrl)}" style="width:100%; height:80px; object-fit:cover; border-radius:6px;" alt="${escapeHtml(m.filename)}">
                        <div style="font-size:0.75rem; color:#374151; margin-top:4px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">${escapeHtml(m.filename)}</div>
                    </div>`;
                } else {
                    html += `<div onclick="selectMediaItem('${escapeHtml(m.id)}', '${escapeHtml(ext)}', '${escapeHtml(m.filename)}')" 
                        style="cursor:pointer; border:2px solid transparent; border-radius:8px; padding:4px; transition: border-color 0.15s; background:#f3f4f6; text-align:center; display:flex; flex-direction:column; align-items:center; justify-content:center; min-height:100px;"
                        onmouseover="this.style.borderColor='#3b82f6'" onmouseout="this.style.borderColor='transparent'">
                        <span style="font-size:2rem;">&#128196;</span>
                        <div style="font-size:0.75rem; color:#374151; margin-top:4px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">${escapeHtml(m.filename)}</div>
                    </div>`;
                }
            }
            html += '</div>';
            content.innerHTML = html;
        }

        function selectMediaItem(mediaId, ext, filename) {
            // Set the media_id input
            const input = document.getElementById(_mediaBrowserTargetInput);
            if (input) {
                input.value = mediaId;
                input.dispatchEvent(new Event('input', { bubbles: true }));
            }
            // Also set media_ext if present
            const extEl = document.getElementById('prop-media_ext');
            if (extEl) {
                extEl.value = ext;
                extEl.dispatchEvent(new Event('change', { bubbles: true }));
            }
            // Build src URL with PREFIX
            const srcEl = document.getElementById('prop-src');
            if (srcEl) {
                const rw = document.getElementById('prop-real_width');
                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;
                if (w > 0 && h > 0) url += '?' + w + 'x' + h;
                srcEl.value = url;
                srcEl.dispatchEvent(new Event('input', { bubbles: true }));
            }
            // Close modal
            const modal = document.getElementById('media-browser-modal');
            if (modal) modal.remove();
            // Trigger auto-save
            if (typeof scheduleAutoSave === 'function') scheduleAutoSave();
        }

        // --- Auto-create preview when real_width/real_height change ---
        async function triggerPreviewCreation() {
            const mediaIdEl = document.getElementById('prop-media_id');
            const rwEl = document.getElementById('prop-real_width');
            const rhEl = document.getElementById('prop-real_height');
            if (!mediaIdEl || !rwEl || !rhEl) return;

            const mediaId = mediaIdEl.value.trim();
            const w = parseInt(rwEl.value, 10) || 0;
            const h = parseInt(rhEl.value, 10) || 0;

            if (!mediaId || w <= 0 || h <= 0) return;

            try {
                await sendApiRequest([{
                    command: 'media_create_preview',
                    media_id: mediaId,
                    width: w,
                    height: h
                }]);
            } catch (e) {
                console.warn('Preview creation failed:', e.message);
            }
        }

        // Hook into auto-save for Image real_width/real_height changes
        const _origScheduleAutoSave = scheduleAutoSave;
        (function() {
            const origSave = window.saveElementProperties || saveElementProperties;
            const patchedSave = async function(uuid, widgetName) {
                if (widgetName === 'Image') {
                    // Rebuild src from media_id + ext + sizes with PREFIX
                    const midEl = document.getElementById('prop-media_id');
                    const extEl = document.getElementById('prop-media_ext');
                    const rwEl = document.getElementById('prop-real_width');
                    const rhEl = document.getElementById('prop-real_height');
                    const srcEl = document.getElementById('prop-src');
                    if (midEl && midEl.value.trim() && extEl && srcEl) {
                        const mid = midEl.value.trim();
                        const ext = extEl.value || 'webp';
                        const w = rwEl ? parseInt(rwEl.value, 10) || 0 : 0;
                        const h = rhEl ? parseInt(rhEl.value, 10) || 0 : 0;
                        let url = PREFIX + '/media/getimage/' + mid + '.' + ext;
                        if (w > 0 && h > 0) url += '?' + w + 'x' + h;
                        srcEl.value = url;
                    }
                    await triggerPreviewCreation();
                }
                return origSave(uuid, widgetName);
            };
            window.saveElementProperties = patchedSave;
        })();

        // Initial setup - Direct initialization
        window.onload = () => {
            setView(false); // Default to desktop view on load
+372 −5

File changed.

Preview size limit exceeded, changes collapsed.

+1082 −0

File added.

Preview size limit exceeded, changes collapsed.

+1475 −0

File added.

Preview size limit exceeded, changes collapsed.

+27 −0
Changes for src/edit.cpp: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1571,6 +1571,33 @@ void blogi::webedit::WebEditor::JsonApi(const int tid, json_object *request, jso
            }
        }

        // --- 3. Delegate to main plugins (e.g., media) ---
        if (!handled && plugins) {
            uuid::uuid authid;
            size_t reqLen = json_object_array_length(request);
            for (size_t ri = 0; ri < reqLen; ++ri) {
                json_object *reqItem = json_object_array_get_idx(request, ri);
                json_object *authObj = nullptr;
                if (json_object_object_get_ex(reqItem, "authid", &authObj)) {
                    const char *authIdStr = json_object_get_string(authObj);
                    if (authIdStr && authIdStr[0] != '\0') {
                        authid = authIdStr;
                    }
                    break;
                }
            }
            for (const Plugin::PluginData *curplg = plugins->getFirstPlugin(); curplg; curplg = curplg->getNextPlg()) {
                PluginApi *api = curplg->getInstace();
                if (api) {
                    api->JsonApi(tid, single_request, single_response, authid);
                    if (json_object_object_get(single_response, "status") != nullptr) {
                        handled = true;
                        break;
                    }
                }
            }
        }

        if (!handled) {
            // If the command was not a global command and was not handled by any plugin
            json_object_object_add(single_response, "status", json_object_new_string("error"));
Loading