Skip to content

Commit

Permalink
refactor: better handling of empty block
Browse files Browse the repository at this point in the history
related to #2140 and #2139
  • Loading branch information
tiensonqin committed Jun 9, 2021
1 parent 0061657 commit 870d46e
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 156 deletions.
2 changes: 2 additions & 0 deletions src/main/frontend/components/block.cljs
Expand Up @@ -342,6 +342,7 @@
:href href
:on-click (fn [e]
(util/stop e)
(editor-handler/insert-first-page-block-if-not-exists! redirect-page-name)
(if (gobj/get e "shiftKey")
(when-let [page-entity (db/entity [:block/name redirect-page-name])]
(state/sidebar-add-block!
Expand Down Expand Up @@ -402,6 +403,7 @@
[:span.text-sm.mr-2 "Alias:" ]
redirect-page-name])]
(let [page (db/entity [:block/name (string/lower-case redirect-page-name)])]
(editor-handler/insert-first-page-block-if-not-exists! redirect-page-name)
(when-let [f (state/get-page-blocks-cp)]
(f (state/get-current-repo) page {:sidebar? sidebar? :preview? true})))]
:interactive true
Expand Down
51 changes: 26 additions & 25 deletions src/main/frontend/components/page.cljs
Expand Up @@ -87,6 +87,17 @@
page-name)]
(db/get-page-format page)))

(rum/defc dummy-block
[page-name]
[:div.ls-block.flex-1.flex-col.rounded-sm {:style {:width "100%"}}
[:div.flex.flex-row
[:div.flex.flex-row.items-center.mr-2.ml-1 {:style {:height 24}}
[:span.bullet-container.cursor
[:span.bullet]]]
[:div.flex.flex-1 {:on-click #(editor-handler/insert-first-page-block-if-not-exists! page-name)}
[:span.opacity-50
"Click here to edit..."]]]])

(rum/defc page-blocks-cp < rum/reactive
db-mixins/query
[repo page-e {:keys [sidebar? preview?] :as config}]
Expand All @@ -98,34 +109,24 @@
journal? (db/journal-page? page-name)
block? (util/uuid-string? page-name)
block-id (and block? (uuid page-name))
page-empty? (and (not block?) (db/page-empty? repo (:db/id page-e)))
page-e (if (and page-e (:db/id page-e))
{:db/id (:db/id page-e)}
page-e)

;; create an empty block if not exists
_ (when (and (not block?) (db/page-empty? repo (:db/id page-e)))
(let [empty-block {:block/uuid (db/new-block-id)
:block/left page-e
:block/format format
:block/content ""
:block/parent page-e
:block/unordered true
:block/page page-e}]
(when (db/page-empty? repo (:db/id page-e))
(db/transact! [empty-block]))))
raw-page-blocks (get-blocks repo page-name page-original-name block? block-id)
page-blocks raw-page-blocks
document-mode? (state/sub :document/mode?)
hiccup-config (merge
{:id (if block? (str block-id) page-name)
:block? block?
:editor-box editor/box
:page page
:document/mode? document-mode?}
config)
hiccup-config (common-handler/config-with-document-mode hiccup-config)
hiccup (block/->hiccup page-blocks hiccup-config {})]
(page-blocks-inner page-name page-blocks hiccup sidebar?))))
page-blocks (get-blocks repo page-name page-original-name block? block-id)]
(if (empty? page-blocks)
(dummy-block page-name)
(let [document-mode? (state/sub :document/mode?)
hiccup-config (merge
{:id (if block? (str block-id) page-name)
:block? block?
:editor-box editor/box
:page page
:document/mode? document-mode?}
config)
hiccup-config (common-handler/config-with-document-mode hiccup-config)
hiccup (block/->hiccup page-blocks hiccup-config {})]
(page-blocks-inner page-name page-blocks hiccup sidebar?))))))

(defn contents-page
[page]
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/components/search.cljs
Expand Up @@ -155,7 +155,7 @@
result (if config/publishing?
(concat pages files blocks)
(concat new-page pages files blocks))]
[:div.rounded-md.shadow-lg
[:div.rounded-md.shadow-lg.search-ac
{:style (merge
{:top 48
:left 32
Expand Down
4 changes: 4 additions & 0 deletions src/main/frontend/components/search.css
Expand Up @@ -4,6 +4,10 @@
}
}

.search-ac {
background-color: var(--ls-primary-background-color);
}

#search-wrapper svg {
color: var(--ls-search-icon-color, #9fa6b2);
opacity: 0.3;
Expand Down
6 changes: 3 additions & 3 deletions src/main/frontend/db/model.cljs
Expand Up @@ -477,7 +477,7 @@

(defn page-empty?
[repo page-id]
(zero? (get-page-blocks-count repo page-id)))
(empty? (:block/_parent (db-utils/entity repo page-id))))

(defn page-empty-or-dummy?
[repo page-id]
Expand Down Expand Up @@ -559,12 +559,12 @@

;; FIXME: alert
(defn- keep-only-one-file
[blocks parent]
[blocks]
(filter (fn [b] (= (:block/file b) (:block/file (first blocks)))) blocks))

(defn sort-by-left
[blocks parent]
(let [blocks (keep-only-one-file blocks parent)]
(let [blocks (keep-only-one-file blocks)]
(when (not= (count blocks) (count (set (map :block/left blocks))))
(let [duplicates (->> (map (comp :db/id :block/left) blocks)
frequencies
Expand Down
4 changes: 3 additions & 1 deletion src/main/frontend/format/block.cljs
Expand Up @@ -359,6 +359,7 @@
{:block/name (string/lower-case ref)}
ref)))
(remove vector?)
(remove nil?)
(distinct))]
(recur (rest blocks)
(conj acc (assoc block :block/path-refs path-ref-pages))
Expand Down Expand Up @@ -630,7 +631,8 @@
(map :db/id))
{:block/keys [refs]} new-block
ref-pages (filter :block/name refs)
path-ref-pages (concat ref-pages parent-refs [(:db/id page)])
path-ref-pages (->> (concat ref-pages parent-refs [(:db/id page)])
(remove nil?))
block (merge
block
new-block
Expand Down

0 comments on commit 870d46e

Please sign in to comment.