@widget-js/core 0.1.31 → 0.5.12

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 (71) hide show
  1. package/dist/cjs/api/AppApi.js +24 -21
  2. package/dist/cjs/api/BaseApi.js +13 -0
  3. package/dist/cjs/api/BroadcastApi.js +14 -13
  4. package/dist/cjs/api/BrowserWindowApi.js +55 -72
  5. package/dist/cjs/api/Channel.js +1 -1
  6. package/dist/cjs/api/ClipboardApi.js +16 -11
  7. package/dist/cjs/api/DeviceApi.js +12 -9
  8. package/dist/cjs/api/DialogApi.js +8 -6
  9. package/dist/cjs/api/HostedWidgetApi.js +27 -36
  10. package/dist/cjs/api/LogApi.js +14 -10
  11. package/dist/cjs/api/NotificationApi.js +52 -74
  12. package/dist/cjs/api/ShortcutApi.js +23 -0
  13. package/dist/cjs/api/WidgetApi.js +29 -69
  14. package/dist/cjs/{repository/WidgetDataRepository.js → api/WidgetDataApi.js} +19 -10
  15. package/dist/cjs/index.js +3 -2
  16. package/dist/cjs/lang/LanguageCode.js +34 -0
  17. package/dist/cjs/model/Widget.js +4 -4
  18. package/dist/cjs/model/WidgetPackage.js +49 -2
  19. package/dist/cjs/model/event/BroadcastEvent.js +4 -8
  20. package/dist/cjs/model/msic/graphics.js +2 -0
  21. package/dist/cjs/utils/ElectronUtils.js +5 -1
  22. package/dist/cjs/utils/LanguageUtils.js +36 -0
  23. package/dist/esm/api/AppApi.js +23 -20
  24. package/dist/esm/api/BaseApi.js +9 -0
  25. package/dist/esm/api/BroadcastApi.js +13 -12
  26. package/dist/esm/api/BrowserWindowApi.js +53 -71
  27. package/dist/esm/api/Channel.js +1 -1
  28. package/dist/esm/api/ClipboardApi.js +15 -10
  29. package/dist/esm/api/DeviceApi.js +11 -8
  30. package/dist/esm/api/DialogApi.js +9 -6
  31. package/dist/esm/api/HostedWidgetApi.js +26 -35
  32. package/dist/esm/api/LogApi.js +15 -10
  33. package/dist/esm/api/NotificationApi.js +51 -73
  34. package/dist/esm/api/ShortcutApi.js +19 -0
  35. package/dist/esm/api/WidgetApi.js +28 -65
  36. package/dist/esm/{repository/WidgetDataRepository.js → api/WidgetDataApi.js} +17 -8
  37. package/dist/esm/index.js +3 -2
  38. package/dist/esm/lang/LanguageCode.js +33 -0
  39. package/dist/esm/model/Widget.js +4 -4
  40. package/dist/esm/model/WidgetPackage.js +49 -2
  41. package/dist/esm/model/event/BroadcastEvent.js +4 -8
  42. package/dist/esm/model/msic/graphics.js +1 -0
  43. package/dist/esm/utils/ElectronUtils.js +5 -1
  44. package/dist/esm/utils/LanguageUtils.js +32 -0
  45. package/dist/types/api/AppApi.d.ts +23 -13
  46. package/dist/types/api/BaseApi.d.ts +5 -0
  47. package/dist/types/api/BroadcastApi.d.ts +19 -4
  48. package/dist/types/api/BrowserWindowApi.d.ts +56 -52
  49. package/dist/types/api/Channel.d.ts +1 -1
  50. package/dist/types/api/ClipboardApi.d.ts +9 -5
  51. package/dist/types/api/DeviceApi.d.ts +13 -9
  52. package/dist/types/api/DialogApi.d.ts +5 -7
  53. package/dist/types/api/HostedWidgetApi.d.ts +28 -18
  54. package/dist/types/api/LogApi.d.ts +8 -5
  55. package/dist/types/api/NotificationApi.d.ts +26 -16
  56. package/dist/types/api/ShortcutApi.d.ts +10 -0
  57. package/dist/types/api/WidgetApi.d.ts +31 -33
  58. package/dist/types/{repository/WidgetDataRepository.d.ts → api/WidgetDataApi.d.ts} +7 -2
  59. package/dist/types/index.d.ts +3 -2
  60. package/dist/types/lang/LanguageCode.d.ts +40 -0
  61. package/dist/types/model/Widget.d.ts +7 -9
  62. package/dist/types/model/WidgetPackage.d.ts +55 -12
  63. package/dist/types/model/event/BroadcastEvent.d.ts +14 -10
  64. package/dist/types/model/msic/graphics.d.ts +8 -0
  65. package/dist/types/utils/ElectronUtils.d.ts +2 -1
  66. package/dist/types/utils/LanguageUtils.d.ts +8 -0
  67. package/dist/umd/index.js +1 -1
  68. package/package.json +4 -2
  69. package/dist/cjs/api/GlobalShortcutApi.js +0 -29
  70. package/dist/esm/api/GlobalShortcutApi.js +0 -25
  71. package/dist/types/api/GlobalShortcutApi.d.ts +0 -14
