Skip to content

Commit 8a27552

Browse files
committed
change event emitted when there was actual change in color
1 parent 1b64759 commit 8a27552

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

.github/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ Notice that event data is always passed in __hsv__ format.
509509
| __alphaInputStart__ | alpha adjustment has begun.<br />This is only emitted when pointerdown over the alpha slider is registered.<br />This will _not_ emit when alpha is changed from text inputs | current state of alpha<br />`{ a: 0.5 }` |
510510
| __alphaInputEnd__ | alpha adjustment has ended.<br />This is only emitted when pointerup of the alpha slider is registered.<br />This will _not_ emit when alpha is changed from text inputs | current state of alpha<br />`{ a: 0.5 }` |
511511
| __alphaInput__ | alpha is being adjusted.<br />This will emit every time alpha is changed, including text inputs | current state of alpha<br />`{ a: 0.5 }` |
512-
| __change__ | the adjustment of some parameter has ended. <br />This will emit every time _any_ parameter is changed. <br />This _will_ emit when color is changed from text inputs as well, on blur | current state of all color components<br />`{ h: 180, s: 0.5, v: 0.5, a: 0.5 }` |
512+
| __change__ | the color has changed by user interaction.<br />This will emit every time _any_ parameter is changed. <br />This _will_ emit when color is changed from text inputs as well, on blur | current state of all color components<br />`{ h: 180, s: 0.5, v: 0.5, a: 0.5 }` |
513513

514514
## Example
515515

src/components/color-picker.vue

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,17 @@ export default {
231231
document.addEventListener('pointermove', this.saturationPickMove);
232232
this.saturationPickMove(e);
233233
this.emitHook('saturationInputStart', { s: this.s, v: this.v });
234+
this.colorSnapshot = this.color.toRgbString(); // this to track change
234235
},
235236
saturationPickEnd(e) {
237+
console.log('end');
236238
document.removeEventListener('pointerup', this.saturationPickEnd);
237239
document.removeEventListener('pointermove', this.saturationPickMove);
238240
this.emitHook('saturationInputEnd', { s: this.s, v: this.v });
239-
this.emitHook('change', { h: this.h, s: this.s, v: this.v, a: this.a });
241+
if (this.colorSnapshot !== this.color.toRgbString()) {
242+
// something changed, emit change hook
243+
this.emitHook('change', { h: this.h, s: this.s, v: this.v, a: this.a });
244+
}
240245
},
241246
saturationPickMove(e) {
242247
if (e.clientX >= this.saturationCanvasRect.x && e.clientX <= this.saturationCanvasRect.right) {
@@ -254,12 +259,16 @@ export default {
254259
document.addEventListener('pointermove', this.huePickMove);
255260
this.huePickMove(e);
256261
this.emitHook('hueInputStart', { h: this.h });
262+
this.colorSnapshot = this.color.toRgbString(); // this to track change
257263
},
258264
huePickEnd(e) {
259265
document.removeEventListener('pointerup', this.huePickEnd);
260266
document.removeEventListener('pointermove', this.huePickMove);
261267
this.emitHook('hueInputEnd', { h: this.h });
262-
this.emitHook('change', { h: this.h, s: this.s, v: this.v, a: this.a });
268+
if (this.colorSnapshot !== this.color.toRgbString()) {
269+
// something changed, emit change hook
270+
this.emitHook('change', { h: this.h, s: this.s, v: this.v, a: this.a });
271+
}
263272
},
264273
huePickMove(e) {
265274
if (e.clientX >= this.hueCanvasRect.x && e.clientX <= this.hueCanvasRect.right) {
@@ -273,12 +282,16 @@ export default {
273282
document.addEventListener('pointermove', this.alphaPickMove);
274283
this.alphaPickMove(e);
275284
this.emitHook('alphaInputStart', { a: this.a });
285+
this.colorSnapshot = this.color.toRgbString(); // this to track change
276286
},
277287
alphaPickEnd(e) {
278288
document.removeEventListener('pointerup', this.alphaPickEnd);
279289
document.removeEventListener('pointermove', this.alphaPickMove);
280290
this.emitHook('alphaInputEnd', { a: this.a });
281-
this.emitHook('change', { h: this.h, s: this.s, v: this.v, a: this.a });
291+
if (this.colorSnapshot !== this.color.toRgbString()) {
292+
// something changed, emit change hook
293+
this.emitHook('change', { h: this.h, s: this.s, v: this.v, a: this.a });
294+
}
282295
},
283296
alphaPickMove(e) {
284297
if (e.clientX >= this.alphaCanvasRect.x && e.clientX <= this.alphaCanvasRect.right) {
@@ -297,6 +310,7 @@ export default {
297310
value = Number(value.toFixed(3));
298311
}
299312
this.$emit(eventName, value);
313+
// if (eventName=='change') console.log('change')
300314
},
301315
textInputInputHandler(e) {
302316
const component = e.target.dataset.component;
@@ -379,16 +393,25 @@ export default {
379393
textInputFocusHandler(e) {
380394
// if focused from blur, freeze current color
381395
// if focused from another text input, don't update
382-
if (!this.textInputActive) this.textInputsFreeze = { ...this.textInputs };
396+
if (!this.textInputActive) {
397+
this.textInputsFreeze = { ...this.textInputs };
398+
this.colorSnapshot = this.color.toRgbString(); // this to track change
399+
}
383400
this.textInputActive = e.target.dataset.component;
384401
},
385402
textInputBlurHandler(e) {
386403
setTimeout(() => {
387404
if (this.textInputActive === e.target.dataset.component) {
388405
// actually blurred, not just focused another
406+
407+
// check if something actually changed
408+
if (this.colorSnapshot !== this.color.toRgbString()) {
409+
// something changed, emit change hook
410+
this.emitHook('change', { h: this.h, s: this.s, v: this.v, a: this.a });
411+
}
412+
389413
this.textInputsFreeze = {};
390414
this.textInputActive = null;
391-
this.emitHook('change', { h: this.h, s: this.s, v: this.v, a: this.a });
392415
}
393416
}, 0);
394417
},

0 commit comments

Comments
 (0)