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
: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
(rum/defcs html < rum/reactive
{:init (fn [state props]
(let [format (keyword (second (:rum/args state)))
loader? (contains? render-formats format)]
(when loader?
(when-not (format/loaded? format)
(format/lazy-load format)))
state))
{:will-mount (fn [state]
(lazy-load-js state)
state)
:did-mount (fn [state]
(highlight!)
(handler/render-local-images!)
state)
:did-update (fn [state]
(highlight!)
(handler/render-local-images!)
(lazy-load-js state)
state)}
[state content format config]
(let [format (format/normalize format)]

View File

@ -17,13 +17,13 @@
decoded-path (b64/decodeString encoded-path)]
[encoded-path decoded-path]))
(rum/defcs file <
(rum/defcs file
[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
(cond
(and suffix (contains? handler/text-formats suffix))
(and format (contains? handler/text-formats format))
[:div.content
[:a {:href (str "/file/" encoded-path "/edit")}
"edit"]
@ -33,17 +33,17 @@
[:span]
content
(content/html content suffix org/default-config)
(content/html content format org/default-config)
:else
"Loading ..."))]
;; image type
(and suffix (contains? #{:png :jpg :jpeg} suffix))
(content/html [:img {:src path}] suffix org/default-config)
(and format (contains? #{:png :jpg :jpeg} format))
(content/html [:img {:src path}] format org/default-config)
:else
[:div "Format ." (name suffix) " is not supported."]))))
[:div "Format ." (name format) " is not supported."]))))
(defn- count-newlines
[s]

View File

@ -48,9 +48,4 @@
(handler/set-format-js-loading! format true)
(protocol/lazyLoad record
(fn [result]
(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)))))))
(handler/set-format-js-loading! format false)))))))

View File

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

View File

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

View File

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

View File

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

View File

@ -2,10 +2,6 @@
(:require [goog.net.jsloader :as jsloader]
[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)))]
(.addCallback loader
(fn [result]
(ok-handler result))
(fn [error]
(error-handler error)))))
(.addCallback loader ok-handler)))