page.cljs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. (ns frontend.worker.handler.page.db-based.page
  2. "Page operations for DB graphs"
  3. (:require [logseq.db :as ldb]
  4. [logseq.graph-parser.block :as gp-block]
  5. [logseq.db.sqlite.util :as sqlite-util]
  6. [datascript.core :as d]
  7. [clojure.string :as string]
  8. [logseq.graph-parser.text :as text]
  9. [logseq.common.util :as common-util]
  10. [logseq.common.config :as common-config]
  11. [logseq.db.frontend.order :as db-order]
  12. [logseq.db.frontend.property.util :as db-property-util]
  13. [logseq.db.frontend.property.build :as db-property-build]
  14. [logseq.db.frontend.class :as db-class]))
  15. (defn- build-page-tx [conn properties page {:keys [whiteboard? class? tags]}]
  16. (when (:block/uuid page)
  17. (let [page (assoc page :block/type (cond class? "class"
  18. whiteboard? "whiteboard"
  19. (:block/type page) (:block/type page)
  20. :else "page"))
  21. page' (merge page
  22. (when tags {:block/tags (mapv #(hash-map :db/id
  23. (:db/id (d/entity @conn [:block/uuid %])))
  24. tags)}))
  25. property-vals-tx-m
  26. ;; Builds property values for built-in properties like logseq.property.pdf/file
  27. (db-property-build/build-property-values-tx-m
  28. page'
  29. (->> properties
  30. (keep (fn [[k v]]
  31. ;; TODO: Pass in property type in order to support property
  32. ;; types other than :default
  33. (when (db-property-util/built-in-has-ref-value? k)
  34. [k v])))
  35. (into {})))]
  36. (cond-> [(if class? (db-class/build-new-class @conn page') page')]
  37. (seq property-vals-tx-m)
  38. (into (vals property-vals-tx-m))
  39. true
  40. (conj (merge {:block/uuid (:block/uuid page)}
  41. properties
  42. (db-property-build/build-properties-with-ref-values property-vals-tx-m)))))))
  43. ;; TODO: Revisit title cleanup as this was copied from file implementation
  44. (defn get-title-and-pagename
  45. [title]
  46. (let [title (-> (string/trim title)
  47. (text/page-ref-un-brackets!)
  48. ;; remove `#` from tags
  49. (string/replace #"^#+" ""))
  50. title (common-util/remove-boundary-slashes title)
  51. page-name (common-util/page-name-sanity-lc title)]
  52. [title page-name]))
  53. (defn build-first-block-tx
  54. [page-uuid format]
  55. (let [page-id [:block/uuid page-uuid]]
  56. [(sqlite-util/block-with-timestamps
  57. {:block/uuid (ldb/new-block-id)
  58. :block/page page-id
  59. :block/parent page-id
  60. :block/order (db-order/gen-key nil nil)
  61. :block/title ""
  62. :block/format format})]))
  63. (defn create!
  64. [conn config title
  65. {:keys [create-first-block? properties uuid persist-op? whiteboard? class? today-journal?]
  66. :or {create-first-block? true
  67. properties nil
  68. uuid nil
  69. persist-op? true}
  70. :as options}]
  71. (let [date-formatter (common-config/get-date-formatter config)
  72. [title page-name] (get-title-and-pagename title)]
  73. (when-not (ldb/get-case-page @conn page-name)
  74. (let [format :markdown
  75. page (-> (gp-block/page-name->map title @conn true date-formatter
  76. {:class? class?
  77. :page-uuid (when (uuid? uuid) uuid)})
  78. (assoc :block/format format))
  79. page-uuid (:block/uuid page)
  80. page-txs (build-page-tx conn properties page (select-keys options [:whiteboard? :class? :tags]))
  81. first-block-tx (when (and
  82. (nil? (d/entity @conn [:block/uuid page-uuid]))
  83. create-first-block?
  84. (not (or whiteboard? class?))
  85. page-txs)
  86. (build-first-block-tx (:block/uuid (first page-txs)) format))
  87. txs (concat
  88. page-txs
  89. first-block-tx)]
  90. (when (seq txs)
  91. (ldb/transact! conn txs (cond-> {:persist-op? persist-op?
  92. :outliner-op :create-page}
  93. today-journal?
  94. (assoc :create-today-journal? true
  95. :today-journal-name page-name))))
  96. [page-name page-uuid]))))