@opentiny/vue-renderless 3.19.4 → 3.20.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.
Files changed (79) hide show
  1. package/base-select/index.js +11 -74
  2. package/base-select/vue.js +10 -10
  3. package/bulletin-board/index.js +5 -1
  4. package/bulletin-board/vue.js +5 -4
  5. package/button/index.js +11 -3
  6. package/button/vue.js +6 -2
  7. package/button-group/index.js +1 -1
  8. package/color-picker/index.js +4 -4
  9. package/color-picker/utils/color.js +358 -83
  10. package/color-picker/utils/hsv-to-rgb.js +59 -0
  11. package/color-picker/vue.js +27 -8
  12. package/color-select-panel/alpha-select/index.js +65 -14
  13. package/color-select-panel/alpha-select/vue.js +29 -44
  14. package/color-select-panel/hue-select/index.js +47 -61
  15. package/color-select-panel/hue-select/vue.js +29 -47
  16. package/color-select-panel/index.js +230 -80
  17. package/color-select-panel/sv-select/index.js +68 -0
  18. package/color-select-panel/sv-select/vue.js +32 -0
  19. package/color-select-panel/utils/color.js +345 -82
  20. package/color-select-panel/utils/getClientXY.js +54 -0
  21. package/color-select-panel/vue.js +48 -72
  22. package/common/deps/popper.js +1 -1
  23. package/common/deps/useUserAgent.js +16 -0
  24. package/common/event.js +21 -0
  25. package/common/index.js +1 -1
  26. package/common/runtime.js +1 -1
  27. package/date-picker-mobile/index.js +4 -4
  28. package/dialog-box/index.js +1 -1
  29. package/dropdown/index.js +5 -1
  30. package/dropdown/vue.js +2 -2
  31. package/file-upload/vue.js +1 -0
  32. package/flowchart/index.js +3 -2
  33. package/form/index.js +2 -2
  34. package/form-item/index.js +4 -1
  35. package/grid-select/index.js +82 -0
  36. package/grid-select/vue.js +30 -0
  37. package/input/index.js +2 -2
  38. package/menu/vue.js +5 -1
  39. package/modal/index.js +2 -0
  40. package/numeric/index.js +29 -9
  41. package/option/index.js +6 -0
  42. package/option/vue.js +12 -2
  43. package/package.json +1 -1
  44. package/picker/index.js +1 -1
  45. package/recycle-scroller/index.js +43 -27
  46. package/recycle-scroller/vue.js +9 -4
  47. package/rich-text/index.js +13 -0
  48. package/rich-text/vue.js +4 -2
  49. package/select/index.js +71 -23
  50. package/select/vue.js +12 -4
  51. package/select-dropdown/vue.js +1 -1
  52. package/tabbar/index.js +4 -1
  53. package/tabbar/vue.js +1 -1
  54. package/tabs/index.js +8 -5
  55. package/tabs/vue.js +4 -1
  56. package/time-panel/vue.js +1 -1
  57. package/tooltip/vue.js +1 -0
  58. package/transfer/index.js +4 -2
  59. package/transfer/vue.js +2 -2
  60. package/tree-menu/index.js +5 -1
  61. package/tree-node/index.js +5 -4
  62. package/tree-select/index.js +77 -1
  63. package/tree-select/vue.js +21 -7
  64. package/types/button.type.d.ts +7 -3
  65. package/types/color-select-panel.type.d.ts +23 -1
  66. package/types/file-upload.type.d.ts +1 -1
  67. package/types/input.type.d.ts +1 -1
  68. package/types/numeric.type.d.ts +5 -5
  69. package/types/popover.type.d.ts +1 -1
  70. package/types/transfer.type.d.ts +4 -4
  71. package/types/tree-menu.type.d.ts +1 -1
  72. package/types/upload-dragger.type.d.ts +1 -1
  73. package/types/{upload-list.type-8d9e2600.d.ts → upload-list.type-1257e07a.d.ts} +7 -3
  74. package/types/upload-list.type.d.ts +1 -1
  75. package/types/upload.type.d.ts +1 -1
  76. package/types/user-head.type.d.ts +4 -0
  77. package/user-head/index.js +1 -1
  78. package/year-table/index.js +14 -29
  79. package/year-table/vue.js +17 -5
