如何在 angular 中使用表情符号选择器制作输入字段或文本区域?
How to make input field or textarea with emoji-picker in angular?
新-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>
我按照这个link解决方案(https://github.com/TypeCtrl/ngx-emoji-mart),现在我想把表情符号选择器放在输入字段或文本区域中。
让我们从
开始
npm install @ctrl/ngx-emoji-mart
那么,
import { PickerModule } from '@ctrl/ngx-emoji-mart'
在angular.json中添加样式表:
"styles": [
"src/styles.css",
"../node_modules/@ctrl/ngx-emoji-mart/picker.css"
],
在 app.module.ts:
的导入数组中添加模块
@NgModule({
...,
imports: [ ..., PickerModule, ... ],
...
})
最后添加以进行测试以查看 app.component.html 中是否所有工作:
<emoji-mart title="Pick your emoji…" emoji="point_up"></emoji-mart>
就是这样:-)
https://stackblitz.com/edit/angular-rxanqx?file=src%2Fapp%2Fapp.component.html
编辑
有一种原始方法需要风格化。
https://stackblitz.com/edit/angular-rxanqx
你有一个 textarea 按钮,可以在你的文本中添加表情符号。
让我知道这是否是您开始的好方法:-)
npm install @ctrl/ngx-emoji-mart
然后在你的 module.ts
import { PickerModule } from '@ctrl/ngx-emoji-mart'
在您的 styles.scss
中添加其样式
@import '~@ctrl/ngx-emoji-mart/picker';
在你的 component.html
<form [formGroup]="emojiForm">
<emoji-mart (emojiClick)="addEmoji($event)" emoji="point_up"></emoji-mart>
<textarea type="text" formControlName="inputField"></textarea>
</form>
在你里面component.ts
addEmoji($event){
let data = this.emojiForm.get('inputField');
data.patchValue(data.value + $event.emoji.native)
}
我知道它晚了,但也许其他人可以使用它。
我改进了那些可用的解决方案。
表情符号应该插入胡萝卜位置,我们应该能够在消息框上撤消,重做
这是解决方案。
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');
}
document.execCommand() is now deprecated.
对于您的问题,我有一个替代解决方案,它考虑了当前插入符号的位置。唯一的缺点是它会擦除 undo/redo 历史记录。
表情符号html:
<emoji-mart *ngIf="showEmojiWindow"
[showPreview]="false"
(emojiClick)="addEmoji($event)"
[isNative]="true"
[showSingleCategory]="true"
[emojiTooltip]="true"></emoji-mart>
表格html:
<form [formGroup]="sendMessageForm" (ngSubmit)="submit(sendMessageForm)">
<input #messageInput type="text" name="message" formControlName="message" class="form-control">
</form>
打字稿:
@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
});
}
新-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>
我按照这个link解决方案(https://github.com/TypeCtrl/ngx-emoji-mart),现在我想把表情符号选择器放在输入字段或文本区域中。
让我们从
开始npm install @ctrl/ngx-emoji-mart
那么,
import { PickerModule } from '@ctrl/ngx-emoji-mart'
在angular.json中添加样式表:
"styles": [
"src/styles.css",
"../node_modules/@ctrl/ngx-emoji-mart/picker.css"
],
在 app.module.ts:
的导入数组中添加模块@NgModule({
...,
imports: [ ..., PickerModule, ... ],
...
})
最后添加以进行测试以查看 app.component.html 中是否所有工作:
<emoji-mart title="Pick your emoji…" emoji="point_up"></emoji-mart>
就是这样:-)
https://stackblitz.com/edit/angular-rxanqx?file=src%2Fapp%2Fapp.component.html
编辑
有一种原始方法需要风格化。
https://stackblitz.com/edit/angular-rxanqx
你有一个 textarea 按钮,可以在你的文本中添加表情符号。
让我知道这是否是您开始的好方法:-)
npm install @ctrl/ngx-emoji-mart
然后在你的 module.ts
import { PickerModule } from '@ctrl/ngx-emoji-mart'
在您的 styles.scss
中添加其样式@import '~@ctrl/ngx-emoji-mart/picker';
在你的 component.html
<form [formGroup]="emojiForm">
<emoji-mart (emojiClick)="addEmoji($event)" emoji="point_up"></emoji-mart>
<textarea type="text" formControlName="inputField"></textarea>
</form>
在你里面component.ts
addEmoji($event){
let data = this.emojiForm.get('inputField');
data.patchValue(data.value + $event.emoji.native)
}
我知道它晚了,但也许其他人可以使用它。
我改进了那些可用的解决方案。 表情符号应该插入胡萝卜位置,我们应该能够在消息框上撤消,重做 这是解决方案。
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');
}
document.execCommand() is now deprecated.
对于您的问题,我有一个替代解决方案,它考虑了当前插入符号的位置。唯一的缺点是它会擦除 undo/redo 历史记录。
表情符号html:
<emoji-mart *ngIf="showEmojiWindow"
[showPreview]="false"
(emojiClick)="addEmoji($event)"
[isNative]="true"
[showSingleCategory]="true"
[emojiTooltip]="true"></emoji-mart>
表格html:
<form [formGroup]="sendMessageForm" (ngSubmit)="submit(sendMessageForm)">
<input #messageInput type="text" name="message" formControlName="message" class="form-control">
</form>
打字稿:
@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
});
}