ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Is there a conveniant way to add custom classes?

Open nuocu opened this issue 7 years ago • 8 comments

Is this a bug report or feature request? (choose one)

🆕 Feature request | Other

💻 Version of CKEditor

11.2.0.

📋

I'm integrating ck5 into a project with a grown and rather lengthy css-style. There are, for example, several classes, that can be applied to lists or tables. I could try and copy or extent the existing plugins and create new buttons for the altered versions. However this seems rather unnecessary complicated, and I'd pretty much prefer to give the classes to the relevant plugins while calling ClassicEditor.create(). 

This might as well be already possible, and if that's the case, I reckon it might be a good idea to add some examples for this in the documentation or place them there more prominently. The heading and the image plugin actually provide a way to handle that (via image: { styles: [ ... ] } and heading: {options: [ {model, view: {name, classes} ... ]). I would like to to add classes to lists (<li>) in the same way, but could not find out how in the documentation.

Perhaps those options provided in ClassicEditor.create() could be unified, so that all plugins share the same interface and one example in the documentation could suffice.

nuocu avatar Jan 15 '19 17:01 nuocu

You can add support for <figure> element classes in such a way:

class AllowClassesPlugin {
	constructor( editor ) {
		this.editor = editor;
	}

	init() {
		const editor = this.editor;

		editor.model.schema.extend( 'table', {
			allowAttributes: 'class'
		} );

		editor.conversion.attributeToAttribute( {
			model: {
				name: 'table',
				key: 'class',
				values: [ 'big', 'small' ]
			},
			view: {
				big: {
					name: 'figure',
					key: 'class',
					value: [ 'table', 'some-big-table' ]
				},

				small: {
					name: 'figure',
					key: 'class',
					value: [ 'table', 'some-big-small' ]
				}
			}
		} );
	}
}

This will handle e.g.:

<figure class="table some-big-table">
    <table>
        <tbody>
            <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
            <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
        </tbody>
    </table>
</figure>

You can read more about attributeToAttribute() conversion helper.

If you'd like to add support for <table class="*"> then it's more tricky, because the model <table> element maps to the <figure>, not the inner <table> (in the model, there's no <figure>). It'd require a custom converter.

Reinmar avatar Jan 22 '19 09:01 Reinmar

Support for classes on the <figure> wrapping the <table>is fine for me. Thanks a lot.

Would the same approach work if I need classes on lists (i.e.: <ul class=*">), too?

nuocu avatar Jan 25 '19 16:01 nuocu

Unfortunately, <ul>s are more difficult because of the approach we took when implementing them. In the model, lists are represented the same way paragraphs are. This means that there is no actual <ul> element in the model, it is a flat structure, with indentation levels represented through attributes.

This gave us more stable algorithms for features like enter, removing selections spanning over multiple items, merging blocks, etc. The drawback is that you don't have the parent element - it is created "on the fly" during conversion.

One reasonable way to overcome this would be to assign anything that you'd like on <ul> to the first item in the list. We will use this approach when we will start implementing features like lists starting number or lists styles. But this requires writing a custom converter, it is more difficult than the example above.

scofalik avatar Jan 26 '19 09:01 scofalik

The answers that we gave in this ticket cover what can be done easily with existing features. https://github.com/ckeditor/ckeditor5/issues/592 will cover creating a nicer and simpler API to extend existing features and add support for more features.

Reinmar avatar Mar 25 '19 14:03 Reinmar

Back to the response about table classes. Could you please tell if I can add these 2 new table types to the toolbar somehow?

romanovma avatar Jun 05 '19 17:06 romanovma

Is there a quick way to add class to a media embed figure tag ?

sandeepbalagopal09 avatar Dec 27 '19 09:12 sandeepbalagopal09

@Reinmar I thought your solution was what I was looking for, it seemed like it was pointing me in the right direction but I got an error:

index.js:467 CKEditorError: schema-cannot-extend-missing-item {"itemName":"table"}
Read more: https://ckeditor.com/docs/ckeditor5/latest/support/error-codes.html#error-schema-cannot-extend-missing-item
    at Schema.extend (schema.js:121:1)
    at AllowClassesPlugin.init (index.js:78:1)

I'm not even sure this is what I need... All I'm really after is changing the default class name that gets applied to tables which is <figure class="table"> and I'd rather give it a custom class name (like <figure class="ck-table">) as the rendered HTML on the front end causes style problems for my tables.

jsfgreen avatar Jul 12 '23 19:07 jsfgreen

@jsfgreen looks like the table feature is not available in your editor. Please create a separate issue for your problem, as this is closed and may have outdated solutions.

Witoso avatar Jul 13 '23 14:07 Witoso