Browse Source

Merge pull request #2513 from logseq/fix/lost-aliases-when-remove-alias-page

fix: delete alias page cause wrong alias relations
Tienson Qin 4 years ago
parent
commit
eebb87d957
2 changed files with 26 additions and 1 deletions
  1. 14 0
      src/main/frontend/db_schema.cljs
  2. 12 1
      src/main/frontend/handler/page.cljs

+ 14 - 0
src/main/frontend/db_schema.cljs

@@ -147,3 +147,17 @@
     :block/updated-at
     }
   )
+
+
+;;; use `(map [:db.fn/retractAttribute <id> <attr>] retract-page-attributes)`
+;;; to remove attrs to make the page as it's just created and no file attached to it
+(def retract-page-attributes
+  #{:block/created-at
+    :block/updated-at
+    :block/file
+    :block/format
+    :block/content
+    :block/properties
+    :block/alias
+    :block/tags
+    :block/unordered})

+ 12 - 1
src/main/frontend/handler/page.cljs

@@ -20,6 +20,8 @@
             [frontend.modules.outliner.tree :as outliner-tree]
             [frontend.commands :as commands]
             [frontend.date :as date]
+            [frontend.db-schema :as db-schema]
+            [frontend.db.model :as model]
             [clojure.walk :as walk]
             [frontend.git :as git]
             [frontend.fs :as fs]
@@ -205,7 +207,16 @@
                (p/catch (fn [err]
                           (js/console.error "error: " err))))))
 
-          (db/transact! [[:db.fn/retractEntity [:block/name page-name]]])
+
+          ;; if other page alias this pagename,
+          ;; then just remove some attrs of this entity instead of retractEntity
+          (if (model/get-alias-source-page (state/get-current-repo) page-name)
+            (when-let [id (:db/id (db/entity [:block/name page-name]))]
+              (let [txs (mapv (fn [attribute]
+                                [:db/retract id attribute])
+                              db-schema/retract-page-attributes)]
+                (db/transact! txs)))
+            (db/transact! [[:db.fn/retractEntity [:block/name page-name]]]))
 
           (ok-handler))))))