@@ -1,12 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotificationApi = void 0;
3
+ exports.NotificationApiEvent = exports.NotificationApi = void 0;
4
4
  const AppNotification_1 = require("../model/AppNotification");
5
5
  const Channel_1 = require("./Channel");
6
- const ElectronUtils_1 = require("../utils/ElectronUtils");
7
- class NotificationApi {
6
+ const BaseApi_1 = require("./BaseApi");
7
+ var NotificationApiEvent;
8
+ (function (NotificationApiEvent) {
9
+ NotificationApiEvent["CONFIRM"] = "event::cn.widgetjs.core.notification.confirm";
10
+ NotificationApiEvent["CANCEL"] = "event::cn.widgetjs.core.notification.cancel";
11
+ NotificationApiEvent["HIDE"] = "event::cn.widgetjs.core.notification.hide";
12
+ })(NotificationApiEvent || (NotificationApiEvent = {}));
13
+ exports.NotificationApiEvent = NotificationApiEvent;
14
+ class NotificationApiImpl extends BaseApi_1.BaseApi {
8
15
  // static async url(url: string, duration: number = -1) {
9
- // ElectronUtils.getAPI()?.invoke(Channel.NOTIFICATION, new AppNotification({
16
+ // this.invoke(Channel.NOTIFICATION, new AppNotification({
10
17
  // url, message: "",
11
18
  // duration,
12
19
  // type: "url",
@@ -20,9 +27,8 @@ class NotificationApi {
20
27
  * @param message 初始消息
21
28
  * @param lyric 歌词字符串
22
29
  */
23
- static async call(avatar, audio, title, message, lyric) {
24
- var _a;
25
- (_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.NOTIFICATION, new AppNotification_1.AppNotification({
30
+ async call(avatar, audio, title, message, lyric) {
31
+ await this.invoke(new AppNotification_1.AppNotification({
26
32
  avatar,
27
33
  audio,
28
34
  message,
@@ -32,11 +38,10 @@ class NotificationApi {
32
38
  type: "call",
33
39
  }));
34
40
  }
35
- static async send(notification) {
36
- var _a;
37
- return (_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.NOTIFICATION, notification);
41
+ async send(notification) {
42
+ return this.invokeMethod('send', notification);
38
43
  }
39
- static async reminder(title, message, icon, cancelButtonText, confirmButtonText, cancelBroadcast, confirmBroadcast, duration = 5000) {
44
+ async reminder(title, message, icon, cancelButtonText, confirmButtonText, cancelBroadcast, confirmBroadcast, duration = 5000) {
40
45
  return await this.send(new AppNotification_1.AppNotification({
41
46
  icon,
42
47
  message,
@@ -49,7 +54,7 @@ class NotificationApi {
49
54
  type: "reminder",
50
55
  }));
51
56
  }
52
- static async advanceCountdown(message, targetTime, title) {
57
+ async advanceCountdown(message, targetTime, title) {
53
58
  return await this.send(new AppNotification_1.AppNotification({
54
59
  title,
55
60
  message,
@@ -57,9 +62,8 @@ class NotificationApi {
57
62
  type: "advance-countdown"
58
63
  }));
59
64
  }
60
- static async countdown(message, targetTime) {
61
- var _a;
62
- (_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.NOTIFICATION, new AppNotification_1.AppNotification({
65
+ async countdown(message, targetTime) {
66
+ await this.send(new AppNotification_1.AppNotification({
63
67
  message,
64
68
  targetTime,
65
69
  backgroundColor: 'rgba(0,0,0,0.5)',
@@ -67,73 +71,47 @@ class NotificationApi {
67
71
  type: "countdown"
68
72
  }));
69
73
  }
70
- static async success(message, duration = 5000) {
71
- var _a;
72
- if (ElectronUtils_1.ElectronUtils.hasElectronApi()) {
73
- (_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.NOTIFICATION, new AppNotification_1.AppNotification({
74
- message,
75
- type: "success",
76
- icon: "check_circle_line",
77
- duration
78
- }));
79
- }
80
- else {
81
- this.callback("success", message, duration);
82
- }
74
+ async success(message, duration = 5000) {
75
+ await this.send(new AppNotification_1.AppNotification({
76
+ message,
77
+ type: "success",
78
+ icon: "check_circle_line",
79
+ duration
80
+ }));
83
81
  }
84
- static async error(message, duration = 5000) {
85
- var _a;
86
- if (ElectronUtils_1.ElectronUtils.hasElectronApi()) {
87
- (_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.NOTIFICATION, new AppNotification_1.AppNotification({
88
- message,
89
- type: "error",
90
- icon: "close_circle_line",
91
- duration
92
- }));
93
- }
94
- else {
95
- this.callback("error", message, duration);
96
- }
82
+ async error(message, duration = 5000) {
83
+ await this.send(new AppNotification_1.AppNotification({
84
+ message,
85
+ type: "error",
86
+ icon: "close_circle_line",
87
+ duration
88
+ }));
97
89
  }
98
- static async warning(message, duration = 5000) {
99
- var _a;
100
- if (ElectronUtils_1.ElectronUtils.hasElectronApi()) {
101
- (_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.NOTIFICATION, new AppNotification_1.AppNotification({
102
- message,
103
- type: "warning",
104
- icon: "warning_line",
105
- duration
106
- }));
107
- }
108
- else {
109
- this.callback("warning", message, duration);
110
- }
90
+ async warning(message, duration = 5000) {
91
+ await this.send(new AppNotification_1.AppNotification({
92
+ message,
93
+ type: "warning",
94
+ icon: "warning_line",
95
+ duration
96
+ }));
111
97
  }
112
- static async info(message, duration = 5000) {
113
- var _a;
114
- if (ElectronUtils_1.ElectronUtils.hasElectronApi()) {
115
- (_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.NOTIFICATION, new AppNotification_1.AppNotification({
116
- message,
117
- type: "info",
118
- icon: "information_line",
119
- duration
120
- }));
121
- }
122
- else {
123
- this.callback("info", message, duration);
124
- }
98
+ async info(message, duration = 5000) {
99
+ await this.send(new AppNotification_1.AppNotification({
100
+ message,
101
+ type: "info",
102
+ icon: "information_line",
103
+ duration
104
+ }));
125
105
  }
126
106
  /**
127
107
  * 隐藏通知
128
108
  */
129
- static async hide() {
130
- var _a;
131
- if (ElectronUtils_1.ElectronUtils.hasElectronApi()) {
132
- (_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.NOTIFICATION, null);
133
- }
109
+ async hide() {
110
+ await this.invokeMethod('hide');
134
111
  }
135
- static setDebugNotification(callback) {
136
- this.callback = callback;
112
+ getChannel() {
113
+ return Channel_1.Channel.NOTIFICATION;
137
114
  }
138
115
  }
116
+ const NotificationApi = new NotificationApiImpl();
139
117
  exports.NotificationApi = NotificationApi;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ShortcutApiEvent = exports.ShortcutApi = void 0;
4
+ const BaseApi_1 = require("./BaseApi");
5
+ const Channel_1 = require("./Channel");
6
+ var ShortcutApiEvent;
7
+ (function (ShortcutApiEvent) {
8
+ ShortcutApiEvent["TRIGGERED"] = "channel::cn.widgetjs.core.shortcut.triggered";
9
+ })(ShortcutApiEvent || (ShortcutApiEvent = {}));
10
+ exports.ShortcutApiEvent = ShortcutApiEvent;
11
+ class ShortcutApiImpl extends BaseApi_1.BaseApi {
12
+ getChannel() {
13
+ return Channel_1.Channel.SHORTCUT;
14
+ }
15
+ async register(shortcut) {
16
+ return await this.invokeMethod('register', shortcut);
17
+ }
18
+ async unregister(shortcut) {
19
+ return await this.invokeMethod('unregister', shortcut);
20
+ }
21
+ }
22
+ const ShortcutApi = new ShortcutApiImpl();
23
+ exports.ShortcutApi = ShortcutApi;
@@ -1,29 +1,29 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WidgetApi = void 0;
3
+ exports.WidgetApiEvent = exports.WidgetApi = void 0;
7
4
  const Widget_1 = require("../model/Widget");
8
- const ElectronUtils_1 = require("../utils/ElectronUtils");
9
5
  const WidgetPackage_1 = require("../model/WidgetPackage");
10
6
  const Channel_1 = require("./Channel");
11
7
  const UrlUtils_1 = require("../utils/UrlUtils");
12
- const BroadcastEvent_1 = require("../model/event/BroadcastEvent");
13
- const BroadcastApi_1 = require("./BroadcastApi");
14
- const localforage_1 = __importDefault(require("localforage"));
15
- class WidgetApi {
16
- static async registerWidgets(widgets) {
17
- var _a;
18
- await ((_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.WIDGET, this.REGISTER_WIDGETS, JSON.stringify(widgets)));
8
+ const BaseApi_1 = require("./BaseApi");
9
+ var WidgetApiEvent;
10
+ (function (WidgetApiEvent) {
11
+ WidgetApiEvent["DATA_CHANGED"] = "event::cn.widgetjs.core.widget.data-changed";
12
+ WidgetApiEvent["EDIT_DESKTOP_WIDGETS"] = "event::cn.widgetjs.core.widget.desktop.edit";
13
+ })(WidgetApiEvent || (WidgetApiEvent = {}));
14
+ exports.WidgetApiEvent = WidgetApiEvent;
15
+ class WidgetApiImpl extends BaseApi_1.BaseApi {
16
+ getChannel() {
17
+ return Channel_1.Channel.WIDGET;
19
18
  }
20
- static async registerWidgetPackage(widgetPackage) {
21
- var _a;
22
- await ((_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.WIDGET, this.REGISTER_WIDGET_PACKAGE, JSON.stringify(widgetPackage)));
19
+ async registerWidgets(widgets) {
20
+ return this.invokeMethod("registerWidgets", JSON.stringify(widgets));
23
21
  }
24
- static async getWidgets() {
25
- var _a;
26
- const data = await ((_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.WIDGET, this.GET_WIDGETS));
22
+ async registerWidgetPackage(widgetPackage) {
23
+ return this.invokeMethod("registerWidgetPackage", JSON.stringify(widgetPackage));
24
+ }
25
+ async getWidgets() {
26
+ const data = await this.invokeMethod('getWidgets');
27
27
  const widgets = [];
28
28
  if (data) {
29
29
  for (const item of data) {
@@ -32,31 +32,28 @@ class WidgetApi {
32
32
  }
33
33
  return widgets;
34
34
  }
35
- static async getWidgetPackages() {
36
- var _a;
37
- return await ((_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.WIDGET, this.GET_WIDGET_PACKAGES));
35
+ async getWidgetPackages() {
36
+ return await this.invokeMethod('getWidgetPackages');
38
37
  }
39
38
  /**
40
39
  *
41
40
  * @param name package name
42
41
  */
43
- static async getWidget(name) {
44
- var _a;
45
- return Widget_1.Widget.parseObject(await ((_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.WIDGET, this.GET_WIDGET, name)));
42
+ async getWidget(name) {
43
+ return Widget_1.Widget.parseObject(await this.invokeMethod('getWidget', name));
46
44
  }
47
45
  /**
48
46
  *
49
47
  * @param name package name
50
48
  */
51
- static async getWidgetPackage(name) {
52
- var _a;
53
- return WidgetPackage_1.WidgetPackage.parseObject(await ((_a = ElectronUtils_1.ElectronUtils.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(Channel_1.Channel.WIDGET, this.GET_WIDGET_PACKAGE, name)));
49
+ async getWidgetPackage(name) {
50
+ return WidgetPackage_1.WidgetPackage.parseObject(await this.invokeMethod('getWidgetPackage', name));
54
51
  }
55
52
  /**
56
53
  * 获取组件配置地址
57
54
  * @param widgetName
58
55
  */
59
- static async getWidgetConfigUrl(widgetName, widgetParams) {
56
+ async getWidgetConfigUrl(widgetName, widgetParams) {
60
57
  const widget = await this.getWidget(widgetName);
61
58
  if (!widget)
62
59
  return null;
@@ -68,7 +65,7 @@ class WidgetApi {
68
65
  return null;
69
66
  return UrlUtils_1.UrlUtils.getWidgetUrl(configRoute.url, widgetPackage, widgetParams);
70
67
  }
71
- static async getWidgetUrl(widgetName, widgetParams) {
68
+ async getWidgetUrl(widgetName, widgetParams) {
72
69
  const widget = await this.getWidget(widgetName);
73
70
  if (!widget)
74
71
  return null;
@@ -78,55 +75,18 @@ class WidgetApi {
78
75
  return null;
79
76
  return UrlUtils_1.UrlUtils.getWidgetUrl(indexRoute.url, widgetPackage, widgetParams);
80
77
  }
81
- static async getWidgetPackageIndexUrl(packageName, hash) {
78
+ async getWidgetPackageIndexUrl(packageName, hash) {
82
79
  const widgetPackage = await this.getWidgetPackage(packageName);
83
80
  if (!widgetPackage)
84
81
  return null;
85
82
  return widgetPackage.getIndexUrl(hash);
86
83
  }
87
- static async getWidgetPackageUrl(packageName) {
84
+ async getWidgetPackageUrl(packageName) {
88
85
  const widgetPackage = await this.getWidgetPackage(packageName);
89
86
  if (!widgetPackage)
90
87
  return null;
91
88
  return widgetPackage.url;
92
89
  }
93
- /**
94
- * 通过组件名保存组件信息,通常用于存储可以在同类组件中共用的数据
95
- * @param data
96
- * @param options
97
- */
98
- static async saveDataByName(data, options = { sendBroadcast: true }) {
99
- const store = this.getStore(data.name);
100
- const json = JSON.stringify(data);
101
- const result = await store.setItem(data.name, json);
102
- if (options.sendBroadcast) {
103
- const broadcastEvent = new BroadcastEvent_1.BroadcastEvent(BroadcastEvent_1.BroadcastEvent.TYPE_WIDGET_UPDATED, "", {
104
- name: data.name,
105
- id: options.id,
106
- json
107
- });
108
- await BroadcastApi_1.BroadcastApi.sendBroadcastEvent(broadcastEvent);
109
- }
110
- return result;
111
- }
112
- /**
113
- * 获取组件 LocalForage 存储实例
114
- * @param name
115
- */
116
- static getStore(name) {
117
- if (this.stores.has(name)) {
118
- return this.stores.get(name);
119
- }
120
- const store = localforage_1.default.createInstance({ name: name });
121
- this.stores.set(name, store);
122
- return store;
123
- }
124
90
  }
91
+ const WidgetApi = new WidgetApiImpl();
125
92
  exports.WidgetApi = WidgetApi;
126
- WidgetApi.REGISTER_WIDGETS = "register-widgets";
127
- WidgetApi.REGISTER_WIDGET_PACKAGE = "register-widget-package";
128
- WidgetApi.GET_WIDGETS = "get-widgets";
129
- WidgetApi.GET_WIDGET = "get-widget";
130
- WidgetApi.GET_WIDGET_PACKAGE = "get-widget-package";
131
- WidgetApi.GET_WIDGET_PACKAGES = "get-widget-packages";
132
- WidgetApi.stores = new Map();
@@ -3,20 +3,24 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WidgetDataRepository = void 0;
6
+ exports.WidgetDataApi = void 0;
7
7
  const localforage_1 = __importDefault(require("localforage"));
8
8
  const BroadcastEvent_1 = require("../model/event/BroadcastEvent");
9
9
  const BroadcastApi_1 = require("../api/BroadcastApi");
10
- class WidgetDataRepository {
10
+ const WidgetApi_1 = require("../api/WidgetApi");
11
+ class WidgetDataApi {
11
12
  /**
12
13
  * 保存组件数据
13
14
  * @param data
14
15
  */
15
16
  static async save(data) {
16
17
  let store = this.getStore(data.name);
17
- const result = await store.setItem(this.getKey(data.name, data.id), JSON.stringify(data));
18
- const broadcastEvent = new BroadcastEvent_1.BroadcastEvent(BroadcastEvent_1.BroadcastEvent.TYPE_WIDGET_UPDATED, "", data);
19
- await BroadcastApi_1.BroadcastApi.sendBroadcastEvent(broadcastEvent);
18
+ let json = JSON.stringify(data);
19
+ const result = await store.setItem(this.getKey(data.name, data.id), json);
20
+ const broadcastEvent = new BroadcastEvent_1.BroadcastEvent({
21
+ event: WidgetApi_1.WidgetApiEvent.DATA_CHANGED, payload: { name: data.name, json }
22
+ });
23
+ await BroadcastApi_1.BroadcastApi.send(broadcastEvent);
20
24
  return result;
21
25
  }
22
26
  /**
@@ -34,13 +38,18 @@ class WidgetDataRepository {
34
38
  /**
35
39
  * 通过组件名保存组件信息,通常用于存储可以在同类组件中共用的数据
36
40
  * @param data
41
+ * @param options
37
42
  */
38
- static async saveByName(data) {
43
+ static async saveByName(data, options = { sendBroadcast: true }) {
39
44
  const store = this.getStore(data.name);
40
45
  const json = JSON.stringify(data);
41
46
  const result = await store.setItem(data.name, json);
42
- const broadcastEvent = new BroadcastEvent_1.BroadcastEvent(BroadcastEvent_1.BroadcastEvent.TYPE_WIDGET_UPDATED, "", { name: data.name, json });
43
- await BroadcastApi_1.BroadcastApi.sendBroadcastEvent(broadcastEvent);
47
+ if (options === null || options === void 0 ? void 0 : options.sendBroadcast) {
48
+ const broadcastEvent = new BroadcastEvent_1.BroadcastEvent({
49
+ event: WidgetApi_1.WidgetApiEvent.DATA_CHANGED, payload: { name: data.name, json }
50
+ });
51
+ await BroadcastApi_1.BroadcastApi.send(broadcastEvent);
52
+ }
44
53
  return result;
45
54
  }
46
55
  static async findByName(name, type) {
@@ -67,5 +76,5 @@ class WidgetDataRepository {
67
76
  return `${name}@${id}`;
68
77
  }
69
78
  }
70
- exports.WidgetDataRepository = WidgetDataRepository;
71
- WidgetDataRepository.stores = new Map();
79
+ exports.WidgetDataApi = WidgetDataApi;
80
+ WidgetDataApi.stores = new Map();
package/dist/cjs/index.js CHANGED
@@ -27,9 +27,9 @@ __exportStar(require("./model/event/NativeKeyboardEvent"), exports);
27
27
  __exportStar(require("./model/event/AppMouseEvent"), exports);
28
28
  __exportStar(require("./model/WidgetPackage"), exports);
29
29
  __exportStar(require("./api/ElectronApi"), exports);
30
- __exportStar(require("./repository/WidgetDataRepository"), exports);
30
+ __exportStar(require("./api/WidgetDataApi"), exports);
31
31
  __exportStar(require("./api/BrowserWindowApi"), exports);
32
- __exportStar(require("./api/GlobalShortcutApi"), exports);
32
+ __exportStar(require("./lang/LanguageCode"), exports);
33
33
  __exportStar(require("./api/NotificationApi"), exports);
34
34
  __exportStar(require("./api/HostedWidgetApi"), exports);
35
35
  __exportStar(require("./api/DeviceApi"), exports);
@@ -37,6 +37,7 @@ __exportStar(require("./api/Channel"), exports);
37
37
  __exportStar(require("./api/ClipboardApi"), exports);
38
38
  __exportStar(require("./api/WidgetApi"), exports);
39
39
  __exportStar(require("./api/ApiConstants"), exports);
40
+ __exportStar(require("./api/ShortcutApi"), exports);
40
41
  __exportStar(require("./api/BroadcastApi"), exports);
41
42
  __exportStar(require("./api/AppApi"), exports);
42
43
  __exportStar(require("./api/DialogApi"), exports);
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class LanguageMapImpl {
4
+ constructor() {
5
+ this['ar-SA'] = { locale: 'ar-SA', language: 'العربية', code: 'ar' };
6
+ this['bn-BD'] = { locale: 'bn-BD', language: 'বাংলা', code: 'bn' };
7
+ this['en-US'] = { locale: 'en-US', language: 'English', code: 'en' };
8
+ this['de-DE'] = { locale: 'de-DE', language: 'Deutsch', code: 'de' };
9
+ this['el-GR'] = { locale: 'el-GR', language: 'Ελληνικά', code: 'el' };
10
+ this['es-ES'] = { locale: 'es-ES', language: 'Español', code: 'es-ES' };
11
+ this['fi-FI'] = { locale: 'fi-FI', language: 'Suomalainen', code: 'fi' };
12
+ this['fil-PH'] = { locale: 'fil-PH', language: 'Filipino', code: 'fil' };
13
+ this['fr-FR'] = { locale: 'fr-FR', language: 'Français', code: 'fr' };
14
+ this['hi-IN'] = { locale: 'hi-IN', language: 'हिंदी', code: 'hi' };
15
+ this['hu-HU'] = { locale: 'hu-HU', language: 'Magyar', code: 'hu' };
16
+ this['id-ID'] = { locale: 'id-ID', language: 'Bahasa Indonesia', code: 'id' };
17
+ this['it-IT'] = { locale: 'it-IT', language: 'Italiano', code: 'it' };
18
+ this['ja-JP'] = { locale: 'ja-JP', language: '日本語', code: 'ja' };
19
+ this['ko-KR'] = { locale: 'ko-KR', language: '한국어', code: 'ko' };
20
+ this['nl-NL'] = { locale: 'nl-NL', language: 'Nederlands', code: 'nl' };
21
+ this['pl-PL'] = { locale: 'pl-PL', language: 'Polski', code: 'pl' };
22
+ this['pt-BR'] = { locale: 'pt-BR', language: 'Português (Brazil)', code: 'pt-br' };
23
+ this['pt-PT'] = { locale: 'pt-PT', language: 'Português', code: 'pt-pt' };
24
+ this['ro-RO'] = { locale: 'ro-RO', language: 'Română', code: 'ro' };
25
+ this['ru-RU'] = { locale: 'ru-RU', language: 'Русский', code: 'ru' };
26
+ this['sv-SE'] = { locale: 'sv-SE', language: 'Svenska', code: 'sv' };
27
+ this['ta-IN'] = { locale: 'ta-IN', language: 'தமிழ்', code: 'ta' };
28
+ this['tr-TR'] = { locale: 'tr-TR', language: 'Türkçe', code: 'tr' };
29
+ this['uk-UA'] = { locale: 'uk-UA', language: 'Українська', code: 'uk' };
30
+ this['vi-VN'] = { locale: 'vi-VN', language: 'Tiếng Việt', code: 'vi' };
31
+ this['zh-CN'] = { locale: 'zh-CN', language: '简体中文', code: 'zh-cn' };
32
+ this['zh-TW'] = { locale: 'zh-TW', language: '繁體中文', code: 'zh-tw' };
33
+ }
34
+ }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WidgetKeyword = exports.Widget = void 0;
4
4
  const HostedMode_1 = require("./HostedMode");
5
+ const LanguageUtils_1 = require("../utils/LanguageUtils");
5
6
  class Widget {
6
7
  constructor(options) {
7
8
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
@@ -46,15 +47,14 @@ class Widget {
46
47
  * @param lang 语言环境,不传则获取默认语言
47
48
  */
48
49
  getTitle(lang) {
49
- var _a;
50
- return lang ? (_a = this.title[lang]) !== null && _a !== void 0 ? _a : this.title[this.lang] : this.title[this.lang];
50
+ return (0, LanguageUtils_1.getTextByLanguageCode)(this.title, lang);
51
51
  }
52
52
  /**
53
- * 获取组件标描述
53
+ * 获取组件描述
54
54
  * @param lang 语言环境,不传则获取默认标题
55
55
  */
56
56
  getDescription(lang) {
57
- return lang ? this.description[lang] : this.description[this.lang];
57
+ return (0, LanguageUtils_1.getTextByLanguageCode)(this.description, lang);
58
58
  }
59
59
  static parseJSON(json) {
60
60
  const object = JSON.parse(json);
@@ -2,22 +2,69 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WidgetPackage = void 0;
4
4
  const UrlUtils_1 = require("../utils/UrlUtils");
5
+ const LanguageUtils_1 = require("../utils/LanguageUtils");
5
6
  class WidgetPackage {
6
- constructor() {
7
+ constructor(options) {
7
8
  /**
8
9
  * Hash路由模式,默认为true
9
10
  */
10
11
  this.hash = true;
12
+ this.widgets = [];
13
+ this.name = options.name;
14
+ this.version = options.version;
15
+ this.author = options.author;
16
+ this.homepage = options.homepage;
17
+ this.title = options.title;
18
+ this.description = options.description;
19
+ this.entry = options.entry;
20
+ this.remoteEntry = options.remoteEntry;
21
+ this.remotePackage = options.remotePackage;
22
+ this.hash = options.hash;
23
+ this.url = options.url;
24
+ this.devOptions = options.devOptions;
25
+ if (options.widgets) {
26
+ for (let widget of options.widgets) {
27
+ this.widgets.push(widget);
28
+ }
29
+ }
11
30
  }
12
31
  static parseJSON(json) {
13
32
  const object = JSON.parse(json);
14
33
  return this.parseObject(object);
15
34
  }
16
35
  static parseObject(obj) {
17
- let widgetPackage = new WidgetPackage();
36
+ let widgetPackage = new WidgetPackage({
37
+ author: "",
38
+ description: {
39
+ "zh-CN": "",
40
+ },
41
+ entry: "",
42
+ hash: false,
43
+ homepage: "",
44
+ name: "",
45
+ title: {
46
+ "zh-CN": "",
47
+ },
48
+ url: "",
49
+ version: ""
50
+ });
18
51
  Object.assign(widgetPackage, obj);
19
52
  return widgetPackage;
20
53
  }
54
+ /**
55
+ * 获取组件包标题
56
+ * @param lang 语言环境,不传则获取默认语言
57
+ */
58
+ getTitle(lang) {
59
+ return (0, LanguageUtils_1.getTextByLanguageCode)(this.title, lang);
60
+ }
61
+ /**
62
+ * 获取组件包描述
63
+ * @param lang 语言环境,不传则获取默认标题
64
+ */
65
+ getDescription(lang) {
66
+ return (0, LanguageUtils_1.getTextByLanguageCode)(this.description, lang);
67
+ }
21
68
  /**
22
69
  * 获取组件完整路径
23
70
  * 如果url是http链接,直接返回链接
@@ -2,14 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BroadcastEvent = void 0;
4
4
  class BroadcastEvent {
5
- constructor(type, from, payload) {
6
- this.type = type;
7
- this.from = from;
8
- this.payload = payload;
5
+ constructor(options) {
6
+ this.event = options.event;
7
+ this.sender = options.sender;
8
+ this.payload = options.payload;
9
9
  }
10
10
  }
11
11
  exports.BroadcastEvent = BroadcastEvent;
12
- BroadcastEvent.TYPE_WIDGET_UPDATED = "broadcast::cn.widgetjs.core.widget_updated";
13
- BroadcastEvent.TYPE_APP_CONFIG_UPDATED = "broadcast::cn.widgetjs.core.app_config_updated";
14
- BroadcastEvent.TYPE_THEME_CHANGED = "broadcast::cn.widgetjs.core.theme_changed";
15
- BroadcastEvent.TYPE_EDIT_DESKTOP_WIDGETS = "broadcast::cn.widgetjs.core.edit_desktop_widgets";
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -20,9 +20,13 @@ class ElectronUtils {
20
20
  }
21
21
  return null;
22
22
  }
23
- static async invoke(channel, method, ...args) {
23
+ static async invokeMethod(channel, method, ...args) {
24
24
  var _a;
25
25
  return (_a = this.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(channel, method, ...args);
26
26
  }
27
+ static async invoke(channel, ...args) {
28
+ var _a;
29
+ return (_a = this.getAPI()) === null || _a === void 0 ? void 0 : _a.invoke(channel, ...args);
30
+ }
27
31
  }
28
32
  exports.ElectronUtils = ElectronUtils;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTextByLanguageCode = void 0;
4
+ /**
5
+ * 根据语言环境获取文本
6
+ * @param langMap 对象
7
+ * @param {LanguageCode} langCode 语言环境 例如:zh-CN,en-US
8
+ * @param fallbackToBrowser
9
+ */
10
+ function getTextByLanguageCode(langMap, langCode, fallbackToBrowser = true) {
11
+ let result;
12
+ if (langCode) {
13
+ if (langCode in langMap) {
14
+ return langMap[langCode];
15
+ }
16
+ }
17
+ // fallback to browser default language
18
+ if (result == undefined && fallbackToBrowser) {
19
+ if (typeof navigator != 'undefined' && navigator.languages) {
20
+ const langCode = navigator.language;
21
+ result = getTextByLanguageCode(langMap, langCode, false);
22
+ if (result != undefined) {
23
+ return result;
24
+ }
25
+ }
26
+ }
27
+ // fallback to first language
28
+ if (result == undefined) {
29
+ let langCode = Object.keys(langMap);
30
+ if (langCode.length > 0) {
31
+ return langMap[langCode[0]];
32
+ }
33
+ }
34
+ return undefined;
35
+ }
36
+ exports.getTextByLanguageCode = getTextByLanguageCode;