Commit 6e84678b authored by Fred Chasen's avatar Fred Chasen

Merge branch 'overflow_hooks' into 'master'

Overflow hooks

See merge request !52
parents 430a082f 31384bec
Pipeline #217 passed with stage
in 3 minutes and 8 seconds
......@@ -127,6 +127,12 @@ onAtPage(atPageNode)
onRule(ruleNode)
onDeclaration(declarationNode, ruleNode)
onContent(contentNode, declarationNode, ruleNode)
// Layout
layoutNode(node)
renderNode(node, sourceNode)
onOverflow(overflow, rendered, bounds)
onBreakToken(breakToken, overflow, rendered)
```
## Setup
......
......@@ -10,7 +10,24 @@
<link rel="coverpage" href="images/cover.jpg" />
<script src="../../../dist/paged.polyfill.js"></script>
<script>
class TestHandler extends Paged.Handler {
constructor(chunker, polisher, caller) {
super(chunker, polisher, caller);
}
onOverflow(overflow, rendered, bounds) {
console.log("overflow", overflow);
// return false;
}
onBreakToken(breakToken, overflow, rendered) {
console.log("breakToken", breakToken);
// return false;
}
}
Paged.registerHandlers(TestHandler);
</script>
<style>
@page {
size: 'letter';
......
......@@ -93,7 +93,8 @@ class Chunker {
this.hooks.layout = new Hook(this);
this.hooks.renderNode = new Hook(this);
this.hooks.layoutNode = new Hook(this);
this.hooks.overflow = new Hook(this);
this.hooks.onOverflow = new Hook(this);
this.hooks.onBreakToken = new Hook();
this.hooks.afterPageLayout = new Hook(this);
this.hooks.afterRendered = new Hook(this);
......
......@@ -47,7 +47,9 @@ class Layout {
this.hooks.layout = new Hook();
this.hooks.renderNode = new Hook();
this.hooks.layoutNode = new Hook();
this.hooks.overflow = new Hook();
this.hooks.beforeOverflow = new Hook();
this.hooks.onOverflow = new Hook();
this.hooks.onBreakToken = new Hook();
}
this.maxChars = maxChars || MAX_CHARS_PER_BREAK;
......@@ -209,7 +211,12 @@ class Layout {
dest.appendChild(clone);
}
this.hooks && this.hooks.renderNode.trigger(clone);
let nodeHooks = this.hooks.renderNode.triggerSync(clone, node);
nodeHooks.forEach((newNode) => {
if (typeof newNode != "undefined") {
clone = newNode;
}
});
return clone;
}
......@@ -327,9 +334,24 @@ class Layout {
let overflow = this.findOverflow(rendered, bounds);
let breakToken;
let overflowHooks = this.hooks.onOverflow.triggerSync(overflow, rendered, bounds, this);
overflowHooks.forEach((newOverflow) => {
if (typeof newOverflow != "undefined") {
overflow = newOverflow;
}
});
if (overflow) {
breakToken = this.createBreakToken(overflow, rendered, source);
let breakHooks = this.hooks.onBreakToken.triggerSync(breakToken, overflow, rendered, this);
breakHooks.forEach((newToken) => {
if (typeof newToken != "undefined") {
breakToken = newToken;
}
});
if (breakToken && breakToken.node && extract) {
this.removeOverflow(overflow);
}
......
......@@ -33,7 +33,7 @@ class Hook {
/**
* Triggers a hook to run all functions
* @example this.content.trigger(args).then(function(){...});
* @return {undefined} void
* @return {Promise} results
*/
trigger(){
var args = arguments;
......@@ -47,13 +47,36 @@ class Hook {
// Task is a function that returns a promise
promises.push(executing);
}
// Otherwise Task resolves immediately, continue
// Otherwise Task resolves immediately, add resolved promise with result
promises.push(new Promise((resolve, reject) => {
resolve(executing);
}));
});
return Promise.all(promises);
}
/**
* Triggers a hook to run all functions synchronously
* @example this.content.trigger(args).then(function(){...});
* @return {Array} results
*/
triggerSync(){
var args = arguments;
var context = this.context;
var results = [];
this.hooks.forEach(function(task) {
var executing = task.apply(context, args);
results.push(executing);
});
return results;
}
// Adds a function to be run before a hook completes
list(){
return this.hooks;
......
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