<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: celsowm</title>
    <description>The latest articles on DEV Community by celsowm (@celsowm).</description>
    <link>https://dev.to/celsowm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F266239%2Fb4bbdab4-04a5-4b03-b415-f725a15453fa.png</url>
      <title>DEV Community: celsowm</title>
      <link>https://dev.to/celsowm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/celsowm"/>
    <language>en</language>
    <item>
      <title>How to move nodes from a contentEditable div to another dynamically when its content exceed a x height?</title>
      <dc:creator>celsowm</dc:creator>
      <pubDate>Wed, 06 Nov 2019 14:27:16 +0000</pubDate>
      <link>https://dev.to/celsowm/how-to-move-nodes-from-a-contenteditable-div-to-another-dynamically-when-its-content-exceed-a-x-height-1geg</link>
      <guid>https://dev.to/celsowm/how-to-move-nodes-from-a-contenteditable-div-to-another-dynamically-when-its-content-exceed-a-x-height-1geg</guid>
      <description>&lt;p&gt;I am trying to prototype a simple wyswyg that emulate the concept of A4 pages using contentEditable divs.&lt;br&gt;
So my current code is this:&lt;/p&gt;

&lt;p&gt;HTML:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="editor"&amp;gt;
            &amp;lt;div contenteditable="true" class="page" id="page-1"&amp;gt;
                &amp;lt;b&amp;gt;hello&amp;lt;/b&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;CSS:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#editor{
                background-color: gray;
                border: 1px black;
                padding: 1em 2em;
            }
            .page{
                background-color: white;
                border: solid black;
                padding: 1em 2em; 
                width:595px;
                height:841px;
                word-wrap: break-word;
                overflow-wrap: break-word;
                white-space: normal;
            }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;JS:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//force br
document.execCommand("DefaultParagraphSeparator", false, "br");
const a4 = {
    height: 841,
    width: 595
};
document.getElementById('editor').addEventListener('input', function(e) {
    let getChildrenHeight = function(element) {
        total = 0;
        if (element.childNodes) {
            for (let child of element.childNodes) {
                switch (child.nodeType) {
                    case Node.ELEMENT_NODE:
                        total += child.offsetHeight;
                        break;
                    case Node.TEXT_NODE:
                        let range = document.createRange();
                        range.selectNodeContents(child);
                        rect = range.getBoundingClientRect();
                        total += (rect.bottom - rect.top);
                        break;
                }
            }
        }
        return total;
    };
    let pages = document.getElementsByClassName('page');
    for (let i in pages) {
        let page = pages[i];
        //remove empty page    
        if (page.offsetHeight == 0 &amp;amp;&amp;amp; i &amp;gt; 1) {
            page.remove();
        }
        let childrenHeight = getChildrenHeight(page);
        while (childrenHeight &amp;gt; a4.height) {
            //recursively try to fit elements on max size 
            //removing/pushing excedents elements to the next div (aka page)
            let excedents = [];
            let children = page.childNodes;
            let children_length = children.length - 1;
            let backup = children[children_length].cloneNode(true);
            children[children_length].remove();
            if (pages.item(i + 1) === null) {
                var newPage = page.cloneNode(true);
                newPage.innerHTML = '';
                newPage.appendChild(backup);
                page.parentNode.appendChild(newPage);
            } else {
                page.item(i + 1).insertBefore(backup, page.item(i + 1).childNodes[0]);
            }
            //console.log(children[i].innerHTML);
        }
    }
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Unfortunately, the result is not as I was expecting.&lt;br&gt;
When the height of one page is exceeded, all content from the first page is removed, not like I would like: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the excess to be moved to next page.&lt;/li&gt;
&lt;li&gt;and when a page is abscent of children, been removed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Something like a very very primitive Microsoft Word multipages editor.&lt;/p&gt;

&lt;p&gt;How to do that? &lt;br&gt;
Thanks in advance&lt;br&gt;
Celso&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
    </item>
  </channel>
</rss>
