Skip to content

Commit

Permalink
merge on-text-input and calculate suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
qfrank committed Apr 13, 2023
1 parent f27b137 commit e9172f9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
23 changes: 16 additions & 7 deletions src/status_im/chat/models/mentions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

(rf/defn on-text-input
{:events [:mention/on-text-input]}
[{:keys [db]} {:keys [previous-text start end] :as args}]
[{:keys [db]} {:keys [previous-text start end full-text] :as args}]
(let [previous-text
;; NOTE(rasom): on iOS `previous-text` contains entire input's text. To
;; get only removed part of text we have cut it.
Expand All @@ -106,7 +106,8 @@
{:previous-text :PreviousText
:new-text :NewText
:start :Start
:end :End})
:end :End
:full-text :FullText})
params [chat-id state]
method "wakuext_chatMentionOnTextInput"]
(log/debug "[mentions] on-text-input" {:params params})
Expand All @@ -121,8 +122,11 @@
{:events [:mention/on-text-input-success]}
[{:keys [db]} result]
(log/debug "[mentions] on-text-input-success" {:result result})
(let [{:keys [state chat-id]} (transfer-mention-result result)]
{:db (assoc-in db [:chats/mentions chat-id :mentions] state)}))
(let [{:keys [state chat-id mentionable-users input-segments]} (transfer-mention-result result)]
{:db (-> db
(assoc-in [:chats/mention-suggestions chat-id] mentionable-users)
(assoc-in [:chats/mentions chat-id :mentions] state)
(assoc-in [:chat/inputs-with-mentions chat-id] input-segments))}))

(rf/defn recheck-at-idxs
[{:keys [db]} public-key]
Expand Down Expand Up @@ -184,7 +188,9 @@
{:new-text match
:previous-text searched-text
:start start
:end end}))
:end end
;; empty full-text to tell backend not calculate suggestions
:full-text ""}))
(recheck-at-idxs public-key))))

(rf/defn clear-suggestions
Expand Down Expand Up @@ -230,8 +236,11 @@
{:events [:mention/on-handle-selection-change-success]}
[{:keys [db]} result]
(log/debug "[mentions] on-check-selection-success" {:result result})
(let [{:keys [state chat-id]} (transfer-mention-result result)]
{:db (assoc-in db [:chats/mentions chat-id :mentions] (rename-state state))}))
(let [{:keys [state chat-id mentionable-users input-segments]} (transfer-mention-result result)]
{:db (-> db
(assoc-in [:chats/mention-suggestions chat-id] mentionable-users)
(assoc-in [:chats/mentions chat-id :mentions] state)
(assoc-in [:chat/inputs-with-mentions chat-id] input-segments))}))

(re-frame/reg-fx
::reset-text-input-cursor
Expand Down
38 changes: 23 additions & 15 deletions src/status_im/ui2/screens/chat/composer/input.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

(defonce input-texts (atom {}))
(defonce input-text-content-heights (atom {}))
(defonce input-text-initial-state {:on-changed? false :on-text-input? false})
(defonce input-text-state (atom input-text-initial-state))
(defonce mentions-enabled? (reagent/atom {}))
(defonce chat-input-key (reagent/atom 1))
(defonce text-input-ref (reagent/atom nil))
Expand Down Expand Up @@ -118,6 +120,17 @@
{:start start
:end end}]))))

(defn- ready-calculate-suggestion?
[]
(let [state @input-text-state]
(and (:on-changed? state) (:on-text-input? state))))

(defn- reset-input-text-state
[]
(when (ready-calculate-suggestion?)
(rf/dispatch [:mention/on-text-input @input-text-state])
(reset! input-text-state input-text-initial-state)))

(defn on-change
[last-text-change timeout-id refs chat-id sending-image args]
(let [text (.-text ^js (.-nativeEvent ^js args))
Expand All @@ -141,10 +154,9 @@
(reset! last-text-change (js/Date.now)))

(on-text-change text chat-id)
;; NOTE(rasom): on iOS `on-change` is dispatched after `on-text-input`,
;; that's why mention suggestions are calculated on `on-change`
(when platform/ios?
(rf/dispatch [:mention/calculate-suggestions]))))

(swap! input-text-state assoc :on-changed? true :full-text text)
(reset-input-text-state)))

(defn on-text-input
[chat-id args]
Expand All @@ -157,17 +169,13 @@
(when (and (not (get @mentions-enabled? chat-id)) (string/index-of text "@"))
(swap! mentions-enabled? assoc chat-id true))

(rf/dispatch
[:mention/on-text-input
{:new-text text
:previous-text previous-text
:start start
:end end}])
;; NOTE(rasom): on Android `on-text-input` is dispatched after
;; `on-change`, that's why mention suggestions are calculated
;; on `on-change`
(when platform/android?
(rf/dispatch [:mention/calculate-suggestions]))))
(swap! input-text-state assoc
:on-text-input? true
:new-text text
:previous-text previous-text
:start start
:end end)
(reset-input-text-state)))

(defn text-input-style
[chat-id]
Expand Down
4 changes: 2 additions & 2 deletions status-go-version.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"owner": "status-im",
"repo": "status-go",
"version": "fix/15616",
"commit-sha1": "c4439202400f7082227a6027dc6217c02ccba61a",
"src-sha256": "1qlg78yj65ff90f0akkmz2v8239vc90y37lag1d1bq3d6wh2qv8b"
"commit-sha1": "190457aeba030c757824f2f335f1adb59eed380e",
"src-sha256": "0247aphnkjvlzn9lf590q2phzl5zigiigql9w4v03s08xl5f76fp"
}

0 comments on commit e9172f9

Please sign in to comment.