-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolves #207 create a browser-compatible version of the Document con…
…verter - Reduce the scope of the classes - Use browserify to produce a browser-compatible file - Inline files content (ie. replace readFileSync) to bundle everything into a single JavaScript file - Add tests
- Loading branch information
1 parent
501e278
commit 801d8be
Showing
9 changed files
with
1,709 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,63 @@ | ||
/* global Paged, PagedPolyfill */ | ||
window.AsciidoctorPDF = window.AsciidoctorPDF || {} | ||
window.AsciidoctorPDF.status = 'rendering' | ||
class PagedReadyHandler extends Paged.Handler { | ||
afterRendered (pages) { | ||
window.AsciidoctorPDF.status = 'ready' | ||
} | ||
(function() { | ||
window.AsciidoctorPDF = window.AsciidoctorPDF || {} | ||
window.AsciidoctorPDF.status = 'rendering'; | ||
|
||
// BEGIN: workaround to prevent duplicated content at end/beginning of page | ||
// https://gitlab.pagedmedia.org/tools/pagedjs/issues/126 | ||
constructor (chunker, polisher, caller) { | ||
super(chunker, polisher, caller) | ||
this.carriageReturn = String.fromCharCode(10) | ||
} | ||
class PagedReadyHandler extends Paged.Handler { | ||
afterRendered (_) { | ||
window.AsciidoctorPDF.status = 'ready' | ||
} | ||
|
||
checkNode (node) { | ||
if (!node) return | ||
if (node.nodeType !== 3) return | ||
if (node.textContent === this.carriageReturn) { | ||
node.remove() | ||
// BEGIN: workaround to prevent duplicated content at end/beginning of page | ||
// https://gitlab.pagedmedia.org/tools/pagedjs/issues/126 | ||
constructor (chunker, polisher, caller) { | ||
super(chunker, polisher, caller) | ||
this.carriageReturn = String.fromCharCode(10) | ||
} | ||
} | ||
|
||
afterParsed (parsed) { | ||
const template = document.querySelector('template').content | ||
const breakAfterAvoidElements = template.querySelectorAll('[data-break-after="avoid"], [data-break-before="avoid"]') | ||
for (const el of breakAfterAvoidElements) { | ||
this.checkNode(el.previousSibling) | ||
this.checkNode(el.nextSibling) | ||
checkNode (node) { | ||
if (!node) return | ||
if (node.nodeType !== 3) return | ||
if (node.textContent === this.carriageReturn) { | ||
node.remove() | ||
} | ||
} | ||
} | ||
// END: workaround to prevent duplicated content at end/beginning of page | ||
} | ||
|
||
Paged.registerHandlers(PagedReadyHandler) | ||
afterParsed (_) { | ||
const template = document.querySelector('template').content | ||
const breakAfterAvoidElements = template.querySelectorAll('[data-break-after="avoid"], [data-break-before="avoid"]') | ||
for (const el of breakAfterAvoidElements) { | ||
this.checkNode(el.previousSibling) | ||
this.checkNode(el.nextSibling) | ||
} | ||
} | ||
// END: workaround to prevent duplicated content at end/beginning of page | ||
} | ||
|
||
window.PagedConfig = window.PagedConfig || {} | ||
Paged.registerHandlers(PagedReadyHandler) | ||
|
||
window.PagedConfig.auto = false | ||
window.PagedConfig = window.PagedConfig || {} | ||
window.PagedConfig.auto = false | ||
|
||
// same logic as pagedjs, but waiting for 'complete' instead of 'interactive' | ||
const ready = new Promise(function (resolve, reject) { | ||
if (document.readyState === 'complete') { | ||
resolve(document.readyState) | ||
return | ||
} | ||
|
||
document.onreadystatechange = function () { | ||
const ready = new Promise(function (resolve, reject) { | ||
if (document.readyState === 'complete') { | ||
resolve(document.readyState) | ||
return | ||
} | ||
} | ||
}) | ||
|
||
ready.then(async function () { | ||
const done = await PagedPolyfill.preview(window.PagedConfig.content, window.PagedConfig.stylesheets, window.PagedConfig.renderTo) | ||
if (window.PagedConfig.after) { | ||
await window.PagedConfig.after(done) | ||
} | ||
}) | ||
document.onreadystatechange = function () { | ||
if (document.readyState === 'complete') { | ||
resolve(document.readyState) | ||
} | ||
} | ||
}) | ||
|
||
ready.then(async function () { | ||
const done = await PagedPolyfill.preview(window.PagedConfig.content, window.PagedConfig.stylesheets, window.PagedConfig.renderTo) | ||
if (window.PagedConfig.after) { | ||
await window.PagedConfig.after(done) | ||
} | ||
}) | ||
})() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
/* global Paged */ | ||
class RepeatingTableHeaders extends Paged.Handler { | ||
constructor (chunker, polisher, caller) { // eslint-disable-line no-useless-constructor | ||
super(chunker, polisher, caller) | ||
} | ||
(function() { | ||
class RepeatingTableHeaders extends Paged.Handler { | ||
constructor (chunker, polisher, caller) { // eslint-disable-line no-useless-constructor | ||
super(chunker, polisher, caller) | ||
} | ||
|
||
afterPageLayout (pageElement, page, breakToken, chunker) { | ||
// Find all split table elements | ||
const tables = pageElement.querySelectorAll('table[data-split-from]') | ||
afterPageLayout (pageElement, page, breakToken, chunker) { | ||
// Find all split table elements | ||
const tables = pageElement.querySelectorAll('table[data-split-from]') | ||
|
||
tables.forEach((table) => { | ||
// Get the reference UUID of the node | ||
const ref = table.dataset.ref | ||
// Find the node in the original source | ||
const sourceTable = chunker.source.querySelector("[data-ref='" + ref + "']") | ||
// Find if there is a header | ||
const header = sourceTable.querySelector('thead') | ||
if (header) { | ||
// Clone the header element | ||
const clonedHeader = header.cloneNode(true) | ||
// Insert the header at the start of the split table | ||
table.insertBefore(clonedHeader, table.firstChild) | ||
} | ||
}) | ||
tables.forEach((table) => { | ||
// Get the reference UUID of the node | ||
const ref = table.dataset.ref | ||
// Find the node in the original source | ||
const sourceTable = chunker.source.querySelector("[data-ref='" + ref + "']") | ||
// Find if there is a header | ||
const header = sourceTable.querySelector('thead') | ||
if (header) { | ||
// Clone the header element | ||
const clonedHeader = header.cloneNode(true) | ||
// Insert the header at the start of the split table | ||
table.insertBefore(clonedHeader, table.firstChild) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
Paged.registerHandlers(RepeatingTableHeaders) | ||
Paged.registerHandlers(RepeatingTableHeaders) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.