fix cursor position for command input

This commit is contained in:
Roman Volosovskyi 2017-02-23 15:54:48 +02:00 committed by Roman Volosovskyi
parent 99931c24b9
commit d4128397b9
2 changed files with 76 additions and 27 deletions

View File

@ -151,7 +151,8 @@
(-> db (-> db
(commands/set-command-input (or type :commands) command-key) (commands/set-command-input (or type :commands) command-key)
(assoc-in [:chats current-chat-id :command-input :content] cu/command-prefix) (assoc-in [:chats current-chat-id :command-input :content] cu/command-prefix)
(assoc :disable-input true))) (assoc :disable-input true)
(assoc :just-set-command? true)))
(register-handler :set-chat-command (register-handler :set-chat-command
[(after invoke-suggestions-handler!) [(after invoke-suggestions-handler!)

View File

@ -15,7 +15,9 @@
[status-im.accessibility-ids :as id] [status-im.accessibility-ids :as id]
[reagent.core :as r] [reagent.core :as r]
[clojure.string :as str] [clojure.string :as str]
[taoensso.timbre :as log])) [taoensso.timbre :as log]
[clojure.string :as s]
[status-im.commands.utils :refer [command-prefix]]))
(defn send-button [{:keys [on-press accessibility-label]}] (defn send-button [{:keys [on-press accessibility-label]}]
[touchable-highlight {:on-press on-press [touchable-highlight {:on-press on-press
@ -40,9 +42,15 @@
:multiline true :multiline true
:editable (not disable?)}) :editable (not disable?)})
(defn command-input-options [{:keys [icon-width disable? sending-disabled?]}] (defn command-input-options [{:keys [icon-width disable? sending-disabled? flag]}]
{:style (st-response/command-input icon-width disable?) {:style (st-response/command-input icon-width disable?)
:on-change-text (when-not disable? command/set-input-message) :on-change-text (when-not disable? (fn [text]
;; update flag only when the first
;; character was added to commands input
(when (and (s/starts-with? text command-prefix)
(= (inc (count command-prefix)) (count text)))
(reset! flag true))
(command/set-input-message text)))
:on-submit-editing (fn [] :on-submit-editing (fn []
(when-not sending-disabled? (when-not sending-disabled?
(dispatch [:send-command!]))) (dispatch [:send-command!])))
@ -62,6 +70,21 @@
options) options)
(assoc options :placeholder "")))) (assoc options :placeholder ""))))
(defn get-selection
[{:keys [focused? flag input-command command? just-set?]}]
(cond (and command?
(= input-command command-prefix)
focused?
(not just-set?))
{:start 2
:end 2}
(and focused? flag)
{:start 3
:end 3}
:else nil))
(defview message-input [set-layout-size] (defview message-input [set-layout-size]
[input-message [:get-chat-input-text] [input-message [:get-chat-input-text]
input-command [:get-chat-command-content] input-command [:get-chat-command-content]
@ -72,24 +95,49 @@
command? [:command?] command? [:command?]
parameter [:get-command-parameter] parameter [:get-command-parameter]
type [:command-type] type [:command-type]
sending-disabled? [:chat-ui-props :sending-disabled?]] just-set? [:get :just-set-command?]
sending-disabled? [:chat-ui-props :sending-disabled?]
state {:input (atom nil)
:focused? (atom nil)
:3-symbols-flag (r/atom false)}]
{:component-did-update (fn []
(when just-set?
(dispatch [:set :just-set-command? false]))
(when @(:3-symbols-flag state)
(reset! (:3-symbols-flag state) false))
(when (and command?
(not (:fullscreen command)))
(.focus @(:input state))))}
(do
@(:3-symbols-flag state)
[text-input [text-input
(merge (merge
(if command? (if command?
(command-input-options {:icon-width icon-width (command-input-options {:icon-width icon-width
:disable? disable? :disable? disable?
:sendind-disabled? sending-disabled?}) :sendind-disabled? sending-disabled?
:flag (:3-symbols-flag state)})
(plain-input-options {:set-layout-size-fn set-layout-size (plain-input-options {:set-layout-size-fn set-layout-size
:disable? (or disable? (not active?))})) :disable? (or disable? (not active?))}))
{:placeholder-text-color :#c0c5c9 {:placeholder-text-color :#c0c5c9
:auto-focus (when command? :onFocus #(reset! (:focused? state) true)
(not (:fullscreen command))) :onBlur #(reset! (:focused? state) false)
:auto-focus (when command? (not (:fullscreen command)))
:ref #(reset! (:input state) %)
:accessibility-label id/chat-message-input :accessibility-label id/chat-message-input
;; for some reason app crashes when this property is not nil and
;; input is not focused
:selection (get-selection {:focused? @(:focused? state)
:flag @(:3-symbols-flag state)
:input-command input-command
:command? command?
:just-set? just-set?})
:default-value (if command? :default-value (if command?
(or input-command "") (or input-command "")
(or input-message ""))} (or input-message ""))}
(when command? (when command?
(get-options parameter type)))]) (get-options parameter type)))]))
(defn plain-message-get-initial-state [_] (defn plain-message-get-initial-state [_]
{:height 0}) {:height 0})