Skip to content

Commit e54627f

Browse files
committed
Fix placeholder update when heading applied above it
1 parent 9f0780b commit e54627f

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ class PlaceholderManager(
110110
positionToId.filter {
111111
it.elementPosition >= selectionStart - 1
112112
}.forEach {
113-
insertContentOverSpanWithId(it.uuid, it.elementPosition)
113+
insertContentOverSpanWithId(it.uuid, it.elementTopOffset)
114114
}
115115
}
116116

117-
private suspend fun insertContentOverSpanWithId(uuid: String, currentPosition: Int? = null) {
117+
private suspend fun insertContentOverSpanWithId(uuid: String, currentTopOffset: Int? = null) {
118118
var aztecAttributes: AztecAttributes? = null
119119
val predicate = object : AztecText.AttributePredicate {
120120
override fun matches(attrs: Attributes): Boolean {
@@ -127,10 +127,10 @@ class PlaceholderManager(
127127
}
128128
val targetPosition = aztecText.getElementPosition(predicate) ?: return
129129

130-
insertInPosition(aztecAttributes ?: return, targetPosition, currentPosition)
130+
insertInPosition(aztecAttributes ?: return, targetPosition, currentTopOffset)
131131
}
132132

133-
private suspend fun insertInPosition(attrs: AztecAttributes, targetPosition: Int, currentPosition: Int? = null) {
133+
private suspend fun insertInPosition(attrs: AztecAttributes, targetPosition: Int, currentTopOffset: Int? = null) {
134134
if (!validateAttributes(attrs)) {
135135
return
136136
}
@@ -139,15 +139,6 @@ class PlaceholderManager(
139139
val textViewLayout: Layout = aztecText.layout
140140
val parentTextViewRect = Rect()
141141
val targetLineOffset = textViewLayout.getLineForOffset(targetPosition)
142-
if (currentPosition != null) {
143-
if (targetLineOffset != 0 && currentPosition == targetPosition) {
144-
return
145-
} else {
146-
positionToId.removeAll {
147-
it.uuid == uuid
148-
}
149-
}
150-
}
151142
textViewLayout.getLineBounds(targetLineOffset, parentTextViewRect)
152143

153144
val parentTextViewLocation = intArrayOf(0, 0)
@@ -157,6 +148,16 @@ class PlaceholderManager(
157148
parentTextViewRect.top += parentTextViewTopAndBottomOffset
158149
parentTextViewRect.bottom += parentTextViewTopAndBottomOffset
159150

151+
if (currentTopOffset != null) {
152+
if (targetLineOffset != 0 && currentTopOffset == parentTextViewRect.top) {
153+
return
154+
} else {
155+
positionToId.removeAll {
156+
it.uuid == uuid
157+
}
158+
}
159+
}
160+
160161
var box = container.findViewWithTag<View>(uuid)
161162
val exists = box != null
162163
val adapter = adapters[type]!!
@@ -178,7 +179,7 @@ class PlaceholderManager(
178179
box.tag = uuid
179180
box.setBackgroundColor(Color.TRANSPARENT)
180181
box.setOnTouchListener(adapter)
181-
positionToId.add(Placeholder(targetPosition, uuid))
182+
positionToId.add(Placeholder(targetPosition, parentTextViewRect.top, uuid))
182183
if (!exists && box.parent == null) {
183184
container.addView(box)
184185
adapter.onViewCreated(box, uuid)
@@ -433,7 +434,7 @@ class PlaceholderManager(
433434
}
434435
}
435436

436-
data class Placeholder(val elementPosition: Int, val uuid: String)
437+
data class Placeholder(val elementPosition: Int, val elementTopOffset: Int, val uuid: String)
437438

438439
companion object {
439440
private const val DEFAULT_HTML_TAG = "placeholder"

0 commit comments

Comments
 (0)