Skip to content

Commit cd306bb

Browse files
authored
Merge pull request #232 from wordpress-mobile/issue/220-fix-highlighting-of-shared-styles
Issue/220 fix highlighting of shared styles
2 parents e4e2f91 + 8e38558 commit cd306bb

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class AztecText : EditText, TextWatcher {
174174
var consumeKeyEvent = false
175175
history.beforeTextChanged(toFormattedHtml())
176176
if (keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_DOWN) {
177+
inlineFormatter.tryRemoveLeadingInlineStyle()
177178
consumeKeyEvent = blockFormatter.tryRemoveBlockStyleFromFirstLine()
178179
}
179180

@@ -514,8 +515,8 @@ class AztecText : EditText, TextWatcher {
514515
}
515516

516517
val newLine = textChangedEventDetails.isNewLine()
517-
blockFormatter.handleBlockStyling(text, textChangedEventDetails)
518518
inlineFormatter.handleInlineStyling(textChangedEventDetails)
519+
blockFormatter.handleBlockStyling(text, textChangedEventDetails)
519520
lineBlockFormatter.handleLineBlockStyling(textChangedEventDetails)
520521

521522
isMediaAdded = text.getSpans(0, text.length, AztecMediaSpan::class.java).isNotEmpty()
@@ -876,6 +877,8 @@ class AztecText : EditText, TextWatcher {
876877
override fun sendKeyEvent(event: KeyEvent): Boolean {
877878
if (event.action === KeyEvent.ACTION_DOWN && event.keyCode === KeyEvent.KEYCODE_DEL) {
878879
history.beforeTextChanged(toFormattedHtml())
880+
881+
inlineFormatter.tryRemoveLeadingInlineStyle()
879882
val isStyleRemoved = blockFormatter.tryRemoveBlockStyleFromFirstLine()
880883
if (isStyleRemoved) {
881884
history.handleHistory(this@AztecText)
@@ -981,7 +984,8 @@ class AztecText : EditText, TextWatcher {
981984
return@filter attributePredicate.matches(it.attributes as Attributes)
982985
} else {
983986
return@filter false
984-
}}
987+
}
988+
}
985989
.map { it.attributes }
986990
}
987991
}

aztec/src/main/kotlin/org/wordpress/aztec/formatting/BlockFormatter.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ class BlockFormatter(editor: AztecText, listStyle: ListStyle, quoteStyle: QuoteS
245245
text.setSpan(blockSpan, text.getSpanStart(blockSpan), end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
246246
}
247247
}
248+
249+
//remove inline styles from the ZWJ characters we inserted
250+
var indexOfZWJ = editableText.indexOf(Constants.ZWJ_CHAR)
251+
while (indexOfZWJ >= 0) {
252+
editor.removeInlineStylesFromRange(indexOfZWJ, indexOfZWJ + 1)
253+
indexOfZWJ = editableText.indexOf(Constants.ZWJ_CHAR, indexOfZWJ + 1)
254+
}
255+
248256
}
249257

250258
fun removeBlockStyle(textFormat: TextFormat) {
@@ -540,11 +548,13 @@ class BlockFormatter(editor: AztecText, listStyle: ListStyle, quoteStyle: QuoteS
540548
val spans = editableText.getSpans(textChangedEvent.inputStart, textChangedEvent.inputStart, AztecLineBlockSpan::class.java)
541549

542550
spans.forEach {
543-
val previousCharacter =
544-
if (textChangedEvent.isAddingCharacters) editableText[textChangedEvent.inputStart - 1]
545-
else editableText[textChangedEvent.inputEnd]
551+
if (textChangedEvent.inputStart > 0) {
552+
val previousCharacter =
553+
if (textChangedEvent.isAddingCharacters) editableText[textChangedEvent.inputStart - 1]
554+
else editableText[textChangedEvent.inputEnd]
546555

547-
if (previousCharacter == '\n') return@forEach
556+
if (previousCharacter == '\n') return@forEach
557+
}
548558

549559
val deletingLastCharacter = !textChangedEvent.isAddingCharacters && editableText.length == textChangedEvent.inputEnd
550560
if (deletingLastCharacter) return@forEach

aztec/src/main/kotlin/org/wordpress/aztec/formatting/InlineFormatter.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,23 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle, val headerSty
403403
}
404404
}
405405

406-
return editableText.subSequence(start, end).toString() == builder.toString()
406+
val originalText = editableText.subSequence(start, end).replace("\n".toRegex(), "")
407+
val textOfCombinedSpans = builder.toString().replace("\n".toRegex(), "")
408+
409+
return originalText.isNotEmpty() && originalText == textOfCombinedSpans
410+
}
411+
}
412+
413+
fun tryRemoveLeadingInlineStyle() {
414+
val selectionStart = editor.selectionStart
415+
val selectionEnd = editor.selectionEnd
416+
417+
if (selectionStart == 1 && selectionEnd == selectionStart) {
418+
editableText.getSpans(0, 0, AztecInlineSpan::class.java).forEach {
419+
if (editableText.getSpanEnd(it) == selectionEnd && editableText.getSpanEnd(it) == selectionStart) {
420+
editableText.removeSpan(it)
421+
}
422+
}
407423
}
408424
}
409425
}

0 commit comments

Comments
 (0)