fix: page-remove-property!

pull/1735/head
Tienson Qin 2021-05-03 18:20:00 +08:00
parent 7a5020b75a
commit df87d047dc
4 changed files with 39 additions and 13 deletions

View File

@ -34,7 +34,7 @@
entity pull pull-many transact! get-key-value]
[frontend.db.model
add-properties! block-and-children-transform blocks-count blocks-count-cache clean-export! cloned? delete-blocks
add-properties! block-and-children-transform blocks-count blocks-count-cache clean-export! cloned? delete-blocks get-pre-block
delete-file! delete-file-blocks! delete-file-pages! delete-file-tx delete-files delete-pages-by-files
filter-only-public-pages-and-blocks get-all-block-contents get-all-tagged-pages
get-all-templates get-block-and-children get-block-by-uuid get-block-children sort-by-left

View File

@ -1276,13 +1276,14 @@
(defn get-pre-block
[repo page-id]
(d/q '[:find (pull ?b [*])
:in $ ?page
:where
[?b :block/page ?page]
[?b :block/pre-block? true]]
(conn/get-conn repo)
page-id))
(-> (d/q '[:find (pull ?b [*])
:in $ ?page
:where
[?b :block/page ?page]
[?b :block/pre-block? true]]
(conn/get-conn repo)
page-id)
ffirst))
(comment
(def page-names ["foo" "bar"])

View File

@ -85,12 +85,37 @@
(p/let [_ (create! page-name)]
(page-add-properties! page-name properties)))))
(defn- remove-property-from-frontmatter-or-directives!
[content format k]
(let [k (name k)
lines (string/split-lines content)
prefix (case format
:org (str "#+" (string/upper-case k) ": ")
:markdown (str (string/lower-case k) ": ")
"")
lines (remove #(util/starts-with? % prefix) lines)]
(string/join "\n" lines)))
(defn page-remove-property!
[page-name k]
(when-let [page (db/entity [:page/name (string/lower-case page-name)])]
(let [tx [{:db/id (:db/id page)
:block/properties (dissoc (:block/properties page) k)}]])
))
(when-let [page (db/entity [:block/name (string/lower-case page-name)])]
(let [repo (state/get-current-repo)
k (keyword k)
pre-block (db/get-pre-block repo (:db/id page))
pre-block-tx (when pre-block
(let [{:block/keys [properties format content]} pre-block]
[{:db/id (:db/id pre-block)
:block/properties (dissoc properties k)
:block/content (-> (text/remove-property! format k content)
(remove-property-from-frontmatter-or-directives! format k))}]))
page-block-tx [{:db/id (:db/id page)
:block/properties (dissoc (:block/properties page) k)}]
txs (->> (concat pre-block-tx page-block-tx)
(remove nil?))]
(db/transact! txs)
(outliner-file/sync-to-file {:db/id (:db/id page)})
(db/refresh! repo {:key :block/change
:data [pre-block]}))))
(defn get-plugins
[blocks]

View File

@ -35,7 +35,7 @@
(log/error :file/write-file-error {:error e})))))
(defn sync-to-file
[{page-db-id :db/id :as page-block}]
[{page-db-id :db/id}]
(async/put! write-chan page-db-id))
(util/batch write-chan