Creating a Custom Panel

How a compose panel is defnined can be found by reading the package.json documentation.

Identify the main entry file in the panel definition and fill in its content. Example:

  1. 'use strict';
  2. // html text
  3. exports.template ='';
  4. // style text
  5. exports.style ='';
  6. // html selector after rendering
  7. exports.$ = {};
  8. // method on the panel
  9. exports.methods = {};
  10. // event triggered on the panel
  11. exports.listeners = {};
  12. // Triggered when the panel is rendered successfully
  13. exports.ready = async function() {};
  14. // Triggered when trying to close the panel
  15. exports.beforeClose = async function() {};
  16. // Triggered when the panel is actually closed
  17. exports.close = async function() {};

Template

An HTML string. Example:

  1. exports.template = `
  2. <header>
  3. Header
  4. </header>
  5. <section class="test">
  6. Section
  7. </section>
  8. `;

It is possible to read an HTML file directly. Example:

  1. const {readFileSync} = require('fs');
  2. const {join} = require('path');
  3. exports.template = readFileSync(join(__dirname,'../static/default.html'),'utf8');

The template is defined, when the panel is opened, the content of the template will be automatically rendered on the interface.

In addition, the editor also provides some custom elements, please refer to the UI documentation.

Style

With HTML, it is possible to customize some styles. Style is a string style template. Example:

  1. exports.style = `
  2. header {padding: 10px;}
  3. `;

It is also posible to read a CSS file directly. Example:

  1. const {readFileSync} = require('fs');
  2. const {join} = require('path');
  3. exports.style = readFileSync(join(__dirname,'../static/default.css'),'utf8');

$

This is an HTML element selector, directly call querySelector to find the specified element and use it as a shortcut. Example:

  1. exports.$ = {
  2. header:'header',
  3. test:'.test',
  4. };

First, define the selector. After the template is rendered, the editor will automatically call document.querySelector to find the corresponding element and hang it on this.$. Example:

  1. exports.ready = function() {
  2. console.log(this.$.header); // <header>
  3. console.log(this.$.test); // <section class="test">
  4. }

Methods

The method defined on the panel. The external functions of the panel need to be encapsulated into methods and provided externally in units of functions. Messages can also directly trigger the methods on the panel. For details, please refer to the Message Communication documentation.

This object is full of functions, do not attach other types of objects here. Example:

  1. const packageJSON = require('./package.json');
  2. exports.methods = {
  3. open() {
  4. Editor.Panel.open(packageJSON.name);
  5. },
  6. };

Listeners

After the basic layout is completed, it is sometimes necessary to update the status on some panels according to some situations. It is necessary to use the listeners function. Example:

  1. exports.listeners = {
  2. /**
  3. * Triggered when the panel is hidden
  4. */
  5. hide() {
  6. console.log(this.hidden);
  7. },
  8. /**
  9. * Triggered when the panel is displayed
  10. */
  11. show() {
  12. console.log(this.hidden);
  13. },
  14. /**
  15. * Triggered when the panel size changes
  16. */
  17. resize() {
  18. console.log(this.clientHeight);
  19. console.log(this.clientWidth);
  20. },
  21. };

beforeClose

When the panel tries to be closed, this function will be triggered. BeforeClose can be an async function that can be used for asynchronous judgment. If it returns false, the current closing operation will be terminated.

Do not execute the actual destruction and close-related logic code in this function. This step is just for inquiry. Please put the actual destruction in the close function.

Note: please use with caution, if the judgment is wrong, the editor or panel window may not close normally.

Close

When all the panels in the window are allowed to be closed, the panel close will be triggered. Once the close is triggered, the window will be forcibly closed after the end, please save the data in the close. If an abnormal close occurs, please make a backup of the data in order to restore data as much as possible when restarting.