-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(color-picker): attempt to fix nondeterministic color-picker bug l…
…ogic rewriting
- Loading branch information
Showing
4 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ple-text-editor/src/lib/components/editor-color-picker/editor-color-picker.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<button class="st-button st-toolbar-item" [title]="button?.title || ''" (stAction)="colorPicker.showPicker()"> | ||
{{rgbStringToHex(state)}} | ||
<input #colorPicker class="st-color-picker" type="color" [ngModel]="rgbStringToHex(state)" tabindex="-1" | ||
(change)="onCommand(button?.command, $event.target.value)" onmousedown='return false;' onselectstart='return false;'> | ||
(stNgModelChangeDebounced)="onCommand(button?.command, $event)"> | ||
<i [class]="button?.icon" [style.color]="state"></i> | ||
</button> |
38 changes: 38 additions & 0 deletions
38
...cts/ngx-simple-text-editor/src/lib/directives/ng-model-change-debounced.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { NgModelChangeDebouncedDirective } from './ng-model-change-debounced.directive'; | ||
import {from, of} from "rxjs"; | ||
import {NgModel} from "@angular/forms"; | ||
import {fakeAsync, flush} from "@angular/core/testing"; | ||
|
||
describe('NgModelChangeDebouncedDirective', () => { | ||
|
||
it('should create an instance', () => { | ||
const ngModel = {control: {valueChanges: of()}} as NgModel; | ||
|
||
const directive = new NgModelChangeDebouncedDirective(ngModel); | ||
|
||
expect(directive).toBeTruthy(); | ||
}); | ||
|
||
it('should emit debounced ngChange event', fakeAsync(() => { | ||
const ngModel = {control: {valueChanges: from([null, '1', '2'])}} as NgModel; | ||
const directive = new NgModelChangeDebouncedDirective(ngModel); | ||
directive.stNgModelChangeDebounced.emit = jasmine.createSpy(); | ||
|
||
directive.ngOnInit(); | ||
|
||
expect(directive.stNgModelChangeDebounced.emit).toHaveBeenCalledWith('2'); | ||
flush(); | ||
})); | ||
|
||
it('should unsubscribe from ngModel', () => { | ||
const unsubscribe = jasmine.createSpy(); | ||
const observable = {subscribe: () => ({unsubscribe})}; | ||
const ngModel = {control: {valueChanges: {pipe: () => observable}}} as unknown as NgModel; | ||
const directive = new NgModelChangeDebouncedDirective(ngModel); | ||
directive.ngOnInit(); | ||
|
||
directive.ngOnDestroy(); | ||
|
||
expect(unsubscribe).toHaveBeenCalled(); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
projects/ngx-simple-text-editor/src/lib/directives/ng-model-change-debounced.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Directive, EventEmitter, OnDestroy, OnInit, Output} from '@angular/core'; | ||
import {NgModel} from "@angular/forms"; | ||
import {Subscription} from "rxjs"; | ||
import {debounceTime, distinctUntilChanged, skip} from "rxjs/operators"; | ||
|
||
const DEBOUNCE_TIME_MS = 100; | ||
|
||
@Directive({ | ||
selector: '[stNgModelChangeDebounced]', | ||
}) | ||
export class NgModelChangeDebouncedDirective implements OnInit, OnDestroy { | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
@Output() stNgModelChangeDebounced = new EventEmitter<any>(); | ||
private subscription: Subscription; | ||
|
||
constructor(private readonly ngModel: NgModel) {} | ||
|
||
ngOnInit(): void { | ||
this.subscription = this.ngModel.control.valueChanges.pipe( | ||
skip(1), | ||
distinctUntilChanged(), | ||
debounceTime(DEBOUNCE_TIME_MS) | ||
).subscribe((value) => this.stNgModelChangeDebounced.emit(value)); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.subscription.unsubscribe(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters