Skip to content

Commit a3f5b83

Browse files
committed
Refactor AztecText html methods to accept a general Spannable.
1 parent 0554236 commit a3f5b83

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

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

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,7 @@ import android.support.graphics.drawable.VectorDrawableCompat
3636
import android.support.v4.content.ContextCompat
3737
import android.support.v7.app.AlertDialog
3838
import android.support.v7.widget.AppCompatEditText
39-
import android.text.Editable
40-
import android.text.InputFilter
41-
import android.text.InputType
42-
import android.text.Spannable
43-
import android.text.SpannableStringBuilder
44-
import android.text.Spanned
45-
import android.text.TextUtils
46-
import android.text.TextWatcher
39+
import android.text.*
4740
import android.text.style.SuggestionSpan
4841
import android.util.AttributeSet
4942
import android.util.DisplayMetrics
@@ -1239,6 +1232,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
12391232
}
12401233

12411234
// returns regular or "calypso" html depending on the mode
1235+
// default behavior returns HTML from this text
12421236
fun toHtml(withCursorTag: Boolean = false): String {
12431237
val html = toPlainHtml(withCursorTag)
12441238

@@ -1251,24 +1245,53 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
12511245
}
12521246
}
12531247

1248+
// general function accepts any Spannable and converts it to regular or "calypso" html
1249+
// depending on the mode
1250+
fun toHtml(content: Spannable, withCursorTag: Boolean = false): String {
1251+
val html = toPlainHtml(content, withCursorTag)
1252+
1253+
if (isInCalypsoMode) {
1254+
// calypso format is a mix of newline characters and html
1255+
// paragraphs and line breaks are added on server, from newline characters
1256+
return Format.addSourceEditorFormatting(html, true)
1257+
} else {
1258+
return html
1259+
}
1260+
}
1261+
12541262
// platform agnostic HTML
1263+
// default behavior returns HTML from this text
12551264
fun toPlainHtml(withCursorTag: Boolean = false): String {
12561265
return if (Looper.myLooper() != Looper.getMainLooper()) {
12571266
runBlocking {
12581267
withContext(Dispatchers.Main) {
1259-
parseHtml(withCursorTag)
1268+
parseHtml(text, withCursorTag)
12601269
}
12611270
}
12621271
} else {
1263-
parseHtml(withCursorTag)
1272+
parseHtml(text, withCursorTag)
12641273
}
12651274
}
12661275

1267-
private fun parseHtml(withCursorTag: Boolean): String {
1276+
// general function accepts any Spannable and converts it to platform agnostic HTML
1277+
fun toPlainHtml(content: Spannable, withCursorTag: Boolean = false): String {
1278+
return if (Looper.myLooper() != Looper.getMainLooper()) {
1279+
runBlocking {
1280+
withContext(Dispatchers.Main) {
1281+
parseHtml(content, withCursorTag)
1282+
}
1283+
}
1284+
} else {
1285+
parseHtml(content, withCursorTag)
1286+
}
1287+
}
1288+
1289+
private fun parseHtml(content: Spannable, withCursorTag: Boolean): String {
12681290
val parser = AztecParser(plugins)
12691291
val output: SpannableStringBuilder
12701292
try {
1271-
output = SpannableStringBuilder(text)
1293+
//output = SpannableStringBuilder(text)
1294+
output = SpannableStringBuilder(content)
12721295
} catch (e: Exception) {
12731296
// FIXME: Remove this log once we've data to replicate the issue, and fix it in some way.
12741297
AppLog.e(AppLog.T.EDITOR, "There was an error creating SpannableStringBuilder. See #452 and #582 for details.")
@@ -1292,8 +1315,14 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
12921315
return EndOfBufferMarkerAdder.removeEndOfTextMarker(parser.toHtml(output, withCursorTag))
12931316
}
12941317

1318+
// default behavior returns formatted HTML from this text
12951319
fun toFormattedHtml(): String {
1296-
return Format.addSourceEditorFormatting(toHtml(), isInCalypsoMode)
1320+
return toFormattedHtml(text)
1321+
}
1322+
1323+
// general function accepts any Spannable and converts it to formatted HTML
1324+
fun toFormattedHtml(content: Spannable): String {
1325+
return Format.addSourceEditorFormatting(toHtml(content), isInCalypsoMode)
12971326
}
12981327

12991328
private fun switchToAztecStyle(editable: Editable, start: Int, end: Int) {

0 commit comments

Comments
 (0)