Skip to content

Commit f571866

Browse files
committed
Improve behaviour of removeImage
1 parent cdc744b commit f571866

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

aztec/src/test/kotlin/org/wordpress/aztec/ImageBlockTest.kt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.robolectric.Robolectric
1111
import org.robolectric.RobolectricTestRunner
1212
import org.wordpress.aztec.source.SourceViewEditText
1313
import org.wordpress.aztec.toolbar.AztecToolbar
14+
import org.xml.sax.Attributes
1415

1516
@RunWith(RobolectricTestRunner::class)
1617
class ImageBlockTest {
@@ -211,4 +212,70 @@ class ImageBlockTest {
211212

212213
Assert.assertEquals("<p>Line 1</p><img id=\"1234\" /><p>Line 2</p><p>Line 3</p>", editText.toHtml())
213214
}
215+
216+
@Test
217+
@Throws(Exception::class)
218+
fun addAndRemoveImage() {
219+
val initialHtml = "<p>Line 1</p><p>Line 2</p>"
220+
editText.fromHtml(initialHtml)
221+
222+
editText.setSelection(editText.editableText.indexOf("1") + 2)
223+
val attributes = AztecAttributes()
224+
attributes.setValue("id", "1234")
225+
editText.insertImage(null, attributes)
226+
227+
Assert.assertEquals("<p>Line 1</p><img id=\"1234\" /><p>Line 2</p>", editText.toHtml())
228+
229+
editText.removeMedia(object : AztecText.AttributePredicate {
230+
override fun matches(attrs: Attributes): Boolean {
231+
return attrs.getValue("id") == "1234"
232+
}
233+
})
234+
235+
Assert.assertEquals(initialHtml, editText.toHtml())
236+
}
237+
238+
@Test
239+
@Throws(Exception::class)
240+
fun addAndRemoveImageAtTheBeginning() {
241+
val initialHtml = "Line 1"
242+
editText.fromHtml(initialHtml)
243+
244+
editText.setSelection(0)
245+
val attributes = AztecAttributes()
246+
attributes.setValue("id", "1234")
247+
editText.insertImage(null, attributes)
248+
249+
Assert.assertEquals("<img id=\"1234\" />Line 1", editText.toHtml())
250+
251+
editText.removeMedia(object : AztecText.AttributePredicate {
252+
override fun matches(attrs: Attributes): Boolean {
253+
return attrs.getValue("id") == "1234"
254+
}
255+
})
256+
257+
Assert.assertEquals(initialHtml, editText.toHtml())
258+
}
259+
260+
@Test
261+
@Throws(Exception::class)
262+
fun addAndRemoveImageAtTheBeginningOfPlaceholder() {
263+
val initialHtml = "<p>Line 1</p>"
264+
editText.fromHtml(initialHtml)
265+
266+
editText.setSelection(0)
267+
val attributes = AztecAttributes()
268+
attributes.setValue("id", "1234")
269+
editText.insertImage(null, attributes)
270+
271+
Assert.assertEquals("<img id=\"1234\" /><p>Line 1</p>", editText.toHtml())
272+
273+
editText.removeMedia(object : AztecText.AttributePredicate {
274+
override fun matches(attrs: Attributes): Boolean {
275+
return attrs.getValue("id") == "1234"
276+
}
277+
})
278+
279+
Assert.assertEquals(initialHtml, editText.toHtml())
280+
}
214281
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ class ImageWithCaptionAdapter(
8282
private const val SRC_ATTRIBUTE = "src"
8383

8484
suspend fun insertImageWithCaption(placeholderManager: PlaceholderManager, src: String, caption: String) {
85-
placeholderManager.insertItem(ADAPTER_TYPE, SRC_ATTRIBUTE to src, CAPTION_ATTRIBUTE to caption)
85+
// placeholderManager.insertItem(ADAPTER_TYPE, SRC_ATTRIBUTE to src, CAPTION_ATTRIBUTE to caption)
86+
placeholderManager.insertOrUpdateItem(ADAPTER_TYPE) { currentAttributes, type ->
87+
if (currentAttributes == null || type != ADAPTER_TYPE) {
88+
mapOf(SRC_ATTRIBUTE to src, CAPTION_ATTRIBUTE to caption)
89+
} else {
90+
val currentCaption = currentAttributes[CAPTION_ATTRIBUTE]
91+
mapOf(SRC_ATTRIBUTE to src, CAPTION_ATTRIBUTE to "$caption - $currentCaption")
92+
}
93+
}
8694
}
8795
}
8896
}

0 commit comments

Comments
 (0)