Skip to content

Commit

Permalink
IBX-8845: Added alignments for custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dew326 committed Sep 27, 2024
1 parent c237fd7 commit dd47ed6
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class IbexaCustomAttributesEditing extends Plugin {
const { model } = this.editor;

model.schema.extend('embedImage', { allowAttributes: 'data-ezalign' });
model.schema.extend('customTag', { allowAttributes: 'data-ezalign' });

this.defineConverters();

Expand Down
10 changes: 10 additions & 0 deletions src/bundle/Resources/public/js/CKEditor/core/base-ckeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ const VIEWPORT_TOP_OFFSET_DISTRACTION_FREE_MODE = 0;
'ibexaRemoveElement',
],
},
customTag: {
toolbar: [
'ibexaBlockLeftAlignment',
'ibexaBlockCenterAlignment',
'ibexaBlockRightAlignment',
'|',
'ibexaCustomTagSettings',
'ibexaRemoveElement',
],
},
heading: {
options: [
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,11 @@ class IbexaCustomTagEditing extends Plugin {
const header = downcastWriter.createUIElement('div', { class: 'ibexa-custom-tag__header' }, function (domDocument) {
const domElement = this.toDomElement(domDocument);
const customTagConfig = window.ibexa.richText.customTags[customTagName];
const attributesButton = `<button type="button" class="ibexa-btn ibexa-btn--ghost ibexa-btn--small ibexa-btn--no-text ibexa-btn--show-custom-tag-attributes">
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--secondary">
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('settings-block')}"></use>
</svg>
</button>`;

domElement.innerHTML = `
<div class="ibexa-custom-tag__header-title" data-cke-tooltip-text="${customTagConfig.label}">
${customTagConfig.label}
</div>
<div class="ibexa-custom-tag__header-actions">
${Object.keys(customTagConfig.attributes).length ? attributesButton : ''}
<button type="button" class="ibexa-btn ibexa-btn--ghost ibexa-btn--small ibexa-btn--no-text ibexa-btn--remove-custom-tag">
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--secondary">
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('trash')}"></use>
</svg>
</button>
</div>
`;

return domElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Command from '@ckeditor/ckeditor5-core/src/command';

const { ibexa } = window;

class IbexaCustomTagSettingsCommand extends Command {
refresh() {
const modelElement = this.editor.model.document.selection.getSelectedElement();
const isCustomTag = modelElement?.name === 'customTag';
const isEnabled =
isCustomTag && !!Object.keys(ibexa.richText.customTags[modelElement.getAttribute('customTagName')].attributes).length;

this.isEnabled = isEnabled;
}
}

export default IbexaCustomTagSettingsCommand;
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import IbexaButtonView from '../../common/button-view/button-view';

import IbexaCustomTagSettingsCommand from './custom-tag-settings-command';

const { ibexa, Translator } = window;

class IbexaCustomTagSettingsUI extends Plugin {
constructor(props) {
super(props);

this.showSettings = this.showSettings.bind(this);
}

showSettings() {
this.editor.editing.view.document.fire('ibexa-show-custom-tag-settings');
}

init() {
const ibexaCustomTagSettingsCommand = new IbexaCustomTagSettingsCommand(this.editor);

this.editor.ui.componentFactory.add('ibexaCustomTagSettings', (locale) => {
const buttonView = new IbexaButtonView(locale);

buttonView.set({
label: Translator.trans(/*@Desc("Settings")*/ 'custom_tag.settings.label', {}, 'ck_editor'),
icon: ibexa.helpers.icon.getIconPath('settings-block'),
tooltip: true,
});

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

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

return buttonView;
});

this.editor.commands.add('ibexaCustomTagSettings', ibexaCustomTagSettingsCommand);
}
}

export default IbexaCustomTagSettingsUI;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import WidgetToolbarRepository from '@ckeditor/ckeditor5-widget/src/widgettoolbarrepository';

const { Translator } = window;

class IbexaCustomTagsToolbar extends Plugin {
static get requires() {
return [WidgetToolbarRepository];
}

getSelectedCustomTagWidget(selection) {
const viewElement = selection.getSelectedElement();
const isCustomTag = viewElement?.hasClass('ibexa-custom-tag') && viewElement?.getAttribute('data-ezelement') === 'eztemplate';

return isCustomTag ? viewElement : null;
}

afterInit() {
const { editor } = this;
const widgetToolbarRepository = editor.plugins.get(WidgetToolbarRepository);

widgetToolbarRepository.register('customTags', {
ariaLabel: Translator.trans(/*@Desc("Custom tag toolbar")*/ 'custom_tag.toolbar.label', {}, 'ck_editor'),
items: editor.config.get('customTag.toolbar') || [],
getRelatedElement: this.getSelectedCustomTagWidget,
});
}
}

export default IbexaCustomTagsToolbar;
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,10 @@ class IbexaCustomTagUI extends Plugin {
this.isNew = false;
}

isCustomTagSelected(eventData) {
isCustomTagSelected() {
const modelElement = this.editor.model.document.selection.getSelectedElement();

return (
!!eventData.domTarget.closest(`[data-ezname="${this.componentName}"]`) &&
modelElement?.name === 'customTag' &&
modelElement?.getAttribute('customTagName') === this.componentName
);
}

isRemoveButtonClicked(eventData) {
return !!eventData.domTarget.closest('.ibexa-btn--remove-custom-tag');
}

isShowAttributesButtonClicked(eventData) {
return !!eventData.domTarget.closest('.ibexa-btn--show-custom-tag-attributes');
return modelElement?.name === 'customTag' && modelElement?.getAttribute('customTagName') === this.componentName;
}

hasAttributes() {
Expand All @@ -45,15 +33,9 @@ class IbexaCustomTagUI extends Plugin {
enableUserBalloonInteractions() {
const viewDocument = this.editor.editing.view.document;

this.listenTo(viewDocument, 'click', (eventInfo, eventData) => {
if (this.isCustomTagSelected(eventData)) {
if (this.isRemoveButtonClicked(eventData)) {
this.removeCustomTag();
}

if (this.isShowAttributesButtonClicked(eventData)) {
this.showAttributes(eventData.domTarget);
}
this.listenTo(viewDocument, 'ibexa-show-custom-tag-settings', () => {
if (this.isCustomTagSelected()) {
this.showAttributes(viewDocument.selection.getSelectedElement());
}
});

Expand Down Expand Up @@ -150,7 +132,7 @@ class IbexaCustomTagUI extends Plugin {
position: { target },
});

this.balloon.updatePosition({ target });
this.balloon.updatePosition(this.getBalloonPositionData());
}

hideAttributes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import IbexaCustomTagsUI from './block-custom-tag/custom-tag-ui';
import IbexaInlineCustomTagsUI from './inline-custom-tag/inline-custom-tag-ui';
import IbexaCustomTagsEditing from './block-custom-tag/custom-tag-editing';
import IbexaInlineCustomTagsEditing from './inline-custom-tag/inline-custom-tag-editing';
import IbexaCustomTagsToolbar from './block-custom-tag/custom-tag-toolbar';
import IbexaCustomTagSettingsUI from './block-custom-tag/custom-tag-settings-ui';

class IbexaCustomTags extends Plugin {
static get requires() {
Expand Down Expand Up @@ -56,7 +58,14 @@ class IbexaCustomTags extends Plugin {
};
});

return [...blockCustomTagsUI, ...inlineCustomTagsUI, IbexaCustomTagsEditing, IbexaInlineCustomTagsEditing];
return [
...blockCustomTagsUI,
...inlineCustomTagsUI,
IbexaCustomTagsEditing,
IbexaInlineCustomTagsEditing,
IbexaCustomTagsToolbar,
IbexaCustomTagSettingsUI,
];
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/bundle/Resources/public/scss/_custom-tag.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
position: relative;
padding: calculateRem(32px) calculateRem(10px) calculateRem(10px);
border: calculateRem(1px) solid $ibexa-color-dark-200;
width: 100%;

&__header {
background-color: $ibexa-color-light-100;
Expand All @@ -17,6 +18,7 @@
align-items: center;
border-top-left-radius: $ibexa-border-radius;
border-top-right-radius: $ibexa-border-radius;
min-height: calculateRem(32px);
}

&__header-title {
Expand Down

0 comments on commit dd47ed6

Please sign in to comment.