Skip to content

Commit

Permalink
IBX-9326: The image link with a class is producing an error (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabowskiM authored Feb 5, 2025
1 parent 0642bbe commit d45c50e
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Widget from '@ckeditor/ckeditor5-widget/src/widget';
import IbexaEmbedImageCommand from './embed-image-command';

import { findContent } from '../../services/content-service';
import { addPredefinedClassToConfig } from '../../custom-attributes/helpers/config-helper';
import { getCustomClassesConfig, addPredefinedClassToConfig } from '../../custom-attributes/helpers/config-helper';

const CONTAINER_CLASS = 'ibexa-embed-type-image';

Expand Down Expand Up @@ -75,11 +75,17 @@ class IbexaEmbedImageEditing extends Plugin {

defineSchema() {
const { schema } = this.editor.model;
const customClassesConfig = getCustomClassesConfig();
const allowedAttributes = ['contentId', 'size', 'ibexaLinkHref', 'ibexaLinkTitle', 'ibexaLinkTarget'];

if (customClassesConfig.link) {
allowedAttributes.push('ibexaLinkClasses');
}

schema.register('embedImage', {
isObject: true,
allowWhere: '$block',
allowAttributes: ['contentId', 'size', 'ibexaLinkHref', 'ibexaLinkTitle', 'ibexaLinkTarget'],
allowAttributes: allowedAttributes,
});
}

Expand Down Expand Up @@ -158,17 +164,28 @@ class IbexaEmbedImageEditing extends Plugin {
downcastWriter.insert(downcastWriter.createPositionAt(container, 0), config);

if (linkHref) {
const link = downcastWriter.createUIElement('a', {
const linkClasses = modelElement.getAttribute('ibexaLinkClasses');
const linkAttributes = {
'data-ezelement': 'ezlink',
href: linkHref,
title: modelElement.getAttribute('ibexaLinkTitle'),
target: modelElement.getAttribute('ibexaLinkTarget'),
});
};

if (linkClasses) {
linkAttributes.class = linkClasses;
}

const link = downcastWriter.createUIElement('a', linkAttributes);

consumable.consume(modelElement, 'attribute:ibexaLinkHref');
consumable.consume(modelElement, 'attribute:ibexaLinkTitle');
consumable.consume(modelElement, 'attribute:ibexaLinkTarget');

if (linkClasses) {
consumable.consume(modelElement, 'attribute:ibexaLinkClasses');
}

downcastWriter.insert(downcastWriter.createPositionAt(container, 'end'), link);
}

Expand All @@ -195,6 +212,7 @@ class IbexaEmbedImageEditing extends Plugin {
upcastWriter.setAttribute('ibexaLinkHref', link.getAttribute('href'), modelElement);
upcastWriter.setAttribute('ibexaLinkTitle', link.getAttribute('title') ?? '', modelElement);
upcastWriter.setAttribute('ibexaLinkTarget', link.getAttribute('target') ?? '', modelElement);
upcastWriter.setAttribute('ibexaLinkClasses', link.getAttribute('class') ?? '', modelElement);
}

return modelElement;
Expand Down

0 comments on commit d45c50e

Please sign in to comment.