8
0

bootstrap-ui-inner.js 11 KB

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