Skip to content

Commit 738a6e8

Browse files
committed
Merge branch 'trunk' into feature/add-code-toolbar-action
2 parents 778a70b + a675243 commit 738a6e8

23 files changed

+495
-277
lines changed

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import org.wordpress.aztec.toolbar.IAztecToolbar
1111
import org.wordpress.aztec.toolbar.IAztecToolbarClickListener
1212
import java.util.ArrayList
1313

14-
open class Aztec private constructor(val visualEditor: AztecText, val toolbar: IAztecToolbar,
15-
private val toolbarClickListener: IAztecToolbarClickListener) {
14+
open class Aztec private constructor(
15+
val visualEditor: AztecText,
16+
val sourceEditor: SourceViewEditText? = null,
17+
val toolbar: IAztecToolbar,
18+
toolbarClickListener: IAztecToolbarClickListener) {
1619
private var imageGetter: Html.ImageGetter? = null
1720
private var videoThumbnailGetter: Html.VideoThumbnailGetter? = null
1821
private var imeBackListener: AztecText.OnImeBackListener? = null
@@ -27,10 +30,12 @@ open class Aztec private constructor(val visualEditor: AztecText, val toolbar: I
2730
private var onLinkTappedListener: AztecText.OnLinkTappedListener? = null
2831
private var isLinkTapEnabled: Boolean = false
2932
private var plugins: ArrayList<IAztecPlugin> = visualEditor.plugins
30-
var sourceEditor: SourceViewEditText? = null
3133

3234
init {
33-
initToolbar()
35+
initToolbar(toolbarClickListener)
36+
sourceEditor?.let {
37+
initSourceEditorHistory()
38+
}
3439
}
3540

3641
private constructor(activity: Activity, @IdRes aztecTextId: Int,
@@ -39,18 +44,10 @@ open class Aztec private constructor(val visualEditor: AztecText, val toolbar: I
3944
activity.findViewById<SourceViewEditText>(sourceTextId), activity.findViewById<AztecToolbar>(toolbarId), toolbarClickListener)
4045

4146
private constructor(activity: Activity, @IdRes aztecTextId: Int,
42-
@IdRes toolbarId: Int,
43-
toolbarClickListener: IAztecToolbarClickListener) : this(activity.findViewById<AztecText>(aztecTextId),
47+
@IdRes toolbarId: Int,
48+
toolbarClickListener: IAztecToolbarClickListener) : this(activity.findViewById<AztecText>(aztecTextId), null,
4449
activity.findViewById<AztecToolbar>(toolbarId), toolbarClickListener)
4550

46-
private constructor(visualEditor: AztecText, sourceEditor: SourceViewEditText,
47-
toolbar: AztecToolbar, toolbarClickListener: IAztecToolbarClickListener) : this(visualEditor, toolbar, toolbarClickListener) {
48-
this.sourceEditor = sourceEditor
49-
50-
initToolbar()
51-
initSourceEditorHistory()
52-
}
53-
5451
companion object Factory {
5552
@JvmStatic
5653
fun with(activity: Activity, @IdRes aztecTextId: Int, @IdRes sourceTextId: Int,
@@ -60,13 +57,13 @@ open class Aztec private constructor(val visualEditor: AztecText, val toolbar: I
6057

6158
@JvmStatic
6259
fun with(visualEditor: AztecText, sourceEditor: SourceViewEditText,
63-
toolbar: AztecToolbar, toolbarClickListener: IAztecToolbarClickListener): Aztec {
60+
toolbar: IAztecToolbar, toolbarClickListener: IAztecToolbarClickListener): Aztec {
6461
return Aztec(visualEditor, sourceEditor, toolbar, toolbarClickListener)
6562
}
6663

6764
@JvmStatic
6865
fun with(visualEditor: AztecText, toolbar: AztecToolbar, toolbarClickListener: IAztecToolbarClickListener): Aztec {
69-
return Aztec(visualEditor, toolbar, toolbarClickListener)
66+
return Aztec(visualEditor, null, toolbar, toolbarClickListener)
7067
}
7168
}
7269

@@ -162,7 +159,7 @@ open class Aztec private constructor(val visualEditor: AztecText, val toolbar: I
162159
sourceEditor?.history = visualEditor.history
163160
}
164161

165-
private fun initToolbar() {
162+
private fun initToolbar(toolbarClickListener: IAztecToolbarClickListener) {
166163
toolbar.setEditor(visualEditor, sourceEditor)
167164
toolbar.setToolbarListener(toolbarClickListener)
168165
visualEditor.setToolbar(toolbar)

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

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
7171
private lateinit var layoutExpandedTranslateInEnd: Animation
7272
private lateinit var layoutExpandedTranslateOutStart: Animation
7373

74-
private lateinit var htmlButton: RippleToggleButton
74+
private val htmlButton: RippleToggleButton? by lazy {
75+
findViewById(R.id.format_bar_button_html)
76+
}
7577
private lateinit var buttonMediaCollapsed: RippleToggleButton
7678
private lateinit var buttonMediaExpanded: RippleToggleButton
7779

@@ -91,6 +93,8 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
9193

9294
private var toolbarButtonPlugins: ArrayList<IToolbarButton> = ArrayList()
9395

96+
private var toolbarItems: ToolbarItems? = null
97+
9498
constructor(context: Context) : super(context) {
9599
initView(null)
96100
}
@@ -390,11 +394,12 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
390394
highlightAppliedStyles(selStart, selEnd)
391395
}
392396
})
397+
setupToolbarItems()
393398

394399
if (sourceEditor == null) {
395-
htmlButton.visibility = View.GONE
400+
htmlButton?.visibility = View.GONE
396401
} else {
397-
htmlButton.visibility = View.VISIBLE
402+
htmlButton?.visibility = View.VISIBLE
398403
}
399404
}
400405

@@ -423,27 +428,12 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
423428
View.inflate(context, layout, this)
424429

425430
toolbarScrolView = findViewById(R.id.format_bar_button_scroll)
426-
htmlButton = findViewById(R.id.format_bar_button_html)
427431
setBackgroundColor(toolbarBackgroundColor)
428432
findViewById<View>(R.id.format_bar_horizontal_divider)?.setBackgroundColor(toolbarBorderColor)
429433

430434
setAdvancedState()
431435
setupMediaToolbar()
432436
setupToolbarButtonsForAccessibility()
433-
434-
for (toolbarAction in ToolbarAction.values()) {
435-
findViewById<ToggleButton>(toolbarAction.buttonId)?.let {
436-
it.setOnClickListener { onToolbarAction(toolbarAction) }
437-
438-
when (toolbarAction) {
439-
ToolbarAction.HEADING -> setHeadingMenu(it)
440-
ToolbarAction.LIST -> setListMenu(it)
441-
}
442-
if (!hasCustomLayout) {
443-
it.setBackgroundDrawableRes(toolbarAction.buttonDrawableRes)
444-
}
445-
}
446-
}
447437
}
448438

449439
override fun addButton(buttonPlugin: IToolbarButton) {
@@ -728,6 +718,46 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
728718
isExpanded = true
729719
}
730720

721+
/**
722+
* Call this method before Aztec is initialized to change the items visible in the Aztec toolbar
723+
*/
724+
fun setToolbarItems(toolbarItems: ToolbarItems) {
725+
this.toolbarItems = toolbarItems
726+
}
727+
728+
private fun setupToolbarItems() {
729+
layoutExpanded = findViewById(R.id.format_bar_button_layout_expanded)
730+
val inflater = LayoutInflater.from(context)
731+
val order = toolbarItems ?: if (isAdvanced) {
732+
ToolbarItems.defaultAdvancedLayout
733+
} else {
734+
ToolbarItems.defaultBasicLayout
735+
}
736+
when (order) {
737+
is ToolbarItems.BasicLayout -> {
738+
order.addInto(layoutExpanded, inflater)
739+
}
740+
is ToolbarItems.AdvancedLayout -> {
741+
val layoutCollapsed = findViewById<LinearLayout>(R.id.format_bar_button_layout_collapsed)
742+
order.addInto(layoutExpanded, layoutCollapsed, inflater)
743+
}
744+
}
745+
746+
for (toolbarAction in ToolbarAction.values()) {
747+
findViewById<ToggleButton>(toolbarAction.buttonId)?.let {
748+
it.setOnClickListener { onToolbarAction(toolbarAction) }
749+
750+
when (toolbarAction) {
751+
ToolbarAction.HEADING -> setHeadingMenu(it)
752+
ToolbarAction.LIST -> setListMenu(it)
753+
}
754+
if (!hasCustomLayout) {
755+
it.setBackgroundDrawableRes(toolbarAction.buttonDrawableRes)
756+
}
757+
}
758+
}
759+
}
760+
731761
private fun setAdvancedState() {
732762
if (isAdvanced) {
733763
setButtonViews()
@@ -969,7 +999,8 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
969999
)
9701000
}
9711001

972-
private fun setHeadingMenu(view: View) {
1002+
private fun setHeadingMenu(view: View?) {
1003+
view ?: return
9731004
headingMenu = PopupMenu(context, view)
9741005
headingMenu?.setOnMenuItemClickListener(this)
9751006
headingMenu?.inflate(R.menu.heading)
@@ -982,7 +1013,8 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
9821013
})
9831014
}
9841015

985-
private fun setListMenu(view: View) {
1016+
private fun setListMenu(view: View?) {
1017+
view ?: return
9861018
listMenu = PopupMenu(context, view)
9871019
listMenu?.setOnMenuItemClickListener(this)
9881020
listMenu?.inflate(R.menu.list)
@@ -995,7 +1027,8 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
9951027
})
9961028
}
9971029

998-
private fun updateListMenuItem(textFormat: ITextFormat, listButton: ToggleButton) {
1030+
private fun updateListMenuItem(textFormat: ITextFormat, listButton: ToggleButton?) {
1031+
listButton ?: return
9991032
var backgroundRes = R.drawable.format_bar_button_ul_selector
10001033
var contentDescriptionRes = R.string.format_bar_description_list
10011034
var check = true
@@ -1022,7 +1055,8 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
10221055
listButton.isChecked = check
10231056
}
10241057

1025-
private fun updateHeadingMenuItem(textFormat: ITextFormat, headingButton: ToggleButton) {
1058+
private fun updateHeadingMenuItem(textFormat: ITextFormat, headingButton: ToggleButton?) {
1059+
headingButton ?: return
10261060
var backgroundRes = R.drawable.format_bar_button_heading_selector
10271061
var contentDescriptionRes = R.string.format_bar_description_heading
10281062
var check = true

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ enum class ToolbarAction constructor(
1212
override val buttonId: Int,
1313
override val buttonDrawableRes: Int,
1414
override val actionType: ToolbarActionType,
15-
override val textFormats: Set<ITextFormat> = setOf())
16-
: IToolbarAction {
15+
override val textFormats: Set<ITextFormat> = setOf(),
16+
override val layout: Int? = null)
17+
: IToolbarAction, ToolbarItems.IToolbarItem {
1718

1819
ADD_MEDIA_COLLAPSE(
1920
R.id.format_bar_button_media_collapsed,
@@ -28,71 +29,85 @@ enum class ToolbarAction constructor(
2829
HEADING(R.id.format_bar_button_heading,
2930
R.drawable.format_bar_button_heading_selector,
3031
ToolbarActionType.LINE_BLOCK,
31-
setOf(AztecTextFormat.FORMAT_NONE)),
32+
setOf(AztecTextFormat.FORMAT_NONE),
33+
R.layout.format_bar_button_heading),
3234
LIST(
3335
R.id.format_bar_button_list,
3436
R.drawable.format_bar_button_ul_selector,
3537
ToolbarActionType.BLOCK_STYLE,
36-
setOf(AztecTextFormat.FORMAT_NONE)),
38+
setOf(AztecTextFormat.FORMAT_NONE),
39+
R.layout.format_bar_button_list),
3740
BOLD(
3841
R.id.format_bar_button_bold,
3942
R.drawable.format_bar_button_bold_selector,
4043
ToolbarActionType.INLINE_STYLE,
41-
setOf(AztecTextFormat.FORMAT_STRONG, AztecTextFormat.FORMAT_BOLD)),
44+
setOf(AztecTextFormat.FORMAT_STRONG, AztecTextFormat.FORMAT_BOLD),
45+
R.layout.format_bar_button_bold),
4246
ITALIC(
4347
R.id.format_bar_button_italic,
4448
R.drawable.format_bar_button_italic_selector,
4549
ToolbarActionType.INLINE_STYLE,
46-
setOf(AztecTextFormat.FORMAT_EMPHASIS, AztecTextFormat.FORMAT_ITALIC)),
50+
setOf(AztecTextFormat.FORMAT_EMPHASIS, AztecTextFormat.FORMAT_ITALIC),
51+
R.layout.format_bar_button_italic),
4752
STRIKETHROUGH(
4853
R.id.format_bar_button_strikethrough,
4954
R.drawable.format_bar_button_strikethrough_selector,
5055
ToolbarActionType.INLINE_STYLE,
51-
setOf(AztecTextFormat.FORMAT_STRIKETHROUGH)),
56+
setOf(AztecTextFormat.FORMAT_STRIKETHROUGH),
57+
R.layout.format_bar_button_strikethrough),
5258
ALIGN_LEFT(R.id.format_bar_button_align_left,
5359
R.drawable.format_bar_button_align_left_selector,
5460
ToolbarActionType.BLOCK_STYLE,
55-
setOf(AztecTextFormat.FORMAT_ALIGN_LEFT)),
61+
setOf(AztecTextFormat.FORMAT_ALIGN_LEFT),
62+
R.layout.format_bar_button_align_left),
5663
ALIGN_CENTER(
5764
R.id.format_bar_button_align_center,
5865
R.drawable.format_bar_button_align_center_selector,
5966
ToolbarActionType.BLOCK_STYLE,
60-
setOf(AztecTextFormat.FORMAT_ALIGN_CENTER)),
67+
setOf(AztecTextFormat.FORMAT_ALIGN_CENTER),
68+
R.layout.format_bar_button_align_center),
6169
ALIGN_RIGHT(
6270
R.id.format_bar_button_align_right,
6371
R.drawable.format_bar_button_align_right_selector,
6472
ToolbarActionType.BLOCK_STYLE,
65-
setOf(AztecTextFormat.FORMAT_ALIGN_RIGHT)),
73+
setOf(AztecTextFormat.FORMAT_ALIGN_RIGHT),
74+
R.layout.format_bar_button_align_right),
6675
UNDERLINE(
6776
R.id.format_bar_button_underline,
6877
R.drawable.format_bar_button_underline_selector,
6978
ToolbarActionType.INLINE_STYLE,
70-
setOf(AztecTextFormat.FORMAT_UNDERLINE)),
79+
setOf(AztecTextFormat.FORMAT_UNDERLINE),
80+
R.layout.format_bar_button_underline),
7181
QUOTE(
7282
R.id.format_bar_button_quote,
7383
R.drawable.format_bar_button_quote_selector,
7484
ToolbarActionType.BLOCK_STYLE,
75-
setOf(AztecTextFormat.FORMAT_QUOTE)),
85+
setOf(AztecTextFormat.FORMAT_QUOTE),
86+
R.layout.format_bar_button_quote),
7687
LINK(
7788
R.id.format_bar_button_link,
7889
R.drawable.format_bar_button_link_selector,
7990
ToolbarActionType.OTHER,
80-
setOf(AztecTextFormat.FORMAT_LINK)),
91+
setOf(AztecTextFormat.FORMAT_LINK),
92+
R.layout.format_bar_button_link),
8193
CODE(
8294
R.id.format_bar_button_code,
8395
R.drawable.format_bar_button_code_selector,
8496
ToolbarActionType.BLOCK_STYLE,
85-
setOf(AztecTextFormat.FORMAT_CODE, AztecTextFormat.FORMAT_PREFORMAT)),
97+
setOf(AztecTextFormat.FORMAT_CODE, AztecTextFormat.FORMAT_PREFORMAT),
98+
R.layout.format_bar_button_code),
8699
HORIZONTAL_RULE(
87100
R.id.format_bar_button_horizontal_rule,
88101
R.drawable.format_bar_button_horizontal_rule_selector,
89102
ToolbarActionType.LINE_BLOCK,
90-
setOf(AztecTextFormat.FORMAT_HORIZONTAL_RULE)),
103+
setOf(AztecTextFormat.FORMAT_HORIZONTAL_RULE),
104+
R.layout.format_bar_button_horizontal_line),
91105
HTML(
92106
R.id.format_bar_button_html,
93107
R.drawable.format_bar_button_html_selector,
94108
ToolbarActionType.OTHER,
95-
setOf(AztecTextFormat.FORMAT_NONE)),
109+
setOf(AztecTextFormat.FORMAT_NONE),
110+
R.layout.format_bar_button_html),
96111
ELLIPSIS_COLLAPSE(
97112
R.id.format_bar_button_ellipsis_collapsed,
98113
R.drawable.format_bar_button_ellipsis_vertical_selector,

0 commit comments

Comments
 (0)