@@ -1,102 +1,377 @@
1
- import "../../chunk-G2ADBYYC.js";
2
- import { hsv, rgb } from "color";
3
- function hexToRgb(hex) {
4
- let r = parseInt(hex.substring(1, 3), 16);
5
- let g = parseInt(hex.substring(3, 5), 16);
6
- let b = parseInt(hex.substring(5, 7), 16);
7
- let a = parseInt(hex.slice(7), 16) / 255;
8
- return { r, g, b, a: a * 100 };
9
- }
10
- const normalizeHexColor = (color) => {
11
- let normalizedColor = color.replace("#", "");
12
- if (normalizedColor.length === 3) {
13
- normalizedColor = normalizedColor.split("").map((char) => char + char).join("");
14
- }
15
- normalizedColor = normalizedColor.padEnd(6, "0");
16
- const r = parseInt(normalizedColor.substr(0, 2), 16);
17
- const g = parseInt(normalizedColor.substr(2, 2), 16);
18
- const b = parseInt(normalizedColor.substr(4, 2), 16);
19
- let a = 255;
20
- if (normalizedColor.length === 8) {
21
- a = parseInt(normalizedColor.slice(6), 16);
22
- }
23
- const hexR = ("0" + r.toString(16)).slice(-2);
24
- const hexG = ("0" + g.toString(16)).slice(-2);
25
- const hexB = ("0" + b.toString(16)).slice(-2);
26
- const alpha = ("0" + a.toString(16)).slice(-2);
27
- return `#${hexR}${hexG}${hexB}${alpha}`;
1
+ import {
2
+ __spreadProps,
3
+ __spreadValues
4
+ } from "../../chunk-G2ADBYYC.js";
5
+ const INT_HEX_MAP = {
6
+ 10: "A",
7
+ 11: "B",
8
+ 12: "C",
9
+ 13: "D",
10
+ 14: "E",
11
+ 15: "F"
12
+ };
13
+ const HEX_INT_MAP = {
14
+ A: 10,
15
+ B: 11,
16
+ C: 12,
17
+ D: 13,
18
+ E: 14,
19
+ F: 15
20
+ };
21
+ const parseHex = function(hex) {
22
+ if (hex.length === 2) {
23
+ return (HEX_INT_MAP[hex[0].toUpperCase()] || +hex[0]) * 16 + (HEX_INT_MAP[hex[1].toUpperCase()] || +hex[1]);
24
+ }
25
+ return HEX_INT_MAP[hex[1].toUpperCase()] || +hex[1];
26
+ };
27
+ const hsv2hsl = ({ hue, sat, val }) => {
28
+ const hue_ = (2 - sat) * val;
29
+ return [hue, sat * val / (hue_ < 1 ? hue_ : 2 - hue_) || 0, hue_ / 2];
30
+ };
31
+ const isString = (val) => typeof val === "string";
32
+ const isOnePointZero = (n) => {
33
+ return isString(n) && n.includes(".") && Number.parseFloat(n) === 1;
34
+ };
35
+ const isPercentage = (n) => {
36
+ return isString(n) && n.includes("%");
37
+ };
38
+ const normalized = (value, max) => {
39
+ if (isOnePointZero(value))
40
+ value = "100%";
41
+ const processPercent = isPercentage(value);
42
+ value = Math.min(max, Math.max(0, Number.parseFloat(`${value}`)));
43
+ if (processPercent) {
44
+ value = Number.parseInt(`${value * max}`, 10) / 100;
45
+ }
46
+ if (Math.abs(value - max) < 1e-6) {
47
+ return 1;
48
+ }
49
+ return value % max / Number.parseFloat(max);
50
+ };
51
+ const hexOne = (value) => {
52
+ value = Math.min(Math.round(value), 255);
53
+ const high = Math.floor(value / 16);
54
+ const low = value % 16;
55
+ return `${INT_HEX_MAP[high] || high}${INT_HEX_MAP[low] || low}`;
56
+ };
57
+ const toHex = ({ r, g, b }) => {
58
+ if (Number.isNaN(+r) || Number.isNaN(+g) || Number.isNaN(+b))
59
+ return "";
60
+ return `#${hexOne(r)}${hexOne(g)}${hexOne(b)}`;
61
+ };
62
+ const hsl2hsv = ({ hue, sat, light }) => {
63
+ sat = sat / 100;
64
+ light = light / 100;
65
+ let smin = sat;
66
+ const lmin = Math.max(light, 0.01);
67
+ light *= 2;
68
+ sat *= light <= 1 ? light : 2 - light;
69
+ smin *= lmin <= 1 ? lmin : 2 - lmin;
70
+ const v = (light + sat) / 2;
71
+ const sv = light === 0 ? 2 * smin / (lmin + smin) : 2 * sat / (light + sat);
72
+ return {
73
+ h: hue,
74
+ s: sv * 100,
75
+ v: v * 100
76
+ };
77
+ };
78
+ const rgb2hsv = ({ r, g, b }) => {
79
+ r = normalized(r, 255);
80
+ g = normalized(g, 255);
81
+ b = normalized(b, 255);
82
+ const max = Math.max(r, g, b);
83
+ const min = Math.min(r, g, b);
84
+ let h;
85
+ const v = max;
86
+ const d = max - min;
87
+ const s = max === 0 ? 0 : d / max;
88
+ if (max === min) {
89
+ h = 0;
90
+ } else {
91
+ switch (max) {
92
+ case r: {
93
+ h = (g - b) / d + (g < b ? 6 : 0);
94
+ break;
95
+ }
96
+ case g: {
97
+ h = (b - r) / d + 2;
98
+ break;
99
+ }
100
+ case b: {
101
+ h = (r - g) / d + 4;
102
+ break;
103
+ }
104
+ }
105
+ h /= 6;
106
+ }
107
+ return { h: h * 360, s: s * 100, v: v * 100 };
108
+ };
109
+ const hsv2rgb = ({ h, s, v }) => {
110
+ h = normalized(h, 360) * 6;
111
+ s = normalized(s, 100);
112
+ v = normalized(v, 100);
113
+ const i = Math.floor(h);
114
+ const f = h - i;
115
+ const p = v * (1 - s);
116
+ const q = v * (1 - f * s);
117
+ const t = v * (1 - (1 - f) * s);
118
+ const mod = i % 6;
119
+ const r = [v, q, p, p, t, v][mod];
120
+ const g = [t, v, v, q, p, p][mod];
121
+ const b = [p, p, t, v, v, q][mod];
122
+ return {
123
+ r: Math.round(r * 255),
124
+ g: Math.round(g * 255),
125
+ b: Math.round(b * 255)
126
+ };
28
127
  };
29
128
  class Color {
30
- constructor(value, alpha = false) {
31
- this.hex = "#000";
32
- this.h = 0;
33
- this.s = 0;
34
- this.v = 0;
35
- this.a = 100;
36
- this.preH = 0;
129
+ constructor(opts) {
130
+ this._hue = 0;
131
+ this._sat = 100;
132
+ this._value = 100;
133
+ this._alpha = 100;
37
134
  this.enableAlpha = false;
38
- this.reset(value);
39
- this.enableAlpha = alpha;
40
- }
41
- reset(hex) {
42
- if (this.hex === "transparent") {
43
- this.h = 0;
44
- this.s = 0;
45
- this.v = 0;
46
- this.a = 0;
135
+ this.format = "hex";
136
+ this.value = "";
137
+ for (const opt in opts) {
138
+ if (opt in opts) {
139
+ this[opt] = opts[opt];
140
+ }
141
+ }
142
+ if (opts.value) {
143
+ this.fromString(opts.value);
144
+ } else {
145
+ this.onChange();
146
+ }
147
+ }
148
+ get(prop) {
149
+ if (prop === "alpha") {
150
+ return Math.floor(this._alpha);
151
+ }
152
+ return this[`_${prop}`];
153
+ }
154
+ set(props, value) {
155
+ if (arguments.length === 1 && typeof props === "object") {
156
+ for (const p in props) {
157
+ if (Object.hasOwn(props, p)) {
158
+ this.set(p, props[p]);
159
+ }
160
+ }
47
161
  return;
48
162
  }
49
- this.hex = normalizeHexColor(hex);
50
- const { r, g, b, a } = hexToRgb(this.hex);
51
- const { h, s, v } = rgb([r, g, b, a]).hsv().object();
52
- this.h = h;
53
- this.s = s;
54
- this.v = v;
55
- this.a = a;
56
- this.preH = h;
163
+ this[`_${props}`] = value;
164
+ this.onChange();
57
165
  }
58
- set({ h, s, v, a }) {
59
- this.h = h != null ? h : this.h;
60
- this.s = s != null ? s : this.s;
61
- this.v = v != null ? v : this.v;
62
- this.a = a != null ? a : this.a;
166
+ compare(color) {
167
+ return Math.abs(color._hue - this._hue) < 2 && Math.abs(color._sat - this._sat) < 1 && Math.abs(color._value - this._value) < 1 && Math.abs(color._alpha - this._alpha) < 1;
63
168
  }
64
- setPrevH(val) {
65
- this.preH = val;
169
+ isHSL(value) {
170
+ return value.includes("hsl");
66
171
  }
67
- /**
68
- *
69
- * @returns [R,G,B]
70
- */
71
- getRGB() {
72
- return hsv(this.h, this.s, this.v).rgb().array();
172
+ isHsv(value) {
173
+ return value.includes("hsv");
174
+ }
175
+ isRgb(value) {
176
+ return value.includes("rgb");
73
177
  }
74
- getHex() {
75
- if (!this.enableAlpha) {
76
- return hsv(this.h, this.s, this.v).hex().toString();
178
+ isHex(value) {
179
+ return value.includes("#");
180
+ }
181
+ splitPart(value, regExp) {
182
+ return value.replace(regExp, "").split(/\s|,/g).filter((v) => v).map((val, idx) => {
183
+ return idx > 2 ? Number.parseFloat(val) : Number.parseInt(val, 10);
184
+ });
185
+ }
186
+ onHsv(value) {
187
+ const parts = this.splitPart(value, /hsva|hsv|\(|\)/gm);
188
+ if (parts.length === 4) {
189
+ this._alpha = Number.parseFloat(String(parts[3])) * 100;
190
+ } else {
191
+ this._alpha = 100;
192
+ }
193
+ if (parts.length >= 3) {
194
+ this.fromHSV({
195
+ h: parts[0],
196
+ s: parts[1],
197
+ v: parts[2]
198
+ });
199
+ }
200
+ }
201
+ onRgb(value) {
202
+ const rgbRegExp = /rgba|rgb|\(|\)/gm;
203
+ const parts = this.splitPart(value, rgbRegExp);
204
+ if (parts.length === 4) {
205
+ this._alpha = Number.parseFloat(String(parts[3])) * 100;
206
+ } else {
207
+ this._alpha = 100;
208
+ }
209
+ if (parts.length >= 3) {
210
+ const { h, s, v } = rgb2hsv({ r: parts[0], g: parts[1], b: parts[2] });
211
+ this.fromHSV({ h, s, v });
212
+ }
213
+ }
214
+ onHex(value) {
215
+ const hex = value.replace("#", "").trim();
216
+ const isValidHex = (hex2) => /^[0-9a-fA-F]{3}$|^[0-9a-fA-F]{6}$|^[0-9a-fA-F]{8}$/.test(hex2);
217
+ if (!isValidHex(hex)) {
218
+ return;
219
+ }
220
+ let [r, g, b] = [0, 0, 0];
221
+ if (hex.length === 3) {
222
+ r = parseHex(hex[0] + hex[0]);
223
+ g = parseHex(hex[1] + hex[1]);
224
+ b = parseHex(hex[2] + hex[2]);
225
+ } else if (hex.length === 6 || hex.length === 8) {
226
+ r = parseHex(hex.slice(0, 2));
227
+ g = parseHex(hex.slice(2, 4));
228
+ b = parseHex(hex.slice(4, 6));
229
+ }
230
+ if (hex.length === 8) {
231
+ this._alpha = parseHex(hex.slice(6)) / 255 * 100;
232
+ } else if (hex.length === 3 || hex.length === 6) {
233
+ this._alpha = 100;
234
+ }
235
+ const { h, s, v } = rgb2hsv({ r, g, b });
236
+ this.fromHSV({ h, s, v });
237
+ }
238
+ onHsl(value) {
239
+ const parts = value.replace(/hsla|hsl\(|\)gm/, "").split(/\s|,/g).filter((val) => val).map((val, idx) => {
240
+ return idx > 2 ? Number.parseFloat(val) : Number.parseInt(val, 10);
241
+ });
242
+ if (parts.length === 4) {
243
+ this._alpha = Number.parseFloat(String(parts[3])) * 100;
244
+ } else {
245
+ this._alpha = 100;
246
+ }
247
+ if (parent.length >= 3) {
248
+ const { h, s, v } = hsl2hsv({
249
+ hue: parts[0],
250
+ sat: parts[1],
251
+ light: parts[2]
252
+ });
253
+ this.fromHSV({ h, s, v });
77
254
  }
78
- return hsv(this.h, this.s, this.v, this.a / 100).hexa().toString();
79
255
  }
80
256
  /**
81
- *
82
- * @returns [h,s,l]
257
+ * @effect 会修改 this._hue, this._sat, this._value
83
258
  */
84
- getHSL() {
85
- return hsv(this.h, this.s, this.v).hsl().unitArray();
259
+ fromHSV({ h, s, v }) {
260
+ this._hue = Math.max(0, Math.min(360, h));
261
+ this._sat = Math.max(0, Math.min(100, s));
262
+ this._value = Math.max(0, Math.min(100, v));
263
+ this.onChange();
264
+ }
265
+ fromString(value) {
266
+ if (!value) {
267
+ this._hue = 0;
268
+ this._sat = 0;
269
+ this._value = 0;
270
+ this.onChange();
271
+ return;
272
+ }
273
+ if (this.isHSL(value)) {
274
+ this.onHsl(value);
275
+ }
276
+ if (this.isHsv(value)) {
277
+ this.onHsv(value);
278
+ }
279
+ if (this.isRgb(value)) {
280
+ this.onRgb(value);
281
+ }
282
+ if (this.isHex(value)) {
283
+ this.onHex(value);
284
+ }
285
+ }
286
+ toRgb() {
287
+ return hsv2rgb({
288
+ h: this._hue,
289
+ s: this._sat,
290
+ v: this._value
291
+ });
86
292
  }
87
- getHSV() {
88
- return {
89
- h: this.h,
90
- s: this.s,
91
- v: this.v,
92
- a: this.a
93
- };
293
+ toRgba() {
294
+ return __spreadProps(__spreadValues({}, hsv2rgb({
295
+ h: this._hue,
296
+ s: this._sat,
297
+ v: this._value
298
+ })), {
299
+ a: this._alpha / 100
300
+ });
94
301
  }
95
- get(key) {
96
- return this[key];
302
+ onChange() {
303
+ const { _hue, _sat, _value, _alpha, format, enableAlpha } = this;
304
+ if (!enableAlpha) {
305
+ switch (format) {
306
+ case "hsl": {
307
+ let [_, sat, light] = hsv2hsl({
308
+ hue: _hue,
309
+ sat: _sat / 100,
310
+ val: _value / 100
311
+ });
312
+ sat = Math.round(sat * 100);
313
+ light = Math.round(light * 100);
314
+ this.value = `hsl(${_hue}, ${sat}%, ${light}%)`;
315
+ break;
316
+ }
317
+ case "hsv": {
318
+ this.value = `hsv(${_hue}, ${Math.round(_sat)}%, ${Math.round(_value)}%)`;
319
+ break;
320
+ }
321
+ case "rgb": {
322
+ const { r, g, b } = hsv2rgb({ h: _hue, s: _sat, v: _value });
323
+ this.value = `rgb(${r},${g},${b})`;
324
+ break;
325
+ }
326
+ default: {
327
+ this.value = toHex(
328
+ hsv2rgb({
329
+ h: _hue,
330
+ s: _sat,
331
+ v: _value
332
+ })
333
+ );
334
+ }
335
+ }
336
+ return;
337
+ }
338
+ switch (format) {
339
+ case "hsl": {
340
+ let [_, sat, light] = hsv2hsl({
341
+ hue: _hue,
342
+ sat: _sat / 100,
343
+ val: _value / 100
344
+ });
345
+ sat = Math.round(sat * 100);
346
+ light = Math.round(light * 100);
347
+ this.value = `hsla(${_hue}, ${sat}%, ${light}%, ${this.get("alpha") / 100})`;
348
+ break;
349
+ }
350
+ case "hsv": {
351
+ this.value = `hsva(${_hue}, ${Math.round(_sat)}%, ${Math.round(_value)}%, ${this.get("alpha") / 100})`;
352
+ break;
353
+ }
354
+ case "hex": {
355
+ this.value = `${toHex(
356
+ hsv2rgb({
357
+ h: _hue,
358
+ s: _sat,
359
+ v: _value
360
+ })
361
+ )}${hexOne(_alpha * 255 / 100)}`;
362
+ break;
363
+ }
364
+ default: {
365
+ const { r, g, b } = hsv2rgb({
366
+ h: _hue,
367
+ s: _sat,
368
+ v: _value
369
+ });
370
+ this.value = `rgba(${r}, ${g}, ${b}, ${this.get("alpha") / 100})`;
371
+ }
372
+ }
97
373
  }
98
374
  }
99
375
  export {
100
- Color as default,
101
- normalizeHexColor
376
+ Color
102
377
  };
@@ -0,0 +1,59 @@
1
+ import "../../chunk-G2ADBYYC.js";
2
+ const splitPart = (value, regExp) => {
3
+ return value.replace(regExp, "").split(/\s|,/g).filter((v) => v).map((val, idx) => {
4
+ return idx > 2 ? Number.parseFloat(val) : Number.parseInt(val, 10);
5
+ });
6
+ };
7
+ const parseHSV = (value) => {
8
+ const parts = splitPart(value, /hsva|hsv|\(|\)/gm);
9
+ if (parts.length >= 3) {
10
+ return hsv2rgb({
11
+ h: parts[0],
12
+ s: parts[1],
13
+ v: parts[2]
14
+ });
15
+ }
16
+ return { r: 0, g: 0, b: 0 };
17
+ };
18
+ const isString = (val) => typeof val === "string";
19
+ const isOnePointZero = (n) => {
20
+ return isString(n) && n.includes(".") && Number.parseFloat(n) === 1;
21
+ };
22
+ const isPercentage = (n) => {
23
+ return isString(n) && n.includes("%");
24
+ };
25
+ const normalized = (value, max) => {
26
+ if (isOnePointZero(value))
27
+ value = "100%";
28
+ const processPercent = isPercentage(value);
29
+ value = Math.min(max, Math.max(0, Number.parseFloat(`${value}`)));
30
+ if (processPercent) {
31
+ value = Number.parseInt(`${value * max}`, 10) / 100;
32
+ }
33
+ if (Math.abs(value - max) < 1e-6) {
34
+ return 1;
35
+ }
36
+ return value % max / Number.parseFloat(max);
37
+ };
38
+ const hsv2rgb = ({ h, s, v }) => {
39
+ h = normalized(h, 360) * 6;
40
+ s = normalized(s, 100);
41
+ v = normalized(v, 100);
42
+ const i = Math.floor(h);
43
+ const f = h - i;
44
+ const p = v * (1 - s);
45
+ const q = v * (1 - f * s);
46
+ const t = v * (1 - (1 - f) * s);
47
+ const mod = i % 6;
48
+ const r = [v, q, p, p, t, v][mod];
49
+ const g = [t, v, v, q, p, p][mod];
50
+ const b = [p, p, t, v, v, q][mod];
51
+ return {
52
+ r: Math.round(r * 255),
53
+ g: Math.round(g * 255),
54
+ b: Math.round(b * 255)
55
+ };
56
+ };
57
+ export {
58
+ parseHSV
59
+ };
@@ -1,5 +1,6 @@
1
1
  import "../chunk-G2ADBYYC.js";
2
- import { toggleVisible, useEvent } from "./index";
2
+ import { updateModelValue, toggleVisible, useEvent } from "./index";
3
+ import { Color } from "./utils/color";
3
4
  const api = ["state", "changeVisible", "onConfirm", "onCancel", "onHueUpdate", "onSVUpdate", "onColorUpdate"];
4
5
  const renderless = (props, ctx, { emit }) => {
5
6
  var _a, _b, _c;
@@ -28,16 +29,34 @@ const renderless = (props, ctx, { emit }) => {
28
29
  triggerBg: ctx.ref(modelValue.value),
29
30
  size,
30
31
  stack,
31
- predefineStack
32
+ predefineStack,
33
+ enablePredefineColor: ctx.computed(() => props.enablePredefineColor),
34
+ enableHistory: ctx.computed(() => props.enableHistory)
32
35
  });
33
- ctx.watch(props, () => {
34
- hex.value = props.modelValue;
35
- });
36
- ctx.watch(hex, () => {
37
- emit("update:modelValue", hex.value);
36
+ const color = new Color({
37
+ value: props.modelValue,
38
+ format: props.format,
39
+ enableAlpha: props.alpha
38
40
  });
41
+ ctx.watch(
42
+ () => [props.alpha, props.format],
43
+ () => {
44
+ color.enableAlpha = props.alpha;
45
+ color.format = props.format || color.format;
46
+ color.onChange();
47
+ updateModelValue(color.value, emit);
48
+ }
49
+ );
50
+ ctx.watch(
51
+ () => props.modelValue,
52
+ () => {
53
+ color.fromString(props.modelValue);
54
+ const { r, g, b, a } = color.toRgba();
55
+ state.hex = `rgba(${r}, ${g}, ${b}, ${a})`;
56
+ }
57
+ );
39
58
  const changeVisible = toggleVisible(isShow);
40
- const { onConfirm, onCancel } = useEvent(hex, emit, changeVisible);
59
+ const { onConfirm, onCancel } = useEvent(state, emit, changeVisible, color);
41
60
  const api2 = {
42
61
  state,
43
62
  changeVisible,
@@ -1,20 +1,71 @@
1
1
  import "../../chunk-G2ADBYYC.js";
2
- const calcLeftByAlpha = (wrapper, thumb, alpha) => {
3
- return Math.round(alpha * (wrapper.offsetWidth - thumb.offsetWidth / 2) / 100);
2
+ import { getClientXY } from "../utils/getClientXY";
3
+ const initState = ({ ref, reactive }) => {
4
+ const background = ref("");
5
+ const left = ref(0);
6
+ const state = reactive({ background, left });
7
+ return state;
4
8
  };
5
- const updateThumb = (alpha, thumb, wrapper) => {
6
- thumb.style.left = `${calcLeftByAlpha(wrapper, thumb, alpha)}px`;
9
+ const useEvent = (state, slider, alphaWrapper, alphaThumb, props) => {
10
+ const onDrag = (event) => {
11
+ if (!slider.value || !alphaThumb.value) {
12
+ return;
13
+ }
14
+ const el = alphaWrapper.value;
15
+ const rect = el.getBoundingClientRect();
16
+ const { clientX } = getClientXY(event);
17
+ let left = clientX - rect.left;
18
+ left = Math.max(alphaThumb.value.offsetWidth / 2, left);
19
+ left = Math.min(left, rect.width - alphaThumb.value.offsetWidth / 2);
20
+ const alpha = Math.round(
21
+ (left - alphaThumb.value.offsetWidth / 2) / (rect.width - alphaThumb.value.offsetWidth) * 100
22
+ );
23
+ props.color.set("alpha", alpha);
24
+ };
25
+ const onClick = (event) => {
26
+ if (event.target !== alphaThumb.value) {
27
+ onDrag(event);
28
+ }
29
+ };
30
+ const getLeft = () => {
31
+ const thumb = alphaThumb.value;
32
+ if (!thumb) {
33
+ return 0;
34
+ }
35
+ const el = alphaWrapper.value;
36
+ if (!el) {
37
+ return 0;
38
+ }
39
+ const alpha = props.color.get("alpha");
40
+ return alpha * (el.offsetWidth - thumb.offsetWidth / 2) / 100;
41
+ };
42
+ const getBackground = () => {
43
+ if (props.color && props.color.value) {
44
+ const { r, g, b } = props.color.toRgb();
45
+ return `linear-gradient(to right, rgba(${r}, ${g}, ${b}, 0) 0%, rgba(${r}, ${g}, ${b}, 1) 100%)`;
46
+ }
47
+ return "";
48
+ };
49
+ const update = () => {
50
+ state.left = getLeft();
51
+ state.background = getBackground();
52
+ };
53
+ return { onDrag, onClick, update };
7
54
  };
8
- const onDrag = (event, bar, thumb, alpha) => {
9
- const rect = bar.value.getBoundingClientRect();
10
- const { clientX } = event;
11
- let left = clientX - rect.left;
12
- left = Math.max(thumb.value.offsetWidth / 2, left);
13
- left = Math.min(left, rect.width - thumb.value.offsetWidth / 2);
14
- alpha.value = Math.round((left - thumb.value.offsetWidth / 2) / (rect.width - thumb.value.offsetWidth) * 100);
55
+ const initWatch = (props, update, { watch }) => {
56
+ watch(
57
+ () => props.color.get("alpha"),
58
+ () => update(),
59
+ { deep: true }
60
+ );
61
+ watch(
62
+ () => props.color,
63
+ () => update(),
64
+ { deep: true }
65
+ );
15
66
  };
16
67
  export {
17
- calcLeftByAlpha,
18
- onDrag,
19
- updateThumb
68
+ initState,
69
+ initWatch,
70
+ useEvent
20
71
  };