Skip to content

Commit e8c934f

Browse files
authored
Merge pull request #987 from wordpress-mobile/feature/add-option-to-remove-placeholders-programatically
Add method to remove placeholder programatically
2 parents ea07cf3 + d96cfc3 commit e8c934f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

media-placeholders/src/main/java/org/wordpress/aztec/placeholders/PlaceholderManager.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ class PlaceholderManager(
7777
insertContentOverSpanWithId(attrs.getValue(UUID_ATTRIBUTE), null)
7878
}
7979

80+
/**
81+
* Call this method to remove a placeholder from both the AztecText and the overlaying layer programatically.
82+
* @param predicate determines whether a span should be removed
83+
*/
84+
fun removeItem(predicate: (AztecAttributes) -> Boolean) {
85+
aztecText.editableText.getSpans(0, aztecText.length(), AztecPlaceholderSpan::class.java).filter {
86+
predicate(it.attributes)
87+
}.forEach { placeholderSpan ->
88+
val startIndex = aztecText.editableText.getSpanStart(placeholderSpan)
89+
val endIndex = aztecText.editableText.getSpanEnd(placeholderSpan)
90+
aztecText.editableText.getSpans(startIndex, endIndex, AztecMediaClickableSpan::class.java).forEach {
91+
aztecText.editableText.removeSpan(it)
92+
}
93+
aztecText.editableText.removeSpan(placeholderSpan)
94+
aztecText.editableText.delete(startIndex, endIndex)
95+
onMediaDeleted(placeholderSpan.attributes)
96+
}
97+
aztecText.refreshText(false)
98+
}
99+
80100
private fun buildPlaceholderDrawable(adapter: PlaceholderAdapter, attrs: AztecAttributes): Drawable {
81101
val drawable = ContextCompat.getDrawable(aztecText.context, android.R.color.transparent)!!
82102
updateDrawableBounds(adapter, attrs, drawable)

0 commit comments

Comments
 (0)