bootstrap-ui-inner.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals $, window, console:false */
  6. // Basic classes to create an editor.
  7. import StandardEditor from '@ckeditor/ckeditor5-core/src/editor/standardeditor';
  8. import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
  9. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  10. import ElementReplacer from '@ckeditor/ckeditor5-utils/src/elementreplacer';
  11. // Basic features that every editor should enable.
  12. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  13. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  14. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  15. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  16. import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
  17. // Basic features to associated with the edited content.
  18. import BoldEngine from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
  19. import ItalicEngine from '@ckeditor/ckeditor5-basic-styles/src/italicengine';
  20. import UnderlineEngine from '@ckeditor/ckeditor5-basic-styles/src/underlineengine';
  21. import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine';
  22. // Extending the StandardEditor, which brings lots of essential API.
  23. export default class BootstrapEditor extends StandardEditor {
  24. constructor( element, config ) {
  25. super( element, config );
  26. // Create the ("main") root element of the model tree.
  27. this.document.createRoot();
  28. // Use the HTML data processor in this editor.
  29. this.data.processor = new HtmlDataProcessor();
  30. // This editor uses a single editable view in DOM.
  31. this.editable = new InlineEditableUIView( this.locale );
  32. // A helper to easily replace the editor#element with editor.editable#element.
  33. this._elementReplacer = new ElementReplacer();
  34. }
  35. destroy() {
  36. // When destroyed, editor sets the output of editor#getData() into editor#element...
  37. this.updateEditorElement();
  38. // ...and restores editor#element.
  39. this._elementReplacer.restore();
  40. return super.destroy();
  41. }
  42. static create( element, config ) {
  43. return new Promise( resolve => {
  44. const editor = new this( element, config );
  45. const editable = editor.editable;
  46. resolve(
  47. editor.initPlugins()
  48. // Initialize the editable view in DOM first.
  49. .then( () => editable.init() )
  50. // Replace the editor#element with editor.editable#element.
  51. .then( () => editor._elementReplacer.replace( element, editable.element ) )
  52. // Handle the UI of the editor.
  53. .then( () => {
  54. // Create an editing root in the editing layer. It will correspond with the
  55. // document root created in the constructor().
  56. const editingRoot = editor.editing.createRoot( 'div' );
  57. // Bind the basic attributes of the editable in DOM with the editing layer.
  58. editable.bind( 'isReadOnly' ).to( editingRoot );
  59. editable.bind( 'isFocused' ).to( editor.editing.view );
  60. editable.name = editingRoot.rootName;
  61. // Setup the external Bootstrap UI so it works with the editor.
  62. setupButtons( editor );
  63. setupHeadingDropdown( editor );
  64. // Tell the world that the UI of the editor is ready to use.
  65. editor.fire( 'uiReady' );
  66. } )
  67. // Bind the editor editing layer to the editable in DOM.
  68. .then( () => editor.editing.view.attachDomRoot( editable.element ) )
  69. .then( () => editor.loadDataFromEditorElement() )
  70. // Fire the events that announce that the editor is complete and ready to use.
  71. .then( () => {
  72. editor.fire( 'dataReady' );
  73. editor.fire( 'ready' );
  74. } )
  75. .then( () => editor )
  76. );
  77. } );
  78. }
  79. }
  80. // This function activates Bold, Italic, Underline, Undo and Redo buttons in the toolbar.
  81. function setupButtons( editor ) {
  82. [ 'bold', 'italic', 'underline', 'undo', 'redo' ].forEach( commandName => {
  83. // Retrieve the editor command corresponding with the id of the button in DOM.
  84. const command = editor.commands.get( commandName );
  85. // Retrieve the jQuery object corresponding with the button in DOM.
  86. const button = $( `#${ commandName }` );
  87. // Clicking on the buttons should execute the editor command...
  88. button.click( () => editor.execute( commandName ) );
  89. // ...but it should not steal the focus so the editing is uninterrupted.
  90. button.mousedown( evt => evt.preventDefault() );
  91. // Commands can become disabled, e.g. when the editor is read-only.
  92. // Make sure the buttons reflect this state change.
  93. command.on( 'change:isEnabled', onIsEnabledChange );
  94. onIsEnabledChange();
  95. // Bold, Italic and Underline commands have a value that changes
  96. // when the selection starts in an element the command creates.
  97. // The button should indicate that e.g. editing text which is already bold.
  98. if ( !new Set( [ 'undo', 'redo' ] ).has( commandName ) ) {
  99. command.on( 'change:value', onValueChange );
  100. onValueChange();
  101. }
  102. function onValueChange() {
  103. button.toggleClass( 'active', command.value );
  104. }
  105. function onIsEnabledChange() {
  106. button.attr( 'disabled', () => !command.isEnabled );
  107. }
  108. } );
  109. }
  110. // This function activates the headings dropdown in the toolbar.
  111. function setupHeadingDropdown( editor ) {
  112. const menu = $( '.ck-editor .dropdown-menu' );
  113. const button = $( '.ck-editor .dropdown-toggle' );
  114. // Create a dropdown menu entry for each heading configuration option.
  115. editor.config.get( 'heading.options' ).map( option => {
  116. // Retrieve the editor command corresponding with the configuration option.
  117. const command = editor.commands.get( option.modelElement );
  118. // Create the menu item DOM element.
  119. const menuItem = $(
  120. `<a href="#" class="dropdown-item heading-item_${ option.modelElement }">` +
  121. `${ option.title }` +
  122. '</a>' );
  123. // Upon click, the dropdown menua item should execute the command and focus
  124. // the editing view to keep the editing process uninterrupted.
  125. menuItem.click( () => {
  126. editor.execute( option.modelElement );
  127. editor.editing.view.focus();
  128. } );
  129. menu.append( menuItem );
  130. // Make sure the dropdown and its items reflect the state of the
  131. // currently active command.
  132. command.on( 'change:value', onValueChange );
  133. onValueChange();
  134. // Heading commands can become disabled, e.g. when the editor is read-only.
  135. // Make sure the UI reflects this state change.
  136. command.on( 'change:isEnabled', onIsEnabledChange );
  137. onIsEnabledChange();
  138. function onValueChange() {
  139. if ( command.value ) {
  140. button.children( ':first' ).text( option.title );
  141. }
  142. menuItem.toggleClass( 'active', command.value );
  143. }
  144. function onIsEnabledChange() {
  145. button.attr( 'disabled', () => !command.isEnabled );
  146. }
  147. } );
  148. }
  149. // Finally, create the BootstrapEditor instance with a selected set of features.
  150. BootstrapEditor
  151. .create( $( '#editor' ).get( 0 ), {
  152. plugins: [
  153. Clipboard, Enter, Typing, Paragraph,
  154. BoldEngine, ItalicEngine, UnderlineEngine, HeadingEngine, UndoEngine
  155. ]
  156. } )
  157. .then( editor => {
  158. window.editor = editor;
  159. $( '#toggle-readonly' ).on( 'click', () => {
  160. editor.isReadOnly = !editor.isReadOnly;
  161. } );
  162. } )
  163. .catch( err => {
  164. console.error( err.stack );
  165. } );