fix: add hidden pages for blocks created from properties

pull/10438/head
Tienson Qin 2023-10-09 20:48:01 +08:00
parent d9f59f863d
commit f56b37d9bc
9 changed files with 125 additions and 74 deletions

View File

@ -267,61 +267,19 @@
(defn create-new-block!
[block property value]
(let [repo (state/get-current-repo)
page-id (or (:db/id (:block/page block)) (:db/id block))
parent-id (db/new-block-id)
metadata {:created-from-block (:block/uuid block)
:created-from-property (:block/uuid property)}
parent (-> {:block/uuid parent-id
:block/format :markdown
:block/content ""
:block/page {:db/id page-id}
:block/metadata metadata}
outliner-core/block-with-timestamps)
child-1-id (db/new-block-id)
child-1 (-> {:block/uuid child-1-id
:block/format :markdown
:block/content value
:block/page {:db/id page-id}
:block/parent [:block/uuid parent-id]
:block/left [:block/uuid parent-id]
:block/metadata metadata}
outliner-core/block-with-timestamps
(editor-handler/wrap-parse-block))
child-2-id (db/new-block-id)
child-2 (-> {:block/uuid child-2-id
:block/format :markdown
:block/content ""
:block/page {:db/id page-id}
:block/parent [:block/uuid parent-id]
:block/left [:block/uuid child-1-id]
:block/metadata metadata}
outliner-core/block-with-timestamps)
blocks (if (string/blank? value)
[parent child-1]
[parent child-1 child-2])
{:keys [page blocks]} (property-handler/property-create-new-block block property value editor-handler/wrap-parse-block)
last-block-id (:block/uuid (last blocks))]
(db/transact! repo blocks {:outliner-op :insert-blocks})
(add-property! block (:block/original-name property) parent-id)
(db/transact! repo (if page (cons page blocks) blocks) {:outliner-op :insert-blocks})
(add-property! block (:block/original-name property)
(:block/uuid (first blocks)))
(editor-handler/edit-block! (db/entity [:block/uuid last-block-id]) 0 last-block-id)))
(defn create-new-block-from-template!
[block property template]
(let [repo (state/get-current-repo)
page-id (or (:db/id (:block/page block)) (:db/id block))
block-id (db/new-block-id)
metadata {:created-from-block (:block/uuid block)
:created-from-property (:block/uuid property)
:created-from-template (:block/uuid template)}
value-block (-> {:block/uuid block-id
:block/format :markdown
:block/content ""
:block/tags #{(:db/id template)}
:block/page {:db/id page-id}
:block/metadata metadata}
outliner-core/block-with-timestamps)
tx-data [value-block]]
(db/transact! repo tx-data {:outliner-op :insert-blocks})
(add-property! block (:block/original-name property) block-id)))
{:keys [page blocks]} (property-handler/property-create-new-block-from-template block property template)]
(db/transact! repo (if page (cons page blocks) blocks) {:outliner-op :insert-blocks})
(add-property! block (:block/original-name property) (:block/uuid (last blocks)))))
(defn- new-text-editor-opts
[repo block property value editor-id]

View File

@ -110,6 +110,13 @@
(->> (get-all-namespace-relation repo)
(map second)))
(defn hidden-page?
[page]
(if (string? page)
(and (string/starts-with? page "$$$")
(util/uuid-string? (gp-util/safe-subs page 3)))
(contains? (:block/type page) "hidden")))
(defn get-pages
[repo]
(->> (d/q
@ -117,22 +124,26 @@
:where
[?page :block/name ?page-name]
[(get-else $ ?page :block/original-name ?page-name) ?page-original-name]]
(conn/get-db repo))
(map first)))
(conn/get-db repo))
(map first)
(remove hidden-page?)))
(defn get-all-pages
[repo]
(d/q
'[:find [(pull ?page [*]) ...]
:where
[?page :block/name]]
(conn/get-db repo)))
(->>
(d/q
'[:find [(pull ?page [*]) ...]
:where
[?page :block/name]]
(conn/get-db repo))
(remove hidden-page?)))
(defn get-all-page-original-names
[repo]
(let [db (conn/get-db repo)]
(->> (d/datoms db :avet :block/original-name)
(map :v))))
(map :v)
(remove hidden-page?))))
(defn get-pages-with-file
"Return full file entity for calling file renaming"
@ -562,7 +573,7 @@ independent of format as format specific heading characters are stripped"
(defn get-block-last-direct-child
"Notice: if `not-collapsed?` is true, will skip searching for any collapsed block."
([db db-id]
(get-block-last-direct-child db db-id true))
(get-block-last-direct-child db db-id false))
([db db-id not-collapsed?]
(when-let [block (db-utils/entity db db-id)]
(when (if not-collapsed?
@ -1448,7 +1459,8 @@ independent of format as format specific heading characters are stripped"
page))))
pages)
(remove false?)
(remove nil?))]
(remove nil?)
(remove hidden-page?))]
orphaned-pages))
;; FIXME: replace :logseq.macro-name with id

