refactor: selection state

pull/1671/head
Weihua Lu 2021-04-16 11:49:03 +08:00
parent c7efda6c62
commit d05c540c15
3 changed files with 12 additions and 8 deletions

View File

@ -268,8 +268,7 @@
:left (str client-x "px") :left (str client-x "px")
:top (str (+ scroll-y client-y) "px"))))) :top (str (+ scroll-y client-y) "px")))))
(and (state/in-selection-mode?) (state/selection?)
(seq (state/get-selection-blocks)))
(do (do
(util/stop e) (util/stop e)
(let [client-x (gobj/get e "clientX") (let [client-x (gobj/get e "clientX")

View File

@ -1082,21 +1082,21 @@
(exit-editing-and-set-selected-blocks! [(gdom/getElement (state/get-editing-block-dom-id))]) (exit-editing-and-set-selected-blocks! [(gdom/getElement (state/get-editing-block-dom-id))])
;; when selection and one block selected, select next block ;; when selection and one block selected, select next block
(and (state/in-selection-mode?) (== 1 (count (state/get-selection-blocks)))) (and (state/selection?) (== 1 (count (state/get-selection-blocks))))
(let [f (if (= :up direction) util/get-prev-block util/get-next-block) (let [f (if (= :up direction) util/get-prev-block util/get-next-block)
element (f (first (state/get-selection-blocks)))] element (f (first (state/get-selection-blocks)))]
(when element (when element
(state/conj-selection-block! element direction))) (state/conj-selection-block! element direction)))
;; if same direction, keep conj on same direction ;; if same direction, keep conj on same direction
(and (state/in-selection-mode?) (= direction (state/get-selection-direction))) (and (state/selection?) (= direction (state/get-selection-direction)))
(let [f (if (= :up direction) util/get-prev-block util/get-next-block) (let [f (if (= :up direction) util/get-prev-block util/get-next-block)
element (f (last (state/get-selection-blocks)))] element (f (last (state/get-selection-blocks)))]
(when element (when element
(state/conj-selection-block! element direction))) (state/conj-selection-block! element direction)))
;; if different direction, keep clear until one left ;; if different direction, keep clear until one left
(state/in-selection-mode?) (state/selection?)
(clear-last-selected-block!)))) (clear-last-selected-block!))))
(defn save-block-aux! (defn save-block-aux!
@ -2384,7 +2384,7 @@
* when in edit mode with text selected, copy selected text as normal" * when in edit mode with text selected, copy selected text as normal"
[e] [e]
(cond (cond
(and (state/in-selection-mode?) (seq (state/get-selection-blocks))) (state/selection?)
(shortcut-copy-selection e) (shortcut-copy-selection e)
(state/editing?) (state/editing?)
@ -2404,7 +2404,7 @@
[state-fn] [state-fn]
(fn [e] (fn [e]
(cond (cond
(and (state/in-selection-mode?) (seq (state/get-selection-blocks))) (state/selection?)
(shortcut-cut-selection e) (shortcut-cut-selection e)
(state/editing?) (state/editing?)
@ -2414,7 +2414,7 @@
[state-fn] [state-fn]
(fn [e] (fn [e]
(cond (cond
(and (state/in-selection-mode?) (seq (state/get-selection-blocks))) (state/selection?)
(shortcut-delete-selection e) (shortcut-delete-selection e)
(state/editing?) (state/editing?)

View File

@ -549,6 +549,11 @@
[] []
(:selection/mode @state)) (:selection/mode @state))
(defn selection?
"True sense of selection mode with valid selected block"
[]
(and (in-selection-mode?) (seq (get-selection-blocks))))
(defn conj-selection-block! (defn conj-selection-block!
[block direction] [block direction]
(dom/add-class! block "selected noselect") (dom/add-class! block "selected noselect")