|
@@ -1,16 +1,31 @@
|
|
|
(ns frontend.extensions.sci
|
|
|
+ "Provides a consistent approach to sci evaluation. Used in at least the following places:
|
|
|
+- For :view evaluation
|
|
|
+- For :result-transform evaluation
|
|
|
+- For cljs evaluation in Src blocks
|
|
|
+- For evaluating {{function }} under query tables"
|
|
|
(:require [sci.core :as sci]
|
|
|
[frontend.util :as util]
|
|
|
[goog.dom]
|
|
|
[goog.object]
|
|
|
[goog.string]))
|
|
|
|
|
|
-;; Some helpers
|
|
|
-(def sum (partial apply +))
|
|
|
+;; Helper fns for eval-string
|
|
|
+;; ==========================
|
|
|
+(def ^:private sum (partial apply +))
|
|
|
|
|
|
-(defn average [coll]
|
|
|
+(defn- average [coll]
|
|
|
(/ (reduce + coll) (count coll)))
|
|
|
|
|
|
+(defn- call-api
|
|
|
+ "Given a fn name from logseq.api, invokes it with the given arguments"
|
|
|
+ [fn-name & args]
|
|
|
+ (when-not (aget js/window.logseq "api" fn-name)
|
|
|
+ (throw (ex-info "Api function does not exist" {:fn fn-name})))
|
|
|
+ (apply js-invoke (aget js/window.logseq "api") fn-name args))
|
|
|
+
|
|
|
+;; Public fns
|
|
|
+;; ==========
|
|
|
(defn eval-string
|
|
|
"Second arg is a map of options for sci/eval-string"
|
|
|
([s]
|
|
@@ -23,7 +38,9 @@
|
|
|
'parseFloat js/parseFloat
|
|
|
'isNaN js/isNaN
|
|
|
'log js/console.log
|
|
|
- 'pprint util/pp-str}}
|
|
|
+ 'pprint util/pp-str
|
|
|
+ ;; Provide to all evals as it useful in most contexts
|
|
|
+ 'call-api call-api}}
|
|
|
options))
|
|
|
(catch :default e
|
|
|
(println "Query: sci eval failed:")
|
|
@@ -39,10 +56,7 @@
|
|
|
[:div
|
|
|
[:code "Results:"]
|
|
|
[:div.results.mt-1
|
|
|
- (let [result (eval-string code {:bindings {'block block}
|
|
|
- :classes {'logseq-api js/logseq.api
|
|
|
- 'logseq-gp js/logseq.graph_parser
|
|
|
- :allow :all}})]
|
|
|
+ (let [result (eval-string code {:bindings {'block block}})]
|
|
|
(if (and (vector? result) (:hiccup (meta result)))
|
|
|
result
|
|
|
[:pre.code (str result)]))]])
|