所以我在Angular的Quill编辑器中实现了一个文本自动完成功能(),它为Quill编辑器的ngModel变量追加了一个用于测试的伪文本。(ngModel变量)有内容,检查其不带HTML标记的实际内容是否大于10(所以自动完成,直到一对夫妇的话写),并检查它是否有自动完成之前太(有一个跨度的额外文本?)
autoCorrect(): void {
if (this.htmlText && stripHTML(this.htmlText).length > 10 && !this.htmlText.includes("<span")){
if (this.htmlText.endsWith("</p>")){
let pos = this.htmlText.indexOf("</p>")
this.autocorrectText = this.htmlText.slice(0,pos) + "<span style=\"color: rgb(187, 187, 187);\">" + " finds you well" + "</span>" + this.htmlText.slice(pos);
this.htmlText = this.autocorrectText;
let range = this.editor.quillEditor.getSelection();
this.editor.quillEditor.insertText(range!.index, this.autocorrectText, 'bold', true);
}
但问题是,它使用这种方式处理这些虚拟数据,但光标会移回文本编辑器的开头
如果我尝试使用getSelection和insertText函数来实现它,就像我在StackOverflow上发现的那样,使用上面的最后两行代码,它说
错误类型错误:无法读取未定义的属性(阅读“quillEditor”)
我在我的typescript文件中这样定义quillEditor:
@ViewChild('editor') editor!: QuillEditorComponent
看起来上面的代码没有引用到我最初的羽毛笔编辑器。
理想的行为是光标将停留在输入的文本和自动完成的文本之间。类似于Gmail的例子。
任何想法都非常赞赏。
1条答案
按热度按时间sy5wg1nm1#
在我发现要获取对编辑器的引用,应该在OnEditorCreated属性中进行操作之后,我也遇到了同样的错误和困惑:
然后在你的组件中:
这是因为编辑器需要一段时间来初始化自己,所以有必要在onEditorCreated侦听器中获取引用。