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

AAE-26043 Use text alignment set in rich text editor #10228

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ describe('DisplayRichTextWidgetComponent', () => {
type: 'paragraph',
data: {
text: 'Display some <font color="#ff1300">formatted</font> <mark class="cdx-marker">text</mark>'
},
tunes: {
anyTuneName: {
alignment: 'left'
}
}
}
],
Expand Down Expand Up @@ -90,7 +95,8 @@ describe('DisplayRichTextWidgetComponent', () => {
});

it('should parse editorjs data to html', async () => {
const expectedHtml = '<h1>Editor.js</h1><p>Display some <font color="#ff1300">formatted</font> <mark class="cdx-marker">text</mark></p>';
const expectedHtml =
'<h1>Editor.js</h1><p class="ce-tune-alignment--left">Display some <font color="#ff1300">formatted</font> <mark class="cdx-marker">text</mark></p>';

fixture.detectChanges();
await fixture.whenStable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,32 @@ import { DomSanitizer } from '@angular/platform-browser';
export class DisplayRichTextWidgetComponent extends WidgetComponent implements OnInit {
parsedHTML: any;

private static readonly CUSTOM_PARSER = {
header: ({ data, tunes }): string => {
const paragraphAlign = data.alignment || data.align || tunes?.anyTuneName?.alignment;
if (typeof paragraphAlign !== 'undefined' && ['left', 'right', 'center'].includes(paragraphAlign)) {
return `<h${data.level} class="ce-tune-alignment--${paragraphAlign}">${data.text}</h${data.level}>`;
} else {
return `<h${data.level}>${data.text}</h${data.level}>`;
}
},
paragraph: ({ data, tunes }): string => {
const paragraphAlign = data.alignment || data.align || tunes?.anyTuneName?.alignment;

if (typeof paragraphAlign !== 'undefined' && ['left', 'right', 'center', 'justify'].includes(paragraphAlign)) {
return `<p class="ce-tune-alignment--${paragraphAlign}">${data.text}</p>`;
} else {
return `<p>${data.text}</p>`;
}
}
};

constructor(public formService: FormService, private readonly sanitizer: DomSanitizer) {
super(formService);
}

ngOnInit(): void {
this.parsedHTML = edjsHTML().parseStrict(this.field.value);
this.parsedHTML = edjsHTML(DisplayRichTextWidgetComponent.CUSTOM_PARSER).parseStrict(this.field.value);

if (!(this.parsedHTML instanceof Error)) {
this.sanitizeHtmlContent();
Expand Down
Loading