瀏覽代碼

more embed fix

simo 1 月之前
父節點
當前提交
2b45c0c307
共有 3 個文件被更改,包括 42 次插入10 次删除
  1. 39 9
      templates/home.html
  2. 1 1
      templates/images.html
  3. 2 0
      templates/results.html

+ 39 - 9
templates/home.html

@@ -17,21 +17,51 @@
         />
         <script>
             (function () {
+                function isEmbedMode() {
+                    return (
+                        (window.location.hash || "").indexOf("#embed") !== -1
+                    );
+                }
+
                 function syncEmbedClassFromHash() {
-                    const isEmbed =
-                        (window.location.hash || "").indexOf("#embed") !== -1;
+                    document.body.classList.toggle("embed", isEmbedMode());
+                }
+
+                function wireEmbedPreservingHomeSearchSubmit() {
+                    const form = document.querySelector(
+                        "form.home-search-form",
+                    );
+                    if (!form) return;
+
+                    form.addEventListener("submit", function (ev) {
+                        if (!isEmbedMode()) return;
 
-                    // Toggle a class on <body>. CSS handles the layout changes.
-                    document.body.classList.toggle("embed", isEmbed);
+                        // Default GET form submission omits the fragment.
+                        // In embed mode, submit by navigating to action + querystring + #embed.
+                        ev.preventDefault();
+
+                        const actionAttr =
+                            form.getAttribute("action") || "/search";
+                        const actionUrl = new URL(
+                            actionAttr,
+                            window.location.origin,
+                        );
+                        const params = new URLSearchParams(new FormData(form));
+                        actionUrl.search = params.toString();
+
+                        window.location.assign(actionUrl.toString() + "#embed");
+                    });
+                }
+
+                function init() {
+                    syncEmbedClassFromHash();
+                    wireEmbedPreservingHomeSearchSubmit();
                 }
 
                 if (document.readyState === "loading") {
-                    document.addEventListener(
-                        "DOMContentLoaded",
-                        syncEmbedClassFromHash,
-                    );
+                    document.addEventListener("DOMContentLoaded", init);
                 } else {
-                    syncEmbedClassFromHash();
+                    init();
                 }
 
                 window.addEventListener("hashchange", syncEmbedClassFromHash);

+ 1 - 1
templates/images.html

@@ -13,7 +13,7 @@
 
     <body class="images-view">
         <header>
-            <h1>Simo <span>Search</span></h1>
+            <h1>Simo<span>Search</span></h1>
             <form action="/images" method="GET" class="search-form">
                 <input
                     name="q"

+ 2 - 0
templates/results.html

@@ -33,6 +33,8 @@
                 />
             </form>
 
+            ,
+
             <script>
                 (function () {
                     const isEmbed =