Skip to content

Instantly share code, notes, and snippets.

@chop0
Created November 14, 2018 05:00
Show Gist options
  • Save chop0/0476ce6e1db75bd438a7e2cc1abcb4f6 to your computer and use it in GitHub Desktop.
Save chop0/0476ce6e1db75bd438a7e2cc1abcb4f6 to your computer and use it in GitHub Desktop.
Page edit divison
var linesPerPage = 3;
function addPage(){
//dom
var pageContainer = document.createElement("div");
pageContainer.classList.add("pageContainer");
var header = document.createElement("div");
header.classList.add("header");
header.innerHTML = "header";
var page = document.createElement("textarea");
page.classList.add("page");
page.setAttribute("cols","60");
page.setAttribute("rows",linesPerPage);
var footer = document.createElement("div");
footer.classList.add("footer");
footer.innerHTML = "footer";
pageContainer.appendChild(header);
pageContainer.appendChild(page);
pageContainer.appendChild(footer);
document.body.appendChild(pageContainer);
//events
page.addEventListener("input", onPageInput)
return {
page: page,
pageContainer: pageContainer
}
}
function onPageInput(event){
var page = event.target;
var lines = page.value.split(/\r*\n/);
if (lines.length > linesPerPage){
var newPage = addPage();
page.parentNode.parentNode.insertBefore(newPage.pageContainer, page.parentNode.nextSibling);
//reset original textArea value
page.value = "";
//populate old textarea and new textarea
page.value = lines.slice(0, linesPerPage).join("\n");
newPage.page.value = lines.slice(linesPerPage, lines.length).join("\n");
newPage.page.focus();
}
}
addPage();
body{
background: gray;
}
.pageContainer{
width: 30rem;
display: block;
margin: 1rem;
}
.page{
width: 100%;
background: white;
}
.header, .footer{
text-align: center;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment