Skip to content

Commit bebccdc

Browse files
committed
Merge branch 'feature/implement-merging-of-placeholders' into feature/implement-delete-or-update
2 parents 57c49f2 + d6837e8 commit bebccdc

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

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

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

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

2172-
fun removeMedia(attributePredicate: AttributePredicate) {
2172+
fun removeMedia(attributePredicate: AttributePredicate, notifyContentChange: Boolean = true) {
2173+
if (!notifyContentChange) {
2174+
disableTextChangedListener()
2175+
}
21732176
text.getSpans(0, text.length, AztecMediaSpan::class.java)
21742177
.filter {
21752178
attributePredicate.matches(it.attributes)
@@ -2217,6 +2220,9 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
22172220
}
22182221
mediaSpan.onMediaDeleted()
22192222
}
2223+
if (!notifyContentChange) {
2224+
enableTextChangedListener()
2225+
}
22202226
}
22212227

22222228
interface AttributePredicate {

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable
77
import android.text.Editable
88
import android.text.Layout
99
import android.text.Spanned
10+
import android.util.Log
1011
import android.view.MotionEvent
1112
import android.view.View
1213
import android.view.ViewTreeObserver
@@ -110,7 +111,15 @@ class PlaceholderManager(
110111
* @param updateItem function to update current parameters with new params
111112
*/
112113
suspend fun insertOrUpdateItem(type: String, shouldMergeItem: (currentItemType: String) -> Boolean = { true }, updateItem: (currentAttributes: Map<String, String>?, currentType: String?) -> Map<String, String>) {
113-
val from = (aztecText.selectionStart - 1).coerceAtLeast(0)
114+
val previousIndex = (aztecText.selectionStart - 1).coerceAtLeast(0)
115+
val indexBeforePrevious = (aztecText.selectionStart - 2).coerceAtLeast(0)
116+
val from = if (aztecText.editableText[previousIndex] == Constants.IMG_CHAR) {
117+
previousIndex
118+
} else if (aztecText.editableText[previousIndex] == '\n') {
119+
indexBeforePrevious
120+
} else {
121+
aztecText.selectionStart
122+
}
114123
val editableText = aztecText.editableText
115124
val currentItem = editableText.getSpans(
116125
from,
@@ -141,7 +150,9 @@ class PlaceholderManager(
141150
currentAttributes[name] = value
142151
}
143152
val updatedAttributes = updateItem(currentAttributes, currentType)
144-
removeItem(uuid)
153+
removeItem(false) { aztecAttributes ->
154+
aztecAttributes.getValue(UUID_ATTRIBUTE) == uuid
155+
}
145156
val attrs = AztecAttributes().apply {
146157
updatedAttributes.forEach { (key, value) ->
147158
setValue(key, value)
@@ -188,8 +199,8 @@ class PlaceholderManager(
188199
* Call this method to remove a placeholder from both the AztecText and the overlaying layer programmatically.
189200
* @param predicate determines whether a span should be removed
190201
*/
191-
fun removeItem(predicate: (Attributes) -> Boolean) {
192-
aztecText.removeMedia { predicate(it) }
202+
fun removeItem(notifyContentChange: Boolean = true, predicate: (Attributes) -> Boolean) {
203+
aztecText.removeMedia(notifyContentChange) { predicate(it) }
193204
}
194205

195206
/**
@@ -210,9 +221,13 @@ class PlaceholderManager(
210221
* Call this method to reload all the placeholders
211222
*/
212223
suspend fun reloadAllPlaceholders() {
213-
positionToIdMutex.withLock {
214-
positionToId.forEach {
215-
insertContentOverSpanWithId(it.uuid)
224+
val tempPositionToId = positionToId.toList()
225+
tempPositionToId.forEach { placeholder ->
226+
val isValid = positionToIdMutex.withLock {
227+
positionToId.contains(placeholder)
228+
}
229+
if (isValid) {
230+
insertContentOverSpanWithId(placeholder.uuid)
216231
}
217232
}
218233
}

0 commit comments

Comments
 (0)