From 720739b0971bde4e49103559774bb2bd482d01d8 Mon Sep 17 00:00:00 2001 From: charlie Date: Tue, 24 Dec 2024 16:26:45 +0800 Subject: [PATCH] enhance(plugin): basic plugin setup for web platform --- public/index.html | 1 + resources/index.html | 1 + resources/js/eventemitter3.umd.min.js | 1 + src/main/frontend/components/settings.cljs | 12 ++++++----- src/main/frontend/components/theme.cljs | 7 +++---- src/main/frontend/config.cljs | 6 +++--- src/main/frontend/handler/plugin.cljs | 21 +++++++++++++++----- src/main/frontend/handler/plugin_config.cljs | 20 +++++++++++-------- src/main/frontend/state.cljs | 2 +- src/main/frontend/util.cljc | 3 ++- src/main/frontend/utils.js | 1 - src/main/logseq/api.cljs | 19 +++++++++++------- 12 files changed, 59 insertions(+), 35 deletions(-) create mode 100644 resources/js/eventemitter3.umd.min.js diff --git a/public/index.html b/public/index.html index f97baaaad8..5d28b4b02d 100644 --- a/public/index.html +++ b/public/index.html @@ -50,6 +50,7 @@ + diff --git a/resources/index.html b/resources/index.html index 0465b29fb6..142c44e26d 100644 --- a/resources/index.html +++ b/resources/index.html @@ -49,6 +49,7 @@ const portal = new MagicPortal(worker); + diff --git a/resources/js/eventemitter3.umd.min.js b/resources/js/eventemitter3.umd.min.js new file mode 100644 index 0000000000..98a61fc492 --- /dev/null +++ b/resources/js/eventemitter3.umd.min.js @@ -0,0 +1 @@ +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).EventEmitter3=t()}(this,(function(){"use strict";var e={};return function(e){var t=Object.prototype.hasOwnProperty,n="~";function r(){}function o(e,t,n){this.fn=e,this.context=t,this.once=n||!1}function s(e,t,r,s,i){if("function"!=typeof r)throw new TypeError("The listener must be a function");var c=new o(r,s||e,i),f=n?n+t:t;return e._events[f]?e._events[f].fn?e._events[f]=[e._events[f],c]:e._events[f].push(c):(e._events[f]=c,e._eventsCount++),e}function i(e,t){0==--e._eventsCount?e._events=new r:delete e._events[t]}function c(){this._events=new r,this._eventsCount=0}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),c.prototype.eventNames=function(){var e,r,o=[];if(0===this._eventsCount)return o;for(r in e=this._events)t.call(e,r)&&o.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?o.concat(Object.getOwnPropertySymbols(e)):o},c.prototype.listeners=function(e){var t=n?n+e:e,r=this._events[t];if(!r)return[];if(r.fn)return[r.fn];for(var o=0,s=r.length,i=new Array(s);o (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)) diff --git a/src/main/frontend/handler/plugin_config.cljs b/src/main/frontend/handler/plugin_config.cljs index 134348b37f..5d0d05576d 100644 --- a/src/main/frontend/handler/plugin_config.cljs +++ b/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 diff --git a/src/main/frontend/state.cljs b/src/main/frontend/state.cljs index 4f315822dd..48ff4e7307 100644 --- a/src/main/frontend/state.cljs +++ b/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 diff --git a/src/main/frontend/util.cljc b/src/main/frontend/util.cljc index 15e5589bb8..77b9ceccf4 100644 --- a/src/main/frontend/util.cljc +++ b/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? diff --git a/src/main/frontend/utils.js b/src/main/frontend/utils.js index 2571dcb275..e45bcc205b 100644 --- a/src/main/frontend/utils.js +++ b/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') { diff --git a/src/main/logseq/api.cljs b/src/main/logseq/api.cljs index a599713f5e..e8b70d7ec6 100644 --- a/src/main/logseq/api.cljs +++ b/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]