plugins.cljs 58 KB

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