Skip to content

Commit a802de9

Browse files
committed
fix: link command handling
1 parent 9283ffd commit a802de9

File tree

2 files changed

+32
-238
lines changed

2 files changed

+32
-238
lines changed

private/js/ckeditor5_plugins/ckeditor5.cmslink/src/cms.linkfield.js

Lines changed: 0 additions & 200 deletions
This file was deleted.

private/js/ckeditor5_plugins/ckeditor5.cmslink/src/cmsLink.js

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ export default class CmsLink extends Plugin {
6161
// DOWNCAST: From model to view (HTML)
6262
editor.conversion.for('dataDowncast').attributeToElement({
6363
model: 'linkHref',
64-
view: this.createLinkElement,
64+
view: this.createLinkElement.bind(this),
6565
converterPriority: 'high',
6666
});
6767
editor.conversion.for('editingDowncast')
6868
.attributeToElement({
6969
model: 'linkHref',
70-
view: (href, conversionApi) => {
71-
if (href) {
72-
href.href = ensureSafeUrl(href.href, allowedProtocols);
70+
view: (value, conversionApi) => {
71+
if (value) {
72+
value.href = ensureSafeUrl(value.href, allowedProtocols);
7373
}
74-
return this.createLinkElement(href, conversionApi);
74+
return this.createLinkElement(value, conversionApi);
7575
},
7676
converterPriority: 'high',
7777
});
@@ -131,7 +131,7 @@ export default class CmsLink extends Plugin {
131131
const linkFormView = editor.plugins.get('LinkUI').formView;
132132
const linkToolbarView = editor.plugins.get('LinkUI').toolbarView;
133133

134-
let autoComplete = null;
134+
this.autoComplete = null;
135135

136136
editor.plugins
137137
.get('ContextualBalloon')
@@ -167,30 +167,31 @@ export default class CmsLink extends Plugin {
167167
}
168168
if (newValue === linkFormView) {
169169
// Patch the link form view to add the autocomplete functionality
170-
if (autoComplete !== null) {
170+
if (this.autoComplete !== null) {
171171
// AutoComplete already added, just reset it, if no link exists
172-
autoComplete.selectElement.value = linkHref.cmsHref || '';
173-
autoComplete.urlElement.value = linkHref.href || '';
174-
autoComplete.populateField();
175-
autoComplete.inputElement.focus();
172+
this.autoComplete.selectElement.value = linkHref.cmsHref || '';
173+
this.autoComplete.urlElement.value = linkHref.href || '';
174+
this.autoComplete.populateField();
175+
this.autoComplete.inputElement.focus();
176176
return;
177177
}
178178
const hiddenInput = document.createElement('input');
179179

180180
hiddenInput.setAttribute('type', 'hidden');
181181
hiddenInput.setAttribute('name', linkFormView.urlInputView.fieldView.element.id + '_select');
182182
hiddenInput.value = linkHref.cmsHref || '';
183-
linkFormView.urlInputView.fieldView.element.name = linkFormView.urlInputView.fieldView.element.id;;
183+
linkFormView.urlInputView.fieldView.element.name = linkFormView.urlInputView.fieldView.element.id;
184+
linkFormView.urlInputView.fieldView.element.value = linkHref.href || '';
184185
linkFormView.urlInputView.fieldView.element.parentNode.insertBefore(
185186
hiddenInput,
186187
linkFormView.urlInputView.fieldView.element
187188
);
188189
// Label is misleading - remove it
189190
linkFormView.urlInputView.fieldView.element.parentNode.querySelector(`label[for="${linkFormView.urlInputView.fieldView.element.id}"]`)?.remove();
190-
autoComplete = new this.LinkField(linkFormView.urlInputView.fieldView.element, {
191+
this.autoComplete = new this.LinkField(linkFormView.urlInputView.fieldView.element, {
191192
url: editor.config.get('url_endpoint') || ''
192193
});
193-
autoComplete.inputElement.focus();
194+
this.autoComplete.inputElement.focus();
194195
}
195196
});
196197
}
@@ -219,34 +220,27 @@ export default class CmsLink extends Plugin {
219220
*/
220221
const {editor} = this;
221222
const linkFormView = editor.plugins.get('LinkUI').formView;
222-
const linkCommand = editor.commands.get('link');
223223

224224
this.listenTo(
225225
linkFormView,
226226
'submit',
227-
(ev) => {
228-
const id = linkFormView.urlInputView.fieldView.element.id + '_select';
229-
const selectElement = linkFormView.urlInputView.fieldView.element.closest('form').querySelector(`input[name="${id}"]`);
230-
// Stop the execution of the link command caused by closing the form.
231-
// Inject the extra attribute value. The highest priority listener here
232-
// injects the argument (here below 👇).
233-
// - The high priority listener in
234-
// _addExtraAttributeOnLinkCommandExecute() gets that argument and sets
235-
// the extra attribute.
236-
// - The normal (default) priority listener in ckeditor5-link sets
237-
// (creates) the actual link.
238-
linkCommand.once(
239-
'execute',
240-
(evt, args) => {
241-
if (args.length > 0) {
242-
args[0] = {href: args[0]};
243-
if (selectElement.value) {
244-
args[0].cmsHref = selectElement.value;
245-
}
246-
}
247-
},
248-
{priority: 'highest'},
249-
);
227+
(event) => {
228+
if (linkFormView.isValid()) {
229+
const attrs = {};
230+
const id = linkFormView.urlInputView.fieldView.element.id + '_select';
231+
const selectElement = linkFormView.urlInputView.fieldView.element.closest('form').querySelector(`input[name="${id}"]`);
232+
if (selectElement?.value) {
233+
attrs.href = linkFormView.urlInputView.fieldView.element.value;
234+
attrs.cmsHref = selectElement.value;
235+
} else {
236+
const url = this.autoComplete?.inputElement?.value || linkFormView.urlInputView.fieldView.element.value;
237+
attrs.href = addLinkProtocolIfApplicable(url, defaultProtocol);
238+
}
239+
const displayedText = formView.displayedTextInputView.fieldView.element.value;
240+
editor.execute('link', attrs, this._getDecoratorSwitchesState(), displayedText !== this.selectedLinkableText ? displayedText : undefined);
241+
this._closeFormView();
242+
}
243+
event.stop();
250244
},
251245
{priority: 'high'},
252246
);

0 commit comments

Comments
 (0)