Skip to content

Commit e7f8e69

Browse files
matthewcaremikecp
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
1 parent 69f8e54 commit e7f8e69

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
@@ -1272,11 +1272,22 @@ function tinyMceService($rootScope, $q, imageHelper, $locale, $http, $timeout, s
12721272
function insertLink() {
12731273
if (anchorElm) {
12741274
editor.dom.setAttribs(anchorElm, createElemAttributes());
1275-
12761275
editor.selection.select(anchorElm);
12771276
editor.execCommand('mceEndTyping');
12781277
} else {
1279-
editor.execCommand('mceInsertLink', false, createElemAttributes());
1278+
var selectedContent = editor.selection.getContent();
1279+
// If there is no selected content, we can't insert a link
1280+
// as TinyMCE needs selected content for this, so instead we
1281+
// create a new dom element and insert it, using the chosen
1282+
// link name as the content.
1283+
if (selectedContent !== "") {
1284+
editor.execCommand('mceInsertLink', false, createElemAttributes());
1285+
} else {
1286+
// Using the target url as a fallback, as href might be confusing with a local link
1287+
var linkContent = typeof target.name !== "undefined" && target.name !== "" ? target.name : target.url
1288+
var domElement = editor.dom.createHTML("a", createElemAttributes(), linkContent);
1289+
editor.execCommand('mceInsertContent', false, domElement);
1290+
}
12801291
}
12811292
}
12821293

0 commit comments

Comments
 (0)