@taole/dev-helper 0.0.7 → 0.0.8
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 +212 -0
- package/Tdiv/index.svelte +7 -2
- package/package.json +1 -1
package/Tdiv/index.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import setting from "./setting.js";
|
|
2
|
+
export * from "./setting.js";
|
|
3
|
+
|
|
4
|
+
const styleText = `
|
|
5
|
+
.tw-img-div {
|
|
6
|
+
display: block;
|
|
7
|
+
background-position: top center;
|
|
8
|
+
background-repeat: no-repeat;
|
|
9
|
+
background-size: 100% auto;
|
|
10
|
+
flex: none;
|
|
11
|
+
}
|
|
12
|
+
.click-effect:active {
|
|
13
|
+
opacity: 0.85;
|
|
14
|
+
transform: translateY(1px);
|
|
15
|
+
}`;
|
|
16
|
+
|
|
17
|
+
function hasValue(value) {
|
|
18
|
+
return value !== null && value !== undefined && value !== '';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function calc(value, { usePx, viewportWidth, viewportUnit }) {
|
|
22
|
+
let vWidth = setting.viewportWidth;
|
|
23
|
+
if (viewportWidth) vWidth = viewportWidth;
|
|
24
|
+
let vUnit = setting.viewportUnit;
|
|
25
|
+
if (viewportUnit) vUnit = viewportUnit;
|
|
26
|
+
let localUsePx = setting.usePx;
|
|
27
|
+
if (typeof usePx === 'boolean') localUsePx = usePx;
|
|
28
|
+
|
|
29
|
+
// console.log("setting", setting, localUsePx);
|
|
30
|
+
|
|
31
|
+
if (typeof value === 'string' && !isNaN(value)) value = Number(value);
|
|
32
|
+
if (typeof value === 'number') {
|
|
33
|
+
if (localUsePx) return value + 'px';
|
|
34
|
+
let val = ((value / vWidth) * 100).toFixed(4);
|
|
35
|
+
return `${val}${vUnit}`;
|
|
36
|
+
}
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class Tdiv extends HTMLElement {
|
|
41
|
+
static get observedAttributes() {
|
|
42
|
+
return [
|
|
43
|
+
'_class', 'usePx', '_style', 'src', 'width', 'height', 'absolute',
|
|
44
|
+
'top', 'left', 'right', 'bottom', 'zIndex', 'pointer', 'flex', 'column', 'center',
|
|
45
|
+
'text', 'color', 'srcMode', 'onClick', 'debug', 'viewportWidth', 'viewportUnit'
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
constructor() {
|
|
50
|
+
super();
|
|
51
|
+
this._onClickHandler = this._onClickHandler.bind(this);
|
|
52
|
+
this._connected = false;
|
|
53
|
+
this._iscalcstyle = false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
connectedCallback() {
|
|
57
|
+
this._connected = true;
|
|
58
|
+
this._upgradeProperties();
|
|
59
|
+
this._update();
|
|
60
|
+
this.addEventListener('click', this._onClickHandler);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
disconnectedCallback() {
|
|
64
|
+
this._connected = false;
|
|
65
|
+
this.removeEventListener('click', this._onClickHandler);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
attributeChangedCallback() {
|
|
69
|
+
if (!this._connected) return;
|
|
70
|
+
this._update();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
_upgradeProperties() {
|
|
74
|
+
// For properties set before upgrade
|
|
75
|
+
Tdiv.observedAttributes.forEach(attr => {
|
|
76
|
+
if (this.hasOwnProperty(attr)) {
|
|
77
|
+
let value = this[attr];
|
|
78
|
+
delete this[attr];
|
|
79
|
+
this[attr] = value;
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
_getAttr(name, type = 'string') {
|
|
85
|
+
if (!this.hasAttribute(name)) return null;
|
|
86
|
+
let val = this.getAttribute(name);
|
|
87
|
+
if (type === 'boolean') return val === '' || val === 'true';
|
|
88
|
+
if (type === 'number') return Number(val);
|
|
89
|
+
return val;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_update() {
|
|
93
|
+
if (this._iscalcstyle) return;
|
|
94
|
+
this._iscalcstyle = true;
|
|
95
|
+
// console.log('update');
|
|
96
|
+
// 读取所有属性
|
|
97
|
+
const _class = this._getAttr('_class') || '';
|
|
98
|
+
const usePx = this._getAttr('usePx', 'boolean');
|
|
99
|
+
const styleAttr = this._getAttr('_style') || '';
|
|
100
|
+
const src = this._getAttr('src') || '';
|
|
101
|
+
const width = this._getAttr('width');
|
|
102
|
+
const height = this._getAttr('height');
|
|
103
|
+
const absolute = this._getAttr('absolute', 'boolean');
|
|
104
|
+
const top = this._getAttr('top');
|
|
105
|
+
const left = this._getAttr('left');
|
|
106
|
+
const right = this._getAttr('right');
|
|
107
|
+
const bottom = this._getAttr('bottom');
|
|
108
|
+
const zIndex = this._getAttr('zIndex');
|
|
109
|
+
const pointer = this._getAttr('pointer', 'boolean');
|
|
110
|
+
const flex = this._getAttr('flex', 'boolean');
|
|
111
|
+
const column = this._getAttr('column', 'boolean');
|
|
112
|
+
const center = this._getAttr('center', 'boolean');
|
|
113
|
+
const text = this._getAttr('text');
|
|
114
|
+
const color = this._getAttr('color');
|
|
115
|
+
const srcMode = this._getAttr('srcMode') || 'auto';
|
|
116
|
+
const debug = this._getAttr('debug', 'boolean');
|
|
117
|
+
const viewportWidth = this._getAttr('viewportWidth', 'number');
|
|
118
|
+
const viewportUnit = this._getAttr('viewportUnit');
|
|
119
|
+
// class
|
|
120
|
+
let extraClass = ['tw-img-div'];
|
|
121
|
+
if (_class) extraClass.push(_class);
|
|
122
|
+
if (setting.useButtonEffect && this._getAttr('onClick')) extraClass.push('click-effect');
|
|
123
|
+
// 分组样式
|
|
124
|
+
const calcOpt = { usePx, viewportWidth, viewportUnit };
|
|
125
|
+
let layoutStyle = '';
|
|
126
|
+
// 宽度
|
|
127
|
+
if (hasValue(width)) {
|
|
128
|
+
const widthStyle = `width: ${calc(width, calcOpt)};`;
|
|
129
|
+
layoutStyle += widthStyle;
|
|
130
|
+
}
|
|
131
|
+
if (hasValue(height)) {
|
|
132
|
+
const heightStyle = `height: ${calc(height, calcOpt)};`;
|
|
133
|
+
layoutStyle += heightStyle;
|
|
134
|
+
}
|
|
135
|
+
if (absolute) {
|
|
136
|
+
layoutStyle += 'position: absolute;';
|
|
137
|
+
if (hasValue(left)) layoutStyle += `left: ${calc(left, calcOpt)};`;
|
|
138
|
+
if (hasValue(top)) layoutStyle += `top: ${calc(top, calcOpt)};`;
|
|
139
|
+
if (hasValue(right)) layoutStyle += `right: ${calc(right, calcOpt)};`;
|
|
140
|
+
if (hasValue(bottom)) layoutStyle += `bottom: ${calc(bottom, calcOpt)};`;
|
|
141
|
+
} else {
|
|
142
|
+
layoutStyle += 'position: relative;';
|
|
143
|
+
if (hasValue(top)) layoutStyle += `margin-top: ${calc(top, calcOpt)};`;
|
|
144
|
+
if (hasValue(left)) layoutStyle += `margin-left: ${calc(left, calcOpt)};`;
|
|
145
|
+
if (hasValue(right)) layoutStyle += `margin-right: ${calc(right, calcOpt)};`;
|
|
146
|
+
if (hasValue(bottom)) layoutStyle += `margin-bottom: ${calc(bottom, calcOpt)};`;
|
|
147
|
+
}
|
|
148
|
+
if (hasValue(zIndex)) layoutStyle += `z-index: ${zIndex};`;
|
|
149
|
+
if (pointer === true) {
|
|
150
|
+
layoutStyle += 'cursor: pointer;';
|
|
151
|
+
} else if (this._getAttr('onClick') && pointer === false) {
|
|
152
|
+
layoutStyle += 'cursor: pointer;';
|
|
153
|
+
}
|
|
154
|
+
if (flex) {
|
|
155
|
+
layoutStyle += 'display:flex;';
|
|
156
|
+
if (column) layoutStyle += 'flex-direction: column;';
|
|
157
|
+
if (center) {
|
|
158
|
+
layoutStyle += 'justify-content: center;';
|
|
159
|
+
layoutStyle += 'align-items: center;';
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
if (center) layoutStyle += 'text-align: center;';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (text) layoutStyle += `font-size: ${calc(text, calcOpt)};`;
|
|
166
|
+
if (src) {
|
|
167
|
+
let realSrc = src;
|
|
168
|
+
if (src.indexOf('https://') === -1) {
|
|
169
|
+
realSrc = `https://dynamics-share.tuwan.com/${src}?x-oss-process=image/format,webp/ignore-error,1`;
|
|
170
|
+
}
|
|
171
|
+
layoutStyle += `background-image: url(${realSrc});`;
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
if (srcMode === 'fill') {
|
|
175
|
+
layoutStyle += 'background-size: 100% 100%;';
|
|
176
|
+
} else if (srcMode === 'cover') {
|
|
177
|
+
layoutStyle += 'background-size: cover;';
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (color) layoutStyle += `color: ${color};`;
|
|
181
|
+
if (styleAttr) layoutStyle += styleAttr;
|
|
182
|
+
if (debug) {
|
|
183
|
+
console.log('layoutStyle', layoutStyle);
|
|
184
|
+
}
|
|
185
|
+
const currentList = Array.from(this.classList);
|
|
186
|
+
const classNeeded = extraClass.filter(item => !currentList.includes(item));
|
|
187
|
+
this.classList.add(...classNeeded);
|
|
188
|
+
this.style.cssText = layoutStyle;
|
|
189
|
+
this._iscalcstyle = false;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
_onClickHandler(e) {
|
|
193
|
+
// 支持onClick属性(字符串,window作用域下查找)
|
|
194
|
+
const onClickAttr = this._getAttr('onClick');
|
|
195
|
+
if (onClickAttr && typeof window[onClickAttr] === 'function') {
|
|
196
|
+
window[onClickAttr](e);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
let isRegister = false;
|
|
203
|
+
export function registerTdiv() {
|
|
204
|
+
if (!isRegister) {
|
|
205
|
+
customElements.define('t-div', Tdiv);
|
|
206
|
+
// append style for t-div
|
|
207
|
+
const style = document.createElement('style');
|
|
208
|
+
style.textContent = styleText;
|
|
209
|
+
document.head.appendChild(style);
|
|
210
|
+
isRegister = true;
|
|
211
|
+
}
|
|
212
|
+
}
|
package/Tdiv/index.svelte
CHANGED
|
@@ -98,11 +98,12 @@
|
|
|
98
98
|
*/
|
|
99
99
|
export let color = "";
|
|
100
100
|
/**
|
|
101
|
-
* @type {'auto'|'fill'|'cover'}
|
|
101
|
+
* @type {'auto'|'fill'|'cover'|'contain'}
|
|
102
102
|
* src传入时有效, 图片的填充模式: 默认auto
|
|
103
103
|
* - fill: 长款拉伸填充
|
|
104
104
|
* - auto: 自动填充(按照宽高比自动填充, 不重复不裁剪)
|
|
105
105
|
* - cover: 覆盖
|
|
106
|
+
* - contain: 包含
|
|
106
107
|
*/
|
|
107
108
|
export let srcMode = "auto";
|
|
108
109
|
|
|
@@ -234,7 +235,9 @@
|
|
|
234
235
|
}
|
|
235
236
|
if (src) {
|
|
236
237
|
let realSrc = src;
|
|
237
|
-
if
|
|
238
|
+
if(src.indexOf("data:") === 0){
|
|
239
|
+
realSrc = src;
|
|
240
|
+
}else if (src.indexOf("https://") === -1) {
|
|
238
241
|
realSrc = `https://dynamics-share.tuwan.com/${src}?x-oss-process=image/format,webp/ignore-error,1`;
|
|
239
242
|
}
|
|
240
243
|
ret += `background-image: url(${realSrc});`;
|
|
@@ -242,6 +245,8 @@
|
|
|
242
245
|
ret += `background-size: 100% 100%;`;
|
|
243
246
|
} else if (srcMode === "cover") {
|
|
244
247
|
ret += `background-size: cover;`;
|
|
248
|
+
}else if (srcMode === "contain") {
|
|
249
|
+
ret += `background-size: contain;`;
|
|
245
250
|
}
|
|
246
251
|
}
|
|
247
252
|
if (color) {
|