Browse Source

fix: update editing block title if it's changed on db

Usually, it can happens when there're multiple tabs or clients editing
the same block at the same time.
Tienson Qin 10 months ago
parent
commit
c6f3746596
2 changed files with 30 additions and 16 deletions
  1. 19 6
      src/main/frontend/modules/outliner/pipeline.cljs
  2. 11 10
      src/main/frontend/state.cljs

+ 19 - 6
src/main/frontend/modules/outliner/pipeline.cljs

@@ -1,13 +1,24 @@
 (ns frontend.modules.outliner.pipeline
-  (:require [frontend.db :as db]
+  (:require [datascript.core :as d]
+            [frontend.config :as config]
+            [frontend.db :as db]
             [frontend.db.react :as react]
-            [frontend.state :as state]
-            [datascript.core :as d]
+            [frontend.fs :as fs]
             [frontend.handler.ui :as ui-handler]
+            [frontend.state :as state]
             [frontend.util :as util]
-            [frontend.fs :as fs]
-            [logseq.common.path :as path]
-            [frontend.config :as config]))
+            [logseq.common.path :as path]))
+
+(defn- update-editing-block-title-if-changed!
+  [tx-data]
+  (when-let [editing-block (state/get-edit-block)]
+    (let [editing-title (state/get-edit-content)]
+      (when-let [new-title (some (fn [d] (when (and (= (:e d) (:db/id editing-block))
+                                                    (= (:a d) :block/title)
+                                                    (not= editing-title (:v d))
+                                                    (:added d))
+                                           (:v d))) tx-data)]
+        (state/set-edit-content! new-title)))))
 
 (defn invoke-hooks
   [{:keys [_request-id repo tx-meta tx-data deleted-block-uuids deleted-assets affected-keys blocks]}]
@@ -55,6 +66,8 @@
                               tx-data))]
               (d/transact! conn tx-data' tx-meta))
 
+            (update-editing-block-title-if-changed! tx-data)
+
             (when (seq deleted-assets)
               (doseq [asset deleted-assets]
                 (fs/unlink! repo (path/path-join (config/get-current-repo-assets-root) (str (:block/uuid asset) "." (:ext asset))) {})))

+ 11 - 10
src/main/frontend/state.cljs

@@ -1038,16 +1038,6 @@ Similar to re-frame subscriptions"
   []
   @(get @state :editor/block))
 
-(defn set-edit-content!
-  ([input-id value] (set-edit-content! input-id value true))
-  ([input-id value set-input-value?]
-   (when input-id
-     (when set-input-value?
-       (when-let [input (gdom/getElement input-id)]
-         (util/set-change-value input value)))
-     (set-state! :editor/content value :path-in-sub-atom
-                 (or (:block/uuid (get-edit-block)) input-id)))))
-
 (defn editing?
   []
   (seq @(:editor/editing? @state)))
@@ -1066,6 +1056,17 @@ Similar to re-frame subscriptions"
                 id))))
         (catch :default _e)))))
 
+(defn set-edit-content!
+  ([value] (set-edit-content! (get-edit-input-id) value))
+  ([input-id value] (set-edit-content! input-id value true))
+  ([input-id value set-input-value?]
+   (when input-id
+     (when set-input-value?
+       (when-let [input (gdom/getElement input-id)]
+         (util/set-change-value input value)))
+     (set-state! :editor/content value :path-in-sub-atom
+                 (or (:block/uuid (get-edit-block)) input-id)))))
+
 (defn get-input
   []
   (when-let [id (get-edit-input-id)]