Skip to content

Commit df511d0

Browse files
committed
Add separate preformat button
1 parent 9f45ec5 commit df511d0

File tree

8 files changed

+99
-28
lines changed

8 files changed

+99
-28
lines changed

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

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import org.wordpress.aztec.R
3333
import org.wordpress.aztec.plugins.IMediaToolbarButton
3434
import org.wordpress.aztec.plugins.IToolbarButton
3535
import org.wordpress.aztec.source.SourceViewEditText
36-
import org.wordpress.aztec.spans.AztecPreformatSpan
3736
import org.wordpress.aztec.util.convertToButtonAccessibilityProperties
3837
import org.wordpress.aztec.util.setBackgroundDrawableRes
3938
import java.util.ArrayList
@@ -559,6 +558,16 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
559558
return editor!!.setSelectedStyles(textFormats)
560559
}
561560

561+
// if text is selected and action is styling - toggle the style
562+
if (action.isStylingAction() && action != ToolbarAction.HEADING && action != ToolbarAction.LIST) {
563+
aztecToolbarListener?.onToolbarFormatButtonClicked(action.textFormats.first(), false)
564+
val returnValue = editor!!.toggleFormatting(action.textFormats.first())
565+
566+
highlightAppliedStyles()
567+
568+
return returnValue
569+
}
570+
562571
// other toolbar action
563572
when (action) {
564573
ToolbarAction.ADD_MEDIA_COLLAPSE, ToolbarAction.ADD_MEDIA_EXPAND -> {
@@ -591,32 +600,8 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
591600
aztecToolbarListener?.onToolbarExpandButtonClicked()
592601
animateToolbarExpand()
593602
}
594-
ToolbarAction.CODE -> {
595-
editor?.apply {
596-
val spans = editableText.getSpans(selectionStart, selectionEnd, AztecPreformatSpan::class.java).size
597-
val isInlineCode = isTextSelected() && spans <= 1 || inlineFormatter.containsInlineStyle(AztecTextFormat.FORMAT_CODE)
598-
val containsLineBreak = editableText.toString().substring(selectionStart, selectionEnd).contains("\n")
599-
if (blockFormatter.containsPreformat() || !isInlineCode || containsLineBreak) {
600-
toggleFormatting(AztecTextFormat.FORMAT_PREFORMAT)
601-
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_PREFORMAT, false)
602-
} else {
603-
toggleFormatting(AztecTextFormat.FORMAT_CODE)
604-
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_CODE, false)
605-
}
606-
}
607-
}
608603
else -> {
609-
// if text is selected and action is styling - toggle the style
610-
if (action.isStylingAction()) {
611-
aztecToolbarListener?.onToolbarFormatButtonClicked(action.textFormats.first(), false)
612-
val returnValue = editor!!.toggleFormatting(action.textFormats.first())
613-
614-
highlightAppliedStyles()
615-
616-
return returnValue
617-
} else {
618-
Toast.makeText(context, "Unsupported action", Toast.LENGTH_SHORT).show()
619-
}
604+
Toast.makeText(context, "Unsupported action", Toast.LENGTH_SHORT).show()
620605
}
621606
}
622607
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,15 @@ enum class ToolbarAction constructor(
9393
CODE(
9494
R.id.format_bar_button_code,
9595
R.drawable.format_bar_button_code_selector,
96-
ToolbarActionType.BLOCK_STYLE,
97-
setOf(AztecTextFormat.FORMAT_CODE, AztecTextFormat.FORMAT_PREFORMAT),
96+
ToolbarActionType.INLINE_STYLE,
97+
setOf(AztecTextFormat.FORMAT_CODE),
9898
R.layout.format_bar_button_code),
99+
PREFORMAT(
100+
R.id.format_bar_button_pre,
101+
R.drawable.format_bar_button_pre_selector,
102+
ToolbarActionType.BLOCK_STYLE,
103+
setOf(AztecTextFormat.FORMAT_PREFORMAT),
104+
R.layout.format_bar_button_pre),
99105
HORIZONTAL_RULE(
100106
R.id.format_bar_button_horizontal_rule,
101107
R.drawable.format_bar_button_horizontal_rule_selector,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:width="24dp"
5+
android:height="24dp"
6+
android:tint="?attr/toolbarIconNormalColor"
7+
android:viewportWidth="24"
8+
android:viewportHeight="24">
9+
<group
10+
android:pivotX="12"
11+
android:pivotY="12"
12+
android:scaleX="0.7"
13+
android:scaleY="0.7">
14+
<path
15+
android:fillColor="@color/white"
16+
android:pathData="M18 4H6c-1.1 0-2 0.9-2 2v12c0 1.1 0.9 2 2 2h12c1.1 0 2-0.9 2-2V6c0-1.1-0.9-2-2-2zm0.5 14c0 0.3-0.2 0.5-0.5 0.5H6c-0.3 0-0.5-0.2-0.5-0.5V6c0-0.3 0.2-0.5 0.5-0.5h12c0.3 0 0.5 0.2 0.5 0.5v12zM7 16.5h6V15H7v1.5zm4-4h6V11h-6v1.5zM9 11H7v1.5h2V11zm6 5.5h2V15h-2v1.5z" />
17+
18+
</group>
19+
20+
</vector>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:width="24dp"
5+
android:height="24dp"
6+
android:tint="?attr/toolbarIconDisabledColor"
7+
android:viewportWidth="24"
8+
android:viewportHeight="24">
9+
<group
10+
android:pivotX="12"
11+
android:pivotY="12"
12+
android:scaleX="0.7"
13+
android:scaleY="0.7">
14+
<path
15+
android:fillColor="@color/white"
16+
android:pathData="M18 4H6c-1.1 0-2 0.9-2 2v12c0 1.1 0.9 2 2 2h12c1.1 0 2-0.9 2-2V6c0-1.1-0.9-2-2-2zm0.5 14c0 0.3-0.2 0.5-0.5 0.5H6c-0.3 0-0.5-0.2-0.5-0.5V6c0-0.3 0.2-0.5 0.5-0.5h12c0.3 0 0.5 0.2 0.5 0.5v12zM7 16.5h6V15H7v1.5zm4-4h6V11h-6v1.5zM9 11H7v1.5h2V11zm6 5.5h2V15h-2v1.5z" />
17+
18+
</group>
19+
20+
</vector>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:width="24dp"
5+
android:height="24dp"
6+
android:tint="?attr/toolbarIconHighlightColor"
7+
android:viewportWidth="24"
8+
android:viewportHeight="24">
9+
<group
10+
android:pivotX="12"
11+
android:pivotY="12"
12+
android:scaleX="0.7"
13+
android:scaleY="0.7">
14+
<path
15+
android:fillColor="@color/white"
16+
android:pathData="M18 4H6c-1.1 0-2 0.9-2 2v12c0 1.1 0.9 2 2 2h12c1.1 0 2-0.9 2-2V6c0-1.1-0.9-2-2-2zm0.5 14c0 0.3-0.2 0.5-0.5 0.5H6c-0.3 0-0.5-0.2-0.5-0.5V6c0-0.3 0.2-0.5 0.5-0.5h12c0.3 0 0.5 0.2 0.5 0.5v12zM7 16.5h6V15H7v1.5zm4-4h6V11h-6v1.5zM9 11H7v1.5h2V11zm6 5.5h2V15h-2v1.5z" />
17+
18+
</group>
19+
20+
</vector>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
tools:targetApi="lollipop">
6+
7+
<item android:drawable="@drawable/format_bar_button_pre_disabled" android:state_enabled="false" />
8+
<item android:drawable="@drawable/format_bar_button_pre_highlighted" android:state_checked="true" />
9+
<item android:drawable="@drawable/format_bar_button_pre_highlighted" android:state_focused="true" />
10+
<item android:drawable="@drawable/format_bar_button_pre" />
11+
12+
</animated-selector>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<org.wordpress.aztec.toolbar.RippleToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/format_bar_button_pre"
4+
style="@style/FormatBarButton"
5+
android:layout_width="wrap_content"
6+
android:layout_height="fill_parent"
7+
android:contentDescription="@string/format_bar_description_pre" />

aztec/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<string name="format_bar_description_align_center">Align Center</string>
2828
<string name="format_bar_description_quote">Block quote</string>
2929
<string name="format_bar_description_code">Code</string>
30+
<string name="format_bar_description_pre">Preformatted code</string>
3031
<string name="format_bar_description_link">Link</string>
3132
<string name="format_bar_description_horizontal_rule">Horizontal Rule</string>
3233
<string name="format_bar_description_media_normal">Show Media Options</string>

0 commit comments

Comments
 (0)