Skip to content

Commit

Permalink
Delay scroll-to-target after recompute to new ns
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Aug 29, 2023
1 parent f092bf2 commit 4f38a3a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
28 changes: 14 additions & 14 deletions notebooks/doc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#_(reset! !ns-query "")

^{::clerk/sync true}
(defonce !active-ns (atom "nextjournal.clerk.viewer"))
(defonce !active-ns (atom "nextjournal.clerk"))
#_(reset! !active-ns "nextjournal.clerk.viewer")

(defn escape-pattern-str [s]
Expand Down Expand Up @@ -195,29 +195,29 @@
(match-nss @!active-ns)))]
:else [:div "No namespaces found."]))]]])

#_(deref nextjournal.clerk.webserver/!doc)
#_ (clerk/clear-cache!)

(defn resolve-internal-link [link]
(viewer/resolve-internal-link (cond->> link
(not (qualified-symbol? (symbol link)))
(and (not (qualified-symbol? (symbol link))) @!active-ns)
(str @!active-ns "/"))))

(defn spy [x] (println :x x :cns @!active-ns) x)

(def custom-markdown-viewers
[{:name :nextjournal.markdown/internal-link
:transform-fn (comp clerk/mark-presented
(fn [wv]
(when-some [{:keys [ns var]} (-> wv :nextjournal/value :text resolve-internal-link spy)]
{:var (symbol var)})))
:render-fn '(fn [{:keys [var]} _]
(when-some [info (-> wv :nextjournal/value :text resolve-internal-link)]
(-> info
(viewer/update-if :var symbol)
(viewer/update-if :ns ns-name)))))
:render-fn '(fn [{:keys [var ns]} _]
[:a {:href (str "#" var)
:on-click (fn [e] (.stopPropagation e) (.preventDefault e)
(js/console.log :click/var var
:elm (js/document.getElementById (name var)))
(when-some [el (js/document.getElementById (name var))]
(.scrollIntoView el)))} var])}])
(when (and var ns)
(let [scroll-to-target #(when-some [el (js/document.getElementById (name var))]
(.scrollIntoView el))]
(if (not= @!active-ns (str ns))
(do (reset! !active-ns (str ns))
(js/setTimeout scroll-to-target 500)) ;; TODO: smarter
(scroll-to-target)))))} (str var)])}])

(def custom-internal-links
(update viewer/markdown-viewer :add-viewers viewer/add-viewers custom-markdown-viewers))
Expand Down
9 changes: 6 additions & 3 deletions src/nextjournal/clerk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,16 @@
([viewer-opts x] (v/html viewer-opts x)))

(defn md
"Displays `x` with the markdown viewer.
"Displays `x` with the markdown viewer. Accepts strings or a structure as returned by [[nextjournal.markdown/parse]].
Supports an optional first `viewer-opts` map arg with the following optional keys:
* `:nextjournal.clerk/width`: set the width to `:full`, `:wide`, `:prose`
* `:nextjournal.clerk/viewers`: a seq of viewers to use for presentation of this value and its children
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`"
* `:nextjournal.clerk/render-opts`: a map argument that will be passed as a secong arg to the viewers `:render-fn`.
See also [[nextjournal.clerk.viewer/markdown-viewer]]."

([x] (v/md x))
([viewer-opts x] (v/md viewer-opts x)))

Expand Down Expand Up @@ -553,7 +556,7 @@
#_(with-cache (do (Thread/sleep 4200) 42))

(defmacro defcached
"Like `clojure.core/def` but with Clerk's caching of the value."
"Like `clojure.core/def` but with Clerk's caching of the value. See also [[with-cache]]."
[name expr]
`(let [result# (-> ~(v/->edn expr) eval/eval-string :blob->result first val :nextjournal/value)]
(def ~name result#)))
Expand Down
5 changes: 4 additions & 1 deletion src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@
%)
presented-result)))

(defn get-default-viewers []
(defn get-default-viewers
"Returns viewers from the global scope when set, defaults to [[default-viewers]] (see also [[!viewers]])."
[]
(:default @!viewers default-viewers))

(defn datafy-scope [scope]
Expand Down Expand Up @@ -982,6 +984,7 @@
{:name `vega-lite-viewer :render-fn 'nextjournal.clerk.render/render-vega-lite :transform-fn mark-presented})

(def markdown-viewer
"A clerk viewer for rendering markdown. See also [[nextjournal.clerk/md]]."
{:name `markdown-viewer
:add-viewers markdown-viewers
:transform-fn (fn [wrapped-value]
Expand Down

0 comments on commit 4f38a3a

Please sign in to comment.