Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Visualizations] Remove vis_default_editor - visualize plugins cyclic dependencies #85422

Merged
merged 11 commits into from
Dec 22, 2020
1 change: 0 additions & 1 deletion src/dev/run_find_plugins_with_circular_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type CircularDepList = Set<string>;
const allowedList: CircularDepList = new Set([
'src/plugins/charts -> src/plugins/discover',
'src/plugins/vis_default_editor -> src/plugins/visualizations',
'src/plugins/vis_default_editor -> src/plugins/visualize',
'src/plugins/visualizations -> src/plugins/visualize',
'x-pack/plugins/actions -> x-pack/plugins/case',
'x-pack/plugins/case -> x-pack/plugins/security_solution',
Expand Down
1 change: 1 addition & 0 deletions src/plugins/vis_default_editor/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"id": "visDefaultEditor",
"version": "kibana",
"ui": true,
"optionalPlugins": ["visualize"],
"requiredBundles": ["kibanaUtils", "kibanaReact", "data"]
}
16 changes: 8 additions & 8 deletions src/plugins/vis_default_editor/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
* under the License.
*/

export { DefaultEditorController } from './default_editor_controller';
import { PluginInitializerContext } from 'kibana/public';
import { DefaultEditorController } from './default_editor_controller';
import { VisDefaultEditorPlugin } from './plugin';

export { DefaultEditorController };
export { useValidation } from './components/controls/utils';
export * from './components/options';
export { RangesParamEditor, RangeValues } from './components/controls/ranges';
Expand All @@ -26,10 +30,6 @@ export * from './vis_options_props';
export * from './utils';
export { ISchemas, Schemas, Schema } from './schemas';

/** dummy plugin, we just want visDefaultEditor to have its own bundle */
export function plugin() {
return new (class VisDefaultEditor {
setup() {}
start() {}
})();
}
export const plugin = (context: PluginInitializerContext) => {
return new VisDefaultEditorPlugin();
};
40 changes: 40 additions & 0 deletions src/plugins/vis_default_editor/public/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { CoreSetup, Plugin } from 'kibana/public';

import { VisualizePluginSetup } from '../../visualize/public';
import { DefaultEditorController } from './default_editor_controller';

export interface VisDefaultEditorSetupDependencies {
visualize: VisualizePluginSetup;
}

export class VisDefaultEditorPlugin
implements Plugin<void, void, VisDefaultEditorSetupDependencies, {}> {
public setup(core: CoreSetup, { visualize }: VisDefaultEditorSetupDependencies) {
if (visualize) {
visualize.setDefaultEditor(DefaultEditorController);
}
}

public start() {}

stop() {}
}
5 changes: 2 additions & 3 deletions src/plugins/visualize/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"kibanaUtils",
"kibanaReact",
"home",
"discover",
"visDefaultEditor",
"presentationUtil"
"presentationUtil",
"discover"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { redirectWhenMissing } from '../../../../../kibana_utils/public';
import { getEditBreadcrumbs, getCreateBreadcrumbs } from '../breadcrumbs';
import { VisualizeServices } from '../../types';
import { VisualizeConstants } from '../../visualize_constants';
import { setDefaultEditor } from '../../../services';

const mockDefaultEditorControllerDestroy = jest.fn();
const mockEmbeddableHandlerDestroy = jest.fn();
Expand Down Expand Up @@ -54,10 +55,14 @@ jest.mock('../breadcrumbs', () => ({
getEditBreadcrumbs: jest.fn((text) => text),
getCreateBreadcrumbs: jest.fn((text) => text),
}));
jest.mock('../../../../../vis_default_editor/public', () => ({
DefaultEditorController: jest.fn(() => ({ destroy: mockDefaultEditorControllerDestroy })),
}));
jest.mock('../../../../../kibana_utils/public');

jest.mock('../../../../../kibana_utils/public', () => {
const actual = jest.requireActual('../../../../../kibana_utils/public');
return {
...actual,
redirectWhenMissing: jest.fn(),
};
});

const mockGetVisualizationInstance = jest.requireMock('../get_visualization_instance')
.getVisualizationInstance;
Expand All @@ -69,6 +74,10 @@ describe('useSavedVisInstance', () => {
const eventEmitter = new EventEmitter();

beforeEach(() => {
setDefaultEditor(
jest.fn().mockImplementation(() => ({ destroy: mockDefaultEditorControllerDestroy }))
);

mockServices = ({
...coreStartMock,
toastNotifications,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import { parse } from 'query-string';
import { i18n } from '@kbn/i18n';

import { redirectWhenMissing } from '../../../../../kibana_utils/public';
import { DefaultEditorController } from '../../../../../vis_default_editor/public';

import { getVisualizationInstance } from '../get_visualization_instance';
import { getEditBreadcrumbs, getCreateBreadcrumbs } from '../breadcrumbs';
import { SavedVisInstance, IEditorController, VisualizeServices } from '../../types';
import { VisualizeConstants } from '../../visualize_constants';
import { getDefaultEditor } from '../../../services';

/**
* This effect is responsible for instantiating a saved vis or creating a new one
Expand Down Expand Up @@ -104,7 +104,7 @@ export const useSavedVisInstance = (
// do not create editor in embeded mode
if (visEditorRef.current) {
if (isChromeVisible) {
const Editor = vis.type.editor || DefaultEditorController;
const Editor = vis.type.editor || getDefaultEditor();
visEditorController = new Editor(
visEditorRef.current,
vis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { VisualizeInput } from 'src/plugins/visualizations/public';
import { ByValueVisInstance, IEditorController, VisualizeServices } from '../../types';
import { getVisualizationInstanceFromInput } from '../get_visualization_instance';
import { getBreadcrumbsPrefixedWithApp, getEditBreadcrumbs } from '../breadcrumbs';
import { DefaultEditorController } from '../../../../../vis_default_editor/public';
import { getDefaultEditor } from '../../../services';

export const useVisByValue = (
services: VisualizeServices,
Expand All @@ -46,7 +46,8 @@ export const useVisByValue = (
}
const byValueVisInstance = await getVisualizationInstanceFromInput(services, valueInput);
const { embeddableHandler, vis } = byValueVisInstance;
const Editor = vis.type.editor || DefaultEditorController;

const Editor = vis.type.editor || getDefaultEditor();
const visEditorController = new Editor(
visEditorRef.current,
vis,
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/visualize/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { PluginInitializerContext } from 'kibana/public';
import { VisualizePlugin } from './plugin';
import { VisualizePlugin, VisualizePluginSetup } from './plugin';

export type {
EditorRenderProps,
Expand All @@ -27,6 +27,8 @@ export type {
} from './application/types';
export { VisualizeConstants } from './application/visualize_constants';

export { VisualizePluginSetup };

export const plugin = (context: PluginInitializerContext) => {
return new VisualizePlugin(context);
};
20 changes: 18 additions & 2 deletions src/plugins/visualize/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { UrlForwardingSetup, UrlForwardingStart } from '../../url_forwarding/pub
import { VisualizationsStart } from '../../visualizations/public';
import { VisualizeConstants } from './application/visualize_constants';
import { FeatureCatalogueCategory, HomePublicPluginSetup } from '../../home/public';
import { VisualizeServices } from './application/types';
import { VisEditorConstructor, VisualizeServices } from './application/types';
import { DEFAULT_APP_CATEGORIES } from '../../../core/public';
import { SavedObjectsStart } from '../../saved_objects/public';
import { EmbeddableStart } from '../../embeddable/public';
Expand All @@ -57,6 +57,7 @@ import {
setIndexPatterns,
setQueryService,
setShareService,
setDefaultEditor,
} from './services';
import { visualizeFieldAction } from './actions/visualize_field_action';
import { createVisualizeUrlGenerator } from './url_generator';
Expand All @@ -81,9 +82,18 @@ export interface VisualizePluginSetupDependencies {
uiActions: UiActionsSetup;
}

export interface VisualizePluginSetup {
setDefaultEditor: (editor: VisEditorConstructor) => void;
}

export class VisualizePlugin
implements
Plugin<void, void, VisualizePluginSetupDependencies, VisualizePluginStartDependencies> {
Plugin<
VisualizePluginSetup,
void,
VisualizePluginSetupDependencies,
VisualizePluginStartDependencies
> {
private appStateUpdater = new BehaviorSubject<AppUpdater>(() => ({}));
private stopUrlTracking: (() => void) | undefined = undefined;
private currentHistory: ScopedHistory | undefined = undefined;
Expand Down Expand Up @@ -231,6 +241,12 @@ export class VisualizePlugin
category: FeatureCatalogueCategory.DATA,
});
}

return {
setDefaultEditor: (editor) => {
setDefaultEditor(editor);
},
} as VisualizePluginSetup;
}

public start(core: CoreStart, plugins: VisualizePluginStartDependencies) {
Expand Down
5 changes: 5 additions & 0 deletions src/plugins/visualize/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ApplicationStart, IUiSettingsClient } from '../../../core/public';
import { createGetterSetter } from '../../../plugins/kibana_utils/public';
import { IndexPatternsContract, DataPublicPluginStart } from '../../../plugins/data/public';
import { SharePluginStart } from '../../../plugins/share/public';
import { VisEditorConstructor } from './application/types';

export const [getUISettings, setUISettings] = createGetterSetter<IUiSettingsClient>('UISettings');

Expand All @@ -32,6 +33,10 @@ export const [getIndexPatterns, setIndexPatterns] = createGetterSetter<IndexPatt
'IndexPatterns'
);

export const [getDefaultEditor, setDefaultEditor] = createGetterSetter<VisEditorConstructor>(
'DefaultEditor'
);

export const [getQueryService, setQueryService] = createGetterSetter<
DataPublicPluginStart['query']
>('Query');