Skip to content

Commit a0f4367

Browse files
committed
Add a parameter to update method to handle media inserted before the placeholder
1 parent 37d0eb7 commit a0f4367

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,29 @@ class PlaceholderManager(
109109
* @param shouldMergeItem this method should return true when the previous type is compatible and should be updated
110110
* @param updateItem function to update current parameters with new params
111111
*/
112-
suspend fun insertOrUpdateItem(type: String, shouldMergeItem: (currentItemType: String) -> Boolean = { true }, updateItem: (currentAttributes: Map<String, String>?, currentType: String?) -> Map<String, String>) {
113-
val currentItem = getTargetItem()
114-
val currentType = currentItem?.attributes?.getValue(TYPE_ATTRIBUTE)
112+
suspend fun insertOrUpdateItem(
113+
type: String,
114+
shouldMergeItem: (currentItemType: String) -> Boolean = { true },
115+
updateItem: (
116+
currentAttributes: Map<String, String>?,
117+
currentType: String?,
118+
placeAtStart: Boolean
119+
) -> Map<String, String>
120+
) {
121+
val targetItem = getTargetItem()
122+
val targetSpan = targetItem?.span
123+
val currentType = targetSpan?.attributes?.getValue(TYPE_ATTRIBUTE)
115124
if (currentType != null && shouldMergeItem(currentType)) {
116125
val adapter = adapters[type]
117126
?: throw IllegalArgumentException("Adapter for inserted type not found. Register it with `registerAdapter` method")
118127
val currentAttributes = mutableMapOf<String, String>()
119-
val uuid = currentItem.attributes.getValue(UUID_ATTRIBUTE)
120-
for (i in 0 until currentItem.attributes.length) {
121-
val name = currentItem.attributes.getQName(i)
122-
val value = currentItem.attributes.getValue(name)
128+
val uuid = targetSpan.attributes.getValue(UUID_ATTRIBUTE)
129+
for (i in 0 until targetSpan.attributes.length) {
130+
val name = targetSpan.attributes.getQName(i)
131+
val value = targetSpan.attributes.getValue(name)
123132
currentAttributes[name] = value
124133
}
125-
val updatedAttributes = updateItem(currentAttributes, currentType)
134+
val updatedAttributes = updateItem(currentAttributes, currentType, targetItem.placeAtStart)
126135
val attrs = AztecAttributes().apply {
127136
updatedAttributes.forEach { (key, value) ->
128137
setValue(key, value)
@@ -138,11 +147,13 @@ class PlaceholderManager(
138147
}
139148
insertContentOverSpanWithId(uuid)
140149
} else {
141-
insertItem(type, *updateItem(null, null).toList().toTypedArray())
150+
insertItem(type, *updateItem(null, null, false).toList().toTypedArray())
142151
}
143152
}
144153

145-
private fun getTargetItem(): AztecPlaceholderSpan? {
154+
private data class TargetItem(val span: AztecPlaceholderSpan, val placeAtStart: Boolean)
155+
156+
private fun getTargetItem(): TargetItem? {
146157
if (aztecText.length() == 0) {
147158
return null
148159
}
@@ -153,13 +164,16 @@ class PlaceholderManager(
153164
val selectionEndPlusOne = (selectionStart + 1).coerceAtMost(aztecText.length())
154165
val selectionEndPlusTwo = (selectionStart + 2).coerceAtMost(aztecText.length())
155166
val editableText = aztecText.editableText
167+
var placeAtStart = false
156168
val (from, to) = if (editableText[selectionStartMinusOne] == Constants.IMG_CHAR) {
157169
selectionStartMinusOne to selectionStart
158170
} else if (editableText[selectionStartMinusOne] == '\n' && editableText[selectionStartMinusTwo] == Constants.IMG_CHAR) {
159171
selectionStartMinusTwo to selectionStart
160172
} else if (editableText[selectionEndPlusOne] == Constants.IMG_CHAR) {
173+
placeAtStart = true
161174
selectionEndPlusOne to (selectionEndPlusOne + 1).coerceAtMost(aztecText.length())
162175
} else if (editableText[selectionEndPlusOne] == '\n' && editableText[selectionEndPlusTwo] == Constants.IMG_CHAR) {
176+
placeAtStart = true
163177
selectionEndPlusTwo to (selectionEndPlusTwo + 1).coerceAtMost(aztecText.length())
164178
} else {
165179
selectionStart to selectionEnd
@@ -168,7 +182,7 @@ class PlaceholderManager(
168182
from,
169183
to,
170184
AztecPlaceholderSpan::class.java
171-
).lastOrNull()
185+
).map { TargetItem(it, placeAtStart) }.lastOrNull()
172186
}
173187

174188
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class PlaceholderTest {
106106
ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 1")
107107
ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 2")
108108

109-
Assert.assertEquals("${placeholderWithCaption("Caption 2 - Caption 1")}<p>Line 1</p>", editText.toHtml())
109+
Assert.assertEquals("${placeholderWithCaption("Caption 1 - Caption 2")}<p>Line 1</p>", editText.toHtml())
110110

111111
placeholderManager.removeItem {
112112
it.getValue("uuid") == uuid

0 commit comments

Comments
 (0)