Link insertion with no selected text

TinyMCE requires text to be selected in order to insert links.
Added checks for if there is a text selection, and insert HTML content if there is not.
Link contents consists of the target name, or the target url if not populated

(cherry picked from commit e7f8e692c2)
This commit is contained in:
Matthew Care
2021-10-06 22:15:28 +01:00
committed by Sebastiaan Janssen
parent f39909dd90
commit 4a4b45fe27

View File

@@ -1273,11 +1273,22 @@ function tinyMceService($rootScope, $q, imageHelper, $locale, $http, $timeout, s
function insertLink() {
if (anchorElm) {
editor.dom.setAttribs(anchorElm, createElemAttributes());
editor.selection.select(anchorElm);
editor.execCommand('mceEndTyping');
} else {
editor.execCommand('mceInsertLink', false, createElemAttributes());
var selectedContent = editor.selection.getContent();
// If there is no selected content, we can't insert a link
// as TinyMCE needs selected content for this, so instead we
// create a new dom element and insert it, using the chosen
// link name as the content.
if (selectedContent !== "") {
editor.execCommand('mceInsertLink', false, createElemAttributes());
} else {
// Using the target url as a fallback, as href might be confusing with a local link
var linkContent = typeof target.name !== "undefined" && target.name !== "" ? target.name : target.url
var domElement = editor.dom.createHTML("a", createElemAttributes(), linkContent);
editor.execCommand('mceInsertContent', false, domElement);
}
}
}