Skip to content

Commit

Permalink
Add eval-notebook fn extension point in editor view
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Aug 2, 2023
1 parent 90ec95d commit a606cd2
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/nextjournal/clerk/render/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,27 @@
(update doc :blocks (partial map (fn [{:as cell :keys [type text var form]}]
(cond-> cell
(= :code type)
(assoc :result
(assoc :result
{:nextjournal/value (cond->> (eval form)
var (hash-map :nextjournal.clerk/var-from-def))}))))))

(defn eval-notebook [code]
(->> code
(parser/parse-clojure-string {:doc? true})
(analyze-doc)
(eval-blocks)
(v/with-viewer v/notebook-viewer)))
(new js/Promise
(fn [resolve reject]
(try
(resolve
(->> code
(parser/parse-clojure-string {:doc? true})
(analyze-doc)
(eval-blocks)
(v/with-viewer v/notebook-viewer)
v/present))
(catch js/Error error
(reject error))))))

(defonce bar-height 26)

(defn view [code-string]
(defn view [code-string {:as _opts :keys [eval-notebook-fn]}]
(let [!notebook (hooks/use-state nil)
!eval-result (hooks/use-state nil)
!container-el (hooks/use-ref nil)
Expand All @@ -170,9 +177,10 @@
!editor-panel (hooks/use-ref nil)
!notebook-panel (hooks/use-ref nil)
on-result #(reset! !eval-result %)
on-eval #(reset! !notebook (try
(eval-notebook (.. % -state -doc toString))
(catch js/Error error (v/html [render/error-view error]))))]
on-eval (fn [^js editor-view]
(.. ((or eval-notebook-fn eval-notebook) (.. editor-view -state -doc toString))
(then (fn [result] (reset! !notebook result)))
(catch (fn [error] (reset! !notebook (v/html [render/error-view error]))))))]
(hooks/use-effect
(fn []
(let [^js view
Expand Down Expand Up @@ -229,7 +237,7 @@
:style {:width "50vw" :height (str "calc(100vh - " (* bar-height 2) "px)")}}
(when-let [notebook @!notebook]
[:> render/ErrorBoundary {:hash (gensym)}
[render/inspect notebook]])]]
[render/inspect-presented notebook]])]]
[:div.absolute.left-0.bottom-0.w-screen.font-mono.text-white.border-t.dark:border-slate-600
[:div.bg-slate-900.dark:bg-slate-800.flex.px-4.font-mono.gap-4.items-center.text-white
{:class "text-[12px]" :style {:height bar-height}}
Expand Down

0 comments on commit a606cd2

Please sign in to comment.