Skip to content

Commit 0afa454

Browse files
authored
Merge pull request #318 from wordpress-mobile/issue/297-heading-menu-regression
Issue/297 heading menu regression
2 parents bdb7fa7 + b274fea commit 0afa454

File tree

4 files changed

+166
-50
lines changed

4 files changed

+166
-50
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,29 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
5252
}
5353

5454
fun toggleHeading(textFormat: TextFormat) {
55-
if (!containsHeadingOnly(textFormat)) {
56-
if (containsOtherHeadings(textFormat)) {
57-
switchHeaderType(textFormat)
58-
} else {
59-
applyBlockStyle(textFormat)
55+
when (textFormat) {
56+
TextFormat.FORMAT_HEADING_1,
57+
TextFormat.FORMAT_HEADING_2,
58+
TextFormat.FORMAT_HEADING_3,
59+
TextFormat.FORMAT_HEADING_4,
60+
TextFormat.FORMAT_HEADING_5,
61+
TextFormat.FORMAT_HEADING_6 -> {
62+
if (!containsHeadingOnly(textFormat)) {
63+
if (containsOtherHeadings(textFormat)) {
64+
switchHeaderType(textFormat)
65+
} else {
66+
applyBlockStyle(textFormat)
67+
}
68+
}
6069
}
61-
} else {
62-
removeBlockStyle(textFormat)
70+
TextFormat.FORMAT_PARAGRAPH -> {
71+
val span = editableText.getSpans(selectionStart, selectionEnd, AztecHeadingSpan::class.java).firstOrNull()
72+
73+
if (span != null) {
74+
removeBlockStyle(span.textFormat)
75+
}
76+
}
77+
else -> { }
6378
}
6479
}
6580

@@ -241,7 +256,7 @@ class BlockFormatter(editor: AztecText, val listStyle: ListStyle, val quoteStyle
241256
AztecUnorderedListSpan::class.java -> AztecUnorderedListSpan(nestingLevel, attrs, listStyle)
242257
AztecListItemSpan::class.java -> AztecListItemSpan(nestingLevel, attrs)
243258
AztecQuoteSpan::class.java -> AztecQuoteSpan(nestingLevel, attrs, quoteStyle)
244-
AztecHeadingSpan::class.java -> AztecHeadingSpan(nestingLevel, attrs)
259+
AztecHeadingSpan::class.java -> AztecHeadingSpan(nestingLevel, "", attrs)
245260
else -> ParagraphSpan(nestingLevel, attrs)
246261
}
247262
}

aztec/src/main/kotlin/org/wordpress/aztec/spans/AztecHeadingSpan.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class AztecHeadingSpan @JvmOverloads constructor(
4949
"h4" -> return TextFormat.FORMAT_HEADING_4
5050
"h5" -> return TextFormat.FORMAT_HEADING_5
5151
"h6" -> return TextFormat.FORMAT_HEADING_6
52-
else -> return TextFormat.FORMAT_PARAGRAPH
52+
else -> return TextFormat.FORMAT_HEADING_1
5353
}
5454
}
5555

@@ -61,13 +61,12 @@ class AztecHeadingSpan @JvmOverloads constructor(
6161
TextFormat.FORMAT_HEADING_4 -> return AztecHeadingSpan.Heading.H4
6262
TextFormat.FORMAT_HEADING_5 -> return AztecHeadingSpan.Heading.H5
6363
TextFormat.FORMAT_HEADING_6 -> return AztecHeadingSpan.Heading.H6
64-
else -> { return AztecHeadingSpan.Heading.H1 } // just use the H!
64+
else -> { return AztecHeadingSpan.Heading.H1 }
6565
}
6666
}
6767
}
6868

69-
constructor(nestingLevel: Int, tag: String, attrs: String = "",
70-
headerStyle: BlockFormatter.HeaderStyle = BlockFormatter.HeaderStyle(0))
69+
constructor(nestingLevel: Int, tag: String, attrs: String, headerStyle: BlockFormatter.HeaderStyle = BlockFormatter.HeaderStyle(0))
7170
: this(nestingLevel, getTextFormat(tag), attrs, headerStyle)
7271

7372
override fun chooseHeight(text: CharSequence, start: Int, end: Int, spanstartv: Int, v: Int, fm: Paint.FontMetricsInt) {
@@ -154,7 +153,7 @@ class AztecHeadingSpan @JvmOverloads constructor(
154153
SCALE_H4 -> return "h4"
155154
SCALE_H5 -> return "h5"
156155
SCALE_H6 -> return "h6"
157-
else -> return "p"
156+
else -> return "h1"
158157
}
159158
}
160159

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

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
265265
button?.setOnClickListener { onToolbarAction(toolbarAction) }
266266

267267
if (toolbarAction == ToolbarAction.HEADING) {
268-
setHeaderMenu(findViewById(toolbarAction.buttonId))
268+
setHeadingMenu(findViewById(toolbarAction.buttonId))
269269
}
270270
}
271271
}
@@ -308,7 +308,7 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
308308

