PublisherConnector
PublisherConnector is a class object that allows you to interact with the CHILI Editor editorObject via postMessage without the complexity of postMessage.
Why choose this over interacting with the editorObject directly?
- The first and most obvious reason is CORS - by using PublisherConnector you do not have to worry about CNAME/domain or reverse proxy tricks.
- The documentation of each function of PublisherConnector (really the editorObject) is the most complete documentation in existence.
- Works with CHILI publisher Online.
- Promise based and modern.
Installation
You can install PublisherConnector via npm
npm i @chili-publish/publisher-connector
or through yarn
yarn add @chili-publish/publisher-connector
Usage
- Import PublisherConnector.
import {PublisherConnector} from "@chili-publish/publisher-connector";
- Get the HTMLIFrameElement which contains the CHILI Editor.
const iframe = document.getElementById("editor-iframe");
- Create an instance of PublisherConnector by using the
build
method and passing the HTMLIFrameElement (created above) as a parameter. Thebuild
method returns a promise, which when resolved has the instance of the PublisherConnector.
PublisherConnector.build(iframe).then(
publisherConnector => publisherConnector.alert("Hi!")
);
🚨️
IMPORTANT
🛑
The PublisherConnector build
method must be called before the IFrame loads the Editor. This is due to how Penpal, the library the PublisherConnector is built on, functions.
If you set the src
attribute of the IFrame in your HTML and have a script tag that runs the build method, then everything will be fine. The issue is only if you were to wait until IFrame was loaded before calling the build
method.
For example this would be fine - assuming PublisherConnector was packaged (using Parcel, Webpack, etc.) into a browser safe JS file called publisher-connector-packaged.js
: