Commit 8f63a33e authored by Jan Köster's avatar Jan Köster
Browse files

postion renderer fixed

parent 085711d2
Loading
Loading
Loading
Loading
+23 −0
Changes for editor/src/webedit_api.cpp: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1418,6 +1418,29 @@ void webedit::Api::handlePreviewPage(libhttppp::HttpRequest &curreq,
        widgetHtml = renderTree(doc.root);
    }

    // Preserve the preview scroll position across reloads (the preview
    // iframe navigates to a fresh document on every refresh, so scroll
    // state has to round-trip through the iframe's own sessionStorage).
    {
        std::string scrollKey = "blogi_preview_scroll_" + sessionid;
        json_object *keyJson = json_object_new_string(scrollKey.c_str());
        std::string keyLiteral = json_object_to_json_string_ext(keyJson, JSON_C_TO_STRING_NOSLASHESCAPE);
        json_object_put(keyJson);
        widgetHtml +=
            "<script>(function(){"
            "var K=" + keyLiteral + ";"
            "try{var y=sessionStorage.getItem(K);"
            "if(y!==null)window.scrollTo(0,parseInt(y,10)||0);}catch(e){}"
            "var save=function(){try{sessionStorage.setItem(K,window.scrollY||window.pageYOffset||0);}catch(e){}};"
            "window.addEventListener('load',function(){"
            "try{var y2=sessionStorage.getItem(K);"
            "if(y2!==null)window.scrollTo(0,parseInt(y2,10)||0);}catch(e){}"
            "});"
            "window.addEventListener('scroll',save,{passive:true});"
            "window.addEventListener('beforeunload',save);"
            "})();</script>";
    }

    json_object *resp = json_object_new_object();
    json_object_object_add(resp, "blogUrl", json_object_new_string(blogUrl.c_str()));
    json_object_object_add(resp, "authid", json_object_new_string(authid.c_str()));