plugin.cljs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. (ns frontend.handler.plugin
  2. (:require [promesa.core :as p]
  3. [rum.core :as rum]
  4. [frontend.util :as util]
  5. [clojure.walk :as walk]
  6. [logseq.graph-parser.mldoc :as gp-mldoc]
  7. [frontend.handler.notification :as notifications]
  8. [camel-snake-kebab.core :as csk]
  9. [frontend.state :as state]
  10. [medley.core :as medley]
  11. [frontend.fs :as fs]
  12. [electron.ipc :as ipc]
  13. [cljs-bean.core :as bean]
  14. [clojure.string :as string]
  15. [lambdaisland.glogi :as log]
  16. [frontend.components.svg :as svg]
  17. [frontend.format :as format]))
  18. (defonce lsp-enabled?
  19. (and (util/electron?)
  20. (state/lsp-enabled?-or-theme)))
  21. (defn- normalize-keyword-for-json
  22. [input]
  23. (when input
  24. (walk/postwalk
  25. (fn [a]
  26. (cond
  27. (keyword? a) (csk/->camelCase (name a))
  28. (uuid? a) (str a)
  29. :else a)) input)))
  30. (defn invoke-exported-api
  31. [type & args]
  32. (try
  33. (apply js-invoke (aget js/window.logseq "api") type args)
  34. (catch js/Error e (js/console.error e))))
  35. ;; state handlers
  36. (defonce central-endpoint "https://raw.githubusercontent.com/logseq/marketplace/master/")
  37. (defonce plugins-url (str central-endpoint "plugins.json"))
  38. (defonce stats-url (str central-endpoint "stats.json"))
  39. (declare select-a-plugin-theme)
  40. (defn gh-repo-url [repo]
  41. (str "https://github.com/" repo))
  42. (defn pkg-asset [id asset]
  43. (if (and asset (string/starts-with? asset "http"))
  44. asset (when-let [asset (and asset (string/replace asset #"^[./]+" ""))]
  45. (str central-endpoint "packages/" id "/" asset))))
  46. (defn load-marketplace-plugins
  47. [refresh?]
  48. (if (or refresh? (nil? (:plugin/marketplace-pkgs @state/state)))
  49. (p/create
  50. (fn [resolve reject]
  51. (-> (util/fetch plugins-url
  52. (fn [res]
  53. (let [pkgs (:packages res)]
  54. (state/set-state! :plugin/marketplace-pkgs pkgs)
  55. (resolve pkgs)))
  56. reject)
  57. (p/catch reject))))
  58. (p/resolved (:plugin/marketplace-pkgs @state/state))))
  59. (defn load-marketplace-stats
  60. [refresh?]
  61. (if (or refresh? (nil? (:plugin/marketplace-stats @state/state)))
  62. (p/create
  63. (fn [resolve reject]
  64. (util/fetch stats-url
  65. (fn [res]
  66. (when res
  67. (state/set-state!
  68. :plugin/marketplace-stats
  69. (into {} (map (fn [[k stat]]
  70. [k (assoc stat
  71. :total_downloads
  72. (reduce (fn [a b] (+ a (get b 2))) 0 (:releases stat)))])
  73. res)))
  74. (resolve nil)))
  75. reject)))
  76. (p/resolved nil)))
  77. (defn installed?
  78. [id]
  79. (and (contains? (:plugin/installed-plugins @state/state) (keyword id))
  80. (get-in @state/state [:plugin/installed-plugins (keyword id) :iir])))
  81. (defn install-marketplace-plugin
  82. [{:keys [id] :as mft}]
  83. (when-not (and (:plugin/installing @state/state)
  84. (installed? id))
  85. (p/create
  86. (fn [resolve]
  87. (state/set-state! :plugin/installing mft)
  88. (ipc/ipc :installMarketPlugin mft)
  89. (resolve id)))))
  90. (defn check-or-update-marketplace-plugin
  91. [{:keys [id] :as pkg} error-handler]
  92. (when-not (and (:plugin/installing @state/state)
  93. (not (installed? id)))
  94. (p/catch
  95. (p/then
  96. (do (state/set-state! :plugin/installing pkg)
  97. (p/catch
  98. (load-marketplace-plugins false)
  99. (fn [^js e]
  100. (state/reset-all-updates-state)
  101. (throw e))))
  102. (fn [mfts]
  103. (let [mft (some #(when (= (:id %) id) %) mfts)]
  104. ;;TODO: (throw (js/Error. [:not-found-in-marketplace id]))
  105. (ipc/ipc :updateMarketPlugin (merge (dissoc pkg :logger) mft)))
  106. true))
  107. (fn [^js e]
  108. (error-handler e)
  109. (state/set-state! :plugin/installing nil)
  110. (js/console.error e)))))
  111. (defn get-plugin-inst
  112. [id]
  113. (try
  114. (js/LSPluginCore.ensurePlugin (name id))
  115. (catch js/Error _e
  116. nil)))
  117. (defn open-updates-downloading
  118. []
  119. (when (and (not (:plugin/updates-downloading? @state/state))
  120. (seq (state/all-available-coming-updates)))
  121. (->> (:plugin/updates-coming @state/state)
  122. (map #(if (state/coming-update-new-version? (second %1))
  123. (update % 1 dissoc :error-code) %1))
  124. (into {})
  125. (state/set-state! :plugin/updates-coming))
  126. (state/set-state! :plugin/updates-downloading? true)))
  127. (defn close-updates-downloading
  128. []
  129. (when (:plugin/updates-downloading? @state/state)
  130. (state/set-state! :plugin/updates-downloading? false)))
  131. (defn has-setting-schema?
  132. [id]
  133. (when-let [pl (and id (get-plugin-inst (name id)))]
  134. (boolean (.-settingsSchema pl))))
  135. (defn get-enabled-plugins-if-setting-schema
  136. []
  137. (when-let [plugins (seq (state/get-enabled?-installed-plugins false nil true))]
  138. (filter #(has-setting-schema? (:id %)) plugins)))
  139. (defn setup-install-listener!
  140. [t]
  141. (let [channel (name :lsp-installed)
  142. listener (fn [^js _ ^js e]
  143. (js/console.debug :lsp-installed e)
  144. (when-let [{:keys [status payload only-check]} (bean/->clj e)]
  145. (case (keyword status)
  146. :completed
  147. (let [{:keys [id dst name title theme]} payload
  148. name (or title name "Untitled")]
  149. (if only-check
  150. (state/consume-updates-coming-plugin payload false)
  151. (if (installed? id)
  152. (when-let [^js pl (get-plugin-inst id)] ;; update
  153. (p/then
  154. (.reload pl)
  155. #(do
  156. ;;(if theme (select-a-plugin-theme id))
  157. (notifications/show!
  158. (str (t :plugin/update) (t :plugins) ": " name " - " (.-version (.-options pl))) :success)
  159. (state/consume-updates-coming-plugin payload true))))
  160. (do ;; register new
  161. (p/then
  162. (js/LSPluginCore.register (bean/->js {:key id :url dst}))
  163. (fn [] (when theme (js/setTimeout #(select-a-plugin-theme id) 300))))
  164. (notifications/show!
  165. (str (t :plugin/installed) (t :plugins) ": " name) :success)))))
  166. :error
  167. (let [error-code (keyword (string/replace (:error-code payload) #"^[\s\:\[]+" ""))
  168. [msg type] (case error-code
  169. :no-new-version
  170. [(str (t :plugin/up-to-date) " :)") :success]
  171. [error-code :error])
  172. pending? (seq (:plugin/updates-pending @state/state))]
  173. (if (and only-check pending?)
  174. (state/consume-updates-coming-plugin payload false)
  175. (do
  176. ;; consume failed download updates
  177. (when (and (not only-check) (not pending?))
  178. (state/consume-updates-coming-plugin payload true))
  179. ;; notify human tips
  180. (notifications/show!
  181. (str
  182. (if (= :error type) "[Error]" "")
  183. (str "<" (:id payload) "> ")
  184. msg) type)))
  185. (js/console.error payload))
  186. :dunno))
  187. ;; reset
  188. (js/setTimeout #(state/set-state! :plugin/installing nil) 512)
  189. true)]
  190. (js/window.apis.addListener channel listener)
  191. ;; clear
  192. (fn []
  193. (js/window.apis.removeAllListeners channel))))
  194. (defn register-plugin
  195. [pl]
  196. (swap! state/state update-in [:plugin/installed-plugins] assoc (keyword (:id pl)) pl))
  197. (defn unregister-plugin
  198. [id]
  199. (js/LSPluginCore.unregister id))
  200. (defn host-mounted!
  201. []
  202. (and lsp-enabled? (js/LSPluginCore.hostMounted)))
  203. (defn register-plugin-slash-command
  204. [pid [cmd actions]]
  205. (when-let [pid (keyword pid)]
  206. (when (contains? (:plugin/installed-plugins @state/state) pid)
  207. (swap! state/state update-in [:plugin/installed-slash-commands pid]
  208. (fnil merge {}) (hash-map cmd (mapv #(conj % {:pid pid}) actions)))
  209. (state/pub-event! [:rebuild-slash-commands-list])
  210. true)))
  211. (defn unregister-plugin-slash-command
  212. [pid]
  213. (swap! state/state medley/dissoc-in [:plugin/installed-slash-commands (keyword pid)])
  214. (state/pub-event! [:rebuild-slash-commands-list]))
  215. (def keybinding-mode-handler-map
  216. {:global :shortcut.handler/editor-global
  217. :non-editing :shortcut.handler/global-non-editing-only
  218. :editing :shortcut.handler/block-editing-only})
  219. (defn simple-cmd->palette-cmd
  220. [pid {:keys [key label type desc keybinding] :as cmd} action]
  221. (let [palette-cmd {:id (keyword (str "plugin." pid "/" key))
  222. :desc (or desc label)
  223. :shortcut (when-let [shortcut (:binding keybinding)]
  224. (if util/mac?
  225. (or (:mac keybinding) shortcut)
  226. shortcut))
  227. :handler-id (let [mode (or (:mode keybinding) :global)]
  228. (get keybinding-mode-handler-map (keyword mode)))
  229. :action (fn []
  230. (state/pub-event!
  231. [:exec-plugin-cmd {:type type :key key :pid pid :cmd cmd :action action}]))}]
  232. palette-cmd))
  233. (defn simple-cmd-keybinding->shortcut-args
  234. [pid key keybinding]
  235. (let [id (keyword (str "plugin." pid "/" key))
  236. binding (:binding keybinding)
  237. binding (if util/mac?
  238. (or (:mac keybinding) binding)
  239. binding)
  240. mode (or (:mode keybinding) :global)
  241. mode (get keybinding-mode-handler-map (keyword mode))]
  242. [mode id {:binding binding}]))
  243. (defn register-plugin-simple-command
  244. ;; action => [:action-key :event-key]
  245. [pid {:keys [type] :as cmd} action]
  246. (when-let [pid (keyword pid)]
  247. (when (contains? (:plugin/installed-plugins @state/state) pid)
  248. (swap! state/state update-in [:plugin/simple-commands pid]
  249. (fnil conj []) [type cmd action pid])
  250. true)))
  251. (defn unregister-plugin-simple-command
  252. [pid]
  253. (swap! state/state medley/dissoc-in [:plugin/simple-commands (keyword pid)]))
  254. (defn register-plugin-ui-item
  255. [pid {:keys [type] :as opts}]
  256. (when-let [pid (keyword pid)]
  257. (when (contains? (:plugin/installed-plugins @state/state) pid)
  258. (swap! state/state update-in [:plugin/installed-ui-items pid]
  259. (fnil conj []) [type opts pid])
  260. true)))
  261. (defn unregister-plugin-ui-items
  262. [pid]
  263. (swap! state/state assoc-in [:plugin/installed-ui-items (keyword pid)] []))
  264. (defn register-plugin-resources
  265. [pid type {:keys [key] :as opts}]
  266. (when-let [pid (keyword pid)]
  267. (when-let [type (and key (keyword type))]
  268. (let [path [:plugin/installed-resources pid type]]
  269. (when (contains? #{:error nil} (get-in @state/state (conj path key)))
  270. (swap! state/state update-in path
  271. (fnil assoc {}) key (merge opts {:pid pid}))
  272. true)))))
  273. (defn unregister-plugin-resources
  274. [pid]
  275. (when-let [pid (keyword pid)]
  276. (swap! state/state medley/dissoc-in [:plugin/installed-resources pid])
  277. true))
  278. (defn unregister-plugin-themes
  279. ([pid] (unregister-plugin-themes pid true))
  280. ([pid effect]
  281. (js/LSPluginCore.unregisterTheme (name pid) effect)))
  282. (def *fenced-code-providers (atom #{}))
  283. (defn register-fenced-code-renderer
  284. [pid type {:keys [before subs render edit] :as _opts}]
  285. (when-let [key (and type (keyword type))]
  286. (register-plugin-resources pid :fenced-code-renderers
  287. {:key key :edit edit :before before :subs subs :render render})
  288. (swap! *fenced-code-providers conj pid)
  289. #(swap! *fenced-code-providers disj pid)))
  290. (defn hook-fenced-code-by-type
  291. [type]
  292. (when-let [key (and (seq @*fenced-code-providers) type (keyword type))]
  293. (first (map #(state/get-plugin-resource % :fenced-code-renderers key)
  294. @*fenced-code-providers))))
  295. (def *extensions-enhancer-providers (atom #{}))
  296. (defn register-extensions-enhancer
  297. [pid type {:keys [enhancer] :as _opts}]
  298. (when-let [key (and type (keyword type))]
  299. (register-plugin-resources pid :extensions-enhancers
  300. {:key key :enhancer enhancer})
  301. (swap! *extensions-enhancer-providers conj pid)
  302. #(swap! *extensions-enhancer-providers disj pid)))
  303. (defn hook-extensions-enhancer-by-type
  304. [type]
  305. (when-let [key (and type (keyword type))]
  306. (map #(state/get-plugin-resource % :extensions-enhancers key)
  307. @*extensions-enhancer-providers)))
  308. (defn select-a-plugin-theme
  309. [pid]
  310. (when-let [themes (get (group-by :pid (:plugin/installed-themes @state/state)) pid)]
  311. (when-let [theme (first themes)]
  312. (js/LSPluginCore.selectTheme (bean/->js theme)))))
  313. (defn update-plugin-settings-state
  314. [id settings]
  315. (state/set-state! [:plugin/installed-plugins id :settings]
  316. ;; TODO: force settings related ui reactive
  317. ;; Sometimes toggle to `disable` not working
  318. ;; But related-option data updated?
  319. (assoc settings :disabled (boolean (:disabled settings)))))
  320. (defn open-settings-file-in-default-app!
  321. [id-or-plugin]
  322. (when-let [plugin (if (coll? id-or-plugin)
  323. id-or-plugin (state/get-plugin-by-id id-or-plugin))]
  324. (when-let [file-path (:usf plugin)]
  325. (js/apis.openPath file-path))))
  326. (defn open-plugin-settings!
  327. ([id] (open-plugin-settings! id false))
  328. ([id nav?]
  329. (when-let [plugin (and id (state/get-plugin-by-id id))]
  330. (if (has-setting-schema? id)
  331. (state/pub-event! [:go/plugins-settings id nav? (or (:name plugin) (:title plugin))])
  332. (open-settings-file-in-default-app! plugin)))))
  333. (defn parse-user-md-content
  334. [content {:keys [url]}]
  335. (try
  336. (when-not (string/blank? content)
  337. (let [content (if-not (string/blank? url)
  338. (string/replace
  339. content #"!\[[^\]]*\]\((.*?)\s*(\"(?:.*[^\"])\")?\s*\)"
  340. (fn [[matched link]]
  341. (if (and link (not (string/starts-with? link "http")))
  342. (string/replace matched link (util/node-path.join url link))
  343. matched)))
  344. content)]
  345. (format/to-html content :markdown (gp-mldoc/default-config :markdown))))
  346. (catch js/Error e
  347. (log/error :parse-user-md-exception e)
  348. content)))
  349. (defn open-readme!
  350. [url item display]
  351. (let [repo (:repo item)]
  352. (if (nil? repo)
  353. ;; local
  354. (-> (p/let [content (invoke-exported-api "load_plugin_readme" url)
  355. content (parse-user-md-content content item)]
  356. (and (string/blank? (string/trim content)) (throw nil))
  357. (state/set-state! :plugin/active-readme [content item])
  358. (state/set-sub-modal! (fn [_] (display))))
  359. (p/catch #(do (js/console.warn %)
  360. (notifications/show! "No README content." :warn))))
  361. ;; market
  362. (state/set-sub-modal! (fn [_] (display repo nil))))))
  363. (defn load-unpacked-plugin
  364. []
  365. (when util/electron?
  366. (p/let [path (ipc/ipc "openDialog")]
  367. (when-not (:plugin/selected-unpacked-pkg @state/state)
  368. (state/set-state! :plugin/selected-unpacked-pkg path)))))
  369. (defn reset-unpacked-state
  370. []
  371. (state/set-state! :plugin/selected-unpacked-pkg nil))
  372. (defn hook-plugin
  373. [tag type payload plugin-id]
  374. (when lsp-enabled?
  375. (try
  376. (js-invoke js/LSPluginCore
  377. (str "hook" (string/capitalize (name tag)))
  378. (name type)
  379. (if (coll? payload)
  380. (bean/->js (normalize-keyword-for-json payload))
  381. payload)
  382. (if (keyword? plugin-id) (name plugin-id) plugin-id))
  383. (catch js/Error e
  384. (js/console.error "[Hook Plugin Err]" e)))))
  385. (defn hook-plugin-app
  386. ([type payload] (hook-plugin-app type payload nil))
  387. ([type payload plugin-id] (hook-plugin :app type payload plugin-id)))
  388. (defn hook-plugin-editor
  389. ([type payload] (hook-plugin-editor type payload nil))
  390. ([type payload plugin-id] (hook-plugin :editor type payload plugin-id)))
  391. (defn hook-plugin-db
  392. ([type payload] (hook-plugin-db type payload nil))
  393. ([type payload plugin-id] (hook-plugin :db type payload plugin-id)))
  394. (defn hook-plugin-block-changes
  395. [{:keys [blocks tx-data tx-meta]}]
  396. (doseq [b blocks
  397. :let [tx-data' (group-by first tx-data)
  398. type (str "block:" (:block/uuid b))]]
  399. (hook-plugin-db type {:block b :tx-data (get tx-data' (:db/id b)) :tx-meta tx-meta})))
  400. (defn get-ls-dotdir-root
  401. []
  402. (ipc/ipc "getLogseqDotDirRoot"))
  403. (defn make-fn-to-load-dotdir-json
  404. [dirname default]
  405. (fn [key]
  406. (when-let [key (and key (name key))]
  407. (p/let [repo ""
  408. path (get-ls-dotdir-root)
  409. exist? (fs/file-exists? path dirname)
  410. _ (when-not exist? (fs/mkdir! (util/node-path.join path dirname)))
  411. path (util/node-path.join path dirname (str key ".json"))
  412. _ (fs/create-if-not-exists repo "" path (or default "{}"))
  413. json (fs/read-file "" path)]
  414. [path (js/JSON.parse json)]))))
  415. (defn make-fn-to-save-dotdir-json
  416. [dirname]
  417. (fn [key content]
  418. (when-let [key (and key (name key))]
  419. (p/let [repo ""
  420. path (get-ls-dotdir-root)
  421. path (util/node-path.join path dirname (str key ".json"))]
  422. (fs/write-file! repo "" path content {:skip-compare? true})))))
  423. (defn make-fn-to-unlink-dotdir-json
  424. [dirname]
  425. (fn [key]
  426. (when-let [key (and key (name key))]
  427. (p/let [repo ""
  428. path (get-ls-dotdir-root)
  429. path (util/node-path.join path dirname (str key ".json"))]
  430. (fs/unlink! repo path nil)))))
  431. (defn show-themes-modal!
  432. []
  433. (state/pub-event! [:modal/show-themes-modal]))
  434. (defn goto-plugins-dashboard!
  435. []
  436. (state/pub-event! [:go/plugins]))
  437. (defn- get-user-default-plugins
  438. []
  439. (p/catch
  440. (p/let [files ^js (ipc/ipc "getUserDefaultPlugins")
  441. files (js->clj files)]
  442. (map #(hash-map :url %) files))
  443. (fn [e]
  444. (js/console.error e))))
  445. (defn check-enabled-for-updates
  446. [theme?]
  447. (let [pending? (seq (:plugin/updates-pending @state/state))]
  448. (when-let [plugins (and (not pending?)
  449. ;; TODO: too many requests may be limited by Github api
  450. (seq (take 32 (state/get-enabled?-installed-plugins theme?))))]
  451. (state/set-state! :plugin/updates-pending
  452. (into {} (map (fn [v] [(keyword (:id v)) v]) plugins)))
  453. (state/pub-event! [:plugin/consume-updates]))))
  454. ;; components
  455. (rum/defc lsp-indicator < rum/reactive
  456. []
  457. (let [text (state/sub :plugin/indicator-text)]
  458. (when-not (= text "END")
  459. [:div.flex.align-items.justify-center.h-screen.w-full.preboot-loading
  460. [:span.flex.items-center.justify-center.w-60.flex-col
  461. [:small.scale-250.opacity-70.mb-10.animate-pulse (svg/logo false)]
  462. [:small.block.text-sm.relative.opacity-50 {:style {:right "-8px"}} text]]])))
  463. (defn init-plugins!
  464. [callback]
  465. (let [el (js/document.createElement "div")]
  466. (.appendChild js/document.body el)
  467. (rum/mount
  468. (lsp-indicator) el))
  469. (state/set-state! :plugin/indicator-text "LOADING")
  470. (p/then
  471. (p/let [root (get-ls-dotdir-root)
  472. _ (.setupPluginCore js/LSPlugin (bean/->js {:localUserConfigRoot root :dotConfigRoot root}))
  473. clear-commands! (fn [pid]
  474. ;; commands
  475. (unregister-plugin-slash-command pid)
  476. (invoke-exported-api "unregister_plugin_simple_command" pid)
  477. (invoke-exported-api "uninstall_plugin_hook" pid)
  478. (unregister-plugin-ui-items pid)
  479. (unregister-plugin-resources pid))
  480. _ (doto js/LSPluginCore
  481. (.on "registered"
  482. (fn [^js pl]
  483. (register-plugin
  484. (bean/->clj (.parse js/JSON (.stringify js/JSON pl))))))
  485. (.on "reloaded"
  486. (fn [^js pl]
  487. (register-plugin
  488. (bean/->clj (.parse js/JSON (.stringify js/JSON pl))))))
  489. (.on "unregistered" (fn [pid]
  490. (let [pid (keyword pid)]
  491. ;; effects
  492. (unregister-plugin-themes pid)
  493. ;; plugins
  494. (swap! state/state medley/dissoc-in [:plugin/installed-plugins pid])
  495. ;; commands
  496. (clear-commands! pid))))
  497. (.on "unlink-plugin" (fn [pid]
  498. (let [pid (keyword pid)]
  499. (ipc/ipc "uninstallMarketPlugin" (name pid)))))
  500. (.on "beforereload" (fn [^js pl]
  501. (let [pid (.-id pl)]
  502. (clear-commands! pid)
  503. (unregister-plugin-themes pid false))))
  504. (.on "disabled" (fn [pid]
  505. (clear-commands! pid)
  506. (unregister-plugin-themes pid)))
  507. (.on "theme-changed" (fn [^js themes]
  508. (swap! state/state assoc :plugin/installed-themes
  509. (vec (mapcat (fn [[pid vs]] (mapv #(assoc % :pid pid) (bean/->clj vs))) (bean/->clj themes))))))
  510. (.on "theme-selected" (fn [^js theme]
  511. (let [theme (bean/->clj theme)
  512. url (:url theme)
  513. mode (:mode theme)]
  514. (when mode
  515. (state/set-custom-theme! mode theme)
  516. (state/set-theme-mode! mode))
  517. (state/set-state! :plugin/selected-theme url))))
  518. (.on "reset-custom-theme" (fn [^js themes]
  519. (let [themes (bean/->clj themes)
  520. custom-theme (dissoc themes :mode)
  521. mode (:mode themes)]
  522. (state/set-custom-theme! {:light (if (nil? (:light custom-theme)) {:mode "light"} (:light custom-theme))
  523. :dark (if (nil? (:dark custom-theme)) {:mode "dark"} (:dark custom-theme))})
  524. (state/set-theme-mode! mode))))
  525. (.on "settings-changed" (fn [id ^js settings]
  526. (let [id (keyword id)]
  527. (when (and settings
  528. (contains? (:plugin/installed-plugins @state/state) id))
  529. (update-plugin-settings-state id (bean/->clj settings)))))))
  530. default-plugins (get-user-default-plugins)
  531. _ (.register js/LSPluginCore (bean/->js (if (seq default-plugins) default-plugins [])) true)])
  532. #(do
  533. (state/set-state! :plugin/indicator-text "END")
  534. (callback))))
  535. (defn setup!
  536. "setup plugin core handler"
  537. [callback]
  538. (if (not lsp-enabled?)
  539. (callback)
  540. (init-plugins! callback)))