Skip to content

Commit

Permalink
Fix adding links to images in ckeditor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas De Keukelaere committed Nov 3, 2021
1 parent f0ec074 commit a6366d9
Showing 1 changed file with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,29 @@ CKEDITOR.dialog.add(
type: 'text',
id: 'displayText',
label: editor.lang.medialibrary.displayText,
validate: CKEDITOR.dialog.validate.notEmpty(editor.lang.medialibrary.displayCannotBeEmpty),
setup: function (element) {
this.setValue(element.getText());
},
onShow: function (element) {
// hide display text when linking a html element
if (element.sender.element.htmlElement) {
this.getElement().getParent().getParent().hide();

return;
}

this.getElement().getParent().getParent().show();
},
commit: function (element) {
element.setText(this.getValue());
// don't set display text when linking a html element
if (element.htmlElement) {
return;
}

// fill in display text
if (this.getValue()) {
element.setText(this.getValue());
}
}
},
{
Expand All @@ -131,6 +148,11 @@ CKEDITOR.dialog.add(
return;
}

// set url as text when no display text is given
if (!element.htmlElement && !element.getText()) {
element.setText(this.getValue());
}

element.setAttribute('href', this.getValue());
element.setAttribute('data-cke-saved-href', this.getValue());
element.removeAttribute('target');
Expand Down Expand Up @@ -244,6 +266,7 @@ CKEDITOR.dialog.add(

var selection = editor.getSelection();
var initialText = selection.getSelectedText();
var initialElement = selection.getSelectedElement();
var element = selection.getStartElement();

if (element) {
Expand All @@ -257,6 +280,14 @@ CKEDITOR.dialog.add(
dialog.setValueOf('tab', initialText.match(urlRegex) ? 'url' : 'displayText', initialText);
}

// save initial element to new a-element
if (initialElement) {
// element used for checks
element.htmlElement = initialElement.$.outerHTML;
// display element
element.setHtml(initialElement.$.outerHTML);
}

this.element = element;

if (!this.insertMode) {
Expand Down

0 comments on commit a6366d9

Please sign in to comment.