sadais-piui 1.2.1 → 1.2.2-beta3

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/README.md CHANGED
@@ -161,7 +161,6 @@ MIT © 2021 zhangzhenfei
161
161
 
162
162
  ## 微信群
163
163
 
164
- 请扫码添加[piui客服小助手]邀请进群
165
-
166
- ![](https://sadais-oss.oss-cn-hangzhou.aliyuncs.com/piui/WechatIMG17.jpeg)
164
+ 请扫码进群
167
165
 
166
+ ![](https://m.sadais.com/piui/WechatIMG411.jpeg)
@@ -131,5 +131,9 @@ export default {
131
131
  left: 0;
132
132
  z-index: 99;
133
133
  }
134
+ ::v-deep .pi-button[size] {
135
+ padding-left: 0;
136
+ padding-right: 0;
137
+ }
134
138
  }
135
139
  </style>
@@ -0,0 +1,400 @@
1
+ <template>
2
+ <view ref="root" :class="[customClass, 'wrapper']" :style="[customStyle]">
3
+ <canvas
4
+ ref="canvas"
5
+ :style="[canvasStyle]"
6
+ type="2d"
7
+ class="content"
8
+ :canvas-id="canvasId"
9
+ :width="canvasw"
10
+ :height="canvash"
11
+ @touchstart="start"
12
+ @touchmove="move"
13
+ @touchcancel="end"
14
+ @touchend="end"
15
+ @mousedown="start"
16
+ @mousemove="move"
17
+ @mouseup="end"
18
+ />
19
+ <canvas
20
+ ref="cropCanvas"
21
+ type="2d"
22
+ class="copy"
23
+ :style="[cropStyle]"
24
+ :canvas-id="cropId"
25
+ :width="cropw"
26
+ :height="croph"
27
+ />
28
+ </view>
29
+ </template>
30
+ <script>
31
+ export default {
32
+ name: 'PiCanvas',
33
+ props: {
34
+ // 自定义class
35
+ customClass: {
36
+ type: String,
37
+ // `''`
38
+ default: ''
39
+ },
40
+ // 自定义style
41
+ customStyle: {
42
+ type: Object,
43
+ // `{}`
44
+ default: () => ({})
45
+ },
46
+ // 是否最小化空白区域
47
+ minimal: {
48
+ type: Boolean,
49
+ // `false`
50
+ default: false
51
+ },
52
+ // 当minimal:true时,指定pandding
53
+ padding: {
54
+ type: Object,
55
+ // `{ top: 8, right: 8, bottom: 8, left: 8 }`
56
+ default: () => ({ top: 8, right: 8, bottom: 8, left: 8 })
57
+ },
58
+ // 画布背景色
59
+ bgColor: {
60
+ type: String,
61
+ // `#ffffff`
62
+ default: '#ffffff'
63
+ },
64
+ // 画笔颜色
65
+ color: {
66
+ type: String,
67
+ // `#333333`
68
+ default: '#333333'
69
+ },
70
+ // 线条宽度
71
+ lineWidth: {
72
+ type: Number,
73
+ // `1`
74
+ default: 1
75
+ }
76
+ },
77
+ data() {
78
+ return {
79
+ isBegin: false, // 标记是否先触发了touchstart or mousedown
80
+ canvasId: '', // 内容画布的ID
81
+ cropId: '', // 裁剪画布的ID
82
+ ctx: null, // 内容画布的上下文
83
+ canvasw: 0, // 内容画布的宽度
84
+ canvash: 0, // 内容画布的高度
85
+ cropw: 0, // 裁剪画布的宽度
86
+ croph: 0 // 裁剪画布的高度
87
+ }
88
+ },
89
+ computed: {
90
+ isUni() {
91
+ return window.uni != null
92
+ },
93
+ canvasStyle() {
94
+ return {
95
+ width: `${this.canvasw}px`,
96
+ height: `${this.canvash}px`
97
+ }
98
+ },
99
+ cropStyle() {
100
+ return {
101
+ width: `${this.cropw}px`,
102
+ height: `${this.croph}px`
103
+ }
104
+ }
105
+ },
106
+ created() {
107
+ this.$nextTick(() => {
108
+ this.initCanvas()
109
+ })
110
+ },
111
+ methods: {
112
+ generateId() {
113
+ // 产生16位随机的字母、数字混合的随机ID
114
+ const tmps = '0123456789'
115
+ .split('')
116
+ .concat('abcdefghijklmnopqrstuvwxyz'.split(''))
117
+ .concat('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''))
118
+ let n = tmps.length
119
+ while (n--) {
120
+ const idx = Math.floor(Math.random() * n)
121
+ const tmp = tmps[idx]
122
+ tmps[idx] = tmps[n]
123
+ tmps[n] = tmp
124
+ }
125
+ return tmps.join('').substr(0, 16)
126
+ },
127
+ initCanvas() {
128
+ this.canvasId = this.generateId()
129
+ this.cropId = this.generateId()
130
+ if (!this.isUni) {
131
+ // 非uni环境
132
+ const root = this.$refs.root
133
+ const style = window.getComputedStyle(root)
134
+ this.canvasw = parseInt(style.width, 10)
135
+ this.canvash = parseInt(style.height, 10)
136
+ const canvas = this.$refs.canvas
137
+ this.ctx = canvas.getContext('2d')
138
+ this.$nextTick(() => {
139
+ this.clear()
140
+ })
141
+ } else {
142
+ // uni环境:无法操作dom 只能通过它提供的API获取布局信息
143
+ const query = uni.createSelectorQuery().in(this)
144
+ query
145
+ .select('.wrapper')
146
+ .boundingClientRect(res => {
147
+ this.canvasw = res.width - 2
148
+ this.canvash = res.height - 2
149
+ this.$nextTick(() => {
150
+ this.ctx = uni.createCanvasContext(this.canvasId, this)
151
+ this.clear()
152
+ })
153
+ })
154
+ .exec()
155
+ }
156
+ },
157
+ /**
158
+ * @vuese
159
+ * 清空画布内容
160
+ */
161
+ clear() {
162
+ this.initRange()
163
+ this.initPen()
164
+ this.ctx.clearRect(0, 0, this.canvasw, this.canvash)
165
+ this.ctx.fillRect(0, 0, this.canvasw, this.canvash)
166
+ if (this.isUni) {
167
+ this.ctx.draw()
168
+ }
169
+ },
170
+ initPen() {
171
+ // 初始化画笔
172
+ if (this.isUni) {
173
+ this.ctx.setFillStyle(this.bgColor)
174
+ this.ctx.setStrokeStyle(this.color)
175
+ this.ctx.setLineWidth(this.lineWidth)
176
+ } else {
177
+ this.ctx.fillStyle = this.bgColor
178
+ this.ctx.strokeStyle = this.color
179
+ this.ctx.lineWidth = this.lineWidth
180
+ }
181
+ },
182
+ initRange() {
183
+ // 记录画布上左上角的点 s: start
184
+ this.s = { x: Number.MAX_VALUE, y: Number.MAX_VALUE }
185
+ // 记录画布右下角的点 e: end
186
+ this.e = { x: 0, y: 0 }
187
+ },
188
+ async getImageData(x, y, w, h) {
189
+ if (this.isUni) {
190
+ return new Promise(resolve => {
191
+ uni.canvasGetImageData({
192
+ canvasId: this.canvasId,
193
+ x,
194
+ y,
195
+ width: w,
196
+ height: h,
197
+ success: res => {
198
+ resolve(res.data)
199
+ },
200
+ fail: err => {
201
+ resolve(null)
202
+ }
203
+ })
204
+ })
205
+ } else {
206
+ return this.ctx.getImageData(x, y, w, h)
207
+ }
208
+ },
209
+ /**
210
+ * @vuese
211
+ * 裁剪画布内容
212
+ */
213
+ async crop() {
214
+ if (this.s.x === Number.MAX_VALUE) {
215
+ // 标明画布无内容 不进行裁剪操作
216
+ return
217
+ }
218
+ if (this.minimal) {
219
+ // 需要最小化空白区域,计算裁剪区域
220
+ let x = this.s.x - (this.padding.left || 0)
221
+ let y = this.s.y - (this.padding.top || 0)
222
+ x = x < 0 ? 0 : x
223
+ y = y < 0 ? 0 : y
224
+
225
+ let x1 = this.e.x + (this.padding.right || 0)
226
+ let y1 = this.e.y + (this.padding.bottom || 0)
227
+ x1 = x1 > this.canvasw ? this.canvasw : x1
228
+ y1 = y1 > this.canvash ? this.canvash : y1
229
+
230
+ // 获取裁剪区域的RGBA像素点数据
231
+ const imageData = await this.getImageData(x, y, x1 - x, y1 - y)
232
+ this.emitCrop(imageData, x1 - x, y1 - y)
233
+ } else {
234
+ // 直接获取整张画布区域的像素点数据
235
+ const imageData = await this.getImageData(0, 0, this.canvasw, this.canvash)
236
+ this.emitCrop(imageData, this.canvasw, this.canvash)
237
+ }
238
+ },
239
+ async getBase64(imageData, w, h) {
240
+ // 原理:将imageData放入裁剪画布中,然后即可生成base64
241
+ if (this.isUni) {
242
+ // uni环境下 按uni提供的API进行操作
243
+ return new Promise(resolve => {
244
+ uni.canvasPutImageData(
245
+ {
246
+ canvasId: this.cropId,
247
+ data: imageData,
248
+ x: 0,
249
+ y: 0,
250
+ width: w,
251
+ height: h,
252
+ success: res => {
253
+ uni.canvasToTempFilePath({
254
+ canvasId: this.cropId,
255
+ x: 0,
256
+ y: 0,
257
+ success: res => {
258
+ resolve(res.tempFilePath)
259
+ },
260
+ fail: err => {
261
+ resolve('')
262
+ }
263
+ })
264
+ },
265
+ fail: err => {
266
+ resolve('')
267
+ }
268
+ },
269
+ this
270
+ )
271
+ })
272
+ } else {
273
+ // 否则 按常规尝试进行
274
+ const canvas = this.$refs.cropCanvas
275
+ ctx = canvas.getContext('2d')
276
+ ctx.putImageData(imageData, 0, 0)
277
+ const base64 = canvas.toDataURL()
278
+ return base64
279
+ }
280
+ },
281
+ emitCrop(imageData, w, h) {
282
+ if (!imageData) {
283
+ return
284
+ }
285
+ this.cropw = w
286
+ this.croph = h
287
+ setTimeout(async () => {
288
+ // $nextTick的目的是为了让裁剪画布的宽高生效后 再进行操作
289
+ const base64 = await this.getBase64(imageData, w, h)
290
+ const results = base64.match(/data:(.*?);.*?,(.*)/)
291
+ const mineType = results[1]
292
+ const suffix = mineType.split('/')[1]
293
+ const binstr = window.atob(results[2])
294
+ let n = binstr.length
295
+ const bytes = new Uint8Array(n)
296
+ while (n--) {
297
+ bytes[n] = binstr.charCodeAt(n)
298
+ }
299
+ const blob = new Blob([bytes], { type: mineType })
300
+ // export
301
+ // arg `{base64, blob, width, height, style}`
302
+ this.$emit('export', {
303
+ base64,
304
+ blob,
305
+ width: w,
306
+ height: h,
307
+ style: { width: `${w}px`, height: `${h}px` }
308
+ })
309
+ }, 100)
310
+ },
311
+ /**
312
+ * 随着画笔移动,不断调用此方法更新:包裹内容的矩形的左上角&右下角的坐标
313
+ */
314
+ updateRange(x, y) {
315
+ if (x < this.s.x) {
316
+ this.s.x = x
317
+ } else if (x > this.e.x) {
318
+ this.e.x = x
319
+ }
320
+ if (y < this.s.y) {
321
+ this.s.y = y
322
+ } else if (y > this.e.y) {
323
+ this.e.y = y
324
+ }
325
+ },
326
+ start(e) {
327
+ // 适配移动端的写法: e.touches移动端才有
328
+ const touches = (e && e.changedTouches) || []
329
+ let x = 0,
330
+ y = 0
331
+ const touch = touches[0]
332
+ if (/^touch/.test(e.type)) {
333
+ x = touch.x
334
+ y = touch.y
335
+ } else {
336
+ x = e.offsetX
337
+ y = e.offsetY
338
+ }
339
+ // 为了清空之前残留的路径
340
+ this.ctx.beginPath()
341
+ this.ctx.moveTo(x, y)
342
+ this.ctx.stroke()
343
+ if (this.isUni) {
344
+ this.ctx.draw(true)
345
+ }
346
+ this.isBegin = true
347
+ this.updateRange(x, y)
348
+ this.prev = { x, y }
349
+ },
350
+ move(e) {
351
+ if (!this.isBegin) {
352
+ return
353
+ }
354
+ let x = 0,
355
+ y = 0
356
+ const touches = (e && e.changedTouches) || []
357
+ const touch = touches[0]
358
+ if (/^touch/.test(e.type)) {
359
+ x = touch.x
360
+ y = touch.y
361
+ } else {
362
+ x = e.offsetX
363
+ y = e.offsetY
364
+ }
365
+ this.ctx.moveTo(this.prev.x, this.prev.y)
366
+ this.ctx.lineTo(x, y)
367
+ this.ctx.stroke()
368
+ if (this.isUni) {
369
+ this.ctx.draw(true)
370
+ }
371
+ this.updateRange(x, y)
372
+ this.prev = { x, y }
373
+ },
374
+ end(e) {
375
+ // this.move(e)
376
+ this.isBegin = false
377
+ }
378
+ }
379
+ }
380
+ </script>
381
+
382
+ <style lang="scss" scoped>
383
+ .wrapper {
384
+ position: relative;
385
+ width: 100%;
386
+ height: 100%;
387
+ border: 1px solid #aaaaaa;
388
+ }
389
+
390
+ .content {
391
+ position: relative;
392
+ margin: 0 auto;
393
+ }
394
+
395
+ .copy {
396
+ position: absolute;
397
+ top: -50000px;
398
+ left: -50000px;
399
+ }
400
+ </style>
@@ -11,6 +11,7 @@
11
11
  :mode="mode"
12
12
  :lazy-load="lazyLoad"
13
13
  :style="[rectStyle, imageStyle]"
14
+ :show-menu-by-longpress="showMenuByLongpress"
14
15
  @error="handleError"
15
16
  @load="handleLoad"
16
17
  />
@@ -127,6 +127,12 @@ export default {
127
127
  validator: function(value) {
128
128
  return ['line', 'line-circle', 'fill', 'fill-circle'].includes(value)
129
129
  }
130
+ },
131
+ // 能否取消选中状态
132
+ canCancel: {
133
+ type: Boolean,
134
+ // false
135
+ default: radio.canCancel
130
136
  }
131
137
  },
132
138
  data() {
@@ -172,6 +178,9 @@ export default {
172
178
  ? this.$pi.common.addUnit(this.inheritProps.iconSize)
173
179
  : this.$pi.common.addUnit(this.iconSize)
174
180
  },
181
+ getCanCancel() {
182
+ return this.inheritProps.canCancel !== null ? this.inheritProps.canCancel : this.canCancel
183
+ },
175
184
  radioStyle() {
176
185
  const style = {
177
186
  width: this.getSize,
@@ -37,7 +37,8 @@ export default {
37
37
  'size',
38
38
  'iconSize',
39
39
  'activeColor',
40
- 'activeMode'
40
+ 'activeMode',
41
+ 'canCancel'
41
42
  ])
42
43
  ], // 注入value与val,进行双向绑定
43
44
  options: {
@@ -137,6 +138,12 @@ export default {
137
138
  validator: function(value) {
138
139
  return ['', 'line', 'line-circle', 'fill', 'fill-circle'].includes(value)
139
140
  }
141
+ },
142
+ // 能否取消选中状态
143
+ canCancel: {
144
+ type: Boolean,
145
+ // null
146
+ default: radioGroup.canCancel
140
147
  }
141
148
  },
142
149
  methods: {
@@ -146,7 +153,19 @@ export default {
146
153
  * @arg pi-radio的name属性值
147
154
  */
148
155
  emitChange(childName) {
149
- this.val = childName
156
+ let canCancel = this.canCancel
157
+ if (!canCancel) {
158
+ this._children.forEach(child => {
159
+ if (child.name === childName) {
160
+ canCancel = child.getCanCancel
161
+ }
162
+ })
163
+ }
164
+ if (canCancel && this.val === childName) {
165
+ this.val = ''
166
+ } else {
167
+ this.val = childName
168
+ }
150
169
  this.handleEmitChange()
151
170
  }
152
171
  }
package/config/radio.js CHANGED
@@ -10,5 +10,6 @@ export default {
10
10
  size: 36, // radio大小,单位rpx
11
11
  iconSize: 28, // radio icon 大小,单位rpx
12
12
  activeColor: '', // 选中时图标的颜色
13
- activeMode: 'line' // 激活模式(line: 线框模式,fill: 实底模式)
13
+ activeMode: 'line', // 激活模式(line: 线框模式,fill: 实底模式)
14
+ canCancel: false // 能否取消选中状态
14
15
  }
@@ -11,5 +11,6 @@ export default {
11
11
  size: 0, // checkbox大小,单位rpx
12
12
  iconSize: 0, // checkbox icon 大小,单位rpx
13
13
  activeColor: '', // 选中时图标的颜色
14
- activeMode: '' // 激活模式(line: 线框模式,fill: 实底模式)
14
+ activeMode: '', // 激活模式(line: 线框模式,fill: 实底模式)
15
+ canCancel: null // 能否取消选中状态
15
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sadais-piui",
3
- "version": "1.2.1",
3
+ "version": "1.2.2-beta3",
4
4
  "description": "piui组件库",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/scss/common.scss CHANGED
@@ -6,27 +6,6 @@
6
6
  * 颜色
7
7
  * -------------------------------------------------------------------------------------------------
8
8
  */
9
-
10
- // 主要颜色
11
- .pi-primary {
12
- color: $pi-primary-color;
13
- }
14
- // 次级颜色
15
- .pi-secondary {
16
- color: $pi-secondary-color;
17
- }
18
- // 第三级颜色
19
- .pi-tertiary {
20
- color: $pi-tertiary-color;
21
- }
22
- // 第四级颜色
23
- .pi-fourth {
24
- color: $pi-fourth-color;
25
- }
26
- // 第五级颜色
27
- .pi-fifth {
28
- color: $pi-fifth-color;
29
- }
30
9
  // 黑色
31
10
  .pi-black {
32
11
  color: #000000;
@@ -47,6 +26,26 @@
47
26
  .pi-light-gray {
48
27
  color: #999999;
49
28
  }
29
+ // 主要颜色
30
+ .pi-primary {
31
+ color: $pi-primary-color;
32
+ }
33
+ // 次级颜色
34
+ .pi-secondary {
35
+ color: $pi-secondary-color;
36
+ }
37
+ // 第三级颜色
38
+ .pi-tertiary {
39
+ color: $pi-tertiary-color;
40
+ }
41
+ // 第四级颜色
42
+ .pi-fourth {
43
+ color: $pi-fourth-color;
44
+ }
45
+ // 第五级颜色
46
+ .pi-fifth {
47
+ color: $pi-fifth-color;
48
+ }
50
49
 
51
50
  /**
52
51
  * 背景颜色