inlinecreator.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Creator from '/ckeditor5/creator/creator.js';
  7. import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
  8. import Editable from '/ckeditor5/editable.js';
  9. import { createEditableUI, createEditorUI } from '/ckeditor5/ui/creator-utils.js';
  10. import EditorUIView from '/ckeditor5/ui/editorui/editoruiview.js';
  11. import BoxlessEditorUI from '/tests/ckeditor5/_utils/ui/boxlesseditorui/boxlesseditorui.js';
  12. import EditableUI from '/ckeditor5/ui/editableui/editableui.js';
  13. import InlineEditableUIView from '/tests/ckeditor5/_utils/ui/editableui/inline/inlineeditableuiview.js';
  14. import Model from '/ckeditor5/ui/model.js';
  15. import FloatingToolbar from '/tests/ckeditor5/_utils/ui/floatingtoolbar/floatingtoolbar.js';
  16. import FloatingToolbarView from '/tests/ckeditor5/_utils/ui/floatingtoolbar/floatingtoolbarview.js';
  17. import { imitateFeatures, imitateDestroyFeatures } from '../imitatefeatures.js';
  18. export default class InlineCreator extends Creator {
  19. constructor( editor ) {
  20. super( editor, new HtmlDataProcessor() );
  21. this._createEditable();
  22. createEditorUI( editor, BoxlessEditorUI, EditorUIView );
  23. // Data controller mock.
  24. this._mockDataController();
  25. }
  26. create() {
  27. const editor = this.editor;
  28. const editable = editor.editables.get( 0 );
  29. // Features mock.
  30. imitateFeatures( editor );
  31. // UI.
  32. this._createToolbars();
  33. editor.ui.add( 'editable', createEditableUI( editor, editable, EditableUI, InlineEditableUIView ) );
  34. // Init.
  35. return super.create()
  36. .then( () => editor.ui.init() )
  37. // We'll be able to do that much earlier once the loading will be done to the document model,
  38. // rather than straight to the editable.
  39. .then( () => this.loadDataFromEditorElement() );
  40. }
  41. destroy() {
  42. imitateDestroyFeatures();
  43. this.updateEditorElement();
  44. super.destroy();
  45. return this.editor.ui.destroy();
  46. }
  47. _createEditable() {
  48. const editor = this.editor;
  49. const editorElement = editor.firstElement;
  50. const editableName = editor.firstElementName;
  51. const editable = new Editable( editor, editableName );
  52. editor.editables.add( editable );
  53. editable.bindTo( editorElement );
  54. editor.document.createRoot( editableName, '$root' );
  55. }
  56. _createToolbars() {
  57. const editableName = this.editor.firstElementName;
  58. const locale = this.editor.locale;
  59. const toolbar1Model = new Model( null, { editableName } );
  60. const toolbar2Model = new Model( null, { editableName } );
  61. const toolbar1 = new FloatingToolbar( toolbar1Model, new FloatingToolbarView( toolbar1Model, locale ), this.editor );
  62. const toolbar2 = new FloatingToolbar( toolbar2Model, new FloatingToolbarView( toolbar2Model, locale ), this.editor );
  63. toolbar1.addButtons( this.editor.config.toolbar );
  64. toolbar2.addButtons( this.editor.config.toolbar.reverse() );
  65. this.editor.ui.add( 'body', toolbar1 );
  66. this.editor.ui.add( 'body', toolbar2 );
  67. }
  68. _mockDataController() {
  69. const editor = this.editor;
  70. editor.data.get = ( rootName ) => {
  71. return editor.editables.get( rootName ).domElement.innerHTML + `<p>getData( '${ rootName }' )</p>`;
  72. };
  73. this.editor.data.set = ( rootName, data ) => {
  74. editor.editables.get( rootName ).domElement.innerHTML = data + `<p>setData( '${ rootName }' )</p>`;
  75. };
  76. }
  77. }