add and expose current block into sci namespace (editor/block)

add logseq-api and logseq-graph-parser into sci classes to make it avaliable in sci eval-string
add bindings custom-js to window object that can be defined in custom.js file as `customJs` object (for custom javascript function to be called from sci)
This commit is contained in:
Darwis
2022-11-10 11:01:08 +08:00
committed by Gabriel Horner
parent fd3bc86c73
commit 66f7656fd8
2 changed files with 15 additions and 9 deletions

View File

@@ -3252,9 +3252,9 @@
:else
[:<>
(lazy-editor/editor config (str (d/squuid)) attr code options)
(let [options (:options options)]
(let [options (:options options) block (:block config)]
(when (and (= language "clojure") (contains? (set options) ":results"))
(sci/eval-result code)))])])))))
(sci/eval-result code block)))])])))))
(defn ^:large-vars/cleanup-todo markup-element-cp
[{:keys [html-export?] :as config} item]

View File

@@ -9,27 +9,33 @@
(/ (reduce + coll) (count coll)))
(defn eval-string
[s]
([s]
(eval-string s {}))
([s ns]
(try
(sci/eval-string s {:bindings {'sum sum
'average average
'parseFloat js/parseFloat
'custom-js (if (exists? js/customJs) js/customJs (Empty.))
'isNaN js/isNaN
'log js/console.log
'pprint util/pp-str}})
'pprint util/pp-str}
:namespaces ns
:classes {'logseq-api js/logseq.api 'logseq-gp js/logseq.graph_parser :allow :all}
})
(catch :default e
(println "Query: sci eval failed:")
(js/console.error e))))
(js/console.error e)))))
(defn call-fn
[f & args]
(apply f args))
(defn eval-result
[code]
[code block]
[:div
[:code "Results:"]
[:div.results.mt-1
[:pre.code
(let [result (eval-string code)]
(str result))]]])
(let [editor-ns {'block (sci/new-var 'block block)}
result (eval-string code {'editor editor-ns})]
(if (and (vector? result) (keyword? (first result))) result [:pre.code (str result)]))]])