Skip to content

Commit ec0c204

Browse files
committed
Trying different approach for fixing the issue.
1 parent 9b7f6a7 commit ec0c204

File tree

1 file changed

+67
-10
lines changed

1 file changed

+67
-10
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import android.os.Parcel
3535
import android.os.Parcelable
3636
import android.text.Editable
3737
import android.text.InputFilter
38+
import android.text.Selection
3839
import android.text.Spannable
3940
import android.text.SpannableStringBuilder
4041
import android.text.Spanned
@@ -50,6 +51,8 @@ import android.view.View
5051
import android.view.View.OnLongClickListener
5152
import android.view.WindowManager
5253
import android.view.inputmethod.BaseInputConnection
54+
import android.view.inputmethod.CompletionInfo
55+
import android.view.inputmethod.CorrectionInfo
5356
import android.widget.CheckBox
5457
import android.widget.EditText
5558
import android.widget.Toast
@@ -116,6 +119,7 @@ import org.wordpress.aztec.watchers.InlineTextWatcher
116119
import org.wordpress.aztec.watchers.ParagraphBleedAdjuster
117120
import org.wordpress.aztec.watchers.ParagraphCollapseAdjuster
118121
import org.wordpress.aztec.watchers.ParagraphCollapseRemover
122+
import org.wordpress.aztec.watchers.SamsungPredictiveEventsWatcher
119123
import org.wordpress.aztec.watchers.SuggestionWatcher
120124
import org.wordpress.aztec.watchers.TextDeleter
121125
import org.wordpress.aztec.watchers.ZeroIndexContentWatcher
@@ -217,6 +221,30 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
217221
}
218222
}
219223

224+
override fun onBeginBatchEdit() {
225+
super.onBeginBatchEdit()
226+
}
227+
228+
override fun onEditorAction(actionCode: Int) {
229+
super.onEditorAction(actionCode)
230+
}
231+
232+
override fun onPrivateIMECommand(action: String?, data: Bundle?): Boolean {
233+
return super.onPrivateIMECommand(action, data)
234+
}
235+
236+
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
237+
return super.onKeyDown(keyCode, event)
238+
}
239+
240+
override fun onCommitCorrection(info: CorrectionInfo?) {
241+
super.onCommitCorrection(info)
242+
}
243+
244+
override fun onCommitCompletion(text: CompletionInfo?) {
245+
super.onCommitCompletion(text)
246+
}
247+
220248
private val REGEXP_EMAIL = Regex("^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,}$",
221249
setOf(RegexOption.DOT_MATCHES_ALL, RegexOption.IGNORE_CASE))
222250
private val REGEXP_STANDALONE_URL = Regex("^(?:[a-z]+:|#|\\?|\\.|/)", RegexOption.DOT_MATCHES_ALL)
@@ -676,14 +704,41 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
676704
val equalRange = start == 0 && dstart == 0 && end == source.length && dend == source.length
677705

678706
if (equalStringValues && equalRange) {
707+
overrideSamsungPredictiveBehavior = false
679708
// we can't just return a dest, so we need to copy it into a new spannable string
680709
// this will also strip all the internal "service" spans
681710
temp = SpannableStringBuilder(dest)
682-
TextUtils.copySpansFrom(dest, 0, dest.length, Any::class.java, temp, 0)
711+
// TextUtils.copySpansFrom(dest, 0, dest.length, Any::class.java, temp, 0)
683712
// copy all the suggestion spans from the source, so we can see underlines
713+
disableCrashPreventerInputFilter()
714+
disableTextChangedListener()
715+
disableMediaDeletedListener()
716+
684717
if (source is Spanned) {
685718
TextUtils.copySpansFrom(source, 0, dest.length, SuggestionSpan::class.java, temp, 0)
686719
}
720+
721+
val selStart = Selection.getSelectionStart(text)
722+
val selEnd = Selection.getSelectionEnd(text)
723+
val len = text.length
724+
725+
text.clearSpans()
726+
setTextKeepState(temp)
727+
728+
if (selStart >= 0 || selEnd >= 0) {
729+
Selection.setSelection(text,
730+
Math.max(0, Math.min(selStart, len)),
731+
Math.max(0, Math.min(selEnd, len)))
732+
}
733+
734+
735+
contentChangeWatcher.notifyContentChanged()
736+
737+
temp = null
738+
enableTextChangedListener()
739+
enableMediaDeletedListener()
740+
enableCrashPreventerInputFilter()
741+
overrideSamsungPredictiveBehavior = true
687742
}
688743
}
689744
temp
@@ -749,14 +804,16 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
749804
source
750805
}
751806

752-
filters = if (Build.MANUFACTURER == "samsung" && Build.VERSION.SDK_INT == 33) {
753-
arrayOf(samsungContentReplacementPreventer, emptyEditTextBackspaceDetector)
754-
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O || Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {
755-
// dynamicLayoutCrashPreventer needs to be first in array as these are going to be chained when processed
756-
arrayOf(dynamicLayoutCrashPreventer, emptyEditTextBackspaceDetector)
757-
} else {
758-
arrayOf(emptyEditTextBackspaceDetector)
759-
}
807+
filters =
808+
if (Build.MANUFACTURER == "samsung" && Build.VERSION.SDK_INT == 33) {
809+
arrayOf(samsungContentReplacementPreventer, emptyEditTextBackspaceDetector)
810+
} else
811+
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O || Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1) {
812+
// dynamicLayoutCrashPreventer needs to be first in array as these are going to be chained when processed
813+
arrayOf(dynamicLayoutCrashPreventer, emptyEditTextBackspaceDetector)
814+
} else {
815+
arrayOf(emptyEditTextBackspaceDetector)
816+
}
760817
}
761818

762819
private fun isCleanStringEmpty(text: CharSequence): Boolean {
@@ -1901,7 +1958,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
19011958
val html = Format.removeSourceEditorFormatting(parser.toHtml(output), isInCalypsoMode, isInGutenbergMode)
19021959

19031960
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as android.content.ClipboardManager
1904-
clipboard.primaryClip = ClipData.newHtmlText("aztec", output.toString(), html)
1961+
clipboard.setPrimaryClip(ClipData.newHtmlText("aztec", output.toString(), html))
19051962
}
19061963

19071964
// copied from TextView with some changes

0 commit comments

Comments
 (0)