Просмотр исходного кода

enhance(plugin): plugin settings persistence for web platform

charlie 1 год назад
Родитель
Сommit
90c4d5eb9b

+ 6 - 3
src/main/frontend/components/plugins.cljs

@@ -501,7 +501,7 @@
      [:div.flex.items-center.l
       (category-tabs t total-nums category #(reset! *category %))
 
-      (when (and develop-mode? (not market?))
+      (when (and develop-mode? (util/electron?) (not market?))
         [:div
          (ui/tippy {:html  [:div (t :plugin/unpacked-tips)]
                     :arrow true}
@@ -891,8 +891,11 @@
                                true nil (get coming-updates pid)))
            (:id item)))]
 
-      (when (seq sorted-plugins)
-        (lazy-items-loader load-more-pages!))]]))
+      (if (seq sorted-plugins)
+        (lazy-items-loader load-more-pages!)
+        [:div.flex.items-center.justify-center.py-16.flex-col.gap-2.opacity-30
+         (shui/tabler-icon "list-search" {:size 40})
+         [:span.text-sm "Nothing Founded."]])]]))
 
 (rum/defcs waiting-coming-updates
   < rum/reactive

+ 29 - 19
src/main/frontend/handler/plugin.cljs

@@ -13,6 +13,7 @@
             [frontend.state :as state]
             [medley.core :as medley]
             [frontend.fs :as fs]
+            [frontend.idb :as idb]
             [electron.ipc :as ipc]
             [cljs-bean.core :as bean]
             [clojure.string :as string]
@@ -590,38 +591,47 @@
 
 (defn get-ls-dotdir-root
   []
-  (ipc/ipc "getLogseqDotDirRoot"))
+  (if (util/electron?)
+    (ipc/ipc "getLogseqDotDirRoot")
+    "LSPUserDotRoot/"))
 
 (defn make-fn-to-load-dotdir-json
-  [dirname default]
+  [dirname ^js default]
   (fn [key]
     (when-let [key (and key (name key))]
-      (p/let [repo ""
-              path (get-ls-dotdir-root)
-              exist? (fs/file-exists? path dirname)
-              _ (when-not exist? (fs/mkdir! (util/node-path.join path dirname)))
-              path (util/node-path.join path dirname (str key ".json"))
-              _ (fs/create-if-not-exists repo nil path (or default "{}"))
-              json (fs/read-file nil path)]
-        [path (js/JSON.parse json)]))))
+      (let [repo ""
+            path (get-ls-dotdir-root)
+            path (util/node-path.join path dirname (str key ".json"))]
+        (if (util/electron?)
+          (p/let [exist? (fs/file-exists? path dirname)
+                  _ (when-not exist? (fs/mkdir! (util/node-path.join path dirname)))
+                  _ (fs/create-if-not-exists repo nil path (js/JSON.stringify default))
+                  json (fs/read-file nil path)]
+            [path (js/JSON.parse json)])
+          (p/let [data (idb/get-item path)]
+            [path (or data default)]))))))
 
 (defn make-fn-to-save-dotdir-json
   [dirname]
-  (fn [key content]
+  (fn [key ^js data]
     (when-let [key (and key (name key))]
-      (p/let [repo ""
-              path (get-ls-dotdir-root)
-              path (util/node-path.join path dirname (str key ".json"))]
-        (fs/write-file! repo nil path content {:skip-compare? true})))))
+      (let [repo ""
+            path (get-ls-dotdir-root)
+            path (util/node-path.join path dirname (str key ".json"))]
+        (if (util/electron?)
+          (fs/write-file! repo nil path (js/JSON.stringify data nil 2) {:skip-compare? true})
+          (idb/set-item! path data))))))
 
 (defn make-fn-to-unlink-dotdir-json
   [dirname]
   (fn [key]
     (when-let [key (and key (name key))]
-      (p/let [repo ""
-              path (get-ls-dotdir-root)
-              path (util/node-path.join path dirname (str key ".json"))]
-        (fs/unlink! repo path nil)))))
+      (let [repo ""
+            path (get-ls-dotdir-root)
+            path (util/node-path.join path dirname (str key ".json"))]
+        (if (util/electron?)
+          (fs/unlink! repo path nil)
+          (idb/remove-item! path))))))
 
 (defn show-themes-modal!
   ([] (show-themes-modal! false))

+ 19 - 18
src/main/logseq/api.cljs

@@ -19,6 +19,7 @@
             [frontend.handler.recent :as recent-handler]
             [frontend.handler.route :as route-handler]
             [frontend.db :as db]
+            [frontend.idb :as idb]
             [frontend.db.async :as db-async]
             [frontend.db.model :as db-model]
             [frontend.db.query-custom :as query-custom]
@@ -354,35 +355,35 @@
 
 (def ^:export load_user_preferences
   (fn []
-    (if (util/electron?)
-      (p/let [repo ""
-              path (plugin-handler/get-ls-dotdir-root)
-              path (util/node-path.join path "preferences.json")
-              _ (fs/create-if-not-exists repo nil path)
-              json (fs/read-file nil path)
-              json (if (string/blank? json) "{}" json)]
-        (js/JSON.parse json))
-      ;; TODO: for web
-      (do
-        (js/console.warn "==> plugin:" "load user preferences for Web!")
-        #js {}))))
+    (let [repo ""
+          path (plugin-handler/get-ls-dotdir-root)
+          path (util/node-path.join path "preferences.json")]
+      (if (util/electron?)
+        (p/let [_ (fs/create-if-not-exists repo nil path)
+                json (fs/read-file nil path)
+                json (if (string/blank? json) "{}" json)]
+          (js/JSON.parse json))
+        (p/let [json (idb/get-item path)]
+          (or json #js {}))))))
 
 (def ^:export save_user_preferences
   (fn [^js data]
     (when data
-      (p/let [repo ""
-              path (plugin-handler/get-ls-dotdir-root)
-              path (util/node-path.join path "preferences.json")]
-        (fs/write-file! repo nil path (js/JSON.stringify data nil 2) {:skip-compare? true})))))
+      (let [repo ""
+            path (plugin-handler/get-ls-dotdir-root)
+            path (util/node-path.join path "preferences.json")]
+        (if (util/electron?)
+          (fs/write-file! repo nil path (js/JSON.stringify data nil 2) {:skip-compare? true})
+          (idb/set-item! path data))))))
 
 (def ^:export load_plugin_user_settings
   ;; results [path data]
-  (plugin-handler/make-fn-to-load-dotdir-json "settings" "{}"))
+  (plugin-handler/make-fn-to-load-dotdir-json "settings" #js {}))
 
 (def ^:export save_plugin_user_settings
   (fn [key ^js data]
     ((plugin-handler/make-fn-to-save-dotdir-json "settings")
-     key (js/JSON.stringify data nil 2))))
+     key data)))
 
 (def ^:export unlink_plugin_user_settings
   (plugin-handler/make-fn-to-unlink-dotdir-json "settings"))