Skip to content

Commit 89d6d0f

Browse files
committed
Preserve selection when updating a gallery
1 parent 993c3e6 commit 89d6d0f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,13 @@ class PlaceholderManager(
168168
} ?: return false
169169
if (shouldUpdateItem(currentItem.attributes)) {
170170
val type = currentItem.attributes.getValue(TYPE_ATTRIBUTE)
171+
val selectionStart = aztecText.selectionStart
172+
val selectionEnd = aztecText.selectionEnd
173+
aztecText.setSelection(aztecText.editableText.getSpanStart(currentItem))
171174
updateSpan(type, currentItem, updateItem = { attributes, _ ->
172175
updateItem(attributes)
173176
}, type)
177+
aztecText.setSelection(selectionStart, selectionEnd)
174178
} else {
175179
removeItem(uuid)
176180
}

media-placeholders/src/test/java/org/wordpress/aztec/placeholders/PlaceholderTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,31 @@ class PlaceholderTest {
143143
}
144144
}
145145

146+
@Test
147+
@Throws(Exception::class)
148+
fun updatePlaceholderAtTheEnd() {
149+
runBlocking {
150+
val initialHtml = "<p>First Line</p><placeholder uuid=\"uuid123\" type=\"image_with_caption\" src=\"image.jpg;image2.jpg\" caption=\"Caption - 1, 2\" /><p>Second Line</p>"
151+
editText.fromHtml(initialHtml)
152+
editText.setSelection(editText.editableText.indexOf("First") + 1)
153+
val initialSelectionStart = editText.selectionStart
154+
val initialSelectionEnd = editText.selectionEnd
155+
156+
placeholderManager.removeOrUpdate("uuid123", shouldUpdateItem = {
157+
true
158+
}) { currentAttributes ->
159+
val result = mutableMapOf<String, String>()
160+
result["src"] = currentAttributes["src"]?.split(";")?.firstOrNull() ?: ""
161+
result["caption"] = "Updated caption"
162+
result
163+
}
164+
165+
Assert.assertEquals("<p>First Line</p><placeholder src=\"image.jpg\" caption=\"Updated caption\" uuid=\"uuid123\" type=\"image_with_caption\" /><p>Second Line</p>", editText.toHtml())
166+
Assert.assertEquals(initialSelectionStart, editText.selectionStart)
167+
Assert.assertEquals(initialSelectionEnd, editText.selectionEnd)
168+
}
169+
}
170+
146171
@Test
147172
@Throws(Exception::class)
148173
fun removePlaceholderWhenItShouldNotBeUpdated() {

0 commit comments

Comments
 (0)