-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathplugin.ts
133 lines (115 loc) · 4.49 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { BehaviorSubject } from 'rxjs';
import { Plugin, CoreSetup, AppMountParameters, AppDeepLink } from '@kbn/core/public';
import { AppUpdater } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { sortBy } from 'lodash';
import { AppNavLinkStatus, DEFAULT_APP_CATEGORIES } from '@kbn/core/public';
import { UrlForwardingSetup } from '@kbn/url-forwarding-plugin/public';
import { deepLinkIds as devtoolsDeeplinkIds } from '@kbn/deeplinks-devtools';
import { CreateDevToolArgs, DevToolApp, createDevToolApp } from './dev_tool';
import { DocTitleService, BreadcrumbService } from './services';
import './index.scss';
export interface DevToolsSetup {
/**
* Register a developer tool. It will be available
* in the dev tools app under a separate tab.
*
* Registering dev tools works almost similar to registering
* applications in the core application service,
* but they will be rendered with a frame containing tabs
* to switch between the tools.
* @param devTool The dev tools descriptor
*/
register: (devTool: CreateDevToolArgs) => DevToolApp;
}
export class DevToolsPlugin implements Plugin<DevToolsSetup, void> {
private readonly devTools = new Map<string, DevToolApp>();
private appStateUpdater = new BehaviorSubject<AppUpdater>(() => ({}));
private breadcrumbService = new BreadcrumbService();
private docTitleService = new DocTitleService();
private getSortedDevTools(): readonly DevToolApp[] {
return sortBy([...this.devTools.values()], 'order');
}
public setup(coreSetup: CoreSetup, { urlForwarding }: { urlForwarding: UrlForwardingSetup }) {
const { application: applicationSetup, getStartServices } = coreSetup;
applicationSetup.register({
id: 'dev_tools',
title: i18n.translate('devTools.devToolsTitle', {
defaultMessage: 'Dev Tools',
}),
updater$: this.appStateUpdater,
euiIconType: 'logoElastic',
order: 9010,
category: DEFAULT_APP_CATEGORIES.management,
mount: async (params: AppMountParameters) => {
const { element, history, theme$ } = params;
element.classList.add('devAppWrapper');
const [core] = await getStartServices();
const { application, chrome, executionContext } = core;
this.docTitleService.setup(chrome.docTitle.change);
this.breadcrumbService.setup(chrome.setBreadcrumbs);
const appServices = {
breadcrumbService: this.breadcrumbService,
docTitleService: this.docTitleService,
executionContext,
};
const { renderApp } = await import('./application');
return renderApp(
element,
application,
chrome,
history,
theme$,
this.getSortedDevTools(),
appServices
);
},
});
urlForwarding.forwardApp('dev_tools', 'dev_tools');
return {
register: (devToolArgs: CreateDevToolArgs) => {
if (this.devTools.has(devToolArgs.id)) {
throw new Error(
`Dev tool with id [${devToolArgs.id}] has already been registered. Use a unique id.`
);
}
const devTool = createDevToolApp(devToolArgs);
this.devTools.set(devTool.id, devTool);
return devTool;
},
};
}
public start() {
if (this.getSortedDevTools().length === 0) {
this.appStateUpdater.next(() => ({ navLinkStatus: AppNavLinkStatus.hidden }));
} else {
this.appStateUpdater.next(() => {
const deepLinks: AppDeepLink[] = [...this.devTools.values()]
.filter(
// Some tools do not use a string title, so we filter those out
(tool) => !tool.enableRouting && !tool.isDisabled() && typeof tool.title === 'string'
)
.map((tool) => {
const deepLink = {
id: tool.id,
title: tool.title as string,
path: `#/${tool.id}`,
};
if (!devtoolsDeeplinkIds.some((id) => id === deepLink.id)) {
throw new Error('Deeplink must be registered in package.');
}
return deepLink;
});
return { deepLinks };
});
}
}
public stop() {}
}