editor.cljs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. (ns frontend.handler.db-based.editor
  2. "DB-based graph implementation"
  3. (:require [clojure.string :as string]
  4. [frontend.config :as config]
  5. [frontend.commands :as commands]
  6. [frontend.db :as db]
  7. [frontend.format.block :as block]
  8. [frontend.format.mldoc :as mldoc]
  9. [frontend.util :as util]
  10. [frontend.state :as state]
  11. [logseq.common.util.page-ref :as page-ref]
  12. [frontend.handler.ui :as ui-handler]
  13. [frontend.handler.common.config-edn :as config-edn-common-handler]
  14. [frontend.handler.property :as property-handler]
  15. [frontend.handler.repo-config :as repo-config-handler]
  16. [frontend.modules.outliner.ui :as ui-outliner-tx]
  17. [frontend.modules.outliner.op :as outliner-op]
  18. [frontend.schema.handler.repo-config :as repo-config-schema]
  19. [promesa.core :as p]
  20. [logseq.db.frontend.content :as db-content]))
  21. (defn- remove-non-existed-refs!
  22. [refs]
  23. (remove (fn [x] (or
  24. (and (vector? x)
  25. (= :block/uuid (first x))
  26. (nil? (db/entity x)))
  27. (nil? x))) refs))
  28. (defn- replace-tag-ref
  29. [content page-name id]
  30. (let [id' (str db-content/page-ref-special-chars id)
  31. [page wrapped-id] (if (string/includes? page-name " ")
  32. (map page-ref/->page-ref [page-name id'])
  33. [page-name id'])
  34. page-name (util/format "#%s" page)
  35. r (util/format "#%s" wrapped-id)]
  36. ;; hash tag parsing rules https://github.com/logseq/mldoc/blob/701243eaf9b4157348f235670718f6ad19ebe7f8/test/test_markdown.ml#L631
  37. ;; Safari doesn't support look behind, don't use
  38. ;; TODO: parse via mldoc
  39. (string/replace content
  40. (re-pattern (str "(?i)(^|\\s)(" (util/escape-regex-chars page-name) ")(?=[,\\.]*($|\\s))"))
  41. ;; case_insense^ ^lhs ^_grp2 look_ahead^ ^_grp3
  42. (fn [[_match lhs _grp2 _grp3]]
  43. (str lhs r)))))
  44. (defn- replace-page-ref
  45. [content page-name id]
  46. (let [id' (str db-content/page-ref-special-chars id)
  47. [page wrapped-id] (map page-ref/->page-ref [page-name id'])]
  48. (util/replace-ignore-case content page wrapped-id)))
  49. (defn- replace-page-ref-with-id
  50. [content page-name id]
  51. (-> content
  52. (replace-page-ref page-name id)
  53. (replace-tag-ref page-name id)))
  54. (defn- replace-page-refs-with-ids
  55. [block]
  56. (let [content (:block/content block)
  57. content' (if (some :block/name (:block/refs block))
  58. (reduce
  59. (fn [content {:block/keys [original-name uuid]}]
  60. (replace-page-ref-with-id content original-name uuid))
  61. content
  62. (filter :block/name (:block/refs block)))
  63. content)]
  64. (assoc block :block/content content')))
  65. (defn wrap-parse-block
  66. [{:block/keys [content left level] :as block}]
  67. (let [block (or (and (:db/id block) (db/pull (:db/id block))) block)
  68. block (if (nil? content)
  69. block
  70. (let [ast (mldoc/->edn (string/trim content) :markdown)
  71. first-elem-type (first (ffirst ast))
  72. block-with-title? (mldoc/block-with-title? first-elem-type)
  73. content' (str (config/get-block-pattern :markdown) (if block-with-title? " " "\n") content)
  74. block' (merge block (block/parse-block (assoc block :block/content content')))
  75. block' (if (seq (:block/properties block))
  76. (update block' :block/properties (fn [m] (merge m (:block/properties block))))
  77. block')]
  78. (update block' :block/refs remove-non-existed-refs!)))
  79. block (if (and left (not= (:block/left block) left)) (assoc block :block/left left) block)
  80. result (-> block
  81. (merge (if level {:block/level level} {}))
  82. (replace-page-refs-with-ids))]
  83. (-> result
  84. ;; Remove tags from content
  85. (assoc :block/content
  86. (db-content/content-without-tags
  87. (:block/content result)
  88. (->>
  89. (map
  90. (fn [tag]
  91. (when (:block/uuid tag)
  92. (str db-content/page-ref-special-chars (:block/uuid tag))))
  93. (concat (:block/tags result) (:block/tags block)))
  94. (remove nil?)))))))
  95. (defn save-file!
  96. "This fn is the db version of file-handler/alter-file"
  97. [path content]
  98. (let [file-valid? (if (= path "logseq/config.edn")
  99. (do (config-edn-common-handler/detect-deprecations path content {:db-graph? true})
  100. (config-edn-common-handler/validate-config-edn path content repo-config-schema/Config-edn))
  101. true)]
  102. (when file-valid?
  103. (p/do!
  104. (db/transact! [{:file/path path
  105. :file/content content
  106. :file/last-modified-at (js/Date.)}])
  107. ;; Post save
  108. (cond (= path "logseq/config.edn")
  109. (p/let [_ (repo-config-handler/restore-repo-config! (state/get-current-repo) content)]
  110. (state/pub-event! [:shortcut/refresh]))
  111. (= path "logseq/custom.css")
  112. (ui-handler/add-style-if-exists!))))))
  113. (defn- set-heading-aux!
  114. [block-id heading]
  115. (let [block (db/pull [:block/uuid block-id])
  116. old-heading (:logseq.property/heading block)]
  117. (cond
  118. ;; nothing changed for first two cases
  119. (or (and (nil? old-heading) (nil? heading))
  120. (and (true? old-heading) (true? heading))
  121. (= old-heading heading))
  122. nil
  123. (or (and (nil? old-heading) (true? heading))
  124. (and (true? old-heading) (nil? heading)))
  125. nil
  126. (and (or (nil? heading) (true? heading))
  127. (number? old-heading))
  128. (let [content (commands/clear-markdown-heading (:block/content block))]
  129. {:block/content content
  130. :block/uuid (:block/uuid block)})
  131. (and (or (nil? old-heading) (true? old-heading))
  132. (number? heading))
  133. (let [content (commands/set-markdown-heading (:block/content block) heading)]
  134. {:block/content content
  135. :block/uuid (:block/uuid block)})
  136. ;; heading-num1 -> heading-num2
  137. :else
  138. (let [content (-> block
  139. :block/content
  140. commands/clear-markdown-heading
  141. (commands/set-markdown-heading heading))]
  142. {:block/uuid (:block/uuid block)
  143. :block/content content}))))
  144. (defn batch-set-heading!
  145. [repo block-ids heading]
  146. (ui-outliner-tx/transact!
  147. {:outliner-op :save-block}
  148. (doseq [block (keep #(set-heading-aux! % heading) block-ids)]
  149. (outliner-op/save-block! block))
  150. (property-handler/batch-set-block-property! repo block-ids :heading heading)))