@limetech/lime-elements 36.4.0-next.5 → 36.4.0-next.7

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.
@@ -5,50 +5,40 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  const index = require('./index-287e25e0.js');
6
6
  const config = require('./config-c6520559.js');
7
7
 
8
+ const CACHE_NAME = '@limetech/lime-elements/icons';
8
9
  class IconCache {
9
10
  constructor() {
10
- /*
11
- * Cache of all loaded SVGs
12
- */
13
- this.cache = {};
14
- /*
15
- * Contains resolve functions for all unresolved promises that are waiting for SVG data.
16
- */
17
- this.resolveFunctions = {};
11
+ this.promises = {};
12
+ this.cache = caches.open(CACHE_NAME);
18
13
  }
19
14
  /**
20
15
  * Get icon data from the cache
21
16
  * @param {string} name name of the icon
22
17
  * @param {string} path path on the server where the assets are located
23
- * @returns {string} svg markup
18
+ * @returns {Promise<string>} svg markup
24
19
  */
25
20
  async get(name, path = '') {
26
- if (!this.cache[name]) {
27
- this.cache[name] = await this.getIcon(name, path);
21
+ const cache = await this.cache;
22
+ const url = this.getUrl(name, path);
23
+ let response = await cache.match(url);
24
+ if (!response) {
25
+ response = await this.fetchData(url, cache);
28
26
  }
29
- return this.cache[name];
27
+ return this.getIcon(response);
30
28
  }
31
- /*
32
- * Creates and returns a promise that will be resolved when SVG data is available
33
- */
34
- getIcon(name, path) {
35
- return new Promise((resolve) => {
36
- if (!this.resolveFunctions[name]) {
37
- this.resolveFunctions[name] = [];
38
- this.fetchData(name, path);
39
- }
40
- this.resolveFunctions[name].push(resolve);
41
- });
29
+ async fetchData(url, cache) {
30
+ let requestPromise = this.promises[url];
31
+ if (requestPromise === undefined) {
32
+ requestPromise = cache.add(url);
33
+ this.promises[url] = requestPromise;
34
+ }
35
+ await requestPromise;
36
+ return cache.match(url);
42
37
  }
43
38
  /*
44
- * Fetch SVG data from the server
39
+ * Get icon data from a response
45
40
  */
46
- async fetchData(name, path) {
47
- let iconPath = path || '';
48
- if (path && !path.endsWith('/')) {
49
- iconPath = `${path}/`;
50
- }
51
- const response = await fetch(`${iconPath}assets/icons/${name}.svg`);
41
+ async getIcon(response) {
52
42
  let svgData = await response.text();
53
43
  // Some of the icons in the Icons8 library have hard coded black color on some of the paths.
54
44
  // In order to apply coloring with CSS, these have to be set to 'currentColor'
@@ -56,7 +46,7 @@ class IconCache {
56
46
  if (!this.validSvg(svgData)) {
57
47
  throw new Error('Invalid SVG');
58
48
  }
59
- this.resolvePromises(name, svgData);
49
+ return svgData;
60
50
  }
61
51
  /*
62
52
  * Check if the given data is a valid SVG document
@@ -66,20 +56,17 @@ class IconCache {
66
56
  const svgDoc = parser.parseFromString(data, 'image/svg+xml');
67
57
  return svgDoc.documentElement.tagName.toLowerCase() === 'svg';
68
58
  }
69
- /*
70
- * Resolve all promises waiting for data for a specific icon
71
- */
72
- resolvePromises(name, svgData) {
73
- const resolves = this.resolveFunctions[name];
74
- resolves.forEach((resolve) => {
75
- resolve(svgData);
76
- });
77
- this.resolveFunctions[name] = null;
59
+ getUrl(name, path) {
60
+ let iconPath = path || '';
61
+ if (path && !path.endsWith('/')) {
62
+ iconPath = `${path}/`;
63
+ }
64
+ return `${iconPath}assets/icons/${name}.svg`;
78
65
  }
79
66
  }
80
- const cache = new IconCache();
81
- const iconCache = (() => {
82
- return cache;
67
+ const iconCache = new IconCache();
68
+ const iconCache$1 = (() => {
69
+ return iconCache;
83
70
  })();
84
71
 
85
72
  const iconCss = ":host{background-color:var(--icon-background-color, transparent);border-radius:50%;display:inline-block;line-height:0;box-sizing:border-box}:host svg{fill:currentColor;height:100%;pointer-events:none;width:100%}:host([hidden]){display:none}:host([size=x-small]){height:0.9375rem !important;width:0.9375rem !important}:host([size=small]){height:1.25rem !important;width:1.25rem !important}:host([size=medium]){height:1.5625rem !important;width:1.5625rem !important}:host([size=large]){height:1.875rem !important;width:1.875rem !important}:host([badge][size=x-small]){height:1.4375rem !important;width:1.4375rem !important}:host([badge][size=x-small])>div{margin:0.25rem}:host([badge][size=small]){height:1.875rem !important;width:1.875rem !important}:host([badge][size=small])>div{margin:0.3125rem}:host([badge][size=medium]){height:2.5rem !important;width:2.5rem !important}:host([badge][size=medium])>div{margin:0.5rem}:host([badge][size=large]){height:2.875rem !important;width:2.875rem !important}:host([badge][size=large])>div{margin:0.5rem}";
@@ -110,7 +97,7 @@ const Icon = class {
110
97
  * @returns {string} the icon SVG data
111
98
  */
112
99
  loadSvg(name) {
113
- return iconCache.get(name, config.config.iconPath);
100
+ return iconCache$1.get(name, config.config.iconPath);
114
101
  }
115
102
  /*
116
103
  * There is no way to style external SVG files with CSS, i.e. SVGs loaded