Commit 4d46a83d authored by Fred Chasen's avatar Fred Chasen

Support recto and verso

parent 5c4669f3
const TIMEOUT = 10000; // Some book might take longer than this to renderer
describe('break-after-recto', async () => {
let page;
let rendered;
beforeAll(async () => {
page = await loadPage('breaks/break-after/break-after-recto/break-after-recto.html')
return page.rendered;
}, TIMEOUT)
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
})
it('should render 39 pages', async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => {
return r.length;
});
expect(pages).toEqual(39);
})
it('should render page 2 as blank', async () => {
let isBlank = await page.$eval("[data-page-number='2']", (r) => {
return r.classList.contains("pagedjs_blank_page");
});
expect(isBlank).toEqual(true);
})
it('should render page 3 as recto', async () => {
let isRight = await page.$eval("[data-page-number='3']", (r) => {
return r.classList.contains("pagedjs_right_page");
});
expect(isRight).toEqual(true);
})
it('page 3 should be Section 1', async () => {
let text = await page.$eval("[data-page-number='3']", (r) => r.textContent);
expect(text).toContain('Section 1');
})
it('should render page 5 as recto', async () => {
let isRight = await page.$eval("[data-page-number='5']", (r) => {
return r.classList.contains("pagedjs_right_page");
});
expect(isRight).toEqual(true);
})
it('page 5 should be Section 2', async () => {
let text = await page.$eval("[data-page-number='5']", (r) => r.textContent);
expect(text).toContain('Section 2');
})
it('should render page 8 as blank', async () => {
let isBlank = await page.$eval("[data-page-number='8']", (r) => {
return r.classList.contains("pagedjs_blank_page");
});
expect(isBlank).toEqual(true);
})
it('should render page 9 as recto', async () => {
let isRight = await page.$eval("[data-page-number='9']", (r) => {
return r.classList.contains("pagedjs_right_page");
});
expect(isRight).toEqual(true);
})
it('page 9 should be Section 3', async () => {
let text = await page.$eval("[data-page-number='9']", (r) => r.textContent);
expect(text).toContain('Section 3');
})
it('page 10 should break after h2', async () => {
let text = await page.$eval("[data-page-number='10']", (r) => r.textContent);
expect(text.trim()).toEqual('A - h2 (inline element)');
})
it('should render page 11 as recto', async () => {
let isRight = await page.$eval("[data-page-number='11']", (r) => {
return r.classList.contains("pagedjs_right_page");
});
expect(isRight).toEqual(true);
})
if (!DEBUG) {
it('should create a pdf', async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(2);
expect(pdf).toMatchPDFSnapshot(3);
expect(pdf).toMatchPDFSnapshot(5);
expect(pdf).toMatchPDFSnapshot(7);
expect(pdf).toMatchPDFSnapshot(9);
expect(pdf).toMatchPDFSnapshot(10);
expect(pdf).toMatchPDFSnapshot(11);
})
}
}
)
const TIMEOUT = 10000; // Some book might take longer than this to renderer
describe('break-after-verso', async () => {
let page;
let rendered;
beforeAll(async () => {
page = await loadPage('breaks/break-after/break-after-verso/break-after-verso.html')
return page.rendered;
}, TIMEOUT)
afterAll(async () => {
if (!DEBUG) {
await page.close();
}
})
it('should render 38 pages', async () => {
let pages = await page.$$eval(".pagedjs_page", (r) => {
return r.length;
});
expect(pages).toEqual(38);
})
it('should render page 4 as verso', async () => {
let isLeft = await page.$eval("[data-page-number='4']", (r) => {
return r.classList.contains("pagedjs_left_page");
});
expect(isLeft).toEqual(true);
})
it('page 4 should be Section 2', async () => {
let text = await page.$eval("[data-page-number='4']", (r) => r.textContent);
expect(text).toContain('Section 2');
})
it('should render page 7 as blank', async () => {
let isBlank = await page.$eval("[data-page-number='7']", (r) => {
return r.classList.contains("pagedjs_blank_page");
});
expect(isBlank).toEqual(true);
})
it('should render page 8 as verso', async () => {
let isLeft = await page.$eval("[data-page-number='8']", (r) => {
return r.classList.contains("pagedjs_left_page");
});
expect(isLeft).toEqual(true);
})
it('page 8 should be Section 3', async () => {
let text = await page.$eval("[data-page-number='8']", (r) => r.textContent);
expect(text).toContain('Section 3');
})
it('page 9 should break after h2', async () => {
let text = await page.$eval("[data-page-number='9']", (r) => r.textContent);
expect(text.trim()).toEqual('A - h2 (inline element)');
})
it('should render page 10 as verso', async () => {
let isLeft = await page.$eval("[data-page-number='10']", (r) => {
return r.classList.contains("pagedjs_left_page");
});
expect(isLeft).toEqual(true);
})
if (!DEBUG) {
it('should create a pdf', async () => {
let pdf = await page.pdf(PDF_SETTINGS);
expect(pdf).toMatchPDFSnapshot(4);
expect(pdf).toMatchPDFSnapshot(7);
expect(pdf).toMatchPDFSnapshot(8);
expect(pdf).toMatchPDFSnapshot(9);
expect(pdf).toMatchPDFSnapshot(10);
})
}
}
)
......@@ -3,8 +3,8 @@ import ContentParser from "./parser";
import EventEmitter from "event-emitter";
import Hook from "../utils/hook";
import {
needsBreakBefore,
needsBreakAfter
needsBreakBefore,
needsBreakAfter
} from "../utils/dom";
const MAX_PAGES = false;
......@@ -126,6 +126,8 @@ class Chunker {
handleBreaks(node) {
let currentPage = this.total + 1;
let currentPosition = currentPage % 2 === 0 ? "left" : "right";
// TODO: Recto and Verso should reverse for rtl languages
let currentSide = currentPage % 2 === 0 ? "verso" : "recto";
let previousBreakAfter;
let breakBefore;
......@@ -145,12 +147,18 @@ class Chunker {
(previousBreakAfter === "left" || previousBreakAfter === "right") &&
previousBreakAfter !== currentPosition) {
this.addPage(true);
}
if( breakBefore &&
} else if( previousBreakAfter &&
(previousBreakAfter === "verso" || previousBreakAfter === "recto") &&
previousBreakAfter !== currentSide) {
this.addPage(true);
} else if( breakBefore &&
(breakBefore === "left" || breakBefore === "right") &&
breakBefore !== currentPosition) {
this.addPage(true);
} else if( breakBefore &&
(breakBefore === "verso" || breakBefore === "recto") &&
breakBefore !== currentSide) {
this.addPage(true);
}
}
......@@ -259,14 +267,14 @@ class Chunker {
return page;
}
get total() {
return this._total;
}
get total() {
return this._total;
}
set total(num) {
this.pagesArea.style.setProperty('--page-count', num);
this._total = num;
}
set total(num) {
this.pagesArea.style.setProperty('--page-count', num);
this._total = num;
}
destroy() {
this.pagesArea.remove()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment