Skip to content

Commit 778a70b

Browse files
committed
Add support for code inline tag and for preformat block
1 parent 6234e64 commit 778a70b

File tree

10 files changed

+109
-12
lines changed

10 files changed

+109
-12
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,8 +1139,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
11391139
AztecTextFormat.FORMAT_HEADING_3,
11401140
AztecTextFormat.FORMAT_HEADING_4,
11411141
AztecTextFormat.FORMAT_HEADING_5,
1142-
AztecTextFormat.FORMAT_HEADING_6,
1143-
AztecTextFormat.FORMAT_PREFORMAT -> blockFormatter.toggleHeading(textFormat)
1142+
AztecTextFormat.FORMAT_HEADING_6 -> blockFormatter.toggleHeading(textFormat)
11441143
AztecTextFormat.FORMAT_ITALIC,
11451144
AztecTextFormat.FORMAT_EMPHASIS,
11461145
AztecTextFormat.FORMAT_CITE,
@@ -1154,6 +1153,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
11541153
AztecTextFormat.FORMAT_ALIGN_LEFT,
11551154
AztecTextFormat.FORMAT_ALIGN_CENTER,
11561155
AztecTextFormat.FORMAT_ALIGN_RIGHT -> return blockFormatter.toggleTextAlignment(textFormat)
1156+
AztecTextFormat.FORMAT_PREFORMAT -> blockFormatter.togglePreformat()
11571157
AztecTextFormat.FORMAT_QUOTE -> blockFormatter.toggleQuote()
11581158
AztecTextFormat.FORMAT_HORIZONTAL_RULE -> lineBlockFormatter.applyHorizontalRule()
11591159
else -> {

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ class BlockFormatter(editor: AztecText,
9191
}
9292
}
9393

