Skip to content

Commit 8b14426

Browse files
committed
refactor: unify heading UX for both markdown and org mode
1 parent 9c7b4ea commit 8b14426

File tree

14 files changed

+124
-96
lines changed

14 files changed

+124
-96
lines changed

deps/db/src/logseq/db/schema.cljs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@
6262
;; first block that's not a heading or unordered list
6363
:block/pre-block? {}
6464

65-
;; heading's level (the block must be a heading)
66-
:block/heading-level {}
67-
6865
;; scheduled day
6966
:block/scheduled {}
7067

@@ -116,7 +113,6 @@
116113
:block/deadline
117114
:block/repeated?
118115
:block/pre-block?
119-
:block/heading-level
120116
:block/type
121117
:block/properties
122118
:block/properties-order

deps/graph-parser/src/logseq/graph_parser/block.cljs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,16 @@
541541
blocks)]
542542
(with-path-refs blocks)))
543543

544+
(defn- with-heading-property
545+
[properties markdown-heading? size]
546+
(let [properties (if markdown-heading?
547+
(assoc properties :heading size)
548+
properties)]
549+
(if (true? (:heading properties))
550+
;; default-level 2
551+
(assoc properties :heading 2)
552+
properties)))
553+
544554
(defn- construct-block
545555
[block properties timestamps body encoded-content format pos-meta with-id? {:keys [block-pattern supported-formats db date-formatter]}]
546556
(let [id (get-custom-id-or-new-id properties)
@@ -551,18 +561,17 @@
551561
markdown-heading? (and (:size block) (= :markdown format))
552562
block (if markdown-heading?
553563
(assoc block
554-
:type :heading
555-
:level (if unordered? (:level block) 1)
556-
:heading-level (or (:size block) 6))
564+
:level (if unordered? (:level block) 1))
557565
block)
558566
block (cond->
559-
(assoc block
560-
:uuid id
561-
:refs ref-pages-in-properties
562-
:format format
563-
:meta pos-meta)
564-
(seq (:properties properties))
565-
(assoc :properties (:properties properties)
567+
(-> (assoc block
568+
:uuid id
569+
:refs ref-pages-in-properties
570+
:format format
571+
:meta pos-meta)
572+
(dissoc :size))
573+
(or (seq (:properties properties)) markdown-heading?)
574+
(assoc :properties (with-heading-property (:properties properties) markdown-heading? (:size block))
566575
:properties-text-values (:properties-text-values properties)
567576
:properties-order (vec (:properties-order properties)))
568577

deps/graph-parser/src/logseq/graph_parser/test/docs_graph_helper.cljs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,9 @@
115115
:block/priority 4
116116
:block/deadline 1
117117
:block/collapsed? 22
118-
:block/heading-level 60
119118
:block/repeated? 1}
120119
(->> [:block/scheduled :block/priority :block/deadline :block/collapsed?
121-
:block/heading-level :block/repeated?]
120+
:block/repeated?]
122121
(map (fn [attr]
123122
[attr
124123
(ffirst (d/q [:find (list 'count '?b) :where ['?b attr]]

src/main/frontend/commands.cljs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@
132132
[:editor/set-heading heading]
133133
[:editor/move-cursor-to-end]])
134134

135-
(defn- markdown-headings
135+
(defn- headings
136136
[]
137-
(let [format (state/get-preferred-format)]
138-
(when (= (name format) "markdown")
139-
(mapv (fn [level]
140-
(let [heading (str "h" level)]
141-
[heading (->heading (apply str (repeat level "#")))])) (range 1 7)))))
137+
(mapv (fn [level]
138+
(let [heading (str "h" level)]
139+
[heading (->heading level)])) (range 1 7)))
142140

143141
(defonce *matched-commands (atom nil))
144142
(defonce *initial-commands (atom nil))
@@ -241,7 +239,7 @@
241239
;; ["Upload an image" [[:editor/click-hidden-file-input :id]]]
242240
)]
243241

244-
(markdown-headings)
242+
(headings)
245243

246244
;; time & date
247245

@@ -593,19 +591,33 @@
593591
(property/goto-properties-end format current-input)
594592
(cursor/move-cursor-backward current-input 3)))))
595593

594+
(defonce markdown-heading-pattern #"^#+\s+")
595+
(defn set-markdown-heading
596+
[content heading]
597+
(let [heading-str (apply str (repeat heading "#"))]
598+
(if (util/safe-re-find markdown-heading-pattern content)
599+
(string/replace-first content
600+
markdown-heading-pattern
601+
(str heading-str " "))
602+
(str heading-str " " (string/triml content)))))
603+
604+
(defn clear-markdown-heading
605+
[content]
606+
[:pre (string? content)]
607+
(string/replace-first content
608+
markdown-heading-pattern
609+
""))
610+
596611
(defmethod handle-step :editor/set-heading [[_ heading]]
597612
(when-let [input-id (state/get-edit-input-id)]
598613
(when-let [current-input (gdom/getElement input-id)]
599-
(let [edit-content (gobj/get current-input "value")
600-
heading-pattern #"^#+\s+"
601-
new-value (cond
602-
(util/safe-re-find heading-pattern edit-content)
603-
(string/replace-first edit-content
604-
heading-pattern
605-
(str heading " "))
606-
:else
607-
(str heading " " (string/triml edit-content)))]
608-
(state/set-edit-content! input-id new-value)))))
614+
(let [current-block (state/get-edit-block)
615+
format (:block/format current-block)]
616+
(if (= format :markdown)
617+
(let [edit-content (gobj/get current-input "value")
618+
new-content (set-markdown-heading edit-content heading)]
619+
(state/set-edit-content! input-id new-content))
620+
(state/pub-event! [:editor/set-org-mode-heading current-block heading]))))))
609621

