@widget-js/core 0.0.6 → 0.0.8

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 (36) hide show
  1. package/dist/cjs/api/ElectronApi.js +73 -53
  2. package/dist/cjs/api/Keys.js +11 -0
  3. package/dist/cjs/index.js +5 -0
  4. package/dist/cjs/model/BroadcastEvent.js +12 -0
  5. package/dist/cjs/model/SocialInfo.js +10 -0
  6. package/dist/cjs/model/Widget.js +60 -29
  7. package/dist/cjs/model/WidgetData.js +16 -0
  8. package/dist/cjs/model/WidgetParams.js +143 -0
  9. package/dist/cjs/repository/WidgetDataRepository.js +88 -0
  10. package/dist/cjs/router/encoding.js +144 -0
  11. package/dist/cjs/router/query.js +107 -0
  12. package/dist/esm/api/ElectronApi.js +73 -54
  13. package/dist/esm/api/Keys.js +7 -0
  14. package/dist/esm/index.js +5 -0
  15. package/dist/esm/model/BroadcastEvent.js +8 -0
  16. package/dist/esm/model/SocialInfo.js +6 -0
  17. package/dist/esm/model/Widget.js +60 -30
  18. package/dist/esm/model/WidgetData.js +12 -0
  19. package/dist/esm/model/WidgetParams.js +139 -0
  20. package/dist/esm/repository/WidgetDataRepository.js +81 -0
  21. package/dist/esm/router/encoding.js +135 -0
  22. package/dist/esm/router/query.js +101 -0
  23. package/dist/types/api/ElectronApi.d.ts +8 -0
  24. package/dist/types/api/Keys.d.ts +7 -0
  25. package/dist/types/index.d.ts +5 -0
  26. package/dist/types/model/BroadcastEvent.d.ts +7 -0
  27. package/dist/types/model/SocialInfo.d.ts +6 -0
  28. package/dist/types/model/Widget.d.ts +47 -5
  29. package/dist/types/model/WidgetData.d.ts +35 -0
  30. package/dist/types/model/WidgetParams.d.ts +63 -0
  31. package/dist/types/repository/WidgetDataRepository.d.ts +27 -0
  32. package/dist/types/router/encoding.d.ts +62 -0
  33. package/dist/types/router/query.d.ts +62 -0
  34. package/dist/umd/index.js +2 -1
  35. package/dist/umd/index.js.LICENSE.txt +6 -0
  36. package/package.json +7 -4
@@ -8,66 +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 ElectronApi = /** @class */ (function () {
41
- function ElectronApi() {
42
- }
43
- ElectronApi.openAddWidgetWindow = function () {
13
+ const Keys_1 = require("./Keys");
14
+ class ElectronApi {
15
+ static openAddWidgetWindow() {
44
16
  if (this.hasElectronApi()) {
45
17
  // @ts-ignore
46
18
  window.electronAPI.invokeIpc("openAddWidgetWindow");
47
19
  }
48
- };
49
- ElectronApi.registerWidgets = function (widgets) {
50
- return __awaiter(this, void 0, void 0, function () {
51
- var data;
52
- return __generator(this, function (_a) {
53
- switch (_a.label) {
54
- case 0:
55
- if (!this.hasElectronApi()) return [3 /*break*/, 2];
56
- data = JSON.parse(JSON.stringify(widgets.map(function (item) { return item.stringify(); })));
57
- // @ts-ignore
58
- return [4 /*yield*/, window.electronAPI.invokeIpc("registerWidgets", data)];
59
- case 1:
60
- // @ts-ignore
61
- _a.sent();
62
- _a.label = 2;
63
- case 2: return [2 /*return*/];
64
- }
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
+ }
28
+ });
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
+ }
36
+ });
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
+ }
44
+ });
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));
65
50
  });
66
51
  });
67
- };
68
- ElectronApi.hasElectronApi = function () {
52
+ }
53
+ static unregisterBroadcast() {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ yield this.removeIpcListener(Keys_1.Keys.BROADCAST_EVENT);
56
+ });
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
+ }
64
+ });
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
+ }
72
+ });
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;
81
+ }
82
+ if (typeof defaultValue == "boolean") {
83
+ return value === "true";
84
+ }
85
+ return value;
86
+ }
87
+ });
88
+ }
89
+ static hasElectronApi() {
69
90
  return Reflect.has(window, "electronAPI");
70
- };
71
- return ElectronApi;
72
- }());
91
+ }
92
+ }
73
93
  exports.ElectronApi = ElectronApi;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Keys = void 0;
4
+ class Keys {
5
+ }
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
@@ -15,4 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./model/Widget"), exports);
18
+ __exportStar(require("./model/BroadcastEvent"), exports);
19
+ __exportStar(require("./model/WidgetData"), exports);
20
+ __exportStar(require("./model/WidgetParams"), exports);
18
21
  __exportStar(require("./api/ElectronApi"), exports);
