Browse Source

feat: remember right sidebar width after resizing

Peng Xiao 3 years ago
parent
commit
2b67daf78b
2 changed files with 24 additions and 1 deletions
  1. 2 1
      src/main/frontend/components/right_sidebar.cljs
  2. 22 0
      src/main/frontend/handler/ui.cljs

+ 2 - 1
src/main/frontend/components/right_sidebar.cljs

@@ -13,6 +13,7 @@
             [frontend.extensions.slide :as slide]
             [frontend.state :as state]
             [frontend.ui :as ui]
+            [frontend.handler.ui :as ui-handler]
             [frontend.util :as util]
             [goog.object :as gobj]
             [medley.core :as medley]
@@ -24,7 +25,7 @@
   (when-not (util/mobile?)
     (ui/with-shortcut :ui/toggle-right-sidebar "left"
       [:a.button.fade-link.toggle
-       {:on-click state/toggle-sidebar-open?!}
+       {:on-click ui-handler/toggle-right-sidebar!}
        (ui/icon "layout-sidebar-right" {:style {:fontSize "20px"}})])))
 
 (rum/defc block-cp < rum/reactive

+ 22 - 0
src/main/frontend/handler/ui.cljs

@@ -16,14 +16,36 @@
             [frontend.mobile.util :as mobile]
             [electron.ipc :as ipc]))
 
+(defn- get-css-var-value
+  [var-name]
+  (.getPropertyValue (js/getComputedStyle (.-documentElement js/document)) var-name))
+
 ;; sidebars
+(defn- get-right-sidebar-width
+  []
+  (or (.. (js/document.getElementById "right-sidebar") -style -width)
+      (get-css-var-value "--right-sidebar-width")))
+
+(defn- persist-right-sidebar-width!
+  []
+  (storage/set "ls-right-sidebar-width" (get-right-sidebar-width)))
+
+(defn- restore-right-sidebar-width!
+  []
+  (when-let [width (storage/get "ls-right-sidebar-width")]
+    (.setProperty (.-style (js/document.getElementById "right-sidebar")) "width" width)))
+
 (defn close-left-sidebar!
   []
   (when-let [elem (gdom/getElement "close-left-bar")]
+    (persist-right-sidebar-width!)
     (.click elem)))
 
 (defn toggle-right-sidebar!
   []
+  (if (:ui/sidebar-open? @state/state) 
+    (persist-right-sidebar-width!) 
+    (restore-right-sidebar-width!))
   (state/toggle-sidebar-open?!))
 
 (defn persist-right-sidebar-state!