如何扩展一个新的视图?

扩展一个新的 TreeView

扩展一个新的 WebView

TreeView

从HBuilderX 2.7.12及以上版本开始支持

扩展一个新的 TreeView 视图(view)通过以下2个步骤:

  1. //package.json
  2. //...NOTEpackage.json不支持注释,以下代码使用时需要将注释删掉
  3. "contributes": {
  4. "viewsContainers": {
  5. "activitybar": [{
  6. "id": "demoview",
  7. "title": "DemoView"
  8. }]
  9. },
  10. "views": {
  11. "demoview": [{
  12. //该id需要和window.createTreeView中的viewId参数一致
  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. //该方法将在插件激活的时候调用
  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("选中了TreeItem:" + param[0]);
  59. });
  60. hx.window.createTreeView("extensions.treedemo", {
  61. showCollapseAll: true,
  62. treeDataProvider: new DemoTreeDataProvider(demoData)
  63. });
  64. }
  65. //该方法将在插件禁用的时候调用(目前是在插件卸载的时候触发)
  66. function deactivate() {
  67. }
  68. module.exports = {
  69. activate,
  70. deactivate
  71. }

扩展后的视图可通过菜单视图-显示扩展视图打开

效果图

如何注册一个视图 - 图1

WebView

从HBuilderX 2.8.1及以上版本开始支持

扩展一个新的 WebView 视图(view)通过以下2个步骤:

  1. //package.json
  2. //...NOTEpackage.json不支持注释,以下代码使用时需要将注释删掉
  3. "contributes": {
  4. "viewsContainers": {
  5. "rightside": [{
  6. "id": "WebViewcontainerId",
  7. "title": "webview 展示"
  8. }]
  9. },
  10. "views": {
  11. "WebViewcontainerId": [{
  12. "id": "extension.WebView",
  13. "title": "webview - 展示"
  14. }]
  15. },
  16. ...
  17. }
  1. const hx = require('hbuilderx');
  2. /**
  3. * @description 显示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. // 以下两种写法等同
  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. webview.postMessage({
  35. command: "test"
  36. });
  37. webview.onDidReceiveMessage((msg) => {
  38. if (msg.command == 'alert') {
  39. hx.window.showInformationMessage(msg.text);
  40. }
  41. });
  42. };
  43. module.exports = {
  44. showWebView
  45. }

扩展后的视图可通过菜单视图-显示扩展视图打开

效果图

如何注册一个视图 - 图2