From e7f8e692c2397f268e864524b892b65acff3d29a Mon Sep 17 00:00:00 2001 From: Matthew Care Date: Wed, 6 Oct 2021 22:15:28 +0100 Subject: [PATCH] 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 --- .../src/common/services/tinymce.service.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/services/tinymce.service.js b/src/Umbraco.Web.UI.Client/src/common/services/tinymce.service.js index 4c3901e63c48..0c62ca058803 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/tinymce.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/tinymce.service.js @@ -1272,11 +1272,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); + } } }