22
+ __exportStar(require("./api/Keys"), exports);
23
+ __exportStar(require("./repository/WidgetDataRepository"), exports);
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BroadcastEvent = void 0;
4
+ class BroadcastEvent {
5
+ constructor(type, from, payload) {
6
+ this.type = type;
7
+ this.from = from;
8
+ this.payload = payload;
9
+ }
10
+ }
11
+ exports.BroadcastEvent = BroadcastEvent;
12
+ BroadcastEvent.TYPE_WIDGET_UPDATED = "BROADCAST:WIDGET_UPDATED";
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SocialInfo = void 0;
4
+ class SocialInfo {
5
+ constructor(name, url) {
6
+ this.name = name;
7
+ this.content = url;
8
+ }
9
+ }
10
+ exports.SocialInfo = SocialInfo;
@@ -1,49 +1,80 @@
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(name, title, desc, keywords, lang, w, h, maxW, maxH, minW, minH, url, configUrl, debugUrl) {
4
+ class Widget {
5
+ constructor(options) {
6
+ var _a, _b, _c, _d, _e;
6
7
  /**
7
8
  * 组件默认语言
8
9
  */
9
10
  this.lang = "zh";
10
- this.name = name;
11
- this.title = title;
12
- this.desc = desc;
13
- this.keywords = keywords;
14
- this.lang = lang;
15
- this.w = w;
16
- this.h = h;
17
- this.maxW = maxW;
18
- this.maxH = maxH;
19
- this.minW = minW;
20
- this.minH = minH;
21
- this.url = url;
22
- this.configUrl = configUrl;
23
- this.debugUrl = debugUrl;
11
+ this.name = options.name;
12
+ this.title = options.title;
13
+ this.description = options.description;
14
+ this.keywords = options.keywords;
15
+ this.lang = options.lang;
16
+ this.w = options.w;
17
+ this.h = options.h;
18
+ this.maxW = (_a = options.maxW) !== null && _a !== void 0 ? _a : options.w;
19
+ this.maxH = (_b = options.maxH) !== null && _b !== void 0 ? _b : options.h;
20
+ this.minW = (_c = options.minW) !== null && _c !== void 0 ? _c : options.w;
21
+ this.minH = (_d = options.minH) !== null && _d !== void 0 ? _d : options.h;
22
+ this.url = options.url;
23
+ this.configUrl = options.configUrl;
24
+ this.extraUrl = (_e = options.extraUrl) !== null && _e !== void 0 ? _e : new Map();
24
25
  }
25
26
  /**
26
27
  * 获取组件标题
27
28
  * @param lang 语言环境,不传则获取默认语言
28
29
  */
29
- Widget.prototype.getTitle = function (lang) {
30
+ getTitle(lang) {
30
31
  return lang ? this.title.get(lang) : this.title.get(this.lang);
31
- };
32
+ }
32
33
  /**
33
34
  * 获取组件标描述
34
35
  * @param lang 语言环境,不传则获取默认标题
35
36
  */
36
- Widget.prototype.getDesc = function (lang) {
37
- return lang ? this.desc.get(lang) : this.desc.get(this.lang);
38
- };
39
- Widget.prototype.stringify = function () {
40
- var jsonObject = JSON.parse(JSON.stringify(this));
41
- jsonObject["title"] = Object.fromEntries(this.title);
42
- jsonObject["desc"] = Object.fromEntries(this.desc);
43
- return JSON.stringify(jsonObject);
44
- };
45
- return Widget;
46
- }());
37
+ getDescription(lang) {
38
+ return lang ? this.description.get(lang) : this.description.get(this.lang);
39
+ }
40
+ toJSON() {
41
+ return {
42
+ name: this.name,
43
+ title: Object.fromEntries(this.title),
44
+ description: Object.fromEntries(this.description),
45
+ keywords: this.keywords,
46
+ lang: this.lang,
47
+ w: this.w,
48
+ h: this.h,
49
+ maxW: this.maxW,
50
+ maxH: this.maxH,
51
+ minW: this.minW,
52
+ minH: this.minH,
53
+ url: this.url,
54
+ configUrl: this.configUrl,
55
+ extraUrl: Object.fromEntries(this.extraUrl),
56
+ };
57
+ }
58
+ static parse(json) {
59
+ const object = JSON.parse(json);
60
+ return new Widget({
61
+ configUrl: object["configUrl"],
62
+ description: new Map(Object.entries(object["description"])),
63
+ extraUrl: new Map(Object.entries(object["extraUrl"])),
64
+ h: object["h"],
65
+ keywords: object["keywords"],
66
+ lang: object["lang"],
67
+ maxH: object["maxH"],
68
+ maxW: object["maxW"],
69
+ minH: object["minH"],
70
+ minW: object["minW"],
71
+ name: object["name"],
72
+ title: new Map(Object.entries(object["title"])),
73
+ url: object["url"],
74
+ w: object["w"]
75
+ });
76
+ }
77
+ }
47
78
  exports.Widget = Widget;
48
79
  var WidgetKeyword;
49
80
  (function (WidgetKeyword) {
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WidgetData = void 0;
4
+ /**
5
+ * 组件配置数据,用于存储组件自定义页面所设置的数据
6
+ */
7
+ class WidgetData {
8
+ constructor(name, id) {
9
+ this.id = id;
10
+ this.name = name;
11
+ }
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 findWithName(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();