Skip to content

Commit 797868e

Browse files
committed
Improve behaviour of history when updating placeholders
1 parent a9bbb9a commit 797868e

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,18 +2161,15 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
21612161
lineBlockFormatter.insertVideo(shouldAddMediaInline, drawable, attributes, onVideoTappedListener, onMediaDeletedListener)
21622162
}
21632163

2164-
fun removeMedia(notifyContentChange: Boolean = true, predicate: (Attributes) -> Boolean) {
2164+
fun removeMedia(predicate: (Attributes) -> Boolean) {
21652165
removeMedia(object : AttributePredicate {
21662166
override fun matches(attrs: Attributes): Boolean {
21672167
return predicate(attrs)
21682168
}
2169-
}, notifyContentChange)
2169+
})
21702170
}
21712171

2172-
fun removeMedia(attributePredicate: AttributePredicate, notifyContentChange: Boolean = true) {
2173-
if (!notifyContentChange) {
2174-
disableTextChangedListener()
2175-
}
2172+
fun removeMedia(attributePredicate: AttributePredicate) {
21762173
text.getSpans(0, text.length, AztecMediaSpan::class.java)
21772174
.filter {
21782175
attributePredicate.matches(it.attributes)
@@ -2220,8 +2217,33 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
22202217
}
22212218
mediaSpan.onMediaDeleted()
22222219
}
2223-
if (!notifyContentChange) {
2224-
enableTextChangedListener()
2220+
}
2221+
2222+
fun replaceMediaSpan(aztecMediaSpan: AztecMediaSpan, predicate: (Attributes) -> Boolean) {
2223+
replaceMediaSpan(object : AttributePredicate {
2224+
override fun matches(attrs: Attributes): Boolean {
2225+
return predicate(attrs)
2226+
}
2227+
}, aztecMediaSpan)
2228+
}
2229+
2230+
fun replaceMediaSpan(attributePredicate: AttributePredicate, aztecMediaSpan: AztecMediaSpan) {
2231+
history.beforeTextChanged(this@AztecText)
2232+
text.getSpans(0, text.length, AztecMediaSpan::class.java).firstOrNull {
2233+
attributePredicate.matches(it.attributes)
2234+
}?.let { mediaSpan ->
2235+
mediaSpan.beforeMediaDeleted()
2236+
val start = text.getSpanStart(mediaSpan)
2237+
val end = text.getSpanEnd(mediaSpan)
2238+
2239+
val clickableSpan = text.getSpans(start, end, AztecMediaClickableSpan::class.java).firstOrNull()
2240+
2241+
text.removeSpan(clickableSpan)
2242+
text.removeSpan(mediaSpan)
2243+
mediaSpan.onMediaDeleted()
2244+
aztecMediaSpan.onMediaDeletedListener = onMediaDeletedListener
2245+
lineBlockFormatter.insertMediaSpanOverCurrentChar(aztecMediaSpan, start)
2246+
contentChangeWatcher.notifyContentChanged()
22252247
}
22262248
}
22272249

aztec/src/main/kotlin/org/wordpress/aztec/formatting/LineBlockFormatter.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ class LineBlockFormatter(editor: AztecText) : AztecFormatter(editor) {
150150
}
151151
}
152152

153+
fun insertMediaSpanOverCurrentChar(span: AztecMediaSpan, position: Int) {
154+
editor.removeInlineStylesFromRange(selectionStart, selectionEnd)
155+
156+
editor.editableText.setSpan(
157+
span,
158+
position,
159+
position + 1,
160+
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
161+
)
162+
163+
editor.editableText.setSpan(
164+
AztecMediaClickableSpan(span),
165+
position,
166+
position + 1,
167+
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
168+
)
169+
}
170+
153171
private fun insertMediaInline(span: AztecMediaSpan) {
154172
editor.removeInlineStylesFromRange(selectionStart, selectionEnd)
155173

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ class PlaceholderManager(
137137
currentAttributes[name] = value
138138
}
139139
val updatedAttributes = updateItem(currentAttributes, currentType)
140-
removeItem(false) { aztecAttributes ->
141-
aztecAttributes.getValue(UUID_ATTRIBUTE) == uuid
142-
}
143140
val attrs = AztecAttributes().apply {
144141
updatedAttributes.forEach { (key, value) ->
145142
setValue(key, value)
@@ -148,8 +145,11 @@ class PlaceholderManager(
148145
attrs.setValue(UUID_ATTRIBUTE, uuid)
149146
attrs.setValue(TYPE_ATTRIBUTE, type)
150147
val drawable = buildPlaceholderDrawable(adapter, attrs)
151-
aztecText.insertMediaSpan(AztecPlaceholderSpan(aztecText.context, drawable, 0, attrs,
152-
this, aztecText, WeakReference(adapter), TAG = htmlTag))
148+
val span = AztecPlaceholderSpan(aztecText.context, drawable, 0, attrs,
149+
this, aztecText, WeakReference(adapter), TAG = htmlTag)
150+
aztecText.replaceMediaSpan(span) { attributes ->
151+
attributes.getValue(UUID_ATTRIBUTE) == uuid
152+
}
153153
insertContentOverSpanWithId(uuid)
154154
} else {
155155
insertItem(type, *updateItem(null, null).toList().toTypedArray())
@@ -160,8 +160,8 @@ class PlaceholderManager(
160160
* Call this method to remove a placeholder from both the AztecText and the overlaying layer programatically.
161161
* @param predicate determines whether a span should be removed
162162
*/
163-
fun removeItem(notifyContentChange: Boolean = true, predicate: (Attributes) -> Boolean) {
164-
aztecText.removeMedia(notifyContentChange) { predicate(it) }
163+
fun removeItem(predicate: (Attributes) -> Boolean) {
164+
aztecText.removeMedia { predicate(it) }
165165
}
166166

167167
private suspend fun buildPlaceholderDrawable(adapter: PlaceholderAdapter, attrs: AztecAttributes): Drawable {
@@ -342,6 +342,12 @@ class PlaceholderManager(
342342
override fun handleTag(opening: Boolean, tag: String, output: Editable, attributes: Attributes, nestingLevel: Int): Boolean {
343343
if (opening) {
344344
val type = attributes.getValue(TYPE_ATTRIBUTE)
345+
attributes.getValue(UUID_ATTRIBUTE)?.also { uuid ->
346+
container.findViewWithTag<View>(uuid)?.let {
347+
it.visibility = View.GONE
348+
container.removeView(it)
349+
}
350+
}
345351
val adapter = adapters[type] ?: return false
346352
val aztecAttributes = AztecAttributes(attributes)
347353
aztecAttributes.setValue(UUID_ATTRIBUTE, generateUuid())

0 commit comments

Comments
 (0)