@widget-js/core 0.0.7 → 0.0.9

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.
@@ -8,183 +8,86 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
11
  Object.defineProperty(exports, "__esModule", { value: true });
39
12
  exports.ElectronApi = void 0;
40
- var Keys_1 = require("./Keys");
41
- var ElectronApi = /** @class */ (function () {
42
- function ElectronApi() {
43
- }
44
- ElectronApi.openAddWidgetWindow = function () {
13
+ const Keys_1 = require("./Keys");
14
+ class ElectronApi {
15
+ static openAddWidgetWindow() {
45
16
  if (this.hasElectronApi()) {
46
17
  // @ts-ignore
47
18
  window.electronAPI.invokeIpc("openAddWidgetWindow");
48
19
  }
49
- };
50
- ElectronApi.registerWidgets = function (widgets) {
51
- return __awaiter(this, void 0, void 0, function () {
52
- var data;
53
- return __generator(this, function (_a) {
54
- switch (_a.label) {
55
- case 0:
56
- if (!this.hasElectronApi()) return [3 /*break*/, 2];
57
- data = JSON.parse(JSON.stringify(widgets.map(function (item) { return JSON.stringify(item); })));
58
- // @ts-ignore
59
- return [4 /*yield*/, window.electronAPI.invokeIpc("registerWidgets", data)];
60
- case 1:
61
- // @ts-ignore
62
- _a.sent();
63
- _a.label = 2;
64
- case 2: return [2 /*return*/];
65
- }
66
- });
20
+ }
21
+ static registerWidgets(widgets) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ if (this.hasElectronApi()) {
24
+ const data = JSON.parse(JSON.stringify(widgets.map(item => JSON.stringify(item))));
25
+ // @ts-ignore
26
+ yield window.electronAPI.invokeIpc("registerWidgets", data);
27
+ }
67
28
  });
68
- };
69
- ElectronApi.setConfig = function (key, value) {
70
- return __awaiter(this, void 0, void 0, function () {
71
- return __generator(this, function (_a) {
72
- switch (_a.label) {
73
- case 0:
74
- if (!this.hasElectronApi()) return [3 /*break*/, 2];
75
- // @ts-ignore
76
- return [4 /*yield*/, window.electronAPI.invokeIpc("setConfig", { key: key, value: value })];
77
- case 1:
78
- // @ts-ignore
79
- _a.sent();
80
- _a.label = 2;
81
- case 2: return [2 /*return*/];
82
- }
83
- });
29
+ }
30
+ static setConfig(key, value) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ if (this.hasElectronApi()) {
33
+ // @ts-ignore
34
+ yield window.electronAPI.invokeIpc("setConfig", { key, value });
35
+ }
84
36
  });
85
- };
86
- ElectronApi.sendBroadcastEvent = function (event) {
87
- return __awaiter(this, void 0, void 0, function () {
88
- return __generator(this, function (_a) {
89
- switch (_a.label) {
90
- case 0:
91
- if (!this.hasElectronApi()) return [3 /*break*/, 2];
92
- // @ts-ignore
93
- return [4 /*yield*/, window.electronAPI.invokeIpc(Keys_1.Keys.BROADCAST_EVENT, JSON.stringify(event))];
94
- case 1:
95
- // @ts-ignore
96
- _a.sent();
97
- _a.label = 2;
98
- case 2: return [2 /*return*/];
99
- }
100
- });
37
+ }
38
+ static sendBroadcastEvent(event) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ if (this.hasElectronApi()) {
41
+ // @ts-ignore
42
+ yield window.electronAPI.invokeIpc(Keys_1.Keys.BROADCAST_EVENT, JSON.stringify(event));
43
+ }
101
44
  });
102
- };
103
- ElectronApi.registerBroadcast = function (callback) {
104
- return __awaiter(this, void 0, void 0, function () {
105
- return __generator(this, function (_a) {
106
- switch (_a.label) {
107
- case 0: return [4 /*yield*/, this.addIpcListener(Keys_1.Keys.BROADCAST_EVENT, function (json) {
108
- callback(JSON.parse(json));
109
- })];
110
- case 1:
111
- _a.sent();
112
- return [2 /*return*/];
113
- }
45
+ }
46
+ static registerBroadcast(callback) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ yield this.addIpcListener(Keys_1.Keys.BROADCAST_EVENT, (json) => {
49
+ callback(JSON.parse(json));
114
50
  });
115
51
  });
116
- };
117
- ElectronApi.unregisterBroadcast = function () {
118
- return __awaiter(this, void 0, void 0, function () {
119
- return __generator(this, function (_a) {
120
- switch (_a.label) {
121
- case 0: return [4 /*yield*/, this.removeIpcListener(Keys_1.Keys.BROADCAST_EVENT)];
122
- case 1:
123
- _a.sent();
124
- return [2 /*return*/];
125
- }
126
- });
52
+ }
53
+ static unregisterBroadcast() {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ yield this.removeIpcListener(Keys_1.Keys.BROADCAST_EVENT);
127
56
  });
128
- };
129
- ElectronApi.addIpcListener = function (key, f) {
130
- return __awaiter(this, void 0, void 0, function () {
131
- return __generator(this, function (_a) {
132
- switch (_a.label) {
133
- case 0:
134
- if (!this.hasElectronApi()) return [3 /*break*/, 2];
135
- // @ts-ignore
136
- return [4 /*yield*/, window.electronAPI.addIpcListener(key, f)];
137
- case 1:
138
- // @ts-ignore
139
- _a.sent();
140
- _a.label = 2;
141
- case 2: return [2 /*return*/];
142
- }
143
- });
57
+ }
58
+ static addIpcListener(key, f) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ if (this.hasElectronApi()) {
61
+ // @ts-ignore
62
+ yield window.electronAPI.addIpcListener(key, f);
63
+ }
144
64
  });
145
- };
146
- ElectronApi.removeIpcListener = function (key) {
147
- return __awaiter(this, void 0, void 0, function () {
148
- return __generator(this, function (_a) {
149
- switch (_a.label) {
150
- case 0:
151
- if (!this.hasElectronApi()) return [3 /*break*/, 2];
152
- // @ts-ignore
153
- return [4 /*yield*/, window.electronAPI.removeIpcListener(key)];
154
- case 1:
155
- // @ts-ignore
156
- _a.sent();
157
- _a.label = 2;
158
- case 2: return [2 /*return*/];
159
- }
160
- });
65
+ }
66
+ static removeIpcListener(key) {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ if (this.hasElectronApi()) {
69
+ // @ts-ignore
70
+ yield window.electronAPI.removeIpcListener(key);
71
+ }
161
72
  });
162
- };
163
- ElectronApi.getConfig = function (key, defaultValue) {
164
- return __awaiter(this, void 0, void 0, function () {
165
- var value;
166
- return __generator(this, function (_a) {
167
- switch (_a.label) {
168
- case 0:
169
- if (!this.hasElectronApi()) return [3 /*break*/, 2];
170
- return [4 /*yield*/, window.electronAPI.invokeIpc("getConfig", key)];
171
- case 1:
172
- value = _a.sent();
173
- if (value === null || value === undefined) {
174
- return [2 /*return*/, defaultValue];
175
- }
176
- if (typeof defaultValue == "boolean") {
177
- return [2 /*return*/, value === "true"];
178
- }
179
- return [2 /*return*/, value];
180
- case 2: return [2 /*return*/];
73
+ }
74
+ static getConfig(key, defaultValue) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ if (this.hasElectronApi()) {
77
+ // @ts-ignore
78
+ const value = yield window.electronAPI.invokeIpc("getConfig", key);
79
+ if (value === null || value === undefined) {
80
+ return defaultValue;
181
81
  }
182
- });
82
+ if (typeof defaultValue == "boolean") {
83
+ return value === "true";
84
+ }
85
+ return value;
86
+ }
183
87
  });
184
- };
185
- ElectronApi.hasElectronApi = function () {
88
+ }
89
+ static hasElectronApi() {
186
90
  return Reflect.has(window, "electronAPI");
187
- };
188
- return ElectronApi;
189
- }());
91
+ }
92
+ }
190
93
  exports.ElectronApi = ElectronApi;
