Tienson Qin 1 éve
szülő
commit
c3aabab338
1 módosított fájl, 104 hozzáadás és 105 törlés
  1. 104 105
      src/main/logseq/api.cljs

+ 104 - 105
src/main/logseq/api.cljs

@@ -54,8 +54,7 @@
             [frontend.handler.search :as search-handler]
             [logseq.api.block :as api-block]
             [logseq.db :as ldb]
-            [logseq.db.frontend.property.util :as db-property-util]
-            [logseq.db.frontend.property :as db-property]))
+            [logseq.db.frontend.property.util :as db-property-util]))
 
 ;; Alert: this namespace shouldn't invoke any reactive queries
 
@@ -74,7 +73,7 @@
 (defn- db-graph?
   []
   (some-> (state/get-current-repo)
-    (config/db-based-graph?)))
+          (config/db-based-graph?)))
 
 (defn get-caller-plugin-id
   [] (gobj/get js/window "$$callerPluginID"))
@@ -83,16 +82,16 @@
   [k]
   (if (string? k)
     (-> k (string/trim)
-      (string/replace #"^\:+" "")
-      (string/lower-case))
+        (string/replace #"^\:+" "")
+        (string/lower-case))
     k))
 
 (defn- encode-user-property-name
   [k]
-  (if (string? k)
+  (when (string? k)
     (-> k (string/trim)
-      (string/replace "/" "")
-      (string/replace " " ""))))
+        (string/replace "/" "")
+        (string/replace " " ""))))
 
 ;; helpers
 (defn ^:export install-plugin-hook
@@ -134,25 +133,25 @@
   ;; get app base info
   []
   (bean/->js
-    (sdk-utils/normalize-keyword-for-json
-      {:version fv/version})))
+   (sdk-utils/normalize-keyword-for-json
+    {:version fv/version})))
 
 (def ^:export get_user_configs
   (fn []
     (bean/->js
-      (sdk-utils/normalize-keyword-for-json
-        {:preferred-language      (:preferred-language @state/state)
-         :preferred-theme-mode    (:ui/theme @state/state)
-         :preferred-format        (state/get-preferred-format)
-         :preferred-workflow      (state/get-preferred-workflow)
-         :preferred-todo          (state/get-preferred-todo)
-         :preferred-date-format   (state/get-date-formatter)
-         :preferred-start-of-week (state/get-start-of-week)
-         :current-graph           (state/get-current-repo)
-         :show-brackets           (state/show-brackets?)
-         :enabled-journals        (state/enable-journals?)
-         :enabled-flashcards      (state/enable-flashcards?)
-         :me                      (state/get-me)}))))
+     (sdk-utils/normalize-keyword-for-json
+      {:preferred-language      (:preferred-language @state/state)
+       :preferred-theme-mode    (:ui/theme @state/state)
+       :preferred-format        (state/get-preferred-format)
+       :preferred-workflow      (state/get-preferred-workflow)
+       :preferred-todo          (state/get-preferred-todo)
+       :preferred-date-format   (state/get-date-formatter)
+       :preferred-start-of-week (state/get-start-of-week)
+       :current-graph           (state/get-current-repo)
+       :show-brackets           (state/show-brackets?)
+       :enabled-journals        (state/enable-journals?)
+       :enabled-flashcards      (state/enable-flashcards?)
+       :me                      (state/get-me)}))))
 
 (def ^:export get_current_graph_configs
   (fn [& keys]
@@ -171,19 +170,19 @@
   (fn []
     (if (db-graph?)
       (-> (page-handler/get-favorites)
-        (p/then #(-> % (sdk-utils/normalize-keyword-for-json) (bean/->js))))
+          (p/then #(-> % (sdk-utils/normalize-keyword-for-json) (bean/->js))))
       (some->> (:favorites (state/get-config))
-        (remove string/blank?)
-        (filter string?)
-        (bean/->js)))))
+               (remove string/blank?)
+               (filter string?)
+               (bean/->js)))))
 
 (def ^:export get_current_graph_recent
   (fn []
     (some->> (recent-handler/get-recent-pages)
-      (map #(db-utils/entity (:db/id %)))
-      (remove nil?)
-      (sdk-utils/normalize-keyword-for-json)
-      (bean/->js))))
+             (map #(db-utils/entity (:db/id %)))
+             (remove nil?)
+             (sdk-utils/normalize-keyword-for-json)
+             (bean/->js))))
 
 (def ^:export get_current_graph_templates
   (fn []
@@ -335,8 +334,8 @@
                         (plugin-handler/get-ls-dotdir-root))
             plugin-id (util/node-path.basename plugin-id)
             exist?    (fs/file-exists?
-                        (util/node-path.join root "storages" plugin-id)
-                        file)]
+                       (util/node-path.join root "storages" plugin-id)
+                       file)]
       exist?)))
 
 (def ^:export clear_plugin_storage_files
@@ -357,8 +356,8 @@
             ^js files  (ipc/ipc :listdir files-path)]
       (when (js-iterable? files)
         (bean/->js
-          (map #(some-> (string/replace-first % files-path "")
-                        (string/replace #"^/+" "")) files))))))
+         (map #(some-> (string/replace-first % files-path "")
+                       (string/replace #"^/+" "")) files))))))
 
 (def ^:export load_user_preferences
   (fn []
@@ -394,8 +393,8 @@
   (fn [pid ^js cmd-actions]
     (when-let [[cmd actions] (bean/->clj cmd-actions)]
       (plugin-handler/register-plugin-slash-command
-        pid [cmd (mapv #(into [(keyword (first %))]
-                              (rest %)) actions)]))))
+       pid [cmd (mapv #(into [(keyword (first %))]
+                             (rest %)) actions)]))))
 
 (def ^:export register_plugin_simple_command
   (fn [pid ^js cmd-action palette?]
@@ -460,7 +459,7 @@
   (fn [pid type ^js opts]
     (when-let [opts (bean/->clj opts)]
       (plugin-handler/register-plugin-ui-item
-        pid (assoc opts :type type)))))
+       pid (assoc opts :type type)))))
 
 ;; app
 (def ^:export relaunch
@@ -581,9 +580,9 @@
     (when-let [blocks (state/selection?)]
       (let [blocks (->> blocks
                         (map (fn [^js el] (some->
-                                            (.getAttribute el "blockid")
-                                            (db-model/query-block-by-uuid)
-                                            (api-block/into-properties)))))]
+                                           (.getAttribute el "blockid")
+                                           (db-model/query-block-by-uuid)
+                                           (api-block/into-properties)))))]
         (bean/->js (sdk-utils/normalize-keyword-for-json blocks))))))
 
 (def ^:export clear_selected_blocks
@@ -595,7 +594,7 @@
     (when-let [page (state/get-current-page)]
       (p/let [page (<pull-block page)]
         (when-let [page (and (:block/name page)
-                          (some->> page (api-block/into-properties (state/get-current-repo))))]
+                             (some->> page (api-block/into-properties (state/get-current-repo))))]
           (bean/->js (sdk-utils/normalize-keyword-for-json page)))))))
 
 (def ^:export get_page
@@ -609,21 +608,21 @@
                                    :else
                                    [:block/name (util/page-name-sanity-lc id-or-page-name)]))]
       (when-let [page (and (:block/name page)
-                        (some->> page (api-block/into-properties (state/get-current-repo))))]
+                           (some->> page (api-block/into-properties (state/get-current-repo))))]
         (bean/->js (sdk-utils/normalize-keyword-for-json page))))))
 
 (def ^:export get_all_pages
   (fn []
     (let [db (conn/get-db (state/get-current-repo))]
       (some->
-        (->>
-          (d/datoms db :avet :block/name)
-          (map #(db-utils/pull (:e %)))
-          (remove ldb/hidden?)
-          (remove (fn [page]
-                    (common-util/uuid-string? (:block/name page)))))
-        (sdk-utils/normalize-keyword-for-json)
-        (bean/->js)))))
+       (->>
+        (d/datoms db :avet :block/name)
+        (map #(db-utils/pull (:e %)))
+        (remove ldb/hidden?)
+        (remove (fn [page]
+                  (common-util/uuid-string? (:block/name page)))))
+       (sdk-utils/normalize-keyword-for-json)
+       (bean/->js)))))
 
 (def ^:export create_page
   (fn [name ^js properties ^js opts]
@@ -633,15 +632,15 @@
       (p/let [page (<pull-block name)
               new-page (when-not page
                          (page-handler/<create!
-                           name
-                           (cond->
-                             {:redirect? (if (boolean? redirect) redirect true)
-                              :journal? journal
-                              :create-first-block? (if (boolean? createFirstBlock) createFirstBlock true)
-                              :format format}
-
-                             (not db-base?)
-                             (assoc :properties properties))))
+                          name
+                          (cond->
+                           {:redirect? (if (boolean? redirect) redirect true)
+                            :journal? journal
+                            :create-first-block? (if (boolean? createFirstBlock) createFirstBlock true)
+                            :format format}
+
+                            (not db-base?)
+                            (assoc :properties properties))))
               _ (when (and db-base? (seq properties))
                   (api-block/save-db-based-block-properties! new-page properties))]
         (some-> (or page new-page)
@@ -660,9 +659,9 @@
 (defn ^:export open_in_right_sidebar
   [block-id-or-uuid]
   (editor-handler/open-block-in-sidebar!
-    (if (number? block-id-or-uuid)
-      block-id-or-uuid
-      (sdk-utils/uuid-or-throw-error block-id-or-uuid))))
+   (if (number? block-id-or-uuid)
+     block-id-or-uuid
+     (sdk-utils/uuid-or-throw-error block-id-or-uuid))))
 
 (defn ^:export new_block_uuid []
   (str (db/new-block-id)))
@@ -757,10 +756,10 @@
                 block (if before
                         (db/pull (:db/id (ldb/get-left-sibling (db/entity (:db/id block))))) block)]
             (some-> (editor-handler/insert-block-tree-after-target
-                      (:db/id block) sibling bb (:block/format block) keep-uuid?)
-              (p/then (fn [results]
-                        (some-> results (ldb/read-transit-str)
-                          :blocks (sdk-utils/normalize-keyword-for-json) (bean/->js)))))))))))
+                     (:db/id block) sibling bb (:block/format block) keep-uuid?)
+                    (p/then (fn [results]
+                              (some-> results (ldb/read-transit-str)
+                                      :blocks (sdk-utils/normalize-keyword-for-json) (bean/->js)))))))))))
 
 (def ^:export remove_block
   (fn [block-uuid ^js _opts]
@@ -777,29 +776,29 @@
             opts (bean/->clj opts)]
       (when block
         (p/do!
-          (when (and db-base? (some? (:properties opts)))
-            (api-block/save-db-based-block-properties! block (:properties opts)))
-          (editor-handler/save-block! repo
-            (sdk-utils/uuid-or-throw-error block-uuid) content
-            (if db-base? (dissoc opts :properties) opts)))))))
+         (when (and db-base? (some? (:properties opts)))
+           (api-block/save-db-based-block-properties! block (:properties opts)))
+         (editor-handler/save-block! repo
+                                     (sdk-utils/uuid-or-throw-error block-uuid) content
+                                     (if db-base? (dissoc opts :properties) opts)))))))
 
 (def ^:export move_block
   (fn [src-block-uuid target-block-uuid ^js opts]
     (p/let [_ (<pull-block src-block-uuid)
             _ (<pull-block target-block-uuid)]
       (let [{:keys [before children]} (bean/->clj opts)
-           move-to      (cond
-                          (boolean before)
-                          :top
+            move-to      (cond
+                           (boolean before)
+                           :top
 
-                          (boolean children)
-                          :nested
+                           (boolean children)
+                           :nested
 
-                          :else
-                          nil)
-           src-block    (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error src-block-uuid))
-           target-block (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error target-block-uuid))]
-       (editor-dnd-handler/move-blocks nil [src-block] target-block nil move-to)))))
+                           :else
+                           nil)
+            src-block    (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error src-block-uuid))
+            target-block (db-model/query-block-by-uuid (sdk-utils/uuid-or-throw-error target-block-uuid))]
+        (editor-dnd-handler/move-blocks nil [src-block] target-block nil move-to)))))
 
 (def ^:export get_block
   (fn [id ^js opts]
@@ -854,7 +853,7 @@
 (defn convert?to-built-in-property-name
   [property-name]
   (if (and (not (qualified-keyword? property-name))
-        (contains? #{:background-color} property-name))
+           (contains? #{:background-color} property-name))
     (keyword :logseq.property property-name)
     property-name))
 
@@ -893,19 +892,19 @@
     (p/let [opts (or (some-> opts (bean/->clj)) {})
             name (or (:name opts) (some-> (str k) (string/trim)))
             k (if (qualified-keyword? k') k'
-                (get-db-ident-for-property-name k))
+                  (get-db-ident-for-property-name k))
             schema (or (some-> schema (bean/->clj)
-                         (update-keys #(if (contains? #{:hide :public} %)
-                                         (keyword (str (name %) "?")) %))) {})
+                               (update-keys #(if (contains? #{:hide :public} %)
+                                               (keyword (str (name %) "?")) %))) {})
             schema (cond-> schema
                      (string? (:cardinality schema))
                      (update :cardinality keyword)
                      (string? (:type schema))
                      (update :type keyword))
             p (db-property-handler/upsert-property! k schema
-                (cond-> opts
-                  name
-                  (assoc :property-name name)))]
+                                                    (cond-> opts
+                                                      name
+                                                      (assoc :property-name name)))]
       (bean/->js (sdk-utils/normalize-keyword-for-json p)))))
 
 ;; block properties
