From aaf856e803ff7539a8104af29fc32050c3b4177b Mon Sep 17 00:00:00 2001 From: Mega Yu Date: Tue, 25 Jul 2023 11:01:50 +0800 Subject: [PATCH] support mod+shift+left-click --- src/main/frontend/components/block.cljs | 110 ++++++++++++------------ src/main/frontend/handler/editor.cljs | 21 +++-- src/main/frontend/state.cljs | 21 +++-- 3 files changed, 83 insertions(+), 69 deletions(-) diff --git a/src/main/frontend/components/block.cljs b/src/main/frontend/components/block.cljs index 1b4df2c76..23ba38edc 100644 --- a/src/main/frontend/components/block.cljs +++ b/src/main/frontend/components/block.cljs @@ -2156,62 +2156,66 @@ shift? (gobj/get e "shiftKey") meta? (util/meta-key? e) forbidden-edit? (target-forbidden-edit? target)] - (when-not forbidden-edit? (.stopPropagation e)) - (if (and meta? - (not (state/get-edit-input-id)) - (not (dom/has-class? target "page-ref")) - (not= "A" (gobj/get target "tagName"))) - (do - (util/stop e) - (let [block-dom-element (gdom/getElement block-id)] - (if (some #(= block-dom-element %) (state/get-selection-blocks)) - (state/drop-selection-block! block-dom-element) - (state/conj-selection-block! block-dom-element :down))) - (if (empty? (state/get-selection-blocks)) - (state/clear-selection!) - (state/set-selection-start-block! block-id))) - (when (contains? #{1 0} button) - (when-not forbidden-edit? - (cond - (and shift? (state/get-selection-start-block-or-first)) - (do - (util/stop e) - (util/clear-selection!) - (editor-handler/highlight-selection-area! block-id)) + (when (and (not forbidden-edit?) (contains? #{1 0} button)) + (util/stop-propagation e) + (let [selection-blocks (state/get-selection-blocks) + starting-block (state/get-selection-start-block-or-first)] + (cond + (and meta? shift?) + (when-not (empty? selection-blocks) + (util/stop e) + (editor-handler/highlight-selection-area! block-id true)) - shift? - (do - (util/clear-selection!) - (state/set-selection-start-block! block-id)) + meta? + (do + (util/stop e) + (let [block-dom-element (gdom/getElement block-id)] + (if (some #(= block-dom-element %) selection-blocks) + (state/drop-selection-block! block-dom-element) + (state/conj-selection-block! block-dom-element :down))) + (if (empty? (state/get-selection-blocks)) + (state/clear-selection!) + (state/set-selection-start-block! block-id))) - :else - (do - (editor-handler/clear-selection!) - (editor-handler/unhighlight-blocks!) - (let [f #(let [block (or (db/pull [:block/uuid (:block/uuid block)]) block) - cursor-range (some-> (gdom/getElement block-id) - (dom/by-class "block-content-wrapper") - first - util/caret-range) - {:block/keys [content format]} block - content (->> content - (property/remove-built-in-properties format) - (drawer/remove-logbook))] - ;; save current editing block - (let [{:keys [value] :as state} (editor-handler/get-state)] - (editor-handler/save-block! state value)) - (state/set-editing! - edit-input-id - content - block - cursor-range - false))] - ;; wait a while for the value of the caret range - (if (util/ios?) - (f) - (js/setTimeout f 5)) + (and shift? starting-block) + (do + (util/stop e) + (util/clear-selection!) + (editor-handler/highlight-selection-area! block-id)) - (when block-id (state/set-selection-start-block! block-id))))))))))) + shift? + (do + (util/clear-selection!) + (state/set-selection-start-block! block-id)) + + :else + (do + (editor-handler/clear-selection!) + (editor-handler/unhighlight-blocks!) + (let [f #(let [block (or (db/pull [:block/uuid (:block/uuid block)]) block) + cursor-range (some-> (gdom/getElement block-id) + (dom/by-class "block-content-wrapper") + first + util/caret-range) + {:block/keys [content format]} block + content (->> content + (property/remove-built-in-properties format) + (drawer/remove-logbook))] + ;; save current editing block + (let [{:keys [value] :as state} (editor-handler/get-state)] + (editor-handler/save-block! state value)) + (state/set-editing! + edit-input-id + content + block + cursor-range + false))] + ;; wait a while for the value of the caret range + (if (util/ios?) + (f) + (js/setTimeout f 5)) + + (state/set-selection-start-block! block-id))))))))) (rum/defc dnd-separator-wrapper < rum/reactive [block block-id slide? top? block-content?] diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 9255336fa..c071fee7e 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -1226,14 +1226,19 @@ (delete-block-aux! block true)))) (defn highlight-selection-area! - [end-block] - (when-let [start-block (state/get-selection-start-block-or-first)] - (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") - blocks (if (= :up direction) - (reverse blocks) - blocks)] - (state/exit-editing-and-set-selected-blocks! blocks direction)))) + ([end-block] + (highlight-selection-area! end-block false)) + ([end-block append?] + (when-let [start-block (state/get-selection-start-block-or-first)] + (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") + blocks (if (= :up direction) + (reverse blocks) + blocks)] + (if append? + (do (state/clear-edit!) + (state/conj-selection-block! blocks direction)) + (state/exit-editing-and-set-selected-blocks! blocks direction)))))) (defn- select-block-up-down [direction] diff --git a/src/main/frontend/state.cljs b/src/main/frontend/state.cljs index 8df34d1b0..c50d01403 100644 --- a/src/main/frontend/state.cljs +++ b/src/main/frontend/state.cljs @@ -1042,19 +1042,24 @@ Similar to re-frame subscriptions" (and (in-selection-mode?) (seq (get-selection-blocks)))) (defn conj-selection-block! - [block direction] - (swap! state assoc - :selection/mode true - :selection/blocks (-> (conj (vec (:selection/blocks @state)) block) - util/sort-by-height - vec) - :selection/direction direction)) + [block-or-blocks direction] + (let [selection-blocks (get-selection-blocks) + blocks (-> (if (sequential? block-or-blocks) + (apply conj selection-blocks block-or-blocks) + (conj selection-blocks block-or-blocks)) + distinct + util/sort-by-height + vec)] + (swap! state assoc + :selection/mode true + :selection/blocks blocks + :selection/direction direction))) (defn drop-selection-block! [block] (swap! state assoc :selection/mode true - :selection/blocks (-> (filter #(not= block %) (get-selection-blocks)) + :selection/blocks (-> (remove #(= block %) (get-selection-blocks)) util/sort-by-height vec)))