@taole/dev-helper 0.0.8 → 0.0.9
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/Tdiv/index.js +14 -2
- package/Tdiv/setting.js +22 -0
- package/dist/tdiv.umd.js +312 -0
- package/package.json +9 -4
package/Tdiv/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import setting from "./setting.js";
|
|
2
|
-
|
|
2
|
+
import { updateConfig, setViewportWidth, setViewportUnit, setUsePx, setUseButtonEffect, subscribe } from "./setting.js";
|
|
3
3
|
|
|
4
4
|
const styleText = `
|
|
5
5
|
.tw-img-div {
|
|
@@ -51,6 +51,7 @@ class Tdiv extends HTMLElement {
|
|
|
51
51
|
this._onClickHandler = this._onClickHandler.bind(this);
|
|
52
52
|
this._connected = false;
|
|
53
53
|
this._iscalcstyle = false;
|
|
54
|
+
this._unsubscribe = null;
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
connectedCallback() {
|
|
@@ -58,11 +59,16 @@ class Tdiv extends HTMLElement {
|
|
|
58
59
|
this._upgradeProperties();
|
|
59
60
|
this._update();
|
|
60
61
|
this.addEventListener('click', this._onClickHandler);
|
|
62
|
+
this._unsubscribe = subscribe(this._update.bind(this));
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
disconnectedCallback() {
|
|
64
66
|
this._connected = false;
|
|
65
67
|
this.removeEventListener('click', this._onClickHandler);
|
|
68
|
+
if (this._unsubscribe) {
|
|
69
|
+
this._unsubscribe();
|
|
70
|
+
this._unsubscribe = null;
|
|
71
|
+
}
|
|
66
72
|
}
|
|
67
73
|
|
|
68
74
|
attributeChangedCallback() {
|
|
@@ -200,7 +206,7 @@ class Tdiv extends HTMLElement {
|
|
|
200
206
|
|
|
201
207
|
|
|
202
208
|
let isRegister = false;
|
|
203
|
-
|
|
209
|
+
function registerTdiv() {
|
|
204
210
|
if (!isRegister) {
|
|
205
211
|
customElements.define('t-div', Tdiv);
|
|
206
212
|
// append style for t-div
|
|
@@ -209,4 +215,10 @@ export function registerTdiv() {
|
|
|
209
215
|
document.head.appendChild(style);
|
|
210
216
|
isRegister = true;
|
|
211
217
|
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
registerTdiv();
|
|
221
|
+
|
|
222
|
+
export default {
|
|
223
|
+
updateConfig, setViewportWidth, setViewportUnit, setUsePx, setUseButtonEffect
|
|
212
224
|
}
|
package/Tdiv/setting.js
CHANGED
|
@@ -17,12 +17,30 @@ const config = {
|
|
|
17
17
|
useButtonEffect: false,
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
let subscribers = [];
|
|
21
|
+
/**
|
|
22
|
+
* 订阅配置变化
|
|
23
|
+
* @param {Function} callback
|
|
24
|
+
*/
|
|
25
|
+
export function subscribe(callback) {
|
|
26
|
+
subscribers.push(callback);
|
|
27
|
+
return () => {
|
|
28
|
+
subscribers = subscribers.filter(cb => cb !== callback);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function notify() {
|
|
33
|
+
subscribers.forEach(cb => cb(config));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
20
37
|
/**
|
|
21
38
|
* 设置视口宽度
|
|
22
39
|
* @param {number} width
|
|
23
40
|
*/
|
|
24
41
|
export function setViewportWidth(width) {
|
|
25
42
|
config.viewportWidth = width;
|
|
43
|
+
notify();
|
|
26
44
|
}
|
|
27
45
|
|
|
28
46
|
/**
|
|
@@ -31,6 +49,7 @@ export function setViewportWidth(width) {
|
|
|
31
49
|
*/
|
|
32
50
|
export function setViewportUnit(unit) {
|
|
33
51
|
config.viewportUnit = unit;
|
|
52
|
+
notify();
|
|
34
53
|
}
|
|
35
54
|
|
|
36
55
|
/**
|
|
@@ -39,6 +58,7 @@ export function setViewportUnit(unit) {
|
|
|
39
58
|
*/
|
|
40
59
|
export function setUsePx(use) {
|
|
41
60
|
config.usePx = use;
|
|
61
|
+
notify();
|
|
42
62
|
}
|
|
43
63
|
|
|
44
64
|
/**
|
|
@@ -47,6 +67,7 @@ export function setUsePx(use) {
|
|
|
47
67
|
*/
|
|
48
68
|
export function setUseButtonEffect(use) {
|
|
49
69
|
config.useButtonEffect = use;
|
|
70
|
+
notify();
|
|
50
71
|
}
|
|
51
72
|
|
|
52
73
|
/**
|
|
@@ -55,6 +76,7 @@ export function setUseButtonEffect(use) {
|
|
|
55
76
|
*/
|
|
56
77
|
export function updateConfig(newConfig) {
|
|
57
78
|
Object.assign(config, newConfig);
|
|
79
|
+
notify();
|
|
58
80
|
}
|
|
59
81
|
|
|
60
82
|
export default config;
|
package/dist/tdiv.umd.js
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Tdiv = factory());
|
|
5
|
+
})(this, (function () { 'use strict';
|
|
6
|
+
|
|
7
|
+
const config = {
|
|
8
|
+
/**
|
|
9
|
+
* 设置视口宽度
|
|
10
|
+
*/
|
|
11
|
+
viewportWidth: 750,
|
|
12
|
+
/**
|
|
13
|
+
* 设置视口单位
|
|
14
|
+
*/
|
|
15
|
+
viewportUnit: "vw",
|
|
16
|
+
/**
|
|
17
|
+
* 设置是否使用px
|
|
18
|
+
*/
|
|
19
|
+
usePx: false,
|
|
20
|
+
/**
|
|
21
|
+
* 设置是否使用按钮效果(指按钮的点击效果)
|
|
22
|
+
*/
|
|
23
|
+
useButtonEffect: false,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
let subscribers = [];
|
|
27
|
+
/**
|
|
28
|
+
* 订阅配置变化
|
|
29
|
+
* @param {Function} callback
|
|
30
|
+
*/
|
|
31
|
+
function subscribe(callback) {
|
|
32
|
+
subscribers.push(callback);
|
|
33
|
+
return () => {
|
|
34
|
+
subscribers = subscribers.filter(cb => cb !== callback);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function notify() {
|
|
39
|
+
subscribers.forEach(cb => cb(config));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 设置视口宽度
|
|
45
|
+
* @param {number} width
|
|
46
|
+
*/
|
|
47
|
+
function setViewportWidth(width) {
|
|
48
|
+
config.viewportWidth = width;
|
|
49
|
+
notify();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 设置视口单位
|
|
54
|
+
* @param {string} unit
|
|
55
|
+
*/
|
|
56
|
+
function setViewportUnit(unit) {
|
|
57
|
+
config.viewportUnit = unit;
|
|
58
|
+
notify();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 设置是否使用px
|
|
63
|
+
* @param {boolean} use
|
|
64
|
+
*/
|
|
65
|
+
function setUsePx(use) {
|
|
66
|
+
config.usePx = use;
|
|
67
|
+
notify();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 设置是否使用按钮效果(指按钮的点击效果)
|
|
72
|
+
* @param {boolean} use
|
|
73
|
+
*/
|
|
74
|
+
function setUseButtonEffect(use) {
|
|
75
|
+
config.useButtonEffect = use;
|
|
76
|
+
notify();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 更新配置
|
|
81
|
+
* @param {Partial<typeof config>} newConfig
|
|
82
|
+
*/
|
|
83
|
+
function updateConfig(newConfig) {
|
|
84
|
+
Object.assign(config, newConfig);
|
|
85
|
+
notify();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const styleText = `
|
|
89
|
+
.tw-img-div {
|
|
90
|
+
display: block;
|
|
91
|
+
background-position: top center;
|
|
92
|
+
background-repeat: no-repeat;
|
|
93
|
+
background-size: 100% auto;
|
|
94
|
+
flex: none;
|
|
95
|
+
}
|
|
96
|
+
.click-effect:active {
|
|
97
|
+
opacity: 0.85;
|
|
98
|
+
transform: translateY(1px);
|
|
99
|
+
}`;
|
|
100
|
+
|
|
101
|
+
function hasValue(value) {
|
|
102
|
+
return value !== null && value !== undefined && value !== '';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function calc(value, { usePx, viewportWidth, viewportUnit }) {
|
|
106
|
+
let vWidth = config.viewportWidth;
|
|
107
|
+
if (viewportWidth) vWidth = viewportWidth;
|
|
108
|
+
let vUnit = config.viewportUnit;
|
|
109
|
+
if (viewportUnit) vUnit = viewportUnit;
|
|
110
|
+
let localUsePx = config.usePx;
|
|
111
|
+
if (typeof usePx === 'boolean') localUsePx = usePx;
|
|
112
|
+
|
|
113
|
+
// console.log("setting", setting, localUsePx);
|
|
114
|
+
|
|
115
|
+
if (typeof value === 'string' && !isNaN(value)) value = Number(value);
|
|
116
|
+
if (typeof value === 'number') {
|
|
117
|
+
if (localUsePx) return value + 'px';
|
|
118
|
+
let val = ((value / vWidth) * 100).toFixed(4);
|
|
119
|
+
return `${val}${vUnit}`;
|
|
120
|
+
}
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
class Tdiv extends HTMLElement {
|
|
125
|
+
static get observedAttributes() {
|
|
126
|
+
return [
|
|
127
|
+
'_class', 'usePx', '_style', 'src', 'width', 'height', 'absolute',
|
|
128
|
+
'top', 'left', 'right', 'bottom', 'zIndex', 'pointer', 'flex', 'column', 'center',
|
|
129
|
+
'text', 'color', 'srcMode', 'onClick', 'debug', 'viewportWidth', 'viewportUnit'
|
|
130
|
+
];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
constructor() {
|
|
134
|
+
super();
|
|
135
|
+
this._onClickHandler = this._onClickHandler.bind(this);
|
|
136
|
+
this._connected = false;
|
|
137
|
+
this._iscalcstyle = false;
|
|
138
|
+
this._unsubscribe = null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
connectedCallback() {
|
|
142
|
+
this._connected = true;
|
|
143
|
+
this._upgradeProperties();
|
|
144
|
+
this._update();
|
|
145
|
+
this.addEventListener('click', this._onClickHandler);
|
|
146
|
+
this._unsubscribe = subscribe(this._update.bind(this));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
disconnectedCallback() {
|
|
150
|
+
this._connected = false;
|
|
151
|
+
this.removeEventListener('click', this._onClickHandler);
|
|
152
|
+
if (this._unsubscribe) {
|
|
153
|
+
this._unsubscribe();
|
|
154
|
+
this._unsubscribe = null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
attributeChangedCallback() {
|
|
159
|
+
if (!this._connected) return;
|
|
160
|
+
this._update();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
_upgradeProperties() {
|
|
164
|
+
// For properties set before upgrade
|
|
165
|
+
Tdiv.observedAttributes.forEach(attr => {
|
|
166
|
+
if (this.hasOwnProperty(attr)) {
|
|
167
|
+
let value = this[attr];
|
|
168
|
+
delete this[attr];
|
|
169
|
+
this[attr] = value;
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
_getAttr(name, type = 'string') {
|
|
175
|
+
if (!this.hasAttribute(name)) return null;
|
|
176
|
+
let val = this.getAttribute(name);
|
|
177
|
+
if (type === 'boolean') return val === '' || val === 'true';
|
|
178
|
+
if (type === 'number') return Number(val);
|
|
179
|
+
return val;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
_update() {
|
|
183
|
+
if (this._iscalcstyle) return;
|
|
184
|
+
this._iscalcstyle = true;
|
|
185
|
+
// console.log('update');
|
|
186
|
+
// 读取所有属性
|
|
187
|
+
const _class = this._getAttr('_class') || '';
|
|
188
|
+
const usePx = this._getAttr('usePx', 'boolean');
|
|
189
|
+
const styleAttr = this._getAttr('_style') || '';
|
|
190
|
+
const src = this._getAttr('src') || '';
|
|
191
|
+
const width = this._getAttr('width');
|
|
192
|
+
const height = this._getAttr('height');
|
|
193
|
+
const absolute = this._getAttr('absolute', 'boolean');
|
|
194
|
+
const top = this._getAttr('top');
|
|
195
|
+
const left = this._getAttr('left');
|
|
196
|
+
const right = this._getAttr('right');
|
|
197
|
+
const bottom = this._getAttr('bottom');
|
|
198
|
+
const zIndex = this._getAttr('zIndex');
|
|
199
|
+
const pointer = this._getAttr('pointer', 'boolean');
|
|
200
|
+
const flex = this._getAttr('flex', 'boolean');
|
|
201
|
+
const column = this._getAttr('column', 'boolean');
|
|
202
|
+
const center = this._getAttr('center', 'boolean');
|
|
203
|
+
const text = this._getAttr('text');
|
|
204
|
+
const color = this._getAttr('color');
|
|
205
|
+
const srcMode = this._getAttr('srcMode') || 'auto';
|
|
206
|
+
const debug = this._getAttr('debug', 'boolean');
|
|
207
|
+
const viewportWidth = this._getAttr('viewportWidth', 'number');
|
|
208
|
+
const viewportUnit = this._getAttr('viewportUnit');
|
|
209
|
+
// class
|
|
210
|
+
let extraClass = ['tw-img-div'];
|
|
211
|
+
if (_class) extraClass.push(_class);
|
|
212
|
+
if (config.useButtonEffect && this._getAttr('onClick')) extraClass.push('click-effect');
|
|
213
|
+
// 分组样式
|
|
214
|
+
const calcOpt = { usePx, viewportWidth, viewportUnit };
|
|
215
|
+
let layoutStyle = '';
|
|
216
|
+
// 宽度
|
|
217
|
+
if (hasValue(width)) {
|
|
218
|
+
const widthStyle = `width: ${calc(width, calcOpt)};`;
|
|
219
|
+
layoutStyle += widthStyle;
|
|
220
|
+
}
|
|
221
|
+
if (hasValue(height)) {
|
|
222
|
+
const heightStyle = `height: ${calc(height, calcOpt)};`;
|
|
223
|
+
layoutStyle += heightStyle;
|
|
224
|
+
}
|
|
225
|
+
if (absolute) {
|
|
226
|
+
layoutStyle += 'position: absolute;';
|
|
227
|
+
if (hasValue(left)) layoutStyle += `left: ${calc(left, calcOpt)};`;
|
|
228
|
+
if (hasValue(top)) layoutStyle += `top: ${calc(top, calcOpt)};`;
|
|
229
|
+
if (hasValue(right)) layoutStyle += `right: ${calc(right, calcOpt)};`;
|
|
230
|
+
if (hasValue(bottom)) layoutStyle += `bottom: ${calc(bottom, calcOpt)};`;
|
|
231
|
+
} else {
|
|
232
|
+
layoutStyle += 'position: relative;';
|
|
233
|
+
if (hasValue(top)) layoutStyle += `margin-top: ${calc(top, calcOpt)};`;
|
|
234
|
+
if (hasValue(left)) layoutStyle += `margin-left: ${calc(left, calcOpt)};`;
|
|
235
|
+
if (hasValue(right)) layoutStyle += `margin-right: ${calc(right, calcOpt)};`;
|
|
236
|
+
if (hasValue(bottom)) layoutStyle += `margin-bottom: ${calc(bottom, calcOpt)};`;
|
|
237
|
+
}
|
|
238
|
+
if (hasValue(zIndex)) layoutStyle += `z-index: ${zIndex};`;
|
|
239
|
+
if (pointer === true) {
|
|
240
|
+
layoutStyle += 'cursor: pointer;';
|
|
241
|
+
} else if (this._getAttr('onClick') && pointer === false) {
|
|
242
|
+
layoutStyle += 'cursor: pointer;';
|
|
243
|
+
}
|
|
244
|
+
if (flex) {
|
|
245
|
+
layoutStyle += 'display:flex;';
|
|
246
|
+
if (column) layoutStyle += 'flex-direction: column;';
|
|
247
|
+
if (center) {
|
|
248
|
+
layoutStyle += 'justify-content: center;';
|
|
249
|
+
layoutStyle += 'align-items: center;';
|
|
250
|
+
}
|
|
251
|
+
} else {
|
|
252
|
+
if (center) layoutStyle += 'text-align: center;';
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (text) layoutStyle += `font-size: ${calc(text, calcOpt)};`;
|
|
256
|
+
if (src) {
|
|
257
|
+
let realSrc = src;
|
|
258
|
+
if (src.indexOf('https://') === -1) {
|
|
259
|
+
realSrc = `https://dynamics-share.tuwan.com/${src}?x-oss-process=image/format,webp/ignore-error,1`;
|
|
260
|
+
}
|
|
261
|
+
layoutStyle += `background-image: url(${realSrc});`;
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
if (srcMode === 'fill') {
|
|
265
|
+
layoutStyle += 'background-size: 100% 100%;';
|
|
266
|
+
} else if (srcMode === 'cover') {
|
|
267
|
+
layoutStyle += 'background-size: cover;';
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (color) layoutStyle += `color: ${color};`;
|
|
271
|
+
if (styleAttr) layoutStyle += styleAttr;
|
|
272
|
+
if (debug) {
|
|
273
|
+
console.log('layoutStyle', layoutStyle);
|
|
274
|
+
}
|
|
275
|
+
const currentList = Array.from(this.classList);
|
|
276
|
+
const classNeeded = extraClass.filter(item => !currentList.includes(item));
|
|
277
|
+
this.classList.add(...classNeeded);
|
|
278
|
+
this.style.cssText = layoutStyle;
|
|
279
|
+
this._iscalcstyle = false;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
_onClickHandler(e) {
|
|
283
|
+
// 支持onClick属性(字符串,window作用域下查找)
|
|
284
|
+
const onClickAttr = this._getAttr('onClick');
|
|
285
|
+
if (onClickAttr && typeof window[onClickAttr] === 'function') {
|
|
286
|
+
window[onClickAttr](e);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
let isRegister = false;
|
|
293
|
+
function registerTdiv() {
|
|
294
|
+
if (!isRegister) {
|
|
295
|
+
customElements.define('t-div', Tdiv);
|
|
296
|
+
// append style for t-div
|
|
297
|
+
const style = document.createElement('style');
|
|
298
|
+
style.textContent = styleText;
|
|
299
|
+
document.head.appendChild(style);
|
|
300
|
+
isRegister = true;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
registerTdiv();
|
|
305
|
+
|
|
306
|
+
var index = {
|
|
307
|
+
updateConfig, setViewportWidth, setViewportUnit, setUsePx, setUseButtonEffect
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
return index;
|
|
311
|
+
|
|
312
|
+
}));
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taole/dev-helper",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
|
+
"build": "npx -y rimraf dist && rollup -c",
|
|
6
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
8
|
},
|
|
8
9
|
"keywords": [],
|
|
@@ -10,10 +11,14 @@
|
|
|
10
11
|
"access": "public",
|
|
11
12
|
"registry": "https://registry.npmjs.org/"
|
|
12
13
|
},
|
|
13
|
-
"files":[
|
|
14
|
-
"Tdiv"
|
|
14
|
+
"files": [
|
|
15
|
+
"Tdiv",
|
|
16
|
+
"dist"
|
|
15
17
|
],
|
|
16
18
|
"author": "",
|
|
17
19
|
"license": "ISC",
|
|
18
|
-
"description": ""
|
|
20
|
+
"description": "",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"rollup": "^4.45.1"
|
|
23
|
+
}
|
|
19
24
|
}
|