描述:

在我的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角插入文本吗?

推荐答案

我有一些东西是有效的,但它不是最好的解决方案之一.要在Summernote编辑器中以12角的当前光标位置插入文本,可以使用以下方法:

获取当前 Select 和范围:

const editableDiv = document.querySelector('.note-editable'); 
// please check as per the stackblitz demo I saw this as the classname for the editable content area
const sel = window.getSelection();
const range = sel.getRangeAt(0);
const startPos = range.startOffset;

在当前光标位置插入HTML:

range.deleteContents();
const newElem = document.createElement('span');
newElem.innerHTML = htmlToInsert;
range.insertNode(newElem);

将光标位置设置为插入的HTML的末尾:

range.setStartAfter(newElem);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
editableDiv.focus();

这段代码将光标位置设置为插入的HTML的末尾.

更新编辑内容,设置光标位置:

const updatedHtml = editableDiv.innerHTML;
const cursorPosition = startPos + htmlToInsert.length;
const newRange = document.createRange();
newRange.setStart(editableDiv.childNodes[0], cursorPosition);
newRange.setEnd(editableDiv.childNodes[0], cursorPosition);
sel.removeAllRanges();
sel.addRange(newRange);
editableDiv.focus();

此代码更新编辑器内容,将光标位置设置为插入文本的末尾,并将焦点放在该编辑器上.

请注意,这种方法使用Document对象来操作DOM,在Angular 应用程序中不推荐这样做.相反,您应该使用ANGLE的视图封装和指令来操作编辑器内容,但正如我所能做到的,当前指令中没有可用于在当前索引处插入内容的方法.

Javascript相关问答推荐

没有输出到带有chrome.Devtools扩展的控制台

如何解决CORS政策的问题

基于变量切换隐藏文本

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

点击按钮一次有文本出现和褪色,而不是点击两次?(html,CSS,JavaScript)

切换时排序对象数组,切换不起作用

在grafana情节,避免冲突的情节和传说

如果Arrow函数返回函数,而不是为useEffect返回NULL,则会出现错误

Angular 订阅部分相互依赖并返回数组多个异步Http调用

使用getBorbingClientRect()更改绝对元素位置

扩展类型的联合被解析为基类型

如何在 Select 文本时停止Click事件?

Reaction Redux&Quot;在派单错误中检测到状态Mutations

元素字符串长度html

无法避免UV:flat的插值:非法使用保留字"

警告框不显示包含HTML输入字段的总和

如何根据查询结果重新排列日期

是否有静态版本的`instanceof`?

AstroJS混合模式服务器终结点返回404

Refine.dev从不同的表取多条记录