editor.js/plugins/header/header.js

170 lines
4 KiB
JavaScript
Raw Normal View History

/**
* Example of making plugin
* H e a d e r
*/
var header = (function(header_plugin) {
2017-01-25 23:41:18 +01:00
/**
* @private
*/
var methods_ = {
/**
* Binds click event to passed button
*/
addSelectTypeClickListener : function (el, type) {
el.addEventListener('click', function () {
2017-01-25 23:41:18 +01:00
methods_.selectTypeClicked(type);
}, false);
},
/**
* Replaces old header with new type
* @params {string} type - new header tagName: H1H6
*/
selectTypeClicked : function (type) {
var old_header, new_header;
/** Now current header stored as a currentNode */
old_header = codex.editor.content.currentNode.querySelector('[contentEditable]');
/** Making new header */
new_header = codex.editor.draw.node(type, ['ce-header'], { innerHTML : old_header.innerHTML });
new_header.contentEditable = true;
2017-01-25 23:41:18 +01:00
new_header.setAttribute('data-placeholder', 'Заголовок');
new_header.dataset.headerData = type;
2017-04-25 17:05:05 +02:00
codex.editor.content.switchBlock(old_header, new_header, 'header');
/** Close settings after replacing */
codex.editor.toolbar.settings.close();
}
};
/**
2017-01-25 23:41:18 +01:00
* @private
*
* Make initial header block
* @param {object} JSON with block data
* @return {Element} element to append
*/
2017-01-25 23:41:18 +01:00
var make_ = function (data) {
2017-01-31 13:52:46 +01:00
var availableTypes = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
2017-01-31 14:19:19 +01:00
tag,
headerType = 'h2';
2017-01-31 14:19:19 +01:00
if ( data && data['heading-styles'] && availableTypes.includes(data['heading-styles']) ) {
2017-01-31 14:19:19 +01:00
headerType = data['heading-styles'];
2017-01-31 14:19:19 +01:00
}
2017-01-31 14:19:19 +01:00
tag = document.createElement(headerType);
/**
* Save header type in data-attr.
* We need it in save method to extract type from HTML to JSON
*/
tag.dataset.headerData = headerType;
if (data && data.text) {
tag.textContent = data.text;
}
if (!tag.dataset.headerData) {
2017-01-31 13:52:46 +01:00
tag.dataset.headerData = 'h2';
}
tag.classList.add('ce-header');
2017-01-25 23:41:18 +01:00
tag.setAttribute('data-placeholder', 'Заголовок');
tag.contentEditable = true;
return tag;
};
header_plugin.prepareDataForSave = function(data) {
2017-01-25 23:41:18 +01:00
};
/**
* Method to render HTML block from JSON
*/
header_plugin.render = function (data) {
2017-01-25 23:41:18 +01:00
return make_(data);
};
/**
* Method to extract JSON data from HTML block
*/
header_plugin.save = function (blockContent) {
var data = {
"heading-styles": blockContent.dataset.headerData,
"format": "html",
"text": blockContent.textContent || ''
};
return data;
};
/**
* Settings panel content
* - - - - - - - - - - - - -
* | настройки H1 H2 H3 |
* - - - - - - - - - - - - -
* @return {Element} element contains all settings
*/
header_plugin.makeSettings = function () {
var holder = codex.editor.draw.node('DIV', ['cdx-plugin-settings--horisontal'], {} ),
types = {
h2: 'H2',
h3: 'H3',
h4: 'H4'
},
selectTypeButton;
/** Now add type selectors */
for (var type in types){
selectTypeButton = codex.editor.draw.node('SPAN', ['cdx-plugin-settings__item'], { textContent : types[type] });
2017-01-25 23:41:18 +01:00
methods_.addSelectTypeClickListener(selectTypeButton, type);
holder.appendChild(selectTypeButton);
}
return holder;
};
header_plugin.validate = function(data) {
2017-01-31 13:52:46 +01:00
if (data.text.trim() === '' || data['heading-styles'].trim() === ''){
return false;
}
return true;
};
header_plugin.destroy = function () {
header = null;
}
return header_plugin;
})({});