Fix lazy loading

pull/645/head
Tienson Qin 2020-04-12 22:19:53 +08:00
parent f1e97f279e
commit 05a63cedad
8 changed files with 30 additions and 36 deletions

View File

@ -17,21 +17,27 @@
#{:org :md :markdown #{:org :md :markdown
:adoc :asciidoc}) :adoc :asciidoc})
(defn lazy-load-js
[state]
(let [format (keyword (second (:rum/args state)))
loader? (contains? render-formats format)]
(when loader?
(when-not (format/loaded? format)
(format/lazy-load format)))))
;; TODO: lazy load highlight.js ;; TODO: lazy load highlight.js
(rum/defcs html < rum/reactive (rum/defcs html < rum/reactive
{:init (fn [state props] {:will-mount (fn [state]
(let [format (keyword (second (:rum/args state))) (lazy-load-js state)
loader? (contains? render-formats format)] state)
(when loader?
(when-not (format/loaded? format)
(format/lazy-load format)))
state))
:did-mount (fn [state] :did-mount (fn [state]
(highlight!) (highlight!)
(handler/render-local-images!) (handler/render-local-images!)
state) state)
:did-update (fn [state] :did-update (fn [state]
(highlight!) (highlight!)
(handler/render-local-images!)
(lazy-load-js state)
state)} state)}
[state content format config] [state content format config]
(let [format (format/normalize format)] (let [format (format/normalize format)]

View File

@ -17,13 +17,13 @@
decoded-path (b64/decodeString encoded-path)] decoded-path (b64/decodeString encoded-path)]
[encoded-path decoded-path])) [encoded-path decoded-path]))
(rum/defcs file < (rum/defcs file
[state] [state]
(let [[encoded-path path] (get-path state) (let [[encoded-path path] (get-path state)
suffix (keyword (string/lower-case (last (string/split path #"\."))))] format (keyword (string/lower-case (last (string/split path #"\."))))]
(sidebar/sidebar (sidebar/sidebar
(cond (cond
(and suffix (contains? handler/text-formats suffix)) (and format (contains? handler/text-formats format))
[:div.content [:div.content
[:a {:href (str "/file/" encoded-path "/edit")} [:a {:href (str "/file/" encoded-path "/edit")}
"edit"] "edit"]
@ -33,17 +33,17 @@
[:span] [:span]
content content
(content/html content suffix org/default-config) (content/html content format org/default-config)
:else :else
"Loading ..."))] "Loading ..."))]
;; image type ;; image type
(and suffix (contains? #{:png :jpg :jpeg} suffix)) (and format (contains? #{:png :jpg :jpeg} format))
(content/html [:img {:src path}] suffix org/default-config) (content/html [:img {:src path}] format org/default-config)
:else :else
[:div "Format ." (name suffix) " is not supported."])))) [:div "Format ." (name format) " is not supported."]))))
(defn- count-newlines (defn- count-newlines
[s] [s]

View File

@ -48,9 +48,4 @@
(handler/set-format-js-loading! format true) (handler/set-format-js-loading! format true)
(protocol/lazyLoad record (protocol/lazyLoad record
(fn [result] (fn [result]
(handler/set-format-js-loading! format false)) (handler/set-format-js-loading! format false)))))))
(fn [error]
(prn format " js failed to load.")
(handler/set-format-js-loading! format false)
;; TODO: notification
(js/console.error error)))))))

View File

@ -23,8 +23,7 @@
(.convert (js/window.Asciidoctor) content (clj->js config))))) (.convert (js/window.Asciidoctor) content (clj->js config)))))
(loaded? [this] (loaded? [this]
(some? (loaded?))) (some? (loaded?)))
(lazyLoad [this ok-handler error-handler] (lazyLoad [this ok-handler]
(loader/load (loader/load
"https://cdnjs.cloudflare.com/ajax/libs/asciidoctor.js/1.5.9/asciidoctor.min.js" "https://cdnjs.cloudflare.com/ajax/libs/asciidoctor.js/1.5.9/asciidoctor.min.js"
ok-handler ok-handler)))
error-handler)))

View File

@ -18,8 +18,7 @@
(.makeHtml (js/window.showdown.Converter.) content))) (.makeHtml (js/window.showdown.Converter.) content)))
(loaded? [this] (loaded? [this]
(some? (loaded?))) (some? (loaded?)))
(lazyLoad [this ok-handler error-handler] (lazyLoad [this ok-handler]
(loader/load (loader/load
"https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.1/showdown.min.js" "https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.1/showdown.min.js"
ok-handler ok-handler)))
error-handler)))

View File

@ -27,11 +27,10 @@
(.parseHtml js/window.MldocOrg content config))) (.parseHtml js/window.MldocOrg content config)))
(loaded? [this] (loaded? [this]
(some? (loaded?))) (some? (loaded?)))
(lazyLoad [this ok-handler error-handler] (lazyLoad [this ok-handler]
(loader/load (loader/load
(config/asset-uri "/static/js/mldoc_org.min.js") (config/asset-uri "/static/js/mldoc_org.min.js")
ok-handler ok-handler)))
error-handler)))
(defn parse-json (defn parse-json
([content] ([content]

View File

@ -3,4 +3,4 @@
(defprotocol Format (defprotocol Format
(toHtml [this content config]) (toHtml [this content config])
(loaded? [this]) (loaded? [this])
(lazyLoad [this ok-handler error-handler])) (lazyLoad [this ok-handler]))

View File

@ -2,10 +2,6 @@
(:require [goog.net.jsloader :as jsloader] (:require [goog.net.jsloader :as jsloader]
[goog.html.legacyconversions :as conv])) [goog.html.legacyconversions :as conv]))
(defn load [url ok-handler error-handler] (defn load [url ok-handler]
(let [loader ^js (jsloader/safeLoad (conv/trustedResourceUrlFromString (str url)))] (let [loader ^js (jsloader/safeLoad (conv/trustedResourceUrlFromString (str url)))]
(.addCallback loader (.addCallback loader ok-handler)))
(fn [result]
(ok-handler result))
(fn [error]
(error-handler error)))))