瀏覽代碼

Api fns can be called in prod builds with `call-api`

Follow up to
https://github.com/logseq/logseq/pull/7287#issuecomment-1321139222:
- Replace :classes approach with approach that works in dev and prod
  builds
- Remove graph-parser as it is unused
- Make api calling available for all contexts
Gabriel Horner 2 年之前
父節點
當前提交
584f3b068a
共有 2 個文件被更改,包括 23 次插入8 次删除
  1. 1 0
      .clj-kondo/config.edn
  2. 22 8
      src/main/frontend/extensions/sci.cljs

+ 1 - 0
.clj-kondo/config.edn

@@ -42,6 +42,7 @@
              frontend.db.query-react query-react
              frontend.diff diff
              frontend.encrypt encrypt
+             frontend.extensions.sci sci
              frontend.format.mldoc mldoc
              frontend.format.block block
              frontend.fs fs

+ 22 - 8
src/main/frontend/extensions/sci.cljs

@@ -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)]))]])