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

Heading style does not autoformat #449

Open
qweyrt opened this issue Jan 26, 2024 · 4 comments
Open

Heading style does not autoformat #449

qweyrt opened this issue Jan 26, 2024 · 4 comments
Labels
pending:feedback This issue is blocked by necessary feedback.

Comments

@qweyrt
Copy link

qweyrt commented Jan 26, 2024

I'm using a custom build of ckeditor 5 and whenever I try to change the heading style the text doesn't change even though the console still show the data text with heading tag:

image

There's no errors shown.

Here's my ckeditor element:

        <CKEditor
            editor={Editor}
            config={editorConfiguration}
            data={null}
            onChange={(event, editor) => {
                const data = editor.getData();
                console.log({ event, editor, data });
            }}
            onReady={(editor) =>{
                editor.conversion.for( 'upcast' ).elementToAttribute( {
                    view: {
                        name: 'a',
                        key: 'data-mention',
                        classes: 'mention',
                        attributes: {
                            href: true,
                            'data-user-id': true
                        }
                    },
                    model: {
                        key: 'mention',
                        value: viewItem => {
                            const mentionAttribute = editor.plugins.get( 'Mention' ).toMentionAttribute( viewItem, {
                                link: viewItem.getAttribute( 'href' ),
                                userId: viewItem.getAttribute( 'data-user-id' )
                            } );
            
                            return mentionAttribute;
                        }
                    },
                    converterPriority: 'high'
                } );                
                editor.conversion.for( 'downcast' ).attributeToElement( {
                    model: 'mention',
                    view: ( modelAttributeValue, { writer } ) => {
                        if ( !modelAttributeValue ) {
                            return;
                        }
                        return writer.createAttributeElement( 'span', {
                        }, {
                            priority: 20,
                        } );
                    },
                    converterPriority: 'high'
                } );
            }}
        />

Here's my custom ckeditor file (Which I use to build my custom editor):

class Editor extends ClassicEditor {
	public static override builtinPlugins = [
		Alignment,
		Autoformat,
		BlockQuote,
		Bold,
		CKBox,
		CloudServices,
		Code,
		CodeBlock,
		DataFilter,
		DataSchema,
		Essentials,
		FindAndReplace,
		FontFamily,
		FontSize,
		GeneralHtmlSupport,
		Heading,
		HorizontalLine,
		Image,
		ImageCaption,
		ImageStyle,
		ImageToolbar,
		ImageUpload,
		Indent,
		Italic,
		Link,
		List,
		MediaEmbed,
		Mention,
		PageBreak,
		Paragraph,
		PasteFromOffice,
		PictureEditing,
		SelectAll,
		Table,
		TableToolbar,
		TextTransformation,
		Underline,
		Undo
	];

	public static override defaultConfig: EditorConfig = {
		toolbar: {
			items: [
				'heading',
				'|',
				'bold',
				'italic',
				'underline',
				'link',
				'bulletedList',
				'numberedList',
				'fontFamily',
				'fontSize',
				'|',
				'outdent',
				'indent',
				'alignment',
				'-',
				'undo',
				'redo',
				'imageUpload',
				'insertTable',
				'blockQuote',
				'mediaEmbed',
				'code',
				'codeBlock',
				'findAndReplace',
				'pageBreak',
				'horizontalLine',
				'selectAll'
			],
			shouldNotGroupWhenFull: true
		},
		language: 'en',
		image: {
			toolbar: [
				'imageTextAlternative',
				'toggleImageCaption',
				'imageStyle:inline',
				'imageStyle:block',
				'imageStyle:side'
			]
		},
		table: {
			contentToolbar: [
				'tableColumn',
				'tableRow',
				'mergeTableCells'
			]
		},
		heading: {
			options: [
				{ class: 'ck-heading_paragraph',model: 'paragraph', title: 'Paragraph' },
				{ class: 'ck-heading_heading1',model: 'heading1', view: 'h1', title: 'Heading 1' },
				{ class: 'ck-heading_heading2',model: 'heading2', view: 'h2', title: 'Heading 2' },
				{ class: 'ck-heading_heading3',model: 'heading3', view: 'h3', title: 'Heading 3' }
	
			]
		},	
		mention: {
            feeds: [
                {
                    marker: '',
					feed: ['[[Barney]]', '[[Lily]]', '[[Marry Ann]]', '[[Marshall]]', '[[Robin]]', '[[Ted]]'],
                    minimumCharacters: 1
                }
            ]
        }
	};
}

So what should I do?

@Witoso
Copy link
Member

Witoso commented Jan 29, 2024

Hi!

I try to change the heading style (...)

Could you explain a bit more? I'm not sure if I fully understand. If you want to apply some specific classes to headings, the configuration would look more or less like this:

heading: {
            options: [
                { model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
                { model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
                { model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' },
                {
                    model: 'headingFancy',
                    view: {
                        name: 'h2',
                        classes: 'fancy' // Class in the content.
                    },
                    title: 'Heading 2 (fancy)',
                    class: 'ck-heading_heading2_fancy', // <- Dropdown's class.

                    // It needs to be converted before the standard 'heading2'.
                    converterPriority: 'high'
                }
            ]
        }

The top-level class is applied to the button in the headings' dropdown, the class in the content.

@Witoso
Copy link
Member

Witoso commented Jan 29, 2024

If the default content styles don't work, make sure there are no clashes with some other stylesheets.

@Witoso Witoso added the pending:feedback This issue is blocked by necessary feedback. label Jan 29, 2024
@qweyrt
Copy link
Author

qweyrt commented Jan 30, 2024

Could you explain a bit more?

Usually when you pick a heading style, the text will automatically format to the style you pick, like this:

image

In my case, whenever I pick any type of heading the text doesn't change at all and it still stuck in the paragraph style

@Witoso
Copy link
Member

Witoso commented Jan 31, 2024

It looks like something may be overriding the styles of the editor with a higher precedence. Please investigate your stylsheets. This is not reproducible in the demos we have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending:feedback This issue is blocked by necessary feedback.
Projects
None yet
Development

No branches or pull requests

2 participants