mirror of
https://github.com/logseq/logseq.git
synced 2026-05-02 01:46:35 +00:00
Lazy loading
This commit is contained in:
36
web/src/main/frontend/extensions/code.cljs
Normal file
36
web/src/main/frontend/extensions/code.cljs
Normal file
@@ -0,0 +1,36 @@
|
||||
(ns frontend.extensions.code
|
||||
(:require [rum.core :as rum]
|
||||
[frontend.loader :as loader]
|
||||
[frontend.config :as config]))
|
||||
|
||||
;; TODO: extracted to a rum mixin
|
||||
(defn loaded? []
|
||||
js/window.katex)
|
||||
|
||||
(defonce *loading? (atom true))
|
||||
|
||||
(defn highlight!
|
||||
[state]
|
||||
(let [id (first (:rum/args state))]
|
||||
(when-let [element (js/document.getElementById id)]
|
||||
(js/hljs.highlightBlock element))))
|
||||
|
||||
(rum/defc highlight < rum/reactive
|
||||
{:did-mount (fn [state]
|
||||
(if (loaded?)
|
||||
(do
|
||||
(reset! *loading? false)
|
||||
(highlight! state))
|
||||
(do
|
||||
(reset! *loading? true)
|
||||
(loader/load
|
||||
(config/asset-uri "/static/js/highlight.min.js")
|
||||
(fn []
|
||||
(reset! *loading? false)
|
||||
(highlight! state)))))
|
||||
state)}
|
||||
[id attr code]
|
||||
(let [loading? (rum/react *loading?)]
|
||||
[:pre.pre-wrap-white-space.code
|
||||
[:code (assoc attr :id id)
|
||||
code]]))
|
||||
Reference in New Issue
Block a user