page.cljs 4.6 KB

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