mirror of
https://github.com/codex-team/editor.js
synced 2024-06-28 02:10:31 +02:00
Merge pull request #140 from codex-team/paragraph-parser-algorythm
Paragraph parser algorythm
This commit is contained in:
commit
44b31c16b7
|
@ -88,7 +88,7 @@ var codex =
|
||||||
codex.sanitizer = __webpack_require__(18);
|
codex.sanitizer = __webpack_require__(18);
|
||||||
};
|
};
|
||||||
|
|
||||||
codex.version = ("1.3.10");
|
codex.version = ("1.3.11");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
|
@ -1173,7 +1173,7 @@ var codex =
|
||||||
* Works with DOM
|
* Works with DOM
|
||||||
*
|
*
|
||||||
* @author Codex Team
|
* @author Codex Team
|
||||||
* @version 1.3.8
|
* @version 1.3.11
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var content = function (content) {
|
var content = function (content) {
|
||||||
|
@ -1808,29 +1808,82 @@ var codex =
|
||||||
*/
|
*/
|
||||||
var sibling = node.nextSibling;
|
var sibling = node.nextSibling;
|
||||||
|
|
||||||
// console.log('Погнали проверять соседей ');
|
|
||||||
|
|
||||||
while (sibling) {
|
while (sibling) {
|
||||||
|
|
||||||
// console.log('Опаньки! нашли соседа: %o', sibling);
|
|
||||||
|
|
||||||
if (sibling.textContent.length) {
|
if (sibling.textContent.length) {
|
||||||
|
|
||||||
// console.log('Соседи не пустые, то есть мы не в конце.');
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// console.log('Сосед пустой. Возможно мы в конце.');
|
|
||||||
// console.log('Смотрим следующего');
|
|
||||||
|
|
||||||
sibling = sibling.nextSibling;
|
sibling = sibling.nextSibling;
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log('Все соседи пустые. -------');
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @public
|
||||||
|
*
|
||||||
|
* @param [String] htmlString - html content as string
|
||||||
|
* @return {string} - html content as string
|
||||||
|
*/
|
||||||
|
content.wrapTextWithParagraphs = function (htmlString) {
|
||||||
|
|
||||||
|
var wrapper = document.createElement('DIV'),
|
||||||
|
newWrapper = document.createElement('DIV'),
|
||||||
|
i,
|
||||||
|
paragraph,
|
||||||
|
firstLevelBlocks = ['DIV', 'P'],
|
||||||
|
blockTyped,
|
||||||
|
node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make HTML Element to Wrap Text
|
||||||
|
* It allows us to work with input data as HTML content
|
||||||
|
*/
|
||||||
|
wrapper.innerHTML = htmlString;
|
||||||
|
paragraph = document.createElement('P');
|
||||||
|
|
||||||
|
for (i = 0; i < wrapper.childNodes.length; i++) {
|
||||||
|
|
||||||
|
node = wrapper.childNodes[i];
|
||||||
|
|
||||||
|
blockTyped = firstLevelBlocks.indexOf(node.tagName) != -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If node is first-levet
|
||||||
|
* we add this node to our new wrapper
|
||||||
|
*/
|
||||||
|
if (blockTyped) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If we had splitted inline nodes to paragraph before
|
||||||
|
*/
|
||||||
|
if (paragraph.childNodes.length) {
|
||||||
|
|
||||||
|
newWrapper.appendChild(paragraph.cloneNode(true));
|
||||||
|
|
||||||
|
/** empty paragraph */
|
||||||
|
paragraph = null;
|
||||||
|
paragraph = document.createElement('P');
|
||||||
|
}
|
||||||
|
|
||||||
|
newWrapper.appendChild(node.cloneNode(true));
|
||||||
|
} else {
|
||||||
|
|
||||||
|
/** Collect all inline nodes to one as paragraph */
|
||||||
|
paragraph.appendChild(node.cloneNode(true));
|
||||||
|
|
||||||
|
/** if node is last we should append this node to paragraph and paragraph to new wrapper */
|
||||||
|
if (i == wrapper.childNodes.length - 1) {
|
||||||
|
newWrapper.appendChild(paragraph.cloneNode(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newWrapper.innerHTML;
|
||||||
|
};
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
}({});
|
}({});
|
||||||
|
|
||||||
|
@ -4404,7 +4457,6 @@ var codex =
|
||||||
target: '_blank',
|
target: '_blank',
|
||||||
rel: 'nofollow'
|
rel: 'nofollow'
|
||||||
},
|
},
|
||||||
ul: {},
|
|
||||||
i: {},
|
i: {},
|
||||||
b: {},
|
b: {},
|
||||||
strong: {},
|
strong: {},
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -3,7 +3,7 @@
|
||||||
* Works with DOM
|
* Works with DOM
|
||||||
*
|
*
|
||||||
* @author Codex Team
|
* @author Codex Team
|
||||||
* @version 1.3.8
|
* @version 1.3.11
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var content = (function(content) {
|
var content = (function(content) {
|
||||||
|
@ -672,32 +672,88 @@ var content = (function(content) {
|
||||||
*/
|
*/
|
||||||
var sibling = node.nextSibling;
|
var sibling = node.nextSibling;
|
||||||
|
|
||||||
// console.log('Погнали проверять соседей ');
|
|
||||||
|
|
||||||
while ( sibling ) {
|
while ( sibling ) {
|
||||||
|
|
||||||
// console.log('Опаньки! нашли соседа: %o', sibling);
|
|
||||||
|
|
||||||
if (sibling.textContent.length){
|
if (sibling.textContent.length){
|
||||||
|
|
||||||
// console.log('Соседи не пустые, то есть мы не в конце.');
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// console.log('Сосед пустой. Возможно мы в конце.');
|
|
||||||
// console.log('Смотрим следующего');
|
|
||||||
|
|
||||||
sibling = sibling.nextSibling;
|
sibling = sibling.nextSibling;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log('Все соседи пустые. -------');
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @public
|
||||||
|
*
|
||||||
|
* @param [String] htmlString - html content as string
|
||||||
|
* @return {string} - html content as string
|
||||||
|
*/
|
||||||
|
content.wrapTextWithParagraphs = function(htmlString) {
|
||||||
|
|
||||||
|
var wrapper = document.createElement('DIV'),
|
||||||
|
newWrapper = document.createElement('DIV'),
|
||||||
|
i,
|
||||||
|
paragraph,
|
||||||
|
firstLevelBlocks = ['DIV', 'P'],
|
||||||
|
blockTyped,
|
||||||
|
node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make HTML Element to Wrap Text
|
||||||
|
* It allows us to work with input data as HTML content
|
||||||
|
*/
|
||||||
|
wrapper.innerHTML = htmlString;
|
||||||
|
paragraph = document.createElement('P');
|
||||||
|
|
||||||
|
for (i = 0; i < wrapper.childNodes.length; i++) {
|
||||||
|
|
||||||
|
node = wrapper.childNodes[i];
|
||||||
|
|
||||||
|
blockTyped = firstLevelBlocks.indexOf(node.tagName) != -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If node is first-levet
|
||||||
|
* we add this node to our new wrapper
|
||||||
|
*/
|
||||||
|
if ( blockTyped ) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If we had splitted inline nodes to paragraph before
|
||||||
|
*/
|
||||||
|
if ( paragraph.childNodes.length ){
|
||||||
|
|
||||||
|
newWrapper.appendChild(paragraph.cloneNode(true));
|
||||||
|
|
||||||
|
/** empty paragraph */
|
||||||
|
paragraph = null;
|
||||||
|
paragraph = document.createElement('P');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
newWrapper.appendChild(node.cloneNode(true));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
/** Collect all inline nodes to one as paragraph */
|
||||||
|
paragraph.appendChild(node.cloneNode(true));
|
||||||
|
|
||||||
|
/** if node is last we should append this node to paragraph and paragraph to new wrapper */
|
||||||
|
if ( i == wrapper.childNodes.length - 1 ){
|
||||||
|
newWrapper.appendChild(paragraph.cloneNode(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newWrapper.innerHTML;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
|
|
||||||
})({});
|
})({});
|
||||||
|
|
|
@ -20,7 +20,6 @@ var sanitizer = (function(sanitizer) {
|
||||||
target: '_blank',
|
target: '_blank',
|
||||||
rel: 'nofollow'
|
rel: 'nofollow'
|
||||||
},
|
},
|
||||||
ul: {},
|
|
||||||
i: {},
|
i: {},
|
||||||
b: {},
|
b: {},
|
||||||
strong: {},
|
strong: {},
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "codex.editor",
|
"name": "codex.editor",
|
||||||
"version": "1.3.10",
|
"version": "1.3.11",
|
||||||
"description": "Codex Editor. Native JS, based on API and Open Source",
|
"description": "Codex Editor. Native JS, based on API and Open Source",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -440,31 +440,12 @@ var quote = (function(quote) {
|
||||||
|
|
||||||
var prepareDataForSave_ = function(data) {
|
var prepareDataForSave_ = function(data) {
|
||||||
|
|
||||||
var TEXTNODE = 3;
|
|
||||||
|
|
||||||
if (data.size == 'withPhoto') {
|
if (data.size == 'withPhoto') {
|
||||||
data.size = 'small';
|
data.size = 'small';
|
||||||
}
|
}
|
||||||
|
|
||||||
var wrapper = document.createElement('DIV');
|
/** Make paragraphs */
|
||||||
wrapper.innerHTML = data.text;
|
data.text = codex.content.wrapTextWithParagraphs(data.text);
|
||||||
|
|
||||||
var child,
|
|
||||||
paragraph;
|
|
||||||
|
|
||||||
for (child = 0; child < wrapper.childNodes.length; child++) {
|
|
||||||
|
|
||||||
// is TEXT node ?
|
|
||||||
if (wrapper.childNodes[child].nodeType === TEXTNODE) {
|
|
||||||
|
|
||||||
paragraph = document.createElement('P');
|
|
||||||
paragraph.innerHTML = wrapper.childNodes[child].textContent;
|
|
||||||
|
|
||||||
wrapper.childNodes[child].replaceWith(paragraph);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
data.text = wrapper.innerHTML;
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
@ -497,7 +478,7 @@ var quote = (function(quote) {
|
||||||
*/
|
*/
|
||||||
var parsedblock = methods_.parseBlockQuote(blockContent);
|
var parsedblock = methods_.parseBlockQuote(blockContent);
|
||||||
|
|
||||||
var data = {
|
var outputData = {
|
||||||
"text" : parsedblock.text,
|
"text" : parsedblock.text,
|
||||||
"format" : "html",
|
"format" : "html",
|
||||||
"cite" : parsedblock.author,
|
"cite" : parsedblock.author,
|
||||||
|
@ -506,7 +487,7 @@ var quote = (function(quote) {
|
||||||
"image" : parsedblock.photo
|
"image" : parsedblock.photo
|
||||||
};
|
};
|
||||||
|
|
||||||
return prepareDataForSave_(data);
|
return prepareDataForSave_(outputData);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 329 B |
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue