瀏覽代碼

enhance(plugin): basic plugin setup for web platform

charlie 10 月之前
父節點
當前提交
720739b097

+ 1 - 0
public/index.html

@@ -50,6 +50,7 @@
 <script defer src="/static/js/highlight.min.js"></script>
 <script defer src="/static/js/interact.min.js"></script>
 <script defer src="/static/js/marked.min.js"></script>
+<script defer src="/static/js/eventemitter3.umd.min.js"></script>
 <script defer src="/static/js/html2canvas.min.js"></script>
 <script defer src="/static/js/react.production.min.js"></script>
 <script defer src="/static/js/react-dom.production.min.js"></script>

+ 1 - 0
resources/index.html

@@ -49,6 +49,7 @@ const portal = new MagicPortal(worker);
 <script defer src="./js/highlight.min.js"></script>
 <script defer src="./js/interact.min.js"></script>
 <script defer src="./js/marked.min.js"></script>
+<script defer src="./js/eventemitter3.umd.min.js"></script>
 <script defer src="./js/html2canvas.min.js"></script>
 <script defer src="./js/lsplugin.core.js"></script>
 <script defer src="./js/react.production.min.js"></script>

文件差異過大導致無法顯示
+ 0 - 0
resources/js/eventemitter3.umd.min.js


+ 7 - 5
src/main/frontend/components/settings.cljs

@@ -640,10 +640,12 @@
                      (storage/set ::storage-spec/lsp-core-enabled v))]
     [:div.flex.items-center.gap-2
      (ui/toggle on? on-toggle true)
-     (when (not= (boolean value) on?)
-       (ui/button (t :plugin/restart)
-                  :on-click #(js/logseq.api.relaunch)
-                  :small? true :intent "logseq"))]))
+
+     (when (util/electron?)
+       (when (not= (boolean value) on?)
+         (ui/button (t :plugin/restart)
+           :on-click #(js/logseq.api.relaunch)
+           :small? true :intent "logseq")))]))
 
 (rum/defc http-server-enabled-switcher
   [t]
@@ -1112,7 +1114,7 @@
                              (when (= "Enter" (util/ekey e))
                                (update-home-page e)))}]]]])
      (when-not db-based? (whiteboards-switcher-row enable-whiteboards?))
