Javascript 在索引处的TinyMCE中插入带有书签的文本时丢失选择

Javascript 在索引处的TinyMCE中插入带有书签的文本时丢失选择,javascript,typescript,tinymce,selection,bookmarks,Javascript,Typescript,Tinymce,Selection,Bookmarks,我正在用Typescript编写代码,并试图在游标索引处将文本插入TinyMCE。我有一个下拉列表,其中包含要插入的不同字符串,我想在单击时插入它们 当我两次调用此函数时,index属性的值为-1,因此我的所有文本都被重新设置,书签似乎无法正确恢复选择: addCustomTag(tag: string) { let bm = this._editor.selection.getBookmark(0); let selector = "[data-mce-type=bookm

我正在用Typescript编写代码,并试图在游标索引处将文本插入TinyMCE。我有一个下拉列表,其中包含要插入的不同字符串,我想在单击时插入它们

当我两次调用此函数时,index属性的值为-1,因此我的所有文本都被重新设置,书签似乎无法正确恢复选择:

  addCustomTag(tag: string) {
    let bm = this._editor.selection.getBookmark(0);
    let selector = "[data-mce-type=bookmark]";
    let bmElements = this._editor.dom.select(selector);
    this._editor.selection.select(bmElements[0]);
    this._editor.selection.collapse();
    let elementID = "######cursor######";
    let cursorPosition = '<span id="' + elementID + '"></span>';
    this._editor.selection.setContent(cursorPosition);
    let content = this._editor.getContent({ format: "html" });
    let index = content.indexOf(cursorPosition);
    this._editor.dom.remove(elementID, false);
    this._editor.selection.moveToBookmark(bm);

    let tagToInsert = '((' + tag + '))';
    let bookmark = this._editor.selection.getBookmark(0);
    cursorPosition = '<span id="' + bookmark.id + '_start" data-mce-type="bookmark" data-mce-style="overflow:hidden;line-height:0px"></span>';
    content = this._editor.getContent({ format: "html" });
    let part1 = content.substr(0, index);
    let part2 = content.substr(index);
    let contentWithString = part1 + tagToInsert + cursorPosition + part2;
    this._editor.setContent(contentWithString, ({ format: "raw" }));
    this._editor.selection.moveToBookmark(bookmark);
  }
你能帮我吗

提前感谢

编辑:


我找到了一个简单的解决方案来避免所有问题:

 addCustomTag(tag: string) {
    this._editor.execCommand('mceInsertContent', false, '((' + tag + '))');
 }
 addCustomTag(tag: string) {
    this._editor.execCommand('mceInsertContent', false, '((' + tag + '))');
 }

我找到了一个简单的解决方案来避免所有问题:

 addCustomTag(tag: string) {
    this._editor.execCommand('mceInsertContent', false, '((' + tag + '))');
 }