@teipublisher/pb-components 1.42.7 → 1.43.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/src/theming.js ADDED
@@ -0,0 +1,150 @@
1
+ import { waitOnce } from "./pb-mixin.js";
2
+
3
+ /**
4
+ * Maps theme selector to CSSStyleSheet or null.
5
+ *
6
+ * @type {Map<string,(CSSStyleSheet|null)>}
7
+ */
8
+ const themeMap = new Map();
9
+
10
+ /**
11
+ * Load one or more CSS stylesheet from the given URL and return
12
+ * a CSSStyleSheet. The returned stylesheet can be assigned
13
+ * to `adoptedStyleSheets`.
14
+ *
15
+ * @param {string[]} urls absolute URL
16
+ * @returns {Promise<CSSStyleSheet|null>} constructed CSSStyleSheet or null
17
+ */
18
+ export async function loadStylesheets(urls) {
19
+ const output = [];
20
+ for(let url of urls) {
21
+ const css = await loadResource(url);
22
+ if (css) {
23
+ output.push(css);
24
+ }
25
+ }
26
+ if (output.length > 0) {
27
+ const sheet = new CSSStyleSheet();
28
+ return sheet.replace(output.join(''));
29
+ }
30
+ return null;
31
+ }
32
+
33
+ function loadResource(url) {
34
+ return fetch(url)
35
+ .then(response => {
36
+ if (response.ok) {
37
+ return response.text();
38
+ }
39
+ console.warn('<theming> Component stylesheet not found: %s', url);
40
+ return null;
41
+ })
42
+ .then(text => {
43
+ return text;
44
+ })
45
+ .catch(error => {
46
+ console.error('<theming> Error loading stylesheet %s: %o', url, error);
47
+ return null;
48
+ });
49
+ }
50
+
51
+ /**
52
+ * From the global component theme, import all rules which would apply to the
53
+ * given element into a new CSSStyleSheet and return it.
54
+ *
55
+ * @param {HTMLElement} elem a web component or HTML element
56
+ * @returns {CSSStyleSheet|null} a new CSSStylesheet or null
57
+ */
58
+ export function importStyles(elem) {
59
+ const page = document.querySelector('pb-page');
60
+ if (!page) {
61
+ return null;
62
+ }
63
+ const theme = page.stylesheet;
64
+ if (!theme) {
65
+ // no component styles defined
66
+ return null;
67
+ }
68
+
69
+ const selectors = getSelectors(elem).join('|');
70
+ if (themeMap.has(selectors)) {
71
+ return themeMap.get(selectors);
72
+ }
73
+ const prefixRegex = new RegExp(`^(${selectors})\\b`);
74
+ let adoptedSheet = null;
75
+ const rules = theme.cssRules;
76
+ const newCSS = copyStyles(rules, prefixRegex, []);
77
+ if (newCSS.length > 0) {
78
+ adoptedSheet = new CSSStyleSheet();
79
+ adoptedSheet.replaceSync(newCSS.join(''));
80
+ }
81
+ console.log('<theming> caching stylesheet for %s', selectors);
82
+ themeMap.set(selectors, adoptedSheet);
83
+ return adoptedSheet;
84
+ }
85
+
86
+ /**
87
+ * Recursively copy matching styles from the theme CSS
88
+ * to create a new CSS stylesheet having all styles required
89
+ * by the component.
90
+ *
91
+ * @param {CSSRule[]} rules
92
+ * @param {RegExp} prefixRegex
93
+ * @param {string[]} output
94
+ * @returns {string[]}
95
+ */
96
+ function copyStyles(rules, prefixRegex, output) {
97
+ for (let i = 0; i < rules.length; i++) {
98
+ const rule = rules[i];
99
+ if (rule instanceof CSSStyleRule) {
100
+ if (prefixRegex.test(rule.selectorText)) {
101
+ const css = rule.cssText.replace(prefixRegex, `:host($1) `);
102
+ output.push(css);
103
+ }
104
+ } else if (rule instanceof CSSMediaRule) {
105
+ output.push(`\n@media ${rule.conditionText} {\n`);
106
+ copyStyles(rule.cssRules, prefixRegex, output);
107
+ output.push('\n}\n');
108
+ } else if (rule instanceof CSSFontFaceRule) {
109
+ // not allowed in constructed stylesheets
110
+ } else {
111
+ output.push(rule.cssText);
112
+ }
113
+ }
114
+ return output;
115
+ }
116
+
117
+ /**
118
+ * Get a list of selectors, which could match the given component.
119
+ * This will return the local name of the component, a selector for the id
120
+ * and all classes assigned.
121
+ *
122
+ * @param {HTMLElement} component the web component
123
+ * @returns {string[]} list of selectors
124
+ */
125
+ function getSelectors(component) {
126
+ const prefixes = [component.localName];
127
+ if (component.id) {
128
+ prefixes.push(`#${component.id}`);
129
+ }
130
+ component.classList.forEach((cls) => prefixes.push(`.${cls}`));
131
+ return prefixes;
132
+ }
133
+
134
+ /**
135
+ * Implements support for injecting user-defined styles into a web component's shadow DOM.
136
+ * Styles will be copied from the global component theme CSS imported by `pb-page`
137
+ * (see `theme` property on `pb-page`)
138
+ */
139
+ export const themableMixin = (superclass) => class ThemableMixin extends superclass {
140
+
141
+ connectedCallback() {
142
+ super.connectedCallback();
143
+ waitOnce('pb-page-ready', (options) => {
144
+ const theme = importStyles(this);
145
+ if (theme) {
146
+ this.shadowRoot.adoptedStyleSheets = [...this.shadowRoot.adoptedStyleSheets, theme];
147
+ }
148
+ });
149
+ }
150
+ };
package/src/utils.js CHANGED
@@ -1,52 +1,51 @@
1
-
2
- export function resolveURL(relPath) {
3
- const src = document.querySelector('script[src*=pb-components]');
4
- if (src) {
5
- return new URL(relPath, src.src).href;
6
- }
7
- return new URL(relPath, window.location.href).href;
8
- }
9
-
10
- export function cmpVersion(a, b) {
11
- var i, cmp, len;
12
- a = (a + '').split('.');
13
- b = (b + '').split('.');
14
- len = Math.max(a.length, b.length);
15
- for (i = 0; i < len; i++) {
16
- if (a[i] === undefined) {
17
- a[i] = '0';
18
- }
19
- if (b[i] === undefined) {
20
- b[i] = '0';
21
- }
22
- cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
23
- if (cmp !== 0) {
24
- return (cmp < 0 ? -1 : 1);
25
- }
26
- }
27
- return 0;
28
- }
29
-
30
- export function minVersion(given, required) {
31
- return cmpVersion(given, required) >= 0;
32
- }
33
-
34
- /**
35
- * Retrieve value of a CSS property.
36
- *
37
- * @param {Element} elem the component
38
- * @param {string} name name of the property
39
- * @param {any} defaultValue default value
40
- * @returns the value or defaultValue if it does not exist
41
- */
42
- export function getCSSProperty(elem, name, defaultValue) {
43
- const property = getComputedStyle(elem).getPropertyValue(name);
44
- if (property) {
45
- try {
46
- return JSON.parse(property);
47
- } catch (e) {
48
- return defaultValue;
49
- }
50
- }
51
- return defaultValue;
1
+ export function resolveURL(relPath) {
2
+ const src = document.querySelector('script[src*=pb-components]');
3
+ if (src) {
4
+ return new URL(relPath, src.src).href;
5
+ }
6
+ return new URL(relPath, window.location.href).href;
7
+ }
8
+
9
+ export function cmpVersion(a, b) {
10
+ var i, cmp, len;
11
+ a = (a + '').split('.');
12
+ b = (b + '').split('.');
13
+ len = Math.max(a.length, b.length);
14
+ for (i = 0; i < len; i++) {
15
+ if (a[i] === undefined) {
16
+ a[i] = '0';
17
+ }
18
+ if (b[i] === undefined) {
19
+ b[i] = '0';
20
+ }
21
+ cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
22
+ if (cmp !== 0) {
23
+ return (cmp < 0 ? -1 : 1);
24
+ }
25
+ }
26
+ return 0;
27
+ }
28
+
29
+ export function minVersion(given, required) {
30
+ return cmpVersion(given, required) >= 0;
31
+ }
32
+
33
+ /**
34
+ * Retrieve value of a CSS property.
35
+ *
36
+ * @param {Element} elem the component
37
+ * @param {string} name name of the property
38
+ * @param {any} defaultValue default value
39
+ * @returns the value or defaultValue if it does not exist
40
+ */
41
+ export function getCSSProperty(elem, name, defaultValue) {
42
+ const property = getComputedStyle(elem).getPropertyValue(name);
43
+ if (property) {
44
+ try {
45
+ return JSON.parse(property);
46
+ } catch (e) {
47
+ return defaultValue;
48
+ }
49
+ }
50
+ return defaultValue;
52
51
  }