|
| 1 | +import { ThemeService } from './../../services/theme.service'; |
| 2 | +import { |
| 3 | + Component, |
| 4 | + Output, |
| 5 | + EventEmitter, |
| 6 | + ViewChild, |
| 7 | + Compiler, |
| 8 | + Injector, |
| 9 | + ChangeDetectorRef, |
| 10 | + ComponentFactoryResolver, |
| 11 | + ViewContainerRef, |
| 12 | +} from '@angular/core'; |
| 13 | +import { NgxPopperjsContentComponent } from 'ngx-popperjs'; |
| 14 | +import { Subscription } from 'rxjs'; |
| 15 | + |
| 16 | +@Component({ |
| 17 | + selector: 'm-emojiPicker', |
| 18 | + templateUrl: './emoji-picker.component.html', |
| 19 | +}) |
| 20 | +export class EmojiPickerComponent { |
| 21 | + popperPlacement: string = 'bottom'; |
| 22 | + showSelector = false; |
| 23 | + |
| 24 | + @ViewChild('emojiPickerOutlet', { read: ViewContainerRef }) |
| 25 | + emojiPickerOutlet: ViewContainerRef; |
| 26 | + |
| 27 | + /** |
| 28 | + * On Post event emitter |
| 29 | + */ |
| 30 | + @Output('emojiSelect') emojiSelectEmitter: EventEmitter< |
| 31 | + any |
| 32 | + > = new EventEmitter(); |
| 33 | + popperModifiers: Array<any> = []; |
| 34 | + @ViewChild('popper') popper: NgxPopperjsContentComponent; |
| 35 | + |
| 36 | + onSelectSubscription: Subscription; |
| 37 | + |
| 38 | + constructor( |
| 39 | + public themeService: ThemeService, |
| 40 | + private compiler: Compiler, |
| 41 | + private injector: Injector, |
| 42 | + private cd: ChangeDetectorRef, |
| 43 | + private componentFactoryResolver: ComponentFactoryResolver |
| 44 | + ) {} |
| 45 | + |
| 46 | + emojiSelect($event) { |
| 47 | + this.emojiSelectEmitter.emit($event.emoji); |
| 48 | + this.popper?.hide(); |
| 49 | + } |
| 50 | + |
| 51 | + onShowSelector() { |
| 52 | + this.showSelector = true; |
| 53 | + this.detectChanges(); |
| 54 | + this.loadEmojiMartComponent(); |
| 55 | + } |
| 56 | + |
| 57 | + onHideSelector() { |
| 58 | + this.showSelector = false; |
| 59 | + this.detectChanges(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Asyncronously loads in the emoji mark module |
| 64 | + */ |
| 65 | + async loadEmojiMartComponent() { |
| 66 | + // Import our module |
| 67 | + const { PickerComponent } = await import('@ctrl/ngx-emoji-mart'); |
| 68 | + |
| 69 | + // const moduleFactory = await this.compiler.compileModuleAsync(PickerModule); |
| 70 | + // const moduleRef = moduleFactory.create(this.injector); |
| 71 | + |
| 72 | + // Resolves the available components |
| 73 | + const componentFactory = this.componentFactoryResolver.resolveComponentFactory( |
| 74 | + PickerComponent |
| 75 | + ); |
| 76 | + |
| 77 | + if (!this.emojiPickerOutlet) { |
| 78 | + console.error('Could not find emoji picker outlet'); |
| 79 | + } |
| 80 | + |
| 81 | + // Clear previous component |
| 82 | + this.emojiPickerOutlet.clear(); |
| 83 | + |
| 84 | + // Attach the component |
| 85 | + const componentRef = this.emojiPickerOutlet.createComponent( |
| 86 | + componentFactory |
| 87 | + ); |
| 88 | + |
| 89 | + componentRef.instance.autoFocus = true; |
| 90 | + componentRef.instance.isNative = true; |
| 91 | + componentRef.instance.title = 'Pick your emoji…'; |
| 92 | + componentRef.instance.emoji = 'point_up'; |
| 93 | + componentRef.instance.color = '#1b85d6'; |
| 94 | + componentRef.instance.darkMode = this.themeService.isDark$.getValue(); |
| 95 | + |
| 96 | + this.onSelectSubscription?.unsubscribe(); |
| 97 | + |
| 98 | + this.onSelectSubscription = componentRef.instance.emojiSelect.subscribe( |
| 99 | + event => { |
| 100 | + this.emojiSelect(event); |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + // Trigger change detection |
| 105 | + this.detectChanges(); |
| 106 | + } |
| 107 | + |
| 108 | + ngOnDestroy() { |
| 109 | + this.onSelectSubscription?.unsubscribe(); |
| 110 | + } |
| 111 | + |
| 112 | + detectChanges() { |
| 113 | + this.cd.markForCheck(); |
| 114 | + this.cd.detectChanges(); |
| 115 | + } |
| 116 | +} |
0 commit comments