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.
//package.json;//...NOTE:Package.json does not support comments, you need to delete the comments when using the following codes."contributes": {"customEditors": [{"viewType": "catEdit.catScratch", // Custom editor type id"displayName": "Cat Scratch","selector": [{"fileNamePattern": "*.cscratch" // File name matching pattern}],"priority": "default"},...]}
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.
var hx = require("hbuilderx");// Call Classeslet CustomDocument = hx.CustomEditor.CustomDocument;let CustomEditorProvider = hx.CustomEditor.CustomEditorProvider;let CustomDocumentEditEvent = hx.CustomEditor.CustomDocumentEditEvent;// Inherits CustomDocumentclass CatCustomDocument extends CustomDocument {constructor(uri) {super(uri)}dispose() {super.dispose();}}// Inherit CustomEditorProvider to implement some methodsclass CatCustomEditorProvider extends CustomEditorProvider{constructor(context){super()}openCustomDocument(uri){// create CustomDocumentreturn Promise.resolve(new CatCustomDocument(uri));}resolveCustomEditor(document, webViewPanel){// link CustomDocument and WebViewPanel}saveCustomDocument(document) {// save documentreturn true;}saveCustomDocumentAs(document, destination) {// document save to destinationreturn true;}}
- Register the custom editor of the above extension through API: window.registerCustomEditorProvider when the extension is activated
Custom editor provides new extension activation event onCustomEditor
// package.json declare the type of custom editor that can activate the extension"activationEvents": ["onCustomEditor:catEdit.catScratch"]
// extension.js is activation entryfunction activate(context) {hx.window.registerCustomEditorProvider("catEdit.catScratch", new CatCustomEditorProvider());}
- Others
// Send a document change event to HBuilderX, and the editor tab becomes dirty statusprovider.onDidChangeCustomDocument.fire(new CustomDocumentEditEvent(document));