@@ -1,14 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Keys = void 0;
4
- var Keys = /** @class */ (function () {
5
- function Keys() {
6
- }
7
- Keys.CONFIG_LAUNCH_AT_STARTUP = "LAUNCH_AT_STARTUP";
8
- Keys.CONFIG_WIDGET_SHADOW = "WIDGET_SHADOW";
9
- Keys.CHANNEL_MAIN = "WeiZ5kaKijae";
10
- Keys.EVENT_WIDGET_UPDATED = "WIDGET_SHADOW";
11
- Keys.BROADCAST_EVENT = "sendBroadcastEvent";
12
- return Keys;
13
- }());
4
+ class Keys {
5
+ }
14
6
  exports.Keys = Keys;
7
+ Keys.CONFIG_LAUNCH_AT_STARTUP = "LAUNCH_AT_STARTUP";
8
+ Keys.CONFIG_WIDGET_SHADOW = "WIDGET_SHADOW";
9
+ Keys.CHANNEL_MAIN = "WeiZ5kaKijae";
10
+ Keys.EVENT_WIDGET_UPDATED = "WIDGET_SHADOW";
11
+ Keys.BROADCAST_EVENT = "sendBroadcastEvent";
package/dist/cjs/index.js CHANGED
@@ -17,5 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./model/Widget"), exports);
18
18
  __exportStar(require("./model/BroadcastEvent"), exports);
