2
我在Typescript中編寫代碼,我試圖在光標索引處將文本插入到TinyMCE中。我有一個下拉列表,插入不同的字符串,我想在點擊時插入它們。在索引處插入文本到TinyMCE時丟失選擇書籤
當我兩次調用這個函數,指數屬性具有值-1,所以我的所有文字ereased,似乎書籤不恢復選項正確:
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);
}
我覺得這是一個問題這條線但我不知道如何解決它:
this._editor.selection.setContent(cursorPosition);
你能幫我嗎?
由於通過提前
編輯:
我發現了一個簡單的解決方案,以避免我的所有問題:
addCustomTag(tag: string) {
this._editor.execCommand('mceInsertContent', false, '((' + tag + '))');
}