diff --git a/src/main/frontend/handler/editor.cljs b/src/main/frontend/handler/editor.cljs index 282595ad6d..0787eb1df5 100644 --- a/src/main/frontend/handler/editor.cljs +++ b/src/main/frontend/handler/editor.cljs @@ -472,7 +472,7 @@ {:keys [ok-handler] :as opts}] (let [input (gdom/getElement (state/get-edit-input-id)) - pos (util/get-input-pos input) + pos (cursor/pos input) repo (or repo (state/get-current-repo)) [fst-block-text snd-block-text] (compute-fst-snd-block-text value pos) current-block (assoc block :block/content snd-block-text) @@ -503,7 +503,7 @@ :as opts}] (let [block-self? (block-self-alone-when-insert? config uuid) input (gdom/getElement (state/get-edit-input-id)) - pos (util/get-input-pos input) + pos (cursor/pos input) repo (or repo (state/get-current-repo)) [fst-block-text snd-block-text] (compute-fst-snd-block-text value pos) current-block (assoc block :block/content fst-block-text) @@ -599,7 +599,7 @@ [properties value] (with-timetracking-properties block value) block-self? (block-self-alone-when-insert? config block-id) input (gdom/getElement (state/get-edit-input-id)) - pos (util/get-input-pos input) + pos (cursor/pos input) repo (or repo (state/get-current-repo)) [fst-block-text snd-block-text] (compute-fst-snd-block-text value pos) insert-fn (match (mapv boolean [block-self? (seq fst-block-text) (seq snd-block-text)]) @@ -745,7 +745,7 @@ [(marker/add-or-update-marker (string/triml content) format marker) marker])) new-content (string/triml new-content)] (let [new-pos (commands/compute-pos-delta-when-change-marker - current-input content new-content marker (util/get-input-pos current-input))] + current-input content new-content marker (cursor/pos current-input))] (state/set-edit-content! edit-input-id new-content) (cursor/move-cursor-to current-input new-pos))))) @@ -1080,7 +1080,7 @@ (when-let [id (:block/uuid block)] (when-let [edit-id (state/get-edit-input-id)] (when-let [input (gdom/getElement edit-id)] - (when-let [pos (util/get-input-pos input)] + (when-let [pos (cursor/pos input)] (let [value (gobj/get input "value") page-pattern #"\[\[([^\]]+)]]" block-pattern #"\(\(([^\)]+)\)\)" @@ -1545,7 +1545,7 @@ [input before after] (when input (let [value (gobj/get input "value") - pos (util/get-input-pos input) + pos (cursor/pos input) start-pos (if (= :start before) 0 (- pos (count before))) end-pos (if (= :end after) (count value) (+ pos (count after)))] (when (>= (count value) end-pos) @@ -1598,7 +1598,7 @@ [input] (try (let [edit-content (or (gobj/get input "value") "") - pos (util/get-input-pos input) + pos (cursor/pos input) last-slash-caret-pos (:pos @*slash-caret-pos) last-command (and last-slash-caret-pos (subs edit-content last-slash-caret-pos pos))] (when (> pos 0) @@ -1615,7 +1615,7 @@ [input] (try (let [edit-content (gobj/get input "value") - pos (util/get-input-pos input) + pos (cursor/pos input) last-command (subs edit-content (:pos @*angle-bracket-caret-pos) pos)] @@ -1645,7 +1645,7 @@ (defn get-previous-input-char [input] - (when-let [pos (util/get-input-pos input)] + (when-let [pos (cursor/pos input)] (let [value (gobj/get input "value")] (when (and (>= (count value) pos) (>= pos 1)) @@ -1653,7 +1653,7 @@ (defn get-previous-input-chars [input length] - (when-let [pos (util/get-input-pos input)] + (when-let [pos (cursor/pos input)] (let [value (gobj/get input "value")] (when (and (>= (count value) pos) (>= pos 1)) @@ -1661,7 +1661,7 @@ (defn get-current-input-char [input] - (when-let [pos (util/get-input-pos input)] + (when-let [pos (cursor/pos input)] (let [value (gobj/get input "value")] (when (and (>= (count value) (inc pos)) (>= pos 1)) @@ -1920,7 +1920,7 @@ [] (when-let [editing-block (db/pull (:db/id (state/get-edit-block)))] (let [input (gdom/getElement (state/get-edit-input-id)) - pos (util/get-input-pos input) + pos (cursor/pos input) value (:value (get-state)) [fst-block-text snd-block-text] (compute-fst-snd-block-text value pos) parent (:db/id (:block/parent editing-block)) @@ -2303,8 +2303,8 @@ (cursor/move-cursor-to input selected-start) (cursor/move-cursor-to input selected-end)) - (or (and left? (util/input-start? input)) - (and right? (util/input-end? input))) + (or (and left? (cursor/start? input)) + (and right? (cursor/end? input))) (move-to-block-when-cross-boundrary direction) :else @@ -2319,7 +2319,7 @@ (defn- delete-concat [current-block] (let [input-id (state/get-edit-input-id) ^js input (state/get-input) - current-pos (util/get-input-pos input) + current-pos (cursor/pos input) value (gobj/get input "value") repo (state/get-current-repo) right (outliner-core/get-right-node (outliner-core/block current-block)) @@ -2345,7 +2345,7 @@ (defn keydown-delete-handler [e] (let [^js input (state/get-input) - current-pos (util/get-input-pos input) + current-pos (cursor/pos input) value (gobj/get input "value") end? (= current-pos (count value)) current-block (state/get-edit-block) @@ -2488,7 +2488,7 @@ value (gobj/get input "value") ctrlKey (gobj/get e "ctrlKey") metaKey (gobj/get e "metaKey") - pos (util/get-input-pos input)] + pos (cursor/pos input)] (cond (or ctrlKey metaKey) nil @@ -2562,7 +2562,7 @@ (fn [e key-code] (let [k (gobj/get e "key") format (:format (get-state)) - current-pos (util/get-input-pos input) + current-pos (cursor/pos input) value (gobj/get input "value") c (util/nth-safe value (dec current-pos))] (when-not (state/get-editor-show-input) diff --git a/src/main/frontend/state.cljs b/src/main/frontend/state.cljs index cb06f8e7c5..9e28121d86 100644 --- a/src/main/frontend/state.cljs +++ b/src/main/frontend/state.cljs @@ -712,7 +712,7 @@ (when container {:last-edit-block edit-block :container (gobj/get container "id") - :pos (util/get-input-pos (gdom/getElement edit-input-id))}))) + :pos (cursor/pos (gdom/getElement edit-input-id))}))) (defn set-editing! ([edit-input-id content block cursor-range] diff --git a/src/main/frontend/util.cljc b/src/main/frontend/util.cljc index cfd84daadf..c9b2764933 100644 --- a/src/main/frontend/util.cljc +++ b/src/main/frontend/util.cljc @@ -713,23 +713,6 @@ (catch js/Error _e nil)))) -#?(:cljs - (defn get-input-pos - [input] - (and input (.-selectionStart input)))) - -#?(:cljs - (defn input-start? - [input] - (and input (zero? (.-selectionStart input))))) - -#?(:cljs - (defn input-end? - [input] - (and input - (= (count (.-value input)) - (.-selectionStart input))))) - #?(:cljs (defn input-selected? [input] diff --git a/src/main/frontend/util/cursor.cljs b/src/main/frontend/util/cursor.cljs index 0834cbdd6b..a0c07dce15 100644 --- a/src/main/frontend/util/cursor.cljs +++ b/src/main/frontend/util/cursor.cljs @@ -22,6 +22,12 @@ int)}) (defn get-caret-pos + "Get caret offset position as well as input element rect. + + This function is only used by autocomplete command or up/down command + where offset position is needed. + + If you only need character position, use `pos` instead. Do NOT call this." [input] (let [pos (.-selectionStart input) rect (bean/->clj (.. input (getBoundingClientRect) (toJSON)))] @@ -44,6 +50,14 @@ (when input (.-selectionStart input))) +(defn start? [input] + (and input (zero? (.-selectionStart input)))) + +(defn end? [input] + (and input + (= (count (.-value input)) + (.-selectionStart input)))) + (defn move-cursor-to [input n] (.setSelectionRange input n n))