Skip to content

Commit 30322dd

Browse files
committed
Support both strong, bold but apply strong only
1 parent af5fa2b commit 30322dd

File tree

10 files changed

+144
-53
lines changed

10 files changed

+144
-53
lines changed

app/src/main/kotlin/org/wordpress/aztec/demo/MediaToolbarAction.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.wordpress.aztec.toolbar.IToolbarAction
66
import org.wordpress.aztec.toolbar.ToolbarActionType
77

88
enum class MediaToolbarAction constructor(override val buttonId: Int, override val actionType: ToolbarActionType,
9-
override val textFormat: ITextFormat) : IToolbarAction {
10-
GALLERY(R.id.media_bar_button_gallery, ToolbarActionType.OTHER, AztecTextFormat.FORMAT_NONE),
11-
CAMERA(R.id.media_bar_button_camera, ToolbarActionType.OTHER, AztecTextFormat.FORMAT_NONE)
12-
}
9+
override val textFormats: Set<ITextFormat> = setOf()) : IToolbarAction {
10+
GALLERY(R.id.media_bar_button_gallery, ToolbarActionType.OTHER, setOf(AztecTextFormat.FORMAT_NONE)),
11+
CAMERA(R.id.media_bar_button_camera, ToolbarActionType.OTHER, setOf(AztecTextFormat.FORMAT_NONE))
12+
}

