Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare plugins #148

Merged
merged 10 commits into from
Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
new module tools.js for methods working with plugins
  • Loading branch information
neSpecc committed Feb 5, 2017
commit d6ed75f530f321c0b2d8d62bf46da12408860f12
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"no-trailing-spaces": 2,
"no-mixed-spaces-and-tabs": 2,
"padded-blocks": [2, "always"],
"space-before-blocks": 2,
"space-before-function-paren": [2, {
"space-before-blocks": 1,
"space-before-function-paren": [1, {
"anonymous": "always",
"named": "never"
}],
Expand All @@ -63,6 +63,7 @@
"module": true,
"require": true,
"window": true,
"console" : true,
"codex": true,
"VERSION" : true,
"Promise" : true,
Expand Down
10 changes: 7 additions & 3 deletions codex-editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,14 @@
padding: 0;
}

.unavailableBlock {
background-color: #f2f3ec;
.cdx-unavailable-block {
display: block;
height: 50px;
margin: 10px 0;
padding: 80px;
background-color: #fff7f7;
text-align: center;
border-radius: 3px;
color: #ce5f5f;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions codex-editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion codex-editor.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion codex.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = (function (editor) {
var init = function () {

editor.core = require('./modules/core');
editor.tools = require('./modules/tools');
editor.ui = require('./modules/ui');
editor.transport = require('./modules/transport');
editor.renderer = require('./modules/renderer');
Expand Down Expand Up @@ -128,7 +129,7 @@ module.exports = (function (editor) {
.then(editor.ui.make)
.then(editor.ui.addTools)
.then(editor.ui.bindEvents)
.then(editor.ui.preparePlugins)
.then(editor.tools.prepare)
.then(editor.transport.prepare)
.then(editor.renderer.makeBlocksFromData)
.then(editor.ui.saveInputs)
Expand Down
52 changes: 29 additions & 23 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,35 @@
}
},
data : {
items: [
{
type : 'paragraph',
data : {
text : 'Привет'
}
},
{
type : 'tweet',
data : {
"media" : true,
"conversation" : false,
"user" : null,
"id" : 12312312312,
"text" : null,
"created_at" : null,
"status_url" : 'ertertert',
"caption" : null
},
cover : false
}
],
count: 2
// items: [
// {
// type : 'paragraph',
// data : {
// text : 'Привет от CodeX'
// }
// },
// {
// type : 'tweet',
// data : {
// "media" : true,
// "conversation" : false,
// "user" : null,
// "id" : 12312312312,
// "text" : null,
// "created_at" : null,
// "status_url" : 'ertertert',
// "caption" : null
// },
// cover : false
// },
// {
// type : 'paragraph',
// data : {
// text : 'Пишите нам на team@ifmo.su'
// }
// },
// ],
// count: 2
}
});
</script>
Expand Down
11 changes: 11 additions & 0 deletions modules/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ module.exports = (function (core) {

};

/**
* Checks passed object for emptiness
* @require ES5 - Object.keys
* @param {object}
*/
core.isEmpty = function ( obj ) {

return Object.keys(obj).length === 0;

};

/**
* Native Ajax
*/
Expand Down
9 changes: 6 additions & 3 deletions modules/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,14 @@ module.exports = (function (draw) {

};

draw.unavailableBlock = function ( data ) {
/**
* Unavailable plugin block
*/
draw.unavailableBlock = function () {

var wrapper = document.createElement('DIV');
wrapper.classList.add('unavailableBlock');
wrapper.dataset.cacheId = data;

wrapper.classList.add('cdx-unavailable-block');

return wrapper;

Expand Down
43 changes: 27 additions & 16 deletions modules/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = (function (renderer) {
/**
* If redactor is empty, add first paragraph to start writing
*/
if (!editor.state.blocks.items.length) {
if (editor.core.isEmpty(editor.state.blocks) || !editor.state.blocks.items.length) {

editor.ui.addInitialBlock();
return;
Expand Down Expand Up @@ -121,7 +121,10 @@ module.exports = (function (renderer) {

return Promise.resolve().then(function () {

return blocksList[index];
return {
tool : blocksList[index],
position : index
};

});

Expand All @@ -132,21 +135,22 @@ module.exports = (function (renderer) {
*
* @uses render method of each plugin
*
* @param {object} blockData looks like
* { header : {
* text: '',
* type: 'H3', ...
* }
* }
* @return {object} with type and Element
* @param {Object} toolData.tool
* { header : {
* text: '',
* type: 'H3', ...
* }
* }
* @param {Number} toolData.position - index in input-blocks array
* @return {Object} with type and Element
*/
renderer.createBlockFromData = function (blockData) {
renderer.createBlockFromData = function ( toolData ) {

/** New parser */
var block,
ArrayOfCachedData = codex.editor.state.ArrayOfCachedData,
pluginName = blockData.type,
cover = blockData.cover;
tool = toolData.tool,
pluginName = tool.type,
cover = tool.cover;

/** Get first key of object that stores plugin name */
// for (var pluginName in blockData) break;
Expand All @@ -167,15 +171,22 @@ module.exports = (function (renderer) {

if ( editor.tools[pluginName].available === false ) {

block = editor.draw.unavailableBlock(ArrayOfCachedData.length);
block = editor.draw.unavailableBlock();

block.innerHTML = editor.tools[pluginName].loadingMessage;

/**
* Saver will extract data from initial block data by position in array
*/
block.dataset.inputPosition = toolData.position;

/** Save to restore data of unavailable plugins */
ArrayOfCachedData.push(blockData.data);
// ArrayOfCachedData.push(tool.data);

} else {

/** New Parser */
block = editor.tools[pluginName].render(blockData.data);
block = editor.tools[pluginName].render(tool.data);

}

Expand Down
15 changes: 9 additions & 6 deletions modules/saver.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,22 @@ module.exports = (function (saver) {
var blockContent = block.childNodes[0],
pluginsContent = blockContent.childNodes[0],
savedData,
ArrayOfCachedData = codex.editor.state.ArrayOfCachedData,
cacheId,
output;
position,
output,
coverFlag = false;

/** If plugin wasn't available then return data from cache */
if ( editor.tools[pluginName].available === false ) {

cacheId = pluginsContent.dataset.cacheId;
savedData = ArrayOfCachedData[cacheId];
position = pluginsContent.dataset.inputPosition;

savedData = codex.editor.state.blocks.items[position].data;
coverFlag = codex.editor.state.blocks.items[position].cover;

} else {

savedData = editor.tools[pluginName].save(pluginsContent);
coverFlag = block.classList.contains(editor.ui.className.BLOCK_IN_FEED_MODE);

if (editor.tools[pluginName].validate) {

Expand All @@ -140,7 +143,7 @@ module.exports = (function (saver) {
};

/** Marks Blocks that will be in main page */
output.cover = block.classList.contains(editor.ui.className.BLOCK_IN_FEED_MODE);
output.cover = coverFlag;

editor.state.jsonOutput.push(output);

Expand Down
Loading