mirror of
https://github.com/logseq/logseq.git
synced 2026-06-01 19:01:22 +00:00
184 lines
4.7 KiB
Clojure
184 lines
4.7 KiB
Clojure
(ns frontend.modules.outliner.op
|
|
"Build outliner ops"
|
|
(:require [datascript.impl.entity :as de]
|
|
[frontend.handler.user :as user-handler]))
|
|
|
|
(defn- current-user-delete-opts
|
|
[opts]
|
|
(cond-> (or opts {})
|
|
(and (nil? (:deleted-by-uuid opts))
|
|
(user-handler/user-uuid))
|
|
(assoc :deleted-by-uuid (uuid (user-handler/user-uuid)))))
|
|
|
|
(def ^:private ^:dynamic *outliner-ops*
|
|
"Stores outliner ops that are generated by the following calls"
|
|
nil)
|
|
|
|
(defn- op-transact!
|
|
[result]
|
|
(when (nil? *outliner-ops*)
|
|
(throw (js/Error. (str (name (first result)) " not used in (transact! ...)"))))
|
|
(conj! *outliner-ops* result)
|
|
result)
|
|
|
|
(defn- ->block-id
|
|
[block-or-id]
|
|
(cond
|
|
(de/entity? block-or-id)
|
|
(:block/uuid block-or-id)
|
|
|
|
(map? block-or-id)
|
|
(:block/uuid block-or-id)
|
|
|
|
:else
|
|
block-or-id))
|
|
|
|
(defn save-block!
|
|
[block & {:as opts}]
|
|
(op-transact!
|
|
(when-let [block' (if (de/entity? block)
|
|
(dissoc (.-kv ^js block) :db/id)
|
|
block)]
|
|
[:save-block [block' opts]])))
|
|
|
|
(defn insert-blocks!
|
|
[blocks target-block opts]
|
|
(op-transact!
|
|
(let [id (->block-id target-block)]
|
|
[:insert-blocks [blocks id opts]])))
|
|
|
|
(defn apply-template!
|
|
[template-id target-block opts]
|
|
(op-transact!
|
|
(let [template-id' (->block-id template-id)
|
|
id (->block-id target-block)]
|
|
[:apply-template [template-id' id opts]])))
|
|
|
|
(defn delete-blocks!
|
|
[blocks opts]
|
|
(op-transact!
|
|
(let [ids (map ->block-id blocks)]
|
|
(when (seq ids)
|
|
[:delete-blocks [ids (current-user-delete-opts opts)]]))))
|
|
|
|
(defn move-blocks!
|
|
[blocks target-block opts]
|
|
(op-transact!
|
|
(let [ids (map ->block-id blocks)
|
|
target-id (->block-id target-block)]
|
|
[:move-blocks [ids target-id opts]])))
|
|
|
|
(defn move-blocks-up-down!
|
|
[blocks up?]
|
|
(op-transact!
|
|
(let [ids (map ->block-id blocks)]
|
|
[:move-blocks-up-down [ids up?]])))
|
|
|
|
(defn indent-outdent-blocks!
|
|
[blocks indent? & {:as opts}]
|
|
(op-transact!
|
|
(let [ids (map ->block-id blocks)]
|
|
[:indent-outdent-blocks [ids indent? opts]])))
|
|
|
|
(defn upsert-property!
|
|
[property-id schema property-opts]
|
|
(op-transact!
|
|
[:upsert-property [property-id schema property-opts]]))
|
|
|
|
(defn set-block-property!
|
|
[block-eid property-id value]
|
|
(op-transact!
|
|
[:set-block-property [(->block-id block-eid) property-id value]]))
|
|
|
|
(defn remove-block-property!
|
|
[block-eid property-id]
|
|
(op-transact!
|
|
[:remove-block-property [(->block-id block-eid) property-id]]))
|
|
|
|
(defn delete-property-value!
|
|
[block-eid property-id property-value]
|
|
(op-transact!
|
|
[:delete-property-value [(->block-id block-eid) property-id property-value]]))
|
|
|
|
(defn batch-delete-property-value!
|
|
[block-eids property-id property-value]
|
|
(op-transact!
|
|
[:batch-delete-property-value [(mapv ->block-id block-eids) property-id property-value]]))
|
|
|
|
(defn create-property-text-block!
|
|
[block-id property-id value opts]
|
|
(op-transact!
|
|
[:create-property-text-block [(->block-id block-id) property-id value opts]]))
|
|
|
|
(defn batch-set-property!
|
|
[block-ids property-id value opts]
|
|
(op-transact!
|
|
[:batch-set-property [(mapv ->block-id block-ids) property-id value opts]]))
|
|
|
|
(defn batch-remove-property!
|
|
[block-ids property-id]
|
|
(op-transact!
|
|
[:batch-remove-property [(mapv ->block-id block-ids) property-id]]))
|
|
|
|
(defn class-add-property!
|
|
[class-id property-id]
|
|
(op-transact!
|
|
[:class-add-property [(->block-id class-id) property-id]]))
|
|
|
|
(defn class-remove-property!
|
|
[class-id property-id]
|
|
(op-transact!
|
|
[:class-remove-property [(->block-id class-id) property-id]]))
|
|
|
|
(defn upsert-closed-value!
|
|
[property-id closed-value-config]
|
|
(op-transact!
|
|
[:upsert-closed-value [property-id closed-value-config]]))
|
|
|
|
(defn delete-closed-value!
|
|
[property-id value-block-id]
|
|
(op-transact!
|
|
[:delete-closed-value [property-id (->block-id value-block-id)]]))
|
|
|
|
(defn add-existing-values-to-closed-values!
|
|
[property-id values]
|
|
(op-transact!
|
|
[:add-existing-values-to-closed-values [property-id values]]))
|
|
|
|
(defn toggle-reaction!
|
|
[target-uuid emoji-id user-uuid]
|
|
(op-transact!
|
|
[:toggle-reaction [target-uuid emoji-id user-uuid]]))
|
|
|
|
(defn batch-import-edn!
|
|
[import-edn options]
|
|
(op-transact!
|
|
[:batch-import-edn [import-edn options]]))
|
|
|
|
(defn transact!
|
|
[tx-data tx-meta]
|
|
(op-transact!
|
|
[:transact [tx-data tx-meta]]))
|
|
|
|
(defn create-page!
|
|
[title options]
|
|
(op-transact!
|
|
[:create-page [title options]]))
|
|
|
|
(defn rename-page!
|
|
[page-uuid new-name]
|
|
(op-transact!
|
|
[:rename-page [page-uuid new-name]]))
|
|
|
|
(defn delete-page!
|
|
([page-uuid]
|
|
(delete-page! page-uuid {}))
|
|
([page-uuid opts]
|
|
(op-transact!
|
|
[:delete-page [page-uuid (current-user-delete-opts opts)]])))
|
|
|
|
(defn recycle-delete-permanently!
|
|
[root-uuid]
|
|
(op-transact!
|
|
[:recycle-delete-permanently [root-uuid]]))
|