letsubs macro (#1392)

* letsubs macro
This commit is contained in:
Roman Volosovskyi 2017-06-30 16:49:28 +03:00 committed by GitHub
parent 9665ea466d
commit 173d4afbf3
2 changed files with 37 additions and 30 deletions

View File

@ -1,5 +1,5 @@
(ns status-im.chat.views.input.input (ns status-im.chat.views.input.input
(:require-macros [status-im.utils.views :refer [defview]]) (:require-macros [status-im.utils.views :refer [defview letsubs]])
(:require [clojure.string :as str] (:require [clojure.string :as str]
[reagent.core :as r] [reagent.core :as r]
[re-frame.core :refer [subscribe dispatch]] [re-frame.core :refer [subscribe dispatch]]
@ -213,30 +213,30 @@
[icon :close_gray style/input-clear-icon]]]))]))}))) [icon :close_gray style/input-clear-icon]]]))]))})))
(defview input-container [{:keys [anim-margin]}] (defview input-container [{:keys [anim-margin]}]
[command-completion [:command-completion] (letsubs [command-completion [:command-completion]
selected-command [:selected-chat-command] selected-command [:selected-chat-command]
input-text [:chat :input-text] input-text [:chat :input-text]
seq-arg-input-text [:chat :seq-argument-input-text] seq-arg-input-text [:chat :seq-argument-input-text]
result-box [:chat-ui-props :result-box]] result-box [:chat-ui-props :result-box]]
(let [single-line-input? (:singleLineInput result-box)] (let [single-line-input? (:singleLineInput result-box)]
[view style/input-container [view style/input-container
[input-view {:anim-margin anim-margin [input-view {:anim-margin anim-margin
:single-line-input? single-line-input?}] :single-line-input? single-line-input?}]
(if (:actions result-box) (if (:actions result-box)
[input-actions/input-actions-view] [input-actions/input-actions-view]
(when (and (not (str/blank? input-text)) (when (and (not (str/blank? input-text))
(or (not selected-command) (or (not selected-command)
(some #{:complete :less-than-needed} [command-completion]))) (some #{:complete :less-than-needed} [command-completion])))
[touchable-highlight {:on-press #(if (get-in selected-command [:command :sequential-params]) [touchable-highlight {:on-press #(if (get-in selected-command [:command :sequential-params])
(do (do
(when-not (str/blank? seq-arg-input-text) (when-not (str/blank? seq-arg-input-text)
(dispatch [:send-seq-argument])) (dispatch [:send-seq-argument]))
(js/setTimeout (js/setTimeout
(fn [] (dispatch [:chat-input-focus :seq-input-ref])) (fn [] (dispatch [:chat-input-focus :seq-input-ref]))
100)) 100))
(dispatch [:send-current-message]))} (dispatch [:send-current-message]))}
[view style/send-message-container [view style/send-message-container
[icon :arrow_top style/send-message-icon]]]))])) [icon :arrow_top style/send-message-icon]]]))])))
(defn container [] (defn container []
(let [margin (subscribe [:chat-input-margin]) (let [margin (subscribe [:chat-input-margin])

View File

@ -34,12 +34,19 @@
[form `(deref ~sym)])) [form `(deref ~sym)]))
pairs))])) pairs))]))
(defmacro letsubs [args body])
(defmacro defview (defmacro defview
[n params & rest] [n params & rest-body]
(let [[subs component-map body] (case (count rest) (let [first-symbol (ffirst rest-body)
1 [nil {} (first rest)] rest-body' (if (and (symbol? first-symbol)
2 [(first rest) {} (second rest)] (= (name first-symbol) "letsubs"))
3 rest) (rest (first rest-body))
rest-body)
[subs component-map body] (case (count rest-body')
1 [nil {} (first rest-body')]
2 [(first rest-body') {} (second rest-body')]
3 rest-body')
[subs-bindings vars-bindings] (prepare-subs subs)] [subs-bindings vars-bindings] (prepare-subs subs)]
`(defn ~n ~params `(defn ~n ~params
(let [~@subs-bindings] (let [~@subs-bindings]