creator.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Plugin from './plugin.js';
  7. /**
  8. * Basic creator class.
  9. *
  10. * @class core.Creator
  11. * @extends core.Plugin
  12. */
  13. export default class Creator extends Plugin {
  14. create() {
  15. return this.editor.ui.init();
  16. }
  17. destroy() {
  18. super.destroy();
  19. if ( this._elementReplacement ) {
  20. this.editor.element.style.display = '';
  21. this._elementReplacement.remove();
  22. }
  23. const ui = this.editor.ui;
  24. this.editor.ui = null;
  25. return ui.destroy();
  26. }
  27. updateEditorElement() {
  28. Creator.setDataInElement( this.editor.element, this.editor.getData() );
  29. }
  30. loadDataFromEditorElement() {
  31. this.editor.setData( Creator.getDataFromElement( this.editor.element ) );
  32. }
  33. /**
  34. * @param {HTMLElement} [newElement]
  35. */
  36. _replaceElement( newElement ) {
  37. if ( !newElement ) {
  38. newElement = this.editor.ui.chrome.view.element;
  39. }
  40. this._elementReplacement = newElement;
  41. const editorEl = this.editor.element;
  42. editorEl.style.display = 'none';
  43. editorEl.parentNode.insertBefore( newElement, editorEl.nextSibling );
  44. }
  45. /**
  46. * Gets data from a given source element.
  47. *
  48. * @param {HTMLElement} el The element from which the data will be retrieved.
  49. * @returns {String} The data string.
  50. */
  51. static getDataFromElement( el ) {
  52. if ( el instanceof HTMLTextAreaElement ) {
  53. return el.value;
  54. }
  55. return el.innerHTML;
  56. }
  57. /**
  58. * Sets data in a given element.
  59. *
  60. * @param {HTMLElement} el The element in which the data will be set.
  61. * @param {String} data The data string.
  62. */
  63. static setDataInElement( el, data ) {
  64. if ( el instanceof HTMLTextAreaElement ) {
  65. el.value = data;
  66. }
  67. el.innerHTML = data;
  68. }
  69. }