19
19
  __exportStar(require("./model/WidgetData"), exports);
20
+ __exportStar(require("./model/WidgetParams"), exports);
20
21
  __exportStar(require("./api/ElectronApi"), exports);
21
22
  __exportStar(require("./api/Keys"), exports);
23
+ __exportStar(require("./repository/WidgetDataRepository"), exports);
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BroadcastEvent = void 0;
4
- var BroadcastEvent = /** @class */ (function () {
5
- function BroadcastEvent(type, from, payload) {
4
+ class BroadcastEvent {
5
+ constructor(type, from, payload) {
6
6
  this.type = type;
7
7
  this.from = from;
8
8
  this.payload = payload;
9
9
  }
10
- return BroadcastEvent;
11
- }());
10
+ }
12
11
  exports.BroadcastEvent = BroadcastEvent;
12
+ BroadcastEvent.TYPE_WIDGET_UPDATED = "BROADCAST:WIDGET_UPDATED";
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SocialInfo = void 0;
4
- var SocialInfo = /** @class */ (function () {
5
- function SocialInfo(name, url) {
4
+ class SocialInfo {
5
+ constructor(name, url) {
6
6
  this.name = name;
7
7
  this.content = url;
8
8
  }
9
- return SocialInfo;
10
- }());
9
+ }
11
10
  exports.SocialInfo = SocialInfo;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WidgetKeyword = exports.Widget = void 0;
4
- var Widget = /** @class */ (function () {
5
- function Widget(options) {
4
+ class Widget {
5
+ constructor(options) {
6
6
  var _a, _b, _c, _d, _e;
7
7
  /**
8
8
  * 组件默认语言
@@ -27,17 +27,17 @@ var Widget = /** @class */ (function () {
27
27
  * 获取组件标题
28
28
  * @param lang 语言环境,不传则获取默认语言
29
29
  */
30
- Widget.prototype.getTitle = function (lang) {
30
+ getTitle(lang) {
31
31
  return lang ? this.title.get(lang) : this.title.get(this.lang);
32
- };
32
+ }
33
33
  /**
34
34
  * 获取组件标描述
35
35
  * @param lang 语言环境,不传则获取默认标题
36
36
  */
37
- Widget.prototype.getDescription = function (lang) {
37
+ getDescription(lang) {
38
38
  return lang ? this.description.get(lang) : this.description.get(this.lang);
39
- };
40
- Widget.prototype.toJSON = function () {
39
+ }
40
+ toJSON() {
41
41
  return {
42
42
  name: this.name,
43
43
  title: Object.fromEntries(this.title),
@@ -54,9 +54,9 @@ var Widget = /** @class */ (function () {
54
54
  configUrl: this.configUrl,
55
55
  extraUrl: Object.fromEntries(this.extraUrl),
56
56
  };
57
- };
58
- Widget.parse = function (json) {
59
- var object = JSON.parse(json);
57
+ }
58
+ static parse(json) {
59
+ const object = JSON.parse(json);
60
60
  return new Widget({
61
61
  configUrl: object["configUrl"],
62
62
  description: new Map(Object.entries(object["description"])),
@@ -73,9 +73,8 @@ var Widget = /** @class */ (function () {
73
73
  url: object["url"],
74
74
  w: object["w"]
75
75
  });
76
- };
77
- return Widget;
78
- }());
76
+ }
77
+ }
79
78
  exports.Widget = Widget;
80
79
  var WidgetKeyword;
81
80
  (function (WidgetKeyword) {
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WidgetData = void 0;
3
4
  /**
4
5
  * 组件配置数据,用于存储组件自定义页面所设置的数据
5
6
  */
6
- var WidgetData = /** @class */ (function () {
7
- function WidgetData(name, id) {
7
+ class WidgetData {
8
+ constructor(name, id) {
8
9
  this.id = id;
9
10
  this.name = name;
10
11
  }
11
- return WidgetData;
12
- }());
13
- exports.default = WidgetData;
12
+ parseJSON(json) {
13
+ Object.assign(this, json);
14
+ }
15
+ }
16
+ exports.WidgetData = WidgetData;
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WidgetHostMode = exports.ThemeMode = exports.WidgetParams = void 0;
4
+ /**
5
+ * 组件参数,如宽,高,id,语言等环境参数
6
+ */
7
+ const query_1 = require("../router/query");
8
+ class WidgetParams {
9
+ /**
10
+ * 将组件参数转为url参数
11
+ * @param object
12
+ * @return URLSearchParams w_w=2&w_h=2&w_id=21&w_width=156&w_height=156
13
+ */
14
+ toUrlParams() {
15
+ const urlParams = new URLSearchParams();
16
+ const ownPropertyNames = Object.getOwnPropertyNames(this);
17
+ for (let ownPropertyName of ownPropertyNames) {
18
+ const key = ownPropertyName;
19
+ const value = this[key];
20
+ if (value) {
21
+ urlParams.append(WidgetParams.PARAM_PREFIX + ownPropertyName, value.toString());
22
+ }
23
+ }
24
+ return urlParams;
25
+ }
26
+ getPersistKey() {
27
+ return `${this.name}-${this.id}`;
28
+ }
29
+ /**
30
+ * 从当前地址解析组件参数:
31
+ * http://localhost:8080/#/widget/config/labor_progress?w_w=2&w_h=2&w_width=156&w_height=156
32
+ * =>
33
+ * {w:2,h:2,id:21,width:156,height:156}
34
+ */
35
+ static fromCurrentLocation() {
36
+ const href = window.location.href;
37
+ let queryString = href.split("?")[1];
38
+ return this.fromObject((0, query_1.parseQuery)(queryString));
39
+ }
40
+ static setValue(widgetEnv, key, value) {
41
+ const keyWithoutPrefix = key.replace(this.PARAM_PREFIX, "");
42
+ if (keyWithoutPrefix == WidgetParams.PARAM_ID) {
43
+ widgetEnv.id = value;
44
+ }
45
+ else if (keyWithoutPrefix == WidgetParams.PARAM_X) {
46
+ widgetEnv.x = parseInt(value);
47
+ }
48
+ else if (keyWithoutPrefix == WidgetParams.PARAM_Y) {
49
+ widgetEnv.y = parseInt(value);
50
+ }
51
+ else if (keyWithoutPrefix == WidgetParams.PARAM_H) {
52
+ widgetEnv.h = parseInt(value);
53
+ }
54
+ else if (keyWithoutPrefix == WidgetParams.PARAM_W) {
55
+ widgetEnv.w = parseInt(value);
56
+ }
57
+ else if (keyWithoutPrefix == WidgetParams.PARAM_LANG) {
58
+ widgetEnv.lang = value;
59
+ }
60
+ else if (keyWithoutPrefix == WidgetParams.PARAM_THEME) {
61
+ widgetEnv.theme = value;
62
+ }
63
+ else if (keyWithoutPrefix == WidgetParams.PARAM_MODE) {
64
+ widgetEnv.mode = parseInt(value);
65
+ }
66
+ else if (keyWithoutPrefix == WidgetParams.PARAM_RADIUS) {
67
+ widgetEnv.radius = parseInt(value);
68
+ }
69
+ else if (keyWithoutPrefix == WidgetParams.PARAM_WIDTH) {
70
+ widgetEnv.width = parseInt(value);
71
+ }
72
+ else if (keyWithoutPrefix == WidgetParams.PARAM_HEIGHT) {
73
+ widgetEnv.height = parseInt(value);
74
+ }
75
+ else if (keyWithoutPrefix == WidgetParams.PARAM_NAME) {
76
+ widgetEnv.name = value;
77
+ }
78
+ else if (keyWithoutPrefix == WidgetParams.PARAM_TITLE) {
79
+ widgetEnv.title = value;
80
+ }
81
+ else if (keyWithoutPrefix == WidgetParams.PARAM_PREVIEW) {
82
+ widgetEnv.preview = (value === 'true');
83
+ }
84
+ }
85
+ /**
86
+ * 从对象键值对中初始化组件参数
87
+ * {w_w:2,w_h:2,w_id:21,w_width:156,w_height:156}=>
88
+ * {w:2,h:2,id:21,width:156,height:156}
89
+ * @param object
90
+ */
91
+ static fromObject(object) {
92
+ const widgetEnv = new WidgetParams();
93
+ const ownPropertyNames = Object.getOwnPropertyNames(object);
94
+ for (let ownPropertyName of ownPropertyNames) {
95
+ const key = ownPropertyName;
96
+ const value = object[key];
97
+ this.setValue(widgetEnv, key, value);
98
+ }
99
+ return widgetEnv;
100
+ }
101
+ }
102
+ exports.WidgetParams = WidgetParams;
103
+ WidgetParams.PARAM_PREFIX = "w_";
104
+ WidgetParams.PARAM_ID = "id";
105
+ WidgetParams.PARAM_W = "w";
106
+ WidgetParams.PARAM_H = "h";
107
+ WidgetParams.PARAM_WIDTH = "width";
108
+ WidgetParams.PARAM_HEIGHT = "height";
109
+ WidgetParams.PARAM_X = "x";
110
+ WidgetParams.PARAM_Y = "y";
111
+ WidgetParams.PARAM_LANG = "lang";
112
+ WidgetParams.PARAM_THEME = "theme";
113
+ WidgetParams.PARAM_MODE = "mode";
114
+ WidgetParams.PARAM_RADIUS = "radius";
115
+ WidgetParams.PARAM_NAME = "name";
116
+ WidgetParams.PARAM_TITLE = "title";
117
+ WidgetParams.PARAM_PREVIEW = "preview";
118
+ WidgetParams.PARAMS = [
119
+ WidgetParams.PARAM_ID,
120
+ WidgetParams.PARAM_W,
121
+ WidgetParams.PARAM_H,
122
+ WidgetParams.PARAM_X,
123
+ WidgetParams.PARAM_Y,
124
+ WidgetParams.PARAM_LANG,
125
+ WidgetParams.PARAM_THEME,
126
+ WidgetParams.PARAM_MODE,
127
+ WidgetParams.PARAM_WIDTH,
128
+ WidgetParams.PARAM_HEIGHT,
129
+ WidgetParams.PARAM_NAME,
130
+ WidgetParams.PARAM_TITLE,
131
+ WidgetParams.PARAM_PREVIEW,
132
+ ];
133
+ var ThemeMode;
134
+ (function (ThemeMode) {
135
+ ThemeMode["AUTO"] = "auto";
136
+ ThemeMode["LIGHT"] = "LIGHT";
137
+ ThemeMode["DARK"] = "DARK";
138
+ })(ThemeMode = exports.ThemeMode || (exports.ThemeMode = {}));
139
+ var WidgetHostMode;
140
+ (function (WidgetHostMode) {
141
+ WidgetHostMode[WidgetHostMode["DEFAULT"] = 0] = "DEFAULT";
142
+ WidgetHostMode[WidgetHostMode["OVERLAP"] = 1] = "OVERLAP";
143
+ })(WidgetHostMode = exports.WidgetHostMode || (exports.WidgetHostMode = {}));
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.WidgetDataRepository = void 0;
16
+ const localforage_1 = __importDefault(require("localforage"));
17
+ const BroadcastEvent_1 = require("../model/BroadcastEvent");
18
+ const ElectronApi_1 = require("../api/ElectronApi");
19
+ class WidgetDataRepository {
20
+ /**
21
+ * 保存组件数据
22
+ * @param data
23
+ */
24
+ static save(data) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ let store = this.getStore(data.name);
27
+ const result = yield store.setItem(this.getKey(data.name, data.id), JSON.stringify(data));
28
+ const broadcastEvent = new BroadcastEvent_1.BroadcastEvent(BroadcastEvent_1.BroadcastEvent.TYPE_WIDGET_UPDATED, "", data);
29
+ yield ElectronApi_1.ElectronApi.sendBroadcastEvent(broadcastEvent);
30
+ return result;
31
+ });
32
+ }
33
+ /**
34
+ * 获取组件 LocalForage 存储实例
35
+ * @param name
36
+ */
37
+ static getStore(name) {
38
+ if (this.stores.has(name)) {
39
+ return this.stores.get(name);
40
+ }
41
+ const store = localforage_1.default.createInstance({ name: name });
42
+ this.stores.set(name, store);
43
+ return store;
44
+ }
45
+ /**
46
+ * 通过组件名保存组件信息,通常用于存储可以在同类组件中共用的数据
47
+ * @param name
48
+ * @param json
49
+ */
50
+ static saveByName(name, json) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ let store = this.getStore(name);
53
+ const result = yield store.setItem(name, json);
54
+ const broadcastEvent = new BroadcastEvent_1.BroadcastEvent(BroadcastEvent_1.BroadcastEvent.TYPE_WIDGET_UPDATED, "", { name, json });
55
+ yield ElectronApi_1.ElectronApi.sendBroadcastEvent(broadcastEvent);
56
+ return result;
57
+ });
58
+ }
59
+ static findByName(name, type) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ let store = this.getStore(name);
62
+ let result = yield store.getItem(name);
63
+ if (result) {
64
+ const widgetData = new type(name);
65
+ widgetData.parseJSON(JSON.parse(result));
66
+ return widgetData;
67
+ }
68
+ return undefined;
69
+ });
70
+ }
71
+ static find(name, id, type) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ let store = this.getStore(name);
74
+ let result = yield store.getItem(this.getKey(name, id));
75
+ if (result) {
76
+ const widgetData = new type(name, id);
77
+ widgetData.parseJSON(JSON.parse(result));
78
+ return widgetData;
79
+ }
80
+ return undefined;
81
+ });
82
+ }
83
+ static getKey(name, id) {
84
+ return `${name}@${id}`;
85
+ }
86
+ }
87
+ exports.WidgetDataRepository = WidgetDataRepository;
88
+ WidgetDataRepository.stores = new Map();