@@ -38,7 +38,7 @@ import im.vector.app.core.extensions.setTextIfDifferent
38
38
import im.vector.app.databinding.ComposerRichTextLayoutBinding
39
39
import im.vector.app.databinding.ViewRichTextMenuButtonBinding
40
40
import io.element.android.wysiwyg.EditorEditText
41
- import io.element.android.wysiwyg.InlineFormat
41
+ import io.element.android.wysiwyg.inputhandlers.models. InlineFormat
42
42
import uniffi.wysiwyg_composer.ComposerAction
43
43
import uniffi.wysiwyg_composer.MenuState
44
44
@@ -57,12 +57,24 @@ class RichTextComposerLayout @JvmOverloads constructor(
57
57
58
58
private var isFullScreen = false
59
59
60
+ var isTextFormattingEnabled = true
61
+ set(value) {
62
+ if (field == value) return
63
+ syncEditTexts()
64
+ field = value
65
+ updateEditTextVisibility()
66
+ }
67
+
60
68
override val text: Editable ?
61
- get() = views.composerEditText .text
69
+ get() = editText .text
62
70
override val formattedText: String?
63
- get() = views.composerEditText .getHtmlOutput()
71
+ get() = (editText as ? EditorEditText )? .getHtmlOutput()
64
72
override val editText: EditText
65
- get() = views.composerEditText
73
+ get() = if (isTextFormattingEnabled) {
74
+ views.richTextComposerEditText
75
+ } else {
76
+ views.plainTextComposerEditText
77
+ }
66
78
override val emojiButton: ImageButton ?
67
79
get() = null
68
80
override val sendButton: ImageButton
@@ -91,21 +103,12 @@ class RichTextComposerLayout @JvmOverloads constructor(
91
103
92
104
collapse(false )
93
105
94
- views.composerEditText.addTextChangedListener(object : TextWatcher {
95
- private var previousTextWasExpanded = false
96
-
97
- override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
98
- override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {}
99
- override fun afterTextChanged (s : Editable ) {
100
- callback?.onTextChanged(s)
101
-
102
- val isExpanded = s.lines().count() > 1
103
- if (previousTextWasExpanded != isExpanded) {
104
- updateTextFieldBorder(isExpanded)
105
- }
106
- previousTextWasExpanded = isExpanded
107
- }
108
- })
106
+ views.richTextComposerEditText.addTextChangedListener(
107
+ TextChangeListener ({ callback?.onTextChanged(it) }, ::updateTextFieldBorder)
108
+ )
109
+ views.plainTextComposerEditText.addTextChangedListener(
110
+ TextChangeListener ({ callback?.onTextChanged(it) }, ::updateTextFieldBorder)
111
+ )
109
112
110
113
views.composerRelatedMessageCloseButton.setOnClickListener {
111
114
collapse()
@@ -130,28 +133,50 @@ class RichTextComposerLayout @JvmOverloads constructor(
130
133
131
134
private fun setupRichTextMenu () {
132
135
addRichTextMenuItem(R .drawable.ic_composer_bold, R .string.rich_text_editor_format_bold, ComposerAction .Bold ) {
133
- views.composerEditText .toggleInlineFormat(InlineFormat .Bold )
136
+ views.richTextComposerEditText .toggleInlineFormat(InlineFormat .Bold )
134
137
}
135
138
addRichTextMenuItem(R .drawable.ic_composer_italic, R .string.rich_text_editor_format_italic, ComposerAction .Italic ) {
136
- views.composerEditText .toggleInlineFormat(InlineFormat .Italic )
139
+ views.richTextComposerEditText .toggleInlineFormat(InlineFormat .Italic )
137
140
}
138
141
addRichTextMenuItem(R .drawable.ic_composer_underlined, R .string.rich_text_editor_format_underline, ComposerAction .Underline ) {
139
- views.composerEditText .toggleInlineFormat(InlineFormat .Underline )
142
+ views.richTextComposerEditText .toggleInlineFormat(InlineFormat .Underline )
140
143
}
141
144
addRichTextMenuItem(R .drawable.ic_composer_strikethrough, R .string.rich_text_editor_format_strikethrough, ComposerAction .StrikeThrough ) {
142
- views.composerEditText .toggleInlineFormat(InlineFormat .StrikeThrough )
145
+ views.richTextComposerEditText .toggleInlineFormat(InlineFormat .StrikeThrough )
143
146
}
147
+ }
148
+
149
+ override fun onAttachedToWindow () {
150
+ super .onAttachedToWindow()
144
151
145
- views.composerEditText .menuStateChangedListener = EditorEditText .OnMenuStateChangedListener { state ->
152
+ views.richTextComposerEditText .menuStateChangedListener = EditorEditText .OnMenuStateChangedListener { state ->
146
153
if (state is MenuState .Update ) {
147
154
updateMenuStateFor(ComposerAction .Bold , state)
148
155
updateMenuStateFor(ComposerAction .Italic , state)
149
156
updateMenuStateFor(ComposerAction .Underline , state)
150
157
updateMenuStateFor(ComposerAction .StrikeThrough , state)
151
158
}
152
159
}
160
+
161
+ updateEditTextVisibility()
153
162
}
154
163
164
+ private fun updateEditTextVisibility () {
165
+ views.richTextComposerEditText.isVisible = isTextFormattingEnabled
166
+ views.richTextMenu.isVisible = isTextFormattingEnabled
167
+ views.plainTextComposerEditText.isVisible = ! isTextFormattingEnabled
168
+ }
169
+
170
+ /* *
171
+ * Updates the non-active input with the contents of the active input.
172
+ */
173
+ private fun syncEditTexts () =
174
+ if (isTextFormattingEnabled) {
175
+ views.plainTextComposerEditText.setText(views.richTextComposerEditText.getPlainText())
176
+ } else {
177
+ views.richTextComposerEditText.setText(views.plainTextComposerEditText.text.toString())
178
+ }
179
+
155
180
private fun addRichTextMenuItem (@DrawableRes iconId : Int , @StringRes description : Int , action : ComposerAction , onClick : () -> Unit ) {
156
181
val inflater = LayoutInflater .from(context)
157
182
val button = ViewRichTextMenuButtonBinding .inflate(inflater, views.richTextMenu, true )
@@ -181,7 +206,7 @@ class RichTextComposerLayout @JvmOverloads constructor(
181
206
}
182
207
183
208
override fun replaceFormattedContent (text : CharSequence ) {
184
- views.composerEditText .setHtml(text.toString())
209
+ views.richTextComposerEditText .setHtml(text.toString())
185
210
}
186
211
187
212
override fun collapse (animate : Boolean , transitionComplete : (() -> Unit )? ) {
@@ -191,6 +216,7 @@ class RichTextComposerLayout @JvmOverloads constructor(
191
216
}
192
217
currentConstraintSetId = R .layout.composer_rich_text_layout_constraint_set_compact
193
218
applyNewConstraintSet(animate, transitionComplete)
219
+ updateEditTextVisibility()
194
220
}
195
221
196
222
override fun expand (animate : Boolean , transitionComplete : (() -> Unit )? ) {
@@ -200,10 +226,11 @@ class RichTextComposerLayout @JvmOverloads constructor(
200
226
}
201
227
currentConstraintSetId = R .layout.composer_rich_text_layout_constraint_set_expanded
202
228
applyNewConstraintSet(animate, transitionComplete)
229
+ updateEditTextVisibility()
203
230
}
204
231
205
232
override fun setTextIfDifferent (text : CharSequence? ): Boolean {
206
- return views.composerEditText .setTextIfDifferent(text)
233
+ return editText .setTextIfDifferent(text)
207
234
}
208
235
209
236
override fun toggleFullScreen (newValue : Boolean ) {
@@ -214,6 +241,7 @@ class RichTextComposerLayout @JvmOverloads constructor(
214
241
}
215
242
216
243
updateTextFieldBorder(newValue)
244
+ updateEditTextVisibility()
217
245
}
218
246
219
247
private fun applyNewConstraintSet (animate : Boolean , transitionComplete : (() -> Unit )? ) {
@@ -233,4 +261,23 @@ class RichTextComposerLayout @JvmOverloads constructor(
233
261
override fun setInvisible (isInvisible : Boolean ) {
234
262
this .isInvisible = isInvisible
235
263
}
264
+
265
+ private class TextChangeListener (
266
+ private val onTextChanged : (s: Editable ) -> Unit ,
267
+ private val onExpandedChanged : (isExpanded: Boolean ) -> Unit ,
268
+ ) : TextWatcher {
269
+ private var previousTextWasExpanded = false
270
+
271
+ override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
272
+ override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {}
273
+ override fun afterTextChanged (s : Editable ) {
274
+ onTextChanged.invoke(s)
275
+
276
+ val isExpanded = s.lines().count() > 1
277
+ if (previousTextWasExpanded != isExpanded) {
278
+ onExpandedChanged(isExpanded)
279
+ }
280
+ previousTextWasExpanded = isExpanded
281
+ }
282
+ }
236
283
}
0 commit comments