Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport stable-16.8.x] XWIKI-22508: Creating a link in WYSIWYG by selecting text should follow the name strategy #3520

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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();
Expand Down Expand Up @@ -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
};
}
});
};
Expand Down
Loading