@widget-js/core 0.11.22-beta.2 → 24.1.1-beta.1

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.
package/dist/index.js CHANGED
@@ -627,7 +627,7 @@ function getTextByLanguageCode(langMap, langCode, fallbackToBrowser = true) {
627
627
  }
628
628
  }
629
629
  if (result == void 0) {
630
- let langCode2 = Object.keys(langMap);
630
+ const langCode2 = Object.keys(langMap);
631
631
  if (langCode2.length > 0) {
632
632
  return langMap[langCode2[0]];
633
633
  }
@@ -637,7 +637,7 @@ function getTextByLanguageCode(langMap, langCode, fallbackToBrowser = true) {
637
637
 
638
638
  // src/model/Page.ts
639
639
  var Page = class {
640
- //窗口名称,名称与包名类似,e.g. com.example.countdown
640
+ // 窗口名称,名称与包名类似,e.g. com.example.countdown
641
641
  name;
642
642
  /**
643
643
  * 窗口标题,显示在界面上的,
@@ -709,8 +709,9 @@ var Page = class {
709
709
  * @param lang 语言环境,不传则获取默认标题
710
710
  */
711
711
  getDescription(lang) {
712
- if (this.description == void 0)
712
+ if (this.description == void 0) {
713
713
  return void 0;
714
+ }
714
715
  return getTextByLanguageCode(this.description, lang);
715
716
  }
716
717
  static parseJSON(json) {
@@ -718,7 +719,7 @@ var Page = class {
718
719
  return this.parseObject(object);
719
720
  }
720
721
  static parseObject(obj) {
721
- let widget = new Page({
722
+ const widget = new Page({
722
723
  path: "",
723
724
  description: {},
724
725
  height: 0,
@@ -761,7 +762,7 @@ var Widget = class extends Page {
761
762
  return this.parseObject(object);
762
763
  }
763
764
  static parseObject(obj) {
764
- let widget = new Widget({
765
+ const widget = new Widget({
765
766
  path: "",
766
767
  description: {},
767
768
  height: 0,
@@ -826,7 +827,7 @@ var WebSocketEventType = /* @__PURE__ */ ((WebSocketEventType2) => {
826
827
  return WebSocketEventType2;
827
828
  })(WebSocketEventType || {});
828
829
  var WebSocketEvent = class {
829
- //类型
830
+ // 类型
830
831
  type;
831
832
  payload;
832
833
  constructor(type, payload) {
@@ -905,7 +906,7 @@ var WidgetData = class {
905
906
  const properties = {};
906
907
  if (this.theme) {
907
908
  const prefix = "--widget-";
908
- let keys = Object.keys(this.theme);
909
+ const keys = Object.keys(this.theme);
909
910
  keys.filter((key) => this.theme[key] != void 0).map((key) => {
910
911
  properties[`${prefix}${(0, import_kebabCase.default)(key)}`] = `${this.theme[key]}`;
911
912
  });
@@ -924,6 +925,9 @@ var WidgetData = class {
924
925
  }
925
926
  };
926
927
 
928
+ // src/model/WidgetParams.ts
929
+ var import_snakeCase = __toESM(require_snakeCase(), 1);
930
+
927
931
  // src/router/encoding.ts
928
932
  var HASH_RE = /#/g;
929
933
  var AMPERSAND_RE = /&/g;
@@ -938,7 +942,7 @@ var ENC_PIPE_RE = /%7C/g;
938
942
  var ENC_CURLY_CLOSE_RE = /%7D/g;
939
943
  var ENC_SPACE_RE = /%20/g;
940
944
  function commonEncode(text) {
941
- return encodeURI("" + text).replace(ENC_PIPE_RE, "|").replace(ENC_BRACKET_OPEN_RE, "[").replace(ENC_BRACKET_CLOSE_RE, "]");
945
+ return encodeURI(`${text}`).replace(ENC_PIPE_RE, "|").replace(ENC_BRACKET_OPEN_RE, "[").replace(ENC_BRACKET_CLOSE_RE, "]");
942
946
  }
943
947
  function encodeQueryValue(text) {
944
948
  return commonEncode(text).replace(PLUS_RE, "%2B").replace(ENC_SPACE_RE, "+").replace(HASH_RE, "%23").replace(AMPERSAND_RE, "%26").replace(ENC_BACKTICK_RE, "`").replace(ENC_CURLY_OPEN_RE, "{").replace(ENC_CURLY_CLOSE_RE, "}").replace(ENC_CARET_RE, "^");
@@ -948,18 +952,19 @@ function encodeQueryKey(text) {
948
952
  }
949
953
  function decode(text) {
950
954
  try {
951
- return decodeURIComponent("" + text);
955
+ return decodeURIComponent(`${text}`);
952
956
  } catch (err) {
953
957
  }
954
- return "" + text;
958
+ return `${text}`;
955
959
  }
956
960
 
957
961
  // src/router/query.ts
958
962
  var isArray = Array.isArray;
959
963
  function parseQuery(search) {
960
964
  const query = {};
961
- if (search === "" || search === "?")
965
+ if (search === "" || search === "?") {
962
966
  return query;
967
+ }
963
968
  const hasLeadingIM = search[0] === "?";
964
969
  const searchParams = (hasLeadingIM ? search.slice(1) : search).split("&");
965
970
  for (let i = 0; i < searchParams.length; ++i) {
@@ -995,8 +1000,9 @@ function stringifyQuery(query) {
995
1000
  values.forEach((value2) => {
996
1001
  if (value2 !== void 0) {
997
1002
  search += (search.length ? "&" : "") + key;
998
- if (value2 != null)
999
- search += "=" + value2;
1003
+ if (value2 != null) {
1004
+ search += `=${value2}`;
1005
+ }
1000
1006
  }
1001
1007
  });
1002
1008
  }
@@ -1004,31 +1010,30 @@ function stringifyQuery(query) {
1004
1010
  }
1005
1011
 
1006
1012
  // src/model/WidgetParams.ts
1007
- var import_snakeCase = __toESM(require_snakeCase(), 1);
1008
1013
  var _WidgetParams = class {
1009
- //组件id
1014
+ // 组件id
1010
1015
  id;
1011
- //网格宽度,1就代表宽度占用1格
1016
+ // 网格宽度,1就代表宽度占用1格
1012
1017
  width;
1013
- //宽度,单位px
1018
+ // 宽度,单位px
1014
1019
  widthPx;
1015
- //宽度,单位px
1020
+ // 宽度,单位px
1016
1021
  heightPx;
1017
- //网格高度,2就代表高度占用2格
1022
+ // 网格高度,2就代表高度占用2格
1018
1023
  height;
1019
1024
  x;
1020
1025
  y;
1021
- //是否是预览模式,添加组件时,预览状态
1026
+ // 是否是预览模式,添加组件时,预览状态
1022
1027
  preview;
1023
- //语言环境:zh,en,jp...
1028
+ // 语言环境:zh,en,jp...
1024
1029
  lang;
1025
- //主题:浅色,深色
1030
+ // 主题:浅色,深色
1026
1031
  theme;
1027
1032
  //
1028
1033
  mode;
1029
- //系统设置的组件圆角半径
1034
+ // 系统设置的组件圆角半径
1030
1035
  radius;
1031
- //组件名
1036
+ // 组件名
1032
1037
  name;
1033
1038
  title;
1034
1039
  /**
@@ -1039,7 +1044,7 @@ var _WidgetParams = class {
1039
1044
  toUrlParams() {
1040
1045
  const urlParams = new URLSearchParams();
1041
1046
  const ownPropertyNames = Object.getOwnPropertyNames(this);
1042
- for (let ownPropertyName of ownPropertyNames) {
1047
+ for (const ownPropertyName of ownPropertyNames) {
1043
1048
  const key = ownPropertyName;
1044
1049
  const value = this[key];
1045
1050
  if (value) {
@@ -1059,17 +1064,17 @@ var _WidgetParams = class {
1059
1064
  */
1060
1065
  static fromCurrentLocation() {
1061
1066
  const href = window.location.href;
1062
- let strings = href.split("?");
1067
+ const strings = href.split("?");
1063
1068
  if (strings.length > 1) {
1064
- let queryString = strings[1];
1069
+ const queryString = strings[1];
1065
1070
  return this.fromObject(parseQuery(queryString));
1066
1071
  }
1067
1072
  return new _WidgetParams();
1068
1073
  }
1069
1074
  static fromLocation(url) {
1070
- let strings = url.split("?");
1075
+ const strings = url.split("?");
1071
1076
  if (strings.length > 1) {
1072
- let queryString = strings[1];
1077
+ const queryString = strings[1];
1073
1078
  return this.fromObject(parseQuery(queryString));
1074
1079
  }
1075
1080
  return new _WidgetParams();
@@ -1079,25 +1084,25 @@ var _WidgetParams = class {
1079
1084
  if (keyWithoutPrefix == _WidgetParams.PARAM_ID) {
1080
1085
  widgetEnv.id = value;
1081
1086
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_X) {
1082
- widgetEnv.x = parseInt(value);
1087
+ widgetEnv.x = Number.parseInt(value);
1083
1088
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_Y) {
1084
- widgetEnv.y = parseInt(value);
1089
+ widgetEnv.y = Number.parseInt(value);
1085
1090
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_HEIGHT) {
1086
- widgetEnv.height = parseInt(value);
1091
+ widgetEnv.height = Number.parseInt(value);
1087
1092
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_WIDTH) {
1088
- widgetEnv.width = parseInt(value);
1093
+ widgetEnv.width = Number.parseInt(value);
1089
1094
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_LANG) {
1090
1095
  widgetEnv.lang = value;
1091
1096
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_THEME) {
1092
1097
  widgetEnv.theme = value;
1093
1098
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_MODE) {
1094
- widgetEnv.mode = parseInt(value);
1099
+ widgetEnv.mode = Number.parseInt(value);
1095
1100
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_RADIUS) {
1096
- widgetEnv.radius = parseInt(value);
1101
+ widgetEnv.radius = Number.parseInt(value);
1097
1102
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_WIDTH_PX) {
1098
- widgetEnv.widthPx = parseInt(value);
1103
+ widgetEnv.widthPx = Number.parseInt(value);
1099
1104
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_HEIGHT_PX) {
1100
- widgetEnv.heightPx = parseInt(value);
1105
+ widgetEnv.heightPx = Number.parseInt(value);
1101
1106
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_NAME) {
1102
1107
  widgetEnv.name = value;
1103
1108
  } else if (keyWithoutPrefix == _WidgetParams.PARAM_TITLE) {
@@ -1115,7 +1120,7 @@ var _WidgetParams = class {
1115
1120
  static fromObject(object) {
1116
1121
  const widgetEnv = new _WidgetParams();
1117
1122
  const ownPropertyNames = Object.getOwnPropertyNames(object);
1118
- for (let ownPropertyName of ownPropertyNames) {
1123
+ for (const ownPropertyName of ownPropertyNames) {
1119
1124
  const key = ownPropertyName;
1120
1125
  const value = object[key];
1121
1126
  this.setValue(widgetEnv, key, value);
@@ -1247,15 +1252,15 @@ var supportedProtocols = /* @__PURE__ */ new Set([
1247
1252
  "http:",
1248
1253
  "file:"
1249
1254
  ]);
1250
- var hasCustomProtocol = (urlString) => {
1255
+ function hasCustomProtocol(urlString) {
1251
1256
  try {
1252
1257
  const { protocol } = new URL(urlString);
1253
1258
  return protocol.endsWith(":") && !supportedProtocols.has(protocol);
1254
1259
  } catch {
1255
1260
  return false;
1256
1261
  }
1257
- };
1258
- var normalizeDataURL = (urlString, option) => {
1262
+ }
1263
+ function normalizeDataURL(urlString, option) {
1259
1264
  const match = /^data:(?<type>[^,]*?),(?<data>[^#]*?)(?:#(?<hash>.*))?$/.exec(urlString);
1260
1265
  if (!match) {
1261
1266
  throw new Error(`Invalid URL: ${urlString}`);
@@ -1289,7 +1294,7 @@ var normalizeDataURL = (urlString, option) => {
1289
1294
  normalizedMediaType.unshift(mimeType);
1290
1295
  }
1291
1296
  return `data:${normalizedMediaType.join(";")},${isBase64 ? data.trim() : data}${hash ? `#${hash}` : ""}`;
1292
- };
1297
+ }
1293
1298
  function normalizeUrl(urlString, options) {
1294
1299
  options = {
1295
1300
  defaultProtocol: "http",
@@ -1377,7 +1382,7 @@ function normalizeUrl(urlString, options) {
1377
1382
  const lastComponent = pathComponents[pathComponents.length - 1];
1378
1383
  if (testParameter(lastComponent, options.removeDirectoryIndex)) {
1379
1384
  pathComponents = pathComponents.slice(0, -1);
1380
- urlObject.pathname = pathComponents.slice(1).join("/") + "/";
1385
+ urlObject.pathname = `${pathComponents.slice(1).join("/")}/`;
1381
1386
  }
1382
1387
  }
1383
1388
  if (urlObject.hostname) {
@@ -1546,7 +1551,7 @@ var WidgetPackage = class {
1546
1551
  this.devOptions = options.devOptions;
1547
1552
  this.remote = options.remote;
1548
1553
  if (options.widgets) {
1549
- for (let widget of options.widgets) {
1554
+ for (const widget of options.widgets) {
1550
1555
  this.widgets.push(widget);
1551
1556
  }
1552
1557
  }
@@ -1556,7 +1561,7 @@ var WidgetPackage = class {
1556
1561
  return this.parseObject(object);
1557
1562
  }
1558
1563
  static parseObject(obj) {
1559
- let widgetPackage = new WidgetPackage({
1564
+ const widgetPackage = new WidgetPackage({
1560
1565
  author: "",
1561
1566
  description: {
1562
1567
  "zh-CN": ""
@@ -1573,8 +1578,8 @@ var WidgetPackage = class {
1573
1578
  Object.assign(widgetPackage, obj);
1574
1579
  const widgets = [];
1575
1580
  if (widgetPackage.widgets) {
1576
- for (let jsonWidget of widgetPackage.widgets) {
1577
- let widget = new Widget({
1581
+ for (const jsonWidget of widgetPackage.widgets) {
1582
+ const widget = new Widget({
1578
1583
  previewImage: "",
1579
1584
  path: "",
1580
1585
  description: { "zh-CN": "" },
@@ -1693,10 +1698,10 @@ var GridSystem = class extends GridRect {
1693
1698
  * @param screenY
1694
1699
  */
1695
1700
  constructor(cellSize, padding, screenWidth, screenHeight, screenX, screenY) {
1696
- let gridMaxWidth = screenWidth - padding * 2;
1697
- let gridMaxHeight = screenHeight - padding * 2;
1698
- let horizontalPadding = gridMaxWidth % cellSize / 2 + padding;
1699
- let verticalPadding = gridMaxHeight % cellSize / 2 + padding;
1701
+ const gridMaxWidth = screenWidth - padding * 2;
1702
+ const gridMaxHeight = screenHeight - padding * 2;
1703
+ const horizontalPadding = gridMaxWidth % cellSize / 2 + padding;
1704
+ const verticalPadding = gridMaxHeight % cellSize / 2 + padding;
1700
1705
  const horizontalCellCount = Math.floor(gridMaxWidth / cellSize);
1701
1706
  const verticalCellCount = Math.floor(gridMaxHeight / cellSize);
1702
1707
  super(horizontalPadding, verticalPadding, horizontalCellCount * cellSize, verticalCellCount * cellSize);
@@ -1717,8 +1722,8 @@ var GridSystem = class extends GridRect {
1717
1722
  * @param gridSystem
1718
1723
  */
1719
1724
  snapCanvasPosition(x, y) {
1720
- let newX = this.getRangeValue(x - this.left, 0, this.right);
1721
- let newY = this.getRangeValue(y - this.top, 0, this.bottom);
1725
+ const newX = this.getRangeValue(x - this.left, 0, this.right);
1726
+ const newY = this.getRangeValue(y - this.top, 0, this.bottom);
1722
1727
  return this.snapPosition(newX, newY);
1723
1728
  }
1724
1729
  snapPosition(newX, newY) {
@@ -1739,8 +1744,8 @@ var GridSystem = class extends GridRect {
1739
1744
  snapSize(width, height) {
1740
1745
  let newWidth = Math.floor(width);
1741
1746
  let newHeight = Math.floor(height);
1742
- let diffWidth = newWidth % this.cellSize;
1743
- let diffHeight = newHeight % this.cellSize;
1747
+ const diffWidth = newWidth % this.cellSize;
1748
+ const diffHeight = newHeight % this.cellSize;
1744
1749
  if (diffWidth > this.cellSize / 2) {
1745
1750
  newWidth = width + this.cellSize - diffWidth;
1746
1751
  } else {
@@ -1762,7 +1767,7 @@ var GridSystem = class extends GridRect {
1762
1767
  * @param maxHeight
1763
1768
  */
1764
1769
  snapCanvasRect(rect, minWidth, maxWidth, minHeight, maxHeight) {
1765
- let [newX, newY] = this.snapCanvasPosition(rect.left, rect.top);
1770
+ const [newX, newY] = this.snapCanvasPosition(rect.left, rect.top);
1766
1771
  return this.snapRect(rect, minWidth, maxWidth, minHeight, maxHeight, newX, newY);
1767
1772
  }
1768
1773
  snapRect(rect, minWidth, maxWidth, minHeight, maxHeight, newX, newY) {
@@ -1788,7 +1793,7 @@ var GridSystem = class extends GridRect {
1788
1793
  snapWindow(rect, minWidth, maxWidth, minHeight, maxHeight) {
1789
1794
  rect.left = rect.left - (this.screenLeft ?? 0);
1790
1795
  rect.top = rect.top - (this.screenTop ?? 0);
1791
- let result = this.snapCanvasRect(rect, minWidth, maxWidth, minHeight, maxHeight);
1796
+ const result = this.snapCanvasRect(rect, minWidth, maxWidth, minHeight, maxHeight);
1792
1797
  return new GridRect(
1793
1798
  result.left + (this.screenLeft ?? 0),
1794
1799
  result.top + (this.screenTop ?? 0),
@@ -1940,35 +1945,42 @@ var WidgetApiImpl = class extends BaseApi {
1940
1945
  */
1941
1946
  async getWidgetConfigUrl(widgetName, widgetParams) {
1942
1947
  const widget = await this.getWidget(widgetName);
1943
- if (!widget)
1948
+ if (!widget) {
1944
1949
  return null;
1950
+ }
1945
1951
  const configPagePath = widget.configPagePath;
1946
- if (!configPagePath)
1952
+ if (!configPagePath) {
1947
1953
  return null;
1954
+ }
1948
1955
  const widgetPackage = await this.getWidgetPackage(widget.packageName);
1949
- if (!widgetPackage)
1956
+ if (!widgetPackage) {
1950
1957
  return null;
1958
+ }
1951
1959
  return WidgetUrlUtils.getWidgetUrl(configPagePath, widgetPackage, widgetParams);
1952
1960
  }
1953
1961
  async getWidgetUrl(widgetName, widgetParams) {
1954
1962
  const widget = await this.getWidget(widgetName);
1955
- if (!widget)
1963
+ if (!widget) {
1956
1964
  return null;
1965
+ }
1957
1966
  const widgetPackage = await this.getWidgetPackage(widget.packageName);
1958
- if (!widgetPackage)
1967
+ if (!widgetPackage) {
1959
1968
  return null;
1969
+ }
1960
1970
  return WidgetUrlUtils.getWidgetUrl(widget.path, widgetPackage, widgetParams);
1961
1971
  }
1962
1972
  async getWidgetPackageIndexUrl(packageName, hash) {
1963
1973
  const widgetPackage = await this.getWidgetPackage(packageName);
1964
- if (!widgetPackage)
1974
+ if (!widgetPackage) {
1965
1975
  return null;
1976
+ }
1966
1977
  return widgetPackage.getIndexUrl(hash);
1967
1978
  }
1968
1979
  async getWidgetPackageUrl(packageName) {
1969
1980
  const widgetPackage = await this.getWidgetPackage(packageName);
1970
- if (!widgetPackage)
1981
+ if (!widgetPackage) {
1971
1982
  return null;
1983
+ }
1972
1984
  return widgetPackage.url;
1973
1985
  }
1974
1986
  // /**
@@ -2020,8 +2032,8 @@ var WidgetDataApiImpl = class {
2020
2032
  * @param options
2021
2033
  */
2022
2034
  async save(data, options = { sendBroadcast: true }) {
2023
- let store = this.getStore(data.name);
2024
- let json = JSON.stringify(data);
2035
+ const store = this.getStore(data.name);
2036
+ const json = JSON.stringify(data);
2025
2037
  const result = await store.setItem(this.getKey(data.name, data.id), json);
2026
2038
  if (options.sendBroadcast) {
2027
2039
  const broadcastEvent = new BroadcastEvent({
@@ -2070,7 +2082,7 @@ var WidgetDataApiImpl = class {
2070
2082
  data = args[0];
2071
2083
  name = data.name;
2072
2084
  }
2073
- let store = this.getStore(name);
2085
+ const store = this.getStore(name);
2074
2086
  dbStr = await store.getItem(name);
2075
2087
  if (dbStr) {
2076
2088
  data.parseJSON(JSON.parse(dbStr));
@@ -2079,8 +2091,8 @@ var WidgetDataApiImpl = class {
2079
2091
  return void 0;
2080
2092
  }
2081
2093
  async find(name, id, type) {
2082
- let store = this.getStore(name);
2083
- let result = await store.getItem(this.getKey(name, id));
2094
+ const store = this.getStore(name);
2095
+ const result = await store.getItem(this.getKey(name, id));
2084
2096
  if (result) {
2085
2097
  const widgetData = new type(name, id);
2086
2098
  widgetData.parseJSON(JSON.parse(result));
@@ -2205,7 +2217,7 @@ var BrowserWindowApiImpl = class extends BaseApi {
2205
2217
  return await this.invokeMethod("unmaximize");
2206
2218
  }
2207
2219
  async setZoomLevel(level) {
2208
- return await this.invokeMethod("setZoomLevel");
2220
+ return await this.invokeMethod("setZoomLevel", level);
2209
2221
  }
2210
2222
  async reload() {
2211
2223
  return await this.invokeMethod("reload");
@@ -2569,8 +2581,8 @@ var AppApiImpl = class extends BaseApi {
2569
2581
  /**
2570
2582
  * 获取应用版本号,格式为 x.y.z
2571
2583
  */
2572
- async getVersion() {
2573
- return this.invokeMethod("getVersion");
2584
+ async getVersion(type) {
2585
+ return this.invokeMethod("getVersion", type);
2574
2586
  }
2575
2587
  async getPreloadPath() {
2576
2588
  return this.invokeMethod("getPreloadPath");
@@ -2707,6 +2719,12 @@ var FileApiImpl = class extends BaseApi {
2707
2719
  }
2708
2720
  };
2709
2721
  var FileApi = new FileApiImpl();
2722
+
2723
+ // src/api/SystemApi.ts
2724
+ var SystemApiEvent = /* @__PURE__ */ ((SystemApiEvent2) => {
2725
+ SystemApiEvent2["DATE_CHANGED"] = "event::cn.widgetjs.core.sys.date.changed";
2726
+ return SystemApiEvent2;
2727
+ })(SystemApiEvent || {});
2710
2728
  export {
2711
2729
  ApiConstants,
2712
2730
  AppApi,
@@ -2744,6 +2762,7 @@ export {
2744
2762
  ShortcutApi,
2745
2763
  ShortcutApiEvent,
2746
2764
  StoreApi,
2765
+ SystemApiEvent,
2747
2766
  ThemeMode,
2748
2767
  WebSocketEvent,
2749
2768
  WebSocketEventType,