[英]How to trigger Angular Mentions on button press in html?
I have created an @ and # button to add @ and # on my WYSIWYG
editor and wish to trigger angular mentions by clicking on the html buttons.我创建了一个 @ 和 # 按钮来在我的
WYSIWYG
编辑器上添加 @ 和 # 并希望通过单击 html 按钮触发角度提及。 However the issue is whenever I press the button the mention-list
tag pops up but I cannot perform any selections on the data I have provided然而,问题是每当我按下按钮时,
mention-list
标签就会弹出,但我无法对我提供的数据执行任何选择
The code below actually triggers the Angular mentions but it also emits the close()
event after clicking of the @
or #
button so I can see the names and dates as a popup mention-list
tag on my editor but cannot select any of them.下面的代码实际上触发了 Angular 提及,但它还会在单击
@
或#
按钮后发出close()
事件,因此我可以在我的编辑器上看到名称和日期作为弹出式mention-list
标签,但无法选择其中任何一个。 How do I fix this issue so that I can trigger mentions and select names & dates instead of sending a close()
event before?如何解决此问题,以便我可以触发提及并选择名称和日期,而不是之前发送
close()
事件?
Component Html file组件 Html 文件
<div id="controls">
<button class="button btn-editor" (click)="insChar('@')">@</button>
<button class="button btn-editor" (click)="insChar('#')">#</button>
</div>
<div id="editor" (closed)="closed()" contenteditable [mentionConfig]="mentionConfig"
placeholder="Jot something down..." >
</div>
Component Typescript file组件打字稿文件
export class TextEditorComponent implements OnInit {
tribute: string;
mentionConfig: any;
ngOnInit(){
}
constructor(){
this.mentionConfig = {
mentions: [
{
items: ['Alec', 'Joyce', 'Nalin', 'Dominic'],
triggerChar: '@',
mentionSelect: (item)=>{
this.tribute = `@${item.name}`;
return this.tribute;
},
labelKey: 'name',
maxItems: 5,
disableSearch: false
},
{
items: [ '20-12-13', '13-04-19', '16-12-11'],
triggerChar: '#',
mentionSelect: (item)=>{
this.tribute = `#${item.date}`;
return this.tribute;
},
labelKey: 'date',
maxItems: 5,
disableSearch: false
}
],
};
}
insChar(char: string){
if(window.getSelection){
document.getElementById('editor').focus();
let r = window.getSelection().getRangeAt(0).cloneRange();
window.getSelection().removeAllRanges();
const a = document.createTextNode(`${char}`);
let code = char==='@'?'Digit2':'Digit3';
let event = new KeyboardEvent('keydown',{'key':`${char}`, 'code':`${code}`});
r.insertNode(a);
r.setStartAfter(a);
this.sel.addRange(r);
document.getElementById('editor').dispatchEvent(event);
}
}
closed(){ // insert mentions
console.log('closed');
if(this.tribute !== '')
{
const input = document.createElement('input');
input.setAttribute('value',`${this.tribute}`);
input.setAttribute('type','button');
input.style.border = 'none';
input.style.padding = "3px";
input.style.backgroundColor = '#dff6f0';
input.style.color = '#2e279d';
input.style.fontWeight = '';
input.style.fontSize = 'inherit';
input.style.cursor = 'pointer';
const range = window.getSelection().getRangeAt(0);
window.getSelection().removeAllRanges();
let sp = document.createTextNode(' ');
range.insertNode(input);
range.insertNode(sp);
range.setStartAfter(input);
window.getSelection().addRange(range);
this.tribute = '';
}
}
}
Your #editor
element has the (closed)
event.您的
#editor
元素具有(closed)
事件。 In your insChar()
method, you call document.getElementById('editor').dispatchEvent(event);
在您的
insChar()
方法中,您调用document.getElementById('editor').dispatchEvent(event);
That will trigger the close event on the #editor
element.这将触发
#editor
元素上的关闭事件。
So consider passing the $event
to the closed
method:所以考虑将
$event
传递给closed
方法:
<div id="editor" (closed)="closed($event)" contenteditable [mentionConfig]="mentionConfig"
placeholder="Jot something down..." >
</div>
And checking to see what triggered the close event, to filter out the events that are trigged by you manually dispatching the event.并检查是什么触发了关闭事件,以过滤掉由您手动调度事件触发的事件。
insChar(char: string){
if(window.getSelection){
document.getElementById('editor').focus();
let code = char==='@'?'Digit2':'Digit3';
let event = new KeyboardEvent('keydown',{'key':`${char}`, 'code':`${code}`});
document.getElementById('editor').dispatchEvent(event); //keyboard event first
let r = window.getSelection().getRangeAt(0).cloneRange();
window.getSelection().removeAllRanges();
const a = document.createTextNode(`${char}`);
r.insertNode(a);
r.setStartAfter(a);
window.getSelection().addRange(r); //inserting @ or # later
}
}
The closed() event is always triggered first, I don't know the reason but I realized that the keyboard event actually triggered after the opened()
event of angular mentions. closed() 事件总是先触发,我不知道原因,但我意识到键盘事件实际上是在 angular 提到的
opened()
事件之后触发的。 Somehow this code works best.不知何故,这段代码效果最好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.