Commit 77aaeb27 authored by Fred Chasen's avatar Fred Chasen
Browse files

spaces -> tabs

parent f7ba5416
This diff is collapsed.
......@@ -24,6 +24,7 @@
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^23.6.0",
"chalk": "^2.4.1",
"eslint": "^5.9.0",
"express": "^4.16.3",
"ghostscript4js": "^3.0.0",
"jest": "^23.4.1",
......@@ -48,7 +49,8 @@
"compile": "./node_modules/.bin/babel src/ -d lib/",
"pretest": "npm run build",
"prepublishOnly": "npm run build && npm run compile && npm run legacy",
"watch": "./node_modules/.bin/rollup -w -c"
"watch": "./node_modules/.bin/rollup -w -c",
"lint": "./node_modules/.bin/eslint -c .eslintrc.js src; exit 0"
},
"repository": {
"type": "git",
......
This diff is collapsed.
......@@ -6,263 +6,263 @@ import EventEmitter from "event-emitter";
* @class
*/
class Page {
constructor(pagesArea, pageTemplate, blank, hooks) {
this.pagesArea = pagesArea;
this.pageTemplate = pageTemplate;
this.blank = blank;
constructor(pagesArea, pageTemplate, blank, hooks) {
this.pagesArea = pagesArea;
this.pageTemplate = pageTemplate;
this.blank = blank;
this.width = undefined;
this.height = undefined;
this.width = undefined;
this.height = undefined;
this.hooks = hooks;
this.hooks = hooks;
// this.element = this.create(this.pageTemplate);
}
// this.element = this.create(this.pageTemplate);
}
create(template, after) {
//let documentFragment = document.createRange().createContextualFragment( TEMPLATE );
//let page = documentFragment.children[0];
let clone = document.importNode(this.pageTemplate.content, true);
create(template, after) {
//let documentFragment = document.createRange().createContextualFragment( TEMPLATE );
//let page = documentFragment.children[0];
let clone = document.importNode(this.pageTemplate.content, true);
let page;
if (after) {
this.pagesArea.insertBefore(clone, after.nextSibling);
let index = Array.prototype.indexOf.call(this.pagesArea.children, after.nextSibling);
page = this.pagesArea.children[index];
} else {
this.pagesArea.appendChild(clone);
page = this.pagesArea.lastChild;
}
let page;
if (after) {
this.pagesArea.insertBefore(clone, after.nextSibling);
let index = Array.prototype.indexOf.call(this.pagesArea.children, after.nextSibling);
page = this.pagesArea.children[index];
} else {
this.pagesArea.appendChild(clone);
page = this.pagesArea.lastChild;
}
let area = page.querySelector(".pagedjs_page_content");
let area = page.querySelector(".pagedjs_page_content");
let size = area.getBoundingClientRect();
let size = area.getBoundingClientRect();
area.style.columnWidth = Math.round(size.width) + "px";
area.style.columnGap = "calc(var(--margin-right) + var(--margin-left))";
// area.style.overflow = "scroll";
area.style.columnWidth = Math.round(size.width) + "px";
area.style.columnGap = "calc(var(--margin-right) + var(--margin-left))";
// area.style.overflow = "scroll";
this.width = Math.round(size.width);
this.height = Math.round(size.height);
this.width = Math.round(size.width);
this.height = Math.round(size.height);
this.element = page;
this.area = area;
this.element = page;
this.area = area;
return page;
}
return page;
}
createWrapper() {
let wrapper = document.createElement("div");
createWrapper() {
let wrapper = document.createElement("div");
this.area.appendChild(wrapper);
this.area.appendChild(wrapper);
this.wrapper = wrapper;
this.wrapper = wrapper;
return wrapper;
}
return wrapper;
}
index(pgnum) {
this.position = pgnum;
index(pgnum) {
this.position = pgnum;
let page = this.element;
let page = this.element;
let id = `page-${pgnum+1}`;
let id = `page-${pgnum+1}`;
this.id = id;
this.id = id;
page.dataset.pageNumber = pgnum+1;
page.dataset.pageNumber = pgnum+1;
if (this.name) {
page.classList.add("pagedjs_" + this.name + "_page");
}
if (this.name) {
page.classList.add("pagedjs_" + this.name + "_page");
}
if (this.blank) {
page.classList.add("pagedjs_blank_page");
}
if (this.blank) {
page.classList.add("pagedjs_blank_page");
}
if (pgnum === 0) {
page.classList.add("pagedjs_first_page");
}
if (pgnum === 0) {
page.classList.add("pagedjs_first_page");
}
if (pgnum % 2 !== 1) {
page.classList.remove("pagedjs_left_page");
page.classList.add("pagedjs_right_page");
} else {
page.classList.remove("pagedjs_right_page");
page.classList.add("pagedjs_left_page");
}
}
if (pgnum % 2 !== 1) {
page.classList.remove("pagedjs_left_page");
page.classList.add("pagedjs_right_page");
} else {
page.classList.remove("pagedjs_right_page");
page.classList.add("pagedjs_left_page");
}
}
/*
size(width, height) {
if (width === this.width && height === this.height) {
return;
}
this.width = width;
this.height = height;
/*
size(width, height) {
if (width === this.width && height === this.height) {
return;
}
this.width = width;
this.height = height;
this.element.style.width = Math.round(width) + "px";
this.element.style.height = Math.round(height) + "px";
this.element.style.columnWidth = Math.round(width) + "px";
}
*/
this.element.style.width = Math.round(width) + "px";
this.element.style.height = Math.round(height) + "px";
this.element.style.columnWidth = Math.round(width) + "px";
}
*/
async layout(contents, breakToken) {
async layout(contents, breakToken) {
this.clear();
this.clear();
this.startToken = breakToken;
this.startToken = breakToken;
this.layoutMethod = new Layout(this.area, this.hooks);
this.layoutMethod = new Layout(this.area, this.hooks);
let newBreakToken = await this.layoutMethod.renderTo(this.wrapper, contents, breakToken);
let newBreakToken = await this.layoutMethod.renderTo(this.wrapper, contents, breakToken);
this.addListeners(contents);
this.addListeners(contents);
this.endToken = newBreakToken;
this.endToken = newBreakToken;
return newBreakToken;
}
return newBreakToken;
}
async append(contents, breakToken) {
async append(contents, breakToken) {
if (!this.layoutMethod) {
return this.layout(contents, breakToken);
}
if (!this.layoutMethod) {
return this.layout(contents, breakToken);
}
let newBreakToken = await this.layoutMethod.renderTo(this.wrapper, contents, breakToken);
let newBreakToken = await this.layoutMethod.renderTo(this.wrapper, contents, breakToken);
this.endToken = newBreakToken;
this.endToken = newBreakToken;
return newBreakToken;
}
return newBreakToken;
}
getByParent(ref, entries) {
let e;
for (var i = 0; i < entries.length; i++) {
e = entries[i];
if(e.dataset.ref === ref) {
return e;
break;
}
}
}
getByParent(ref, entries) {
let e;
for (var i = 0; i < entries.length; i++) {
e = entries[i];
if(e.dataset.ref === ref) {
return e;
break;
}
}
}
onOverflow(func) {
this._onOverflow = func;
}
onOverflow(func) {
this._onOverflow = func;
}
onUnderflow(func) {
this._onUnderflow = func;
}
onUnderflow(func) {
this._onUnderflow = func;
}
clear() {
this.removeListeners();
this.wrapper && this.wrapper.remove();
this.createWrapper();
}
clear() {
this.removeListeners();
this.wrapper && this.wrapper.remove();
this.createWrapper();
}
addListeners(contents) {
if (typeof ResizeObserver !== "undefined") {
this.addResizeObserver(contents);
} else {
this._checkOverflowAfterResize = this.checkOverflowAfterResize.bind(this, contents);
this.element.addEventListener("overflow", this._checkOverflowAfterResize, false);
this.element.addEventListener("underflow", this._checkOverflowAfterResize, false);
}
// TODO: fall back to mutation observer?
addListeners(contents) {
if (typeof ResizeObserver !== "undefined") {
this.addResizeObserver(contents);
} else {
this._checkOverflowAfterResize = this.checkOverflowAfterResize.bind(this, contents);
this.element.addEventListener("overflow", this._checkOverflowAfterResize, false);
this.element.addEventListener("underflow", this._checkOverflowAfterResize, false);
}
// TODO: fall back to mutation observer?
this._onScroll = function() {
if(this.listening) {
this.element.scrollLeft = 0;
}
}.bind(this);
this._onScroll = function() {
if(this.listening) {
this.element.scrollLeft = 0;
}
}.bind(this);
// Keep scroll left from changing
this.element.addEventListener("scroll", this._onScroll);
// Keep scroll left from changing
this.element.addEventListener("scroll", this._onScroll);
this.listening = true;
this.listening = true;
return true;
}
return true;
}
removeListeners() {
this.listening = false;
removeListeners() {
this.listening = false;
if (typeof ResizeObserver !== "undefined" && this.ro) {
this.ro.disconnect();
} else if (this.element) {
this.element.removeEventListener("overflow", this._checkOverflowAfterResize, false);
this.element.removeEventListener("underflow", this._checkOverflowAfterResize, false);
}
if (typeof ResizeObserver !== "undefined" && this.ro) {
this.ro.disconnect();
} else if (this.element) {
this.element.removeEventListener("overflow", this._checkOverflowAfterResize, false);
this.element.removeEventListener("underflow", this._checkOverflowAfterResize, false);
}
this.element &&this.element.removeEventListener("scroll", this._onScroll);
this.element &&this.element.removeEventListener("scroll", this._onScroll);
}
}
addResizeObserver(contents) {
let wrapper = this.wrapper;
let prevHeight = wrapper.getBoundingClientRect().height;
this.ro = new ResizeObserver( entries => {
addResizeObserver(contents) {
let wrapper = this.wrapper;
let prevHeight = wrapper.getBoundingClientRect().height;
this.ro = new ResizeObserver( entries => {
if (!this.listening) {
return;
}
if (!this.listening) {
return;
}
for (let entry of entries) {
const cr = entry.contentRect;
for (let entry of entries) {
const cr = entry.contentRect;
if (cr.height > prevHeight) {
this.checkOverflowAfterResize(contents);
prevHeight = wrapper.getBoundingClientRect().height;
} else if (cr.height < prevHeight ) { // TODO: calc line height && (prevHeight - cr.height) >= 22
this.checkUnderflowAfterResize(contents);
prevHeight = cr.height;
}
}
});
this.ro.observe(wrapper);
}
checkOverflowAfterResize(contents) {
if (!this.listening || !this.layoutMethod) {
return;
}
let newBreakToken = this.layoutMethod.findBreakToken(this.wrapper, contents);
if (newBreakToken) {
this.endToken = newBreakToken;
this._onOverflow && this._onOverflow(newBreakToken);
}
}
checkUnderflowAfterResize(contents) {
if (!this.listening || !this.layoutMethod) {
return;
}
let endToken = this.layoutMethod.findEndToken(this.wrapper, contents);
// let newBreakToken = this.layoutMethod.findBreakToken(this.wrapper, contents);
if (cr.height > prevHeight) {
this.checkOverflowAfterResize(contents);
prevHeight = wrapper.getBoundingClientRect().height;
} else if (cr.height < prevHeight ) { // TODO: calc line height && (prevHeight - cr.height) >= 22
this.checkUnderflowAfterResize(contents);
prevHeight = cr.height;
}
}
});
this.ro.observe(wrapper);
}
checkOverflowAfterResize(contents) {
if (!this.listening || !this.layoutMethod) {
return;
}
let newBreakToken = this.layoutMethod.findBreakToken(this.wrapper, contents);
if (newBreakToken) {
this.endToken = newBreakToken;
this._onOverflow && this._onOverflow(newBreakToken);
}
}
checkUnderflowAfterResize(contents) {
if (!this.listening || !this.layoutMethod) {
return;
}
let endToken = this.layoutMethod.findEndToken(this.wrapper, contents);
// let newBreakToken = this.layoutMethod.findBreakToken(this.wrapper, contents);
if (endToken) {
this._onUnderflow && this._onUnderflow(endToken);
}
}
if (endToken) {
this._onUnderflow && this._onUnderflow(endToken);
}
}
destroy() {
this.removeListeners();
this.element.remove();
this.element = undefined;
this.wrapper = undefined;
}
destroy() {
this.removeListeners();
this.element.remove();
this.element = undefined;
this.wrapper = undefined;
}
}
EventEmitter(Page.prototype);
......
......@@ -6,22 +6,22 @@ import htmlparser2 from 'htmlparser2';
*/
class Parser {
constructor(content, cb) {
// this.text = text;
// this.nodes = [];
constructor(content, cb) {
// this.text = text;
// this.nodes = [];
this.refs = {};
this.sections = [];
if (content && content.nodeType) {
// handle dom
} else if(typeof content === "object") {
this.add(content);
} else if (typeof content === "string") {
this.dom = this.parse(content);
}
}
parse(data) {
this.sections = [];
if (content && content.nodeType) {
// handle dom
} else if(typeof content === "object") {
this.add(content);
} else if (typeof content === "string") {
this.dom = this.parse(content);
}
}
parse(data) {
let options = {};
let process = (node) => {
let uuid = this.uuid();
......@@ -36,30 +36,30 @@ class Parser {
node.dataset.text = node.data.length;
}
if (node.name === "section") {
this.sections.push(node);
}
if (node.name === "section") {
this.sections.push(node);
}
}
let handler = new htmlparser2.DomHandler(undefined, options, process.bind(this));
new htmlparser2.Parser(handler, options).end(data);
return handler.dom;
}
}
add(contents) {
this.dom = [contents];
add(contents) {
this.dom = [contents];
let walker = this.walk(this.dom[0]);
let next, done, node;
while (!done) {
next = walker.next();
node = next.value;
done = next.done;
let walker = this.walk(this.dom[0]);
let next, done, node;
while (!done) {
next = walker.next();
node = next.value;
done = next.done;
if (node && node.dataset && node.dataset.ref) {
this.refs[node.dataset.ref] = node;
}
}
}
if (node && node.dataset && node.dataset.ref) {
this.refs[node.dataset.ref] = node;
}
}
}
find(ref) {
return this.refs[ref];
......@@ -87,60 +87,60 @@ class Parser {
}
}
// isWrapper(element) {
// return wrappersRegex.test(element.nodeName);
// }
isText(node) {
return node.name === "tag";
}
isElement(node) {
return node.name === "text";
}
hasChildren(node) {
return node.children && node.children.length;
}
after(node) {
let after = node;
if (after.next) {
after = after.next;
} else {
while (after) {
after = after.parent;
if (after && after.next) {
after = after.next;
break;
}
}
}