diff --git a/web/src/main/frontend/components/content.cljs b/web/src/main/frontend/components/content.cljs index f3a0bb6ee..41e4acd1f 100644 --- a/web/src/main/frontend/components/content.cljs +++ b/web/src/main/frontend/components/content.cljs @@ -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)] diff --git a/web/src/main/frontend/components/file.cljs b/web/src/main/frontend/components/file.cljs index c6bebcea8..666bf74a4 100644 --- a/web/src/main/frontend/components/file.cljs +++ b/web/src/main/frontend/components/file.cljs @@ -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] diff --git a/web/src/main/frontend/format.cljs b/web/src/main/frontend/format.cljs index b1f0eaffb..b5cbb786f 100644 --- a/web/src/main/frontend/format.cljs +++ b/web/src/main/frontend/format.cljs @@ -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))))))) diff --git a/web/src/main/frontend/format/adoc.cljs b/web/src/main/frontend/format/adoc.cljs index 4a88b2a26..a5c44b590 100644 --- a/web/src/main/frontend/format/adoc.cljs +++ b/web/src/main/frontend/format/adoc.cljs @@ -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))) diff --git a/web/src/main/frontend/format/markdown.cljs b/web/src/main/frontend/format/markdown.cljs index 99c979597..a29b9f246 100644 --- a/web/src/main/frontend/format/markdown.cljs +++ b/web/src/main/frontend/format/markdown.cljs @@ -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))) diff --git a/web/src/main/frontend/format/org_mode.cljs b/web/src/main/frontend/format/org_mode.cljs index 9cfac8989..4c9468e20 100644 --- a/web/src/main/frontend/format/org_mode.cljs +++ b/web/src/main/frontend/format/org_mode.cljs @@ -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] diff --git a/web/src/main/frontend/format/protocol.cljs b/web/src/main/frontend/format/protocol.cljs index 98a00a71d..279086b1a 100644 --- a/web/src/main/frontend/format/protocol.cljs +++ b/web/src/main/frontend/format/protocol.cljs @@ -3,4 +3,4 @@ (defprotocol Format (toHtml [this content config]) (loaded? [this]) - (lazyLoad [this ok-handler error-handler])) + (lazyLoad [this ok-handler])) diff --git a/web/src/main/frontend/loader.cljs b/web/src/main/frontend/loader.cljs index 0b68338cb..6f331d5f0 100644 --- a/web/src/main/frontend/loader.cljs +++ b/web/src/main/frontend/loader.cljs @@ -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)))