94+
fun togglePreformat() {
95+
if (!containsPreformat()) {
96+
if (containsOtherHeadings(AztecTextFormat.FORMAT_PREFORMAT)) {
97+
switchHeadingToPreformat()
98+
} else {
99+
applyBlockStyle(AztecTextFormat.FORMAT_PREFORMAT)
100+
}
101+
} else {
102+
removeEntireBlock(AztecPreformatSpan::class.java)
103+
}
104+
}
105+
94106
fun toggleHeading(textFormat: ITextFormat) {
95107
when (textFormat) {
96108
AztecTextFormat.FORMAT_HEADING_1,
@@ -118,15 +130,6 @@ class BlockFormatter(editor: AztecText,
118130

119131
removeBlockStyle(AztecTextFormat.FORMAT_PREFORMAT)
120132
}
121-
AztecTextFormat.FORMAT_PREFORMAT -> {
122-
if (!containsPreformat()) {
123-
if (containsOtherHeadings(AztecTextFormat.FORMAT_PREFORMAT)) {
124-
switchHeadingToPreformat()
125-
} else {
126-
applyBlockStyle(textFormat)
127-
}
128-
}
129-
}
130133
else -> {
131134
}
132135
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ 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
3637
import org.wordpress.aztec.util.convertToButtonAccessibilityProperties
3738
import org.wordpress.aztec.util.setBackgroundDrawableRes
3839
import java.util.ArrayList
@@ -569,7 +570,7 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
569570
}
570571

571572
// if text is selected and action is styling - toggle the style
572-
if (action.isStylingAction() && action != ToolbarAction.HEADING && action != ToolbarAction.LIST) {
573+
if (action.isStylingAction() && action != ToolbarAction.HEADING && action != ToolbarAction.LIST && action != ToolbarAction.CODE) {
573574
aztecToolbarListener?.onToolbarFormatButtonClicked(action.textFormats.first(), false)
574575
val returnValue = editor!!.toggleFormatting(action.textFormats.first())
575576

@@ -610,6 +611,19 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
610611
aztecToolbarListener?.onToolbarExpandButtonClicked()
611612
animateToolbarExpand()
612613
}
614+
ToolbarAction.CODE -> {
615+
editor?.apply {
616+
val spans = editableText.getSpans(selectionStart, selectionEnd, AztecPreformatSpan::class.java).size
617+
val isInlineCode = isTextSelected() && spans == 1 || inlineFormatter.containsInlineStyle(AztecTextFormat.FORMAT_CODE)
618+
if (blockFormatter.containsPreformat() || !isInlineCode) {
619+
toggleFormatting(AztecTextFormat.FORMAT_PREFORMAT)
620+
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_PREFORMAT, false)
621+
} else {
622+
toggleFormatting(AztecTextFormat.FORMAT_CODE)
623+
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_CODE, false)
624+
}
625+
}
626+
}
613627
else -> {
614628
Toast.makeText(context, "Unsupported action", Toast.LENGTH_SHORT).show()
615629
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ enum class ToolbarAction constructor(
7878
R.drawable.format_bar_button_link_selector,
7979
ToolbarActionType.OTHER,
8080
setOf(AztecTextFormat.FORMAT_LINK)),
81+
CODE(
82+
R.id.format_bar_button_code,
83+
R.drawable.format_bar_button_code_selector,
84+
ToolbarActionType.BLOCK_STYLE,
85+
setOf(AztecTextFormat.FORMAT_CODE, AztecTextFormat.FORMAT_PREFORMAT)),
8186
HORIZONTAL_RULE(
8287
R.id.format_bar_button_horizontal_rule,
8388
R.drawable.format_bar_button_horizontal_rule_selector,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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:viewportWidth="24"
7+
android:viewportHeight="24">
8+
<group
9+
android:pivotX="12"
10+
android:pivotY="12"
11+
android:scaleX="0.7"
12+
android:scaleY="0.7">
13+
<path
14+
android:fillColor="?attr/toolbarIconNormalColor"
15+
android:pathData="M20.8 10.7l-4.3-4.3-1.1 1.1 4.3 4.3c0.1 0.1 0.1 0.3 0 0.4l-4.3 4.3 1.1 1.1 4.3-4.3c0.7-0.8 0.7-1.9 0-2.6zM4.2 11.8l4.3-4.3-1-1-4.3 4.3c-0.7 0.7-0.7 1.8 0 2.5l4.3 4.3 1.1-1.1-4.3-4.3c-0.2-0.1-0.2-0.3-0.1-0.4z" />
16+
</group>
17+
18+
</vector>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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:viewportWidth="24"
7+
android:viewportHeight="24">
8+
<group
9+
android:pivotX="12"
10+
android:pivotY="12"
11+
android:scaleX="0.7"
12+
android:scaleY="0.7">
13+
<path
14+
android:fillColor="?attr/toolbarIconDisabledColor"
15+
android:pathData="M20.8 10.7l-4.3-4.3-1.1 1.1 4.3 4.3c0.1 0.1 0.1 0.3 0 0.4l-4.3 4.3 1.1 1.1 4.3-4.3c0.7-0.8 0.7-1.9 0-2.6zM4.2 11.8l4.3-4.3-1-1-4.3 4.3c-0.7 0.7-0.7 1.8 0 2.5l4.3 4.3 1.1-1.1-4.3-4.3c-0.2-0.1-0.2-0.3-0.1-0.4z" />
16+
</group>
17+
18+
</vector>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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:viewportWidth="24"
7+
android:viewportHeight="24">
8+
<group
9+
android:pivotX="12"
10+
android:pivotY="12"
11+
android:scaleX="0.7"
12+
android:scaleY="0.7">
13+
<path
14+
android:fillColor="?attr/toolbarIconHighlightColor"
15+
android:pathData="M20.8 10.7l-4.3-4.3-1.1 1.1 4.3 4.3c0.1 0.1 0.1 0.3 0 0.4l-4.3 4.3 1.1 1.1 4.3-4.3c0.7-0.8 0.7-1.9 0-2.6zM4.2 11.8l4.3-4.3-1-1-4.3 4.3c-0.7 0.7-0.7 1.8 0 2.5l4.3 4.3 1.1-1.1-4.3-4.3c-0.2-0.1-0.2-0.3-0.1-0.4z" />
16+
</group>
17+
18+
</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_code_disabled" android:state_enabled="false" />
8+
<item android:drawable="@drawable/format_bar_button_code_highlighted" android:state_checked="true" />
9+
<item android:drawable="@drawable/format_bar_button_code_highlighted" android:state_focused="true" />
10+
<item android:drawable="@drawable/format_bar_button_code" />
11+
12+
</animated-selector>

aztec/src/main/res/layout/aztec_format_bar_basic.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@
114114
android:contentDescription="@string/format_bar_description_quote">
115115
</org.wordpress.aztec.toolbar.RippleToggleButton>
116116

117+
<org.wordpress.aztec.toolbar.RippleToggleButton
118+
android:id="@+id/format_bar_button_code"
119+
style="@style/FormatBarButton"
120+
android:layout_width="wrap_content"
121+
android:layout_height="fill_parent"
122+
android:contentDescription="@string/format_bar_description_code">
123+
</org.wordpress.aztec.toolbar.RippleToggleButton>
124+
117125
<org.wordpress.aztec.toolbar.RippleToggleButton
118126
android:id="@+id/format_bar_button_bold"
119127
style="@style/FormatBarButton"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<string name="format_bar_description_align_right">Align Right</string>
2727
<string name="format_bar_description_align_center">Align Center</string>
2828
<string name="format_bar_description_quote">Block quote</string>
29+
<string name="format_bar_description_code">Code</string>
2930
<string name="format_bar_description_link">Link</string>
3031
<string name="format_bar_description_horizontal_rule">Horizontal Rule</string>
3132
<string name="format_bar_description_media_normal">Show Media Options</string>

0 commit comments

Comments
 (0)