mirror of
https://github.com/apache/superset.git
synced 2026-04-19 16:14:52 +00:00
refactor(extensions): simplify registerEditorProvider API (#38127)
This commit is contained in:
committed by
GitHub
parent
2a3567d2f1
commit
cbb80f0462
@@ -172,13 +172,9 @@ import { editors } from '@apache-superset/core';
|
||||
import MonacoSQLEditor from './MonacoSQLEditor';
|
||||
|
||||
export function activate(context) {
|
||||
// Register the Monaco editor for SQL
|
||||
// Register the Monaco editor for SQL using the contribution ID from extension.json
|
||||
const disposable = editors.registerEditorProvider(
|
||||
{
|
||||
id: 'monaco-sql-editor.sql',
|
||||
name: 'Monaco SQL Editor',
|
||||
languages: ['sql'],
|
||||
},
|
||||
'monaco-sql-editor.sql',
|
||||
MonacoSQLEditor,
|
||||
);
|
||||
|
||||
|
||||
@@ -497,25 +497,25 @@ export interface EditorProviderUnregisteredEvent {
|
||||
* Register an editor provider for specific languages.
|
||||
* When an extension registers an editor, it replaces the default for those languages.
|
||||
*
|
||||
* @param contribution The editor contribution metadata from extension.json
|
||||
* The contribution metadata (name, languages, description) is read from the
|
||||
* extension's manifest (extension.json), so only the contribution ID and
|
||||
* component are needed at registration time.
|
||||
*
|
||||
* @param id The editor contribution ID declared in extension.json
|
||||
* @param component The React component implementing EditorProps
|
||||
* @returns A Disposable to unregister the provider
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const disposable = registerEditorProvider(
|
||||
* {
|
||||
* id: 'acme.monaco-sql',
|
||||
* name: 'Monaco SQL Editor',
|
||||
* languages: ['sql'],
|
||||
* },
|
||||
* 'acme.monaco-sql',
|
||||
* MonacoSQLEditor
|
||||
* );
|
||||
* context.disposables.push(disposable);
|
||||
* ```
|
||||
*/
|
||||
export declare function registerEditorProvider(
|
||||
contribution: EditorContribution,
|
||||
id: string,
|
||||
component: EditorComponent,
|
||||
): Disposable;
|
||||
|
||||
|
||||
@@ -26,12 +26,12 @@
|
||||
|
||||
import type { contributions } from '@apache-superset/core';
|
||||
import { editors as editorsApi } from '@apache-superset/core';
|
||||
import ExtensionsManager from 'src/extensions/ExtensionsManager';
|
||||
import { Disposable } from '../models';
|
||||
import EditorProviders from './EditorProviders';
|
||||
|
||||
type EditorLanguage = contributions.EditorLanguage;
|
||||
type EditorProvider = editorsApi.EditorProvider;
|
||||
type EditorContribution = editorsApi.EditorContribution;
|
||||
type EditorComponent = editorsApi.EditorComponent;
|
||||
type EditorProviderRegisteredEvent = editorsApi.EditorProviderRegisteredEvent;
|
||||
type EditorProviderUnregisteredEvent =
|
||||
@@ -39,18 +39,30 @@ type EditorProviderUnregisteredEvent =
|
||||
|
||||
/**
|
||||
* Register an editor provider for specific languages.
|
||||
* When an extension registers an editor, it replaces the default for those languages.
|
||||
* The contribution metadata is resolved from the extension manifest by ID.
|
||||
*
|
||||
* @param contribution The editor contribution metadata from extension.json
|
||||
* @param id The editor contribution ID declared in extension.json
|
||||
* @param component The React component implementing EditorProps
|
||||
* @returns A Disposable to unregister the provider
|
||||
*/
|
||||
export const registerEditorProvider = (
|
||||
contribution: EditorContribution,
|
||||
id: string,
|
||||
component: EditorComponent,
|
||||
): Disposable => {
|
||||
const manager = EditorProviders.getInstance();
|
||||
return manager.registerProvider(contribution, component);
|
||||
const manager = ExtensionsManager.getInstance();
|
||||
const contribution = manager.getEditorContributions().find(c => c.id === id);
|
||||
|
||||
if (!contribution) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(
|
||||
`No editor contribution found in extension.json for id "${id}". ` +
|
||||
'Ensure the editor is declared in the contributions.editors array.',
|
||||
);
|
||||
return new Disposable(() => {});
|
||||
}
|
||||
|
||||
const providers = EditorProviders.getInstance();
|
||||
return providers.registerProvider(contribution, component);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user