|
6 | 6 | [react-native.core :as rn]
|
7 | 7 | [react-native.platform :as platform]
|
8 | 8 | [react-native.reanimated :as reanimated]
|
| 9 | + [reagent.core :as reagent] |
9 | 10 | [status-im2.contexts.chat.composer.constants :as constants]
|
10 | 11 | [status-im2.contexts.chat.composer.keyboard :as kb]
|
11 | 12 | [status-im2.contexts.chat.composer.utils :as utils]
|
|
105 | 106 |
|
106 | 107 | (defn use-edit
|
107 | 108 | [{:keys [input-ref]}
|
108 |
| - {:keys [text-value saved-cursor-position]} |
| 109 | + {:keys [text-value saved-cursor-position cursor-position]} |
109 | 110 | {:keys [edit input-with-mentions]}
|
110 | 111 | messages-list-on-layout-finished?]
|
111 |
| - (let [mention? (some #(= :mention (first %)) (seq input-with-mentions))] |
| 112 | + (let [mention? (some #(= :mention (first %)) (seq input-with-mentions)) |
| 113 | + composer-just-opened? (not @messages-list-on-layout-finished?)] |
112 | 114 | (rn/use-effect
|
113 | 115 | (fn []
|
114 | 116 | (let [mention-text (reduce (fn [acc item]
|
115 | 117 | (str acc (second item)))
|
116 | 118 | ""
|
117 | 119 | input-with-mentions)
|
118 |
| - edit-text (if mention? mention-text (get-in edit [:content :text])) |
119 |
| - text-value-count (count @text-value) |
| 120 | + edit-text (cond |
| 121 | + mention? mention-text |
| 122 | + ;; NOTE: using text-value for cases when the user |
| 123 | + ;; leaves the app with an unfinished edit and re-opens |
| 124 | + ;; the chat. |
| 125 | + (and (seq @text-value) composer-just-opened?) |
| 126 | + @text-value |
| 127 | + :else (get-in edit [:content :text])) |
| 128 | + selection-pos (count edit-text) |
120 | 129 | inject-edit-text (fn []
|
121 | 130 | (reset! text-value edit-text)
|
122 |
| - (reset! saved-cursor-position (if (seq edit-text) |
123 |
| - (count edit-text) |
124 |
| - text-value-count)) |
| 131 | + (reset! cursor-position selection-pos) |
| 132 | + (reset! saved-cursor-position selection-pos) |
125 | 133 | (when @input-ref
|
126 |
| - (let [selection-pos (count edit-text)] |
127 |
| - (.setNativeProps ^js @input-ref |
128 |
| - (clj->js {:text edit-text |
129 |
| - :selection {:start selection-pos |
130 |
| - :end selection-pos}})))))] |
| 134 | + (.setNativeProps ^js @input-ref |
| 135 | + (clj->js {:text edit-text}))))] |
| 136 | + |
131 | 137 | (when (and edit @input-ref)
|
132 | 138 | ;; NOTE: A small setTimeout is necessary to ensure the focus is enqueued and is executed
|
133 | 139 | ;; ASAP. Check https://github.com/software-mansion/react-native-screens/issues/472
|
|
139 | 145 | ;; `on-content-size-change` will animate the height of the input based on the injected
|
140 | 146 | ;; text.
|
141 | 147 | (js/setTimeout #(do (when @messages-list-on-layout-finished? (.focus ^js @input-ref))
|
142 |
| - (js/setTimeout inject-edit-text 250)) |
143 |
| - 100)))) |
144 |
| - [(:message-id edit) mention?]))) |
| 148 | + (reagent/next-tick inject-edit-text)) |
| 149 | + 600)))) |
| 150 | + [(:message-id edit)]))) |
145 | 151 |
|
146 | 152 | (defn use-reply
|
147 | 153 | [{:keys [input-ref]}
|
|
0 commit comments