Skip to content

Commit 021a34f

Browse files
committed
fix(ngx-jodit): databinding using [(value)] resets cursor on change
1 parent c180147 commit 021a34f

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

libs/ngx-jodit/src/lib/ngx-jodit.component.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { CommonModule } from '@angular/common';
1+
import {CommonModule} from '@angular/common';
22
import {
33
AfterViewInit,
44
ChangeDetectionStrategy,
55
ChangeDetectorRef,
66
Component,
77
ElementRef,
88
EventEmitter,
9+
forwardRef,
910
Input,
1011
OnDestroy,
1112
Output,
1213
ViewChild,
13-
forwardRef,
1414
} from '@angular/core';
15-
import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR } from '@angular/forms';
16-
import { Jodit } from 'jodit';
17-
import { BehaviorSubject, Subscription, combineLatest, delay, distinctUntilChanged, filter, withLatestFrom } from 'rxjs';
15+
import {ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR} from '@angular/forms';
16+
import {Jodit} from 'jodit';
17+
import {BehaviorSubject, combineLatest, delay, distinctUntilChanged, filter, Subscription, withLatestFrom} from 'rxjs';
1818

19-
import { JoditConfig } from './types';
19+
import {JoditConfig} from './types';
2020

2121
@Component({
2222
selector: 'ngx-jodit',
@@ -50,9 +50,14 @@ export class NgxJoditComponent implements ControlValueAccessor, AfterViewInit, O
5050

5151
// value property (subject)
5252
private valueSubject: BehaviorSubject<string> = new BehaviorSubject<string>('');
53+
5354
@Input() set value(value: string) {
5455
const sanitizedText = this.prepareText(value);
55-
this.valueSubject.next(sanitizedText);
56+
if (!this.internValueChange) {
57+
this.valueSubject.next(sanitizedText);
58+
} else {
59+
this.internValueChange = false;
60+
}
5661
this.onChange(sanitizedText);
5762
}
5863

@@ -81,6 +86,7 @@ export class NgxJoditComponent implements ControlValueAccessor, AfterViewInit, O
8186
// Used for delay value assignment to wait for jodit to be initialized
8287
private joditInitializedSubject: BehaviorSubject<boolean> = new BehaviorSubject(false);
8388
private valueSubscription?: Subscription;
89+
private internValueChange = false;
8490

8591
constructor(
8692
private readonly cdr: ChangeDetectorRef,
@@ -131,6 +137,7 @@ export class NgxJoditComponent implements ControlValueAccessor, AfterViewInit, O
131137
this.jodit = Jodit.make(this.joditContainer.nativeElement, this._options);
132138
this.jodit.value = this.valueSubject.getValue();
133139
this.jodit.events.on('change', (text: string) => {
140+
this.internValueChange = true;
134141
this.changeValue(text);
135142
this.joditChange.emit(text);
136143
this.onChange(text);

0 commit comments

Comments
 (0)