plugins.cljs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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.space-x-1 (ui/icon "bug") (t :plugin/open-logseq-dir) [:code "~/.logseq"]]
  543. :options {:on-click
  544. #(p/let [root (plugin-handler/get-ls-dotdir-root)]
  545. (js/apis.openPath root))}}])
  546. [{:hr true :key "dropdown-more"}
  547. {:title (auto-check-for-updates-control)
  548. :options {:no-padding? true}}])
  549. {})
  550. ;; developer
  551. (panel-tab-developer)]]))
  552. (def plugin-items-list-mixins
  553. {:did-mount
  554. (fn [s]
  555. (when-let [^js el (rum/dom-node s)]
  556. (when-let [^js el-list (.querySelector el ".cp__plugins-item-lists")]
  557. (when-let [^js cls (.-classList (.querySelector el ".control-tabs"))]
  558. (.addEventListener
  559. el-list "scroll"
  560. #(if (> (.-scrollTop el-list) 1)
  561. (.add cls "scrolled")
  562. (.remove cls "scrolled"))))))
  563. s)})
  564. (rum/defc lazy-items-loader
  565. [load-more!]
  566. (let [^js inViewState (ui/useInView #js {:threshold 0})
  567. in-view? (.-inView inViewState)]
  568. (rum/use-effect!
  569. (fn []
  570. (load-more!))
  571. [in-view?])
  572. [:div {:ref (.-ref inViewState)}
  573. [:p.py-1.text-center.opacity-0 (when (.-inView inViewState) "·")]]))
  574. (rum/defcs ^:large-vars/data-var marketplace-plugins
  575. < rum/static rum/reactive
  576. plugin-items-list-mixins
  577. (rum/local false ::fetching)
  578. (rum/local "" ::search-key)
  579. (rum/local :plugins ::category)
  580. (rum/local :downloads ::sort-by) ;; downloads / stars / letters / updates
  581. (rum/local :default ::filter-by)
  582. (rum/local nil ::error)
  583. (rum/local nil ::cached-query-flag)
  584. (rum/local 1 ::current-page)
  585. {:did-mount
  586. (fn [s]
  587. (let [reload-fn (fn [force-refresh?]
  588. (when-not @(::fetching s)
  589. (reset! (::fetching s) true)
  590. (reset! (::error s) nil)
  591. (-> (plugin-handler/load-marketplace-plugins force-refresh?)
  592. (p/then #(plugin-handler/load-marketplace-stats false))
  593. (p/catch #(do (js/console.error %) (reset! (::error s) %)))
  594. (p/finally #(reset! (::fetching s) false)))))]
  595. (reload-fn false)
  596. (assoc s ::reload (partial reload-fn true))))}
  597. [state]
  598. (let [*list-node-ref (rum/create-ref)
  599. pkgs (state/sub :plugin/marketplace-pkgs)
  600. stats (state/sub :plugin/marketplace-stats)
  601. installed-plugins (state/sub :plugin/installed-plugins)
  602. installing (state/sub :plugin/installing)
  603. online? (state/sub :network/online?)
  604. develop-mode? (state/sub :ui/developer-mode?)
  605. agent-opts (state/sub [:electron/user-cfgs :settings/agent])
  606. *search-key (::search-key state)
  607. *category (::category state)
  608. *sort-by (::sort-by state)
  609. *filter-by (::filter-by state)
  610. *cached-query-flag (::cached-query-flag state)
  611. *current-page (::current-page state)
  612. *fetching (::fetching state)
  613. *error (::error state)
  614. theme-plugins (filter #(:theme %) pkgs)
  615. normal-plugins (filter #(not (:theme %)) pkgs)
  616. filtered-pkgs (when (seq pkgs)
  617. (if (= @*category :themes) theme-plugins normal-plugins))
  618. total-nums [(count normal-plugins) (count theme-plugins)]
  619. filtered-pkgs (if (and (seq filtered-pkgs) (not= :default @*filter-by))
  620. (filter #(apply
  621. (if (= :installed @*filter-by) identity not)
  622. [(contains? installed-plugins (keyword (:id %)))])
  623. filtered-pkgs)
  624. filtered-pkgs)
  625. filtered-pkgs (if-not (string/blank? @*search-key)
  626. (if-let [author (and (string/starts-with? @*search-key "@")
  627. (subs @*search-key 1))]
  628. (filter #(= author (:author %)) filtered-pkgs)
  629. (search/fuzzy-search
  630. filtered-pkgs @*search-key
  631. :limit 30
  632. :extract-fn :title))
  633. filtered-pkgs)
  634. filtered-pkgs (map #(if-let [stat (get stats (keyword (:id %)))]
  635. (let [downloads (:total_downloads stat)
  636. stars (:stargazers_count stat)]
  637. (assoc % :stat stat
  638. :stars stars
  639. :downloads downloads))
  640. %) filtered-pkgs)
  641. sorted-plugins (apply sort-by
  642. (conj
  643. (case @*sort-by
  644. :letters [#(util/safe-lower-case (or (:title %) (:name %)))]
  645. [@*sort-by #(compare %2 %1)])
  646. filtered-pkgs))
  647. fn-query-flag (fn [] (string/join "_" (map #(str @%) [*filter-by *sort-by *search-key *category])))
  648. str-query-flag (fn-query-flag)
  649. _ (when (not= str-query-flag @*cached-query-flag)
  650. (when-let [^js list-cnt (rum/deref *list-node-ref)]
  651. (set! (.-scrollTop list-cnt) 0))
  652. (reset! *current-page 1))
  653. _ (reset! *cached-query-flag str-query-flag)
  654. page-total-items (count sorted-plugins)
  655. sorted-plugins (if-not (> page-total-items PER-PAGE-SIZE)
  656. sorted-plugins (take (* @*current-page PER-PAGE-SIZE) sorted-plugins))
  657. load-more-pages! #(when (> page-total-items PER-PAGE-SIZE)
  658. (when (< (* PER-PAGE-SIZE @*current-page) page-total-items)
  659. (reset! *current-page (inc @*current-page))))]
  660. [:div.cp__plugins-marketplace
  661. (panel-control-tabs
  662. @*search-key *search-key
  663. @*category *category
  664. @*sort-by *sort-by @*filter-by *filter-by
  665. total-nums nil true develop-mode? (::reload state)
  666. agent-opts)
  667. (cond
  668. (not online?)
  669. [:p.flex.justify-center.pt-20.opacity-50 (svg/offline 30)]
  670. @*fetching
  671. [:p.flex.justify-center.py-20 svg/loading]
  672. @*error
  673. [:p.flex.justify-center.pt-20.opacity-50 (t :plugin/remote-error) (.-message @*error)]
  674. :else
  675. [:div.cp__plugins-marketplace-cnt
  676. {:class (util/classnames [{:has-installing (boolean installing)}])}
  677. [:div.cp__plugins-item-lists
  678. {:ref *list-node-ref}
  679. [:div.cp__plugins-item-lists-inner
  680. ;; items list
  681. (for [item sorted-plugins]
  682. (rum/with-key
  683. (let [pid (keyword (:id item))
  684. stat (:stat item)]
  685. (plugin-item-card t item
  686. (get-in item [:settings :disabled]) true *search-key installing
  687. (and installing (= (keyword (:id installing)) pid))
  688. (contains? installed-plugins pid) stat nil))
  689. (:id item)))]
  690. ;; items loader
  691. (when (seq sorted-plugins)
  692. (lazy-items-loader load-more-pages!))]])]))
  693. (rum/defcs installed-plugins
  694. < rum/static rum/reactive
  695. plugin-items-list-mixins
  696. (rum/local "" ::search-key)
  697. (rum/local :default ::filter-by) ;; default / enabled / disabled / unpacked / update-available
  698. (rum/local :default ::sort-by)
  699. (rum/local :plugins ::category)
  700. (rum/local nil ::cached-query-flag)
  701. (rum/local 1 ::current-page)
  702. [state]
  703. (let [*list-node-ref (rum/create-ref)
  704. installed-plugins (state/sub [:plugin/installed-plugins])
  705. installed-plugins (vals installed-plugins)
  706. updating (state/sub :plugin/installing)
  707. develop-mode? (state/sub :ui/developer-mode?)
  708. selected-unpacked-pkg (state/sub :plugin/selected-unpacked-pkg)
  709. coming-updates (state/sub :plugin/updates-coming)
  710. agent-opts (state/sub [:electron/user-cfgs :settings/agent])
  711. *filter-by (::filter-by state)
  712. *sort-by (::sort-by state)
  713. *search-key (::search-key state)
  714. *category (::category state)
  715. *cached-query-flag (::cached-query-flag state)
  716. *current-page (::current-page state)
  717. default-filter-by? (= :default @*filter-by)
  718. theme-plugins (filter #(:theme %) installed-plugins)
  719. normal-plugins (filter #(not (:theme %)) installed-plugins)
  720. filtered-plugins (when (seq installed-plugins)
  721. (if (= @*category :themes) theme-plugins normal-plugins))
  722. total-nums [(count normal-plugins) (count theme-plugins)]
  723. filtered-plugins (if-not default-filter-by?
  724. (filter (fn [it]
  725. (let [disabled (get-in it [:settings :disabled])]
  726. (case @*filter-by
  727. :enabled (not disabled)
  728. :disabled disabled
  729. :unpacked (not (:iir it))
  730. :update-available (state/plugin-update-available? (:id it))
  731. true))) filtered-plugins)
  732. filtered-plugins)
  733. filtered-plugins (if-not (string/blank? @*search-key)
  734. (if-let [author (and (string/starts-with? @*search-key "@")
  735. (subs @*search-key 1))]
  736. (filter #(= author (:author %)) filtered-plugins)
  737. (search/fuzzy-search
  738. filtered-plugins @*search-key
  739. :limit 30
  740. :extract-fn :name))
  741. filtered-plugins)
  742. sorted-plugins (if default-filter-by?
  743. (->> filtered-plugins
  744. (reduce #(let [disabled? (get-in %2 [:settings :disabled])
  745. old-dirty (get @*dirties-toggle-items (keyword (:id %2)))
  746. k (if (if (boolean? old-dirty) (not old-dirty) disabled?) 1 0)]
  747. (update %1 k conj %2)) [[] []])
  748. (#(update % 0 (fn [coll] (sort-by :iir coll))))
  749. (flatten))
  750. (do
  751. (clear-dirties-states!)
  752. filtered-plugins))
  753. fn-query-flag (fn [] (string/join "_" (map #(str @%) [*filter-by *sort-by *search-key *category])))
  754. str-query-flag (fn-query-flag)
  755. _ (when (not= str-query-flag @*cached-query-flag)
  756. (when-let [^js list-cnt (rum/deref *list-node-ref)]
  757. (set! (.-scrollTop list-cnt) 0))
  758. (reset! *current-page 1))
  759. _ (reset! *cached-query-flag str-query-flag)
  760. page-total-items (count sorted-plugins)
  761. sorted-plugins (if-not (> page-total-items PER-PAGE-SIZE)
  762. sorted-plugins (take (* @*current-page PER-PAGE-SIZE) sorted-plugins))
  763. load-more-pages! #(when (> page-total-items PER-PAGE-SIZE)
  764. (when (< (* PER-PAGE-SIZE @*current-page) page-total-items)
  765. (reset! *current-page (inc @*current-page))))]
  766. [:div.cp__plugins-installed
  767. (panel-control-tabs
  768. @*search-key *search-key
  769. @*category *category
  770. @*sort-by *sort-by
  771. @*filter-by *filter-by
  772. total-nums selected-unpacked-pkg
  773. false develop-mode? nil
  774. agent-opts)
  775. [:div.cp__plugins-item-lists.pb-6
  776. {:ref *list-node-ref}
  777. [:div.cp__plugins-item-lists-inner
  778. (for [item sorted-plugins]
  779. (rum/with-key
  780. (let [pid (keyword (:id item))]
  781. (plugin-item-card t item
  782. (get-in item [:settings :disabled]) false *search-key updating
  783. (and updating (= (keyword (:id updating)) pid))
  784. true nil (get coming-updates pid)))
  785. (:id item)))]
  786. (when (seq sorted-plugins)
  787. (lazy-items-loader load-more-pages!))]]))
  788. (rum/defcs waiting-coming-updates
  789. < rum/reactive
  790. {:will-mount (fn [s] (state/reset-unchecked-update) s)}
  791. [_s]
  792. (let [_ (state/sub :plugin/updates-coming)
  793. downloading? (state/sub :plugin/updates-downloading?)
  794. unchecked (state/sub :plugin/updates-unchecked)
  795. updates (state/all-available-coming-updates)]
  796. [:div.cp__plugins-waiting-updates
  797. [:h1.mb-4.text-2xl.p-1 (t :plugin/found-n-updates (count updates))]
  798. (if (seq updates)
  799. ;; lists
  800. [:ul
  801. {:class (when downloading? "downloading")}
  802. (for [it updates
  803. :let [k (str "lsp-it-" (:id it))
  804. c? (not (contains? unchecked (:id it)))
  805. notes (util/trim-safe (:latest-notes it))]]
  806. [:li.flex.items-center
  807. {:key k
  808. :class (when c? "checked")}
  809. [:label.flex-1
  810. {:for k}
  811. (ui/checkbox {:id k
  812. :checked c?
  813. :on-change (fn [^js e]
  814. (when-not downloading?
  815. (state/set-unchecked-update (:id it) (not (util/echecked? e)))))})
  816. [:strong.px-3 (:title it)
  817. [:sup (str (:version it) " 👉 " (:latest-version it))]]]
  818. [:div.px-4
  819. (when-not (string/blank? notes)
  820. (ui/tippy
  821. {:html [:p notes]}
  822. [:span.opacity-30.hover:opacity-80 (ui/icon "info-circle")]))]])]
  823. ;; all done
  824. [:div.py-4 [:strong.text-4xl (str "\uD83C\uDF89 " (t :plugin/all-updated))]])
  825. ;; actions
  826. (when (seq updates)
  827. [:div.pt-5.flex.justify-end
  828. (ui/button
  829. (if downloading?
  830. [:span (ui/loading (t :plugin/updates-downloading))]
  831. [:span.flex.items-center (ui/icon "download") (t :plugin/update-all-selected)])
  832. :on-click
  833. #(when-not downloading?
  834. (plugin-handler/open-updates-downloading)
  835. (if-let [n (state/get-next-selected-coming-update)]
  836. (plugin-handler/check-or-update-marketplace-plugin!
  837. (assoc n :only-check false)
  838. (fn [^js e] (notification/show! (.toString e) :error)))
  839. (plugin-handler/close-updates-downloading)))
  840. :disabled
  841. (or downloading?
  842. (and (seq unchecked)
  843. (= (count unchecked) (count updates)))))])]))
  844. (rum/defc plugins-from-file
  845. < rum/reactive
  846. [plugins]
  847. [:div.cp__plugins-fom-file
  848. [:h1.mb-4.text-2xl.p-1 (t :plugin.install-from-file/title)]
  849. (if (seq plugins)
  850. [:div
  851. [:div.mb-2.text-xl (t :plugin.install-from-file/notice)]
  852. ;; lists
  853. [:ul
  854. (for [it (:install plugins)
  855. :let [k (str "lsp-it-" (name (:id it)))]]
  856. [:li.flex.items-center
  857. {:key k}
  858. [:label.flex-1
  859. {:for k}
  860. [:strong.px-3 (str (name (:id it)) " " (:version it))]]])]
  861. ;; actions
  862. [:div.pt-5
  863. (ui/button [:span (t :plugin/install)]
  864. :on-click #(do
  865. (plugin-config-handler/replace-plugins plugins)
  866. (state/close-sub-modal! "ls-plugins-from-file-modal")))]]
  867. ;; all done
  868. [:div.py-4 [:strong.text-xl (str "\uD83C\uDF89 " (t :plugin.install-from-file/success))]])])
  869. (defn open-select-theme!
  870. []
  871. (state/set-sub-modal! installed-themes))
  872. (rum/defc hook-ui-slot
  873. ([type payload] (hook-ui-slot type payload nil #(plugin-handler/hook-plugin-app type % nil)))
  874. ([type payload opts callback]
  875. (let [rs (util/rand-str 8)
  876. id (str "slot__" rs)
  877. *el-ref (rum/use-ref nil)]
  878. (rum/use-effect!
  879. (fn []
  880. (let [timer (js/setTimeout #(callback {:type type :slot id :payload payload}) 50)]
  881. #(js/clearTimeout timer)))
  882. [id])
  883. (rum/use-effect!
  884. (fn []
  885. (let [el (rum/deref *el-ref)]
  886. #(when-let [uis (seq (.querySelectorAll el "[data-injected-ui]"))]
  887. (doseq [^js el uis]
  888. (when-let [id (.-injectedUi (.-dataset el))]
  889. (js/LSPluginCore._forceCleanInjectedUI id))))))
  890. [])
  891. [:div.lsp-hook-ui-slot
  892. (merge opts {:id id
  893. :ref *el-ref
  894. :on-mouse-down (fn [e] (util/stop-propagation e))})])))
  895. (rum/defc hook-block-slot < rum/static
  896. [type block]
  897. (hook-ui-slot type {} nil #(plugin-handler/hook-plugin-block-slot block %)))
  898. (rum/defc ui-item-renderer
  899. [pid type {:keys [key template prefix]}]
  900. (let [*el (rum/use-ref nil)
  901. uni #(str prefix "injected-ui-item-" %)
  902. ^js pl (js/LSPluginCore.registeredPlugins.get (name pid))]
  903. (rum/use-effect!
  904. (fn []
  905. (when-let [^js el (rum/deref *el)]
  906. (js/LSPlugin.pluginHelpers.setupInjectedUI.call
  907. pl #js {:slot (.-id el) :key key :template template} #js {})))
  908. [template])
  909. (if-not (nil? pl)
  910. [:div
  911. {:id (uni (str (name key) "-" (name pid)))
  912. :title key
  913. :class (uni (name type))
  914. :ref *el}]
  915. [:<>])))
  916. (rum/defc toolbar-plugins-manager-list
  917. [updates-coming items]
  918. (let [badge-updates? (and (not (plugin-handler/get-auto-checking?))
  919. (seq (state/all-available-coming-updates updates-coming)))]
  920. (ui/dropdown-with-links
  921. (fn [{:keys [toggle-fn]}]
  922. [:div.toolbar-plugins-manager
  923. {:on-click toggle-fn}
  924. [:a.button.relative
  925. (ui/icon "puzzle" {:size 20})
  926. (when badge-updates?
  927. (ui/point "bg-red-600.top-1.right-1.absolute" 4 {:style {:margin-right 2 :margin-top 2}}))]])
  928. ;; items
  929. (concat
  930. (for [[_ {:keys [key pinned?] :as opts} pid] items
  931. :let [pkey (str (name pid) ":" key)]]
  932. {:title key
  933. :item [:div.flex.items-center.item-wrap
  934. (ui-item-renderer pid :toolbar (assoc opts :prefix "pl-" :key (str "pl-" key)))
  935. [:span.opacity-80 {:style {:padding-left "2px"}} key]
  936. [:span.pin.flex.items-center.opacity-60
  937. {:class (util/classnames [{:pinned pinned?}])}
  938. (ui/icon (if pinned? "pinned" "pin"))]]
  939. :options {:on-click (fn [^js e]
  940. (let [^js target (.-target e)
  941. user-btn? (boolean (.closest target "div[data-injected-ui]"))]
  942. (when-not user-btn?
  943. (plugin-handler/op-pinned-toolbar-item! pkey (if pinned? :remove :add))))
  944. false)}})
  945. [{:hr true}
  946. {:title (t :plugins)
  947. :options {:on-click #(plugin-handler/goto-plugins-dashboard!)
  948. :class "extra-item mt-2"}
  949. :icon (ui/icon "apps")}
  950. {:title (t :settings)
  951. :options {:on-click #(plugin-handler/goto-plugins-settings!)
  952. :class "extra-item"}
  953. :icon (ui/icon "adjustments")}
  954. (when badge-updates?
  955. {:title [:div.flex.items-center.space-x-5.leading-none
  956. [:span (t :plugin/found-updates)] (ui/point "bg-red-600" 5 {:style {:margin-top 2}})]
  957. :options {:on-click #(open-waiting-updates-modal!)
  958. :class "extra-item"}
  959. :icon (ui/icon "download")})]
  960. [{:hr true :key "dropdown-more"}
  961. {:title (auto-check-for-updates-control)
  962. :options {:no-padding? true}}])
  963. {:trigger-class "toolbar-plugins-manager-trigger"})))
  964. (rum/defc header-ui-items-list-wrap
  965. [children]
  966. (let [*wrap-el (rum/use-ref nil)
  967. [right-sidebar-resized] (rum-utils/use-atom ui-handler/*right-sidebar-resized-at)]
  968. (rum/use-effect!
  969. (fn []
  970. (when-let [^js wrap-el (rum/deref *wrap-el)]
  971. (when-let [^js header-el (.closest wrap-el ".cp__header")]
  972. (let [^js header-l (.querySelector header-el "* > .l")
  973. ^js header-r (.querySelector header-el "* > .r")
  974. set-max-width! #(when (number? %) (set! (.-maxWidth (.-style wrap-el)) (str % "px")))
  975. calc-wrap-max-width #(let [width-l (.-offsetWidth header-l)
  976. width-t (-> (js/document.querySelector "#main-content-container") (.-offsetWidth))
  977. children (to-array (.-children header-r))
  978. width-c' (reduce (fn [acc ^js e]
  979. (when (some-> e (.-classList) (.contains "ui-items-container") (not))
  980. (+ acc (or (.-offsetWidth e) 0)))) 0 children)]
  981. (when-let [width-t (and (number? width-t)
  982. (if-not (state/get-left-sidebar-open?)
  983. (- width-t width-l) width-t))]
  984. (set-max-width! (max (- width-t width-c' 100) 76))))]
  985. (.addEventListener js/window "resize" calc-wrap-max-width)
  986. (js/setTimeout calc-wrap-max-width 16)
  987. #(.removeEventListener js/window "resize" calc-wrap-max-width)))))
  988. [right-sidebar-resized])
  989. [:div.list-wrap
  990. {:ref *wrap-el}
  991. children]))
  992. (rum/defcs hook-ui-items < rum/reactive
  993. < {:key-fn #(identity "plugin-hook-items")}
  994. "type of :toolbar, :pagebar"
  995. [_state type]
  996. (when (state/sub [:plugin/installed-ui-items])
  997. (let [toolbar? (= :toolbar type)
  998. pinned-items (state/sub [:plugin/preferences :pinnedToolbarItems])
  999. pinned-items (and (sequential? pinned-items) (into #{} pinned-items))
  1000. items (state/get-plugins-ui-items-with-type type)
  1001. items (sort-by #(:key (second %)) items)]
  1002. (when-let [items (and (seq items)
  1003. (if toolbar?
  1004. (map #(assoc-in % [1 :pinned?]
  1005. (let [[_ {:keys [key]} pid] %
  1006. pkey (str (name pid) ":" key)]
  1007. (contains? pinned-items pkey)))
  1008. items)
  1009. items))]
  1010. [:div.ui-items-container
  1011. {:data-type (name type)}
  1012. [:<>
  1013. (header-ui-items-list-wrap
  1014. (for [[_ {:keys [key pinned?] :as opts} pid] items]
  1015. (when (or (not toolbar?)
  1016. (not (set? pinned-items)) pinned?)
  1017. (rum/with-key (ui-item-renderer pid type opts) key))))
  1018. ;; manage plugin buttons
  1019. (when toolbar?
  1020. (let [updates-coming (state/sub :plugin/updates-coming)]
  1021. (toolbar-plugins-manager-list updates-coming items)))]]))))
  1022. (rum/defc hook-ui-fenced-code
  1023. [block content {:keys [render edit] :as _opts}]
  1024. (let [[content1 set-content1!] (rum/use-state content)
  1025. [editor-active? set-editor-active!] (rum/use-state (string/blank? content))
  1026. *cm (rum/use-ref nil)
  1027. *el (rum/use-ref nil)]
  1028. (rum/use-effect!
  1029. #(set-content1! content)
  1030. [content])
  1031. (rum/use-effect!
  1032. (fn []
  1033. (some-> (rum/deref *el)
  1034. (.closest ".ui-fenced-code-wrap")
  1035. (.-classList)
  1036. (#(if editor-active?
  1037. (.add % "is-active")
  1038. (.remove % "is-active"))))
  1039. (when-let [cm (rum/deref *cm)]
  1040. (.refresh cm)
  1041. (.focus cm)
  1042. (.setCursor cm (.lineCount cm) (count (.getLine cm (.lastLine cm))))))
  1043. [editor-active?])
  1044. (rum/use-effect!
  1045. (fn []
  1046. (let [t (js/setTimeout
  1047. #(when-let [^js cm (some-> (rum/deref *el)
  1048. (.closest ".ui-fenced-code-wrap")
  1049. (.querySelector ".CodeMirror")
  1050. (.-CodeMirror))]
  1051. (rum/set-ref! *cm cm)
  1052. (doto cm
  1053. (.on "change" (fn []
  1054. (some-> cm (.getDoc) (.getValue) (set-content1!))))))
  1055. ;; wait for the cm loaded
  1056. 1000)]
  1057. #(js/clearTimeout t)))
  1058. [])
  1059. [:div.ui-fenced-code-result
  1060. {:on-mouse-down (fn [e] (when (false? edit) (util/stop e)))
  1061. :class (util/classnames [{:not-edit (false? edit)}])
  1062. :ref *el}
  1063. [:<>
  1064. [:span.actions
  1065. {:on-mouse-down #(util/stop %)}
  1066. (ui/button (ui/icon "square-toggle-horizontal" {:size 14})
  1067. :on-click #(set-editor-active! (not editor-active?)))
  1068. (ui/button (ui/icon "source-code" {:size 14})
  1069. :on-click #(editor-handler/edit-block! block (count content1) (:block/uuid block)))]
  1070. (when (fn? render)
  1071. (js/React.createElement render #js {:content content1}))]]))
  1072. (rum/defc plugins-page
  1073. []
  1074. (let [[active set-active!] (rum/use-state :installed)
  1075. market? (= active :marketplace)
  1076. *el-ref (rum/create-ref)]
  1077. (rum/use-effect!
  1078. (fn []
  1079. (state/load-app-user-cfgs)
  1080. #(clear-dirties-states!))
  1081. [])
  1082. (rum/use-effect!
  1083. #(clear-dirties-states!)
  1084. [market?])
  1085. [:div.cp__plugins-page
  1086. {:ref *el-ref
  1087. :tab-index "-1"}
  1088. [:h1 (t :plugins)]
  1089. (security-warning)
  1090. [:hr.my-4]
  1091. [:div.tabs.flex.items-center.justify-center
  1092. [:div.tabs-inner.flex.items-center
  1093. (ui/button [:span.it (t :plugin/installed)]
  1094. :on-click #(set-active! :installed)
  1095. :intent (if-not market? "" "link"))
  1096. (ui/button [:span.mk (svg/apps 16) (t :plugin/marketplace)]
  1097. :on-click #(set-active! :marketplace)
  1098. :intent (if market? "" "link"))]]
  1099. [:div.panels
  1100. (if market?
  1101. (marketplace-plugins)
  1102. (installed-plugins))]]))
  1103. (def *updates-sub-content-timer (atom nil))
  1104. (def *updates-sub-content (atom nil))
  1105. (defn set-updates-sub-content!
  1106. [content duration]
  1107. (reset! *updates-sub-content content)
  1108. (when (> duration 0)
  1109. (some-> @*updates-sub-content-timer (js/clearTimeout))
  1110. (->> (js/setTimeout #(reset! *updates-sub-content nil) duration)
  1111. (reset! *updates-sub-content-timer))))
  1112. (rum/defc updates-notifications-impl
  1113. [check-pending? auto-checking? online?]
  1114. (let [[uid, set-uid] (rum/use-state nil)
  1115. [sub-content, _set-sub-content!] (rum-utils/use-atom *updates-sub-content)
  1116. notify! (fn [content status]
  1117. (if auto-checking?
  1118. (println (t :plugin/list-of-updates) content)
  1119. (let [cb #(plugin-handler/cancel-user-checking!)]
  1120. (try
  1121. (set-uid (notification/show! content status false uid nil cb))
  1122. (catch js/Error _
  1123. (set-uid (notification/show! content status false nil nil cb)))))))]
  1124. (rum/use-effect!
  1125. (fn []
  1126. (if check-pending?
  1127. (notify!
  1128. [:div
  1129. [:div (t :plugin/checking-for-updates)]
  1130. (when sub-content [:p.opacity-60 sub-content])]
  1131. (ui/loading ""))
  1132. (when uid (notification/clear! uid))))
  1133. [check-pending? sub-content])
  1134. (rum/use-effect!
  1135. ;; scheduler for auto updates
  1136. (fn []
  1137. (when online?
  1138. (let [last-updates (storage/get :lsp-last-auto-updates)]
  1139. (when (and (not (false? last-updates))
  1140. (or (true? last-updates)
  1141. (not (number? last-updates))
  1142. ;; interval 12 hours
  1143. (> (- (js/Date.now) last-updates) (* 60 60 12 1000))))
  1144. (js/setTimeout
  1145. (fn []
  1146. (plugin-handler/auto-check-enabled-for-updates!)
  1147. (storage/set :lsp-last-auto-updates (js/Date.now))))))))
  1148. [online?])
  1149. [:<>]))
  1150. (rum/defcs updates-notifications < rum/reactive
  1151. [_]
  1152. (let [updates-pending (state/sub :plugin/updates-pending)
  1153. online? (state/sub :network/online?)
  1154. auto-checking? (state/sub :plugin/updates-auto-checking?)
  1155. check-pending? (boolean (seq updates-pending))]
  1156. (updates-notifications-impl check-pending? auto-checking? online?)))
  1157. (rum/defcs focused-settings-content
  1158. < rum/reactive
  1159. (rum/local (state/sub :plugin/focused-settings) ::cache)
  1160. [_state title]
  1161. (let [*cache (::cache _state)
  1162. focused (state/sub :plugin/focused-settings)
  1163. nav? (state/sub :plugin/navs-settings?)
  1164. _ (state/sub :plugin/installed-plugins)
  1165. _ (js/setTimeout #(reset! *cache focused) 100)]
  1166. [:div.cp__plugins-settings.cp__settings-main
  1167. [:header
  1168. [:h1.title (ui/icon "puzzle" {:size 22})
  1169. [:strong (or title (t :settings-of-plugins))]]]
  1170. [:div.cp__settings-inner.md:flex
  1171. {:class (util/classnames [{:no-aside (not nav?)}])}
  1172. (when nav?
  1173. [:aside.md:w-64 {:style {:min-width "10rem"}}
  1174. (let [plugins (plugin-handler/get-enabled-plugins-if-setting-schema)]
  1175. [:ul.settings-plugin-list
  1176. (for [{:keys [id name title icon]} plugins]
  1177. [:li
  1178. {:key id :class (util/classnames [{:active (= id focused)}])}
  1179. [:a.flex.items-center.settings-plugin-item
  1180. {:data-id id
  1181. :on-click #(do (state/set-state! :plugin/focused-settings id))}
  1182. (if (and icon (not (string/blank? icon)))
  1183. [:img.icon {:src icon}]
  1184. svg/folder)
  1185. [:strong.flex-1 (or title name)]]])])])
  1186. [:article
  1187. [:div.panel-wrap
  1188. {:data-id focused}
  1189. (when-let [^js pl (and focused (= @*cache focused)
  1190. (plugin-handler/get-plugin-inst focused))]
  1191. (ui/catch-error
  1192. [:p.warning.text-lg.mt-5 "Settings schema Error!"]
  1193. (plugins-settings/settings-container
  1194. (bean/->clj (.-settingsSchema pl)) pl)))]]]]))
  1195. (rum/defc custom-js-installer
  1196. [{:keys [t current-repo db-restoring? nfs-granted?]}]
  1197. (rum/use-effect!
  1198. (fn []
  1199. (when (and (not db-restoring?)
  1200. (or (not util/nfs?) nfs-granted?))
  1201. (ui-handler/exec-js-if-exists-&-allowed! t)))
  1202. [current-repo db-restoring? nfs-granted?])
  1203. nil)
  1204. (rum/defc perf-tip-content
  1205. [pid name url]
  1206. [:div
  1207. [:span.block.whitespace-normal
  1208. "This plugin "
  1209. [:strong.text-error "#" name]
  1210. " takes too long to load, affecting the application startup time and
  1211. potentially causing other plugins to fail to load."]
  1212. [:path.opacity-50
  1213. [:small [:span.pr-1 (ui/icon "folder")] url]]
  1214. [:p
  1215. (ui/button "Disable now"
  1216. :small? true
  1217. :on-click
  1218. (fn []
  1219. (-> (js/LSPluginCore.disable pid)
  1220. (p/then #(do
  1221. (notification/clear! pid)
  1222. (notification/show!
  1223. [:span "The plugin "
  1224. [:strong.text-error "#" name]
  1225. " is disabled."] :success
  1226. true nil 3000 nil)))
  1227. (p/catch #(js/console.error %)))))]])
  1228. (defn open-plugins-modal!
  1229. []
  1230. (state/set-modal!
  1231. (fn [_close!]
  1232. (plugins-page))
  1233. {:label "plugins-dashboard"}))
  1234. (defn open-waiting-updates-modal!
  1235. []
  1236. (state/set-sub-modal!
  1237. (fn [_close!]
  1238. (waiting-coming-updates))
  1239. {:center? true}))
  1240. (defn open-plugins-from-file-modal!
  1241. [plugins]
  1242. (state/set-sub-modal!
  1243. (fn [_close!]
  1244. (plugins-from-file plugins))
  1245. {:center? true
  1246. :id "ls-plugins-from-file-modal"}))
  1247. (defn open-focused-settings-modal!
  1248. [title]
  1249. (state/set-sub-modal!
  1250. (fn [_close!]
  1251. [:div.settings-modal.of-plugins
  1252. (focused-settings-content title)])
  1253. {:center? false
  1254. :label "plugin-settings-modal"
  1255. :id "ls-focused-settings-modal"}))