View File

@ -73,7 +73,7 @@
(outliner-core/block)
(outliner-tree/-get-down)
(outliner-core/get-data))
to-last-direct-child-id (model/get-block-last-direct-child (db/get-db) to-id false)
to-last-direct-child-id (model/get-block-last-direct-child (db/get-db) to-id)
repo (state/get-current-repo)
conn (conn/get-db repo false)
datoms (d/datoms @conn :avet :block/page from-id)
@ -140,4 +140,4 @@
(when (string/blank? new-name)
(notification/show! "Please use a valid name, empty name is not allowed!" :error)))
(ui-handler/re-render-root!))))
(ui-handler/re-render-root!))))

View File

@ -788,11 +788,10 @@
transact-opts
(cond
(and prev-block (:block/name prev-block)
(not= (:db/id prev-block) (:db/id (:block/parent block)))) ; embed page
(not= (:db/id prev-block) (:db/id (:block/parent block)))
(model/hidden-page? (:block/page block))) ; embed page
(let [target (or
(some-> (model/get-block-last-direct-child (db/get-db)
(:db/id prev-block)
false)
(some-> (model/get-block-last-direct-child (db/get-db) (:db/id prev-block))
db/entity)
prev-block)]
(outliner-core/move-blocks! [block] target (not= (:db/id target) (:db/id prev-block)))

View File

@ -321,7 +321,7 @@
(outliner-core/block)
(outliner-tree/-get-down)
(outliner-core/get-data))
to-last-direct-child-id (model/get-block-last-direct-child (db/get-db) to-id false)
to-last-direct-child-id (model/get-block-last-direct-child (db/get-db) to-id)
repo (state/get-current-repo)
conn (conn/get-db repo false)
datoms (d/datoms @conn :avet :block/page from-id)
@ -384,4 +384,4 @@
(rename-nested-pages old-name new-name))
(when (string/blank? new-name)
(notification/show! "Please use a valid name, empty name is not allowed!" :error)))
(ui-handler/re-render-root!))))
(ui-handler/re-render-root!))))

View File

