@@ -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