bootstrap-ui-inner.js 7.8 KB

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