Commit 081da5b4 authored by jan.koester's avatar jan.koester
Browse files

test

parent f53d8800
Loading
Loading
Loading
Loading
+15 −28
Original line number Diff line number Diff line
@@ -5,42 +5,29 @@
var Preview = (function() {
    'use strict';

    var shadow = null;
    var iframe = null;

    function getShadow() {
        if (shadow) return shadow;
    function getIframe() {
        if (iframe) return iframe;
        var host = document.getElementById('preview-frame');
        shadow = host.attachShadow({ mode: 'open' });
        return shadow;
    }

    function getThemeStyle() {
        var theme = document.documentElement.getAttribute('data-theme') || 'dark';
        if (theme === 'light') {
            return ':host{display:block;height:100%;background:#fff;color:#1e1e2e;}' +
                   'div.preview-body{font-family:sans-serif;padding:16px;margin:0;}';
        }
        return ':host{display:block;height:100%;background:#1e1e2e;color:#cdd6f4;}' +
               'div.preview-body{font-family:sans-serif;padding:16px;margin:0;}';
        host.innerHTML = '';
        iframe = document.createElement('iframe');
        iframe.id = 'preview-iframe';
        iframe.style.cssText = 'width:100%;height:100%;border:none;';
        host.appendChild(iframe);
        return iframe;
    }

    function refresh() {
        EditorApi.getPreview().then(function(resp) {
            var s = getShadow();
            s.innerHTML = '<style>' + getThemeStyle() + '</style>' +
                          '<div class="preview-body">' + (resp.html || '') + '</div>';
        }).catch(function(err) {
            console.error('Preview error:', err);
        });
        var f = getIframe();
        // Reload full page preview with cache-busting timestamp
        f.src = '/api/preview/page?t=' + Date.now();
    }

    function clear() {
        var s = getShadow();
        var theme = document.documentElement.getAttribute('data-theme') || 'dark';
        var color = theme === 'light' ? '#666' : '#999';
        s.innerHTML = '<style>' + getThemeStyle() + '</style>' +
                      '<div class="preview-body" style="padding:40px;text-align:center;color:' + color + '">' +
                      '<p>Vorschau wird hier angezeigt.</p></div>';
        var f = getIframe();
        f.srcdoc = '<html><body style="display:flex;align-items:center;justify-content:center;height:100vh;margin:0;font-family:sans-serif;color:#999;background:#1e1e2e;">' +
                   '<p>Vorschau wird hier angezeigt.</p></body></html>';
    }

    return {
+32 −0
Original line number Diff line number Diff line
@@ -133,6 +133,12 @@ bool webedit::Api::handleRequest(libhttppp::HttpRequest &curreq, const int tid,
        return true;
    }

    // Route: /api/preview/page (GET) — returns full HTML page for iframe
    if (path == "/api/preview/page") {
        handlePreviewPage(curreq, sessionid);
        return true;
    }

    // Route: /api/preview
    if (path == "/api/preview") {
        handlePreview(curreq, sessionid);
@@ -749,6 +755,32 @@ void webedit::Api::handlePreview(libhttppp::HttpRequest &curreq,
    json_object_put(resp);
}

void webedit::Api::handlePreviewPage(libhttppp::HttpRequest &curreq,
                                      const std::string &sessionid) {
    auto &doc = getDocState(sessionid);
    std::lock_guard<std::mutex> lk(doc.mtx);

    std::string widgetHtml = renderTree(doc.root);

    // Build a complete HTML page
    std::string page;
    page += "<!DOCTYPE html>\n<html>\n<head>\n";
    page += "<meta charset=\"utf-8\">\n";
    page += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n";
    page += "<style>\n";
    page += "html, body { margin: 0; padding: 0; }\n";
    page += "body { font-family: sans-serif; }\n";
    page += "</style>\n";
    page += "</head>\n<body>\n";
    page += widgetHtml;
    page += "\n</body>\n</html>";

    libhttppp::HttpResponse curres;
    curres.setState(HTTP200);
    curres.setContentType("text/html");
    curres.send(curreq, page.data(), page.size());
}

// --- Export XML ---

void webedit::Api::handleExportXml(libhttppp::HttpRequest &curreq,
+1 −0
Original line number Diff line number Diff line
@@ -81,6 +81,7 @@ namespace webedit {
        void handleGetSchema(libhttppp::HttpRequest &curreq, const std::string &sessionid,
                             const std::string &uuid);
        void handlePreview(libhttppp::HttpRequest &curreq, const std::string &sessionid);
        void handlePreviewPage(libhttppp::HttpRequest &curreq, const std::string &sessionid);
        void handleExportXml(libhttppp::HttpRequest &curreq, const std::string &sessionid);
        void handleImportXml(libhttppp::HttpRequest &curreq, const std::string &sessionid);
        void handleSaveDocument(libhttppp::HttpRequest &curreq, const std::string &sessionid);