Skip to content

Commit e9bbcaf

Browse files
authored
IBX-8845: Added alignments for custom tags (#187)
1 parent 2ef456b commit e9bbcaf

File tree

10 files changed

+127
-38
lines changed

10 files changed

+127
-38
lines changed

src/bundle/Resources/public/js/CKEditor/block-alignment/block-alignment-editing.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class IbexaCustomAttributesEditing extends Plugin {
2525
const { model } = this.editor;
2626

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

2930
this.defineConverters();
3031

src/bundle/Resources/public/js/CKEditor/core/base-ckeditor.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ const VIEWPORT_TOP_OFFSET_DISTRACTION_FREE_MODE = 0;
205205
'ibexaRemoveElement',
206206
],
207207
},
208+
customTag: {
209+
toolbar: [
210+
'ibexaBlockLeftAlignment',
211+
'ibexaBlockCenterAlignment',
212+
'ibexaBlockRightAlignment',
213+
'|',
214+
'ibexaCustomTagSettings',
215+
'ibexaRemoveElement',
216+
],
217+
},
208218
heading: {
209219
options: [
210220
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },

src/bundle/Resources/public/js/CKEditor/custom-tags/block-custom-tag/custom-tag-editing.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,11 @@ class IbexaCustomTagEditing extends Plugin {
4141
const header = downcastWriter.createUIElement('div', { class: 'ibexa-custom-tag__header' }, function (domDocument) {
4242
const domElement = this.toDomElement(domDocument);
4343
const customTagConfig = window.ibexa.richText.customTags[customTagName];
44-
const attributesButton = `<button type="button" class="ibexa-btn ibexa-btn--ghost ibexa-btn--small ibexa-btn--no-text ibexa-btn--show-custom-tag-attributes">
45-
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--secondary">
46-
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('settings-block')}"></use>
47-
</svg>
48-
</button>`;
4944

5045
domElement.innerHTML = `
5146
<div class="ibexa-custom-tag__header-title" data-cke-tooltip-text="${customTagConfig.label}">
5247
${customTagConfig.label}
5348
</div>
54-
<div class="ibexa-custom-tag__header-actions">
55-
${Object.keys(customTagConfig.attributes).length ? attributesButton : ''}
56-
<button type="button" class="ibexa-btn ibexa-btn--ghost ibexa-btn--small ibexa-btn--no-text ibexa-btn--remove-custom-tag">
57-
<svg class="ibexa-icon ibexa-icon--small ibexa-icon--secondary">
58-
<use xlink:href="${window.ibexa.helpers.icon.getIconPath('trash')}"></use>
59-
</svg>
60-
</button>
61-
</div>
6249
`;
6350

6451
return domElement;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Command from '@ckeditor/ckeditor5-core/src/command';
2+
3+
const { ibexa } = window;
4+
5+
class IbexaCustomTagSettingsCommand extends Command {
6+
refresh() {
7+
const modelElement = this.editor.model.document.selection.getSelectedElement();
8+
const isCustomTag = modelElement?.name === 'customTag';
9+
const isEnabled =
10+
isCustomTag && !!Object.keys(ibexa.richText.customTags[modelElement.getAttribute('customTagName')].attributes).length;
11+
12+
this.isEnabled = isEnabled;
13+
}
14+
}
15+
16+
export default IbexaCustomTagSettingsCommand;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
2+
import IbexaButtonView from '../../common/button-view/button-view';
3+
4+
import IbexaCustomTagSettingsCommand from './custom-tag-settings-command';
5+
6+
const { ibexa, Translator } = window;
7+
8+
class IbexaCustomTagSettingsUI extends Plugin {
9+
constructor(props) {
10+
super(props);
11+
12+
this.showSettings = this.showSettings.bind(this);
13+
}
14+
15+
showSettings() {
16+
this.editor.editing.view.document.fire('ibexa-show-custom-tag-settings');
17+
}
18+
19+
init() {
20+
const ibexaCustomTagSettingsCommand = new IbexaCustomTagSettingsCommand(this.editor);
21+
22+
this.editor.ui.componentFactory.add('ibexaCustomTagSettings', (locale) => {
23+
const buttonView = new IbexaButtonView(locale);
24+
25+
buttonView.set({
26+
label: Translator.trans(/*@Desc("Settings")*/ 'custom_tag.settings.label', {}, 'ck_editor'),
27+
icon: ibexa.helpers.icon.getIconPath('settings-block'),
28+
tooltip: true,
29+
});
30+
31+
this.listenTo(buttonView, 'execute', this.showSettings);
32+
33+
buttonView.bind('isEnabled').to(ibexaCustomTagSettingsCommand);
34+
35+
return buttonView;
36+
});
37+
38+
this.editor.commands.add('ibexaCustomTagSettings', ibexaCustomTagSettingsCommand);
39+
}
40+
}
41+
42+
export default IbexaCustomTagSettingsUI;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
2+
import WidgetToolbarRepository from '@ckeditor/ckeditor5-widget/src/widgettoolbarrepository';
3+
4+
const { Translator } = window;
5+
6+
class IbexaCustomTagsToolbar extends Plugin {
7+
static get requires() {
8+
return [WidgetToolbarRepository];
9+
}
10+
11+
getSelectedCustomTagWidget(selection) {
12+
const viewElement = selection.getSelectedElement();
13+
const isCustomTag = viewElement?.hasClass('ibexa-custom-tag') && viewElement?.getAttribute('data-ezelement') === 'eztemplate';
14+
15+
return isCustomTag ? viewElement : null;
16+
}
17+
18+
afterInit() {
19+
const { editor } = this;
20+
const widgetToolbarRepository = editor.plugins.get(WidgetToolbarRepository);
21+
22+
widgetToolbarRepository.register('customTags', {
23+
ariaLabel: Translator.trans(/*@Desc("Custom tag toolbar")*/ 'custom_tag.toolbar.label', {}, 'ck_editor'),
24+
items: editor.config.get('customTag.toolbar') || [],
25+
getRelatedElement: this.getSelectedCustomTagWidget,
26+
});
27+
}
28+
}
29+
30+
export default IbexaCustomTagsToolbar;

src/bundle/Resources/public/js/CKEditor/custom-tags/block-custom-tag/custom-tag-ui.js

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,10 @@ class IbexaCustomTagUI extends Plugin {
2929
});
3030
}
3131

32-
isCustomTagSelected(eventData) {
32+
isCustomTagSelected() {
3333
const modelElement = this.editor.model.document.selection.getSelectedElement();
3434

35-
return (
36-
!!eventData.domTarget.closest(`[data-ezname="${this.componentName}"]`) &&
37-
modelElement?.name === 'customTag' &&
38-
modelElement?.getAttribute('customTagName') === this.componentName
39-
);
40-
}
41-
42-
isRemoveButtonClicked(eventData) {
43-
return !!eventData.domTarget.closest('.ibexa-btn--remove-custom-tag');
44-
}
45-
46-
isShowAttributesButtonClicked(eventData) {
47-
return !!eventData.domTarget.closest('.ibexa-btn--show-custom-tag-attributes');
35+
return modelElement?.name === 'customTag' && modelElement?.getAttribute('customTagName') === this.componentName;
4836
}
4937

5038
hasAttributes() {
@@ -54,15 +42,9 @@ class IbexaCustomTagUI extends Plugin {
5442
enableUserBalloonInteractions() {
5543
const viewDocument = this.editor.editing.view.document;
5644

57-
this.listenTo(viewDocument, 'click', (eventInfo, eventData) => {
58-
if (this.isCustomTagSelected(eventData)) {
59-
if (this.isRemoveButtonClicked(eventData)) {
60-
this.removeCustomTag();
61-
}
62-
63-
if (this.isShowAttributesButtonClicked(eventData)) {
64-
this.showAttributes(eventData.domTarget);
65-
}
45+
this.listenTo(viewDocument, 'ibexa-show-custom-tag-settings', () => {
46+
if (this.isCustomTagSelected()) {
47+
this.showAttributes(viewDocument.selection.getSelectedElement());
6648
}
6749
});
6850

@@ -159,7 +141,7 @@ class IbexaCustomTagUI extends Plugin {
159141
position: { target },
160142
});
161143

162-
this.balloon.updatePosition({ target });
144+
this.balloon.updatePosition(this.getBalloonPositionData());
163145
}
164146

165147
hideAttributes() {

src/bundle/Resources/public/js/CKEditor/custom-tags/custom-tags.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import IbexaCustomTagsUI from './block-custom-tag/custom-tag-ui';
44
import IbexaInlineCustomTagsUI from './inline-custom-tag/inline-custom-tag-ui';
55
import IbexaCustomTagsEditing from './block-custom-tag/custom-tag-editing';
66
import IbexaInlineCustomTagsEditing from './inline-custom-tag/inline-custom-tag-editing';
7+
import IbexaCustomTagsToolbar from './block-custom-tag/custom-tag-toolbar';
8+
import IbexaCustomTagSettingsUI from './block-custom-tag/custom-tag-settings-ui';
79

810
class IbexaCustomTags extends Plugin {
911
static get requires() {
@@ -56,7 +58,14 @@ class IbexaCustomTags extends Plugin {
5658
};
5759
});
5860

59-
return [...blockCustomTagsUI, ...inlineCustomTagsUI, IbexaCustomTagsEditing, IbexaInlineCustomTagsEditing];
61+
return [
62+
...blockCustomTagsUI,
63+
...inlineCustomTagsUI,
64+
IbexaCustomTagsEditing,
65+
IbexaInlineCustomTagsEditing,
66+
IbexaCustomTagsToolbar,
67+
IbexaCustomTagSettingsUI,
68+
];
6069
}
6170
}
6271

src/bundle/Resources/public/scss/_custom-tag.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
position: relative;
33
padding: calculateRem(32px) calculateRem(10px) calculateRem(10px);
44
border: calculateRem(1px) solid $ibexa-color-dark-200;
5+
width: 100%;
56

67
&__header {
78
background-color: $ibexa-color-light-100;
@@ -17,6 +18,7 @@
1718
align-items: center;
1819
border-top-left-radius: $ibexa-border-radius;
1920
border-top-right-radius: $ibexa-border-radius;
21+
min-height: calculateRem(32px);
2022
}
2123

2224
&__header-title {

src/bundle/Resources/translations/ck_editor.en.xliff

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
<target state="new">Custom styles</target>
4747
<note>key: custom_styles_btn.label</note>
4848
</trans-unit>
49+
<trans-unit id="1027bcb4083e4adb65f39bfa202e9a6968327ef2" resname="custom_tag.settings.label">
50+
<source>Settings</source>
51+
<target state="new">Settings</target>
52+
<note>key: custom_tag.settings.label</note>
53+
</trans-unit>
54+
<trans-unit id="af69b1457df848a63cb4c9cb2acf4583f110fb84" resname="custom_tag.toolbar.label">
55+
<source>Custom tag toolbar</source>
56+
<target state="new">Custom tag toolbar</target>
57+
<note>key: custom_tag.toolbar.label</note>
58+
</trans-unit>
4959
<trans-unit id="3b257be2c6dd1f7bf0d5c70e64c22bf5b298dbbf" resname="elements_path.list.label">
5060
<source>list</source>
5161
<target state="new">list</target>

0 commit comments

Comments
 (0)