Skip to content

Commit eddbce8

Browse files
committed
Move loading from HTML to a background thread
1 parent caf8f43 commit eddbce8

File tree

1 file changed

+55
-16
lines changed

1 file changed

+55
-16
lines changed

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

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import android.text.TextWatcher
4444
import android.text.style.SuggestionSpan
4545
import android.util.AttributeSet
4646
import android.util.DisplayMetrics
47+
import android.util.Log
4748
import android.util.TypedValue
4849
import android.view.KeyEvent
4950
import android.view.LayoutInflater
@@ -1497,37 +1498,75 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
14971498
}
14981499

14991500
open fun fromHtml(source: String, isInit: Boolean = true) {
1500-
val builder = SpannableStringBuilder()
1501-
val parser = AztecParser(alignmentRendering, plugins)
1501+
val builder = getBuilderFromHtml(source)
1502+
setupCursorPosition(builder)
1503+
calculateSha(isInit)
1504+
loadMedia()
1505+
}
15021506

1503-
var cleanSource = CleaningUtils.cleanNestedBoldTags(source)
1504-
cleanSource = Format.removeSourceEditorFormatting(cleanSource, isInCalypsoMode, isInGutenbergMode)
1505-
builder.append(parser.fromHtml(cleanSource, context, shouldSkipTidying(), shouldIgnoreWhitespace()))
1507+
open suspend fun fromHtmlAsync(source: String, isInit: Boolean = true) = withContext(Dispatchers.Default) {
1508+
val builder = getBuilderFromHtml(source)
1509+
withContext(Dispatchers.Main) {
1510+
setupCursorPosition(builder)
1511+
}
15061512

1507-
Format.preProcessSpannedText(builder, isInCalypsoMode)
1513+
calculateSha(isInit)
1514+
loadMedia()
1515+
}
15081516

1509-
switchToAztecStyle(builder, 0, builder.length)
1510-
disableTextChangedListener()
1517+
private fun loadMedia() {
1518+
loadImages()
1519+
loadVideos()
1520+
mediaCallback?.mediaLoadingStarted()
1521+
}
15111522

1512-
builder.getSpans(0, builder.length, AztecDynamicImageSpan::class.java).forEach {
1513-
it.textView = WeakReference(this)
1523+
private fun calculateSha(isInit: Boolean) {
1524+
if (isInit) {
1525+
initialEditorContentParsedSHA256 = calculateInitialHTMLSHA(
1526+
toPlainHtml(false),
1527+
initialEditorContentParsedSHA256
1528+
)
15141529
}
1530+
}
15151531

1532+
private fun setupCursorPosition(builder: SpannableStringBuilder) {
15161533
val cursorPosition = consumeCursorPosition(builder)
15171534
setSelection(0)
15181535

15191536
setTextKeepState(builder)
15201537
enableTextChangedListener()
15211538

15221539
setSelection(cursorPosition)
1540+
}
15231541

1524-
if (isInit) {
1525-
initialEditorContentParsedSHA256 = calculateInitialHTMLSHA(toPlainHtml(false), initialEditorContentParsedSHA256)
1526-
}
1542+
private fun getBuilderFromHtml(source: String): SpannableStringBuilder {
1543+
val builder = SpannableStringBuilder()
1544+
val parser = AztecParser(alignmentRendering, plugins)
15271545

1528-
loadImages()
1529-
loadVideos()
1530-
mediaCallback?.mediaLoadingStarted()
1546+
var cleanSource = CleaningUtils.cleanNestedBoldTags(source)
1547+
cleanSource = Format.removeSourceEditorFormatting(
1548+
cleanSource,
1549+
isInCalypsoMode,
1550+
isInGutenbergMode
1551+
)
1552+
builder.append(
1553+
parser.fromHtml(
1554+
cleanSource,
1555+
context,
1556+
shouldSkipTidying(),
1557+
shouldIgnoreWhitespace()
1558+
)
1559+
)
1560+
1561+
Format.preProcessSpannedText(builder, isInCalypsoMode)
1562+
1563+
switchToAztecStyle(builder, 0, builder.length)
1564+
disableTextChangedListener()
1565+
1566+
builder.getSpans(0, builder.length, AztecDynamicImageSpan::class.java).forEach {
1567+
it.textView = WeakReference(this)
1568+
}
1569+
return builder
15311570
}
15321571

15331572
private fun loadImages() {

0 commit comments

Comments
 (0)