@taole/dev-helper 0.0.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.
Files changed (2) hide show
  1. package/Tdiv/index.svelte +263 -0
  2. package/package.json +19 -0
@@ -0,0 +1,263 @@
1
+ <script>
2
+ import { createEventDispatcher } from "svelte";
3
+
4
+ /**
5
+ * @type {string}
6
+ * 传入的class请务必放在global下
7
+ */
8
+ export let _class = "";
9
+
10
+ /**
11
+ * @type {boolean}
12
+ * 默认false, 传入true表示直接使用px单位,一般使用场景比如pc
13
+ */
14
+ export let usePx = false;
15
+
16
+ /**
17
+ * @type {string}
18
+ * 请不要传入数值相关的style, 因为这部分不会被转换成vw单位
19
+ */
20
+ export let style = "";
21
+
22
+ /**
23
+ * @type {string}
24
+ * 图片地址
25
+ * 传入完整url或者path,组件会自动拼上`https://dynamics-share.tuwan.com/${src}?x-oss-process=image/format,webp/ignore-error,1`
26
+ */
27
+ export let src = "";
28
+ /**
29
+ * @type {number|string}
30
+ * 宽度
31
+ */
32
+ export let width = null;
33
+ /**
34
+ * @type {number|string}
35
+ * 高度
36
+ */
37
+ export let height = null;
38
+ /**
39
+ * @type {boolean}
40
+ * 使用绝对布局, false则为相对布局
41
+ */
42
+ export let absolute = false;
43
+
44
+ /**
45
+ * @type {number|string}
46
+ * 绝对布局下为top,相对布局下为margin-top
47
+ */
48
+ export let top = null;
49
+ /**
50
+ * @type {number|string}
51
+ * 绝对布局下为left,相对布局下为margin-left
52
+ */
53
+ export let left = null;
54
+ /**
55
+ * @type {number|string}
56
+ * 绝对布局下为right,相对布局下为margin-right
57
+ */
58
+ export let right = null;
59
+ /**
60
+ * @type {number|string}
61
+ * 绝对布局下为bottom,相对布局下为margin-bottom
62
+ */
63
+ export let bottom = null;
64
+ /**
65
+ * @type {number}
66
+ * z-index层级
67
+ */
68
+ export let zIndex = null;
69
+ /**
70
+ * @type {boolean}
71
+ * cursor:pointer
72
+ */
73
+ export let pointer = null;
74
+ /**
75
+ * @type {boolean}
76
+ * 使用flex布局
77
+ */
78
+ export let flex = false;
79
+ /**
80
+ * @type {boolean}
81
+ * flex布局下,flex-direction:column
82
+ */
83
+ export let column = false;
84
+ /**
85
+ * @type {boolean}
86
+ * flex布局下,align-items:center;justify-content: center;
87
+ */
88
+ export let center = false;
89
+ /**
90
+ * @type {number|string}
91
+ * 内部文本的大小
92
+ */
93
+ export let text = null;
94
+ /**
95
+ * @type {string}
96
+ * 内部文本的颜色
97
+ */
98
+ export let color = "";
99
+ /**
100
+ * @type {'auto'|'fill'|'cover'}
101
+ * src传入时有效, 图片的填充模式: 默认auto
102
+ * - fill: 长款拉伸填充
103
+ * - auto: 自动填充(按照宽高比自动填充, 不重复不裁剪)
104
+ * - cover: 覆盖
105
+ */
106
+ export let srcMode = "auto";
107
+
108
+ /**
109
+ * @type {(e) => void}
110
+ * 点击事件监听
111
+ * - onClick 使用onClick将会自动设置cursor:pointer样式
112
+ * - on:click 不会
113
+ */
114
+ export let onClick = null;
115
+
116
+ /**
117
+ * @type {boolean}
118
+ * 打开后,属性变化的时候会打印最终的所有style
119
+ */
120
+ export let debug = false;
121
+
122
+ // 留个口子
123
+ export const viewportWidth = 750; // 默认值不改
124
+ export const viewportUnit = "vw"; // 默认值不改
125
+
126
+ const dispatch = createEventDispatcher();
127
+
128
+ /**
129
+ * 计算宽高
130
+ * @param value
131
+ * @returns {string}
132
+ */
133
+ function calc(value) {
134
+ // @ts-ignore
135
+ if (typeof value === "string" && !isNaN(value)) {
136
+ value = Number(value);
137
+ }
138
+ if (typeof value === "number") {
139
+ if (usePx) {
140
+ return value + "px";
141
+ }
142
+ let val = ((value / viewportWidth) * 100).toFixed(4); // 4?
143
+ return `${val}${viewportUnit}`;
144
+ }
145
+ return value;
146
+ }
147
+
148
+ function handleOnClick(e) {
149
+ if(typeof onClick === 'function'){
150
+ onClick(e);
151
+ }
152
+ dispatch("click", e);
153
+ }
154
+
155
+ function hasValue(value) {
156
+ return value !== null && value !== undefined && value !== "";
157
+ }
158
+
159
+ // 计算最终样式
160
+ $: styleStr = (function fmtStyle() {
161
+ let ret = "";
162
+ if (hasValue(width)) {
163
+ ret += `width: ${calc(width)};`;
164
+ }
165
+ if (hasValue(height)) {
166
+ ret += `height: ${calc(height)};`;
167
+ }
168
+ if (absolute) {
169
+ ret += `position: absolute;`;
170
+ if (hasValue(left)) {
171
+ ret += `left: ${calc(left)};`;
172
+ }
173
+ if (hasValue(top)) {
174
+ ret += `top: ${calc(top)};`;
175
+ }
176
+ if (hasValue(right)) {
177
+ ret += `right: ${calc(right)};`;
178
+ }
179
+ if (hasValue(bottom)) {
180
+ ret += `bottom: ${calc(bottom)};`;
181
+ }
182
+ } else {
183
+ ret += `position: relative;`;
184
+ if (hasValue(top)) {
185
+ ret += `margin-top: ${calc(top)};`;
186
+ }
187
+ if (hasValue(left)) {
188
+ ret += `margin-left: ${calc(left)};`;
189
+ }
190
+ if (hasValue(right)) {
191
+ ret += `margin-right: ${calc(right)};`;
192
+ }
193
+ if (hasValue(bottom)) {
194
+ ret += `margin-bottom: ${calc(bottom)};`;
195
+ }
196
+ }
197
+ if (hasValue(zIndex)) {
198
+ ret += `z-index: ${zIndex};`;
199
+ }
200
+ if (pointer === true) {
201
+ ret += `cursor: pointer;`;
202
+ } else if(typeof onClick === "function" && pointer === null){
203
+ ret += `cursor: pointer;`;
204
+ }
205
+ if (flex) {
206
+ ret += "display:flex;";
207
+ if (column) {
208
+ ret += "flex-direction: column;";
209
+ }
210
+ if (center) {
211
+ ret += "justify-content: center;";
212
+ ret += "align-items: center;";
213
+ }
214
+ } else {
215
+ if (center) {
216
+ ret += "text-align: center;";
217
+ }
218
+ }
219
+ if (text) {
220
+ ret += `font-size: ${calc(text)};`;
221
+ }
222
+ if (src) {
223
+ let realSrc = src;
224
+ if (src.indexOf("https://") === -1) {
225
+ realSrc = `https://dynamics-share.tuwan.com/${src}?x-oss-process=image/format,webp/ignore-error,1`;
226
+ }
227
+ ret += `background-image: url(${realSrc});`;
228
+ if (srcMode === "fill") {
229
+ ret += `background-size: 100% 100%;`;
230
+ } else if (srcMode === "cover") {
231
+ ret += `background-size: cover;`;
232
+ }
233
+ }
234
+ if (color) {
235
+ ret += `color: ${color};`;
236
+ }
237
+ if (style) {
238
+ ret += style;
239
+ }
240
+ if (debug) {
241
+ console.log("styleStr", ret);
242
+ }
243
+ return ret;
244
+ })();
245
+ </script>
246
+
247
+ <div
248
+ role="presentation"
249
+ on:click={handleOnClick}
250
+ class="tw-img-div {_class}"
251
+ style={styleStr}
252
+ >
253
+ <slot />
254
+ </div>
255
+
256
+ <style scoped>
257
+ .tw-img-div {
258
+ background-position: top center;
259
+ background-repeat: no-repeat;
260
+ background-size: 100% auto;
261
+ flex: none;
262
+ }
263
+ </style>
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@taole/dev-helper",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": [],
9
+ "publishConfig": {
10
+ "access": "public",
11
+ "registry": "https://registry.npmjs.org/"
12
+ },
13
+ "files":[
14
+ "Tdiv"
15
+ ],
16
+ "author": "",
17
+ "license": "ISC",
18
+ "description": ""
19
+ }