Skip to content

Differences With editorObject

Sean Crowe edited this page Dec 9, 2022 · 4 revisions

Differences With editorObject

The PublisherInterface class and editorObject object share many similarities. This is not odd because the PublisherInterface is an interface for interacting with the editorObject via postMessage(). Below we will mark any key differences.

Note: When Publisher was on Flash, the editorObject was just called editor. The two terms can be used and discussed interchangeably.

Differences:

Method Names

A small, but important difference is that the methods from the PublisherInterface uses common JavaScript naming - camelCase. This differs from the PascalCase used by the editorObject.

✏️ The editorObject way:

editorObject.ExecuteFunction("document", "Save");

💻 The publisher way:

await publisher.executeFunction("document", "Save");

There is a convenience getter on the PublisherInterface class named editorObject which acts as an alias that can help current implementations switch to Publisher Interface.

See Moving to Publisher Interface for more details.

Promise as a Return

While the editorObject methods return by holding up the main thread, the PublisherInterface methods use postMessage() under the hood. This means that the message must be serialized, sent, and deserialized across from one window to another. It is a message system, and thus things happen asynchronously.

The only way to deal with asynchronous message communication is to use callbacks. This can be done natively in JavaScript via a Promise.

Unless noted otherwise, all PubliserInterface methods return a Promise.

✏️ The editorObject way:

function logDocName() {
  const documentId = editorObject.GetObject("document.id");
  console.log(documentId);
}

💻 The publisher way:

function logDocName() {
  publisher.getObject("document.id").then(
      documentId => console.log(documentId)
  );
}

or

async function logDocName() {
  const documentId = await publisher.getObject("document.id");
  console.log(documentId);
}

This means that all code written to use Publisher Interface, must be written with asynchronously in mind.

Side note, if something goes wrong, both editorObject and PublisherInterface behave the same by throwing an exception.

Events

To use events with the editorObject, it involved called the editorObject.AddListener() method and then defining a function on window.OnEditorEvent() that took a string for the event name and a string for the id of the target. The OnEditorEvent() function would typically have a switch case or a series of if/else to determine which event was called, and react accordingly.

The good news is that PublisherInterface behave the same.

✏️ The editorObject way:

editorObject.AddListener("FrameMovedFinished");

💻 The publisher way:

await publisher.addListener("FrameMovedFinished");

Both these implementations will call window.OnEditorEvent() method when the FrameMovedFinished event is fired off.

So you could use something like the below example in both cases to log when FrameMovedFinished event is fired off.

window.OnEditorEvent = (eventName, targetId) => {
  if (eventName == "FrameMovedFinished") {
    console.log("Frame with id " + targetId + " was moved");
  }
}

However, with the PublisherInterface there more concise way to handle events using a callback. If you supply a callback when calling addListener(), then the callback will be used instead of window.OnEditorEvent().

So our example can be rewritten as:

💻 The publisher way:

await publisher.addListener("FrameMovedFinished", (targetId) => {
  console.log("Frame with id " + targetId + " was moved");
});

Removing events is the same in both implementations.

✏️ The editorObject way:

editorObject.RemoveListener("FrameMovedFinished");

💻 The publisher way:

await publisher.removeListener("FrameMovedFinished");