Reputation: 6003
new-status.component.ts
<mat-form-field>
<textarea matInput placeholder="Status Content" style="height: 200px;"></textarea>
<emoji-mart title="Pick your emoji…" emoji="point_up"></emoji-mart>
</mat-form-field>
i follow this link for solution(https://github.com/TypeCtrl/ngx-emoji-mart), now i want put emoji picker in input field or textarea.
Upvotes: 4
Views: 23294
Reputation: 1830
document.execCommand() is now deprecated.
I have an alternative solution to your problem, which takes into consideration current caret position. The only downside is that it wipes undo/redo history.
Emoji html:
<emoji-mart *ngIf="showEmojiWindow"
[showPreview]="false"
(emojiClick)="addEmoji($event)"
[isNative]="true"
[showSingleCategory]="true"
[emojiTooltip]="true"></emoji-mart>
Form html:
<form [formGroup]="sendMessageForm" (ngSubmit)="submit(sendMessageForm)">
<input #messageInput type="text" name="message" formControlName="message" class="form-control">
</form>
Typescript:
@ViewChild('messageInput')
messageInput: ElementRef;
sendMessageForm: FormGroup;
private messageFormControl: FormControl;
ngOnInit(): void {
this.buildSendMessageForm();
}
addEmoji(event: EmojiEvent): void {
this.messageInput.nativeElement.focus();
const selectionStart = this.messageInput.nativeElement.selectionStart;
const currentValue = this.messageInput.nativeElement.value;
const newValue = currentValue.substring(0, selectionStart) + event.emoji.native + currentValue.substring(selectionStart);
this.messageFormControl.setValue(newValue);
// the following 2 lines set the caret position behind the emoji
this.messageInput.nativeElement.selectionStart = selectionStart + event.emoji.native.length;
this.messageInput.nativeElement.selectionEnd = selectionStart + event.emoji.native.length;
this.showEmojiWindow = false;
}
private buildSendMessageForm(): void {
this.messageFormControl = new FormControl('');
this.sendMessageForm = new FormGroup({
message: this.messageFormControl
});
}
Upvotes: 2
Reputation: 1880
I know its late but maybe someone else could use it.
I have improved those available solutions. emoji should be inserted on carrot position, and we should be able to undo ,redo on message box here is solution.
HTML
<textarea #input [formControl]="message" (keydown.enter)="send()"> </textarea>
<emoji-mart
title="Pick your emoji…"
(emojiSelect)="addEmoji($event)"
[hideObsolete]="true"
[isNative]="true">
</emoji-mart>
TS
addEmoji(selected: Emoji) {
const emoji: string = (selected.emoji as any).native;
const input = this.input.nativeElement;
input.focus();
if (document.execCommand){ // document.execCommand is absolute but it //add support for undo redo and insert emoji at carrot position
//any one has better solution ?
var event = new Event('input');
document.execCommand('insertText', false, emoji);
return;
}
// insert emoji on carrot position
const [start, end] = [input.selectionStart, input.selectionEnd];
input.setRangeText(emoji, start, end, 'end');
}
Upvotes: 6
Reputation: 422
let's begin by
npm install @ctrl/ngx-emoji-mart
then,
import { PickerModule } from '@ctrl/ngx-emoji-mart'
add the stylesheet in angular.json:
"styles": [
"src/styles.css",
"../node_modules/@ctrl/ngx-emoji-mart/picker.css"
],
add the module in the imports array of app.module.ts:
@NgModule({
...,
imports: [ ..., PickerModule, ... ],
...
})
finally add for testing to see if all work in app.component.html :
<emoji-mart title="Pick your emoji…" emoji="point_up"></emoji-mart>
That's it :-)
https://stackblitz.com/edit/angular-rxanqx?file=src%2Fapp%2Fapp.component.html
EDIT
There is a raw approach you need to stylisize.
https://stackblitz.com/edit/angular-rxanqx
you have a textarea a button to add an emoticon to your text.
let me know if it's a good way for you to start :-)
Upvotes: 11
Reputation: 41
npm install @ctrl/ngx-emoji-mart
then in your module.ts
import { PickerModule } from '@ctrl/ngx-emoji-mart'
add its style in your styles.scss
@import '~@ctrl/ngx-emoji-mart/picker';
in your component.html
<form [formGroup]="emojiForm">
<emoji-mart (emojiClick)="addEmoji($event)" emoji="point_up"></emoji-mart>
<textarea type="text" formControlName="inputField"></textarea>
</form>
in you component.ts
addEmoji($event){
let data = this.emojiForm.get('inputField');
data.patchValue(data.value + $event.emoji.native)
}
Upvotes: 4