bootstrap-ui-inner.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
  9. import EditorUIView from '@ckeditor/ckeditor5-ui/src/editorui/editoruiview';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
  12. import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
  13. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  14. import ElementReplacer from '@ckeditor/ckeditor5-utils/src/elementreplacer';
  15. // Interfaces to extend basic Editor API.
  16. import DataApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/dataapimixin';
  17. import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementapimixin';
  18. // Helper function for adding interfaces to the Editor class.
  19. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  20. // Helper function that gets data from HTML element that the Editor is attached to.
  21. import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
  22. // Helper function that binds editor with HTMLForm element.
  23. import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform';
  24. // Basic features that every editor should enable.
  25. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  26. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  27. import Typing from '@ckeditor/ckeditor5-typing/src/typing';
  28. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  29. import UndoEditing from '@ckeditor/ckeditor5-undo/src/undoediting';
  30. // Basic features to associated with the edited content.
  31. import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
  32. import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
  33. import UnderlineEditing from '@ckeditor/ckeditor5-basic-styles/src/underline/underlineediting';
  34. import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
  35. // The easy image integration.
  36. import EasyImage from '@ckeditor/ckeditor5-easy-image/src/easyimage';
  37. import { CS_CONFIG } from '@ckeditor/ckeditor5-cloud-services/tests/_utils/cloud-services-config';
  38. // Extending the Editor class, which brings base editor API.
  39. export default class BootstrapEditor extends Editor {
  40. constructor( element, config ) {
  41. super( config );
  42. // Remember the element the editor is created with.
  43. this.sourceElement = element;
  44. // Use the HTML data processor in this editor.
  45. this.data.processor = new HtmlDataProcessor();
  46. // Create the ("main") root element of the model tree.
  47. this.model.document.createRoot();
  48. // The UI layer of the editor.
  49. this.ui = new BootstrapEditorUI( this );
  50. // When editor#element is a textarea inside a form element
  51. // then content of this textarea will be updated on form submit.
  52. attachToForm( this );
  53. // A helper to easily replace the editor#element with editor.editable#element.
  54. this._elementReplacer = new ElementReplacer();
  55. }
  56. destroy() {
  57. // When destroyed, editor sets the output of editor#getData() into editor#element...
  58. this.updateSourceElement();
  59. // ...and restores the original editor#element...
  60. this._elementReplacer.restore();
  61. // ...and destroys the UI.
  62. this.ui.destroy();
  63. return super.destroy();
  64. }
  65. static create( element, config ) {
  66. return new Promise( resolve => {
  67. const editor = new this( element, config );
  68. const editable = editor.ui.view.editable;
  69. resolve(
  70. editor.initPlugins()
  71. .then( () => {
  72. // Initialize the UI first. See the BootstrapEditorUI class to learn more.
  73. editor.ui.init();
  74. // Replace the editor#element with editor.editable#element.
  75. editor._elementReplacer.replace( element, editable.element );
  76. // Tell the world that the UI of the editor is ready to use.
  77. editor.ui.ready();
  78. } )
  79. // Bind the editor editing layer to the editable in DOM.
  80. .then( () => editor.editing.view.attachDomRoot( editable.element ) )
  81. // Fill the editable with the initial data.
  82. .then( () => editor.data.init( getDataFromElement( element ) ) )
  83. // Fire the events that announce that the editor is complete and ready to use.
  84. .then( () => {
  85. editor.fire( 'dataReady' );
  86. editor.fire( 'ready' );
  87. } )
  88. .then( () => editor )
  89. );
  90. } );
  91. }
  92. }
  93. // Mixing interfaces, which extends basic editor API.
  94. mix( BootstrapEditor, DataApiMixin );
  95. mix( BootstrapEditor, ElementApiMixin );
  96. // The class organizing the UI of the editor, binding it with existing Bootstrap elements in DOM.
  97. class BootstrapEditorUI extends EditorUI {
  98. constructor( editor ) {
  99. super( editor );
  100. // The global UI view of the editor. It aggregates various Bootstrap DOM elements.
  101. const view = this._view = new EditorUIView( editor.locale );
  102. // This is the main editor element in the DOM.
  103. view.element = $( '.ck-editor' );
  104. // This is the editable view in the DOM. It will replace the data container in the DOM.
  105. view.editable = new InlineEditableUIView( editor.locale );
  106. // References to the dropdown elements for further usage. See #_setupBootstrapHeadingDropdown.
  107. view.dropdownMenu = view.element.find( '.dropdown-menu' );
  108. view.dropdownToggle = view.element.find( '.dropdown-toggle' );
  109. // References to the toolbar buttons for further usage. See #_setupBootstrapToolbarButtons.
  110. view.toolbarButtons = {};
  111. [ 'bold', 'italic', 'underline', 'undo', 'redo' ].forEach( name => {
  112. // Retrieve the jQuery object corresponding with the button in the DOM.
  113. view.toolbarButtons[ name ] = view.element.find( `#${ name }` );
  114. } );
  115. }
  116. get view() {
  117. return this._view;
  118. }
  119. init() {
  120. const editor = this.editor;
  121. const view = this.view;
  122. // Render the editable component in the DOM first.
  123. view.editable.render();
  124. // Create an editing root in the editing layer. It will correspond with the
  125. // document root created in the constructor().
  126. const editingRoot = editor.editing.view.document.getRoot();
  127. // Bind the basic attributes of the editable in the DOM with the editing layer.
  128. view.editable.bind( 'isReadOnly' ).to( editingRoot );
  129. view.editable.bind( 'isFocused' ).to( editor.editing.view.document );
  130. view.editable.name = editingRoot.rootName;
  131. // Setup the existing, external Bootstrap UI so it works with the rest of the editor.
  132. this._setupBootstrapToolbarButtons();
  133. this._setupBootstrapHeadingDropdown();
  134. }
  135. // This method activates Bold, Italic, Underline, Undo and Redo buttons in the toolbar.
  136. _setupBootstrapToolbarButtons() {
  137. const editor = this.editor;
  138. for ( const name in this.view.toolbarButtons ) {
  139. // Retrieve the editor command corresponding with the id of the button in DOM.
  140. const command = editor.commands.get( name );
  141. const button = this.view.toolbarButtons[ name ];
  142. // Clicking on the buttons should execute the editor command...
  143. button.click( () => editor.execute( name ) );
  144. // ...but it should not steal the focus so the editing is uninterrupted.
  145. button.mousedown( evt => evt.preventDefault() );
  146. const onValueChange = () => {
  147. button.toggleClass( 'active', command.value );
  148. };
  149. const onIsEnabledChange = () => {
  150. button.attr( 'disabled', () => !command.isEnabled );
  151. };
  152. // Commands can become disabled, e.g. when the editor is read-only.
  153. // Make sure the buttons reflect this state change.
  154. command.on( 'change:isEnabled', onIsEnabledChange );
  155. onIsEnabledChange();
  156. // Bold, Italic and Underline commands have a value that changes
  157. // when the selection starts in an element the command creates.
  158. // The button should indicate that e.g. editing text which is already bold.
  159. if ( !new Set( [ 'undo', 'redo' ] ).has( name ) ) {
  160. command.on( 'change:value', onValueChange );
  161. onValueChange();
  162. }
  163. }
  164. }
  165. // This method activates the headings dropdown in the toolbar.
  166. _setupBootstrapHeadingDropdown() {
  167. const editor = this.editor;
  168. const dropdownMenu = this.view.dropdownMenu;
  169. const dropdownToggle = this.view.dropdownToggle;
  170. // Retrieve the editor commands for heading and paragraph.
  171. const headingCommand = editor.commands.get( 'heading' );
  172. const paragraphCommand = editor.commands.get( 'paragraph' );
  173. // Create a dropdown menu entry for each heading configuration option.
  174. editor.config.get( 'heading.options' ).map( option => {
  175. // Check is options is paragraph or heading as their commands slightly differ.
  176. const isParagraph = option.model === 'paragraph';
  177. // Create the menu item DOM element.
  178. const menuItem = $(
  179. `<a href="#" class="dropdown-item heading-item_${ option.model }">` +
  180. `${ option.title }` +
  181. '</a>'
  182. );
  183. // Upon click, the dropdown menu item should execute the command and focus
  184. // the editing view to keep the editing process uninterrupted.
  185. menuItem.click( () => {
  186. const commandName = isParagraph ? 'paragraph' : 'heading';
  187. const commandValue = isParagraph ? undefined : { value: option.model };
  188. editor.execute( commandName, commandValue );
  189. editor.editing.view.focus();
  190. } );
  191. dropdownMenu.append( menuItem );
  192. const command = isParagraph ? paragraphCommand : headingCommand;
  193. // Make sure the dropdown and its items reflect the state of the
  194. // currently active command.
  195. const onValueChange = isParagraph ? onValueChangeParagraph : onValueChangeHeading;
  196. command.on( 'change:value', onValueChange );
  197. onValueChange();
  198. // Heading commands can become disabled, e.g. when the editor is read-only.
  199. // Make sure the UI reflects this state change.
  200. command.on( 'change:isEnabled', onIsEnabledChange );
  201. onIsEnabledChange();
  202. function onValueChangeHeading() {
  203. const isActive = !isParagraph && command.value === option.model;
  204. if ( isActive ) {
  205. dropdownToggle.children( ':first' ).text( option.title );
  206. }
  207. menuItem.toggleClass( 'active', isActive );
  208. }
  209. function onValueChangeParagraph() {
  210. if ( command.value ) {
  211. dropdownToggle.children( ':first' ).text( option.title );
  212. }
  213. menuItem.toggleClass( 'active', command.value );
  214. }
  215. function onIsEnabledChange() {
  216. dropdownToggle.attr( 'disabled', () => !command.isEnabled );
  217. }
  218. } );
  219. }
  220. }
  221. // Finally, create the BootstrapEditor instance with a selected set of features.
  222. BootstrapEditor
  223. .create( $( '#editor' ).get( 0 ), {
  224. plugins: [
  225. Clipboard, Enter, Typing, Paragraph, EasyImage,
  226. BoldEditing, ItalicEditing, UnderlineEditing, HeadingEditing, UndoEditing
  227. ],
  228. cloudServices: CS_CONFIG
  229. } )
  230. .then( editor => {
  231. window.editor = editor;
  232. $( '#toggle-readonly' ).on( 'click', () => {
  233. editor.isReadOnly = !editor.isReadOnly;
  234. } );
  235. } )
  236. .catch( err => {
  237. console.error( err.stack );
  238. } );