I'm not sure this is easily possible with one contenteditable element. I setup a basic example using multiple contenteditable elements, one for each page.
Example: https://fiddle.jshell.net/d68ew9qc/
The following snippet adds a new page if the maximum rows per page is reached and the key used was an "enter":
$(".pageEdit").on('keypress', function(e) {
var length = $(this).children().length + 1;
/* only add a new page if the next sibling is not available */
// enter
if (e.which == 13 && length >= this.getAttribute("max_rows")) {
if ($(this).parent('.page').next().length === 0) {
var newPage = $('.page').clone(true);
$('.pageEdit', newPage).html('');
newPage.appendTo('#editor');
$('.pageEdit', newPage).focus();
}
e.preventDefault();
return false;
}
});
The keypress duplicates an element with the class=".page" and emptys the contenteditable element on the newly created one.
The keyup handles the backspace deletion of empty pages.
$(".pageEdit").on('keyup', function(e) {
var length = $(this).children().length;
var total_pages = $(this).parent('.page').parent().children().length;
// backspace is not working with keypress, this portion needs some work!!!
if (e.which == 8 && $(this).parent('.page').prev().length > 0) {
if (length === 0 && total_pages > 1) {
// found via: http://stackoverflow.com/a/4238971/3298029
placeCaretAtEnd($('.pageEdit>div:last', $(this).parent('.page').prev())[0]);
$(this).closest('.page').remove();
}
}
});
The basic html for that looks like the following:
<style>
.editor {
background-color: transparent;
border: 1px solid #eaedf1;
}
.page {
width: 600px;
position: relative;
margin-top: 15px;
background-color: #fff;
}
.header {
height: 100px;
width: 100%;
margin: 0;
}
.footer {
height: 100px;
bottom: 0;
width: 100%;
margin: 0;
}
.pageEdit {
min-height: 1400px;
border: 1px solid #eaedf1;
overflow: hidden;
padding: 0;
}
/* found: http://stackoverflow.com/a/4407335/3298029 */
.noselect {
-webkit-touch-callout: none;
/* iOS Safari */
-webkit-user-select: none;
/* Chrome/Safari/Opera */
-khtml-user-select: none;
/* Konqueror */
-moz-user-select: none;
/* Firefox */
-ms-user-select: none;
/* Internet Explorer/Edge */
user-select: none;
/* Non-prefixed version, currently
not supported by any browser */
pointer-events: none;
}
</style>
<div style="padding:40px 40px 40px 40px; background-color:#eaedf1">
<div id="editor">
<div class="page">
<div class="header noselect">
<h2 style="text-align:center;">This is header</h2>
</div>
<div contenteditable="true" class="pageEdit" max_rows="77">
</div>
<div class="footer noselect">
<h2 style="text-align:center;">This is footer
</h2>
</div>
</div>
</div>
</div>
This is a fairly simple demo and many things won't work. If a line breaks to a new one, this should be handled as well.
This was only tested in Chrome which adds <div><br /></div>
for a new line. Firefox only adds <br /><br />
. So with that in mind the total number of rows should be calculated dynamically and not set to a fixed value because it defers from one browser to another.
Hope this gets you started.