-
Notifications
You must be signed in to change notification settings - Fork 992
/
Copy pathview.cljs
168 lines (150 loc) · 5.64 KB
/
view.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(ns status-im.contexts.chat.messenger.messages.content.text.view
(:require
[quo.core :as quo]
[quo.foundations.colors :as colors]
[react-native.core :as rn]
[react-native.platform :as platform]
[status-im.contexts.chat.messenger.messages.content.link-preview.view :as link-preview]
[status-im.contexts.chat.messenger.messages.content.text.style :as style]
[utils.i18n :as i18n]
[utils.re-frame :as rf]))
(defn render-inline
[units {:keys [type literal destination]} chat-id style-override first-child-mention]
(let [show-as-plain-text? (seq style-override)]
(case (keyword type)
:code
(conj units
[quo/text
{:style (if show-as-plain-text?
{:color colors/white}
(merge style/block (style/code)))
:weight :code} literal])
:emph
(conj units
[quo/text
{:style {:font-style :italic
:color (when show-as-plain-text? colors/white)}} literal])
:strong
(conj units
[quo/text
(cond-> {:weight :bold}
show-as-plain-text? (assoc :style {:color colors/white})) literal])
:strong-emph
(conj units
[quo/text
{:weight :bold
:style {:font-style :italic
:color (when show-as-plain-text? colors/white)}} literal])
:del
(conj units
[quo/text
{:style {:text-decoration-line :line-through
:color (when show-as-plain-text? colors/white)}} literal])
:link
(conj units
[quo/text
{:style {:color (colors/theme-colors colors/primary-50 colors/primary-60)}
:on-press #(rf/dispatch [:browser.ui/message-link-pressed destination])}
destination])
:mention
(conj
units
(let [resolved-mention (rf/sub [:messages/resolve-mention literal])]
[rn/pressable
{:on-press #(rf/dispatch [:chat.ui/show-profile literal])
:key resolved-mention
:style (style/mention-tag-wrapper first-child-mention)}
[quo/text
{:weight :medium
:style style/mention-tag-text
:size :paragraph-1}
resolved-mention]]))
:edited
(conj units
[quo/text
{:weight :medium
:style {:font-size 11 ; Font-size must be used instead of props or the
; styles will clash with original message text
:color (colors/theme-colors colors/neutral-40
colors/neutral-50)}}
literal])
:status-tag
(let [community-id (rf/sub [:community-id-by-chat-id chat-id])]
(conj units
[rn/text
(when community-id
{:style {:color :blue
:text-decoration-line :underline}
:on-press #(rf/dispatch [:communities/status-tag-pressed community-id literal])})
"#"
literal]))
(conj units literal))))
(defn first-child-mention
[children]
(and (> (count children) 0)
(= (keyword (:type (second children))) :mention)
(empty? (get-in children [0 :literal]))))
(defn render-block
[blocks {:keys [type literal children]} chat-id style-override]
(let [mention-first (first-child-mention children)]
(case (keyword type)
:paragraph
(conj blocks
[rn/view
(reduce
(fn [acc e]
(render-inline acc e chat-id style-override mention-first))
[quo/text
{:size :paragraph-1
:style {:margin-bottom (if mention-first (if platform/ios? 4 0) 2)
:margin-top (if mention-first (if platform/ios? -4 0) -1)
:color (when (seq style-override) colors/white)
:line-height 22.75}}]
children)])
:edited-block
(conj blocks
(reduce
(fn [acc e]
(render-inline acc e chat-id style-override first-child-mention))
[quo/text {:size :paragraph-1}]
children))
:blockquote
(conj blocks
[rn/view {:style style/quote}
[quo/text literal]])
:codeblock
(conj blocks
[rn/view {:style (merge style/block (style/code))}
[quo/text (subs literal 0 (dec (count literal)))]])
blocks)))
(def edited-tag
{:literal (str "(" (i18n/label :t/edited) ")")
:type :edited})
(defn add-edited-tag
[parsed-text]
(let [items-count (count parsed-text)
last-item (get parsed-text (dec items-count))]
(if (= (keyword (:type last-item)) :paragraph)
(update parsed-text
(dec items-count)
(fn [last-literal]
(update last-literal :children into [{:literal " "} edited-tag])))
(conj parsed-text {:type :edited-block :children [edited-tag]}))))
(defn render-parsed-text
[{:keys [content chat-id edited-at style-override on-layout]}]
^{:key (:parsed-text content)}
[rn/view
{:style style-override
:on-layout on-layout
:accessibility-label :message-text-content}
(reduce (fn [acc e]
(render-block acc e chat-id style-override))
[:<>]
(cond-> (:parsed-text content)
edited-at
add-edited-tag))])
(defn text-content
[message-data]
[:<>
[render-parsed-text message-data]
[link-preview/view message-data]])