-     (when (and (util/electron?) config/feature-plugin-system-on?)
+     (when (and web-platform? config/feature-plugin-system-on?)
        (plugin-system-switcher-row))
      (when (util/electron?)
        (http-server-switcher-row))

+ 3 - 4
src/main/frontend/components/theme.cljs

@@ -85,11 +85,10 @@
 
     (rum/use-effect!
      #(when config/lsp-enabled?
-        (plugin-handler/setup-install-listener!)
-        (plugin-config-handler/setup-install-listener!)
         (plugin-handler/load-plugin-preferences)
-        (fn []
-          (js/window.apis.removeAllListeners (name :lsp-updates))))
+        (comp
+          (plugin-handler/setup-install-listener!)
+          (plugin-config-handler/setup-install-listener!)))
      [])
 
     (rum/use-effect!

+ 3 - 3
src/main/frontend/config.cljs

@@ -74,9 +74,9 @@
 
 ;; User level configuration for whether plugins are enabled
 (defonce lsp-enabled?
-  (and (util/electron?)
-       (not (false? feature-plugin-system-on?))
-       (state/lsp-enabled?-or-theme)))
+  (and util/plugin-platform?
+    (not (false? feature-plugin-system-on?))
+    (state/lsp-enabled?-or-theme)))
 
 (defn plugin-config-enabled?
   []

+ 16 - 5
src/main/frontend/handler/plugin.cljs

@@ -37,7 +37,7 @@
 (defn invoke-exported-api
   [type & args]
   (try
-    (apply js-invoke (aget js/window.logseq "api") type args)
+    (apply js-invoke (aget js/window.logseq "api") (name type) args)
     (catch :default e (js/console.error e))))
 
 (defn markdown-to-html
@@ -54,9 +54,16 @@
 (defonce stats-url (str central-endpoint "stats.json"))
 (declare select-a-plugin-theme)
 
+(defn setup-global-apis-for-web!
+  []
+  (when (and util/web-platform?
+          (nil? js/window.apis))
+    (let [^js e (js/window.EventEmitter3.)]
+      (set! (. js/window -apis) e))))
+
 (defn load-plugin-preferences
   []
-  (-> (invoke-exported-api "load_user_preferences")
+  (-> (invoke-exported-api :load_user_preferences)
     (p/then #(bean/->clj %))
     (p/then #(state/set-state! :plugin/preferences %))
     (p/catch
@@ -249,8 +256,10 @@
                    ;; reset
                    (js/setTimeout #(state/set-state! :plugin/installing nil) 512)
                    true)]
-
-    (js/window.apis.addListener channel listener)))
+    (js/window.apis.addListener channel listener)
+    ;; teardown
+    (fn []
+      (js/window.apis.removeListener channel listener))))
 
 (defn- normalize-plugin-metadata
   [metadata]
@@ -853,7 +862,9 @@
   [callback]
   (if (not config/lsp-enabled?)
     (callback)
-    (init-plugins! callback)))
+    (do
+      (setup-global-apis-for-web!)
+      (init-plugins! callback))))
 
 (comment
   {:pending (count (:plugin/updates-pending @state/state))

+ 12 - 8
src/main/frontend/handler/plugin_config.cljs

@@ -110,18 +110,22 @@ returns map of plugins to install and uninstall"
 (defn setup-install-listener!
   "Sets up a listener for the lsp-installed event to update plugins.edn"
   []
-  (let [listener (fn listener [_ e]
+  (let [channel (name :lsp-updates)
+        listener (fn listener [_ e]
                    (when-let [{:keys [status payload only-check]} (bean/->clj e)]
                      (when (and (= status "completed") (not only-check))
                        (let [{:keys [theme effect]} payload]
                          (add-or-update-plugin
-                          (assoc payload
-                                 :version (:installed-version payload)
-                                 :effect (boolean effect)
-                                 ;; Manual installation doesn't have theme field but
-                                 ;; plugin.edn requires this field
-                                 :theme (boolean theme)))))))]
-    (js/window.apis.addListener (name :lsp-updates) listener)))
+                           (assoc payload
+                             :version (:installed-version payload)
+                             :effect (boolean effect)
+                             ;; Manual installation doesn't have theme field but
+                             ;; plugin.edn requires this field
+                             :theme (boolean theme)))))))]
+    (js/window.apis.addListener channel listener)
+    ;;teardown
+    (fn []
+      (js/window.apis.removeListener channel listener))))
 
 (defn start
   "This component has just one responsibility on start, to create a plugins.edn

+ 1 - 1
src/main/frontend/state.cljs

@@ -202,7 +202,7 @@
       :mobile/app-state-change                 (atom nil)
 
       ;; plugin
-      :plugin/enabled                        (and (util/electron?)
+      :plugin/enabled                        (and util/plugin-platform?
                                                   ;; true false :theme-only
                                                   ((fnil identity true) (storage/get ::storage-spec/lsp-core-enabled)))
       :plugin/preferences                    nil

+ 2 - 1
src/main/frontend/util.cljc

@@ -146,7 +146,8 @@
    (do
      (def nfs? (and (not (electron?))
                     (not (mobile-util/native-platform?))))
-     (def web-platform? nfs?)))
+     (def web-platform? nfs?)
+     (def plugin-platform? (or web-platform? (electron?)))))
 
 #?(:cljs
    (defn file-protocol?

+ 0 - 1
src/main/frontend/utils.js

@@ -2,7 +2,6 @@ import path from 'path/path.js'
 
 // TODO split the capacitor abilities to a separate file for capacitor APIs
 import { Capacitor } from '@capacitor/core'
-import { StatusBar, Style } from '@capacitor/status-bar'
 import { Clipboard as CapacitorClipboard } from '@capacitor/clipboard'
 
 if (typeof window === 'undefined') {

+ 12 - 7
src/main/logseq/api.cljs

@@ -354,13 +354,18 @@
 
 (def ^:export load_user_preferences
   (fn []
-    (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))))
+    (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 {}))))
 
 (def ^:export save_user_preferences
   (fn [^js data]

部分文件因文件數量過多而無法顯示