Communicate with the Panel

In general, the interaction model is dominated by extension process and panel for data presentation. Similarly to the traditional Web, the plug-in function is the server side, and the panel function is the browser on the client’s computer.

In this case, there is usually no direct data sent to the panel, the majority is some state synchronization, just using broadcast to broadcast.

But for simple extensions, or extensions to the browser environment, the actual functionality may be on the panel, and a request needs to be sent to the panel.

Some level of understanding of the Message System is required before reading this section.

Define methods on extensions and panels

First we define the file: package.json

  1. {
  2. "name": "hello-world",
  3. "panels": {
  4. "default": {
  5. "title": "hw",
  6. "main": "./panel.js"
  7. }
  8. },
  9. "contributions": {
  10. "messages": {
  11. "console": {
  12. "methods": ["default.console"]
  13. }
  14. }
  15. }
  16. }

The method name defined by methods in messages.console is default.console. Represents a console method issued to the default panel. (to send to the plug-in process, fill in methdName directly)

Then define the panel.js file of the panel:

  1. exports.template = '';
  2. exports.style = '';
  3. exports.methods = {
  4. console(str) {
  5. console.log(str);
  6. },
  7. };
  8. exports.ready = async function() {};
  9. exports.close = function() {};

Send a message

Once we have defined the extension and the panels within the extension, we can try to trigger these messages.

Press CTRL (CMD) + Shift + I to open the console. Open the panel in the console:

  1. Editor.Panel.open('hello-world');
  2. Editor.Message.send('hello-world', 'console', 'log');

When the Hello World plug-in receives a message, it passes it to the methods.console in panel.js for processing.

The result is printing a string to the log on the console.