8
0

codeui.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module basic-styles/code/codeui
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  10. import codeIcon from '../../theme/icons/code.svg';
  11. import '../../theme/code.css';
  12. const CODE = 'code';
  13. /**
  14. * The code UI feature. It introduces the Code button.
  15. *
  16. * @extends module:core/plugin~Plugin
  17. */
  18. export default class CodeUI extends Plugin {
  19. /**
  20. * @inheritDoc
  21. */
  22. init() {
  23. const editor = this.editor;
  24. const t = editor.t;
  25. // Add code button to feature components.
  26. editor.ui.componentFactory.add( CODE, locale => {
  27. const command = editor.commands.get( CODE );
  28. const view = new ButtonView( locale );
  29. view.set( {
  30. label: t( 'Code' ),
  31. icon: codeIcon,
  32. tooltip: true,
  33. isToggleable: true
  34. } );
  35. view.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
  36. // Execute command.
  37. this.listenTo( view, 'execute', () => {
  38. editor.execute( CODE );
  39. editor.focus();
  40. } );
  41. return view;
  42. } );
  43. }
  44. }