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

IBX-2349/5244/5245: Added possibility to add link into embed image #78

Merged
merged 3 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 21 additions & 4 deletions src/bundle/Resources/public/js/CKEditor/link/link-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,32 @@ import Command from '@ckeditor/ckeditor5-core/src/command';
class IbexaLinkCommand extends Command {
execute(linkData) {
this.editor.model.change((writer) => {
const modelElement = this.editor.model.document.selection.getSelectedElement();
const ranges = this.editor.model.schema.getValidRanges(this.editor.model.document.selection.getRanges(), 'ibexaLinkHref');

for (const range of ranges) {
writer.setAttribute('ibexaLinkHref', linkData.href, range);
writer.setAttribute('ibexaLinkTitle', linkData.title, range);
writer.setAttribute('ibexaLinkTarget', linkData.target, range);
if (modelElement) {
this.setAttributes(writer, linkData, modelElement);
} else {
for (const range of ranges) {
this.setAttributes(writer, linkData, range);
}
}
});
}

refresh() {
const modelElement = this.editor.model.document.selection.getSelectedElement();
const isValidElement = modelElement && this.editor.model.schema.checkAttribute(modelElement, 'ibexaLinkHref');
const validRanges = this.editor.model.schema.getValidRanges(this.editor.model.document.selection.getRanges(), 'ibexaLinkHref');

this.isEnabled = isValidElement || !validRanges.next().done;
}

setAttributes(writer, linkData, element) {
writer.setAttribute('ibexaLinkHref', linkData.href, element);
writer.setAttribute('ibexaLinkTitle', linkData.title, element);
writer.setAttribute('ibexaLinkTarget', linkData.target, element);
}
}

export default IbexaLinkCommand;
3 changes: 3 additions & 0 deletions src/bundle/Resources/public/js/CKEditor/link/link-editing.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class IbexaCustomTagEditing extends Plugin {
this.editor.model.schema.extend('$text', { allowAttributes: 'ibexaLinkHref' });
this.editor.model.schema.extend('$text', { allowAttributes: 'ibexaLinkTitle' });
this.editor.model.schema.extend('$text', { allowAttributes: 'ibexaLinkTarget' });
this.editor.model.schema.extend('embedImage', { allowAttributes: 'ibexaLinkHref' });
this.editor.model.schema.extend('embedImage', { allowAttributes: 'ibexaLinkTitle' });
this.editor.model.schema.extend('embedImage', { allowAttributes: 'ibexaLinkTarget' });

this.defineConverters();

Expand Down
31 changes: 23 additions & 8 deletions src/bundle/Resources/public/js/CKEditor/link/link-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class IbexaLinkUI extends Plugin {

this.isNew = false;

this.removeLink();
this.editor.execute('insertIbexaLink', { href: url, title: title, target: target });
this.hideForm();
});
Expand All @@ -42,21 +41,34 @@ class IbexaLinkUI extends Plugin {
return formView;
}

removeAttributes(writer, element) {
writer.removeAttribute('ibexaLinkHref', element);
writer.removeAttribute('ibexaLinkTitle', element);
writer.removeAttribute('ibexaLinkTarget', element);
}

removeLink() {
const modelElement = this.editor.model.document.selection.getSelectedElement();
const range = findAttributeRange(
this.editor.model.document.selection.getFirstPosition(),
'ibexaLinkHref',
this.editor.model.document.selection.getAttribute('ibexaLinkHref'),
this.editor.model,
);

this.editor.model.change((writer) => {
writer.removeAttribute('ibexaLinkHref', range);
writer.removeAttribute('ibexaLinkTitle', range);
writer.removeAttribute('ibexaLinkTarget', range);
if (modelElement) {
if (this.editor.model.schema.checkAttribute(modelElement, 'ibexaLinkHref')) {
this.editor.model.change((writer) => {
this.removeAttributes(writer, modelElement);
});
}
} else {
this.editor.model.change((writer) => {
this.removeAttributes(writer, range);

writer.setSelection(range);
});
writer.setSelection(range);
});
}
}

showForm() {
Expand All @@ -83,7 +95,7 @@ class IbexaLinkUI extends Plugin {
const ranges = this.editor.model.schema.getValidRanges(this.editor.model.document.selection.getRanges(), 'ibexaLinkHref');

for (const range of ranges) {
writer.removeAttribute('ibexaLinkHref', range);
this.removeAttributes(writer, range);
}
});
}
Expand Down Expand Up @@ -141,13 +153,16 @@ class IbexaLinkUI extends Plugin {
init() {
this.editor.ui.componentFactory.add('ibexaLink', (locale) => {
const buttonView = new IbexaButtonView(locale);
const insertIbexaLinkCommand = this.editor.commands.get('insertIbexaLink');

buttonView.set({
label: Translator.trans(/*@Desc("Link")*/ 'link_btn.label', {}, 'ck_editor'),
icon: window.ibexa.helpers.icon.getIconPath('link'),
tooltip: true,
});

buttonView.bind('isEnabled').to(insertIbexaLinkCommand);

this.listenTo(buttonView, 'execute', this.addLink);

return buttonView;
Expand Down