plugins.cljs 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. (ns frontend.components.plugins
  2. (:require [cljs-bean.core :as bean]
  3. [clojure.string :as string]
  4. [electron.ipc :as ipc]
  5. [frontend.components.plugins-settings :as plugins-settings]
  6. [frontend.components.svg :as svg]
  7. [frontend.config :as config]
  8. [frontend.context.i18n :refer [t]]
  9. [frontend.handler.common.plugin :as plugin-common-handler]
  10. [frontend.handler.editor :as editor-handler]
  11. [frontend.handler.notification :as notification]
  12. [frontend.handler.plugin :as plugin-handler]
  13. [frontend.handler.plugin-config :as plugin-config-handler]
  14. [frontend.handler.ui :as ui-handler]
  15. [frontend.mixins :as mixins]
  16. [frontend.rum :as rum-utils]
  17. [frontend.search :as search]
  18. [frontend.state :as state]
  19. [frontend.storage :as storage]
  20. [frontend.ui :as ui]
  21. [frontend.util :as util]
  22. [logseq.shui.ui :as shui]
  23. [promesa.core :as p]
  24. [rum.core :as rum]
  25. [frontend.hooks :as hooks]))
  26. (declare open-waiting-updates-modal!)
  27. (defonce PER-PAGE-SIZE 15)
  28. (def *dirties-toggle-items (atom {}))
  29. (defn- clear-dirties-states!
  30. []
  31. (reset! *dirties-toggle-items {}))
  32. (defn render-classic-dropdown-items
  33. [id items]
  34. (for [{:keys [hr item title options icon]} items]
  35. (let [on-click' (:on-click options)]
  36. (if hr
  37. (shui/dropdown-menu-separator)
  38. (shui/dropdown-menu-item
  39. (assoc options
  40. :on-click (fn [^js e]
  41. (when on-click'
  42. (when-not (false? (on-click' e))
  43. (shui/popup-hide! id)))))
  44. (or item
  45. [:span.flex.items-center.gap-1.w-full
  46. icon [:div title]]))))))
  47. (rum/defcs installed-themes
  48. <
  49. (rum/local [] ::themes)
  50. (rum/local 0 ::cursor)
  51. (rum/local 0 ::total)
  52. {:did-mount (fn [state]
  53. (let [*themes (::themes state)
  54. *cursor (::cursor state)
  55. *total (::total state)
  56. mode (state/sub :ui/theme)
  57. all-themes (state/sub :plugin/installed-themes)
  58. themes (->> all-themes
  59. (filter #(= (:mode %) mode))
  60. (sort-by #(:name %)))
  61. no-mode-themes (->> all-themes
  62. (filter #(= (:mode %) nil))
  63. (sort-by #(:name %))
  64. (map-indexed (fn [idx opt] (assoc opt :group-first (zero? idx) :group-desc (if (zero? idx) "light & dark themes" nil)))))
  65. selected (state/sub :plugin/selected-theme)
  66. themes (map-indexed (fn [idx opt]
  67. (let [selected? (= (:url opt) selected)]
  68. (when selected? (reset! *cursor (+ idx 1)))
  69. (assoc opt :mode mode :selected selected?))) (concat themes no-mode-themes))
  70. themes (cons {:name (string/join " " ["Default" (string/capitalize mode) "Theme"])
  71. :url nil
  72. :description (string/join " " ["Logseq default" mode "theme."])
  73. :mode mode
  74. :selected (nil? selected)
  75. :group-first true
  76. :group-desc (str mode " themes")} themes)]
  77. (reset! *themes themes)
  78. (reset! *total (count themes))
  79. state))}
  80. (mixins/event-mixin
  81. (fn [state]
  82. (let [*cursor (::cursor state)
  83. *total (::total state)
  84. ^js target (rum/dom-node state)]
  85. (.focus target)
  86. (mixins/on-key-down
  87. state {38 ;; up
  88. (fn [^js _e]
  89. (reset! *cursor
  90. (if (zero? @*cursor)
  91. (dec @*total) (dec @*cursor))))
  92. 40 ;; down
  93. (fn [^js _e]
  94. (reset! *cursor
  95. (if (= @*cursor (dec @*total))
  96. 0 (inc @*cursor))))
  97. 13 ;; enter
  98. #(when-let [^js active (.querySelector target ".is-active")]
  99. (.click active))}))))
  100. [state]
  101. (let [*cursor (::cursor state)
  102. *themes (::themes state)]
  103. [:div.cp__themes-installed
  104. {:tab-index -1}
  105. [:h1.mb-4.text-2xl.p-1 (t :themes)]
  106. (map-indexed
  107. (fn [idx opt]
  108. (let [current-selected? (:selected opt)
  109. group-first? (:group-first opt)
  110. plg (get (:plugin/installed-plugins @state/state) (keyword (:pid opt)))]
  111. [:div
  112. {:key (str idx (:name opt))}
  113. (when (and group-first? (not= idx 0)) [:hr.my-2])
  114. [:div.it.flex.px-3.py-1.5.rounded-sm.justify-between
  115. {:title (:description opt)
  116. :class (util/classnames
  117. [{:is-selected current-selected?
  118. :is-active (= idx @*cursor)}])
  119. :on-click #(do (js/LSPluginCore.selectTheme (bean/->js opt))
  120. (shui/dialog-close!))}
  121. [:div.flex.items-center.text-xs
  122. [:div.opacity-60 (str (or (:name plg) "Logseq") " •")]
  123. [:div.name.ml-1 (:name opt)]]
  124. (when (or group-first? current-selected?)
  125. [:div.flex.items-center
  126. (when group-first? [:small.opacity-60 (:group-desc opt)])
  127. (when current-selected? [:small.inline-flex.ml-1.opacity-60 (ui/icon "check")])])]]))
  128. @*themes)]))
  129. (rum/defc unpacked-plugin-loader
  130. [unpacked-pkg-path]
  131. (hooks/use-effect!
  132. (fn []
  133. (let [err-handle
  134. (fn [^js e]
  135. (case (keyword (aget e "name"))
  136. :IllegalPluginPackageError
  137. (notification/show! "Illegal Logseq plugin package." :error)
  138. :ExistedImportedPluginPackageError
  139. (notification/show! (str "Existed plugin package (" (.-message e) ").") :error)
  140. :default)
  141. (plugin-handler/reset-unpacked-state))
  142. reg-handle #(plugin-handler/reset-unpacked-state)]
  143. (when unpacked-pkg-path
  144. (doto js/LSPluginCore
  145. (.once "error" err-handle)
  146. (.once "registered" reg-handle)
  147. (.register (bean/->js {:url unpacked-pkg-path}))))
  148. #(doto js/LSPluginCore
  149. (.off "error" err-handle)
  150. (.off "registered" reg-handle))))
  151. [unpacked-pkg-path])
  152. (when unpacked-pkg-path
  153. [:strong.inline-flex.px-3 "Loading ..."]))
  154. (rum/defc category-tabs
  155. [t total-nums category on-action]
  156. [:div.secondary-tabs.categories.flex
  157. (ui/button
  158. [:span.flex.items-center
  159. (ui/icon "puzzle")
  160. (t :plugins) (when (vector? total-nums) (str " (" (first total-nums) ")"))]
  161. :intent "link"
  162. :on-click #(on-action :plugins)
  163. :class (if (= category :plugins) "active" ""))
  164. (ui/button
  165. [:span.flex.items-center
  166. (ui/icon "palette")
  167. (t :themes) (when (vector? total-nums) (str " (" (last total-nums) ")"))]
  168. :intent "link"
  169. :on-click #(on-action :themes)
  170. :class (if (= category :themes) "active" ""))])
  171. (rum/defc local-markdown-display
  172. < rum/reactive
  173. []
  174. (let [[content item] (state/sub :plugin/active-readme)]
  175. [:div.cp__plugins-details
  176. {:on-click (fn [^js/MouseEvent e]
  177. (when-let [target (.-target e)]
  178. (when (and (= (string/lower-case (.-nodeName target)) "a")
  179. (not (string/blank? (. target getAttribute "href"))))
  180. (js/apis.openExternal (. target getAttribute "href"))
  181. (.preventDefault e))))}
  182. (when-let [repo (:repository item)]
  183. (when-let [repo (if (string? repo) repo (:url repo))]
  184. [:div.p-4.rounded-md.bg-base-3
  185. [:strong [:a.flex.items-center {:target "_blank" :href repo}
  186. [:span.mr-1 (svg/github {:width 25 :height 25})] repo]]]))
  187. [:div.p-1.bg-transparent.border-none.ls-block
  188. {:style {:min-height "60vw"
  189. :max-width 900}
  190. :dangerouslySetInnerHTML {:__html content}}]]))
  191. (rum/defc remote-readme-display
  192. [{:keys [repo]} _content]
  193. (let [src (str "./marketplace.html?repo=" repo)]
  194. [:iframe.lsp-frame-readme {:src src}]))
  195. (defn security-warning
  196. []
  197. (ui/admonition
  198. :warning
  199. [:p.text-sm
  200. (t :plugin/security-warning)]))
  201. (rum/defc card-ctls-of-market < rum/static
  202. [item stat installed? installing-or-updating?]
  203. [:div.ctl
  204. [:ul.l.flex.items-center
  205. ;; stars
  206. [:li.flex.text-sm.items-center.pr-3
  207. (svg/star 16) [:span.pl-1 (:stargazers_count stat)]]
  208. ;; downloads
  209. (when-let [downloads (and stat (:total_downloads stat))]
  210. (when (and downloads (> downloads 0))
  211. [:li.flex.text-sm.items-center.pr-3
  212. (svg/cloud-down 16) [:span.pl-1 downloads]]))]
  213. [:div.r.flex.items-center
  214. [:a.btn
  215. {:class (util/classnames [{:disabled (or installed? installing-or-updating?)
  216. :installing installing-or-updating?}])
  217. :on-click #(plugin-common-handler/install-marketplace-plugin! item)}
  218. (if installed?
  219. (t :plugin/installed)
  220. (if installing-or-updating?
  221. [:span.flex.items-center [:small svg/loading]
  222. (t :plugin/installing)]
  223. (t :plugin/install)))]]])
  224. (rum/defc card-ctls-of-installed < rum/static
  225. [id name url sponsors unpacked? disabled?
  226. installing-or-updating? has-other-pending?
  227. new-version item]
  228. [:div.ctl
  229. [:div.l
  230. [:div.de
  231. [:strong (ui/icon "settings")]
  232. [:ul.menu-list
  233. [:li {:on-click #(plugin-handler/open-plugin-settings! id false)} (t :plugin/open-settings)]
  234. (when (util/electron?)
  235. [:li {:on-click #(js/apis.openPath url)} (t :plugin/open-package)])
  236. [:li {:on-click #(plugin-handler/open-report-modal! id name)} (t :plugin/report-security)]
  237. [:li {:on-click
  238. #(-> (shui/dialog-confirm!
  239. [:b (t :plugin/delete-alert name)])
  240. (p/then (fn []
  241. (plugin-common-handler/unregister-plugin id)
  242. (when (util/electron?)
  243. (plugin-config-handler/remove-plugin id)))))}
  244. (t :plugin/uninstall)]]]
  245. (when (seq sponsors)
  246. [:div.de.sponsors
  247. [:strong (ui/icon "coffee")]
  248. [:ul.menu-list
  249. (for [link sponsors]
  250. [:li {:key link}
  251. [:a {:href link :target "_blank"}
  252. [:span.flex.items-center link (ui/icon "external-link")]]])]])]
  253. [:div.r.flex.items-center
  254. (when (and unpacked? (not disabled?))
  255. [:a.btn
  256. {:on-click #(js-invoke js/LSPluginCore "reload" id)}
  257. (t :plugin/reload)])
  258. (when (not unpacked?)
  259. [:div.updates-actions
  260. [:a.btn
  261. {:class (util/classnames [{:disabled installing-or-updating?}])
  262. :on-click #(when-not has-other-pending?
  263. (plugin-handler/check-or-update-marketplace-plugin!
  264. (assoc item :only-check (not new-version))
  265. (fn [^js e] (notification/show! (.toString e) :error))))}
  266. (if installing-or-updating?
  267. (t :plugin/updating)
  268. (if new-version
  269. [:span (t :plugin/update) " 👉 " new-version]
  270. (t :plugin/check-update)))]])
  271. (ui/toggle (not disabled?)
  272. (fn []
  273. (js-invoke js/LSPluginCore (if disabled? "enable" "disable") id)
  274. (when (nil? (get @*dirties-toggle-items (keyword id)))
  275. (swap! *dirties-toggle-items assoc (keyword id) (not disabled?))))
  276. true)]])
  277. (defn get-open-plugin-readme-handler
  278. [url {:keys [webPkg] :as item} repo]
  279. #(plugin-handler/open-readme!
  280. url item (if (or repo webPkg) remote-readme-display local-markdown-display)))
  281. (rum/defc plugin-item-card < rum/static
  282. [t {:keys [id name title version url description author icon iir repo sponsors webPkg] :as item}
  283. disabled? market? *search-key has-other-pending?
  284. installing-or-updating? installed? stat coming-update]
  285. (let [name (or title name "Untitled")
  286. web? (not (nil? webPkg))
  287. unpacked? (and (not web?) (not iir))
  288. new-version (state/coming-update-new-version? coming-update)]
  289. [:div.cp__plugins-item-card
  290. {:key (str "lsp-card-" id)
  291. :class (util/classnames
  292. [{:market market?
  293. :installed installed?
  294. :updating installing-or-updating?
  295. :has-new-version new-version}])}
  296. [:div.l.link-block.cursor-pointer
  297. {:on-click (get-open-plugin-readme-handler url item repo)}
  298. (if (and icon (not (string/blank? icon)))
  299. [:img.icon {:src (if market? (plugin-handler/pkg-asset id icon) icon)}]
  300. svg/folder)
  301. (when (and (not market?) unpacked?)
  302. [:span.flex.justify-center.text-xs.text-error.pt-2 (t :plugin/unpacked)])]
  303. [:div.r
  304. [:h3.head.text-xl.font-bold.pt-1.5
  305. [:span.l.link-block.cursor-pointer
  306. {:on-click (get-open-plugin-readme-handler url item repo)}
  307. name]
  308. (when (not market?) [:sup.inline-block.px-1.text-xs.opacity-50 version])]
  309. [:div.desc.text-xs.opacity-70
  310. [:p description]
  311. ;;[:small (js/JSON.stringify (bean/->js settings))]
  312. ]
  313. ;; Author & Identity
  314. [:div.flag
  315. [:p.text-xs.pr-2.flex.justify-between
  316. [:small {:on-click #(when-let [^js el (js/document.querySelector ".cp__plugins-page .search-ctls input")]
  317. (reset! *search-key (str "@" author))
  318. (.select el))} author]
  319. [:small {:on-click #(do
  320. (notification/show! "Copied!" :success)
  321. (util/copy-to-clipboard! id))}
  322. (str "ID: " id)]]]
  323. ;; Github repo
  324. [:div.flag.is-top.opacity-50
  325. (when repo
  326. [:a.flex {:target "_blank"
  327. :href (plugin-handler/gh-repo-url repo)}
  328. (svg/github {:width 16 :height 16})])]
  329. (if market?
  330. ;; market ctls
  331. (card-ctls-of-market item stat installed? installing-or-updating?)
  332. ;; installed ctls
  333. (card-ctls-of-installed
  334. id name url sponsors unpacked? disabled?
  335. installing-or-updating? has-other-pending? new-version item))]]))
  336. (rum/defc panel-tab-search < rum/static
  337. [search-key *search-key *search-ref]
  338. [:div.search-ctls
  339. [:small.absolute.s1
  340. (ui/icon "search")]
  341. (when-not (string/blank? search-key)
  342. [:small.absolute.s2
  343. {:on-click #(when-let [^js target (rum/deref *search-ref)]
  344. (reset! *search-key nil)
  345. (.focus target))}
  346. (ui/icon "x")])
  347. (shui/input
  348. {:placeholder (t :plugin/search-plugin)
  349. :ref *search-ref
  350. :auto-focus true
  351. :on-key-down (fn [^js e]
  352. (when (= 27 (.-keyCode e))
  353. (util/stop e)
  354. (if (string/blank? search-key)
  355. (some-> (js/document.querySelector ".cp__plugins-page") (.focus))
  356. (reset! *search-key nil))))
  357. :on-change #(let [^js target (.-target %)]
  358. (reset! *search-key (some-> (.-value target) (string/triml))))
  359. :value (or search-key "")})])
  360. (rum/defc panel-tab-developer
  361. []
  362. (ui/button
  363. (t :plugin/contribute)
  364. :href "https://github.com/logseq/marketplace"
  365. :class "contribute"
  366. :intent "link"
  367. :target "_blank"))
  368. (rum/defc user-proxy-settings-container
  369. [{:keys [protocol type] :as agent-opts}]
  370. (let [type (or (not-empty type) (not-empty protocol) "system")
  371. [opts set-opts!] (rum/use-state agent-opts)
  372. [testing? set-testing?!] (rum/use-state false)
  373. *test-input (rum/create-ref)
  374. disabled? (or (= (:type opts) "system") (= (:type opts) "direct"))]
  375. [:div.cp__settings-network-proxy-cnt
  376. [:h1.mb-2.text-2xl.font-bold (t :settings-page/network-proxy)]
  377. [:div.p-2
  378. [:p [:label [:strong (t :type)]
  379. (ui/select [{:label "System" :value "system" :selected (= type "system")}
  380. {:label "Direct" :value "direct" :selected (= type "direct")}
  381. {:label "HTTP" :value "http" :selected (= type "http")}
  382. {:label "SOCKS5" :value "socks5" :selected (= type "socks5")}]
  383. (fn [_e value]
  384. (set-opts! (assoc opts :type value :protocol value))))]]
  385. [:p.flex
  386. [:label.pr-4
  387. {:class (if disabled? "opacity-50" nil)}
  388. [:strong (t :host)]
  389. [:input.form-input.is-small
  390. {:value (:host opts)
  391. :disabled disabled?
  392. :on-change #(set-opts!
  393. (assoc opts :host (util/trim-safe (util/evalue %))))}]]
  394. [:label
  395. {:class (if disabled? "opacity-50" nil)}
  396. [:strong (t :port)]
  397. [:input.form-input.is-small
  398. {:value (:port opts) :type "number" :min 1 :max 65535
  399. :disabled disabled?
  400. :on-change #(set-opts!
  401. (assoc opts :port (util/trim-safe (util/evalue %))))}]]]
  402. [:hr]
  403. [:p.flex.items-center.space-x-2
  404. [:span.w-60
  405. [:input.form-input.is-small
  406. {:ref *test-input
  407. :list "proxy-test-url-datalist"
  408. :type "url"
  409. :placeholder "https://"
  410. :on-change #(set-opts!
  411. (assoc opts :test (util/trim-safe (util/evalue %))))
  412. :value (:test opts)}]
  413. [:datalist#proxy-test-url-datalist
  414. [:option "https://api.logseq.com/logseq/version"]
  415. [:option "https://logseq-connectivity-testing-prod.s3.us-east-1.amazonaws.com/logseq-connectivity-testing"]
  416. [:option "https://www.google.com"]
  417. [:option "https://s3.amazonaws.com"]
  418. [:option "https://clients3.google.com/generate_204"]]]
  419. (ui/button (if testing? (ui/loading "Testing") "Test URL")
  420. :intent "logseq"
  421. :on-click #(let [val (util/trim-safe (.-value (rum/deref *test-input)))]
  422. (when (and (not testing?) (not (string/blank? val)))
  423. (set-testing?! true)
  424. (-> (p/let [result (ipc/ipc :testProxyUrl val opts)]
  425. (js->clj result :keywordize-keys true))
  426. (p/then (fn [{:keys [code response-ms]}]
  427. (notification/clear! :proxy-net-check)
  428. (notification/show! (str "Success! Status " code " in " response-ms "ms.") :success)))
  429. (p/catch (fn [e]
  430. (notification/show! (str e) :error false :proxy-net-check)))
  431. (p/finally (fn [] (set-testing?! false)))))))]
  432. [:p.pt-2
  433. (ui/button (t :save)
  434. :on-click (fn []
  435. (p/let [_ (ipc/ipc :setProxy opts)]
  436. (state/set-state! [:electron/user-cfgs :settings/agent] opts))))]]]))
  437. (rum/defc load-from-web-url-container
  438. []
  439. (let [[url set-url!] (rum/use-state "http://127.0.0.1:8080/")
  440. [pending? set-pending?] (rum/use-state false)
  441. handle-submit! (fn []
  442. (set-pending? true)
  443. (-> (plugin-handler/load-plugin-from-web-url! url)
  444. (p/then #(do (notification/show! "New plugin registered!" :success)
  445. (shui/dialog-close!)))
  446. (p/catch #(notification/show! (str %) :error))
  447. (p/finally
  448. #(set-pending? false))))]
  449. [:div.px-4.pt-4.pb-2.rounded-md.flex.flex-col.gap-2
  450. [:div.flex.flex-col.gap-3
  451. (shui/input {:placeholder "http://"
  452. :value url
  453. :on-change #(set-url! (-> (util/evalue %) (util/trim-safe)))
  454. :auto-focus true})
  455. [:span.text-gray-10
  456. (shui/tabler-icon "info-circle" {:size 13})
  457. [:span "URLs support both GitHub repositories and local development servers.
  458. (For examples: https://github.com/xyhp915/logseq-journals-calendar,
  459. http://localhost:8080/<plugin-dir-root>)"]]
  460. ]
  461. [:div.flex.justify-end
  462. (shui/button {:disabled (or pending? (string/blank? url))
  463. :on-click handle-submit!}
  464. (if pending? (ui/loading) "Install"))]]))
  465. (rum/defc auto-check-for-updates-control
  466. []
  467. (let [[enabled, set-enabled!] (rum/use-state (plugin-handler/get-enabled-auto-check-for-updates?))
  468. text (t :plugin/auto-check-for-updates)]
  469. [:div.flex.items-center.justify-between.px-3.py-2
  470. {:on-click (fn []
  471. (let [t (not enabled)]
  472. (set-enabled! t)
  473. (plugin-handler/set-enabled-auto-check-for-updates t)
  474. (notification/show!
  475. [:span text [:strong.pl-1 (if t "ON" "OFF")] "!"]
  476. (if t :success :info))))}
  477. [:span.pr-3.opacity-80 text]
  478. (ui/toggle enabled #() true)]))
  479. (rum/defc ^:large-vars/cleanup-todo panel-control-tabs < rum/static
  480. [search-key *search-key category *category
  481. sort-by *sort-by filter-by *filter-by total-nums
  482. selected-unpacked-pkg market? develop-mode?
  483. reload-market-fn agent-opts]
  484. (let [*search-ref (rum/create-ref)]
  485. [:div.pb-3.flex.justify-between.control-tabs.relative
  486. [:div.flex.items-center.l
  487. (category-tabs t total-nums category #(reset! *category %))
  488. (when (and develop-mode? (util/electron?) (not market?))
  489. [:div
  490. (ui/tippy {:html [:div (t :plugin/unpacked-tips)]
  491. :arrow true}
  492. (ui/button
  493. (t :plugin/load-unpacked)
  494. {:icon "upload"
  495. :intent "link"
  496. :class "load-unpacked"
  497. :on-click plugin-handler/load-unpacked-plugin}))
  498. (when (util/electron?)
  499. (unpacked-plugin-loader selected-unpacked-pkg))])]
  500. [:div.flex.items-center.r
  501. ;; extra info
  502. (when-let [proxy-val (state/http-proxy-enabled-or-val?)]
  503. (ui/button
  504. [:span.flex.items-center.text-indigo-500
  505. (ui/icon "world-download") proxy-val]
  506. :small? true
  507. :intent "link"
  508. :on-click #(state/pub-event! [:go/proxy-settings agent-opts])))
  509. ;; search
  510. (panel-tab-search search-key *search-key *search-ref)
  511. ;; sorter & filter
  512. (let [aim-icon #(if (= filter-by %) "check" "circle")
  513. items (if market?
  514. [{:title (t :plugin/all)
  515. :options {:on-click #(reset! *filter-by :default)}
  516. :icon (ui/icon (aim-icon :default))}
  517. {:title (t :plugin/installed)
  518. :options {:on-click #(reset! *filter-by :installed)}
  519. :icon (ui/icon (aim-icon :installed))}
  520. {:title (t :plugin/not-installed)
  521. :options {:on-click #(reset! *filter-by :not-installed)}
  522. :icon (ui/icon (aim-icon :not-installed))}]
  523. [{:title (t :plugin/all)
  524. :options {:on-click #(reset! *filter-by :default)}
  525. :icon (ui/icon (aim-icon :default))}
  526. {:title (t :plugin/enabled)
  527. :options {:on-click #(reset! *filter-by :enabled)}
  528. :icon (ui/icon (aim-icon :enabled))}
  529. {:title (t :plugin/disabled)
  530. :options {:on-click #(reset! *filter-by :disabled)}
  531. :icon (ui/icon (aim-icon :disabled))}
  532. {:title (t :plugin/unpacked)
  533. :options {:on-click #(reset! *filter-by :unpacked)}
  534. :icon (ui/icon (aim-icon :unpacked))}
  535. {:title (t :plugin/update-available)
  536. :options {:on-click #(reset! *filter-by :update-available)}
  537. :icon (ui/icon (aim-icon :update-available))}])]
  538. (ui/button
  539. (ui/icon "filter")
  540. :class (str (when-not (contains? #{:default} filter-by) "picked ") "sort-or-filter-by")
  541. :on-click #(shui/popup-show! (.-target %)
  542. (fn [{:keys [id]}]
  543. (render-classic-dropdown-items id items))
  544. {:as-dropdown? true})
  545. :variant :ghost))
  546. (when market?
  547. (let [aim-icon #(if (= sort-by %) "check" "circle")
  548. items [{:title (t :plugin/downloads)
  549. :options {:on-click #(reset! *sort-by :downloads)}
  550. :icon (ui/icon (aim-icon :downloads))}
  551. {:title (t :plugin/stars)
  552. :options {:on-click #(reset! *sort-by :stars)}
  553. :icon (ui/icon (aim-icon :stars))}
  554. {:title (t :plugin/title "A - Z")
  555. :options {:on-click #(reset! *sort-by :letters)}
  556. :icon (ui/icon (aim-icon :letters))}]]
  557. (ui/button
  558. (ui/icon "arrows-sort")
  559. :class (str (when-not (contains? #{:default :downloads} sort-by) "picked ") "sort-or-filter-by")
  560. :on-click #(shui/popup-show! (.-target %)
  561. (fn [{:keys [id]}]
  562. (render-classic-dropdown-items id items))
  563. {:as-dropdown? true})
  564. :variant :ghost)))
  565. ;; more - updater
  566. (let [items (concat (if market?
  567. [{:title [:span.flex.items-center.gap-1 (ui/icon "rotate-clockwise") (t :plugin/refresh-lists)]
  568. :options {:on-click #(reload-market-fn)}}]
  569. [{:title [:span.flex.items-center.gap-1 (ui/icon "rotate-clockwise") (t :plugin/check-all-updates)]
  570. :options {:on-click #(plugin-handler/user-check-enabled-for-updates! (not= :plugins category))}}])
  571. (when (util/electron?)
  572. [{:title [:span.flex.items-center.gap-1 (ui/icon "world") (t :settings-page/network-proxy)]
  573. :options {:on-click #(state/pub-event! [:go/proxy-settings agent-opts])}}
  574. {:title [:span.flex.items-center.gap-1 (ui/icon "arrow-down-circle") (t :plugin.install-from-file/menu-title)]
  575. :options {:on-click plugin-config-handler/open-replace-plugins-modal}}])
  576. [{:hr true}]
  577. (when (state/developer-mode?)
  578. (if (util/electron?)
  579. [{:title [:span.flex.items-center.gap-1 (ui/icon "file-code") (t :plugin/open-preferences)]
  580. :options {:on-click
  581. #(p/let [root (plugin-handler/get-ls-dotdir-root)]
  582. (js/apis.openPath (str root "/preferences.json")))}}
  583. {:title [:span.flex.items-center.whitespace-nowrap.gap-1
  584. (ui/icon "bug") (t :plugin/open-logseq-dir) [:code "~/.logseq"]]
  585. :options {:on-click
  586. #(p/let [root (plugin-handler/get-ls-dotdir-root)]
  587. (js/apis.openPath root))}}]
  588. [{:title [:span.flex.items-center.whitespace-nowrap.gap-1
  589. (ui/icon "plug") (t :plugin/load-from-web-url)]
  590. :options {:on-click
  591. #(shui/dialog-open! load-from-web-url-container)}}]))
  592. [{:title [:span.flex.items-center.gap-1 (ui/icon "alert-triangle") (t :plugin/report-security)]
  593. :options {:on-click #(plugin-handler/open-report-modal!)}}]
  594. [{:hr true :key "dropdown-more"}
  595. {:title (auto-check-for-updates-control)}])]
  596. (ui/button
  597. (ui/icon "dots-vertical")
  598. :class "more-do"
  599. :on-click #(shui/popup-show! (.-target %)
  600. (fn [{:keys [id]}]
  601. (render-classic-dropdown-items id items))
  602. {:as-dropdown? true
  603. :align "center"
  604. :content-props {:side-offset 10}})
  605. :variant :ghost))
  606. ;; developer
  607. (panel-tab-developer)]]))
  608. (def plugin-items-list-mixins
  609. {:did-mount
  610. (fn [s]
  611. (when-let [^js el (rum/dom-node s)]
  612. (when-let [^js el-list (.querySelector el ".cp__plugins-item-lists")]
  613. (when-let [^js cls (.-classList (.querySelector el ".control-tabs"))]
  614. (.addEventListener
  615. el-list "scroll"
  616. #(if (> (.-scrollTop el-list) 1)
  617. (.add cls "scrolled")
  618. (.remove cls "scrolled"))))))
  619. s)})
  620. (rum/defc lazy-items-loader
  621. [load-more!]
  622. (let [^js inViewState (ui/useInView #js {:threshold 0})
  623. in-view? (.-inView inViewState)]
  624. (hooks/use-effect!
  625. (fn []
  626. (load-more!))
  627. [in-view?])
  628. [:div {:ref (.-ref inViewState)}
  629. [:p.py-1.text-center.opacity-0 (when (.-inView inViewState) "·")]]))
  630. (rum/defcs ^:large-vars/data-var marketplace-plugins
  631. < rum/static rum/reactive
  632. plugin-items-list-mixins
  633. (rum/local false ::fetching)
  634. (rum/local "" ::search-key)
  635. (rum/local :plugins ::category)
  636. (rum/local :downloads ::sort-by) ;; downloads / stars / letters / updates
  637. (rum/local :default ::filter-by)
  638. (rum/local nil ::error)
  639. (rum/local nil ::cached-query-flag)
  640. (rum/local 1 ::current-page)
  641. {:did-mount
  642. (fn [s]
  643. (let [reload-fn (fn [force-refresh?]
  644. (when-not @(::fetching s)
  645. (reset! (::fetching s) true)
  646. (reset! (::error s) nil)
  647. (-> (plugin-handler/load-marketplace-plugins force-refresh?)
  648. (p/then #(plugin-handler/load-marketplace-stats false))
  649. (p/catch #(do (js/console.error %) (reset! (::error s) %)))
  650. (p/finally #(reset! (::fetching s) false)))))]
  651. (reload-fn false)
  652. (assoc s ::reload (partial reload-fn true))))}
  653. [state]
  654. (let [*list-node-ref (rum/create-ref)
  655. pkgs (state/sub :plugin/marketplace-pkgs)
  656. stats (state/sub :plugin/marketplace-stats)
  657. installed-plugins (state/sub :plugin/installed-plugins)
  658. installing (state/sub :plugin/installing)
  659. online? (state/sub :network/online?)
  660. develop-mode? (state/sub :ui/developer-mode?)
  661. agent-opts (state/sub [:electron/user-cfgs :settings/agent])
  662. *search-key (::search-key state)
  663. *category (::category state)
  664. *sort-by (::sort-by state)
  665. *filter-by (::filter-by state)
  666. *cached-query-flag (::cached-query-flag state)
  667. *current-page (::current-page state)
  668. *fetching (::fetching state)
  669. *error (::error state)
  670. theme-plugins (filter #(:theme %) pkgs)
  671. normal-plugins (filter #(not (:theme %)) pkgs)
  672. filtered-pkgs (when (seq pkgs)
  673. (if (= @*category :themes) theme-plugins normal-plugins))
  674. total-nums [(count normal-plugins) (count theme-plugins)]
  675. filtered-pkgs (if (and (seq filtered-pkgs) (not= :default @*filter-by))
  676. (filter #(apply
  677. (if (= :installed @*filter-by) identity not)
  678. [(contains? installed-plugins (keyword (:id %)))])
  679. filtered-pkgs)
  680. filtered-pkgs)
  681. filtered-pkgs (if-not (string/blank? @*search-key)
  682. (if-let [author (and (string/starts-with? @*search-key "@")
  683. (subs @*search-key 1))]
  684. (filter #(= author (:author %)) filtered-pkgs)
  685. (search/fuzzy-search
  686. filtered-pkgs @*search-key
  687. :limit 30
  688. :extract-fn :title))
  689. filtered-pkgs)
  690. filtered-pkgs (map #(if-let [stat (get stats (keyword (:id %)))]
  691. (let [downloads (:total_downloads stat)
  692. stars (:stargazers_count stat)]
  693. (assoc % :stat stat
  694. :stars stars
  695. :downloads downloads))
  696. %) filtered-pkgs)
  697. sorted-plugins (apply sort-by
  698. (conj
  699. (case @*sort-by
  700. :letters [#(util/safe-lower-case (or (:title %) (:name %)))]
  701. [@*sort-by #(compare %2 %1)])
  702. filtered-pkgs))
  703. fn-query-flag (fn [] (string/join "_" (map #(str @%) [*filter-by *sort-by *search-key *category])))
  704. str-query-flag (fn-query-flag)
  705. _ (when (not= str-query-flag @*cached-query-flag)
  706. (when-let [^js list-cnt (rum/deref *list-node-ref)]
  707. (set! (.-scrollTop list-cnt) 0))
  708. (reset! *current-page 1))
  709. _ (reset! *cached-query-flag str-query-flag)
  710. page-total-items (count sorted-plugins)
  711. sorted-plugins (if-not (> page-total-items PER-PAGE-SIZE)
  712. sorted-plugins (take (* @*current-page PER-PAGE-SIZE) sorted-plugins))
  713. load-more-pages! #(when (> page-total-items PER-PAGE-SIZE)
  714. (when (< (* PER-PAGE-SIZE @*current-page) page-total-items)
  715. (reset! *current-page (inc @*current-page))))]
  716. [:div.cp__plugins-marketplace
  717. (panel-control-tabs
  718. @*search-key *search-key
  719. @*category *category
  720. @*sort-by *sort-by @*filter-by *filter-by
  721. total-nums nil true develop-mode? (::reload state)
  722. agent-opts)
  723. (cond
  724. (not online?)
  725. [:p.flex.justify-center.pt-20.opacity-50 (svg/offline 30)]
  726. @*fetching
  727. [:p.flex.justify-center.py-20 svg/loading]
  728. @*error
  729. [:p.flex.justify-center.pt-20.opacity-50 (t :plugin/remote-error) (.-message @*error)]
  730. :else
  731. [:div.cp__plugins-marketplace-cnt
  732. {:class (util/classnames [{:has-installing (boolean installing)}])}
  733. [:div.cp__plugins-item-lists
  734. {:ref *list-node-ref}
  735. [:div.cp__plugins-item-lists-inner
  736. ;; items list
  737. (for [item sorted-plugins]
  738. (rum/with-key
  739. (let [pid (keyword (:id item))
  740. stat (:stat item)]
  741. (plugin-item-card t item
  742. (get-in item [:settings :disabled]) true *search-key installing
  743. (and installing (= (keyword (:id installing)) pid))
  744. (contains? installed-plugins pid) stat nil))
  745. (:id item)))]
  746. ;; items loader
  747. (when (seq sorted-plugins)
  748. (lazy-items-loader load-more-pages!))]])]))
  749. (rum/defcs ^:large-vars/data-var installed-plugins
  750. < rum/static rum/reactive
  751. plugin-items-list-mixins
  752. (rum/local "" ::search-key)
  753. (rum/local :default ::filter-by) ;; default / enabled / disabled / unpacked / update-available
  754. (rum/local :default ::sort-by)
  755. (rum/local :plugins ::category)
  756. (rum/local nil ::cached-query-flag)
  757. (rum/local 1 ::current-page)
  758. [state]
  759. (let [*list-node-ref (rum/create-ref)
  760. installed-plugins' (vals (state/sub [:plugin/installed-plugins]))
  761. updating (state/sub :plugin/installing)
  762. develop-mode? (state/sub :ui/developer-mode?)
  763. selected-unpacked-pkg (state/sub :plugin/selected-unpacked-pkg)
  764. coming-updates (state/sub :plugin/updates-coming)
  765. agent-opts (state/sub [:electron/user-cfgs :settings/agent])
  766. *filter-by (::filter-by state)
  767. *sort-by (::sort-by state)
  768. *search-key (::search-key state)
  769. *category (::category state)
  770. *cached-query-flag (::cached-query-flag state)
  771. *current-page (::current-page state)
  772. default-filter-by? (= :default @*filter-by)
  773. theme-plugins (filter #(:theme %) installed-plugins')
  774. normal-plugins (filter #(not (:theme %)) installed-plugins')
  775. filtered-plugins (when (seq installed-plugins')
  776. (if (= @*category :themes) theme-plugins normal-plugins))
  777. total-nums [(count normal-plugins) (count theme-plugins)]
  778. filtered-plugins (if-not default-filter-by?
  779. (filter (fn [it]
  780. (let [disabled (get-in it [:settings :disabled])]
  781. (case @*filter-by
  782. :enabled (not disabled)
  783. :disabled disabled
  784. :unpacked (not (:iir it))
  785. :update-available (state/plugin-update-available? (:id it))
  786. true))) filtered-plugins)
  787. filtered-plugins)
  788. filtered-plugins (if-not (string/blank? @*search-key)
  789. (if-let [author (and (string/starts-with? @*search-key "@")
  790. (subs @*search-key 1))]
  791. (filter #(= author (:author %)) filtered-plugins)
  792. (search/fuzzy-search
  793. filtered-plugins @*search-key
  794. :limit 30
  795. :extract-fn :name))
  796. filtered-plugins)
  797. sorted-plugins (if default-filter-by?
  798. (->> filtered-plugins
  799. (reduce #(let [disabled? (get-in %2 [:settings :disabled])
  800. old-dirty (get @*dirties-toggle-items (keyword (:id %2)))
  801. k (if (if (boolean? old-dirty) (not old-dirty) disabled?) 1 0)]
  802. (update %1 k conj %2)) [[] []])
  803. (#(update % 0 (fn [coll] (sort-by :iir coll))))
  804. (flatten))
  805. (do
  806. (clear-dirties-states!)
  807. filtered-plugins))
  808. fn-query-flag (fn [] (string/join "_" (map #(str @%) [*filter-by *sort-by *search-key *category])))
  809. str-query-flag (fn-query-flag)
  810. _ (when (not= str-query-flag @*cached-query-flag)
  811. (when-let [^js list-cnt (rum/deref *list-node-ref)]
  812. (set! (.-scrollTop list-cnt) 0))
  813. (reset! *current-page 1))
  814. _ (reset! *cached-query-flag str-query-flag)
  815. page-total-items (count sorted-plugins)
  816. sorted-plugins (if-not (> page-total-items PER-PAGE-SIZE)
  817. sorted-plugins (take (* @*current-page PER-PAGE-SIZE) sorted-plugins))
  818. load-more-pages! #(when (> page-total-items PER-PAGE-SIZE)
  819. (when (< (* PER-PAGE-SIZE @*current-page) page-total-items)
  820. (reset! *current-page (inc @*current-page))))]
  821. [:div.cp__plugins-installed
  822. (panel-control-tabs
  823. @*search-key *search-key
  824. @*category *category
  825. @*sort-by *sort-by
  826. @*filter-by *filter-by
  827. total-nums selected-unpacked-pkg
  828. false develop-mode? nil
  829. agent-opts)
  830. [:div.cp__plugins-item-lists.pb-6
  831. {:ref *list-node-ref}
  832. [:div.cp__plugins-item-lists-inner
  833. (for [item sorted-plugins]
  834. (rum/with-key
  835. (let [pid (keyword (:id item))]
  836. (plugin-item-card t item
  837. (get-in item [:settings :disabled]) false *search-key updating
  838. (and updating (= (keyword (:id updating)) pid))
  839. true nil (get coming-updates pid)))
  840. (:id item)))]
  841. (if (seq sorted-plugins)
  842. (lazy-items-loader load-more-pages!)
  843. [:div.flex.items-center.justify-center.py-28.flex-col.gap-2.opacity-30
  844. (shui/tabler-icon "list-search" {:size 40})
  845. [:span.text-sm "Nothing Founded."]])]]))
  846. (rum/defcs waiting-coming-updates
  847. < rum/reactive
  848. {:will-mount (fn [s] (state/reset-unchecked-update) s)}
  849. [_s]
  850. (let [_ (state/sub :plugin/updates-coming)
  851. downloading? (state/sub :plugin/updates-downloading?)
  852. unchecked (state/sub :plugin/updates-unchecked)
  853. updates (state/all-available-coming-updates)]
  854. [:div.cp__plugins-waiting-updates
  855. [:h1.mb-4.text-2xl.p-1 (t :plugin/found-n-updates (count updates))]
  856. (if (seq updates)
  857. ;; lists
  858. [:ul
  859. {:class (when downloading? "downloading")}
  860. (for [it updates
  861. :let [k (str "lsp-it-" (:id it))
  862. c? (not (contains? unchecked (:id it)))
  863. notes (util/trim-safe (:latest-notes it))]]
  864. [:li.flex.items-center
  865. {:key k
  866. :class (when c? "checked")}
  867. [:label.flex-1
  868. {:for k}
  869. (shui/checkbox
  870. {:id k
  871. :default-checked c?
  872. :on-checked-change (fn [checked?]
  873. (when-not downloading?
  874. (state/set-unchecked-update (:id it) (not checked?))))})
  875. [:strong.px-3 (:title it)
  876. [:sup (str (:version it) " 👉 " (:latest-version it))]]]
  877. [:div.px-4
  878. (when-not (string/blank? notes)
  879. (ui/tippy
  880. {:html [:p notes]}
  881. [:span.opacity-30.hover:opacity-80 (ui/icon "info-circle")]))]])]
  882. ;; all done
  883. [:div.py-4 [:strong.text-4xl (str "\uD83C\uDF89 " (t :plugin/all-updated))]])
  884. ;; actions
  885. (when (seq updates)
  886. [:div.pt-5.flex.justify-end
  887. (ui/button
  888. (if downloading?
  889. [:span (ui/loading (t :plugin/updates-downloading))]
  890. [:span.flex.items-center (ui/icon "download") (t :plugin/update-all-selected)])
  891. :on-click
  892. #(when-not downloading?
  893. (plugin-handler/open-updates-downloading)
  894. (if-let [n (state/get-next-selected-coming-update)]
  895. (plugin-handler/check-or-update-marketplace-plugin!
  896. (assoc n :only-check false)
  897. (fn [^js e] (notification/show! (.toString e) :error)))
  898. (plugin-handler/close-updates-downloading)))
  899. :disabled
  900. (or downloading?
  901. (and (seq unchecked)
  902. (= (count unchecked) (count updates)))))])]))
  903. (rum/defc plugins-from-file
  904. < rum/reactive
  905. [plugins]
  906. [:div.cp__plugins-fom-file
  907. [:h1.mb-4.text-2xl.p-1 (t :plugin.install-from-file/title)]
  908. (if (seq plugins)
  909. [:div
  910. [:div.mb-2.text-xl (t :plugin.install-from-file/notice)]
  911. ;; lists
  912. [:ul
  913. (for [it (:install plugins)
  914. :let [k (str "lsp-it-" (name (:id it)))]]
  915. [:li.flex.items-center
  916. {:key k}
  917. [:label.flex-1
  918. {:for k}
  919. [:strong.px-3 (str (name (:id it)) " " (:version it))]]])]
  920. ;; actions
  921. [:div.pt-5
  922. (ui/button [:span (t :plugin/install)]
  923. :on-click #(do
  924. (plugin-config-handler/replace-plugins plugins)
  925. (shui/dialog-close! "ls-plugins-from-file-modal")))]]
  926. ;; all done
  927. [:div.py-4 [:strong.text-xl (str "\uD83C\uDF89 " (t :plugin.install-from-file/success))]])])
  928. (defn open-select-theme!
  929. []
  930. (shui/dialog-open! installed-themes
  931. {:align :top}))
  932. (rum/defc hook-ui-slot
  933. ([type payload] (hook-ui-slot type payload nil #(plugin-handler/hook-plugin-app type % nil)))
  934. ([type payload opts callback]
  935. (let [rs (util/rand-str 8)
  936. id (str "slot__" rs)
  937. *el-ref (rum/use-ref nil)]
  938. (hooks/use-effect!
  939. (fn []
  940. (let [timer (js/setTimeout #(callback {:type type :slot id :payload payload}) 50)]
  941. #(js/clearTimeout timer)))
  942. [id])
  943. (hooks/use-effect!
  944. (fn []
  945. (let [el (rum/deref *el-ref)]
  946. #(when-let [uis (seq (.querySelectorAll el "[data-injected-ui]"))]
  947. (doseq [^js el uis]
  948. (when-let [id (.-injectedUi (.-dataset el))]
  949. (js/LSPluginCore._forceCleanInjectedUI id))))))
  950. [])
  951. [:div.lsp-hook-ui-slot
  952. (merge opts {:id id
  953. :ref *el-ref
  954. :on-pointer-down (fn [e] (util/stop-propagation e))})])))
  955. (rum/defc hook-block-slot < rum/static
  956. [type block]
  957. (hook-ui-slot type {} nil #(plugin-handler/hook-plugin-block-slot block %)))
  958. (rum/defc ui-item-renderer
  959. [pid type {:keys [key template prefix]}]
  960. (let [*el (rum/use-ref nil)
  961. uni #(str prefix "injected-ui-item-" %)
  962. ^js pl (js/LSPluginCore.registeredPlugins.get (name pid))]
  963. (hooks/use-effect!
  964. (fn []
  965. (when-let [^js el (rum/deref *el)]
  966. (js/LSPlugin.pluginHelpers.setupInjectedUI.call
  967. pl #js {:slot (.-id el) :key key :template template} #js {})))
  968. [template])
  969. (if-not (nil? pl)
  970. [:div
  971. {:id (uni (str (name key) "-" (name pid)))
  972. :title key
  973. :class (uni (name type))
  974. :ref *el}]
  975. [:<>])))
  976. (rum/defc toolbar-plugins-manager-list
  977. [updates-coming items]
  978. (let [badge-updates? (and (not (plugin-handler/get-auto-checking?))
  979. (seq (state/all-available-coming-updates updates-coming)))
  980. items (fn []
  981. (->> (concat
  982. (for [[_ {:keys [key pinned?] :as opts} pid] items
  983. :let [pkey (str (name pid) ":" key)]]
  984. {:title key
  985. :item [:div.flex.items-center.item-wrap
  986. (ui-item-renderer pid :toolbar (assoc opts :prefix "pl-" :key (str "pl-" key)))
  987. [:span {:style {:padding-left "2px"}} key]
  988. [:span.pin.flex.items-center.opacity-60
  989. {:class (util/classnames [{:pinned pinned?}])}
  990. (ui/icon (if pinned? "pinned" "pin"))]]
  991. :options {:on-click (fn [^js e]
  992. (let [^js target (.-target e)
  993. user-btn? (boolean (.closest target "div[data-injected-ui]"))]
  994. (when-not user-btn?
  995. (plugin-handler/op-pinned-toolbar-item! pkey (if pinned? :remove :add)))
  996. true))}})
  997. [{:hr true}
  998. {:title (t :plugins)
  999. :options {:on-click #(plugin-handler/goto-plugins-dashboard!)
  1000. :class "extra-item mt-2"}
  1001. :icon (ui/icon "apps")}
  1002. {:title (t :themes)
  1003. :options {:on-click #(plugin-handler/show-themes-modal!)
  1004. :class "extra-item"}
  1005. :icon (ui/icon "palette")}
  1006. {:title (t :settings)
  1007. :options {:on-click #(plugin-handler/goto-plugins-settings!)
  1008. :class "extra-item"}
  1009. :icon (ui/icon "adjustments")}
  1010. (when badge-updates?
  1011. {:title [:div.flex.items-center.space-x-5.leading-none
  1012. [:span (t :plugin/found-updates)] (ui/point "bg-red-700" 5 {:style {:margin-top 2}})]
  1013. :options {:on-click #(open-waiting-updates-modal!)
  1014. :class "extra-item"}
  1015. :icon (ui/icon "download")})]
  1016. [{:hr true :key "dropdown-more"}
  1017. {:title (auto-check-for-updates-control)}])
  1018. (remove nil?)))]
  1019. [:div.toolbar-plugins-manager
  1020. {:on-pointer-down
  1021. (fn [^js e]
  1022. (shui/popup-show! (.-target e)
  1023. (fn [{:keys [id]}]
  1024. (render-classic-dropdown-items id (items)))
  1025. {:as-dropdown? true
  1026. :content-props {:class "toolbar-plugins-manager-content"}}))}
  1027. (shui/button-ghost-icon :puzzle
  1028. {:class "flex relative toolbar-plugins-manager-trigger"}
  1029. (when badge-updates?
  1030. (ui/point "bg-red-600.top-1.right-1.absolute" 4 {:style {:margin-right 2 :margin-top 2}})))]))
  1031. (rum/defc header-ui-items-list-wrap
  1032. [children]
  1033. (let [*wrap-el (rum/use-ref nil)
  1034. [right-sidebar-resized] (rum-utils/use-atom ui-handler/*right-sidebar-resized-at)]
  1035. (hooks/use-effect!
  1036. (fn []
  1037. (when-let [^js wrap-el (rum/deref *wrap-el)]
  1038. (when-let [^js header-el (.closest wrap-el ".cp__header")]
  1039. (let [^js header-l (.querySelector header-el "* > .l")
  1040. ^js header-r (.querySelector header-el "* > .r")
  1041. set-max-width! #(when (number? %) (set! (.-maxWidth (.-style wrap-el)) (str % "px")))
  1042. calc-wrap-max-width #(let [width-l (.-offsetWidth header-l)
  1043. width-t (-> (js/document.querySelector "#main-content-container") (.-offsetWidth))
  1044. children (to-array (.-children header-r))
  1045. width-c' (reduce (fn [acc ^js e]
  1046. (when (some-> e (.-classList) (.contains "ui-items-container") (not))
  1047. (+ acc (or (.-offsetWidth e) 0)))) 0 children)]
  1048. (when-let [width-t (and (number? width-t)
  1049. (if-not (state/get-left-sidebar-open?)
  1050. (- width-t width-l) width-t))]
  1051. (set-max-width! (max (- width-t width-c' 100) 76))))]
  1052. (.addEventListener js/window "resize" calc-wrap-max-width)
  1053. (js/setTimeout calc-wrap-max-width 16)
  1054. #(.removeEventListener js/window "resize" calc-wrap-max-width)))))
  1055. [right-sidebar-resized])
  1056. [:div.list-wrap
  1057. {:ref *wrap-el}
  1058. children]))
  1059. (rum/defcs hook-ui-items < rum/reactive
  1060. < {:key-fn #(identity "plugin-hook-items")}
  1061. "type of :toolbar, :pagebar"
  1062. [_state type]
  1063. (when (state/sub [:plugin/installed-ui-items])
  1064. (let [toolbar? (= :toolbar type)
  1065. pinned-items (state/sub [:plugin/preferences :pinnedToolbarItems])
  1066. pinned-items (and (sequential? pinned-items) (into #{} pinned-items))
  1067. items (state/get-plugins-ui-items-with-type type)
  1068. items (sort-by #(:key (second %)) items)]
  1069. (when-let [items (and (seq items)
  1070. (if toolbar?
  1071. (map #(assoc-in % [1 :pinned?]
  1072. (let [[_ {:keys [key]} pid] %
  1073. pkey (str (name pid) ":" key)]
  1074. (contains? pinned-items pkey)))
  1075. items)
  1076. items))]
  1077. [:div.ui-items-container
  1078. {:data-type (name type)}
  1079. [:<>
  1080. (header-ui-items-list-wrap
  1081. (for [[_ {:keys [key pinned?] :as opts} pid] items]
  1082. (when (or (not toolbar?)
  1083. (not (set? pinned-items)) pinned?)
  1084. (rum/with-key (ui-item-renderer pid type opts) key))))
  1085. ;; manage plugin buttons
  1086. (when toolbar?
  1087. (let [updates-coming (state/sub :plugin/updates-coming)]
  1088. (toolbar-plugins-manager-list updates-coming items)))]]))))
  1089. (rum/defc hook-ui-fenced-code
  1090. [block content {:keys [render edit] :as _opts}]
  1091. (let [[content1 set-content1!] (rum/use-state content)
  1092. [editor-active? set-editor-active!] (rum/use-state (string/blank? content))
  1093. *cm (rum/use-ref nil)
  1094. *el (rum/use-ref nil)]
  1095. (hooks/use-effect!
  1096. #(set-content1! content)
  1097. [content])
  1098. (hooks/use-effect!
  1099. (fn []
  1100. (some-> (rum/deref *el)
  1101. (.closest ".ui-fenced-code-wrap")
  1102. (.-classList)
  1103. (#(if editor-active?
  1104. (.add % "is-active")
  1105. (.remove % "is-active"))))
  1106. (when-let [cm (rum/deref *cm)]
  1107. (.refresh cm)
  1108. (.focus cm)
  1109. (.setCursor cm (.lineCount cm) (count (.getLine cm (.lastLine cm))))))
  1110. [editor-active?])
  1111. (hooks/use-effect!
  1112. (fn []
  1113. (let [t (js/setTimeout
  1114. #(when-let [^js cm (some-> (rum/deref *el)
  1115. (.closest ".ui-fenced-code-wrap")
  1116. (.querySelector ".CodeMirror")
  1117. (.-CodeMirror))]
  1118. (rum/set-ref! *cm cm)
  1119. (doto cm
  1120. (.on "change" (fn []
  1121. (some-> cm (.getDoc) (.getValue) (set-content1!))))))
  1122. ;; wait for the cm loaded
  1123. 1000)]
  1124. #(js/clearTimeout t)))
  1125. [])
  1126. [:div.ui-fenced-code-result
  1127. {:on-pointer-down (fn [e] (when (false? edit) (util/stop e)))
  1128. :class (util/classnames [{:not-edit (false? edit)}])
  1129. :ref *el}
  1130. [:<>
  1131. [:span.actions
  1132. {:on-pointer-down #(util/stop %)}
  1133. (ui/button (ui/icon "square-toggle-horizontal" {:size 14})
  1134. :on-click #(set-editor-active! (not editor-active?)))
  1135. (ui/button (ui/icon "source-code" {:size 14})
  1136. :on-click #(editor-handler/edit-block! block (count content1)))]
  1137. (when (fn? render)
  1138. (js/React.createElement render #js {:content content1}))]]))
  1139. (rum/defc plugins-page
  1140. []
  1141. (let [[active set-active!] (rum/use-state :installed)
  1142. market? (= active :marketplace)
  1143. *el-ref (rum/create-ref)]
  1144. (hooks/use-effect!
  1145. (fn []
  1146. (state/load-app-user-cfgs)
  1147. #(clear-dirties-states!))
  1148. [])
  1149. (hooks/use-effect!
  1150. #(clear-dirties-states!)
  1151. [market?])
  1152. [:div.cp__plugins-page
  1153. {:ref *el-ref
  1154. :class (when-not (util/electron?) "web-platform")
  1155. :tab-index "-1"}
  1156. [:h1 (t :plugins)]
  1157. (when (util/electron?)
  1158. [:<>
  1159. (security-warning)
  1160. [:hr.my-4]])
  1161. [:div.tabs.flex.items-center.justify-center
  1162. [:div.tabs-inner.flex.items-center
  1163. (shui/button {:on-click #(set-active! :installed)
  1164. :class (when (not market?) "active")
  1165. :size :sm
  1166. :variant :text}
  1167. (t :plugin/installed))
  1168. (shui/button {:on-click #(set-active! :marketplace)
  1169. :class (when market? "active")
  1170. :size :sm
  1171. :variant :text}
  1172. (shui/tabler-icon "apps")
  1173. (t :plugin/marketplace))]]
  1174. [:div.panels
  1175. (if market?
  1176. (marketplace-plugins)
  1177. (installed-plugins))]]))
  1178. (def *updates-sub-content-timer (atom nil))
  1179. (def *updates-sub-content (atom nil))
  1180. (defn set-updates-sub-content!
  1181. [content duration]
  1182. (reset! *updates-sub-content content)
  1183. (when (> duration 0)
  1184. (some-> @*updates-sub-content-timer (js/clearTimeout))
  1185. (->> (js/setTimeout #(reset! *updates-sub-content nil) duration)
  1186. (reset! *updates-sub-content-timer))))
  1187. (rum/defc updates-notifications-impl
  1188. [check-pending? auto-checking? online?]
  1189. (let [[uid, set-uid] (rum/use-state nil)
  1190. [sub-content, _set-sub-content!] (rum-utils/use-atom *updates-sub-content)
  1191. notify! (fn [content status]
  1192. (if auto-checking?
  1193. (println (t :plugin/list-of-updates) content)
  1194. (let [cb #(plugin-handler/cancel-user-checking!)]
  1195. (try
  1196. (set-uid (notification/show! content status false uid nil cb))
  1197. (catch js/Error _
  1198. (set-uid (notification/show! content status false nil nil cb)))))))]
  1199. (hooks/use-effect!
  1200. (fn []
  1201. (if check-pending?
  1202. (notify!
  1203. [:div
  1204. [:div (t :plugin/checking-for-updates)]
  1205. (when sub-content [:p.opacity-60 sub-content])]
  1206. (ui/loading ""))
  1207. (when uid (notification/clear! uid))))
  1208. [check-pending? sub-content])
  1209. (hooks/use-effect!
  1210. ;; scheduler for auto updates
  1211. (fn []
  1212. (when online?
  1213. (let [last-updates (storage/get :lsp-last-auto-updates)]
  1214. (when (and (not (false? last-updates))
  1215. (or (true? last-updates)
  1216. (not (number? last-updates))
  1217. ;; interval 12 hours
  1218. (> (- (js/Date.now) last-updates) (* 60 60 12 1000))))
  1219. (let [update-timer (js/setTimeout
  1220. (fn []
  1221. (plugin-handler/auto-check-enabled-for-updates!)
  1222. (storage/set :lsp-last-auto-updates (js/Date.now)))
  1223. (if (util/electron?) 3000 (* 60 1000) ))]
  1224. #(js/clearTimeout update-timer))))))
  1225. [online?])
  1226. [:<>]))
  1227. (rum/defcs updates-notifications < rum/reactive
  1228. [_]
  1229. (let [updates-pending (state/sub :plugin/updates-pending)
  1230. online? (state/sub :network/online?)
  1231. auto-checking? (state/sub :plugin/updates-auto-checking?)
  1232. check-pending? (boolean (seq updates-pending))]
  1233. (updates-notifications-impl check-pending? auto-checking? online?)))
  1234. (rum/defcs focused-settings-content
  1235. < rum/reactive
  1236. (rum/local (state/sub :plugin/focused-settings) ::cache)
  1237. [_state title]
  1238. (let [*cache (::cache _state)
  1239. focused (state/sub :plugin/focused-settings)
  1240. nav? (state/sub :plugin/navs-settings?)
  1241. _ (state/sub :plugin/installed-plugins)
  1242. _ (js/setTimeout #(reset! *cache focused) 100)]
  1243. [:div.cp__plugins-settings.cp__settings-main
  1244. [:div.cp__settings-inner.md:flex
  1245. {:class (util/classnames [{:no-aside (not nav?)}])}
  1246. (when nav?
  1247. [:aside.md:w-64 {:style {:min-width "10rem"}}
  1248. [:header.cp__settings-header
  1249. [:h1.cp__settings-modal-title (or title (t :settings-of-plugins))]]
  1250. (let [plugins (plugin-handler/get-enabled-plugins-if-setting-schema)]
  1251. [:ul.settings-plugin-list
  1252. (for [{:keys [id name title icon]} plugins]
  1253. [:li
  1254. {:key id :class (util/classnames [{:active (= id focused)}])}
  1255. [:a.flex.items-center.settings-plugin-item
  1256. {:data-id id
  1257. :on-click #(do (state/set-state! :plugin/focused-settings id))}
  1258. (if (and icon (not (string/blank? icon)))
  1259. [:img.icon {:src icon}]
  1260. svg/folder)
  1261. [:strong.flex-1 (or title name)]]])])])
  1262. [:article
  1263. [:div.panel-wrap
  1264. {:data-id focused}
  1265. (when-let [^js pl (and focused (= @*cache focused)
  1266. (plugin-handler/get-plugin-inst focused))]
  1267. (ui/catch-error
  1268. [:p.warning.text-lg.mt-5 "Settings schema Error!"]
  1269. (plugins-settings/settings-container
  1270. (bean/->clj (.-settingsSchema pl)) pl)))]]]]))
  1271. (rum/defc custom-js-installer
  1272. [{:keys [t current-repo db-restoring? nfs-granted?]}]
  1273. (hooks/use-effect!
  1274. (fn []
  1275. (when (and (not db-restoring?)
  1276. (or (not util/nfs?) nfs-granted?))
  1277. (ui-handler/exec-js-if-exists-&-allowed! t)))
  1278. [current-repo db-restoring? nfs-granted?])
  1279. nil)
  1280. (rum/defc perf-tip-content
  1281. [pid name url]
  1282. [:div
  1283. [:span.block.whitespace-normal
  1284. "This plugin "
  1285. [:strong.text-error "#" name]
  1286. " takes too long to load, affecting the application startup time and
  1287. potentially causing other plugins to fail to load."]
  1288. [:path.opacity-50
  1289. [:small [:span.pr-1 (ui/icon "folder")] url]]
  1290. [:p
  1291. (ui/button "Disable now"
  1292. :small? true
  1293. :on-click
  1294. (fn []
  1295. (-> (js/LSPluginCore.disable pid)
  1296. (p/then #(do
  1297. (notification/clear! pid)
  1298. (notification/show!
  1299. [:span "The plugin "
  1300. [:strong.text-error "#" name]
  1301. " is disabled."] :success
  1302. true nil 3000 nil)))
  1303. (p/catch #(js/console.error %)))))]])
  1304. (defn open-plugins-modal!
  1305. []
  1306. (shui/dialog-open!
  1307. (plugins-page)
  1308. {:label :plugins-dashboard
  1309. :align :start}))
  1310. (defn open-waiting-updates-modal!
  1311. []
  1312. (shui/dialog-open!
  1313. (fn []
  1314. (waiting-coming-updates))
  1315. {:center? true}))
  1316. (defn open-plugins-from-file-modal!
  1317. [plugins]
  1318. (shui/dialog-open!
  1319. (fn []
  1320. (plugins-from-file plugins))
  1321. {:id "ls-plugins-from-file-modal"}))
  1322. (defn open-focused-settings-modal!
  1323. [title]
  1324. (shui/dialog-open!
  1325. (fn []
  1326. [:div.settings-modal.of-plugins
  1327. (focused-settings-content title)])
  1328. {:label "plugin-settings-modal"
  1329. :align :start
  1330. :id "ls-focused-settings-modal"}))
  1331. (defn hook-custom-routes
  1332. [routes]
  1333. (cond-> routes
  1334. config/lsp-enabled?
  1335. (concat (some->> (plugin-handler/get-route-renderers)
  1336. (mapv #(when-let [{:keys [name path render]} %]
  1337. (when (not (string/blank? path))
  1338. [path {:name name :view (fn [r] (render r %))}])))
  1339. (remove nil?)))))
  1340. (defn hook-daemon-renderers
  1341. []
  1342. (when-let [rs (seq (plugin-handler/get-daemon-renderers))]
  1343. [:div.lsp-daemon-container.fixed.z-10
  1344. (for [{:keys [key _pid render]} rs]
  1345. (when (fn? render)
  1346. [:div.lsp-daemon-container-card {:data-key key} (render)]))]))