Skip to content

Commit 67515e6

Browse files
committed
Move layout to ToolbarAction.kt and improve ToolbarItems.kt
1 parent 50e58fd commit 67515e6

File tree

3 files changed

+246
-98
lines changed

3 files changed

+246
-98
lines changed

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

Lines changed: 27 additions & 14 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,31 +29,37 @@ 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,
@@ -62,32 +69,38 @@ enum class ToolbarAction constructor(
6269
R.id.format_bar_button_align_right,
6370
R.drawable.format_bar_button_align_right_selector,
6471
ToolbarActionType.BLOCK_STYLE,
65-
setOf(AztecTextFormat.FORMAT_ALIGN_RIGHT)),
72+
setOf(AztecTextFormat.FORMAT_ALIGN_RIGHT),
73+
R.layout.format_bar_button_align_center),
6674
UNDERLINE(
6775
R.id.format_bar_button_underline,
6876
R.drawable.format_bar_button_underline_selector,
6977
ToolbarActionType.INLINE_STYLE,
70-
setOf(AztecTextFormat.FORMAT_UNDERLINE)),
78+
setOf(AztecTextFormat.FORMAT_UNDERLINE),
79+
R.layout.format_bar_button_underline),
7180
QUOTE(
7281
R.id.format_bar_button_quote,
7382
R.drawable.format_bar_button_quote_selector,
7483
ToolbarActionType.BLOCK_STYLE,
75-
setOf(AztecTextFormat.FORMAT_QUOTE)),
84+
setOf(AztecTextFormat.FORMAT_QUOTE),
85+
R.layout.format_bar_button_quote),
7686
LINK(
7787
R.id.format_bar_button_link,
7888
R.drawable.format_bar_button_link_selector,
7989
ToolbarActionType.OTHER,
80-
setOf(AztecTextFormat.FORMAT_LINK)),
90+
setOf(AztecTextFormat.FORMAT_LINK),
91+
R.layout.format_bar_button_link),
8192
HORIZONTAL_RULE(
8293
R.id.format_bar_button_horizontal_rule,
8394
R.drawable.format_bar_button_horizontal_rule_selector,
8495
ToolbarActionType.LINE_BLOCK,
85-
setOf(AztecTextFormat.FORMAT_HORIZONTAL_RULE)),
96+
setOf(AztecTextFormat.FORMAT_HORIZONTAL_RULE),
97+
R.layout.format_bar_button_horizontal_line),
8698
HTML(
8799
R.id.format_bar_button_html,
88100
R.drawable.format_bar_button_html_selector,
89101
ToolbarActionType.OTHER,
90-
setOf(AztecTextFormat.FORMAT_NONE)),
102+
setOf(AztecTextFormat.FORMAT_NONE),
103+
R.layout.format_bar_button_html),
91104
ELLIPSIS_COLLAPSE(
92105
R.id.format_bar_button_ellipsis_collapsed,
93106
R.drawable.format_bar_button_ellipsis_vertical_selector,

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

Lines changed: 88 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@ sealed class ToolbarItems {
1212
* This class defines the elements in the basic layout. It contains a set of visible items in a given order.
1313
* The plugins are added automatically at the end if they are missing from the toolbarElements object.
1414
*/
15-
data class BasicLayout(val toolbarItemGroup: ToolbarItemGroup) : ToolbarItems() {
16-
constructor(vararg toolbarItems: ToolbarItem): this(toolbarItemGroup = ToolbarItemGroup(linkedSetOf(*toolbarItems)))
17-
init {
18-
if (!toolbarItemGroup.toolbarItems.contains(ToolbarItem.PLUGINS)) {
19-
toolbarItemGroup.toolbarItems.add(ToolbarItem.PLUGINS)
20-
}
21-
}
15+
class BasicLayout(vararg toolbarItems: IToolbarItem) : ToolbarItems() {
16+
val sanitizedInput = toolbarItems.toList().sanitize(containsPlugins = true)
2217

2318
/**
2419
* Draws toolbar elements into the given layout
2520
*/
2621
fun addInto(toolbarContainer: LinearLayout, inflater: LayoutInflater) {
27-
addItems(toolbarContainer, inflater, toolbarItemGroup)
22+
sanitizedInput.forEachIndexed { index, toolbarElement ->
23+
toolbarElement.addInto(toolbarContainer, inflater, index)
24+
}
2825
}
2926
}
3027

@@ -33,108 +30,115 @@ sealed class ToolbarItems {
3330
* Collapsed items are visible by default and the expanded items are visible on button click. The Plugins element
3431
* is added to the expanded items if missing. Plugins are never visible in the collapsed items.
3532
*/
36-
data class AdvancedLayout(val expandedItemGroup: ToolbarItemGroup, val collapsedItemGroup: ToolbarItemGroup) : ToolbarItems() {
37-
init {
38-
if (!expandedItemGroup.toolbarItems.contains(ToolbarItem.PLUGINS)) {
39-
expandedItemGroup.toolbarItems.add(ToolbarItem.PLUGINS)
40-
}
41-
if (collapsedItemGroup.toolbarItems.contains(ToolbarItem.PLUGINS)) {
42-
collapsedItemGroup.toolbarItems.remove(ToolbarItem.PLUGINS)
43-
}
44-
}
33+
class AdvancedLayout(expandedItems: List<IToolbarItem>, collapsedItems: List<IToolbarItem>) : ToolbarItems() {
34+
val sanitizedExpandedItems = expandedItems.sanitize(containsPlugins = true)
35+
val sanitizedCollapsedItems = collapsedItems.sanitize(containsPlugins = false, listOfUsedItems = sanitizedExpandedItems)
4536

4637
/**
4738
* Draws the expanded items into the expanded container and collapsed items into the collapsed container.
4839
*/
4940
fun addInto(expandedContainer: LinearLayout, collapsedContainer: LinearLayout, inflater: LayoutInflater) {
50-
addItems(expandedContainer, inflater, expandedItemGroup)
51-
addItems(collapsedContainer, inflater, collapsedItemGroup)
41+
sanitizedExpandedItems.forEachIndexed { index, toolbarElement ->
42+
toolbarElement.addInto(expandedContainer, inflater, index)
43+
}
44+
sanitizedCollapsedItems.forEachIndexed { index, toolbarElement ->
45+
toolbarElement.addInto(collapsedContainer, inflater, index)
46+
}
5247
}
5348
}
5449

5550
companion object {
56-
fun addItems(container: LinearLayout, inflater: LayoutInflater, toolbarItemGroup: ToolbarItemGroup) {
57-
var offset = 0
58-
toolbarItemGroup.toolbarItems.forEachIndexed { index, toolbarElement ->
59-
if (toolbarItemGroup.verticalDividerIndices.contains(index)) {
60-
val view = inflater.inflate(R.layout.format_bar_vertical_divider, null)
61-
container.addView(view, index + offset)
62-
offset += 1
63-
}
64-
toolbarElement.addInto(container, inflater, index + offset)
65-
}
66-
}
67-
6851
/**
6952
* The default order of elements in the basic layout. It's used when there is no order added by the user.
7053
*/
7154
val defaultBasicLayout = BasicLayout(
72-
ToolbarItem.HEADING,
73-
ToolbarItem.LIST,
74-
ToolbarItem.QUOTE,
75-
ToolbarItem.BOLD,
76-
ToolbarItem.ITALIC,
77-
ToolbarItem.LINK,
78-
ToolbarItem.UNDERLINE,
79-
ToolbarItem.STRIKETHROUGH,
80-
ToolbarItem.ALIGN_LEFT,
81-
ToolbarItem.ALIGN_CENTER,
82-
ToolbarItem.ALIGN_RIGHT,
83-
ToolbarItem.HORIZONTAL_RULE,
84-
ToolbarItem.PLUGINS,
85-
ToolbarItem.HTML
55+
ToolbarAction.HEADING,
56+
ToolbarAction.LIST,
57+
ToolbarAction.QUOTE,
58+
ToolbarAction.BOLD,
59+
ToolbarAction.ITALIC,
60+
ToolbarAction.LINK,
61+
ToolbarAction.UNDERLINE,
62+
ToolbarAction.STRIKETHROUGH,
63+
ToolbarAction.ALIGN_LEFT,
64+
ToolbarAction.ALIGN_CENTER,
65+
ToolbarAction.ALIGN_RIGHT,
66+
ToolbarAction.HORIZONTAL_RULE,
67+
PLUGINS,
68+
ToolbarAction.HTML
8669
)
8770

8871
/**
8972
* The default order of elements in the advanced layout. It's used when there is no order added by the user.
9073
*/
9174
val defaultAdvancedLayout = AdvancedLayout(
92-
expandedItemGroup = ToolbarItemGroup(linkedSetOf(
93-
ToolbarItem.LINK,
94-
ToolbarItem.UNDERLINE,
95-
ToolbarItem.STRIKETHROUGH,
96-
ToolbarItem.ALIGN_LEFT,
97-
ToolbarItem.ALIGN_CENTER,
98-
ToolbarItem.ALIGN_RIGHT,
99-
ToolbarItem.HORIZONTAL_RULE,
100-
ToolbarItem.PLUGINS,
101-
ToolbarItem.HTML
102-
)),
103-
collapsedItemGroup = ToolbarItemGroup(linkedSetOf(
104-
ToolbarItem.HEADING,
105-
ToolbarItem.LIST,
106-
ToolbarItem.QUOTE,
107-
ToolbarItem.BOLD,
108-
ToolbarItem.ITALIC
109-
)))
75+
expandedItems = mutableListOf(
76+
ToolbarAction.LINK,
77+
ToolbarAction.UNDERLINE,
78+
ToolbarAction.STRIKETHROUGH,
79+
ToolbarAction.ALIGN_LEFT,
80+
ToolbarAction.ALIGN_CENTER,
81+
ToolbarAction.ALIGN_RIGHT,
82+
ToolbarAction.HORIZONTAL_RULE,
83+
PLUGINS,
84+
ToolbarAction.HTML
85+
),
86+
collapsedItems = mutableListOf(
87+
ToolbarAction.HEADING,
88+
ToolbarAction.LIST,
89+
ToolbarAction.QUOTE,
90+
ToolbarAction.BOLD,
91+
ToolbarAction.ITALIC
92+
))
11093
}
11194

112-
data class ToolbarItemGroup(val toolbarItems: LinkedHashSet<ToolbarItem>, val verticalDividerIndices: Set<Int> = emptySet())
113-
11495
/**
11596
* A list of supported default toolbar elements. If you need to add custom toolbar elements, create a new Plugin.
11697
*/
117-
enum class ToolbarItem(val layout: Int? = null) {
118-
HEADING(R.layout.format_bar_button_heading),
119-
LIST(R.layout.format_bar_button_list),
120-
QUOTE(R.layout.format_bar_button_quote),
121-
BOLD(R.layout.format_bar_button_bold),
122-
ITALIC(R.layout.format_bar_button_italic),
123-
LINK(R.layout.format_bar_button_link),
124-
UNDERLINE(R.layout.format_bar_button_underline),
125-
STRIKETHROUGH(R.layout.format_bar_button_strikethrough),
126-
ALIGN_LEFT(R.layout.format_bar_button_align_left),
127-
ALIGN_CENTER(R.layout.format_bar_button_align_center),
128-
ALIGN_RIGHT(R.layout.format_bar_button_align_right),
129-
HORIZONTAL_RULE(R.layout.format_bar_button_horizontal_line),
130-
HTML(R.layout.format_bar_button_html),
131-
PLUGINS;
98+
fun IToolbarItem.addInto(toolbarContainer: LinearLayout, inflater: LayoutInflater, index: Int) {
99+
this.layout?.let {
100+
val view = inflater.inflate(it, null)
101+
toolbarContainer.addView(view, index)
102+
}
103+
}
132104

133-
fun addInto(toolbarContainer: LinearLayout, inflater: LayoutInflater, index: Int) {
134-
layout?.let {
135-
val view = inflater.inflate(layout, null)
136-
toolbarContainer.addView(view, index)
105+
protected fun List<IToolbarItem>.sanitize(containsPlugins: Boolean, listOfUsedItems: List<IToolbarItem> = emptyList()): List<IToolbarItem> {
106+
val usedToolbarActions = mutableSetOf<ToolbarAction>()
107+
usedToolbarActions.addAll(listOfUsedItems.filterIsInstance(ToolbarAction::class.java).toSet())
108+
val result = mutableListOf<IToolbarItem>()
109+
var hasPlugins = false
110+
for (toolbarItem in this) {
111+
when (toolbarItem) {
112+
is ToolbarAction -> {
113+
if (toolbarItem.layout != null && !usedToolbarActions.contains(toolbarItem)) {
114+
usedToolbarActions.add(toolbarItem)
115+
result.add(toolbarItem)
116+
}
117+
}
118+
is PLUGINS -> {
119+
if (!hasPlugins && containsPlugins) {
120+
hasPlugins = true
121+
result.add(toolbarItem)
122+
}
123+
}
124+
is DIVIDER -> {
125+
result.add(toolbarItem)
126+
}
137127
}
138128
}
129+
if (!hasPlugins && containsPlugins) {
130+
result.add(PLUGINS)
131+
}
132+
return result
133+
}
134+
135+
interface IToolbarItem {
136+
val layout: Int?
137+
}
138+
object PLUGINS : IToolbarItem {
139+
override val layout: Int? = null
140+
}
141+
object DIVIDER : IToolbarItem {
142+
override val layout: Int = R.layout.format_bar_vertical_divider
139143
}
140144
}

0 commit comments

Comments
 (0)