Skip to content

Commit

Permalink
Merge pull request #16035 from ckeditor/ck/app-menu-bar-highlight
Browse files Browse the repository at this point in the history
Internal (highlight): Added menu bar integration. Related to #15894.
  • Loading branch information
oleq authored Mar 21, 2024
2 parents 3c7a1a6 + 749522d commit 6a586f8
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
86 changes: 84 additions & 2 deletions packages/ckeditor5-highlight/src/highlightui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@

import { Plugin, icons } from 'ckeditor5/src/core.js';
import {
addToolbarToDropdown,
createDropdown,
ButtonView,
ListSeparatorView,
MenuBarMenuView,
MenuBarMenuListView,
MenuBarMenuListItemView,
MenuBarMenuListItemButtonView,
SplitButtonView,
ToolbarSeparatorView,
createDropdown,
addToolbarToDropdown,
type DropdownView
} from 'ckeditor5/src/ui.js';

Expand Down Expand Up @@ -89,6 +94,8 @@ export default class HighlightUI extends Plugin {
this._addRemoveHighlightButton();

this._addDropdown( options );

this._addMenuBarButton( options );
}

/**
Expand Down Expand Up @@ -252,6 +259,81 @@ export default class HighlightUI extends Plugin {
return dropdownView;
} );
}

/**
* Creates the menu bar button for highlight including submenu with available options.
*/
private _addMenuBarButton( options: Array<HighlightOption> ) {
const editor = this.editor;
const t = editor.t;

editor.ui.componentFactory.add( 'menuBar:highlight', locale => {
const command: HighlightCommand = editor.commands.get( 'highlight' )!;
const menuView = new MenuBarMenuView( locale );

menuView.buttonView.set( {
label: t( 'Highlight' ),
icon: getIconForType( 'marker' )
} );
menuView.buttonView.iconView.fillColor = 'transparent';

const listView = new MenuBarMenuListView( locale );

for ( const option of options ) {
const listItemView = new MenuBarMenuListItemView( locale, menuView );
const buttonView = new MenuBarMenuListItemButtonView( locale );

buttonView.set( {
label: option.title,
icon: getIconForType( option.type )
} );

buttonView.extendTemplate( {
attributes: {
'aria-checked': buttonView.bindTemplate.to( 'isOn' )
}
} );

buttonView.delegate( 'execute' ).to( menuView );
buttonView.bind( 'isOn' ).to( command, 'value', value => value === option.model );
buttonView.iconView.bind( 'fillColor' ).to( buttonView, 'isOn', value => value ? 'transparent' : option.color );

buttonView.on( 'execute', () => {
editor.execute( 'highlight', { value: option.model } );

editor.editing.view.focus();
} );

listItemView.children.add( buttonView );
listView.items.add( listItemView );
}

// Add remove highlight button
listView.items.add( new ListSeparatorView( locale ) );
const listItemView = new MenuBarMenuListItemView( locale, menuView );
const buttonView = new MenuBarMenuListItemButtonView( locale );

buttonView.set( {
label: t( 'Remove highlight' ),
icon: icons.eraser
} );

buttonView.delegate( 'execute' ).to( menuView );

buttonView.on( 'execute', () => {
editor.execute( 'highlight', { value: null } );

editor.editing.view.focus();
} );

listItemView.children.add( buttonView );
listView.items.add( listItemView );

menuView.panelView.children.add( listView );

return menuView;
} );
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/ckeditor5-ui/src/menubar/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ export const DefaultMenuBarItems: DeepReadonly<MenuBarConfigObject[ 'items' ]> =
'menuBar:heading',
'menuBar:fontSize',
'menuBar:fontFamily',
'menuBar:highlight',
'menuBar:textPartLanguage'
]
},
Expand Down

0 comments on commit 6a586f8

Please sign in to comment.