描述:
在我的Angular 12项目中,我有一个Summernote编辑器(https://www.npmjs.com/package/ngx-summernote),并且在该编辑器之外还有一个按钮.当用户单击此按钮时,我希望在编辑器中的当前光标位置插入文本"Heeello".
我曾try 通过ngx-Summernote指令访问编辑器对象,但一直收到类似"无法读取未定义属性"之类的错误.我也看过Summernote API文档,但我不确定如何在我的ANGLE项目中使用它.
<div [ngxSummernote]="config"></div>
Component.ts
import { NgxSummernoteDirective } from 'ngx-summernote';
@ViewChild(NgxSummernoteDirective) ngxSummernote: NgxSummernoteDirective;
config = {
placeholder: '',
tabsize: 2,
height: '200px',
uploadImagePath: '/api/upload',
toolbar: [
['misc', ['codeview', 'undo', 'redo']],
['style', ['bold', 'italic', 'underline', 'clear']],
['font', ['bold', 'italic', 'underline', 'strikethrough', 'superscript', 'subscript', 'clear']],
['fontsize', ['fontname', 'fontsize', 'color']],
['para', ['style', 'ul', 'ol', 'paragraph', 'height']],
['insert', ['table', 'picture', 'link', 'video', 'hr']]
],
fontNames: ['Helvetica', 'Arial', 'Arial Black', 'Comic Sans MS', 'Courier New', 'Roboto', 'Times']
}
...
}
addText(){
console.log(this.ngxSummernote);
console.log(this.ngxSummernote['_editor'][0] )
this.ngxSummernote['_editor'].insertText('as');
const currentPosition = this.ngxSummernote['_editor'].sumo.getSelection().index;
console.log(currentPosition)
// const currentCursorPosition = ($(this.mySummernote.nativeElement) as any).summernote('code').length;
// const textToInsert = 'Hello';
}
上述方法都没有奏效.下面是指向我的代码的StackBlitz链接:https://stackblitz.com/edit/angular-summernote-demo-duemtn?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts
有人能帮我弄清楚如何在Summernote编辑器的当前光标位置以12角插入文本吗?