@ -6,7 +6,11 @@
[frontend.config :as config]
[frontend.util :as util]
[frontend.state :as state]
[frontend.db :as db]))
[frontend.db :as db]
[frontend.format.block :as block]
[frontend.db.model :as model]
[frontend.modules.outliner.core :as outliner-core]
[clojure.string :as string]))
(defn remove-block-property!
[repo block-id key]
@ -168,3 +172,76 @@
(seq (:block/alias properties))
(and (seq properties)
(not= (keys properties) [(:block/uuid (db/entity [:block/name "icon"]))])))))
(defn property-create-new-block
[block property value parse-block]
(let [current-page-id (:block/uuid (or (:block/page block) block))
page-name (str "$$$" current-page-id)
page-entity (db/entity [:block/name page-name])
page (or page-entity
(-> (block/page-name->map page-name true)
(assoc :block/type #{"hidden"})))
page-tx (when-not page-entity page)
page-id [:block/uuid (:block/uuid page)]
parent-id (db/new-block-id)
metadata {:created-from-block (:block/uuid block)
:created-from-property (:block/uuid property)}
parent (-> {:block/uuid parent-id
:block/format :markdown
:block/content ""
:block/page page-id
:block/parent page-id
:block/left (or (when page-entity (model/get-block-last-direct-child (db/get-db) (:db/id page-entity)))
page-id)
:block/metadata metadata}
outliner-core/block-with-timestamps)
child-1-id (db/new-block-id)
child-1 (-> {:block/uuid child-1-id
:block/format :markdown
:block/content value
:block/page page-id
:block/parent [:block/uuid parent-id]
:block/left [:block/uuid parent-id]
:block/metadata metadata}
outliner-core/block-with-timestamps
parse-block)
child-2-id (db/new-block-id)
child-2 (-> {:block/uuid child-2-id
:block/format :markdown
:block/content ""
:block/page page-id
:block/parent [:block/uuid parent-id]
:block/left [:block/uuid child-1-id]
:block/metadata metadata}
outliner-core/block-with-timestamps)]
{:page page-tx
:blocks (if (string/blank? value)
[parent child-1]
[parent child-1 child-2])}))
(defn property-create-new-block-from-template
[block property template]
(let [current-page-id (:block/uuid (or (:block/page block) block))
page-name (str "$$$" current-page-id)
page-entity (db/entity [:block/name page-name])
page (or page-entity
(-> (block/page-name->map page-name true)
(assoc :block/type #{"hidden"})))
page-tx (when-not page-entity page)
page-id [:block/uuid (:block/uuid page)]
block-id (db/new-block-id)
metadata {:created-from-block (:block/uuid block)
:created-from-property (:block/uuid property)
:created-from-template (:block/uuid template)}
new-block (-> {:block/uuid block-id
:block/format :markdown
:block/content ""
:block/tags #{(:db/id template)}
:block/page page-id
:block/metadata metadata
:block/parent page-id
:block/left (or (when page-entity (model/get-block-last-direct-child (db/get-db) (:db/id page-entity)))
page-id)}
outliner-core/block-with-timestamps)]
{:page page-tx
:blocks [new-block]}))

View File

@ -133,7 +133,7 @@
(defn- get-last-child-or-self
[block]
(let [last-child (some-> (db-model/get-block-last-direct-child (conn/get-db) (:db/id block))
(let [last-child (some-> (db-model/get-block-last-direct-child (conn/get-db) (:db/id block) true)
db/entity)
target (or last-child block)]
[target (some? last-child)]))
@ -1050,7 +1050,7 @@
opts {:outliner-op :indent-outdent-blocks}]
(if indent?
(when (and left (not (page-first-child? first-block)))
(let [last-direct-child-id (db-model/get-block-last-direct-child db (:db/id left) false)
(let [last-direct-child-id (db-model/get-block-last-direct-child db (:db/id left))
blocks' (drop-while (fn [b]
(= (:db/id (:block/parent b))
(:db/id left)))
@ -1089,7 +1089,7 @@
right-siblings (->> (get-right-siblings (block last-top-block))
(map :data))]
(if (seq right-siblings)
(let [result2 (if-let [last-direct-child-id (db-model/get-block-last-direct-child db (:db/id last-top-block) false)]
(let [result2 (if-let [last-direct-child-id (db-model/get-block-last-direct-child db (:db/id last-top-block))]
(move-blocks right-siblings (db/entity last-direct-child-id) (merge opts {:sibling? true}))
(move-blocks right-siblings last-top-block (merge opts {:sibling? false})))]
(concat-tx-fn result result2))

View File

@ -94,7 +94,9 @@
(when-not importing?
(react/refresh! repo tx-report'))
(when (and (config/db-based-graph? repo) (not (:skip-persist? tx-meta)) (not replace?))
(when (and (config/db-based-graph? repo) (not (:skip-persist? tx-meta))
(not replace?)
(not (:update-tx-ids? tx-meta)))
(let [upsert-blocks (outliner-pipeline/build-upsert-blocks blocks deleted-block-uuids (:db-after tx-report'))
updated-blocks (remove (fn [b] (contains? (set deleted-block-uuids) (:block/uuid b))) blocks)
tx-id (get-in tx-report' [:tempids :db/current-tx])
@ -103,7 +105,8 @@
{:db/id db-id
:block/tx-id tx-id})) updated-blocks)]
(when (seq update-tx-ids)
(db/transact! repo update-tx-ids {:replace? true}))
(db/transact! repo update-tx-ids {:replace? true
:update-tx-ids? true}))
(when-not config/publishing?
(p/let [_transact-result (persist-db/<transact-data repo upsert-blocks deleted-block-uuids)
_ipc-result (comment ipc/ipc :db-transact-data repo

View File

@ -2,6 +2,7 @@
(:require [cljs-bean.core :as bean]
[clojure.string :as string]
[frontend.db :as db]
[frontend.db.model :as model]
[frontend.state :as state]
[frontend.util :as util]
["fuse.js" :as fuse]))
@ -35,7 +36,8 @@
[{:block/keys [uuid original-name] :as page}]
(when-let [content (some-> (:block/file page)
(:file/content))]
(when-not (string/blank? original-name)
(when-not (or (string/blank? original-name)
(model/hidden-page? original-name))
(when-not (> (count content) (* (max-len) 10))
{:id (:db/id page)
:uuid (str uuid)