@@ -931,8 +930,8 @@
             key (if key-ns? key (-> (if (keyword? key) (name key) key) (util/safe-lower-case)))
             key (if (and db? (not key-ns?)) (get-db-ident-for-property-name key) key)]
       (property-handler/remove-block-property!
-      (state/get-current-repo)
-      block-uuid key))))
+       (state/get-current-repo)
+       block-uuid key))))
 
 (def ^:export get_block_property
   (fn [block-uuid key]
@@ -943,8 +942,8 @@
           (let [key (sanitize-user-property-name key)
                 property-name (-> (if (keyword? key) (name key) key) (util/safe-lower-case))
                 property-value (or (get properties key)
-                                 (get properties (keyword property-name))
-                                 (get properties (get-db-ident-for-property-name property-name)))
+                                   (get properties (keyword property-name))
+                                   (get properties (get-db-ident-for-property-name property-name)))
                 property-value (if-let [property-id (:db/id property-value)]
                                  (db/pull property-id) property-value)
                 ret (sdk-utils/normalize-keyword-for-json property-value)]
@@ -1020,12 +1019,12 @@
                                                                        :format              (state/get-preferred-format)}))]
     (when-let [block (db-model/get-page uuid-or-page-name)]
       (-> (api-block/sync-children-blocks! block)
-        (p/then (fn []
-                  (let [block' (first-child-of-block block)
-                        opts (bean/->clj opts)
-                        [block opts] (if block' [block' (assoc opts :before true :sibling true)] [block opts])
-                        target (str (:block/uuid block))]
-                    (insert_block target content (bean/->js opts)))))))))
+          (p/then (fn []
+                    (let [block' (first-child-of-block block)
+                          opts (bean/->clj opts)
+                          [block opts] (if block' [block' (assoc opts :before true :sibling true)] [block opts])
+                          target (str (:block/uuid block))]
+                      (insert_block target content (bean/->js opts)))))))))
 
 (defn ^:export append_block_in_page
   [uuid-or-page-name content ^js opts]
@@ -1076,7 +1075,7 @@
                                       :else %)
                                    inputs)
               result          (apply db-async/<q repo {:transact-db? false}
-                                (cons query resolved-inputs))]
+                                     (cons query resolved-inputs))]
         (bean/->js (sdk-utils/normalize-keyword-for-json result false))))))
 
 (defn ^:export custom_query
@@ -1121,10 +1120,10 @@
                 (plugin-handler/register-plugin-resources pid :scripts {:key s :src s})
                 (upt-status :pending)
                 (-> (loader/load s nil {:attributes {:data-ref (name pid)}})
-                  (p/then (fn [] (upt-status :done)))
-                  (p/catch (fn [] (upt-status :error))))))
-      (vec)
-      (p/all))))
+                    (p/then (fn [] (upt-status :done)))
+                    (p/catch (fn [] (upt-status :error))))))
+            (vec)
+            (p/all))))
 
 ;; http request
 (defonce *request-k (volatile! 0))
@@ -1156,7 +1155,7 @@
   (p/let [exists? (page-handler/<template-exists? template-name)]
     (when exists?
       (when-let [target (db-model/get-block-by-uuid target-uuid)]
-       (editor-handler/insert-template! nil template-name {:target target}) nil))))
+        (editor-handler/insert-template! nil template-name {:target target}) nil))))
 
 (defn ^:export exist_template
   [name]