Skip to content

Commit 16332cb

Browse files
committed
Implement adding to a gallery when inserting an item before
1 parent 797868e commit 16332cb

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,7 @@ class PlaceholderManager(
110110
* @param updateItem function to update current parameters with new params
111111
*/
112112
suspend fun insertOrUpdateItem(type: String, shouldMergeItem: (currentItemType: String) -> Boolean = { true }, updateItem: (currentAttributes: Map<String, String>?, currentType: String?) -> Map<String, String>) {
113-
val previousIndex = (aztecText.selectionStart - 1).coerceAtLeast(0)
114-
val indexBeforePrevious = (aztecText.selectionStart - 2).coerceAtLeast(0)
115-
val from = if (aztecText.editableText.length > previousIndex && aztecText.editableText[previousIndex] == Constants.IMG_CHAR) {
116-
previousIndex
117-
} else if (aztecText.editableText.length > previousIndex && aztecText.editableText[previousIndex] == '\n') {
118-
indexBeforePrevious
119-
} else {
120-
aztecText.selectionStart
121-
}
122-
val editableText = aztecText.editableText
123-
val currentItem = editableText.getSpans(
124-
from,
125-
aztecText.selectionStart,
126-
AztecPlaceholderSpan::class.java
127-
).lastOrNull()
113+
val currentItem = getTargetItem()
128114
val currentType = currentItem?.attributes?.getValue(TYPE_ATTRIBUTE)
129115
if (currentType != null && shouldMergeItem(currentType)) {
130116
val adapter = adapters[type]
@@ -156,6 +142,35 @@ class PlaceholderManager(
156142
}
157143
}
158144

145+
private fun getTargetItem(): AztecPlaceholderSpan? {
146+
if (aztecText.length() == 0) {
147+
return null
148+
}
149+
val selectionStart = aztecText.selectionStart
150+
val selectionStartMinusOne = (selectionStart - 1).coerceAtLeast(0)
151+
val selectionStartMinusTwo = (selectionStart - 2).coerceAtLeast(0)
152+
val selectionEnd = aztecText.selectionEnd
153+
val selectionEndPlusOne = (selectionStart + 1).coerceAtMost(aztecText.length())
154+
val selectionEndPlusTwo = (selectionStart + 2).coerceAtMost(aztecText.length())
155+
val editableText = aztecText.editableText
156+
val (from, to) = if (editableText[selectionStartMinusOne] == Constants.IMG_CHAR) {
157+
selectionStartMinusOne to selectionStart
158+
} else if (editableText[selectionStartMinusOne] == '\n' && editableText[selectionStartMinusTwo] == Constants.IMG_CHAR) {
159+
selectionStartMinusTwo to selectionStart
160+
} else if (editableText[selectionEndPlusOne] == Constants.IMG_CHAR){
161+
selectionEndPlusOne to (selectionEndPlusOne + 1).coerceAtMost(aztecText.length())
162+
} else if (editableText[selectionEndPlusOne] == '\n' && editableText[selectionEndPlusTwo] == Constants.IMG_CHAR) {
163+
selectionEndPlusTwo to (selectionEndPlusTwo + 1).coerceAtMost(aztecText.length())
164+
} else {
165+
selectionStart to selectionEnd
166+
}
167+
return editableText.getSpans(
168+
from,
169+
to,
170+
AztecPlaceholderSpan::class.java
171+
).lastOrNull()
172+
}
173+
159174
/**
160175
* Call this method to remove a placeholder from both the AztecText and the overlaying layer programatically.
161176
* @param predicate determines whether a span should be removed

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ class PlaceholderTest {
6464
editText.fromHtml(initialHtml)
6565

6666
editText.setSelection(0)
67-
val attributes = AztecAttributes()
68-
attributes.setValue("id", "1234")
6967
ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 123")
7068

7169
Assert.assertEquals("<placeholder uuid=\"$uuid\" type=\"image_with_caption\" src=\"image.jpg\" caption=\"Caption 123\" /><p>Line 1</p>", editText.toHtml())
@@ -86,8 +84,6 @@ class PlaceholderTest {
8684
editText.fromHtml(initialHtml)
8785

8886
editText.setSelection(editText.editableText.indexOf("1"))
89-
val attributes = AztecAttributes()
90-
attributes.setValue("id", "1234")
9187
ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 123")
9288

9389
Assert.assertEquals("<p>Line 123<placeholder uuid=\"uuid123\" type=\"image_with_caption\" src=\"image.jpg\" caption=\"Caption 123\" /></p><p>Line 2</p>", editText.toHtml())
@@ -108,12 +104,10 @@ class PlaceholderTest {
108104
editText.fromHtml(initialHtml)
109105

110106
editText.setSelection(0)
111-
val attributes = AztecAttributes()
112-
attributes.setValue("id", "1234")
113107
ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 1")
114108
ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Caption 2")
115109

116-
Assert.assertEquals("<placeholder src=\"image.jpg\" caption=\"Caption 2 - Caption 1\" uuid=\"uuid123\" type=\"image_with_caption\" /><p>Line 1</p>", editText.toHtml())
110+
Assert.assertEquals("${placeholderWithCaption("Caption 2 - Caption 1")}<p>Line 1</p>", editText.toHtml())
117111

118112
placeholderManager.removeItem {
119113
it.getValue("uuid") == uuid
@@ -122,4 +116,36 @@ class PlaceholderTest {
122116
Assert.assertEquals(initialHtml, editText.toHtml())
123117
}
124118
}
119+
120+
@Test
121+
@Throws(Exception::class)
122+
fun insertOrUpdateAPlaceholderWhenInsertingBeforeNewLine() {
123+
runBlocking {
124+
val initialHtml = "<p>Line 1</p>${placeholderWithCaption("First")}<p>Line 2</p>"
125+
editText.fromHtml(initialHtml)
126+
127+
editText.setSelection(editText.editableText.indexOf("1"))
128+
ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Second")
129+
130+
Assert.assertEquals("<p>Line 1</p>${placeholderWithCaption("Second - First")}<p>Line 2</p>", editText.toHtml())
131+
}
132+
}
133+
134+
@Test
135+
@Throws(Exception::class)
136+
fun insertOrUpdateAPlaceholderWhenInsertingRightBefore() {
137+
runBlocking {
138+
val initialHtml = "<p>Line 1</p>${placeholderWithCaption("First")}<p>Line 2</p>"
139+
editText.fromHtml(initialHtml)
140+
141+
editText.setSelection(editText.editableText.indexOf("1") + 1)
142+
ImageWithCaptionAdapter.insertImageWithCaption(placeholderManager, "image.jpg", "Second")
143+
144+
Assert.assertEquals("<p>Line 1</p>${placeholderWithCaption("Second - First")}<p>Line 2</p>", editText.toHtml())
145+
}
146+
}
147+
148+
private fun placeholderWithCaption(caption: String): String {
149+
return "<placeholder src=\"image.jpg\" caption=\"$caption\" uuid=\"uuid123\" type=\"image_with_caption\" />"
150+
}
125151
}

0 commit comments

Comments
 (0)