Skip to content

Commit 4a4b45f

Browse files
matthewcarenul800sebastiaan
authored andcommitted
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 e7f8e69)
1 parent f39909d commit 4a4b45f

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/Umbraco.Web.UI.Client/src/common/services/tinymce.service.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,11 +1273,22 @@ function tinyMceService($rootScope, $q, imageHelper, $locale, $http, $timeout, s
12731273
function insertLink() {
12741274
if (anchorElm) {
12751275
editor.dom.setAttribs(anchorElm, createElemAttributes());
1276-
12771276
editor.selection.select(anchorElm);
12781277
editor.execCommand('mceEndTyping');
12791278
} else {
1280-
editor.execCommand('mceInsertLink', false, createElemAttributes());
1279+
var selectedContent = editor.selection.getContent();
1280+
// If there is no selected content, we can't insert a link
1281+
// as TinyMCE needs selected content for this, so instead we
1282+
// create a new dom element and insert it, using the chosen
1283+
// link name as the content.
1284+
if (selectedContent !== "") {
1285+
editor.execCommand('mceInsertLink', false, createElemAttributes());
1286+
} else {
1287+
// Using the target url as a fallback, as href might be confusing with a local link
1288+
var linkContent = typeof target.name !== "undefined" && target.name !== "" ? target.name : target.url
1289+
var domElement = editor.dom.createHTML("a", createElemAttributes(), linkContent);
1290+
editor.execCommand('mceInsertContent', false, domElement);
1291+
}
12811292
}
12821293
}
12831294

0 commit comments

Comments
 (0)