Skip to content

Commit

Permalink
feature: new keybinding for "toggle expand/collapse this block"
Browse files Browse the repository at this point in the history
Fixes issue logseq#9676
Binds to mod + ; by default.
  • Loading branch information
topherhunt authored and andelf committed Feb 20, 2024
1 parent ebb6d86 commit 2bbd1bf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3586,6 +3586,43 @@
(doseq [{:block/keys [uuid]} blocks-to-collapse]
(collapse-block! uuid))))))))))

(defn toggle-collapse!
([e] (toggle-collapse! e false))
([e clear-selection?]
(when e (util/stop e))
(cond
(state/editing?)
(when-let [block (state/get-edit-block)]
;; get-edit-block doesn't track the latest collapsed state, so we need to reload from db.
(let [block-id (:block/uuid block)
block (db/pull [:block/uuid block-id])]
(if (:block/collapsed? block)
(expand! e clear-selection?)
(collapse! e clear-selection?))))

(state/selection?)
(do
(let [block-ids (map #(-> % (dom/attr "blockid") uuid) (get-selected-blocks))
first-block-id (first block-ids)]
(when first-block-id
;; If multiple blocks are selected, they may not have all the same collapsed state.
;; For simplicity, use the first block's state to decide whether to collapse/expand all.
(let [first-block (db/pull [:block/uuid first-block-id])]
(if (:block/collapsed? first-block)
(doseq [block-id block-ids] (expand-block! block-id))
(doseq [block-id block-ids] (collapse-block! block-id))))))
(and clear-selection? (clear-selection!)))

(whiteboard?)
;; TODO: Looks like detecting the whiteboard selection's collapse state will take more work.
;; Leaving unimplemented for now.
nil

:else
;; If no block is being edited or selected, the "toggle" action doesn't make sense,
;; so we no-op here, unlike in the expand! & collapse! functions.
nil)))

(defn collapse-all!
([]
(collapse-all! nil {}))
Expand Down
5 changes: 5 additions & 0 deletions src/main/frontend/modules/shortcut/config.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@
:editor/collapse-block-children {:binding "mod+up"
:fn editor-handler/collapse!}

:editor/toggle-block-children {:binding "mod+;"
:fn editor-handler/toggle-collapse!}

:editor/indent {:binding "tab"
:fn (editor-handler/keydown-tab-handler :right)}

Expand Down Expand Up @@ -667,6 +670,7 @@
:editor/delete-selection
:editor/expand-block-children
:editor/collapse-block-children
:editor/toggle-block-children
:editor/indent
:editor/outdent
:editor/copy
Expand Down Expand Up @@ -782,6 +786,7 @@
:editor/right
:editor/collapse-block-children
:editor/expand-block-children
:editor/toggle-block-children
:editor/toggle-open-blocks
:go/backward
:go/forward
Expand Down
1 change: 1 addition & 0 deletions src/resources/dicts/en.edn
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@
:editor/delete-selection "Delete selected blocks"
:editor/expand-block-children "Expand"
:editor/collapse-block-children "Collapse"
:editor/toggle-block-children "Toggle expand/collapse"
:editor/indent "Indent block"
:editor/outdent "Outdent block"
:editor/copy "Copy (copies either selection, or block reference)"
Expand Down

0 comments on commit 2bbd1bf

Please sign in to comment.