@@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable
77import android.text.Editable
88import android.text.Layout
99import android.text.Spanned
10+ import android.util.Log
1011import android.view.MotionEvent
1112import android.view.View
1213import 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