Skip to content

Commit

Permalink
emmy viewers: ratios
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuebert committed Aug 3, 2023
1 parent b0946e6 commit 153b036
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion editor2/shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
:ext.reagent {:entries [maria.editor.extensions.reagent]
:depends-on #{:core}}
:ext.emmy {:entries [maria.editor.extensions.emmy]
:depends-on #{:core :ext.reagent}}}}}}
:depends-on #{:core :ext.reagent :ext.katex}}}}}}
17 changes: 13 additions & 4 deletions editor2/src/main/maria/editor/extensions/emmy.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns maria.editor.extensions.emmy
(:require ["fraction.js/bigfraction.js$default" :as Fraction]
(:require [emmy.ratio]
[emmy.viewer.sci]
[emmy.value]
[emmy.expression]
Expand All @@ -14,7 +14,8 @@
[maria.editor.extensions.reagent :as ext.reagent]
[maria.editor.code.show-values :as show :refer [show]]
[sci.ctx-store :refer [get-ctx]]
[yawn.view :as v]))
[yawn.view :as v]
#_[maria.editor.extensions.katex :as katex]))

(defn show-frozen [opts x]
(show opts (emmy.value/freeze x)))
Expand All @@ -25,6 +26,14 @@
(defn show-number-string [opts x]
(v/x [:span.text-number (str x)]))

(defn show-ratio [opts x]
(let [[_ n d] (emmy.value/freeze x)]
#_(katex/show-katex :span (str "\\dfrac{" n "}{" d "}"))
(v/x [:<>
[:span.text-number n]
[:span.text-brackets "/"]
[:span.text-number d]])))

(def viewers-by-type
{emmy.expression/Literal show-expression-of
emmy.operator/Operator show-frozen
Expand All @@ -37,8 +46,8 @@
js/BigInt show-number-string})

(defn show-emmy [opts x]
(if (instance? Fraction x)
(show-number-string opts x)
(if (emmy.ratio/ratio? x)
(show-ratio opts x)
(if-let [viewer (viewers-by-type (type x))]
(viewer opts x)
(when-let [m (meta x)]
Expand Down
17 changes: 11 additions & 6 deletions editor2/src/main/maria/editor/extensions/katex.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
[yawn.hooks :as h]
[yawn.view :as v]))

;; TODO - load this css
;; {:href "https://cdn.jsdelivr.net/npm/katex@0.16.2/dist/katex.min.css"}
(defonce !load-css! (delay
(let [link (.createElement js/document "link")]
(set! (.-rel link) "stylesheet")
(set! (.-href link) "https://cdn.jsdelivr.net/npm/katex@0.16.2/dist/katex.min.css")
(js/document.head.appendChild link))))

(v/defview show-katex [el x]
[el {:ref (h/use-callback
(fn [el]
(when el
(katex/render x el))))}])
@!load-css!
[el {:ref (h/use-callback
(fn [el]
(when el
(katex/render x el))))}])

0 comments on commit 153b036

Please # to comment.