Commit c15c0990 authored by Thomas Parisot's avatar Thomas Parisot

feat(previewer): add beforePreview and afterPreview hooks

parent 458f4728
Pipeline #269 failed with stage
in 24 seconds
...@@ -117,6 +117,10 @@ New handlers can be registered from `import { registerHandlers } from 'pagedjs'` ...@@ -117,6 +117,10 @@ New handlers can be registered from `import { registerHandlers } from 'pagedjs'`
Handlers have methods that correspond to the hooks for the parsing, layout and rendering of the Chunker and Polisher. Returning an promise or `async` function from a method in a handler will complete that task before continuing with the other registered methods for that hook. Handlers have methods that correspond to the hooks for the parsing, layout and rendering of the Chunker and Polisher. Returning an promise or `async` function from a method in a handler will complete that task before continuing with the other registered methods for that hook.
```js ```js
// Previewer
beforePreview(content, renderTo)
afterPreview(pages)
// Chunker // Chunker
beforeParsed(content) beforeParsed(content)
afterParsed(parsed) afterParsed(parsed)
......
import EventEmitter from "event-emitter"; import EventEmitter from "event-emitter";
import Hook from "../utils/hook";
import Chunker from "../chunker/chunker"; import Chunker from "../chunker/chunker";
import Polisher from "../polisher/polisher"; import Polisher from "../polisher/polisher";
...@@ -17,6 +18,8 @@ class Previewer { ...@@ -17,6 +18,8 @@ class Previewer {
// Hooks // Hooks
this.hooks = {}; this.hooks = {};
this.hooks.beforeAll = new Hook(this);
this.hooks.afterAll = new Hook(this);
// Handlers // Handlers
this.handlers = this.initializeHandlers(); this.handlers = this.initializeHandlers();
...@@ -119,6 +122,8 @@ class Previewer { ...@@ -119,6 +122,8 @@ class Previewer {
async preview(content, stylesheets, renderTo) { async preview(content, stylesheets, renderTo) {
await this.hooks.beforePreview.trigger(content, renderTo);
if (!content) { if (!content) {
content = this.wrapContent(); content = this.wrapContent();
} }
...@@ -143,6 +148,8 @@ class Previewer { ...@@ -143,6 +148,8 @@ class Previewer {
this.emit("rendered", flow); this.emit("rendered", flow);
await this.hooks.afterPreview.trigger(flow.pages);
return flow; return flow;
} }
} }
......
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