Skip to content

Commit

Permalink
Allow autocomplete typing in the middle of a message
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnvc committed Apr 14, 2019
1 parent 4dd9fa0 commit 44ca786
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/braid/core/client/ui/views/new_message.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
state (r/atom {:text thread-text
:force-close? false
:highlighted-result-index 0
:results nil})
:results nil
:pos 0})

reset-state! (fn []
(swap! state assoc
Expand Down Expand Up @@ -161,8 +162,11 @@
(update-thread-text! thread-id text)))

update-text! (fn [f]
(swap! state update :text f)
(update-thread-text! thread-id (@state :text)))
(let [{:keys [text pos]} @state
updated-text (f (.slice text 0 pos))
new-text (str updated-text (.slice text pos))]
(swap! state assoc :text new-text)
(update-thread-text! thread-id (@state :text))))

choose-result!
(fn [result]
Expand All @@ -175,9 +179,9 @@
;TODO


handle-text-change! (fn [text]
handle-text-change! (fn [cursor text]
(clear-force-close!)
(put! autocomplete-chan text))
(put! autocomplete-chan {:text text :pos cursor}))

; returns a function that can be used in a textarea's :on-key-down handler
; takes a callback fn, the textareas intended submit action
Expand Down Expand Up @@ -215,7 +219,9 @@
autocomplete-on-change
(fn [{:keys [on-change]}]
(fn [e]
(handle-text-change! (.. e -target -value))
(let [cursor-pos (.. e -target -selectionStart)]
(handle-text-change! cursor-pos (.. e -target -value))
(swap! state assoc :pos cursor-pos))
(on-change e)))]

(r/create-class
Expand All @@ -224,12 +230,14 @@
(fn [c]
(let [config (r/props c)]
(go (loop []
(let [[text ch] (alts! [throttled-autocomplete-chan kill-chan])]
(let [[data ch] (alts! [throttled-autocomplete-chan kill-chan])]
(when (= ch throttled-autocomplete-chan)
(when-not (inside-code-block? text)
(set-results!
(seq (mapcat (fn [e] (e text)) @autocomplete-engines)))
(highlight-first!))
(let [{:keys [text pos]} data
text (.slice text 0 pos)]
(when-not (inside-code-block? text)
(set-results!
(seq (mapcat (fn [e] (e text)) @autocomplete-engines)))
(highlight-first!)))
(recur)))))
(go (loop []
(let [[v ch] (alts! [throttled-thread-text-chan
Expand Down

0 comments on commit 44ca786

Please sign in to comment.