309309
val appliedStyles = editor!!.getAppliedStyles(selStart, selEnd)
310310
highlightActionButtons(ToolbarAction.getToolbarActionsForStyles(appliedStyles))
311-
selectHeaderMenu(appliedStyles)
311+
selectHeadingMenuItem(appliedStyles)
312312
}
313313

314314
private fun onToolbarAction(action: ToolbarAction) {
@@ -320,8 +320,8 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
320320
val textFormats = ArrayList<TextFormat>()
321321

322322
actions.forEach { if (it.isStylingAction() && it.textFormat != null) textFormats.add(it.textFormat) }
323-
if (getSelectedHeading() != null) {
324-
textFormats.add(getSelectedHeading()!!)
323+
if (getSelectedHeadingMenuItem() != null) {
324+
textFormats.add(getSelectedHeadingMenuItem()!!)
325325
}
326326
return editor!!.setSelectedStyles(textFormats)
327327
}
@@ -359,31 +359,43 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
359359
}
360360
}
361361

362-
private fun selectHeaderMenu(textFormats: ArrayList<TextFormat>) {
363-
headingMenu?.menu?.getItem(0)?.isChecked = true
364-
textFormats.forEach {
365-
when (it) {
366-
TextFormat.FORMAT_HEADING_1 -> headingMenu?.menu?.getItem(1)?.isChecked = true
367-
TextFormat.FORMAT_HEADING_2 -> headingMenu?.menu?.getItem(2)?.isChecked = true
368-
TextFormat.FORMAT_HEADING_3 -> headingMenu?.menu?.getItem(3)?.isChecked = true
369-
TextFormat.FORMAT_HEADING_4 -> headingMenu?.menu?.getItem(4)?.isChecked = true
370-
TextFormat.FORMAT_HEADING_5 -> headingMenu?.menu?.getItem(5)?.isChecked = true
371-
TextFormat.FORMAT_HEADING_6 -> headingMenu?.menu?.getItem(6)?.isChecked = true
372-
else -> {
373-
362+
private fun selectHeadingMenuItem(textFormats: ArrayList<TextFormat>) {
363+
if (textFormats.size == 0) {
364+
// Select TextFormat.FORMAT_PARAGRAPH by default.
365+
headingMenu?.menu?.getItem(0)?.isChecked = true
366+
} else {
367+
textFormats.forEach {
368+
when (it) {
369+
TextFormat.FORMAT_HEADING_1 -> headingMenu?.menu?.getItem(1)?.isChecked = true
370+
TextFormat.FORMAT_HEADING_2 -> headingMenu?.menu?.getItem(2)?.isChecked = true
371+
TextFormat.FORMAT_HEADING_3 -> headingMenu?.menu?.getItem(3)?.isChecked = true
372+
TextFormat.FORMAT_HEADING_4 -> headingMenu?.menu?.getItem(4)?.isChecked = true
373+
TextFormat.FORMAT_HEADING_5 -> headingMenu?.menu?.getItem(5)?.isChecked = true
374+
TextFormat.FORMAT_HEADING_6 -> headingMenu?.menu?.getItem(6)?.isChecked = true
375+
else -> {
376+
// Select TextFormat.FORMAT_PARAGRAPH by default.
377+
headingMenu?.menu?.getItem(0)?.isChecked = true
378+
}
374379
}
380+
381+
return
375382
}
376383
}
377384
}
378385

379-
private fun setHeaderMenu(view: View) {
386+
fun getHeadingMenu(): PopupMenu? {
387+
return headingMenu
388+
}
389+
390+
private fun setHeadingMenu(view: View) {
380391
headingMenu = PopupMenu(context, view)
381392
headingMenu?.setOnMenuItemClickListener(this)
382393
headingMenu?.inflate(R.menu.heading)
383394
}
384395

385-
fun getSelectedHeading(): TextFormat? {
386-
if (headingMenu?.menu?.getItem(1)?.isChecked!!) return TextFormat.FORMAT_HEADING_1
396+
fun getSelectedHeadingMenuItem(): TextFormat? {
397+
if (headingMenu?.menu?.getItem(0)?.isChecked!!) return TextFormat.FORMAT_PARAGRAPH
398+
else if (headingMenu?.menu?.getItem(1)?.isChecked!!) return TextFormat.FORMAT_HEADING_1
387399
else if (headingMenu?.menu?.getItem(2)?.isChecked!!) return TextFormat.FORMAT_HEADING_2
388400
else if (headingMenu?.menu?.getItem(3)?.isChecked!!) return TextFormat.FORMAT_HEADING_3
389401
else if (headingMenu?.menu?.getItem(4)?.isChecked!!) return TextFormat.FORMAT_HEADING_4

0 commit comments

Comments
 (0)