Procházet zdrojové kódy

fix: update content when changing markdown heading

moved db-based and file-based implementations to their respective
namespaces
Fixes LOG-2821
Gabriel Horner před 2 roky
rodič
revize
7b6b9c5df2

+ 49 - 0
src/main/frontend/handler/db_based/editor.cljs

@@ -2,6 +2,7 @@
   "DB-based graph implementation"
   (:require [clojure.string :as string]
             [frontend.config :as config]
+            [frontend.commands :as commands]
             [frontend.db :as db]
             [frontend.format.block :as block]
             [frontend.format.mldoc :as mldoc]
@@ -11,7 +12,11 @@
             [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.handler.ui :as ui-handler]
             [frontend.handler.common.config-edn :as config-edn-common-handler]
+            [frontend.handler.property :as property-handler]
+            [frontend.handler.property.util :as pu]
             [frontend.handler.repo-config :as repo-config-handler]
+            [frontend.modules.outliner.core :as outliner-core]
+            [frontend.modules.outliner.transaction :as outliner-tx]
             [frontend.schema.handler.repo-config :as repo-config-schema]
             [promesa.core :as p]))
 
@@ -108,3 +113,47 @@
               (state/pub-event! [:shortcut/refresh]))
             (= path "logseq/custom.css")
             (ui-handler/add-style-if-exists!)))))
+
+(defn- set-heading-aux!
+  [block-id heading]
+  (let [block (db/pull [:block/uuid block-id])
+        old-heading (pu/lookup (:block/properties block) :heading)]
+    (cond
+      ;; nothing changed for first two cases
+      (or (and (nil? old-heading) (nil? heading))
+          (and (true? old-heading) (true? heading))
+          (= old-heading heading))
+      nil
+
+      (or (and (nil? old-heading) (true? heading))
+          (and (true? old-heading) (nil? heading)))
+      nil
+
+      (and (or (nil? heading) (true? heading))
+           (number? old-heading))
+      (let [content (commands/clear-markdown-heading (:block/content block))]
+        {:block/content content
+         :block/uuid (:block/uuid block)})
+
+      (and (or (nil? old-heading) (true? old-heading))
+           (number? heading))
+      (let [content (commands/set-markdown-heading (:block/content block) heading)]
+        {:block/content content
+         :block/uuid (:block/uuid block)})
+
+        ;; heading-num1 -> heading-num2
+      :else
+      (let [content (-> block
+                        :block/content
+                        commands/clear-markdown-heading
+                        (commands/set-markdown-heading heading))]
+        {:block/uuid (:block/uuid block)
+         :block/content content}))))
+
+(defn batch-set-heading!
+  [repo block-ids heading]
+  (outliner-tx/transact!
+   {:outliner-op :save-block}
+   (doseq [block-tx (keep #(set-heading-aux! % heading) block-ids)]
+     (outliner-core/save-block! block-tx))
+   (property-handler/batch-set-block-property! repo block-ids :heading heading)))

+ 2 - 70
src/main/frontend/handler/editor.cljs

@@ -837,26 +837,6 @@
                             (dom/attr sibling-block "id")
                             "")))))
 
-(defn- set-block-property-aux!
-  [repo block-or-id key value]
-  (when-let [block (cond (string? block-or-id) (db/entity [:block/uuid (uuid block-or-id)])
-                         (uuid? block-or-id) (db/entity [:block/uuid block-or-id])
-                         :else block-or-id)]
-    (let [format (:block/format block)
-          content (:block/content block)
-          properties (:block/properties block)
-          properties (if (nil? value)
-                       (dissoc properties key)
-                       (assoc properties key value))
-          content (if (nil? value)
-                    (file-property/remove-property-when-file-based repo format key content)
-                    (file-property/insert-property format content key value))
-          content (file-property/remove-empty-properties-when-file-based repo content)]
-      {:block/uuid (:block/uuid block)
-       :block/properties properties
-       :block/properties-order (or (keys properties) [])
-       :block/content content})))
-
 (defn set-block-query-properties!
   [block-id all-properties key add?]
   (when-let [block (db/entity [:block/uuid block-id])]
@@ -3785,60 +3765,12 @@
     (first (:block/_parent (db/entity (:db/id block)))))
    (util/collapsed? block)))
 
