enhance: allow class pages to be deleted if unused

pull/10438/head
Gabriel Horner 2023-09-22 15:28:59 -04:00
parent 19be2850ff
commit e7423f93ac
3 changed files with 53 additions and 28 deletions

View File

@ -26,7 +26,9 @@
(page-handler/delete! page-name (page-handler/delete! page-name
(fn [] (fn []
(notification/show! (str "Page " page-name " was deleted successfully!") (notification/show! (str "Page " page-name " was deleted successfully!")
:success))) :success))
{:error-handler (fn [{:keys [msg]}]
(notification/show! msg :error))})
(state/close-modal!) (state/close-modal!)
(route-handler/redirect-to-home!)) (route-handler/redirect-to-home!))

View File

@ -75,7 +75,17 @@
[?page :block/original-name ?original-name] [?page :block/original-name ?original-name]
[?page :block/name ?name]] [?page :block/name ?name]]
(conn/get-db repo) (conn/get-db repo)
(util/page-name-sanity-lc tag-name)))) (util/page-name-sanity-lc tag-name))))
(defn get-tag-blocks
[repo tag-name]
(d/q '[:find ?b
:in $ ?tag
:where
[?e :block/name ?tag]
[?b :block/tags ?e]]
(conn/get-db repo)
(util/page-name-sanity-lc tag-name)))
(defn get-all-tagged-pages (defn get-all-tagged-pages
[repo] [repo]

View File

@ -476,11 +476,23 @@
refs)] refs)]
tx-data))))) tx-data)))))
(defn- page-unable-to-delete
"If a page is unable to delete, returns a map with more information. Otherwise returns nil"
[repo page]
(cond
(and (= "class" (:block/type page))
(seq (model/get-tag-blocks repo (:block/name page))))
{:msg "Unable to delete this page because blocks are tagged with this page"}
(= "property" (:block/type page))
{:msg "Unable to delete this page because this page is a property"}))
(defn delete! (defn delete!
[page-name ok-handler & {:keys [delete-file? redirect-to-home? persist-op?] "Deletes a page and then either calls the ok-handler or the error-handler if unable to delete"
[page-name ok-handler & {:keys [delete-file? redirect-to-home? persist-op? error-handler]
:or {delete-file? true :or {delete-file? true
redirect-to-home? true redirect-to-home? true
persist-op? true}}] persist-op? true
error-handler (fn [{:keys [msg]}] (log/error :msg msg))}}]
(when redirect-to-home? (route-handler/redirect-to-home!)) (when redirect-to-home? (route-handler/redirect-to-home!))
(when page-name (when page-name
(when-let [repo (state/get-current-repo)] (when-let [repo (state/get-current-repo)]
@ -490,33 +502,34 @@
(fn [block] (fn [block]
[:db.fn/retractEntity [:block/uuid (:block/uuid block)]]) [:db.fn/retractEntity [:block/uuid (:block/uuid block)]])
blocks) blocks)
page (db/entity [:block/name page-name]) page (db/entity [:block/name page-name])]
_ (delete-file! repo page-name delete-file?) (if-let [msg (and (config/db-based-graph? repo)
;; if other page alias this pagename, (page-unable-to-delete repo page))]
;; then just remove some attrs of this entity instead of retractEntity (error-handler msg)
delete-page-tx (cond (let [_ (delete-file! repo page-name delete-file?)
(contains? #{"property" "class"} (:block/type page)) ;; if other page alias this pagename,
nil ;; then just remove some attrs of this entity instead of retractEntity
delete-page-tx (cond
(not (:block/_namespace page))
(if (model/get-alias-source-page (state/get-current-repo) page-name)
(when-let [id (:db/id (db/entity [:block/name page-name]))]
(mapv (fn [attribute]
[:db/retract id attribute])
db-schema/retract-page-attributes))
(concat (db-refs->page repo page)
[[:db.fn/retractEntity [:block/name page-name]]]))
(not (:block/_namespace page)) :else
(if (model/get-alias-source-page (state/get-current-repo) page-name) nil)
(when-let [id (:db/id (db/entity [:block/name page-name]))] _ (prn :PTX delete-page-tx)
(mapv (fn [attribute] _ (prn :BTX truncate-blocks-tx-data)
[:db/retract id attribute]) tx-data (concat truncate-blocks-tx-data delete-page-tx)]
db-schema/retract-page-attributes)) (db/transact! repo tx-data {:outliner-op :delete-page :persist-op? persist-op?})
(concat (db-refs->page repo page)
[[:db.fn/retractEntity [:block/name page-name]]]))
:else (unfavorite-page! page-name)
nil)
tx-data (concat truncate-blocks-tx-data delete-page-tx)]
(db/transact! repo tx-data {:outliner-op :delete-page :persist-op? persist-op?}) (when (fn? ok-handler) (ok-handler))
(ui-handler/re-render-root!)))))))
(unfavorite-page! page-name)
(when (fn? ok-handler) (ok-handler))
(ui-handler/re-render-root!)))))
(defn- rename-update-block-refs! (defn- rename-update-block-refs!
[refs from-id to-id] [refs from-id to-id]