-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Is it possible to set style attribute on td
and `table tag ?
#3235
Comments
Hi! It simply depends on where you apply that attribute. If you apply it to a text and write a converter for a text attribute, then it's displayed on the text. If you feature will apply it to a Note: Adding support for classes that are set on |
OK, this is an awful day. The corrected code: editor.model.schema.extend( 'table', {
allowAttributes: 'style'
} );
editor.conversion.for( 'upcast' ).add( upcastAttributeToAttribute( {
view: {
name: 'table',
key: 'style'
},
model: 'style'
} ) );
editor.conversion.for( 'downcast' ).add( downcastAttributeToAttribute( {
model: {
name: 'table',
key: 'style'
},
view: 'style'
} ) );
window.setTableStyle = function( style ) {
const table = editor.model.document.selection.getFirstPosition().getAncestors().find( item => item.name == 'table' );
editor.model.change( writer => {
writer.setAttribute( 'style', style, table );
} );
}; But what the hell is this: There are two issues:
|
BTW, I also found the API confusing. I realised that the mistake I did is something that may happen to anyone simply because I was just copying pieces of code from API docs. This matter is being discussed in https://github.com/ckeditor/ckeditor5-engine/issues/1556#issuecomment-424105185 |
@Reinmar Thanks for explanation. I have a question, from where should I call |
From wherever you want :) That's just an example code which applies an attribute on a model's table element. Depending on your use case you need to turn it into some command. You said that you want it to work when a table is selected, so I'd recommend using |
Thanks. I got it working. But having same issue as you had
Is there any workaround for it or it is going to be fixed in ckeditor? |
PS. that's not a bug. The model's |
@gauravjain028: Sorry for longer response - we have a release in progress so we have less time to answer questions. So the working code for styling editor.model.schema.extend( 'table', {
allowAttributes: 'style'
} );
editor.conversion.for( 'upcast' ).add( upcastAttributeToAttribute( {
view: {
name: 'table',
key: 'style',
value: {
'background-color': /[\s\S]+/
}
},
model: {
key: 'style',
value: viewElement => viewElement.getStyle( 'background-color' )
}
} ) );
editor.conversion.for( 'downcast' ).add( dispatcher => {
dispatcher.on( 'attribute:style:table', ( evt, data, conversionApi ) => {
const table = data.item;
// The table from the model is mapped to the widget element: <figure>.
const viewFigure = conversionApi.mapper.toViewElement( table );
// A <table> is direct child of a <figure> but there might be other view (including UI) elments inside <figure>.
const viewTable = [ ...viewFigure.getChildren() ].find( element => element.name === 'table' );
// it should be consumed...
// User view writer to change style of a view table.
if ( data.attributeNewValue ) {
conversionApi.writer.setStyle( 'background-color', data.attributeNewValue, viewTable );
} else {
conversionApi.writer.removeStyle( 'background-color', viewTable );
}
} );
} );
window.setTableStyle = function( style ) {
const table = editor.model.document.selection.getFirstPosition().getAncestors().find( item => item.name == 'table' );
editor.model.change( writer => {
writer.setAttribute( 'style', style, table );
} );
}; There are few things to note:
cc @Reinmar - look on those remarks as we might think how to improve that for widgets. (see linked issue from Image feature). |
I reported a ticket for the bug when you try to use Regarding allowing |
I have created color plugins which works fine on
$text
and$block
. It can select any color from color picker and set that color asstyle="color:#AAAAAA;"
.Now I want to do like, if I select the whole table by it's handler, and can select the color. After that it can update the border color as well. Currently it updated the color of text written inside.
It is possible ?
The text was updated successfully, but these errors were encountered: