editor.cljs 7.0 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.property.util :as pu]
  16. [frontend.handler.repo-config :as repo-config-handler]
  17. [frontend.modules.outliner.ui :as ui-outliner-tx]
  18. [frontend.modules.outliner.op :as outliner-op]
  19. [frontend.schema.handler.repo-config :as repo-config-schema]
  20. [promesa.core :as p]
  21. [logseq.db.frontend.content :as db-content]))
  22. (defn- remove-non-existed-refs!
  23. [refs]
  24. (remove (fn [x] (or
  25. (and (vector? x)
  26. (= :block/uuid (first x))
  27. (nil? (db/entity x)))
  28. (nil? x))) refs))
  29. (defn- replace-tag-ref
  30. [content page-name id]
  31. (let [id' (str db-content/page-ref-special-chars id)
  32. [page wrapped-id] (if (string/includes? page-name " ")
  33. (map page-ref/->page-ref [page-name id'])
  34. [page-name id'])
  35. page-name (util/format "#%s" page)
  36. r (util/format "#%s" wrapped-id)]
  37. ;; hash tag parsing rules https://github.com/logseq/mldoc/blob/701243eaf9b4157348f235670718f6ad19ebe7f8/test/test_markdown.ml#L631
  38. ;; Safari doesn't support look behind, don't use
  39. ;; TODO: parse via mldoc
  40. (string/replace content
  41. (re-pattern (str "(?i)(^|\\s)(" (util/escape-regex-chars page-name) ")(?=[,\\.]*($|\\s))"))
  42. ;; case_insense^ ^lhs ^_grp2 look_ahead^ ^_grp3
  43. (fn [[_match lhs _grp2 _grp3]]
  44. (str lhs r)))))
  45. (defn- replace-page-ref
  46. [content page-name id]
  47. (let [id' (str db-content/page-ref-special-chars id)
  48. [page wrapped-id] (map page-ref/->page-ref [page-name id'])]
  49. (util/replace-ignore-case content page wrapped-id)))
  50. (defn- replace-page-ref-with-id
  51. [content page-name id]
  52. (-> content
  53. (replace-page-ref page-name id)
  54. (replace-tag-ref page-name id)))
  55. (defn- replace-page-refs-with-ids
  56. [block]
  57. (let [content (:block/content block)
  58. content' (if (some :block/name (:block/refs block))
  59. (reduce
  60. (fn [content {:block/keys [original-name uuid]}]
  61. (replace-page-ref-with-id content original-name uuid))
  62. content
  63. (filter :block/name (:block/refs block)))
  64. content)]
  65. (assoc block :block/content content')))
  66. (defn wrap-parse-block
  67. [{:block/keys [content left level] :as block}]
  68. (let [block (or (and (:db/id block) (db/pull (:db/id block))) block)
  69. block (if (nil? content)
  70. block
  71. (let [ast (mldoc/->edn (string/trim content) :markdown)
  72. first-elem-type (first (ffirst ast))
  73. block-with-title? (mldoc/block-with-title? first-elem-type)
  74. content' (str (config/get-block-pattern :markdown) (if block-with-title? " " "\n") content)
  75. block' (merge block (block/parse-block (assoc block :block/content content')))
  76. block' (if (seq (:block/properties block))
  77. (update block' :block/properties (fn [m] (merge m (:block/properties block))))
  78. block')]
  79. (update block' :block/refs remove-non-existed-refs!)))
  80. block (if (and left (not= (:block/left block) left)) (assoc block :block/left left) block)
  81. result (-> block
  82. (merge (if level {:block/level level} {}))
  83. (replace-page-refs-with-ids))]
  84. (-> result
  85. ;; Remove tags from content
  86. (assoc :block/content
  87. (db-content/content-without-tags
  88. (:block/content result)
  89. (->>
  90. (map
  91. (fn [tag]
  92. (when (:block/uuid tag)
  93. (str db-content/page-ref-special-chars (:block/uuid tag))))
  94. (concat (:block/tags result) (:block/tags block)))
  95. (remove nil?)))))))
  96. (defn save-file!
  97. "This fn is the db version of file-handler/alter-file"
  98. [path content]
  99. (let [file-valid? (if (= path "logseq/config.edn")
  100. (do (config-edn-common-handler/detect-deprecations path content {:db-graph? true})
  101. (config-edn-common-handler/validate-config-edn path content repo-config-schema/Config-edn))
  102. true)]
  103. (when file-valid?
  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 (pu/lookup (:block/properties block) :heading)]
  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)))