Skip to content

Commit

Permalink
Link insertion with no selected text
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
matthewcare authored and nul800sebastiaan committed Nov 3, 2021
1 parent 8acf04c commit dd2b528
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Umbraco.Web.UI.Client/src/common/services/tinymce.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit dd2b528

Please sign in to comment.