Understanding the view

HBuilderX supports customized and special web pages: Views, which can communicate with nodejs.

Use the view to build a complex UI that supports local file operations.

It’s very important to design a perfect UI.

Extend a new WebView

Extend a new TreeView

WebView

Supported from HBuilderX 2.8.1+

Extend a new WebView view (view) with the following 2 steps:

  1. //package.json
  2. //...NOTEPackage.json does not support comments, you need to delete the comments when using the following code
  3. "contributes": {
  4. "viewsContainers": {
  5. "rightside": [{
  6. "id": "WebViewcontainerId",
  7. "title": "webview - display"
  8. }]
  9. },
  10. "views": {
  11. "WebViewcontainerId": [{
  12. "id": "extension.WebView",
  13. "title": "webview - display"
  14. }]
  15. },
  16. ...
  17. }
  1. const hx = require('hbuilderx');
  2. /**
  3. * @description display webview
  4. */
  5. function showWebView(webviewPanel) {
  6. let webview = webviewPanel.webView;
  7. var background = '';
  8. let config = hx.workspace.getConfiguration();
  9. let colorScheme = config.get('editor.colorScheme');
  10. if (colorScheme == 'Monokai') {
  11. background = 'rgb(39,40,34)'
  12. } else if (colorScheme == 'Atom One Dark') {
  13. background = 'rgb(40,44,53)'
  14. } else {
  15. background = 'rgb(255,250,232)'
  16. };
  17. webview.html =
  18. `
  19. <body style="background-color:${background};border:1px solid ${background};">
  20. <div style="max-width:200px;">
  21. <img src="https://download1.dcloud.net.cn/uploads/images/hbuilderx/hx_desc@1x.png" style="position: absolute;bottom: 0;left: 0;right: 0;width: 100%;margin: auto;">
  22. </div>
  23. <script>
  24. //The following two methods are equivalent
  25. hbuilderx.onDidReceiveMessage((msg) => {});
  26. window.addEventListener("message", (msg) => {});
  27. hbuiderx.postMessage({
  28. command: 'alert',
  29. text: 'HelloWorld'
  30. });
  31. </script>
  32. </body>
  33. `;
  34. //The extension sends messages (data that can be JSONized) to the webview
  35. webview.postMessage({
  36. command: "test"
  37. });
  38. //The extension receives messages sent by webview (data that can be JSONized)
  39. webview.onDidReceiveMessage((msg) => {
  40. if (msg.command == 'alert') {
  41. hx.window.showInformationMessage(msg.text);
  42. }
  43. });
  44. };
  45. module.exports = {
  46. showWebView
  47. }

The expanded View can be opened via the menu View-Show Plug-in View

Screenshot

Understanding the view - 图1

TreeView

Supported from 从HBuilderX 2.7.12+

Extend a new TreeView view (view) through the following 2 steps:

  1. //package.json
  2. //...NOTEPackage.json does not support comments, you need to delete the comments when using the following code
  3. "contributes": {
  4. "viewsContainers": {
  5. "activitybar": [{
  6. "id": "demoview",
  7. "title": "DemoView"
  8. }]
  9. },
  10. "views": {
  11. "demoview": [{
  12. //The id needs to be consistent with the viewId parameter in window.createTreeView
  13. "id": "extensions.treedemo",
  14. "name": "DemoView"
  15. }]
  16. }
  17. }
  1. // extension.js
  2. var hx = require("hbuilderx");
  3. class DemoTreeDataProvider extends hx.TreeDataProvider{
  4. constructor(demoData) {
  5. super();
  6. this._demoData = demoData;
  7. }
  8. getChildren(element) {
  9. let demoData = this._demoData;
  10. return new Promise(resolve => {
  11. if (!element) {
  12. resolve(demoData);
  13. } else {
  14. resolve(element.children);
  15. }
  16. });
  17. }
  18. getTreeItem(element) {
  19. return {
  20. label:element.name,
  21. collapsibleState:element.children ? 1 : 0,
  22. command:{
  23. command:element.children ? "":"extension.helloWorld",
  24. arguments:[
  25. element.name
  26. ]
  27. }
  28. }
  29. }
  30. }
  31. //This method will be called when the extension is activated
  32. function activate(context) {
  33. let demoData = [
  34. {
  35. name:"Root1",
  36. children:[
  37. {
  38. name:"child1"
  39. },
  40. {
  41. name:"child2"
  42. }
  43. ]
  44. },
  45. {
  46. name:"Root2",
  47. children:[
  48. {
  49. name:"child3",
  50. },
  51. {
  52. name:"child4"
  53. }
  54. ]
  55. }
  56. ]
  57. hx.commands.registerCommand("extension.helloWorld",function(param){
  58. hx.window.showInformationMessage("Select TreeItem:" + param[0]);
  59. });
  60. hx.window.createTreeView("extensions.treedemo", {
  61. showCollapseAll: true,
  62. treeDataProvider: new DemoTreeDataProvider(demoData)
  63. });
  64. }
  65. //This method will be called when the extension is disabled (currently it is triggered when the extension is uninstalled
  66. function deactivate() {
  67. }
  68. module.exports = {
  69. activate,
  70. deactivate
  71. }

The expanded View can be opened via the menu View-Show Plug-in View

Screenshot

Understanding the view - 图2