Extending the Assets Panel

Extending the Right-Click Menu

The right-click menu display mechanism is to get the live menu data before it is displayed. The extension pre-registers the assets module in its own package.json to get the right-click menu display events for the Assets panel, and returns the menu data to be displayed via the events, and then displays it uniformly. The registered menu is usually displayed after the existing menu.

Assuming a location (where) in the Assets panel has a right-click menu event, the currently supported extended locations (where) are:

  • createMenu — two entry points for creating assets: one is the + button in the top left corner of the panel; the other is the Create option in the right-click menu.
  • dbMenu — the asset database root node
  • assetMenu — asset general node
  • panelMenu — blank area of the panel

Specific implementation steps:

  • Select Extension —> Create Extension in the menu bar at the top of the editor and create a new extension in the Global/Projects directory as needed. The extension package will then be generated in the extensions directory of the root/project directory.
  • Open the package.json file of the extension package and configure the contributions.assets.menu property, where methods is introduced into the assets-menu.js file. Other properties such as createMenu are explicit declarations of where above, and the corresponding onCreateMenu is the method exported from assets-menu.js:

    1. // package.json
    2. {
    3. contributions: {
    4. assets: {
    5. menu: {
    6. methods: '. /assets-menu.js', // This file is in the following demo example
    7. createMenu: 'onCreateMenu',
    8. assetMenu: 'onAssetMenu',
    9. dbMenu: 'onDBMenu',
    10. panelMenu: 'onPanelMenu',
    11. },
    12. },
    13. },
    14. }
  • The assets-menu.js part of the code example in the Demo example at the end of the article is as follows:

    1. // assets-menu.js
    2. exports.onCreateMenu = function (assetInfo) {
    3. return [
    4. {
    5. label: 'i18n:extend-assets-demo.menu.createAsset',
    6. click() {
    7. if (!assetInfo) {
    8. console.log('get create command from header menu');
    9. } else {
    10. console.log('get create command, the detail of diretory asset is:');
    11. console.log(assetInfo);
    12. }
    13. },
    14. },
    15. ];
    16. };
    17. exports.onAssetMenu = function (assetInfo) {
    18. return [
    19. {
    20. label: 'i18n:extend-assets-demo.menu.assetCommandParent',
    21. submenu: [
    22. {
    23. label: 'i18n:extend-assets-demo.menu.assetCommand1',
    24. enabled: assetInfo.isDirectory,
    25. click() {
    26. console.log('get it');
    27. console.log(assetInfo);
    28. },
    29. },
    30. {
    31. label: 'i18n:extend-assets-demo.menu.assetCommand2',
    32. enabled: !assetInfo.isDirectory,
    33. click() {
    34. console.log('yes, you clicked');
    35. console.log(assetInfo);
    36. },
    37. },
    38. ],
    39. },
    40. ];
    41. };
  • The onCreateMenu(assetInfo) function in assets-menu.js and similar functions are described as follows:

    • Parameter assetInfo Object

      • displayName String - the name of the asset to be displayed
      • extends (optional) String[] - the class to inherit from
      • importer String - the name of the importer
      • isDirectory Boolean - whether it is a folder
      • instantiation (optional) String - will carry this extension name if the virtual asset can be instantiated as an entity
      • imported Boolean - whether the import is complete
      • invalid Boolean - whether the import is failed
      • name String - the name of the asset
      • file String - the absolute path to the disk where the asset file is located
      • redirect Object - jump to the asset
        • type String - the asset type
        • uuid String - the asset ID
      • readonly Boolean - whether to be read-only
      • type String - asset type
      • url String - the address of the asset starting with db://
      • uuid String - asset ID
    • Return Value MenuItem[], MenuItem Object

      • type (optional) String - optional, normal, separator, submenu, checkbox or radio
      • label (optional) String - the text displayed
      • sublabel (optional) String - the secondary text displayed
      • submenu (optional) MenuItem[] - the submenu
      • click (optional) Function - the click event
      • enable (optional) Boolean - whether it is available, if not it will be grayed out style
      • visible (optional) Boolean - whether to show
      • accelerator (optional) String - Show shortcut keys
      • checked (optional) Boolean - whether checked when type is checkbox / radio

More properties can be found in the data format of the electron menu-item documentation.

The effect of implementing the extension is illustrated as follows:

extend-create-menu

Extending Drag-and-Drop Recognition

Recognizing an acceptable type requires the support of the editor UI component <ui-drag-item>, where an important property is type, <ui-drag-item type="xxx">. Customize a drag-in type and inject it into the Assets panel’s scope. If a <ui-drag-item> element containing that custom type is subsequently dragged into the Assets panel from another editor panel, the Assets panel recognizes it and sends a message to the registrant (plugin) of the custom type. The registrant can then perform a custom action, such as creating a new set of assets.

The specific implementation steps are roughly the same as the above extension right-click menu, open the extension’s package.json file to do the corresponding configuration.

  1. // package.json
  2. {
  3. contributions: {
  4. assets: {
  5. drop: [
  6. {
  7. type: 'my-defined-asset-type-for-drop', // corresponds to the usage of panel.html in the demo example
  8. message: 'drop-asset',
  9. },
  10. menu: ',]
  11. menu: '. /assets-menu.js',
  12. },
  13. },
  14. messages: {
  15. 'drop-asset': {
  16. methods: ['default.dropAsset'], // 'default' is the current extension's default panel
  17. },
  18. },
  19. }
  • The panel.js file in the Demo example at the end of the article:

    1. exports.methods = {
    2. dropAsset(assetInfo, dragInfo) {
    3. console.log(Editor.I18n.t('extend-assets-demo.drop.callback'));
    4. console.log(assetInfo);
    5. console.log(dragInfo);
    6. },
    7. };

    assetInfo parameter description:

    • uuid String - The uuid of the asset at the mouse release location when dragging an asset in the Assets panel.
    • type String - the asset type
    • isDirectory Boolean - whether the asset is a folder
  • The panel.html file in the Demo example at the end of the article:

    1. <ui-drag-item
    2. type="my-defined-asset-type-for-drop"
    3. additional='{"value": "this additional is dragInfo data."}'
    4. >
    5. <ui-label>Drag me to assets panel, and look conosole log.</ui-label>
    6. </ui-drag-item>

Demo examples

For executable code for both extensions, please download the workingexamples