fix(editor): hide page properties if there's only a title attribute

We need to design a better UX for both page and block's properties.
This commit is contained in:
Tienson Qin
2020-12-21 12:12:11 +08:00
parent d4f27fc798
commit 82a2f049e1
2 changed files with 37 additions and 25 deletions

View File

@@ -125,6 +125,19 @@
:block/collapsed? false})
block-ids))))
(defn pre-block-with-only-title?
[repo block-id]
(when-let [block (db/entity repo [:block/uuid block-id])]
(let [properties (:page/properties (:block/page block))]
(and (:title properties)
(= 1 (count properties))
(let [ast (mldoc/->edn (:block/content block) (mldoc/default-config (:block/format block)))]
(or
(empty? (rest ast))
(every? (fn [[[typ break-lines]] _]
(and (= typ "Paragraph")
(every? #(= % ["Break_Line"]) break-lines))) (rest ast))))))))
(defn with-dummy-block
([blocks format]
(with-dummy-block blocks format {} {}))
@@ -132,37 +145,37 @@
:or {journal? false}}]
(let [format (or format (state/get-preferred-format) :markdown)
blocks (if (and journal?
(seq blocks)
(when-let [title (second (first (:block/title (first blocks))))]
(date/valid-journal-title? title)))
(seq blocks)
(when-let [title (second (first (:block/title (first blocks))))]
(date/valid-journal-title? title)))
(rest blocks)
blocks)
blocks (vec blocks)]
(cond
(and (seq blocks)
(or (and (> (count blocks) 1)
(:block/pre-block? (first blocks)))
(and (>= (count blocks) 1)
(not (:block/pre-block? (first blocks))))))
(or (and (> (count blocks) 1)
(:block/pre-block? (first blocks)))
(and (>= (count blocks) 1)
(not (:block/pre-block? (first blocks))))))
blocks
:else
(let [last-block (last blocks)
end-pos (get-in last-block [:block/meta :end-pos] 0)
dummy (merge last-block
(let [uuid (d/squuid)]
{:block/uuid uuid
:block/title ""
:block/content (config/default-empty-block format)
:block/format format
:block/level 2
:block/priority nil
:block/anchor (str uuid)
:block/meta {:start-pos end-pos
:end-pos end-pos}
:block/body nil
:block/dummy? true
:block/marker nil
:block/pre-block? false})
default-option)]
(let [uuid (d/squuid)]
{:block/uuid uuid
:block/title ""
:block/content (config/default-empty-block format)
:block/format format
:block/level 2
:block/priority nil
:block/anchor (str uuid)
:block/meta {:start-pos end-pos
:end-pos end-pos}
:block/body nil
:block/dummy? true
:block/marker nil
:block/pre-block? false})
default-option)]
(conj blocks dummy))))))