1
0

plugin.cljs 29 KB

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