refactor selection shortcut

This commit is contained in:
Weihua Lu
2021-04-09 18:09:20 +08:00
committed by Tienson Qin
parent 0b1f2f7cc4
commit d187b767b5
4 changed files with 58 additions and 95 deletions

View File

@@ -797,10 +797,13 @@
(util/copy-to-clipboard! (tap-clipboard block-id))))
(defn exit-editing-and-set-selected-blocks!
[blocks]
(util/clear-selection!)
(state/clear-edit!)
(state/set-selection-blocks! blocks))
([blocks]
(exit-editing-and-set-selected-blocks! blocks :down))
([blocks direction]
(util/clear-selection!)
(state/clear-edit!)
(state/set-selection-blocks! blocks direction)
(util/select-highlight! blocks)))
(defn select-all-blocks!
[]
@@ -808,8 +811,6 @@
(let [input (gdom/getElement current-input-id)
blocks-container (util/rec-get-blocks-container input)
blocks (dom/by-class blocks-container "ls-block")]
(doseq [block blocks]
(dom/add-class! block "selected noselect"))
(exit-editing-and-set-selected-blocks! blocks))))
(defn- get-selected-blocks-with-children
@@ -827,10 +828,8 @@
ids (->> (distinct (map #(when-let [id (dom/attr % "blockid")]
(uuid id)) blocks))
(remove nil?))
up? (state/selection-up?)
content (some->> (db/get-blocks-contents repo ids)
(map :block/content))
content (if (false? up?) (reverse content) content)
content (string/join "" content)]
(when-not (string/blank? content)
(common-handler/copy-to-clipboard-without-id-property! content)))))
@@ -934,13 +933,11 @@
(common-handler/copy-to-clipboard-without-id-property! content)
(delete-block-aux! block false))))
(defonce select-start-block-state (atom nil))
(defn clear-last-selected-block!
[]
(let [first-block (state/pop-selection-block!)]
(dom/remove-class! first-block "selected")
(dom/remove-class! first-block "noselect")))
(let [block (state/drop-last-selection-block!)]
(dom/remove-class! block "selected")
(dom/remove-class! block "noselect")))
(defn input-start-or-end?
([input]
@@ -958,46 +955,36 @@
[end-block]
(when-let [start-block (:selection/start-block @state/state)]
(clear-selection! nil)
(let [blocks (util/get-nodes-between-two-nodes start-block end-block "ls-block")]
(doseq [block blocks]
(dom/add-class! block "selected noselect"))
(let [blocks (util/get-nodes-between-two-nodes start-block end-block "ls-block")
direction (util/get-direction-between-two-nodes start-block end-block "ls-block")]
(exit-editing-and-set-selected-blocks! blocks))))
(defn on-select-block
[state e up?]
(when (and
(gobj/get e "shiftKey")
(not (gobj/get e "altKey"))
(or (state/in-selection-mode?)
(when-let [input-id (state/get-edit-input-id)]
(when-let [input (gdom/getElement input-id)]
(input-start-or-end? input up?)))))
(state/clear-edit!)
(let [{:keys [id block-id block block-parent-id dummy? value pos format] :as block-state} @select-start-block-state
element (gdom/getElement block-parent-id)
selected-blocks (state/get-selection-blocks)
selected-blocks-count (count selected-blocks)
first-block (first selected-blocks)
selection-up? (state/selection-up?)]
(when block-id
(util/stop e)
(when-let [element (if-not (state/in-selection-mode?)
element
(let [f (if up? util/get-prev-block util/get-next-block)]
(f first-block)))]
(if (and (not (nil? selection-up?)) (not= up? selection-up?))
(cond
(>= selected-blocks-count 2) ; back to the start block
(do
(when (= 2 selected-blocks-count) (state/set-selection-up! nil))
(clear-last-selected-block!))
[direction]
(fn [e]
(cond
;; when editing, quit editing and select current block
(state/editing?)
(exit-editing-and-set-selected-blocks! [(gdom/getElement (state/get-editing-block-dom-id))])
:else
nil)
(do
(util/clear-selection!)
(state/clear-edit!)
(state/conj-selection-block! element up?))))))))
;; when selection and one block selected, select next block
(and (state/in-selection-mode?) (== 1 (count (state/get-selection-blocks))))
(let [f (if (= :up direction) util/get-prev-block util/get-next-block)
element (f (first (state/get-selection-blocks)))]
(when element
(state/conj-selection-block! element direction)))
;; if same direction, keep conj on same direction
(and (state/in-selection-mode?) (= direction (state/get-selection-direction)))
(let [f (if (= :up direction) util/get-prev-block util/get-next-block)
element (f (last (state/get-selection-blocks)))]
(when element
(state/conj-selection-block! element direction)))
;; if different direction, keep clear until one left
(state/in-selection-mode?)
(clear-last-selected-block!))))
(defn save-block-aux!
[block value format opts]