-;; file graph only
-(defn- set-heading-aux!
-  [repo block-id heading]
-  (let [block (db/pull [:block/uuid block-id])
-        format (:block/format block)
-        old-heading (get-in block [:block/properties :heading])]
-    (if (= format :markdown)
-      (cond
-        ;; nothing changed
-        (or (and (nil? old-heading) (nil? heading))
-            (and (true? old-heading) (true? heading))
-            (= old-heading heading))
-        nil
-
-        (or (and (nil? old-heading) (true? heading))
-            (and (true? old-heading) (nil? heading)))
-        (set-block-property-aux! repo block :heading heading)
-
-        (and (or (nil? heading) (true? heading))
-             (number? old-heading))
-        (let [block' (set-block-property-aux! repo block :heading heading)
-              content (commands/clear-markdown-heading (:block/content block'))]
-          (merge block' {:block/content content}))
-
-        (and (or (nil? old-heading) (true? old-heading))
-             (number? heading))
-        (let [block' (set-block-property-aux! repo block :heading nil)
-              properties (assoc (:block/properties block) :heading heading)
-              content (commands/set-markdown-heading (:block/content block') heading)]
-          (merge block' {:block/content content :block/properties properties}))
-
-        ;; heading-num1 -> heading-num2
-        :else
-        (let [properties (assoc (:block/properties block) :heading heading)
-              content (-> block
-                          :block/content
-                          commands/clear-markdown-heading
-                          (commands/set-markdown-heading heading))]
-          {:block/uuid (:block/uuid block)
-           :block/properties properties
-           :block/content content}))
-      (set-block-property-aux! repo block :heading heading))))
-
 (defn batch-set-heading!
   [block-ids heading]
   (let [repo (state/get-current-repo)]
     (if (config/db-based-graph? repo)
-      ;; FIXME: Update content like is done with set-heading-aux!
-      (property-handler/batch-set-block-property! repo block-ids :heading heading)
-      (outliner-tx/transact!
-       {:outliner-op :save-block}
-       (doseq [block-id block-ids]
-         (when-let [block (set-heading-aux! repo block-id heading)]
-           (outliner-core/save-block! block)))))))
+      (db-editor-handler/batch-set-heading! repo block-ids heading) 
+      (file-editor-handler/batch-set-heading! block-ids heading))))
 
 (defn set-heading!
   [block-id heading]

+ 73 - 0
src/main/frontend/handler/file_based/editor.cljs

@@ -2,15 +2,19 @@
   "File-based graph implementation"
   (:require [clojure.string :as string]
             [frontend.config :as config]
+            [frontend.commands :as commands]
             [frontend.format.block :as block]
             [frontend.db :as db]
             [frontend.format.mldoc :as mldoc]
+            [frontend.modules.outliner.core :as outliner-core]
             [frontend.state :as state]
+            [frontend.modules.outliner.transaction :as outliner-tx]
             [frontend.util :as util]
             [frontend.util.clock :as clock]
             [frontend.util.drawer :as drawer]
             [frontend.util.marker :as marker]
             [frontend.handler.file-based.property :as file-property]
+            [frontend.handler.file-based.property.util :as property-util]
             [logseq.db.frontend.schema :as db-schema]
             [logseq.graph-parser.block :as gp-block]
             [logseq.graph-parser.mldoc :as gp-mldoc]
@@ -142,3 +146,72 @@
      :block/content content
      :block/parent page
      :block/page page}))
+
+(defn- set-block-property-aux!
+  [block-or-id key value]
+  (when-let [block (cond (string? block-or-id) (db/entity [:block/uuid (uuid block-or-id)])
+                         (uuid? block-or-id) (db/entity [:block/uuid block-or-id])
+                         :else block-or-id)]
+    (let [format (:block/format block)
+          content (:block/content block)
+          properties (:block/properties block)
+          properties (if (nil? value)
+                       (dissoc properties key)
+                       (assoc properties key value))
+          content (if (nil? value)
+                    (property-util/remove-property format key content)
+                    (property-util/insert-property format content key value))
+          content (property-util/remove-empty-properties content)]
+      {:block/uuid (:block/uuid block)
+       :block/properties properties
+       :block/properties-order (or (keys properties) [])
+       :block/content content})))
+
+(defn- set-heading-aux!
+  [block-id heading]
+  (let [block (db/pull [:block/uuid block-id])
+        format (:block/format block)
+        old-heading (get-in block [:block/properties :heading])]
+    (if (= format :markdown)
+      (cond
+        ;; nothing changed
+        (or (and (nil? old-heading) (nil? heading))
+            (and (true? old-heading) (true? heading))
+            (= old-heading heading))
+        nil
+
+        (or (and (nil? old-heading) (true? heading))
+            (and (true? old-heading) (nil? heading)))
+        (set-block-property-aux! block :heading heading)
+
+        (and (or (nil? heading) (true? heading))
+             (number? old-heading))
+        (let [block' (set-block-property-aux! block :heading heading)
+              content (commands/clear-markdown-heading (:block/content block'))]
+          (merge block' {:block/content content}))
+
+        (and (or (nil? old-heading) (true? old-heading))
+             (number? heading))
+        (let [block' (set-block-property-aux! block :heading nil)
+              properties (assoc (:block/properties block) :heading heading)
+              content (commands/set-markdown-heading (:block/content block') heading)]
+          (merge block' {:block/content content :block/properties properties}))
+
+        ;; heading-num1 -> heading-num2
+        :else
+        (let [properties (assoc (:block/properties block) :heading heading)
+              content (-> block
+                          :block/content
+                          commands/clear-markdown-heading
+                          (commands/set-markdown-heading heading))]
+          {:block/uuid (:block/uuid block)
+           :block/properties properties
+           :block/content content}))
+      (set-block-property-aux! block :heading heading))))
+
+(defn batch-set-heading! [block-ids heading]
+  (outliner-tx/transact!
+       {:outliner-op :save-block}
+       (doseq [block-id block-ids]
+         (when-let [block (set-heading-aux! block-id heading)]
+           (outliner-core/save-block! block)))))