enhance(plugins): persistance strategy for the installed web plugins

This commit is contained in:
charlie
2024-12-26 00:33:09 +08:00
parent 1af1c14edc
commit 5c6290339a
7 changed files with 76 additions and 45 deletions

View File

@@ -593,7 +593,8 @@ class PluginLocal extends EventEmitter<
if (this.isInstalledInDotRoot) {
this._id = path.basename(localRoot)
} else {
if (logseq.id) {
// development mode
if (!this.isWebPlugin && logseq.id) {
this._id = logseq.id
} else {
logseq.id = this.id
@@ -985,7 +986,7 @@ class PluginLocal extends EventEmitter<
}
get isWebPlugin() {
return !!this.options.webPkg
return this._ctx.isWebPlatform || !!this.options.webPkg
}
get layoutCore(): any {
@@ -1092,14 +1093,19 @@ class PluginLocal extends EventEmitter<
this._sdk = value
}
toJSON() {
toJSON(settings = true) {
const json = { ...this.options } as any
json.id = this.id
json.err = this.loadErr
json.usf = this.dotSettingsFile
json.iir = this.isInstalledInDotRoot
json.lsr = this._resolveResourceFullUrl('/')
json.settings = json.settings?.toJSON()
if (settings) {
json.settings = json.settings?.toJSON()
} else {
delete json.settings
}
return json
}
@@ -1336,7 +1342,7 @@ class LSPluginCore
this.emit('registered', pluginLocal)
// external plugins
if (!pluginLocal.isInstalledInDotRoot) {
if (!pluginLocal.isWebPlugin && !pluginLocal.isInstalledInDotRoot) {
externals.add(url)
}
}

View File

@@ -1078,7 +1078,7 @@
dependencies:
"@types/ms" "*"
"@types/dompurify@2.5.4":
"@types/dompurify@2.4.0":
version "2.4.0"
resolved "https://registry.yarnpkg.com/@types/dompurify/-/dompurify-2.4.0.tgz#fd9706392a88e0e0e6d367f3588482d817df0ab9"
integrity sha512-IDBwO5IZhrKvHFUl+clZxgf3hn2b/lU6H1KaBShPkQyGJUQ0xwebezIPSuiyGwfz1UzJWQl4M7BDxtHtCCPlTg==

View File

@@ -136,6 +136,8 @@
(defn start!
[render]
(idb/start)
(test/setup-test!)
(get-system-info)
(set-global-error-notification!)
@@ -156,7 +158,6 @@
(p/catch (fn [_e]
(notification/show! "Sorry, it seems that your browser doesn't support IndexedDB, we recommend to use latest Chrome(Chromium) or Firefox(Non-private mode)." :error false)
(state/set-indexedb-support! false))))
(idb/start)
(react/run-custom-queries-when-idle!)

View File

@@ -41,12 +41,13 @@
(js/console.log "debug:install-or-update" mft)
(-> (fetch-web-plugin-entry-info repo version)
(p/then (fn [{:keys [_version] :as web-pkg}]
(let [web-pkg (merge web-pkg (dissoc mft :stat))]
(emit-lsp-updates!
{:status :completed
:only-check only-check
:payload (if only-check
(assoc mft :latest-version version :latest-notes "TODO: update notes")
(assoc mft :dst repo :installed-version version :web-pkg (merge web-pkg mft)))})))
{:status :completed
:only-check only-check
:payload (if only-check
(assoc mft :latest-version version :latest-notes "TODO: update notes")
(assoc mft :dst repo :installed-version version :web-pkg web-pkg))}))))
(p/catch (fn [^js e]
(emit-lsp-updates!
{:status :error

View File

@@ -220,9 +220,12 @@
;; register plugin
(-> (js/LSPluginCore.register (bean/->js {:key id :url dst :webPkg web-pkg}))
(p/then (fn []
(when theme (js/setTimeout #(select-a-plugin-theme id) 300))
(notification/show!
(t :plugin/installed-plugin name) :success)))
(when-let [^js pl (get-plugin-inst id)]
(when theme (js/setTimeout #(select-a-plugin-theme id) 300))
(when (.-isWebPlugin pl)
(invoke-exported-api :save_installed_web_plugin (.toJSON pl false)))
(notification/show!
(t :plugin/installed-plugin name) :success))))
(p/catch (fn [^js e]
(notification/show!
(str "Install failed: " name "\n" (.-message e))
@@ -658,12 +661,17 @@
(defn- get-user-default-plugins
[]
(p/catch
(p/let [files ^js (ipc/ipc "getUserDefaultPlugins")
files (js->clj files)]
(map #(hash-map :url %) files))
(fn [e]
(js/console.error e))))
(-> (if (util/electron?)
(ipc/ipc "getUserDefaultPlugins")
(invoke-exported-api :load_installed_web_plugins))
(p/then #(bean/->clj %))
(p/then (fn [plugins]
(if (util/electron?)
(map #(hash-map :url %) plugins)
(some->> (vals plugins)
(filter #(:url %))))))
(p/catch (fn [e]
(js/console.error "[get-user-default-plugins:error]" e)))))
(defn set-auto-checking!
[v]
@@ -787,14 +795,14 @@
_ (doto js/LSPluginCore
(.on "registered"
(fn [^js pl]
(register-plugin
(bean/->clj (.parse js/JSON (.stringify js/JSON pl))))))
(fn [^js pl]
(register-plugin
(bean/->clj (.parse js/JSON (.stringify js/JSON pl))))))
(.on "reloaded"
(fn [^js pl]
(register-plugin
(bean/->clj (.parse js/JSON (.stringify js/JSON pl))))))
(fn [^js pl]
(register-plugin
(bean/->clj (.parse js/JSON (.stringify js/JSON pl))))))
(.on "unregistered" (fn [pid]
(let [pid (keyword pid)]
@@ -820,7 +828,7 @@
(.on "themes-changed" (fn [^js themes]
(swap! state/state assoc :plugin/installed-themes
(vec (mapcat (fn [[pid vs]] (mapv #(assoc % :pid pid) (bean/->clj vs))) (bean/->clj themes))))))
(vec (mapcat (fn [[pid vs]] (mapv #(assoc % :pid pid) (bean/->clj vs))) (bean/->clj themes))))))
(.on "theme-selected" (fn [^js theme]
(let [theme (bean/->clj theme)
@@ -837,31 +845,31 @@
custom-theme (dissoc themes :mode)
mode (:mode themes)]
(state/set-custom-theme! {:light (if (nil? (:light custom-theme)) {:mode "light"} (:light custom-theme))
:dark (if (nil? (:dark custom-theme)) {:mode "dark"} (:dark custom-theme))})
:dark (if (nil? (:dark custom-theme)) {:mode "dark"} (:dark custom-theme))})
(state/set-theme-mode! mode))))
(.on "settings-changed" (fn [id ^js settings]
(let [id (keyword id)]
(when (and settings
(contains? (:plugin/installed-plugins @state/state) id))
(contains? (:plugin/installed-plugins @state/state) id))
(update-plugin-settings-state id (bean/->clj settings))))))
(.on "ready" (fn [^js perf-table]
(when-let [plugins (and perf-table (.entries perf-table))]
(->> plugins
(keep
(fn [[_k ^js v]]
(when-let [end (and (some-> v (.-o) (.-disabled) (not))
(.-e v))]
(when (and (number? end)
;; valid end time
(> end 0)
;; greater than 6s
(> (- end (.-s v)) 6000))
v))))
((fn [perfs]
(doseq [perf perfs]
(state/pub-event! [:plugin/loader-perf-tip (bean/->clj perf)])))))))))
(keep
(fn [[_k ^js v]]
(when-let [end (and (some-> v (.-o) (.-disabled) (not))
(.-e v))]
(when (and (number? end)
;; valid end time
(> end 0)
;; greater than 6s
(> (- end (.-s v)) 6000))
v))))
((fn [perfs]
(doseq [perf perfs]
(state/pub-event! [:plugin/loader-perf-tip (bean/->clj perf)])))))))))
default-plugins (get-user-default-plugins)
@@ -883,6 +891,7 @@
(if (not config/lsp-enabled?)
(callback)
(do
(idb/start)
(setup-global-apis-for-web!)
(init-plugins! callback))))

View File

@@ -64,4 +64,5 @@
(defn start
"This component's only responsibility is to create a Store object"
[]
(reset! store (idb-keyval/newStore "localforage" "keyvaluepairs" 2)))
(when (nil? @store)
(reset! store (idb-keyval/newStore "localforage" "keyvaluepairs" 2))))

View File

@@ -218,7 +218,6 @@
(def ^:export save_plugin_config
(fn [path ^js data]
(let [repo ""
path (util/node-path.join path "package.json")]
(fs/write-file! repo nil path (js/JSON.stringify data nil 2) {:skip-compare? true}))))
@@ -388,6 +387,20 @@
((plugin-handler/make-fn-to-save-dotdir-json "settings")
key data)))
(defn ^:export load_installed_web_plugins
[]
(let [getter (plugin-handler/make-fn-to-load-dotdir-json "installed-plugins-for-web" #js {})]
(some-> (getter :all) (p/then second))))
(defn ^:export save_installed_web_plugin
[^js plugin]
(when-let [id (some-> plugin (.-key))]
(let [setter (plugin-handler/make-fn-to-save-dotdir-json "installed-plugins-for-web")
plugin (js/JSON.parse (js/JSON.stringify plugin))]
(p/let [^js plugins (or (load_installed_web_plugins) #js {})]
(gobj/set plugins id plugin)
(setter :all plugins)))))
(def ^:export unlink_plugin_user_settings
(plugin-handler/make-fn-to-unlink-dotdir-json "settings"))