8
0

highlightui.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module highlight/highlightui
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import HighlightEditing from './highlightediting';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import highlightIcon from '@ckeditor/ckeditor5-core/theme/icons/input.svg';
  12. import highlightRemoveIcon from '@ckeditor/ckeditor5-core/theme/icons/low-vision.svg';
  13. import Model from '../../ckeditor5-ui/src/model';
  14. import createSplitButtonDropdown from '@ckeditor/ckeditor5-ui/src/dropdown/button/createsplitbuttondropdown';
  15. /**
  16. * The default Highlight UI plugin.
  17. *
  18. * @extends module:core/plugin~Plugin
  19. */
  20. export default class HighlightUI extends Plugin {
  21. /**
  22. * @inheritDoc
  23. */
  24. static get requires() {
  25. return [ HighlightEditing ];
  26. }
  27. /**
  28. * @inheritDoc
  29. */
  30. static get pluginName() {
  31. return 'HighlightUI';
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. init() {
  37. const editor = this.editor;
  38. const highlighters = editor.config.get( 'highlight' );
  39. for ( const highlighter of highlighters ) {
  40. this._addButton( highlighter );
  41. }
  42. // Add rubber button
  43. const componentFactory = editor.ui.componentFactory;
  44. componentFactory.add( 'highlightRemove', locale => {
  45. const buttonView = new ButtonView( locale );
  46. buttonView.set( {
  47. label: 'Remove highlighting',
  48. icon: highlightRemoveIcon,
  49. tooltip: true
  50. } );
  51. this.listenTo( buttonView, 'execute', () => {
  52. // TODO: minor duplication of code
  53. editor.execute( 'highlight' );
  54. editor.editing.view.focus();
  55. } );
  56. return buttonView;
  57. } );
  58. // Add highlight dropdown
  59. componentFactory.add( 'highlightDropdown', locale => {
  60. const model = new Model( {
  61. label: 'Highlight',
  62. withText: false,
  63. selected: highlighters[ 0 ].class,
  64. icon: highlightIcon
  65. } );
  66. const buttons = highlighters.map( highlighter => componentFactory.create( 'highlight-' + highlighter.class ) );
  67. buttons.push( componentFactory.create( 'highlightRemove' ) );
  68. const buttonView = componentFactory.create( 'highlight-' + highlighters[ 0 ].class );
  69. const dropdown = createSplitButtonDropdown( model, buttons, locale, buttonView );
  70. buttons.map( buttonView => {
  71. this.listenTo( buttonView, 'execute', () => {
  72. if ( dropdown.buttonView.buttonView.class !== buttonView.class ) {
  73. const newButton = componentFactory.create( 'highlight-' + buttonView.class );
  74. dropdown.buttonView.swapButton( newButton );
  75. }
  76. } );
  77. } );
  78. return dropdown;
  79. } );
  80. }
  81. _addButton( highlighter ) {
  82. const editor = this.editor;
  83. const command = editor.commands.get( 'highlight' );
  84. editor.ui.componentFactory.add( 'highlight-' + highlighter.class, locale => {
  85. const buttonView = new ButtonView( locale );
  86. buttonView.set( {
  87. label: highlighter.title,
  88. icon: highlightIcon,
  89. tooltip: true,
  90. class: highlighter.class
  91. } );
  92. // Bind button model to command.
  93. buttonView.bind( 'isEnabled' ).to( command, 'isEnabled' );
  94. buttonView.bind( 'isOn' ).to( command, 'value', value => {
  95. return value === highlighter.class;
  96. } );
  97. // Execute command.
  98. this.listenTo( buttonView, 'execute', () => {
  99. editor.execute( 'highlight', { class: highlighter.class } );
  100. editor.editing.view.focus();
  101. } );
  102. buttonView.iconView.extendTemplate( {
  103. attributes: { style: highlighter.type === 'pen' ? { color: highlighter.color } : { backgroundColor: highlighter.color } }
  104. } );
  105. return buttonView;
  106. } );
  107. }
  108. }