Created
February 13, 2019 08:25
-
-
Save dongsik-yoo/6ef14888831371167582355dffdb1cd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Insert paragraphs to body at first | |
* @param {HTMLElement} pageBodyElement - page body element | |
* @param {Array.<HTMLElement>} paragraphs - paragraph array | |
*/ | |
_insertParagraphsToBodyAtFirst(pageBodyElement, paragraphs) { | |
if (pageBodyElement.firstChild) { | |
// merge split paragraphs before. | |
paragraphs.slice().reverse().forEach(paragraph => { | |
const splitParagraphId = paragraph.getAttribute(SPLIT_PARAGRAPH_ID); | |
let appended = false; | |
if (splitParagraphId) { | |
const nextParagraph = pageBodyElement.querySelector(`[${SPLIT_PARAGRAPH_ID}="${splitParagraphId}"]`); | |
if (nextParagraph) { | |
const {firstChild} = nextParagraph; | |
paragraph.childNodes.forEach( | |
node => nextParagraph.insertBefore(node, firstChild) | |
); | |
paragraph.parentElement.removeChild(paragraph); | |
appended = true; | |
} | |
} | |
if (!appended) { | |
pageBodyElement.insertBefore(paragraph, pageBodyElement.firstChild); | |
} | |
}); | |
} else { | |
paragraphs.forEach( | |
paragraph => pageBodyElement.appendChild(paragraph) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment