@ibiz-template/core 0.0.1-beta.1 → 0.0.1-beta.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/dist/system/index.system.js +1 -1
  2. package/out/command/command-register.d.ts +66 -0
  3. package/out/command/command-register.d.ts.map +1 -0
  4. package/out/command/command-register.js +113 -0
  5. package/out/command/command.d.ts +54 -0
  6. package/out/command/command.d.ts.map +1 -0
  7. package/out/command/command.js +71 -0
  8. package/out/command/index.d.ts +10 -0
  9. package/out/command/index.d.ts.map +1 -0
  10. package/out/command/index.js +9 -0
  11. package/out/command/interface/command/command-option.d.ts +23 -0
  12. package/out/command/interface/command/command-option.d.ts.map +1 -0
  13. package/out/command/interface/command/command-option.js +1 -0
  14. package/out/command/interface/command/command.d.ts +74 -0
  15. package/out/command/interface/command/command.d.ts.map +1 -0
  16. package/out/command/interface/command/command.js +1 -0
  17. package/out/command/interface/disposable/disposable.d.ts +4 -0
  18. package/out/command/interface/disposable/disposable.d.ts.map +1 -0
  19. package/out/command/interface/disposable/disposable.js +1 -0
  20. package/out/command/interface/index.d.ts +4 -0
  21. package/out/command/interface/index.d.ts.map +1 -0
  22. package/out/command/interface/index.js +1 -0
  23. package/out/command/utils/index.d.ts +3 -0
  24. package/out/command/utils/index.d.ts.map +1 -0
  25. package/out/command/utils/index.js +2 -0
  26. package/out/command/utils/linked-list.d.ts +16 -0
  27. package/out/command/utils/linked-list.d.ts.map +1 -0
  28. package/out/command/utils/linked-list.js +120 -0
  29. package/out/command/utils/util.d.ts +27 -0
  30. package/out/command/utils/util.d.ts.map +1 -0
  31. package/out/command/utils/util.js +77 -0
  32. package/out/context/index.d.ts +24 -0
  33. package/out/context/index.d.ts.map +1 -1
  34. package/out/context/index.js +24 -1
  35. package/out/error/http-error/http-error.d.ts.map +1 -1
  36. package/out/error/http-error/http-error.js +0 -4
  37. package/out/ibizsys.d.ts +9 -0
  38. package/out/ibizsys.d.ts.map +1 -1
  39. package/out/ibizsys.js +9 -0
  40. package/out/index.d.ts +1 -0
  41. package/out/index.d.ts.map +1 -1
  42. package/out/index.js +1 -0
  43. package/out/interface/click-outside/click-outside.d.ts +1 -1
  44. package/out/interface/click-outside/click-outside.d.ts.map +1 -1
  45. package/out/utils/download-file/download-file.d.ts +10 -0
  46. package/out/utils/download-file/download-file.d.ts.map +1 -1
  47. package/out/utils/download-file/download-file.js +20 -0
  48. package/out/utils/index.d.ts +3 -0
  49. package/out/utils/index.d.ts.map +1 -1
  50. package/out/utils/index.js +3 -0
  51. package/out/utils/sync/count-latch.d.ts +60 -0
  52. package/out/utils/sync/count-latch.d.ts.map +1 -0
  53. package/out/utils/sync/count-latch.js +93 -0
  54. package/out/utils/sync/index.d.ts +2 -0
  55. package/out/utils/sync/index.d.ts.map +1 -0
  56. package/out/utils/sync/index.js +1 -0
  57. package/out/utils/upload/select-file.d.ts +53 -0
  58. package/out/utils/upload/select-file.d.ts.map +1 -0
  59. package/out/utils/upload/select-file.js +47 -0
  60. package/out/utils/upload/upload-file.d.ts +46 -0
  61. package/out/utils/upload/upload-file.d.ts.map +1 -0
  62. package/out/utils/upload/upload-file.js +150 -0
  63. package/package.json +3 -3
  64. package/src/command/command-register.ts +135 -0
  65. package/src/command/command.ts +79 -0
  66. package/src/command/index.ts +11 -0
  67. package/src/command/interface/command/command-option.ts +22 -0
  68. package/src/command/interface/command/command.ts +86 -0
  69. package/src/command/interface/disposable/disposable.ts +3 -0
  70. package/src/command/interface/index.ts +9 -0
  71. package/src/command/utils/index.ts +2 -0
  72. package/src/command/utils/linked-list.ts +136 -0
  73. package/src/command/utils/util.ts +95 -0
  74. package/src/context/index.ts +45 -1
  75. package/src/error/http-error/http-error.ts +0 -4
  76. package/src/ibizsys.ts +10 -0
  77. package/src/index.ts +1 -0
  78. package/src/utils/download-file/download-file.ts +21 -0
  79. package/src/utils/index.ts +3 -0
  80. package/src/utils/sync/count-latch.ts +99 -0
  81. package/src/utils/sync/index.ts +1 -0
  82. package/src/utils/upload/select-file.ts +86 -0
  83. package/src/utils/upload/upload-file.ts +206 -0
@@ -0,0 +1,53 @@
1
+ /**
2
+ * 文件List转数组
3
+ *
4
+ * @author lxm
5
+ * @date 2022-11-18 13:11:03
6
+ * @export
7
+ * @param {FileList} fileList
8
+ * @returns {*}
9
+ */
10
+ export declare function fileListToArr(fileList: FileList): File[];
11
+ /**
12
+ * JS打开文件上传操作配置参数
13
+ *
14
+ * @author lxm
15
+ * @date 2022-11-20 21:11:47
16
+ * @export
17
+ * @interface SelectFileOpts
18
+ */
19
+ export interface SelectFileOpts {
20
+ /**
21
+ * 接受的文件类型
22
+ *
23
+ * @author lxm
24
+ * @date 2022-11-20 21:11:52
25
+ * @type {string}
26
+ */
27
+ accept?: string;
28
+ /**
29
+ * 是否支持多选
30
+ *
31
+ * @author lxm
32
+ * @date 2022-11-20 21:11:52
33
+ * @type {boolean}
34
+ */
35
+ multiple?: boolean;
36
+ /**
37
+ * 选中文件后回调
38
+ *
39
+ * @author lxm
40
+ * @date 2022-11-20 21:11:50
41
+ */
42
+ onSelected: (_fileList: File[]) => void;
43
+ }
44
+ /**
45
+ * JS打开文件上传操作
46
+ *
47
+ * @author lxm
48
+ * @date 2022-11-20 21:11:31
49
+ * @export
50
+ * @param {SelectFileOpts} _opts 配置参数
51
+ */
52
+ export declare function selectFile(_opts: SelectFileOpts): void;
53
+ //# sourceMappingURL=select-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"select-file.d.ts","sourceRoot":"","sources":["../../../src/utils/upload/select-file.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,EAAE,CAMxD;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;;;;OAKG;IACH,UAAU,EAAE,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,IAAI,CAAC;CACzC;AAED;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,cAAc,QAwB/C"}
@@ -0,0 +1,47 @@
1
+ import { merge } from 'lodash-es';
2
+ /**
3
+ * 文件List转数组
4
+ *
5
+ * @author lxm
6
+ * @date 2022-11-18 13:11:03
7
+ * @export
8
+ * @param {FileList} fileList
9
+ * @returns {*}
10
+ */
11
+ export function fileListToArr(fileList) {
12
+ const files = [];
13
+ for (let i = 0; i < fileList.length; i++) {
14
+ files.push(fileList[i]);
15
+ }
16
+ return files;
17
+ }
18
+ /**
19
+ * JS打开文件上传操作
20
+ *
21
+ * @author lxm
22
+ * @date 2022-11-20 21:11:31
23
+ * @export
24
+ * @param {SelectFileOpts} _opts 配置参数
25
+ */
26
+ export function selectFile(_opts) {
27
+ const opts = merge({
28
+ multiple: true,
29
+ accept: '',
30
+ }, _opts);
31
+ const input = document.createElement('input');
32
+ input.setAttribute('type', 'file');
33
+ input.setAttribute('multiple', `${opts.multiple}`);
34
+ input.setAttribute('accept', opts.accept);
35
+ input.onchange = e => {
36
+ const inputEl = e.target;
37
+ const files = inputEl.files ? fileListToArr(inputEl.files) : [];
38
+ if (files.length === 0) {
39
+ return;
40
+ }
41
+ opts.onSelected(files);
42
+ inputEl.value = ''; // 如果不置空,相同的文件不会触发change事件
43
+ };
44
+ document.body.appendChild(input);
45
+ input.click();
46
+ document.body.removeChild(input);
47
+ }
@@ -0,0 +1,46 @@
1
+ import { HttpResponse } from '../net/http-response';
2
+ export interface IUploadFile {
3
+ name: string;
4
+ uid: string;
5
+ status: 'uploading' | 'finished' | 'fail' | 'cancel';
6
+ percentage: number;
7
+ response?: HttpResponse;
8
+ error?: unknown;
9
+ }
10
+ export interface IUploadFileOpts {
11
+ uploadUrl: string;
12
+ /**
13
+ * 接受的文件类型
14
+ *
15
+ * @author lxm
16
+ * @date 2022-11-20 21:11:52
17
+ * @type {string}
18
+ */
19
+ accept?: string;
20
+ /**
21
+ * 是否支持多选
22
+ *
23
+ * @author lxm
24
+ * @date 2022-11-20 21:11:52
25
+ * @type {Boolean}
26
+ */
27
+ multiple?: boolean;
28
+ separate?: string;
29
+ request?: (_files: File[]) => Promise<HttpResponse>;
30
+ beforeUpload?: (_fileData: File[], _files: IUploadFile[]) => boolean;
31
+ finish?: (_resultFiles: IUploadFile[]) => void;
32
+ success?: (_resultFiles: IUploadFile[], _res: HttpResponse) => void;
33
+ error?: (_resultFiles: IUploadFile[], _error: unknown) => void;
34
+ progress?: (_files: IUploadFile[]) => void;
35
+ }
36
+ /**
37
+ * 使用上传文件逻辑
38
+ *
39
+ * @author lxm
40
+ * @date 2022-11-20 21:11:52
41
+ * @export
42
+ * @param {IUploadFileOpts} _opts
43
+ * @returns {*}
44
+ */
45
+ export declare function uploadFile(_opts: IUploadFileOpts): void;
46
+ //# sourceMappingURL=upload-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upload-file.d.ts","sourceRoot":"","sources":["../../../src/utils/upload/upload-file.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAGpD,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,WAAW,GAAG,UAAU,GAAG,MAAM,GAAG,QAAQ,CAAC;IACrD,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,YAAY,CAAC;IACxB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC;IACpD,YAAY,CAAC,EAAE,CAAC,SAAS,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,OAAO,CAAC;IACrE,MAAM,CAAC,EAAE,CAAC,YAAY,EAAE,WAAW,EAAE,KAAK,IAAI,CAAC;IAC/C,OAAO,CAAC,EAAE,CAAC,YAAY,EAAE,WAAW,EAAE,EAAE,IAAI,EAAE,YAAY,KAAK,IAAI,CAAC;IACpE,KAAK,CAAC,EAAE,CAAC,YAAY,EAAE,WAAW,EAAE,EAAE,MAAM,EAAE,OAAO,KAAK,IAAI,CAAC;IAC/D,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,IAAI,CAAC;CAC5C;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,eAAe,QA0JhD"}
@@ -0,0 +1,150 @@
1
+ /* eslint-disable @typescript-eslint/no-empty-function */
2
+ /* eslint-disable no-param-reassign */
3
+ import { cloneDeep, isFunction, merge, round, uniqueId } from 'lodash-es';
4
+ import { selectFile } from './select-file';
5
+ /**
6
+ * 使用上传文件逻辑
7
+ *
8
+ * @author lxm
9
+ * @date 2022-11-20 21:11:52
10
+ * @export
11
+ * @param {IUploadFileOpts} _opts
12
+ * @returns {*}
13
+ */
14
+ export function uploadFile(_opts) {
15
+ const opts = merge({
16
+ multiple: true,
17
+ accept: '',
18
+ separate: true,
19
+ beforeUpload: (_fileData, _files) => true,
20
+ finish: (_resultFiles) => { },
21
+ success: (_resultFiles, _res) => { },
22
+ error: (_resultFiles, _error) => { },
23
+ progress: (_files) => { },
24
+ }, _opts);
25
+ /**
26
+ * 进度条回调
27
+ *
28
+ * @author lxm
29
+ * @date 2022-11-20 21:11:47
30
+ * @param {IParams} event
31
+ * @param {IUploadFile[]} files
32
+ */
33
+ const onUploadProgress = (event, files) => {
34
+ files.forEach(file => {
35
+ file.percentage = round(event.progress * 100);
36
+ });
37
+ opts.progress(cloneDeep(files));
38
+ };
39
+ /**
40
+ * 上传请求方法,返回响应对象
41
+ *
42
+ * @author lxm
43
+ * @date 2022-11-18 13:11:57
44
+ * @param {File[]} files
45
+ * @returns {*} {Promise<HttpResponse>}
46
+ */
47
+ const uploadRequest = async (files, onProgress) => {
48
+ // 自定义上传请求
49
+ if (opts.request && isFunction(opts.request)) {
50
+ return opts.request(files);
51
+ }
52
+ // 默认每次单个文件上传,可能存在接口一次上传多个文件,
53
+ const data = new FormData();
54
+ files.forEach(file => {
55
+ data.append('file', file);
56
+ });
57
+ const res = await ibiz.net.request(opts.uploadUrl, {
58
+ method: 'post',
59
+ baseURL: '',
60
+ data,
61
+ headers: { 'Content-Type': 'multipart/form-data' },
62
+ onUploadProgress: onProgress,
63
+ });
64
+ return res;
65
+ };
66
+ /**
67
+ * 执行一次上传,可能是一个文件也可能是多个文件
68
+ *
69
+ * @author lxm
70
+ * @date 2022-11-18 14:11:54
71
+ * @param {File[]} files 此次上传文件的集合
72
+ * @returns {*} {Promise<IUploadFile[]>}
73
+ */
74
+ const executeSingleUpload = async (files) => {
75
+ const resultFiles = files.map(file => {
76
+ return {
77
+ status: 'uploading',
78
+ name: file.name,
79
+ uid: uniqueId(),
80
+ percentage: 0,
81
+ };
82
+ });
83
+ // 上传前回调,可以取消此次下载
84
+ const pass = opts.beforeUpload(files, resultFiles);
85
+ if (!pass) {
86
+ resultFiles.forEach(file => {
87
+ file.status = 'cancel';
88
+ });
89
+ ibiz.log.debug('取消上传', resultFiles);
90
+ return resultFiles;
91
+ }
92
+ try {
93
+ const res = await uploadRequest(files, event => {
94
+ onUploadProgress(event, resultFiles);
95
+ });
96
+ resultFiles.forEach(file => {
97
+ file.status = 'finished';
98
+ });
99
+ // 上传成功事件
100
+ opts.success(resultFiles, res);
101
+ resultFiles.forEach(file => {
102
+ file.response = res;
103
+ });
104
+ }
105
+ catch (error) {
106
+ resultFiles.forEach(file => {
107
+ file.status = 'fail';
108
+ });
109
+ // 上传失败事件
110
+ opts.error(resultFiles, error);
111
+ resultFiles.forEach(file => {
112
+ file.error = error;
113
+ });
114
+ ibiz.log.error(error);
115
+ ibiz.log.error(`${files.map(file => file.name).join(',')}上传失败`);
116
+ }
117
+ return resultFiles;
118
+ };
119
+ /**
120
+ * 执行上传文件逻辑
121
+ *
122
+ * @author lxm
123
+ * @date 2022-11-18 13:11:21
124
+ * @param {files} File[] 文件集合
125
+ */
126
+ const uploadFiles = async (files) => {
127
+ const uploadSequence = opts.separate ? files.map(file => [file]) : [files];
128
+ const res = await Promise.allSettled(uploadSequence.map(async (sequence) => {
129
+ return executeSingleUpload(sequence);
130
+ }));
131
+ // 整合所有的返回文件
132
+ const resultFiles = [];
133
+ res.forEach(result => {
134
+ if (result.status === 'fulfilled') {
135
+ resultFiles.push(...result.value);
136
+ }
137
+ });
138
+ opts.finish(resultFiles);
139
+ };
140
+ const select = () => {
141
+ selectFile({
142
+ accept: opts.accept,
143
+ multiple: opts.multiple,
144
+ onSelected: files => {
145
+ uploadFiles(files);
146
+ },
147
+ });
148
+ };
149
+ select();
150
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ibiz-template/core",
3
- "version": "0.0.1-beta.1",
3
+ "version": "0.0.1-beta.17",
4
4
  "description": "核心包",
5
5
  "type": "module",
6
6
  "main": "out/index.js",
@@ -30,7 +30,7 @@
30
30
  "author": "chitanda",
31
31
  "license": "MIT",
32
32
  "dependencies": {
33
- "axios": "^1.1.3",
33
+ "axios": "^1.2.1",
34
34
  "loglevel": "^1.8.0",
35
35
  "pluralize": "^8.0.0",
36
36
  "qs": "^6.11.0"
@@ -45,5 +45,5 @@
45
45
  "lodash-es": "^4.17.21",
46
46
  "qx-util": "^0.4.4"
47
47
  },
48
- "gitHead": "9514cbc720208af6160c67bc43e822262274da1f"
48
+ "gitHead": "ec02483ab23e246c852c28e4973fd7b086416657"
49
49
  }
@@ -0,0 +1,135 @@
1
+ import {
2
+ ICommand,
3
+ ICommandHandler,
4
+ ICommandOption,
5
+ ICommandRegistry,
6
+ ICommandsMap,
7
+ IDisposable,
8
+ } from './interface';
9
+ import { LinkedList, toDisposable } from './utils';
10
+
11
+ /**
12
+ * 命令注册中心
13
+ *
14
+ * @author chitanda
15
+ * @date 2022-06-27 13:06:50
16
+ * @export
17
+ * @class CommandRegisterService
18
+ * @implements {ICommandRegistry}
19
+ */
20
+ export class CommandsRegistry implements ICommandRegistry {
21
+ /**
22
+ * 已经注册的所有指令
23
+ *
24
+ * @author chitanda
25
+ * @date 2022-07-21 15:07:47
26
+ * @private
27
+ */
28
+ private readonly commands = new Map<string, LinkedList<ICommand>>();
29
+
30
+ /**
31
+ * 注册指令
32
+ *
33
+ * @author chitanda
34
+ * @date 2022-06-27 13:06:31
35
+ * @param {(string | ICommand)} idOrCommand
36
+ * @param {ICommandHandler} [handler]
37
+ * @return {*} {IDisposable}
38
+ */
39
+ registerCommand(
40
+ idOrCommand: string | ICommand,
41
+ handler?: ICommandHandler,
42
+ opts?: ICommandOption,
43
+ ): IDisposable {
44
+ if (!idOrCommand) {
45
+ throw new Error(`invalid command`);
46
+ }
47
+
48
+ if (typeof idOrCommand === 'string') {
49
+ if (!handler) {
50
+ throw new Error(`invalid command`);
51
+ }
52
+ return this.registerCommand({ id: idOrCommand, handler, opts });
53
+ }
54
+
55
+ const { id } = idOrCommand;
56
+
57
+ let commands = this.commands.get(id);
58
+ if (!commands) {
59
+ commands = new LinkedList<ICommand>();
60
+ this.commands.set(id, commands);
61
+ }
62
+
63
+ const removeFn = commands.unshift(idOrCommand);
64
+
65
+ const ret = toDisposable(() => {
66
+ removeFn();
67
+ const command = this.commands.get(id);
68
+ if (command?.isEmpty()) {
69
+ this.commands.delete(id);
70
+ }
71
+ });
72
+
73
+ return ret;
74
+ }
75
+
76
+ /**
77
+ * 指令是否已经注册
78
+ *
79
+ * @author chitanda
80
+ * @date 2022-07-21 15:07:58
81
+ * @param {string} id
82
+ * @return {*} {boolean}
83
+ */
84
+ hasCommand(id: string): boolean {
85
+ return this.commands.has(id);
86
+ }
87
+
88
+ /**
89
+ * 查找指令
90
+ *
91
+ * @author chitanda
92
+ * @date 2022-07-21 16:07:12
93
+ * @param {string} id
94
+ * @return {*} {(ICommand | undefined)}
95
+ */
96
+ getCommand(id: string): ICommand | undefined {
97
+ const list = this.commands.get(id);
98
+ if (!list || list.isEmpty()) {
99
+ return undefined;
100
+ }
101
+ return list[Symbol.iterator]().next().value;
102
+ }
103
+
104
+ /**
105
+ * 获取所有指令
106
+ *
107
+ * @author chitanda
108
+ * @date 2022-07-21 16:07:20
109
+ * @return {*} {ICommandsMap}
110
+ */
111
+ getCommands(): ICommandsMap {
112
+ const result = new Map<string, ICommand>();
113
+ const keys = this.commands.keys();
114
+ for (const key of keys) {
115
+ const command = this.getCommand(key);
116
+ if (command) {
117
+ result.set(key, command);
118
+ }
119
+ }
120
+ return result;
121
+ }
122
+
123
+ /**
124
+ * 获取指令配置参数
125
+ *
126
+ * @author chitanda
127
+ * @date 2022-07-21 16:07:27
128
+ * @param {string} id
129
+ * @return {*} {(ICommandOption | undefined)}
130
+ */
131
+ getCommandOpt(id: string): ICommandOption | undefined {
132
+ const cmd = this.getCommand(id);
133
+ return cmd?.opts;
134
+ }
135
+ }
@@ -0,0 +1,79 @@
1
+ import { ICommandHandler, ICommandOption, IDisposable } from './interface';
2
+ import { CommandsRegistry } from './command-register';
3
+
4
+ /**
5
+ * 指令控制器
6
+ *
7
+ * @author chitanda
8
+ * @date 2022-06-28 19:06:51
9
+ * @export
10
+ * @class CommandController
11
+ */
12
+ export class CommandController {
13
+ private commandRegister = new CommandsRegistry();
14
+
15
+ /**
16
+ * 注册指令
17
+ *
18
+ * @author chitanda
19
+ * @date 2022-06-28 19:06:45
20
+ * @param {string} id
21
+ * @param {ICommandHandler} handler
22
+ * @param {ICommandOption} [opts]
23
+ * @return {*} {IDisposable}
24
+ */
25
+ register(
26
+ id: string,
27
+ handler: ICommandHandler,
28
+ opts?: ICommandOption,
29
+ ): IDisposable {
30
+ return this.commandRegister.registerCommand(id, handler, opts);
31
+ }
32
+
33
+ /**
34
+ * 执行指令
35
+ *
36
+ * @author chitanda
37
+ * @date 2022-06-28 19:06:38
38
+ * @template T
39
+ * @param {string} id
40
+ * @param {...unknown[]} args
41
+ * @return {*} {Promise<T>}
42
+ */
43
+ async execute<T = undefined>(id: string, ...args: unknown[]): Promise<T> {
44
+ const command = this.commandRegister.getCommand(id);
45
+ if (command) {
46
+ return command.handler(...args) as T | Promise<T>;
47
+ }
48
+ throw new Error(`未注册指令: ${id},请先注册指令`);
49
+ }
50
+
51
+ /**
52
+ * 判断指令是否存在,可直接抛出异常
53
+ *
54
+ * @author chitanda
55
+ * @date 2022-06-28 19:06:11
56
+ * @param {string} id
57
+ * @param {boolean} [err]
58
+ * @return {*} {boolean}
59
+ */
60
+ hasCommand(id: string, err?: boolean): boolean {
61
+ const bol = !!this.commandRegister.hasCommand(id);
62
+ if (err === true && bol === true) {
63
+ throw new Error(`未注册指令: ${id},请先注册指令`);
64
+ }
65
+ return bol;
66
+ }
67
+
68
+ /**
69
+ * 获取指令配置参数
70
+ *
71
+ * @author chitanda
72
+ * @date 2022-07-21 17:07:11
73
+ * @param {string} id
74
+ * @return {*} {(ICommandOption | undefined)}
75
+ */
76
+ getCommandOpts(id: string): ICommandOption | undefined {
77
+ return this.commandRegister.getCommandOpt(id);
78
+ }
79
+ }
@@ -0,0 +1,11 @@
1
+ import { CommandController } from './command';
2
+
3
+ export * from './interface';
4
+ export * from './utils';
5
+ export { CommandsRegistry } from './command-register';
6
+ export { CommandController } from './command';
7
+
8
+ /**
9
+ * 命令控制器
10
+ */
11
+ export const commands = new CommandController();
@@ -0,0 +1,22 @@
1
+ /**
2
+ * 指令参数, 主要用于在指令呈现时的展示参数
3
+ *
4
+ * @author chitanda
5
+ * @date 2022-06-28 17:06:47
6
+ * @export
7
+ * @interface ICommandOption
8
+ */
9
+ export interface ICommandOption {
10
+ readonly id: string;
11
+ readonly title: string;
12
+ readonly tooltip?: string;
13
+ readonly description?: string;
14
+ /**
15
+ * 只支持 svg 图标
16
+ *
17
+ * @author chitanda
18
+ * @date 2022-06-29 14:06:42
19
+ * @type {string}
20
+ */
21
+ readonly icon?: string;
22
+ }
@@ -0,0 +1,86 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { IDisposable } from '../disposable/disposable';
3
+ import { ICommandOption } from './command-option';
4
+
5
+ /**
6
+ * 注册指令
7
+ *
8
+ * @author chitanda
9
+ * @date 2022-06-27 13:06:25
10
+ * @export
11
+ * @interface ICommand
12
+ */
13
+ export interface ICommand {
14
+ id: string;
15
+ handler: ICommandHandler;
16
+ opts?: ICommandOption;
17
+ }
18
+
19
+ /**
20
+ * 触发指令声明
21
+ *
22
+ * @author chitanda
23
+ * @date 2022-06-28 17:06:10
24
+ * @export
25
+ * @interface Command
26
+ */
27
+ export interface Command {
28
+ /**
29
+ * Title of the command, like `save`.
30
+ */
31
+ title: string;
32
+
33
+ /**
34
+ * The identifier of the actual command handler.
35
+ * @see {@link commands.registerCommand}
36
+ */
37
+ command: string;
38
+
39
+ /**
40
+ * A tooltip for the command, when represented in the UI.
41
+ */
42
+ tooltip?: string;
43
+
44
+ /**
45
+ * Arguments that the command handler should be
46
+ * invoked with.
47
+ */
48
+ arguments?: any[];
49
+ }
50
+
51
+ /**
52
+ * 指令处理回调
53
+ *
54
+ * @author chitanda
55
+ * @date 2022-06-27 13:06:33
56
+ * @export
57
+ * @interface ICommandHandler
58
+ */
59
+ export interface ICommandHandler {
60
+ (...args: any[]): unknown | Promise<unknown>;
61
+ }
62
+
63
+ /**
64
+ * 指令控制器集
65
+ */
66
+ export type ICommandsMap = Map<string, ICommand>;
67
+
68
+ /**
69
+ * 注册服务
70
+ *
71
+ * @author chitanda
72
+ * @date 2022-06-27 13:06:16
73
+ * @export
74
+ * @interface ICommandRegistry
75
+ */
76
+ export interface ICommandRegistry {
77
+ registerCommand(
78
+ id: string,
79
+ command: ICommandHandler,
80
+ opts: ICommandOption,
81
+ ): IDisposable;
82
+ registerCommand(id: string, command: ICommandHandler): IDisposable;
83
+ registerCommand(command: ICommand): IDisposable;
84
+ getCommand(id: string): ICommand | undefined;
85
+ getCommands(): ICommandsMap;
86
+ }
@@ -0,0 +1,3 @@
1
+ export interface IDisposable {
2
+ dispose(): void;
3
+ }
@@ -0,0 +1,9 @@
1
+ export {
2
+ ICommand,
3
+ Command,
4
+ ICommandHandler,
5
+ ICommandsMap,
6
+ ICommandRegistry,
7
+ } from './command/command';
8
+ export { ICommandOption } from './command/command-option';
9
+ export { IDisposable } from './disposable/disposable';
@@ -0,0 +1,2 @@
1
+ export { LinkedList } from './linked-list';
2
+ export { toDisposable, debounce, throttle } from './util';