@empathyco/x-archetype-utils 1.0.0-alpha.1 → 1.0.0-alpha.3
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/cjs/build/rollup/rollup.config.js +16 -0
- package/dist/cjs/css-injector/css-injector.js +83 -0
- package/dist/cjs/index.js +5 -0
- package/dist/esm/build/rollup/rollup.config.js +12 -0
- package/dist/esm/css-injector/css-injector.js +78 -0
- package/dist/esm/index.js +2 -0
- package/dist/types/__tests__/css-injector.spec.d.ts +1 -0
- package/dist/types/build/rollup/rollup.config.d.ts +9 -0
- package/dist/types/css-injector/css-injector.d.ts +47 -0
- package/dist/types/css-injector/css-injector.types.d.ts +15 -0
- package/dist/types/index.d.ts +3 -0
- package/package.json +5 -4
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const rollupCssInjectorConfig = {
|
|
6
|
+
replace: {
|
|
7
|
+
// Replace X CSS injector by our custom one.
|
|
8
|
+
'addStyle(id, style);': 'window.xCSSInjector.addStyle(style);',
|
|
9
|
+
delimiters: ['', '']
|
|
10
|
+
},
|
|
11
|
+
styles: {
|
|
12
|
+
mode: ['inject', (varname) => `window.xCSSInjector.addStyle({ source: ${varname} });`]
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
exports.rollupCssInjectorConfig = rollupCssInjectorConfig;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Instance of the injector that will be used across all the initializations.
|
|
7
|
+
*/
|
|
8
|
+
let instance = null;
|
|
9
|
+
/**
|
|
10
|
+
* Custom CSS injector that allows to inject styles into a host element.
|
|
11
|
+
*
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
class CssInjector {
|
|
15
|
+
/**
|
|
16
|
+
* Initializes the instance of the injector if it's not already initialized and sets it in the
|
|
17
|
+
* window object if it's required.
|
|
18
|
+
*
|
|
19
|
+
* @param setInWindow - Whether to set the injector instance in the window object.
|
|
20
|
+
*/
|
|
21
|
+
constructor(setInWindow = true) {
|
|
22
|
+
this.stylesQueue = [];
|
|
23
|
+
if (!(instance instanceof CssInjector)) {
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
25
|
+
instance = this;
|
|
26
|
+
}
|
|
27
|
+
if (setInWindow) {
|
|
28
|
+
this.setInWindow();
|
|
29
|
+
}
|
|
30
|
+
return instance;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Adds the styles to the host element.
|
|
34
|
+
*
|
|
35
|
+
* @param styles - The styles to be added.
|
|
36
|
+
*/
|
|
37
|
+
addStyle(styles) {
|
|
38
|
+
this.stylesQueue.push(styles.source);
|
|
39
|
+
if (this.host) {
|
|
40
|
+
this.stylesQueue.forEach(styles => {
|
|
41
|
+
const styleTag = document.createElement('style');
|
|
42
|
+
styleTag.textContent = styles;
|
|
43
|
+
this.host.appendChild(styleTag);
|
|
44
|
+
});
|
|
45
|
+
this.stylesQueue = [];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sets the host element.
|
|
50
|
+
*
|
|
51
|
+
* @param host - The host element.
|
|
52
|
+
*/
|
|
53
|
+
setHost(host) {
|
|
54
|
+
this.host = host;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Sets the injector instance in the window object.
|
|
58
|
+
*/
|
|
59
|
+
setInWindow() {
|
|
60
|
+
if (typeof window !== 'undefined' && instance) {
|
|
61
|
+
window.xCSSInjector = instance;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Checks if the injector instance is in the window object.
|
|
66
|
+
*
|
|
67
|
+
* @returns Whether the injector instance is in the window object.
|
|
68
|
+
*/
|
|
69
|
+
isInWindow() {
|
|
70
|
+
return typeof window === 'undefined'
|
|
71
|
+
? false
|
|
72
|
+
: window.xCSSInjector === instance;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Instance of the injector.
|
|
77
|
+
*
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
const cssInjector = new CssInjector(typeof window !== 'undefined');
|
|
81
|
+
|
|
82
|
+
exports.CssInjector = CssInjector;
|
|
83
|
+
exports.cssInjector = cssInjector;
|
package/dist/cjs/index.js
CHANGED
|
@@ -4,8 +4,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var i18n_plugin = require('./i18n/i18n.plugin.js');
|
|
6
6
|
var webpack_config = require('./build/webpack/webpack.config.js');
|
|
7
|
+
var rollup_config = require('./build/rollup/rollup.config.js');
|
|
8
|
+
var cssInjector = require('./css-injector/css-injector.js');
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
|
|
10
12
|
exports.I18n = i18n_plugin.I18n;
|
|
11
13
|
exports.webpackConfig = webpack_config.webpackConfig;
|
|
14
|
+
exports.rollupCssInjectorConfig = rollup_config.rollupCssInjectorConfig;
|
|
15
|
+
exports.CssInjector = cssInjector.CssInjector;
|
|
16
|
+
exports.cssInjector = cssInjector.cssInjector;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const rollupCssInjectorConfig = {
|
|
2
|
+
replace: {
|
|
3
|
+
// Replace X CSS injector by our custom one.
|
|
4
|
+
'addStyle(id, style);': 'window.xCSSInjector.addStyle(style);',
|
|
5
|
+
delimiters: ['', '']
|
|
6
|
+
},
|
|
7
|
+
styles: {
|
|
8
|
+
mode: ['inject', (varname) => `window.xCSSInjector.addStyle({ source: ${varname} });`]
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export { rollupCssInjectorConfig };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instance of the injector that will be used across all the initializations.
|
|
3
|
+
*/
|
|
4
|
+
let instance = null;
|
|
5
|
+
/**
|
|
6
|
+
* Custom CSS injector that allows to inject styles into a host element.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
class CssInjector {
|
|
11
|
+
/**
|
|
12
|
+
* Initializes the instance of the injector if it's not already initialized and sets it in the
|
|
13
|
+
* window object if it's required.
|
|
14
|
+
*
|
|
15
|
+
* @param setInWindow - Whether to set the injector instance in the window object.
|
|
16
|
+
*/
|
|
17
|
+
constructor(setInWindow = true) {
|
|
18
|
+
this.stylesQueue = [];
|
|
19
|
+
if (!(instance instanceof CssInjector)) {
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
21
|
+
instance = this;
|
|
22
|
+
}
|
|
23
|
+
if (setInWindow) {
|
|
24
|
+
this.setInWindow();
|
|
25
|
+
}
|
|
26
|
+
return instance;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Adds the styles to the host element.
|
|
30
|
+
*
|
|
31
|
+
* @param styles - The styles to be added.
|
|
32
|
+
*/
|
|
33
|
+
addStyle(styles) {
|
|
34
|
+
this.stylesQueue.push(styles.source);
|
|
35
|
+
if (this.host) {
|
|
36
|
+
this.stylesQueue.forEach(styles => {
|
|
37
|
+
const styleTag = document.createElement('style');
|
|
38
|
+
styleTag.textContent = styles;
|
|
39
|
+
this.host.appendChild(styleTag);
|
|
40
|
+
});
|
|
41
|
+
this.stylesQueue = [];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Sets the host element.
|
|
46
|
+
*
|
|
47
|
+
* @param host - The host element.
|
|
48
|
+
*/
|
|
49
|
+
setHost(host) {
|
|
50
|
+
this.host = host;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Sets the injector instance in the window object.
|
|
54
|
+
*/
|
|
55
|
+
setInWindow() {
|
|
56
|
+
if (typeof window !== 'undefined' && instance) {
|
|
57
|
+
window.xCSSInjector = instance;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Checks if the injector instance is in the window object.
|
|
62
|
+
*
|
|
63
|
+
* @returns Whether the injector instance is in the window object.
|
|
64
|
+
*/
|
|
65
|
+
isInWindow() {
|
|
66
|
+
return typeof window === 'undefined'
|
|
67
|
+
? false
|
|
68
|
+
: window.xCSSInjector === instance;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Instance of the injector.
|
|
73
|
+
*
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
76
|
+
const cssInjector = new CssInjector(typeof window !== 'undefined');
|
|
77
|
+
|
|
78
|
+
export { CssInjector, cssInjector };
|
package/dist/esm/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { XCSSInjector } from './css-injector.types';
|
|
2
|
+
/**
|
|
3
|
+
* Custom CSS injector that allows to inject styles into a host element.
|
|
4
|
+
*
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare class CssInjector implements XCSSInjector {
|
|
8
|
+
protected host: Element | ShadowRoot;
|
|
9
|
+
protected stylesQueue: string[];
|
|
10
|
+
/**
|
|
11
|
+
* Initializes the instance of the injector if it's not already initialized and sets it in the
|
|
12
|
+
* window object if it's required.
|
|
13
|
+
*
|
|
14
|
+
* @param setInWindow - Whether to set the injector instance in the window object.
|
|
15
|
+
*/
|
|
16
|
+
constructor(setInWindow?: boolean);
|
|
17
|
+
/**
|
|
18
|
+
* Adds the styles to the host element.
|
|
19
|
+
*
|
|
20
|
+
* @param styles - The styles to be added.
|
|
21
|
+
*/
|
|
22
|
+
addStyle(styles: {
|
|
23
|
+
source: string;
|
|
24
|
+
}): void;
|
|
25
|
+
/**
|
|
26
|
+
* Sets the host element.
|
|
27
|
+
*
|
|
28
|
+
* @param host - The host element.
|
|
29
|
+
*/
|
|
30
|
+
setHost(host: Element | ShadowRoot): void;
|
|
31
|
+
/**
|
|
32
|
+
* Sets the injector instance in the window object.
|
|
33
|
+
*/
|
|
34
|
+
setInWindow(): void;
|
|
35
|
+
/**
|
|
36
|
+
* Checks if the injector instance is in the window object.
|
|
37
|
+
*
|
|
38
|
+
* @returns Whether the injector instance is in the window object.
|
|
39
|
+
*/
|
|
40
|
+
isInWindow(): boolean;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Instance of the injector.
|
|
44
|
+
*
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
export declare const cssInjector: CssInjector;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface XCSSInjector {
|
|
2
|
+
/** Function that will add the styles to the host. */
|
|
3
|
+
addStyle: (styles: {
|
|
4
|
+
source: string;
|
|
5
|
+
}) => void;
|
|
6
|
+
/** Function setting the host for the injector. */
|
|
7
|
+
setHost: (el: Element | ShadowRoot) => void;
|
|
8
|
+
/** Set injector instance in the window object. */
|
|
9
|
+
setInWindow: () => void;
|
|
10
|
+
/** Check if the instance is set in the window object. */
|
|
11
|
+
isInWindow: () => boolean;
|
|
12
|
+
}
|
|
13
|
+
export type WindowWithInjector = Window & {
|
|
14
|
+
xCSSInjector?: XCSSInjector;
|
|
15
|
+
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export * from './i18n/i18n.plugin';
|
|
2
2
|
export * from './i18n/i18n.types';
|
|
3
3
|
export * from './build/webpack/webpack.config';
|
|
4
|
+
export * from './build/rollup/rollup.config';
|
|
5
|
+
export * from './css-injector/css-injector';
|
|
6
|
+
export * from './css-injector/css-injector.types';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@empathyco/x-archetype-utils",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.3",
|
|
4
4
|
"description": "Utilities for x-archetype",
|
|
5
5
|
"author": "Empathy Systems Corporation S.L.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -32,15 +32,16 @@
|
|
|
32
32
|
"postbuild": "pnpm pack",
|
|
33
33
|
"lint": "eslint . --ext .ts",
|
|
34
34
|
"test": "jest",
|
|
35
|
+
"test:unit": "jest",
|
|
35
36
|
"prepublishOnly": "pnpm run build"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
|
-
"@empathyco/x-deep-merge": "^2.0.0-alpha.
|
|
39
|
+
"@empathyco/x-deep-merge": "^2.0.0-alpha.2",
|
|
39
40
|
"tslib": "~2.5.0",
|
|
40
41
|
"vue-i18n": "~8.28.2"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
|
-
"@empathyco/x-utils": "^1.0.0-alpha.
|
|
44
|
+
"@empathyco/x-utils": "^1.0.0-alpha.22",
|
|
44
45
|
"@types/jest": "~27.5.0",
|
|
45
46
|
"@types/node": "~18.16.0",
|
|
46
47
|
"@vue/test-utils": "~1.0.3",
|
|
@@ -61,5 +62,5 @@
|
|
|
61
62
|
"publishConfig": {
|
|
62
63
|
"access": "public"
|
|
63
64
|
},
|
|
64
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "6b4faad31c4e92997ea893ad9d2c470f4a708ce0"
|
|
65
66
|
}
|