aztec/src/main/java/org/wordpress/aztec/Html.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.wordpress.aztec.spans.AztecStyleBoldSpan;
4747
import org.wordpress.aztec.spans.AztecStyleCiteSpan;
4848
import org.wordpress.aztec.spans.AztecStyleItalicSpan;
49+
import org.wordpress.aztec.spans.AztecStyleStrongSpan;
4950
import org.wordpress.aztec.spans.AztecSubscriptSpan;
5051
import org.wordpress.aztec.spans.AztecSuperscriptSpan;
5152
import org.wordpress.aztec.spans.AztecTypefaceMonospaceSpan;
@@ -310,7 +311,7 @@ private void handleStartTag(String tag, Attributes attributes, int nestingLevel)
310311
} else if (tag.equalsIgnoreCase("aztec_cursor")) {
311312
handleCursor(spannableStringBuilder);
312313
} else if (tag.equalsIgnoreCase("strong")) {
313-
start(spannableStringBuilder, AztecTextFormat.FORMAT_BOLD, attributes);
314+
start(spannableStringBuilder, AztecTextFormat.FORMAT_STRONG, attributes);
314315
} else if (tag.equalsIgnoreCase("b")) {
315316
start(spannableStringBuilder, AztecTextFormat.FORMAT_BOLD, attributes);
316317
} else if (tag.equalsIgnoreCase("em")) {
@@ -403,7 +404,7 @@ private void handleEndTag(String tag, int nestingLevel) {
403404
if (tag.equalsIgnoreCase("br")) {
404405
handleBr(spannableStringBuilder);
405406
} else if (tag.equalsIgnoreCase("strong")) {
406-
end(spannableStringBuilder, AztecTextFormat.FORMAT_BOLD);
407+
end(spannableStringBuilder, AztecTextFormat.FORMAT_STRONG);
407408
} else if (tag.equalsIgnoreCase("b")) {
408409
end(spannableStringBuilder, AztecTextFormat.FORMAT_BOLD);
409410
} else if (tag.equalsIgnoreCase("em")) {
@@ -488,6 +489,9 @@ private static void start(SpannableStringBuilder text, AztecTextFormat textForma
488489
case FORMAT_BOLD:
489490
newSpan = new AztecStyleBoldSpan(attributes);
490491
break;
492+
case FORMAT_STRONG:
493+
newSpan = new AztecStyleStrongSpan(attributes);
494+
break;
491495
case FORMAT_ITALIC:
492496
newSpan = new AztecStyleItalicSpan(attributes);
493497
break;
@@ -537,6 +541,9 @@ private static void end(SpannableStringBuilder text, AztecTextFormat textFormat)
537541
case FORMAT_BOLD:
538542
span = (AztecStyleBoldSpan) getLast(text, AztecStyleBoldSpan.class);
539543
break;
544+
case FORMAT_STRONG:
545+
span = (AztecStyleStrongSpan) getLast(text, AztecStyleStrongSpan.class);
546+
break;
540547
case FORMAT_ITALIC:
541548
span = (AztecStyleItalicSpan) getLast(text, AztecStyleItalicSpan.class);
542549
break;

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import org.wordpress.aztec.spans.IAztecBlockSpan
8585
import org.wordpress.aztec.spans.UnknownClickableSpan
8686
import org.wordpress.aztec.spans.UnknownHtmlSpan
8787
import org.wordpress.aztec.toolbar.IAztecToolbar
88+
import org.wordpress.aztec.toolbar.ToolbarAction
8889
import org.wordpress.aztec.util.AztecLog
8990
import org.wordpress.aztec.util.InstanceStateUtils
9091
import org.wordpress.aztec.util.SpanWrapper
@@ -822,7 +823,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
822823
}
823824

824825
plugins.filter { it is IToolbarButton }
825-
.map { (it as IToolbarButton).action.textFormat }
826+
.flatMap { (it as IToolbarButton).action.textFormats }
826827
.forEach {
827828
if (contains(it, newSelStart, newSelEnd)) {
828829
styles.add(it)
@@ -864,12 +865,13 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
864865
AztecTextFormat.FORMAT_HEADING_5,
865866
AztecTextFormat.FORMAT_HEADING_6,
866867
AztecTextFormat.FORMAT_PREFORMAT -> blockFormatter.toggleHeading(textFormat)
867-
AztecTextFormat.FORMAT_BOLD,
868868
AztecTextFormat.FORMAT_ITALIC,
869869
AztecTextFormat.FORMAT_CITE,
870870
AztecTextFormat.FORMAT_UNDERLINE,
871871
AztecTextFormat.FORMAT_STRIKETHROUGH,
872872
AztecTextFormat.FORMAT_CODE -> inlineFormatter.toggle(textFormat)
873+
AztecTextFormat.FORMAT_BOLD,
874+
AztecTextFormat.FORMAT_STRONG -> inlineFormatter.toggleAny(ToolbarAction.BOLD.textFormats)
873875
AztecTextFormat.FORMAT_UNORDERED_LIST -> blockFormatter.toggleUnorderedList()
874876
AztecTextFormat.FORMAT_ORDERED_LIST -> blockFormatter.toggleOrderedList()
875877
AztecTextFormat.FORMAT_ALIGN_LEFT,
@@ -878,7 +880,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
878880
AztecTextFormat.FORMAT_QUOTE -> blockFormatter.toggleQuote()
879881
AztecTextFormat.FORMAT_HORIZONTAL_RULE -> lineBlockFormatter.applyHorizontalRule()
880882
else -> {
881-
plugins.filter { it is IToolbarButton && textFormat == it.action.textFormat }
883+
plugins.filter { it is IToolbarButton && it.action.textFormats.contains(textFormat) }
882884
.map { it as IToolbarButton }
883885
.forEach { it.toggle() }
884886
}
@@ -894,6 +896,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
894896
AztecTextFormat.FORMAT_HEADING_5,
895897
AztecTextFormat.FORMAT_HEADING_6 -> return lineBlockFormatter.containsHeading(format, selStart, selEnd)
896898
AztecTextFormat.FORMAT_BOLD,
899+
AztecTextFormat.FORMAT_STRONG,
897900
AztecTextFormat.FORMAT_ITALIC,
898901
AztecTextFormat.FORMAT_CITE,
899902
AztecTextFormat.FORMAT_UNDERLINE,
@@ -1278,6 +1281,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
12781281

12791282
fun removeInlineStylesFromRange(start: Int, end: Int) {
12801283
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_BOLD, start, end)
1284+
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_STRONG, start, end)
12811285
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_ITALIC, start, end)
12821286
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_CITE, start, end)
12831287
inlineFormatter.removeInlineStyle(AztecTextFormat.FORMAT_STRIKETHROUGH, start, end)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum class AztecTextFormat : ITextFormat {
1414
FORMAT_UNORDERED_LIST,
1515
FORMAT_ORDERED_LIST,
1616
FORMAT_BOLD,
17+
FORMAT_STRONG,
1718
FORMAT_ITALIC,
1819
FORMAT_CITE,
1920
FORMAT_UNDERLINE,

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.wordpress.aztec.spans.AztecStrikethroughSpan
1616
import org.wordpress.aztec.spans.AztecStyleBoldSpan
1717
import org.wordpress.aztec.spans.AztecStyleCiteSpan
1818
import org.wordpress.aztec.spans.AztecStyleItalicSpan
19+
import org.wordpress.aztec.spans.AztecStyleStrongSpan
1920
import org.wordpress.aztec.spans.AztecStyleSpan
2021
import org.wordpress.aztec.spans.AztecUnderlineSpan
2122
import org.wordpress.aztec.spans.IAztecInlineSpan
@@ -38,6 +39,17 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
3839
}
3940
}
4041

42+
/**
43+
* Removes all formats in the list but if none found, applies the first one
44+
*/
45+
fun toggleAny(textFormats: Set<ITextFormat>) {
46+
if (!textFormats
47+
.filter { containsInlineStyle(it) }
48+
.fold(false, { found, containedTextFormat -> removeInlineStyle(containedTextFormat); true })) {
49+
applyInlineStyle(textFormats.first())
50+
}
51+
}
52+
4153
fun handleInlineStyling(textChangedEvent: TextChangedEvent) {
4254
if (textChangedEvent.isEndOfBufferMarker()) return
4355

@@ -51,6 +63,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
5163
for (item in editor.selectedStyles) {
5264
when (item) {
5365
AztecTextFormat.FORMAT_BOLD,
66+
AztecTextFormat.FORMAT_STRONG,
5467
AztecTextFormat.FORMAT_ITALIC,
5568
AztecTextFormat.FORMAT_CITE,
5669
AztecTextFormat.FORMAT_STRIKETHROUGH,
@@ -149,14 +162,13 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
149162
}
150163
}
151164

152-
// if we already have same span within selection - reuse it by changing it's bounds
165+
// if we already have same span within selection - reuse its attributes
153166
if (existingSpanOfSameStyle != null) {
154167
editableText.removeSpan(existingSpanOfSameStyle)
155-
(existingSpanOfSameStyle as IAztecInlineSpan).attributes = attrs
156-
applySpan(existingSpanOfSameStyle as IAztecInlineSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
157-
} else {
158-
applySpan(spanToApply, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
168+
spanToApply.attributes = attrs
159169
}
170+
171+
applySpan(spanToApply, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
160172
}
161173

162174
joinStyleSpans(start, end)
@@ -184,6 +196,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
184196
fun spanToTextFormat(span: IAztecInlineSpan): ITextFormat? {
185197
when (span::class.java) {
186198
AztecStyleBoldSpan::class.java -> return AztecTextFormat.FORMAT_BOLD
199+
AztecStyleStrongSpan::class.java -> return AztecTextFormat.FORMAT_STRONG
187200
AztecStyleItalicSpan::class.java -> return AztecTextFormat.FORMAT_ITALIC
188201
AztecStyleCiteSpan::class.java -> return AztecTextFormat.FORMAT_CITE
189202
AztecStrikethroughSpan::class.java -> return AztecTextFormat.FORMAT_STRIKETHROUGH
@@ -234,16 +247,12 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
234247
}
235248

236249
fun isSameInlineSpanType(firstSpan: IAztecInlineSpan, secondSpan: IAztecInlineSpan): Boolean {
237-
if (firstSpan.javaClass == secondSpan.javaClass) {
238-
// special check for StyleSpan
239-
if (firstSpan is StyleSpan && secondSpan is StyleSpan) {
240-
return firstSpan.style == secondSpan.style
241-
} else {
242-
return true
243-
}
250+
// special check for StyleSpans
251+
if (firstSpan is StyleSpan && secondSpan is StyleSpan) {
252+
return firstSpan.style == secondSpan.style
244253
}
245254

246-
return false
255+
return firstSpan.javaClass == secondSpan.javaClass
247256
}
248257

249258
// TODO: Check if there is more efficient way to tidy spans
@@ -330,6 +339,7 @@ class InlineFormatter(editor: AztecText, val codeStyle: CodeStyle) : AztecFormat
330339
fun makeInlineSpan(textFormat: ITextFormat): IAztecInlineSpan {
331340
when (textFormat) {
332341
AztecTextFormat.FORMAT_BOLD -> return AztecStyleBoldSpan()
342+
AztecTextFormat.FORMAT_STRONG -> return AztecStyleStrongSpan()
333343
AztecTextFormat.FORMAT_ITALIC -> return AztecStyleItalicSpan()
334344
AztecTextFormat.FORMAT_CITE -> return AztecStyleCiteSpan()
335345
AztecTextFormat.FORMAT_STRIKETHROUGH -> return AztecStrikethroughSpan()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.wordpress.aztec.spans
2+
3+
import android.graphics.Typeface
4+
import org.wordpress.aztec.AztecAttributes
5+
6+
class AztecStyleStrongSpan(attributes: AztecAttributes = AztecAttributes())
7+
: AztecStyleSpan(Typeface.BOLD, attributes) {
8+
9+
override val TAG by lazy {
10+
when (style) {
11+
Typeface.BOLD -> {
12+
return@lazy "strong"
13+
}
14+
}
15+
throw IllegalArgumentException()
16+
}
17+
}

aztec/src/main/kotlin/org/wordpress/aztec/toolbar/AztecToolbar.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
155155
}
156156
KeyEvent.KEYCODE_B -> {
157157
if (event.isCtrlPressed) { // Bold = Ctrl + B
158-
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_BOLD, true)
158+
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_STRONG, true)
159159
findViewById<ToggleButton>(ToolbarAction.BOLD.buttonId).performClick()
160160
return true
161161
}
@@ -245,7 +245,7 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
245245
else -> {
246246
toolbarButtonPlugins.forEach {
247247
if (it.matchesKeyShortcut(keyCode, event)) {
248-
aztecToolbarListener?.onToolbarFormatButtonClicked(it.action.textFormat, true)
248+
aztecToolbarListener?.onToolbarFormatButtonClicked(it.action.textFormats.first(), true)
249249
it.toggle()
250250
return true
251251
}
@@ -493,7 +493,7 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
493493
val textFormats = ArrayList<ITextFormat>()
494494

495495
actions.filter { it.isStylingAction() }
496-
.forEach { textFormats.add(it.textFormat) }
496+
.forEach { textFormats.add(it.textFormats.first()) }
497497

498498
if (getSelectedHeadingMenuItem() != null) {
499499
textFormats.add(getSelectedHeadingMenuItem()!!)
@@ -503,14 +503,14 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
503503
textFormats.add(getSelectedListMenuItem()!!)
504504
}
505505

506-
aztecToolbarListener?.onToolbarFormatButtonClicked(action.textFormat, false)
506+
aztecToolbarListener?.onToolbarFormatButtonClicked(action.textFormats.first(), false)
507507
return editor!!.setSelectedStyles(textFormats)
508508
}
509509

510510
// if text is selected and action is styling - toggle the style
511511
if (action.isStylingAction() && action != ToolbarAction.HEADING && action != ToolbarAction.LIST) {
512-
aztecToolbarListener?.onToolbarFormatButtonClicked(action.textFormat, false)
513-
val returnValue = editor!!.toggleFormatting(action.textFormat)
512+
aztecToolbarListener?.onToolbarFormatButtonClicked(action.textFormats.first(), false)
513+
val returnValue = editor!!.toggleFormatting(action.textFormats.first())
514514

515515
highlightAppliedStyles()
516516

aztec/src/main/kotlin/org/wordpress/aztec/toolbar/IToolbarAction.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.wordpress.aztec.ITextFormat
1414
interface IToolbarAction {
1515
val buttonId: Int
1616
val actionType: ToolbarActionType
17-
val textFormat: ITextFormat
17+
val textFormats: Set<ITextFormat>
1818

1919
/**
2020
* Determines, whether this action performs any text styling.
@@ -26,4 +26,4 @@ interface IToolbarAction {
2626
fun isStylingAction(): Boolean {
2727
return actionType != ToolbarActionType.OTHER
2828
}
29-
}
29+
}

0 commit comments

Comments
 (0)