How to extend a custom editor?

Supported from HBuilderX 2.9.2+

  • Configure contribution points through [customEditors] (/ExtensionDocs/ContributionPoints/README.md#customEditors) and declare custom editors that need to be registered.
  1. //package.json
  2. //...NOTEPackage.json does not support comments, you need to delete the comments when using the following codes.
  3. "contributes": {
  4. "customEditors": [{
  5. "viewType": "catEdit.catScratch", // Custom editor type id
  6. "displayName": "Cat Scratch",
  7. "selector": [{
  8. "fileNamePattern": "*.cscratch" // File name matching pattern
  9. }],
  10. "priority": "default"
  11. },
  12. ...]
  13. }
  • Extension inherits from CustomEditorProvider

    HBuilderX uses WebViewPanel as the view of the custom editor. The usage of WebViewPanel can also refer to View Extension Example.

  1. var hx = require("hbuilderx");
  2. // Call Classes
  3. let CustomDocument = hx.CustomEditor.CustomDocument;
  4. let CustomEditorProvider = hx.CustomEditor.CustomEditorProvider;
  5. let CustomDocumentEditEvent = hx.CustomEditor.CustomDocumentEditEvent;
  6. // Inherits CustomDocument
  7. class CatCustomDocument extends CustomDocument {
  8. constructor(uri) {
  9. super(uri)
  10. }
  11. dispose() {
  12. super.dispose();
  13. }
  14. }
  15. // Inherit CustomEditorProvider to implement some methods
  16. class CatCustomEditorProvider extends CustomEditorProvider{
  17. constructor(context){
  18. super()
  19. }
  20. openCustomDocument(uri){
  21. // create CustomDocument
  22. return Promise.resolve(new CatCustomDocument(uri));
  23. }
  24. resolveCustomEditor(document, webViewPanel){
  25. // link CustomDocument and WebViewPanel
  26. }
  27. saveCustomDocument(document) {
  28. // save document
  29. return true;
  30. }
  31. saveCustomDocumentAs(document, destination) {
  32. // document save to destination
  33. return true;
  34. }
  35. }

Custom editor provides new extension activation event onCustomEditor

  1. // package.json declare the type of custom editor that can activate the extension
  2. "activationEvents": [
  3. "onCustomEditor:catEdit.catScratch"
  4. ]
  1. // extension.js is activation entry
  2. function activate(context) {
  3. hx.window.registerCustomEditorProvider("catEdit.catScratch", new CatCustomEditorProvider());
  4. }
  • Others
  1. // Send a document change event to HBuilderX, and the editor tab becomes dirty status
  2. provider.onDidChangeCustomDocument.fire(new CustomDocumentEditEvent(document));