typescript Quill文本编辑器Angular -插入文本内容而不移动光标错误

6za6bjd0  于 2023-04-07  发布在  TypeScript
关注(0)|答案(1)|浏览(167)

所以我在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的例子。
任何想法都非常赞赏。

sy5wg1nm

sy5wg1nm1#

在我发现要获取对编辑器的引用,应该在OnEditorCreated属性中进行操作之后,我也遇到了同样的错误和困惑:

<quill-editor
  #editor
  [formControl]="control"
  [placeholder]="''"
  [styles]="{ 'min-height': '120px' }"
  [modules]="modules"
  (onEditorCreated)="onEditorCreated($event)"
  (onEditorChanged)="onEditorChanged($event)"
>
</quill-editor>

然后在你的组件中:

onEditorCreated(quill: any): any {
  this.quillEditor = quill;
  this.quillEditor.insertText(this.quillEditor.getSelection()?.index, tag);
}

这是因为编辑器需要一段时间来初始化自己,所以有必要在onEditorCreated侦听器中获取引用。

相关问题