This commit is contained in:
George Berezhnoy 2017-11-25 14:29:45 +03:00
commit 2d25181e19

View file

@ -1,19 +1,39 @@
/**
* Paragraph Plugin
* Creates DIV tag and adds content to this tag
*/
* @class Paragraph
* Paragraph plugin for CodexEditor
*
* @author CodeX Team (team@ifmo.su)
* @copyright CodeX Team 2017
* @license The MIT License (MIT)
* @version 2.0.0
*
*
* @typedef {Object} ParagraphData
* @property {String} text HTML content to insert to paragraph element
*
*/
class Paragraph {
export default class Paragraph {
/**
* Get the name of the plugin
*
* @returns {string} The plugin name
*/
static get name() {
return 'paragraph';
}
/**
* Render plugin`s html and set initial content
*
* @param {ParagraphData} data initial plugin content
*/
constructor(data={}) {
this.CSS = {
this._CSS = {
wrapper: 'ce-paragraph'
};
@ -21,22 +41,34 @@ class Paragraph {
this.data = data;
this.element = this.render();
this._element = this._render();
}
render() {
/**
* Create div element and add needed css classes
*
* @returns {HTMLDivElement} Created DIV element
* @private
*/
_render() {
let div = document.createElement('DIV');
div.classList.add(this.CSS.wrapper);
div.classList.add(this._CSS.wrapper);
div.contentEditable = true;
return div;
}
/**
* Check if saved text is empty
*
* @param {ParagraphData} savedData data received from plugins`s element
* @returns {boolean} false if saved text is empty, true otherwise
*/
validate(savedData) {
if (savedData.text.trim() === '') {
@ -49,15 +81,25 @@ class Paragraph {
}
/**
* Get plugin`s element HTMLDivElement
*
* @returns {HTMLDivElement} Plugin`s element
*/
get html() {
return this.element;
return this._element;
}
/**
* Get current plugin`s data
*
* @returns {ParagraphData} Current data
*/
get data() {
let text = this.element.innerHTML;
let text = this._element.innerHTML;
this._data.text = text;
@ -65,11 +107,16 @@ class Paragraph {
}
/**
* Set new data for plugin
*
* @param {ParagraphData} data data to set
*/
set data(data) {
Object.assign(this._data, data);
this.element.innerHTML = this._data.text || '';
this._element.innerHTML = this._data.text || '';
}