@@ -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,
@@ -129,7 +138,7 @@ class PlaceholderManager(
129138 currentAttributes[name] = value
130139 }
131140 val updatedAttributes = updateItem(currentAttributes, currentType)
132- removeItem { aztecAttributes ->
141+ removeItem( false ) { aztecAttributes ->
133142 aztecAttributes.getValue(UUID_ATTRIBUTE ) == uuid
134143 }
135144 val attrs = AztecAttributes ().apply {
@@ -152,8 +161,8 @@ class PlaceholderManager(
152161 * Call this method to remove a placeholder from both the AztecText and the overlaying layer programatically.
153162 * @param predicate determines whether a span should be removed
154163 */
155- fun removeItem (predicate : (Attributes ) -> Boolean ) {
156- aztecText.removeMedia { predicate(it) }
164+ fun removeItem (notifyContentChange : Boolean = true, predicate : (Attributes ) -> Boolean ) {
165+ aztecText.removeMedia(notifyContentChange) { predicate(it) }
157166 }
158167
159168 private suspend fun buildPlaceholderDrawable (adapter : PlaceholderAdapter , attrs : AztecAttributes ): Drawable {
@@ -166,9 +175,13 @@ class PlaceholderManager(
166175 * Call this method to reload all the placeholders
167176 */
168177 suspend fun reloadAllPlaceholders () {
169- positionToIdMutex.withLock {
170- positionToId.forEach {
171- insertContentOverSpanWithId(it.uuid)
178+ val tempPositionToId = positionToId.toList()
179+ tempPositionToId.forEach { placeholder ->
180+ val isValid = positionToIdMutex.withLock {
181+ positionToId.contains(placeholder)
182+ }
183+ if (isValid) {
184+ insertContentOverSpanWithId(placeholder.uuid)
172185 }
173186 }
174187 }
@@ -401,14 +414,17 @@ class PlaceholderManager(
401414 }
402415
403416 private suspend fun clearAllViews () {
417+ Log .d(" vojta" , " Before clearing all with lock" )
404418 positionToIdMutex.withLock {
419+ Log .d(" vojta" , " Clearing all with lock" )
405420 for (placeholder in positionToId) {
406421 container.findViewWithTag<View >(placeholder.uuid)?.let {
407422 it.visibility = View .GONE
408423 container.removeView(it)
409424 }
410425 }
411426 positionToId.clear()
427+ Log .d(" vojta" , " Cleared all with lock" )
412428 }
413429 }
414430
0 commit comments