610622
(defmethod handle-step :editor/search-page [[_]]
611623
(state/set-editor-action! :page-search))

src/main/frontend/components/block.cljs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,7 @@
18391839
(declare block-content)
18401840

18411841
(defn build-block-title
1842-
[config {:block/keys [title marker pre-block? properties level heading-level]
1842+
[config {:block/keys [title marker pre-block? properties]
18431843
:as t}]
18441844
(let [config (assoc config :block t)
18451845
slide? (boolean (:slide? config))
@@ -1856,14 +1856,9 @@
18561856
priority (priority-cp t)
18571857
tags (block-tags-cp t)
18581858
bg-color (:background-color properties)
1859-
heading-level (or (and heading-level
1860-
(<= heading-level 6)
1861-
heading-level)
1862-
(and (get properties :heading)
1863-
(<= level 6)
1864-
level))
1865-
elem (if heading-level
1866-
(keyword (str "h" heading-level
1859+
heading (:heading properties)
1860+
elem (if heading
1861+
(keyword (str "h" heading
18671862
(when block-ref? ".inline")))
18681863
:span.inline)]
18691864
(->elem
@@ -2289,7 +2284,7 @@
22892284

22902285
(rum/defcs block-content-or-editor < rum/reactive
22912286
(rum/local true ::hide-block-refs?)
2292-
[state config {:block/keys [uuid format] :as block} edit-input-id block-id heading-level edit?]
2287+
[state config {:block/keys [uuid format] :as block} edit-input-id block-id edit?]
22932288
(let [*hide-block-refs? (get state ::hide-block-refs?)
22942289
hide-block-refs? @*hide-block-refs?
22952290
editor-box (get config :editor-box)
@@ -2307,7 +2302,6 @@
23072302
:block-id uuid
23082303
:block-parent-id block-id
23092304
:format format
2310-
:heading-level heading-level
23112305
:on-hide (fn [value event]
23122306
(when (= event :esc)
23132307
(editor-handler/save-block! (editor-handler/get-state) value)
@@ -2574,7 +2568,7 @@
25742568
block (if ref?
25752569
(merge block (db/pull-block (:db/id block)))
25762570
block)
2577-
{:block/keys [uuid children pre-block? top? refs heading-level level format content properties]} block
2571+
{:block/keys [uuid children pre-block? top? refs level format content properties]} block
25782572
config (if navigated? (assoc config :id (str navigating-block)) config)
25792573
block (merge block (block/parse-title-and-body uuid format pre-block? content))
25802574
blocks-container-id (:blocks-container-id config)
@@ -2583,7 +2577,7 @@
25832577
config (if (nil? (:query-result config))
25842578
(assoc config :query-result (atom nil))
25852579
config)
2586-
heading? (or (:heading properties) (and heading-level (<= heading-level 6)))
2580+
heading? (:heading properties)
25872581
*control-show? (get state ::control-show?)
25882582
db-collapsed? (util/collapsed? block)
25892583
collapsed? (cond
@@ -2665,7 +2659,7 @@
26652659
(when @*show-left-menu?
26662660
(block-left-menu config block))
26672661

2668-
(block-content-or-editor config block edit-input-id block-id heading-level edit?)
2662+
(block-content-or-editor config block edit-input-id block-id edit?)
26692663

26702664
(when @*show-right-menu?
26712665
(block-right-menu config block edit?))]

src/main/frontend/components/content.cljs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
:on-click editor-handler/copy-block-embeds}
8585
"Copy block embeds"
8686
nil)
87-
87+
8888
[:hr.menu-separator]
8989

9090
(ui/menu-link
@@ -100,8 +100,7 @@
100100
"#787f97"
101101
"#978626"
102102
"#49767b"
103-
"#264c9b"
104-
"#793e3e"])
103+
"#264c9b"])
105104

106105
(defonce *template-including-parent? (atom nil))
107106

@@ -163,22 +162,36 @@
163162
(rum/defc ^:large-vars/cleanup-todo block-context-menu-content
164163
[_target block-id]
165164
(when-let [block (db/entity [:block/uuid block-id])]
166-
(let [properties (:block/properties block)
167-
heading? (true? (:heading properties))]
165+
(let [format (:block/format block)]
168166
[:.menu-links-wrapper
169-
[:div.flex-row.flex.justify-between.pb-2.pt-1.px-2
170-
[:div.flex-row.flex.justify-between
167+
[:div.flex.flex-row.justify-between.pb-2.pt-1.px-2.items-center
168+
[:div.flex.flex-row.justify-between.flex-1
171169
(for [color block-background-colors]
172170
[:a.m-2.shadow-sm
173171
{:on-click (fn [_e]
174172
(editor-handler/set-block-property! block-id "background-color" color))}
175-
[:div.heading-bg {:style {:background-color color}}]])
176-
[:a.m-2.shadow-sm
177-
{:title (t :remove-background)
178-
:on-click (fn [_e]
179-
(editor-handler/remove-block-property! block-id "background-color"))}
180-
[:div.heading-bg.remove "-"]]]]
181-
173+
[:div.heading-bg {:style {:background-color color}}]])]
174+
[:a.m-2.shadow-sm
175+
{:title (t :remove-background)
176+
:on-click (fn [_e]
177+
(editor-handler/remove-block-property! block-id "background-color"))}
178+
[:div.heading-bg.remove "-"]]]
179+
180+
[:div.flex.flex-row.justify-between.pb-2.pt-1.px-2.items-center
181+
[:div.flex.flex-row.justify-between.flex-1
182+
(for [i (range 1 7)]
183+
(ui/button
184+
(str "H" i)
185+
:on-click (fn [_e]
186+
(editor-handler/set-heading! block-id format i))
187+
:intent "link"
188+
:small? true))]
189+
[:a.m-2
190+
{:title (t :remove-heading)
191+
:on-click (fn [_e]
192+
(editor-handler/remove-heading! block-id format))}
193+
[:div.heading-bg.remove "-"]]]
194+
182195
[:hr.menu-separator]
183196

184197
(ui/menu-link
@@ -232,17 +245,6 @@
232245

233246
[:hr.menu-separator]
234247

235-
(ui/menu-link
236-
{:key "Convert heading"
237-
:on-click (fn [_e]
238-
(if heading?
239-
(editor-handler/remove-block-property! block-id :heading)
240-
(editor-handler/set-block-property! block-id :heading true)))}
241-
(if heading?
242-
"Convert back to a block"
243-
"Convert to a heading")
244-
nil)
245-
246248
(block-template block-id)
247249

248250
(if (srs/card-block? block)

src/main/frontend/components/editor.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@
568568

569569
(rum/defcs box < rum/reactive
570570
{:init (fn [state]
571-
(assoc state ::heading-level (:heading-level (first (:rum/args state)))
571+
(assoc state
572572
::id (str (random-uuid))))
573573
:did-mount (fn [state]
574574
(state/set-editor-args! (:rum/args state))

src/main/frontend/db/model.cljs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
:block/created-at
5555
:block/updated-at
5656
:block/file
57-
:block/heading-level
5857
{:block/page [:db/id :block/name :block/original-name :block/journal-day]}
5958
{:block/_parent ...}])
6059

src/main/frontend/handler/editor.cljs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,10 @@
316316
(block/parse-title-and-body uuid format pre-block? (:block/content block)))
317317
properties (:block/properties block)
318318
real-content (:block/content block)
319-
content (if (and (seq properties) real-content (not= real-content content))
320-
(property/with-built-in-properties properties content format)
321-
content)
319+
content (let [properties (if (= format :markdown) (dissoc properties :heading) properties)]
320+
(if (and (seq properties) real-content (not= real-content content))
321+
(property/with-built-in-properties properties content format)
322+
content))
322323
content (drawer/with-logbook block content)
323324
content (with-timetracking block content)
324325
first-block? (= left page)
@@ -1939,6 +1940,7 @@
19391940
(property/insert-properties format content props))
19401941
ast (mldoc/->edn content* (gp-mldoc/default-config format))
19411942
blocks (block/extract-blocks ast content* format {})
1943+
_ (prn {:block (first blocks)})
19421944
fst-block (first blocks)
19431945
fst-block (if (and keep-uuid? (uuid? (:uuid block)))
19441946
(assoc fst-block :block/uuid (:uuid block))
@@ -3465,3 +3467,21 @@
34653467
;; has children
34663468
(first (:block/_parent (db/entity (:db/id block)))))
34673469
(util/collapsed? block)))
3470+
3471+
(defn set-heading!
3472+
[block-id format heading]
3473+
(if (= format :markdown)
3474+
(let [repo (state/get-current-repo)
3475+
block (db/entity [:block/uuid block-id])
3476+
content' (commands/set-markdown-heading (:block/content block) heading)]
3477+
(save-block! repo block-id content'))
3478+
(set-block-property! block-id "heading" heading)))
3479+
3480+
(defn remove-heading!
3481+
[block-id format]
3482+
(remove-block-property! block-id "heading")
3483+
(when (= format :markdown)
3484+
(let [repo (state/get-current-repo)
3485+
block (db/entity [:block/uuid block-id])
3486+
content' (commands/clear-markdown-heading (:block/content block))]
3487+
(save-block! repo block-id content'))))

src/main/frontend/handler/events.cljs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,10 @@
611611
template
612612
{:target page}))))))
613613

614+
(defmethod handle :editor/set-org-mode-heading [[_ block heading]]
615+
(when-let [id (:block/uuid block)]
616+
(editor-handler/set-heading! id :org heading)))
617+
614618
(defmethod handle :file-sync-graph/restore-file [[_ graph page-entity content]]
615619
(when (db/get-db graph)
616620
(let [file (:block/file page-entity)]

src/main/frontend/handler/export.cljs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,6 @@
482482
[:block/id
483483
:block/page-name
484484
:block/properties
485-
:block/heading-level
486485
:block/format
487486
:block/children
488487
:block/content]))})

src/main/frontend/mobile/action_bar.cljs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@
5252
(.scrollBy (util/app-scroll-container-node) #js {:top (- 10 delta)})))
5353
[:div.action-bar
5454
[:div.action-bar-commands
55-
(when-not (= (:block/format block) :org)
56-
(action-command "heading" "Heading"
57-
#(let [properties (:block/properties block)
58-
heading? (true? (:heading properties))]
59-
(if heading?
60-
(editor-handler/remove-block-property! uuid :heading)
61-
(editor-handler/set-block-property! uuid :heading true)))))
6255
(action-command "infinity" "Card" #(srs/make-block-a-card! (:block/uuid block)))
6356
(action-command "copy" "Copy" #(editor-handler/copy-selection-blocks false))
6457
(action-command "cut" "Cut" #(editor-handler/cut-selection-blocks true))

0 commit comments

Comments
 (0)