mirror of
https://github.com/codex-team/editor.js
synced 2024-06-22 23:46:37 +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.version = ("1.3.10");
|
||||
codex.version = ("1.3.11");
|
||||
|
||||
/**
|
||||
* @public
|
||||
|
@ -1173,7 +1173,7 @@ var codex =
|
|||
* Works with DOM
|
||||
*
|
||||
* @author Codex Team
|
||||
* @version 1.3.8
|
||||
* @version 1.3.11
|
||||
*/
|
||||
|
||||
var content = function (content) {
|
||||
|
@ -1808,29 +1808,82 @@ var codex =
|
|||
*/
|
||||
var sibling = node.nextSibling;
|
||||
|
||||
// console.log('Погнали проверять соседей ');
|
||||
|
||||
while (sibling) {
|
||||
|
||||
// console.log('Опаньки! нашли соседа: %o', sibling);
|
||||
|
||||
if (sibling.textContent.length) {
|
||||
|
||||
// console.log('Соседи не пустые, то есть мы не в конце.');
|
||||
return false;
|
||||
}
|
||||
//
|
||||
// console.log('Сосед пустой. Возможно мы в конце.');
|
||||
// console.log('Смотрим следующего');
|
||||
|
||||
sibling = sibling.nextSibling;
|
||||
}
|
||||
|
||||
// console.log('Все соседи пустые. -------');
|
||||
|
||||
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;
|
||||
}({});
|
||||
|
||||
|
@ -4404,7 +4457,6 @@ var codex =
|
|||
target: '_blank',
|
||||
rel: 'nofollow'
|
||||
},
|
||||
ul: {},
|
||||
i: {},
|
||||
b: {},
|
||||
strong: {},
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -3,7 +3,7 @@
|
|||
* Works with DOM
|
||||
*
|
||||
* @author Codex Team
|
||||
* @version 1.3.8
|
||||
* @version 1.3.11
|
||||
*/
|
||||
|
||||
var content = (function(content) {
|
||||
|
@ -672,32 +672,88 @@ var content = (function(content) {
|
|||
*/
|
||||
var sibling = node.nextSibling;
|
||||
|
||||
// console.log('Погнали проверять соседей ');
|
||||
|
||||
while ( sibling ) {
|
||||
|
||||
// console.log('Опаньки! нашли соседа: %o', sibling);
|
||||
|
||||
if (sibling.textContent.length){
|
||||
|
||||
// console.log('Соседи не пустые, то есть мы не в конце.');
|
||||
return false;
|
||||
|
||||
}
|
||||
//
|
||||
// console.log('Сосед пустой. Возможно мы в конце.');
|
||||
// console.log('Смотрим следующего');
|
||||
|
||||
sibling = sibling.nextSibling;
|
||||
|
||||
}
|
||||
|
||||
// console.log('Все соседи пустые. -------');
|
||||
|
||||
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;
|
||||
|
||||
})({});
|
||||
|
|
|
@ -20,7 +20,6 @@ var sanitizer = (function(sanitizer) {
|
|||
target: '_blank',
|
||||
rel: 'nofollow'
|
||||
},
|
||||
ul: {},
|
||||
i: {},
|
||||
b: {},
|
||||
strong: {},
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "codex.editor",
|
||||
"version": "1.3.10",
|
||||
"version": "1.3.11",
|
||||
"description": "Codex Editor. Native JS, based on API and Open Source",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
|
|
@ -440,31 +440,12 @@ var quote = (function(quote) {
|
|||
|
||||
var prepareDataForSave_ = function(data) {
|
||||
|
||||
var TEXTNODE = 3;
|
||||
|
||||
if (data.size == 'withPhoto') {
|
||||
data.size = 'small';
|
||||
}
|
||||
|
||||
var wrapper = document.createElement('DIV');
|
||||
wrapper.innerHTML = 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;
|
||||
/** Make paragraphs */
|
||||
data.text = codex.content.wrapTextWithParagraphs(data.text);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
@ -497,7 +478,7 @@ var quote = (function(quote) {
|
|||
*/
|
||||
var parsedblock = methods_.parseBlockQuote(blockContent);
|
||||
|
||||
var data = {
|
||||
var outputData = {
|
||||
"text" : parsedblock.text,
|
||||
"format" : "html",
|
||||
"cite" : parsedblock.author,
|
||||
|
@ -506,7 +487,7 @@ var quote = (function(quote) {
|
|||
"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