@vvi/browser 1.0.1 → 1.1.0
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/CHANGELOG.md +5 -0
- package/LICENSE +1 -1
- package/cjs/index.js +3 -0
- package/cjs/inline-style.js +103 -0
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/es/inline-style.d.ts +14 -0
- package/es/inline-style.js +100 -0
- package/package.json +14 -4
package/CHANGELOG.md
CHANGED
package/LICENSE
CHANGED
package/cjs/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var copyText = require('./copy-text.js');
|
|
|
5
5
|
var isOrientation = require('./is-orientation.js');
|
|
6
6
|
var isDark = require('./is-dark.js');
|
|
7
7
|
var storage = require('./storage.js');
|
|
8
|
+
var inlineStyle = require('./inline-style.js');
|
|
8
9
|
|
|
9
10
|
|
|
10
11
|
|
|
@@ -23,3 +24,5 @@ exports.matchMediaSome = isDark.matchMediaSome;
|
|
|
23
24
|
exports.matchMediaSomeState = isDark.matchMediaSomeState;
|
|
24
25
|
exports.matchMediaState = isDark.matchMediaState;
|
|
25
26
|
exports.Storage = storage.Storage;
|
|
27
|
+
exports.removeStyle = inlineStyle.removeStyle;
|
|
28
|
+
exports.setStyle = inlineStyle.setStyle;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var is = require('@vvi/is');
|
|
4
|
+
|
|
5
|
+
// if (Object.keys(oldStyle).length !== 0) {
|
|
6
|
+
// for (let i = 0, k = Object.keys(oldStyle), j = k.length; i < j; i++) {
|
|
7
|
+
// if (k[i].startsWith('--')) {
|
|
8
|
+
// oldStyle[i] = window
|
|
9
|
+
// .getComputedStyle(node)
|
|
10
|
+
// .getPropertyValue(oldStyle[i]); /// 处理自定义属性
|
|
11
|
+
// } else {
|
|
12
|
+
// /** @ts-expect-error: 油盐不进 */
|
|
13
|
+
// templateStyle[oldStyle[i]] = oldStyle[oldStyle[i]];
|
|
14
|
+
// }
|
|
15
|
+
// }
|
|
16
|
+
// }
|
|
17
|
+
/**
|
|
18
|
+
* ## 为节点元素设置标准样式
|
|
19
|
+
* @param node 将设置样式的元素节点
|
|
20
|
+
* @param style 将设置的新样式
|
|
21
|
+
*/
|
|
22
|
+
function setStyle(node, style) {
|
|
23
|
+
node.setAttribute('style', transformStyle({ ...getOldStyle(node), ...style }).replaceAll(/;;/gm, ';'));
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* ## 移除样式
|
|
27
|
+
* @param node 元素节点
|
|
28
|
+
* @param style 要移除的样式名称
|
|
29
|
+
*/
|
|
30
|
+
function removeStyle(node, style) {
|
|
31
|
+
node.setAttribute('style', transformStyle(getOldStyle(node), style).replaceAll(/;;/gm, ';'));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* ## 获取原有的行内样式
|
|
35
|
+
*
|
|
36
|
+
* @param node 节点元素
|
|
37
|
+
* @returns 元素的行内样式
|
|
38
|
+
*/
|
|
39
|
+
function getOldStyle(node) {
|
|
40
|
+
const oldStyle = (node.getAttribute('style') ?? '')
|
|
41
|
+
.replace(/(;{2,})/gm, ';') // 去除多余的 `;`
|
|
42
|
+
.split(';') // 按 `;` 分割为数组
|
|
43
|
+
.reduce((previousValue, currentValue) => {
|
|
44
|
+
// 使用数组方法进行累计
|
|
45
|
+
if (!is.isBusinessEmptyString(currentValue)) {
|
|
46
|
+
/** 第一个被找到的下标 */
|
|
47
|
+
const firstIndex = currentValue.indexOf(':');
|
|
48
|
+
/** css Property 属性 */
|
|
49
|
+
let property = currentValue
|
|
50
|
+
.slice(0, firstIndex) // 从元组中切出所需的属性名
|
|
51
|
+
.trim(); /// 去除多余空格
|
|
52
|
+
property = hyphenToCapitalization(property); /// 连字符转大写
|
|
53
|
+
/** css property value 属性值 */
|
|
54
|
+
const propertyValue = node.style[property];
|
|
55
|
+
if (!is.isUndefined(propertyValue))
|
|
56
|
+
previousValue[property] = propertyValue; // 配置属性
|
|
57
|
+
}
|
|
58
|
+
return previousValue;
|
|
59
|
+
}, {});
|
|
60
|
+
return oldStyle;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* ## 将对象的样式转化为 string
|
|
64
|
+
*
|
|
65
|
+
* 方便赋值
|
|
66
|
+
* @param style 待转化的样式,对象形式方便处理,字符串形式方便赋值
|
|
67
|
+
* @param removeProperty 待移除的属性,是一个字符串数组
|
|
68
|
+
* @returns 返回一个行内样式
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
function transformStyle(style, removeProperty) {
|
|
72
|
+
return Object.keys(style).reduce((previousValue, _c) => {
|
|
73
|
+
// 移除属性
|
|
74
|
+
if (removeProperty && removeProperty.includes(_c))
|
|
75
|
+
return previousValue;
|
|
76
|
+
return (previousValue + `${capitalizationToHyphen(_c)}: ${style[_c]};`);
|
|
77
|
+
}, '');
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* ## 连字符形式转大写
|
|
81
|
+
*
|
|
82
|
+
* @param property 待转化的属性字符串
|
|
83
|
+
* @returns
|
|
84
|
+
*/
|
|
85
|
+
function hyphenToCapitalization(property) {
|
|
86
|
+
return property.startsWith('--')
|
|
87
|
+
? property
|
|
88
|
+
: property.replace(/([a-z])-([a-z])/g, (_, m1, m2) => m1.concat(m2.toUpperCase()));
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* ## 大写转连字符
|
|
92
|
+
*
|
|
93
|
+
* @param property 待转化为连字符形式的字符串
|
|
94
|
+
* @returns 转换后de
|
|
95
|
+
*/
|
|
96
|
+
function capitalizationToHyphen(property) {
|
|
97
|
+
return property.startsWith('--')
|
|
98
|
+
? property
|
|
99
|
+
: property.replace(/[A-Z]/gm, letter => `-${letter.toLowerCase()}`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
exports.removeStyle = removeStyle;
|
|
103
|
+
exports.setStyle = setStyle;
|
package/es/index.d.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { copyText } from './copy-text';
|
|
|
4
4
|
export { isOrientation } from './is-orientation';
|
|
5
5
|
export { isDarkTheme, matchMediaByString, matchMediaState, matchMediaEvery, matchMediaEveryState, matchMediaSome, matchMediaSomeState, } from './is-dark';
|
|
6
6
|
export { Storage } from './storage';
|
|
7
|
+
export { setStyle, removeStyle } from './inline-style';
|
package/es/index.js
CHANGED
|
@@ -3,3 +3,4 @@ export { copyText } from './copy-text.js';
|
|
|
3
3
|
export { isOrientation } from './is-orientation.js';
|
|
4
4
|
export { isDarkTheme, matchMediaByString, matchMediaEvery, matchMediaEveryState, matchMediaSome, matchMediaSomeState, matchMediaState } from './is-dark.js';
|
|
5
5
|
export { Storage } from './storage.js';
|
|
6
|
+
export { removeStyle, setStyle } from './inline-style.js';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ## 为节点元素设置标准样式
|
|
3
|
+
* @param node 将设置样式的元素节点
|
|
4
|
+
* @param style 将设置的新样式
|
|
5
|
+
*/
|
|
6
|
+
export declare function setStyle(node: HTMLElement | HTMLInputElement, style: {
|
|
7
|
+
[x: string]: string | number;
|
|
8
|
+
}): void;
|
|
9
|
+
/**
|
|
10
|
+
* ## 移除样式
|
|
11
|
+
* @param node 元素节点
|
|
12
|
+
* @param style 要移除的样式名称
|
|
13
|
+
*/
|
|
14
|
+
export declare function removeStyle(node: HTMLElement | HTMLInputElement, style: string[]): void;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { isBusinessEmptyString, isUndefined } from '@vvi/is';
|
|
2
|
+
|
|
3
|
+
// if (Object.keys(oldStyle).length !== 0) {
|
|
4
|
+
// for (let i = 0, k = Object.keys(oldStyle), j = k.length; i < j; i++) {
|
|
5
|
+
// if (k[i].startsWith('--')) {
|
|
6
|
+
// oldStyle[i] = window
|
|
7
|
+
// .getComputedStyle(node)
|
|
8
|
+
// .getPropertyValue(oldStyle[i]); /// 处理自定义属性
|
|
9
|
+
// } else {
|
|
10
|
+
// /** @ts-expect-error: 油盐不进 */
|
|
11
|
+
// templateStyle[oldStyle[i]] = oldStyle[oldStyle[i]];
|
|
12
|
+
// }
|
|
13
|
+
// }
|
|
14
|
+
// }
|
|
15
|
+
/**
|
|
16
|
+
* ## 为节点元素设置标准样式
|
|
17
|
+
* @param node 将设置样式的元素节点
|
|
18
|
+
* @param style 将设置的新样式
|
|
19
|
+
*/
|
|
20
|
+
function setStyle(node, style) {
|
|
21
|
+
node.setAttribute('style', transformStyle({ ...getOldStyle(node), ...style }).replaceAll(/;;/gm, ';'));
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* ## 移除样式
|
|
25
|
+
* @param node 元素节点
|
|
26
|
+
* @param style 要移除的样式名称
|
|
27
|
+
*/
|
|
28
|
+
function removeStyle(node, style) {
|
|
29
|
+
node.setAttribute('style', transformStyle(getOldStyle(node), style).replaceAll(/;;/gm, ';'));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* ## 获取原有的行内样式
|
|
33
|
+
*
|
|
34
|
+
* @param node 节点元素
|
|
35
|
+
* @returns 元素的行内样式
|
|
36
|
+
*/
|
|
37
|
+
function getOldStyle(node) {
|
|
38
|
+
const oldStyle = (node.getAttribute('style') ?? '')
|
|
39
|
+
.replace(/(;{2,})/gm, ';') // 去除多余的 `;`
|
|
40
|
+
.split(';') // 按 `;` 分割为数组
|
|
41
|
+
.reduce((previousValue, currentValue) => {
|
|
42
|
+
// 使用数组方法进行累计
|
|
43
|
+
if (!isBusinessEmptyString(currentValue)) {
|
|
44
|
+
/** 第一个被找到的下标 */
|
|
45
|
+
const firstIndex = currentValue.indexOf(':');
|
|
46
|
+
/** css Property 属性 */
|
|
47
|
+
let property = currentValue
|
|
48
|
+
.slice(0, firstIndex) // 从元组中切出所需的属性名
|
|
49
|
+
.trim(); /// 去除多余空格
|
|
50
|
+
property = hyphenToCapitalization(property); /// 连字符转大写
|
|
51
|
+
/** css property value 属性值 */
|
|
52
|
+
const propertyValue = node.style[property];
|
|
53
|
+
if (!isUndefined(propertyValue))
|
|
54
|
+
previousValue[property] = propertyValue; // 配置属性
|
|
55
|
+
}
|
|
56
|
+
return previousValue;
|
|
57
|
+
}, {});
|
|
58
|
+
return oldStyle;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* ## 将对象的样式转化为 string
|
|
62
|
+
*
|
|
63
|
+
* 方便赋值
|
|
64
|
+
* @param style 待转化的样式,对象形式方便处理,字符串形式方便赋值
|
|
65
|
+
* @param removeProperty 待移除的属性,是一个字符串数组
|
|
66
|
+
* @returns 返回一个行内样式
|
|
67
|
+
*
|
|
68
|
+
*/
|
|
69
|
+
function transformStyle(style, removeProperty) {
|
|
70
|
+
return Object.keys(style).reduce((previousValue, _c) => {
|
|
71
|
+
// 移除属性
|
|
72
|
+
if (removeProperty && removeProperty.includes(_c))
|
|
73
|
+
return previousValue;
|
|
74
|
+
return (previousValue + `${capitalizationToHyphen(_c)}: ${style[_c]};`);
|
|
75
|
+
}, '');
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* ## 连字符形式转大写
|
|
79
|
+
*
|
|
80
|
+
* @param property 待转化的属性字符串
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
function hyphenToCapitalization(property) {
|
|
84
|
+
return property.startsWith('--')
|
|
85
|
+
? property
|
|
86
|
+
: property.replace(/([a-z])-([a-z])/g, (_, m1, m2) => m1.concat(m2.toUpperCase()));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* ## 大写转连字符
|
|
90
|
+
*
|
|
91
|
+
* @param property 待转化为连字符形式的字符串
|
|
92
|
+
* @returns 转换后de
|
|
93
|
+
*/
|
|
94
|
+
function capitalizationToHyphen(property) {
|
|
95
|
+
return property.startsWith('--')
|
|
96
|
+
? property
|
|
97
|
+
: property.replace(/[A-Z]/gm, letter => `-${letter.toLowerCase()}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export { removeStyle, setStyle };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vvi/browser",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"main": "cjs/index.js",
|
|
5
5
|
"types": "es/index.d.ts",
|
|
6
6
|
"author": {
|
|
@@ -49,6 +49,16 @@
|
|
|
49
49
|
"types": "./es/copy-text.d.ts"
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
|
+
"./inline-style": {
|
|
53
|
+
"import": {
|
|
54
|
+
"default": "./es/inline-style.js",
|
|
55
|
+
"types": "./es/inline-style.d.ts"
|
|
56
|
+
},
|
|
57
|
+
"require": {
|
|
58
|
+
"default": "./cjs/inline-style.js",
|
|
59
|
+
"types": "./es/inline-style.d.ts"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
52
62
|
"./is-dark": {
|
|
53
63
|
"import": {
|
|
54
64
|
"default": "./es/is-dark.js",
|
|
@@ -85,10 +95,10 @@
|
|
|
85
95
|
"vvi",
|
|
86
96
|
"mudbean"
|
|
87
97
|
],
|
|
88
|
-
"homepage": "https://
|
|
98
|
+
"homepage": "https://npms.gleanings.cn/browser",
|
|
89
99
|
"dependencies": {
|
|
90
|
-
"@vvi/is": "^2.0.
|
|
91
|
-
"@vvi/utils": "^2.1.
|
|
100
|
+
"@vvi/is": "^2.0.8",
|
|
101
|
+
"@vvi/utils": "^2.1.2"
|
|
92
102
|
},
|
|
93
103
|
"bugs": {
|
|
94
104
|
"url": "https://github.com/gleanings/browser/issues",
|