From 536b8276e3476abc40aa5bfaaff27578348fc597 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Fri, 13 Sep 2024 17:19:54 +0200 Subject: [PATCH] XWIKI-22508: Creating a link in WYSIWYG by selecting text should follow the name strategy * Ensure to use the defined name strategy before providing a link default value when selecting a text (cherry picked from commit 7c1fa96c4550e58f1cdb0e4ae0957a6b73f19780) --- .../src/main/webjar/xwiki-link/plugin.js | 75 ++++++++++++++----- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/xwiki-platform-core/xwiki-platform-ckeditor/xwiki-platform-ckeditor-plugins/src/main/webjar/xwiki-link/plugin.js b/xwiki-platform-core/xwiki-platform-ckeditor/xwiki-platform-ckeditor-plugins/src/main/webjar/xwiki-link/plugin.js index 412bea40fee0..72221b52c1c3 100644 --- a/xwiki-platform-core/xwiki-platform-ckeditor/xwiki-platform-ckeditor-plugins/src/main/webjar/xwiki-link/plugin.js +++ b/xwiki-platform-core/xwiki-platform-ckeditor/xwiki-platform-ckeditor-plugins/src/main/webjar/xwiki-link/plugin.js @@ -406,9 +406,23 @@ }, setup: function(data) { // Create a link to a new page if the resource reference is not provided. - var resourceReference = data.resourceReference || this.getDefaultResourceReference(); - if (resourceReference.type === 'space' && this.resourceTypes.indexOf('space') < 0 && - this.resourceTypes.indexOf('doc') >= 0) { + var resourceReference = data.resourceReference; + $(this.getResourcePickerInput().$).prop('disabled', true); + if (resourceReference) { + this.setDefaultValue(this, resourceReference); + } else { + let self = this; + + this.getDefaultResourceReference() + .done(ref => self.setDefaultValue(self, ref)) + .fail(function () { + self.setDefaultValue(self, self.getDefaultResourceReferenceFallback()); + }); + } + }, + setDefaultValue: function(self, resourceReference) { + if (resourceReference.type === 'space' && self.resourceTypes.indexOf('space') < 0 && + self.resourceTypes.indexOf('doc') >= 0) { // Convert the space resource reference to a document resource reference. resourceReference = { type: 'doc', @@ -417,7 +431,47 @@ reference: resourceReference.reference + '.WebHome' }; } - this.setValue(resourceReference); + self.setValue(resourceReference); + $(self.getResourcePickerInput().$).prop('disabled', false); + }, + getDefaultResourceReference: function() { + let self = this; + // Compute the default reference by cleaning up the link label. + // Fall-back on the empty string if there's no text selection (e.g. if an image is selected). + var linkLabel = this.getDialog().getParentEditor().getSelection().getSelectedText() || ''; + // Normalize the white space. + var defaultReference = linkLabel.trim().replace(/\s+/g, ' '); + var deferred = $.Deferred(); + $.post(new XWiki.Document('LinkNameStrategyHelper', 'CKEditor').getURL('get'), { + outputSyntax: 'plain', + input: defaultReference, + base: XWiki.Model.serialize(this.base.getBase()) + }).done(function(data) { + deferred.resolve({ + reference: data[0].reference, + location: data[0].location, + type: self.resourceTypes[0], + // Make sure the picker doesn't try to resolve the link label as a resource reference. + isNew: true + }); + }).fail(function(data) { + console.error("Error while loading creation link response", data); + deferred.resolve(self.getDefaultResourceReferenceFallback()); + }); + return deferred.promise(); + }, + getDefaultResourceReferenceFallback: function() { + // Compute the default reference by cleaning up the link label. + // Fall-back on the empty string if there's no text selection (e.g. if an image is selected). + var linkLabel = this.getDialog().getParentEditor().getSelection().getSelectedText() || ''; + // Normalize the white space. + var defaultReference = linkLabel.trim().replace(/\s+/g, ' '); + return { + type: this.resourceTypes[0], + reference: defaultReference, + // Make sure the picker doesn't try to resolve the link label as a resource reference. + isNew: true + }; }, commit: function(data) { var resourceReference = this.getValue(); @@ -460,19 +514,6 @@ }); dialog.getContentElement('info', 'optionsToggle').sync(); dialog.layout(); - }, - getDefaultResourceReference: function() { - // Compute the default reference by cleaning up the link label. - // Fall-back on the empty string if there's no text selection (e.g. if an image is selected). - var linkLabel = this.getDialog().getParentEditor().getSelection().getSelectedText() || ''; - // Normalize the white space. - var defaultReference = linkLabel.trim().replace(/\s+/g, ' '); - return { - type: this.resourceTypes[0], - reference: defaultReference, - // Make sure the picker doesn't try to resolve the link label as a resource reference. - isNew: true - }; } }); };