|
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 [edit]}] |
110 |
| - (rn/use-effect |
111 |
| - (fn [] |
112 |
| - (let [edit-text (get-in edit [:content :text]) |
113 |
| - text-value-count (count @text-value)] |
114 |
| - (when (and edit @input-ref) |
115 |
| - ;; A small setTimeout is necessary to ensure the statement is enqueued and will get executed |
116 |
| - ;; ASAP. |
117 |
| - ;; https://github.com/software-mansion/react-native-screens/issues/472 |
118 |
| - (js/setTimeout #(.focus ^js @input-ref) 250) |
119 |
| - (.setNativeProps ^js @input-ref (clj->js {:text edit-text})) |
120 |
| - (reset! text-value edit-text) |
121 |
| - (reset! saved-cursor-position (if (zero? text-value-count) |
122 |
| - (count edit-text) |
123 |
| - text-value-count))))) |
124 |
| - [(:message-id edit)])) |
| 109 | + {:keys [text-value saved-cursor-position cursor-position]} |
| 110 | + {:keys [edit input-with-mentions]} |
| 111 | + messages-list-on-layout-finished?] |
| 112 | + (let [mention? (some #(= :mention (first %)) (seq input-with-mentions)) |
| 113 | + composer-just-opened? (not @messages-list-on-layout-finished?)] |
| 114 | + (rn/use-effect |
| 115 | + (fn [] |
| 116 | + (let [mention-text (reduce (fn [acc item] |
| 117 | + (str acc (second item))) |
| 118 | + "" |
| 119 | + input-with-mentions) |
| 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) |
| 129 | + inject-edit-text (fn [] |
| 130 | + (reset! text-value edit-text) |
| 131 | + (reset! cursor-position selection-pos) |
| 132 | + (reset! saved-cursor-position selection-pos) |
| 133 | + (when @input-ref |
| 134 | + (.setNativeProps ^js @input-ref |
| 135 | + (clj->js {:text edit-text}))))] |
| 136 | + |
| 137 | + (when (and edit @input-ref) |
| 138 | + ;; NOTE: A small setTimeout is necessary to ensure the focus is enqueued and is executed |
| 139 | + ;; ASAP. Check https://github.com/software-mansion/react-native-screens/issues/472 |
| 140 | + ;; |
| 141 | + ;; The nested setTimeout is necessary to avoid both `on-focus` and |
| 142 | + ;; `on-content-size-change` handlers triggering the height animation simultaneously, as |
| 143 | + ;; this causes a jump in the |
| 144 | + ;; UI. This way, `on-focus` will trigger first without changing the height, after which |
| 145 | + ;; `on-content-size-change` will animate the height of the input based on the injected |
| 146 | + ;; text. |
| 147 | + (js/setTimeout #(do (when @messages-list-on-layout-finished? (.focus ^js @input-ref)) |
| 148 | + (reagent/next-tick inject-edit-text)) |
| 149 | + 600)))) |
| 150 | + [(:message-id edit)]))) |
125 | 151 |
|
126 | 152 | (defn use-reply
|
127 | 153 | [{:keys [input-ref]}
|
128 | 154 | {:keys [container-opacity]}
|
129 |
| - {:keys [reply]}] |
| 155 | + {:keys [reply]} |
| 156 | + messages-list-on-layout-finished?] |
130 | 157 | (rn/use-effect
|
131 | 158 | (fn []
|
132 | 159 | (when reply
|
133 | 160 | (reanimated/animate container-opacity 1))
|
134 |
| - (when (and reply @input-ref) |
135 |
| - (js/setTimeout #(.focus ^js @input-ref) 250))) |
| 161 | + (when (and reply @input-ref @messages-list-on-layout-finished?) |
| 162 | + (js/setTimeout #(.focus ^js @input-ref) 600))) |
136 | 163 | [(:message-id reply)]))
|
137 | 164 |
|
138 |
| -(defn edit-mentions |
139 |
| - [{:keys [input-ref]} {:keys [text-value cursor-position]} {:keys [input-with-mentions]}] |
140 |
| - (rn/use-effect (fn [] |
141 |
| - (let [input-text (reduce (fn [acc item] |
142 |
| - (str acc (second item))) |
143 |
| - "" |
144 |
| - input-with-mentions)] |
145 |
| - (reset! text-value input-text) |
146 |
| - (reset! cursor-position (count input-text)) |
147 |
| - (js/setTimeout #(when @input-ref |
148 |
| - (.setNativeProps ^js @input-ref |
149 |
| - (clj->js {:selection {:start (count input-text) |
150 |
| - :end (count |
151 |
| - input-text)}}))) |
152 |
| - 300))) |
153 |
| - [(some #(= :mention (first %)) (seq input-with-mentions))])) |
154 |
| - |
155 | 165 | (defn update-input-mention
|
156 | 166 | [{:keys [input-ref]}
|
157 | 167 | {:keys [text-value]}
|
|
0 commit comments