Browse Source

enhance(plugin): install plugin from web url

charlie 10 months ago
parent
commit
890a06fa19

+ 9 - 1
libs/src/LSPlugin.core.ts

@@ -512,6 +512,10 @@ class PluginLocal extends EventEmitter<
 
     if (this.isWebPlugin) {
       // TODO: strategy for Logseq plugins center
+      if (this.installedFromUserWebUrl) {
+        return `${this.installedFromUserWebUrl}/${filePath}`
+      }
+
       return `https://pub-80f42b85b62c40219354a834fcf2bbfa.r2.dev/${path.join(localRoot, filePath)}`
     }
 
@@ -565,7 +569,7 @@ class PluginLocal extends EventEmitter<
       })
 
     const { repo, version } = this._options
-    const localRoot = (this._localRoot = this.isWebPlugin ? `${repo}/${version}` : safetyPathNormalize(url))
+    const localRoot = (this._localRoot = this.isWebPlugin ? `${repo || url}/${version}` : safetyPathNormalize(url))
     const logseq: Partial<LSPluginPkgConfig> = pkg.logseq || {}
     const validateEntry = (main) => main && /\.(js|html)$/.test(main)
 
@@ -994,6 +998,10 @@ class PluginLocal extends EventEmitter<
     return this._ctx.isWebPlatform || !!this.options.webPkg
   }
 
+  get installedFromUserWebUrl() {
+    return this.isWebPlugin && this.options.webPkg?.installedFromUserWebUrl
+  }
+
   get layoutCore(): any {
     // @ts-expect-error
     return window.frontend.modules.layout.core

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

@@ -480,11 +480,14 @@
 
 (rum/defc load-from-web-url-container
   []
-  (let [[url set-url!] (rum/use-state "")
+  (let [[url set-url!] (rum/use-state "http://127.0.0.1:8080/")
         [pending? set-pending?] (rum/use-state false)
         handle-submit! (fn []
                          (set-pending? true)
-                         (-> (p/delay 3000)
+                         (-> (plugin-handler/load-plugin-from-web-url! url)
+                           (p/then #(do (notification/show! "New plugin registered!" :success)
+                                      (shui/dialog-close!)))
+                           (p/catch #(notification/show! (str %) :error))
                            (p/finally
                              #(set-pending? false))))]
 
@@ -503,7 +506,7 @@
      [:div.flex.justify-end
       (shui/button {:disabled (or pending? (string/blank? url))
                     :on-click handle-submit!}
-        (if pending? (ui/loading) "Save"))]]))
+        (if pending? (ui/loading) "Install"))]]))
 
 (rum/defc auto-check-for-updates-control
   []

+ 40 - 0
src/main/frontend/handler/plugin.cljs

@@ -773,6 +773,46 @@
     (when config/lsp-enabled?
       (hook-plugin-app (str :after-command-invoked type) nil))))
 
+(defn load-plugin-from-web-url!
+  [url]
+  (if (not (and (string? url) (string/starts-with? url "http")))
+    (p/rejected (js/Error. "Invalid web url"))
+    (p/let [_ (p/delay 1000)
+            url (string/replace url #"/+$" "")
+            github? (string/includes? url "github.com")
+            github-repo (when github?
+                          (some-> (re-find #"github.com/([^/]+/[^/]+)" url) (last)))
+            package-url (if github?
+                          (some-> github-repo
+                             (plugin-common-handler/get-web-plugin-checker-url!))
+                          (str url "/package.json"))
+            ^js res (js/window.fetch package-url)
+            package (if (and (.-ok res)
+                          (= (.-status res) 200))
+                      (-> (.json res)
+                        (p/then bean/->clj))
+                      (throw (js/Error. (.text res))))
+            logseq (or (:logseq package)
+                     (throw (js/Error. "Illegal logseq package")))]
+      (let [id (if github?
+                 (some-> github-repo (string/replace "/" "_"))
+                 (or (:id logseq) (:name package)))
+            repo (or github-repo id)
+            theme? (some? (or (:theme logseq) (:themes logseq)))]
+
+        (plugin-common-handler/emit-lsp-updates!
+          {:status :completed
+           :only-check false
+           :payload {:id id
+                     :repo repo
+                     :dst repo
+                     :theme theme?
+                     :web-pkg (cond-> package
+
+                                (not github?)
+                                (assoc :installedFromUserWebUrl url))}}))
+      url)))
+
 ;; components
 (rum/defc lsp-indicator < rum/reactive
   []