UNPKG

12.2 kBTypeScriptView Raw
1import lodash from 'lodash';
2import { connect } from 'react-redux';
3import { Debugger } from 'debug';
4import { ReactNode, Context, FC, FunctionComponent, ReactElement, ComponentClass } from 'react';
5import { Terminal as XTerminal, ITerminalOptions } from 'xterm';
6import * as hooks from '@umijs/hooks';
7import moment from 'moment';
8import * as intl from './locale';
9import { IRoute } from './';
10
11declare namespace IUI {
12 export enum LOCALES {
13 'zh-CN' = '中文',
14 'en-US' = 'English',
15 }
16
17 export enum THEME {
18 'dark' = 'dark',
19 'light' = 'light',
20 }
21
22 export enum CONFIG_TYPES {
23 'string' = 'string',
24 'string[]' = 'string[]',
25 'boolean' = 'boolean',
26 'object' = 'object',
27 'object[]' = 'object[]',
28 'list' = 'list',
29 'textarea' = 'textarea',
30 'any' = 'any',
31 }
32
33 interface IBasicUI {
34 /** framework name, Umi / Bigfish */
35 name: string;
36 /** framework logo ReactNode */
37 logo: ReactNode;
38 /** framework logo image url */
39 logo_remote: string;
40 /** feedback Image */
41 feedback: {
42 /** Image src */
43 src: string;
44 /** Image width */
45 width: number;
46 /** Image height */
47 height: number;
48 };
49 /** create Project Button ReactNode */
50 'create.project.button': ReactNode;
51 /** helpDoc link */
52 helpDoc: string;
53 /** project pages current step */
54 'project.pages': {
55 /** create step */
56 create: ReactNode;
57 };
58 /** Dashboard extend */
59 dashboard: {
60 /** sider Footer */
61 siderFooter: ReactNode;
62 };
63 }
64
65 type ILang = keyof typeof LOCALES;
66 type ITheme = keyof typeof THEME;
67
68 type Locale = { [key in string]: string };
69
70 type ILocale = { [x in ILang]: Locale };
71
72 interface IconType {
73 type: string;
74 theme?: 'filled' | 'outlined' | 'twoTone';
75 rotate?: number;
76 twoToneColor?: string;
77 }
78
79 interface IPanelConfigAction {
80 title: string;
81 type?: 'default' | 'primary';
82 action?: IAction;
83 onClick?: () => void;
84 }
85
86 type IPanelAction = (IPanelConfigAction | ReactNode | FC)[];
87
88 interface IPanel extends IRoute {
89 path: string;
90 component: FunctionComponent | ComponentClass;
91 icon: IconType | string;
92 actions?: IPanelAction;
93 beta?: boolean;
94 /** header title, default use `title` */
95 headerTitle?: ReactNode;
96 /** custom title Component */
97 renderTitle?: (title: string) => ReactNode;
98 }
99
100 interface IService {
101 panels: IPanel[];
102 locales: ILocale[];
103 configSections: any[];
104 basicUI: Partial<IBasicUI>;
105 dashboard: IDashboard[];
106 }
107
108 type SetFactory<T> = ((state: T) => T) | T;
109
110 interface IAction<T = object, K = void> {
111 type: string;
112 payload?: T;
113 onProgress?: (data: K) => void;
114 onMessage?: (data: any) => void;
115 keep?: boolean;
116 }
117
118 type IValue = string | object | boolean | string[] | object[];
119
120 interface IFieldLabel {
121 /** label title */
122 title: string;
123 /** label description */
124 description: string;
125 /** description detail link */
126 link: string;
127 }
128
129 export interface IFieldProps {
130 /** formItem type */
131 type: IConfigTypes;
132 /** form field name */
133 name: string;
134 /** defaultValue(only using in `object` field type) */
135 defaultValue?: IValue;
136 /** Array Select options */
137 options?: string[];
138 /** antd form ins */
139 form: object;
140 /** antd label, if object using <Label /> */
141 label: string | ReactNode | IFieldLabel;
142 /** size, default */
143 size?: 'default' | 'small' | 'large';
144 /** same as antd Form.Item props */
145 [key: string]: any;
146 }
147
148 type IConfigTypes = keyof typeof CONFIG_TYPES;
149 type ITerminal = XTerminal;
150
151 interface ITwoColumnPanel {
152 className?: string;
153 sections: Array<{
154 key?: string;
155 title: string;
156 icon: string | ReactNode;
157 description: string;
158 component: FunctionComponent<any>;
159 }>;
160 disableLeftOverflow?: boolean;
161 disableRightOverflow?: boolean;
162 }
163
164 interface ITerminalProps {
165 /** Terminal title */
166 title?: ReactNode;
167 className?: string;
168 terminalClassName?: string;
169 /** defaultValue in Terminal */
170 defaultValue?: string;
171 /** terminal init event */
172 onInit?: (ins: XTerminal, fitAddon: any) => void;
173 /** https://xtermjs.org/docs/api/terminal/interfaces/iterminaloptions/ */
174 config?: ITerminalOptions;
175 onResize?: (ins: XTerminal) => void;
176 [key: string]: any;
177 }
178
179 interface IDirectoryForm {
180 /** path / cwd */
181 value?: string;
182 onChange?: (value: string) => void;
183 }
184
185 interface IStepItemForm {
186 currentStep: number;
187 handleFinish: () => void;
188 goNext: () => void;
189 goPrev: () => void;
190 index: number;
191 active: boolean;
192 [key: string]: any;
193 }
194
195 interface IStepItemProps {
196 children: ReactElement<Partial<IStepItemForm>>;
197 [key: string]: any;
198 }
199
200 interface IStepFormProps {
201 onFinish: (values: object) => void;
202 className?: string;
203 children: ReactElement<IStepItemForm>[];
204 }
205 type IStepForm = FC<IStepFormProps> & {
206 StepItem: FC<IStepItemProps>;
207 };
208
209 // from fuzz.js
210 export interface FuseOptions<T> {
211 id?: keyof T;
212 caseSensitive?: boolean;
213 includeMatches?: boolean;
214 includeScore?: boolean;
215 shouldSort?: boolean;
216 sortFn?: (a: { score: number }, b: { score: number }) => number;
217 getFn?: (obj: any, path: string) => any;
218 keys?: (keyof T)[] | T[keyof T] | { name: keyof T; weight: number }[];
219 verbose?: boolean;
220 tokenize?: boolean;
221 tokenSeparator?: RegExp;
222 matchAllTokens?: boolean;
223 location?: number;
224 distance?: number;
225 threshold?: number;
226 maxPatternLength?: number;
227 minMatchCharLength?: number;
228 findAllMatches?: boolean;
229 }
230
231 interface IConfigFormProps {
232 /** config title in the top */
233 title: string;
234 /** list config interface */
235 list: string;
236 /** edit config interface */
237 edit: string;
238 /** enable Toc, default false */
239 enableToc?: boolean;
240 /** Search fuse options */
241 fuseOpts?: FuseOptions<number>;
242 }
243
244 type IApiActionFactory<P = {}, K = {}> = (action: IAction<P, K>) => Promise<K>;
245
246 type ICallRemote<T = unknown, P = unknown> = IApiActionFactory<T, P>;
247 type IListenRemote = IApiActionFactory<{}, () => void>;
248 type ISend = IApiActionFactory<{}, void>;
249 type IFormatMessage = typeof intl.formatMessage;
250 type PickIntl = Pick<
251 typeof intl,
252 | 'FormattedDate'
253 | 'FormattedTime'
254 | 'FormattedRelative'
255 | 'FormattedNumber'
256 | 'FormattedPlural'
257 | 'FormattedMessage'
258 | 'FormattedHTMLMessage'
259 | 'formatMessage'
260 | 'formatHTMLMessage'
261 | 'formatDate'
262 | 'formatTime'
263 | 'formatRelative'
264 | 'formatNumber'
265 | 'formatPlural'
266 >;
267 type IIntl<T = PickIntl> = { [key in keyof T]: T[key] } & typeof intl.formatMessage;
268 type IGetCwd = () => Promise<string>;
269
270 interface INotifyParams {
271 title: string;
272 message: string;
273 subtitle?: string;
274 /** URL to open on click */
275 open?: string;
276 /**
277 * The amount of seconds before the notification closes.
278 * Takes precedence over wait if both are defined.
279 */
280 timeout?: number;
281 /** notify type, default info */
282 type?: 'error' | 'info' | 'warning' | 'success';
283 }
284 interface IDashboard {
285 /** uniq dashboard Card id, required */
286 key: string;
287 enable?: boolean;
288 /** card title */
289 title: ReactNode;
290 /** card description */
291 description: ReactNode;
292 /** icon */
293 icon: ReactNode;
294 /** card Right button */
295 right?: ReactNode;
296 /** col wrapper container className */
297 colClassName?: string;
298 /** icon background color, default #459BF7 */
299 backgroundColor?: string;
300 content: ReactNode | ReactNode[];
301 }
302
303 type INotify = (params: INotifyParams) => void | boolean;
304 type IAddPanel = (panel: IPanel) => void;
305 type IAddDashboard = (dashboard: IDashboard | IDashboard[]) => void;
306 type IRegisterModel = (model: any) => void;
307 type IAddLocales = (locale: ILocale) => void;
308 type IShowLogPanel = () => void;
309 type IHideLogPanel = () => void;
310 type ILodash = typeof lodash;
311 interface ICurrentProject {
312 key?: string;
313 name?: string;
314 path?: string;
315 created_at?: number;
316 opened_at?: number;
317 npmClient?: string;
318 taobaoSpeedUp?: boolean;
319 }
320
321 type IRedirect = (url: string) => void;
322 type IEvent = NodeJS.EventEmitter;
323 type IDebug = Debugger;
324 type IConnect = typeof connect;
325 type IMini = () => boolean;
326 type IShowMini = () => void;
327 type IHideMini = () => void;
328 type IGetLocale = () => ILang;
329 type IGetDashboard = () => IDashboard[];
330 type IGetBasicUI = () => IBasicUI;
331 type IGetSharedDataDir = () => Promise<string>;
332 type IGetSearchParams = () => any;
333 type IDetectLanguage = () => Promise<string>;
334 type ISetActionPanel = (action: SetFactory<IPanelAction>) => void;
335 type IModifyBasicUI = (memo: Partial<IBasicUI>) => void;
336 type LaunchEditorTypes = 'project' | 'config';
337 type IMoment = typeof moment;
338 interface IAnalyze {
339 gtag: any;
340 Tracert: any;
341 }
342
343 interface ILaunchEditorParams {
344 type: LaunchEditorTypes;
345 lineNumber?: number;
346 editor?: string;
347 }
348 // beta API, not show in doc
349 type ILaunchEditor = (params: ILaunchEditorParams) => void;
350
351 interface IContext {
352 theme: ITheme;
353 locale: ILang;
354 currentProject?: ICurrentProject;
355 formatMessage: typeof intl.formatMessage;
356 FormattedMessage: typeof intl.FormattedMessage;
357 setLocale: typeof intl.setLocale;
358 setTheme: (theme: ITheme) => void;
359 /** open footer log panel */
360 showLogPanel: IShowLogPanel;
361 /** close footer log panel */
362 hideLogPanel: IHideLogPanel;
363 isMini: boolean;
364 basicUI: IBasicUI;
365 service: IService;
366 }
367
368 type UmiHooks = typeof hooks;
369
370 interface Hooks extends UmiHooks {
371 // extend more hooks
372 }
373
374 class IApiClass {
375 constructor(service: IService);
376 service: IService;
377 /** event */
378 event: IEvent;
379 /** React hooks for UI development */
380 readonly hooks: Hooks;
381 /** umi-request */
382 readonly request: any;
383 /** lodash */
384 readonly _: ILodash;
385 /** debug for client */
386 readonly debug: IDebug;
387 /** can use as vars */
388 readonly mini: boolean;
389 /** whether Bigfish */
390 readonly bigfish: boolean;
391 readonly _analyze: IAnalyze;
392 /** currentProject */
393 currentProject: ICurrentProject;
394 /** get current locale: zh-CN or en-US */
395 getLocale: IGetLocale;
396 getCwd: IGetCwd;
397 /** current is in Mini version */
398 isMini(): boolean;
399 /** intl, formatMessage */
400 intl: IIntl;
401 /** add plugin Panel */
402 addPanel: IAddPanel;
403 addDashboard: IAddDashboard;
404 /** register dva model Panel */
405 registerModel: IRegisterModel;
406 /** add plugin locales { zh-CN: {}, en-US: {} } */
407 addLocales: IAddLocales;
408 /** react component context */
409 getContext(): Context<IContext>;
410 /** get Plugin UI Service */
411 getBasicUI: IGetBasicUI;
412 /** system notify */
413 notify: INotify;
414 /** moment */
415 moment: IMoment;
416 /** redirect */
417 redirect: IRedirect;
418 callRemote: ICallRemote;
419 /** React Two Column Panel Layout */
420 TwoColumnPanel: FC<ITwoColumnPanel>;
421 /** Terminal Component */
422 Terminal: FC<ITerminalProps>;
423 DirectoryForm: FC<IDirectoryForm>;
424 StepForm: IStepForm;
425 /** React Config Form Component */
426 ConfigForm: FC<IConfigFormProps>;
427 /** Antd Form Field */
428 Field: FC<IFieldProps>;
429 listenRemote: IListenRemote;
430 launchEditor: ILaunchEditor;
431 /** open footer log panel */
432 showLogPanel: IShowLogPanel;
433 /** close footer log panel */
434 hideLogPanel: IHideLogPanel;
435 setActionPanel: ISetActionPanel;
436 /** show Mini Modal */
437 showMini: IShowMini;
438 /** hide Mini Modal */
439 hideMini: IHideMini;
440 send: ISend;
441 connect: IConnect;
442 /** get dashboard list */
443 getDashboard: IGetDashboard;
444 /** get the current project's temp dir path */
445 getSharedDataDir: IGetSharedDataDir;
446 /** get location search params */
447 getSearchParams: IGetSearchParams;
448 detectLanguage: IDetectLanguage;
449 detectNpmClients: () => Promise<string[]>;
450 modifyBasicUI: IModifyBasicUI;
451 }
452
453 type IApi = InstanceType<typeof IApiClass>;
454}
455
456export = IUI;