editor.js/modules/toolbar/toolbox.js
khaydarov c3ee7560f1 cover restoring, versioning, sanitize and new initialization structure (#105)
* cover restoring fixed

* upd

* fetch fixed in safari

* updated

* plugins

* plugins ready

* code improved

* fixed bug with backspace

* improved architecture

* Versioning (#102)

* start versioning

* codex.version

* eslint settings

* versioning improved

* cover restoring and fetch function fixed (#101)

* cover restoring fixed

* upd

* fetch fixed in safari

* updated

* plugins

* plugins ready

* code improved

* fixed bug with backspace

* improved architecture

* new sanitize method (#103)

* new sanitize method

Need to fix caret position

* removed console logs

* version updated

* eslint style

* caret position

* big fixed on sanitize method

* sanitize improved, using observers

* sanitize: using html-janitor

* fixes

* last fixes, code improved after review

* updated

* new bundle

* webpack config improved

* upd

* upd

* upd

* upd

* clear from conflicts

* upd

* upd
2017-01-10 21:22:40 +03:00

195 lines
4.4 KiB
JavaScript

/**
* Codex Editor toolbox
*
* All tools be able to appended here
*
* @author Codex Team
* @version 1.0
*/
var toolbox = (function(toolbox) {
toolbox.init = function () {
require('./toolbar');
};
toolbox.opened = false;
/** Shows toolbox */
toolbox.open = function() {
/** Close setting if toolbox is opened */
if (codex.toolbar.settings.opened) {
codex.toolbar.settings.close();
}
/** display toolbox */
codex.nodes.toolbox.classList.add('opened');
/** Animate plus button */
codex.nodes.plusButton.classList.add('clicked');
/** toolbox state */
codex.toolbar.toolbox.opened = true;
};
/** Closes toolbox */
toolbox.close = function() {
/** Makes toolbox disapear */
codex.nodes.toolbox.classList.remove('opened');
/** Rotate plus button */
codex.nodes.plusButton.classList.remove('clicked');
/** toolbox state */
codex.toolbar.toolbox.opened = false;
};
toolbox.leaf = function(){
var currentTool = codex.toolbar.current,
tools = Object.keys(codex.tools),
barButtons = codex.nodes.toolbarButtons,
nextToolIndex,
hiddenToolsAmount = 0,
toolToSelect;
/** Count toolbox hidden tools */
for( var tool in codex.tools ) {
if (!codex.tools[tool].displayInToolbox) {
hiddenToolsAmount ++;
}
}
if ( !currentTool ) {
/** Get first tool from object*/
for (toolToSelect in barButtons) break;
} else {
nextToolIndex = tools.indexOf(currentTool) + 1;
var toolIsLastInToolbox = nextToolIndex == tools.length - (hiddenToolsAmount - 2);
if ( toolIsLastInToolbox ) {
nextToolIndex = 0;
/** getting first displayed tool */
for( var tool in codex.tools ) {
if (codex.tools[tool].displayInToolbox){
break;
}
nextToolIndex ++;
}
}
toolToSelect = tools[nextToolIndex];
}
for (var button in barButtons) barButtons[button].classList.remove('selected');
barButtons[toolToSelect].classList.add('selected');
codex.toolbar.current = toolToSelect;
};
/**
* Transforming selected node type into selected toolbar element type
* @param {event} event
*/
toolbox.toolClicked = function() {
/**
* UNREPLACEBLE_TOOLS this types of tools are forbidden to replace even they are empty
*/
var UNREPLACEBLE_TOOLS = ['image', 'link', 'list', 'instagram', 'twitter'],
tool = codex.tools[codex.toolbar.current],
workingNode = codex.content.currentNode,
currentInputIndex = codex.caret.inputIndex,
newBlockContent,
appendCallback,
blockData;
/** Make block from plugin */
newBlockContent = tool.make();
/** information about block */
blockData = {
block : newBlockContent,
type : tool.type,
stretched : false
};
if (
workingNode &&
UNREPLACEBLE_TOOLS.indexOf(workingNode.dataset.tool) === -1 &&
workingNode.textContent.trim() === ''
){
/** Replace current block */
codex.content.switchBlock(workingNode, newBlockContent, tool.type);
} else {
/** Insert new Block from plugin */
codex.content.insertBlock(blockData);
/** increase input index */
currentInputIndex++;
}
/** Fire tool append callback */
appendCallback = tool.appendCallback;
if (appendCallback && typeof appendCallback == 'function') {
appendCallback.call(event);
}
setTimeout(function() {
/** Set caret to current block */
codex.caret.setToBlock(currentInputIndex);
}, 10);
/**
* Changing current Node
*/
codex.content.workingNodeChanged();
/**
* Move toolbar when node is changed
*/
codex.toolbar.move();
};
return toolbox;
})({});
toolbox.init();
module.exports = toolbox;