Skip to content

Commit aeb6169

Browse files
refactor: use effect, computed instead of toObservable
1 parent e3c3017 commit aeb6169

File tree

4 files changed

+40
-36
lines changed

4 files changed

+40
-36
lines changed

projects/ngx-quill/src/lib/quill-editor.component.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Component,
1111
DestroyRef,
1212
Directive,
13+
effect,
1314
ElementRef,
1415
EventEmitter,
1516
forwardRef,
@@ -22,7 +23,7 @@ import {
2223
signal,
2324
ViewEncapsulation
2425
} from '@angular/core'
25-
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop'
26+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'
2627
import { debounceTime, fromEvent, Subscription } from 'rxjs'
2728
import { mergeMap } from 'rxjs/operators'
2829

@@ -299,12 +300,15 @@ export abstract class QuillEditorBase implements ControlValueAccessor, Validator
299300
})
300301
})
301302

302-
toObservable(this.customToolbarPosition).pipe(takeUntilDestroyed()).subscribe((customToolbarPosition) => {
303+
effect(() => {
304+
const customToolbarPosition = this.customToolbarPosition()
303305
if (this.init && this.toolbarPosition() !== customToolbarPosition) {
304306
this.toolbarPosition.set(customToolbarPosition)
305307
}
306308
})
307-
toObservable(this.readOnly).pipe(takeUntilDestroyed()).subscribe((readOnly) => {
309+
310+
effect(() => {
311+
const readOnly = this.readOnly()
308312
if (this.init) {
309313
if (readOnly) {
310314
this.quillEditor?.disable()
@@ -313,8 +317,16 @@ export abstract class QuillEditorBase implements ControlValueAccessor, Validator
313317
}
314318
}
315319
})
316-
toObservable(this.placeholder).pipe(takeUntilDestroyed()).subscribe((placeholder) => { if (this.init && this.quillEditor) this.quillEditor.root.dataset.placeholder = placeholder })
317-
toObservable(this.styles).pipe(takeUntilDestroyed()).subscribe((styles) => {
320+
321+
effect(() => {
322+
const placeholder = this.placeholder()
323+
if (this.init && this.quillEditor) {
324+
this.quillEditor.root.dataset.placeholder = placeholder
325+
}
326+
})
327+
328+
effect(() => {
329+
const styles = this.styles()
318330
if (!this.init || !this.editorElem) {
319331
return
320332
}
@@ -332,8 +344,10 @@ export abstract class QuillEditorBase implements ControlValueAccessor, Validator
332344
})
333345
}
334346
})
335-
toObservable(this.classes).pipe(takeUntilDestroyed()).subscribe((classes) => {
336-
if (!this.init || !this.editorElem) {
347+
348+
effect(() => {
349+
const classes = this.classes()
350+
if (!this.init || !this.quillEditor) {
337351
return
338352
}
339353
const currentClasses = classes
@@ -347,7 +361,9 @@ export abstract class QuillEditorBase implements ControlValueAccessor, Validator
347361
this.addClasses(currentClasses)
348362
}
349363
})
350-
toObservable(this.debounceTime).pipe(takeUntilDestroyed()).subscribe((debounceTime) => {
364+
365+
effect(() => {
366+
const debounceTime = this.debounceTime()
351367
if (!this.init || !this.quillEditor) {
352368
return
353369
}

projects/ngx-quill/src/lib/quill-view-html.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('Basic QuillViewHTMLComponent', () => {
1616
const element = fixture.nativeElement
1717

1818
expect(element.querySelectorAll('.ql-editor').length).toBe(1)
19-
expect(fixture.componentInstance.themeClass()).toBe('ql-snow')
19+
expect(fixture.componentInstance.themeClass()).toBe('ql-snow ngx-quill-view-html')
2020
const viewElement = element.querySelector('.ql-container.ql-snow.ngx-quill-view-html > .ql-editor')
2121
expect(viewElement).toBeDefined()
2222
})
Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
1+
import { DomSanitizer } from '@angular/platform-browser'
22
import { QuillService } from './quill.service'
33

44
import {
55
Component,
66
ViewEncapsulation,
7+
computed,
78
inject,
8-
input,
9-
signal
9+
input
1010
} from '@angular/core'
11-
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop'
12-
import { combineLatest } from 'rxjs'
1311

1412
@Component({
1513
encapsulation: ViewEncapsulation.None,
@@ -31,27 +29,16 @@ export class QuillViewHTMLComponent {
3129
readonly theme = input<string | undefined>(undefined)
3230
readonly sanitize = input<boolean | undefined>(undefined)
3331

34-
readonly innerHTML = signal<SafeHtml>('')
35-
readonly themeClass = signal('ql-snow')
32+
readonly innerHTML = computed(() => {
33+
const sanitize = this.sanitize()
34+
const content = this.content()
35+
return ([true, false].includes(sanitize) ? sanitize : (this.service.config.sanitize || false)) ? content : this.sanitizer.bypassSecurityTrustHtml(content)
36+
})
37+
readonly themeClass = computed(() => {
38+
const base = this.service.config.theme ? this.service.config.theme : 'snow'
39+
return `ql-${this.theme() || base} ngx-quill-view-html`
40+
})
3641

3742
private sanitizer = inject(DomSanitizer)
3843
private service = inject(QuillService)
39-
40-
constructor() {
41-
toObservable(this.theme).pipe(takeUntilDestroyed()).subscribe((newTheme) => {
42-
if (newTheme) {
43-
const theme = newTheme || (this.service.config.theme ? this.service.config.theme : 'snow')
44-
this.themeClass.set(`ql-${theme} ngx-quill-view-html`)
45-
} else {
46-
const theme = this.service.config.theme ? this.service.config.theme : 'snow'
47-
this.themeClass.set(`ql-${theme} ngx-quill-view-html`)
48-
}
49-
})
50-
51-
combineLatest([toObservable(this.content), toObservable(this.sanitize)]).pipe(takeUntilDestroyed()).subscribe(([content, shouldSanitize]) => {
52-
const sanitize = [true, false].includes(shouldSanitize) ? shouldSanitize : (this.service.config.sanitize || false)
53-
const innerHTML = sanitize ? content : this.sanitizer.bypassSecurityTrustHtml(content)
54-
this.innerHTML.set(innerHTML)
55-
})
56-
}
5744
}

projects/ngx-quill/src/lib/quill-view.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {
1212
SecurityContext,
1313
ViewEncapsulation,
1414
afterNextRender,
15+
effect,
1516
inject,
1617
input
1718
} from '@angular/core'
18-
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop'
1919
import { DomSanitizer } from '@angular/platform-browser'
2020
import { mergeMap } from 'rxjs/operators'
2121

@@ -125,7 +125,8 @@ export class QuillViewComponent {
125125
this.destroyRef.onDestroy(() => quillSubscription.unsubscribe())
126126
})
127127

128-
toObservable(this.content).pipe(takeUntilDestroyed()).subscribe((content) => {
128+
effect(() => {
129+
const content = this.content()
129130
if (!this.quillEditor || !this.init) {
130131
return
131132
}

0 commit comments

Comments
 (0)