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

Adding inline styles and attributes to widget with applyStyle #1681

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a53dc5a
Feature: Adding inline styles and attributes to widget
engineering-this Feb 15, 2018
f24c6ba
Added manual and unit tests
engineering-this Feb 15, 2018
37bb75b
Improved checkStyleActive, updated docs
engineering-this Feb 20, 2018
2080fe5
Added new asserts and refactored tests
engineering-this Feb 20, 2018
45514a5
Managing widget style on upcast and downcast
engineering-this Feb 27, 2018
0c417c3
Reworked widget styles added test cases changed docs.
engineering-this Feb 28, 2018
9e8ca33
Changed data attribute name to data-cke-
engineering-this Mar 16, 2018
281bcbe
Fixed test after change in data name
engineering-this Mar 16, 2018
c1879f0
Minor refactoring, improving docs
engineering-this Mar 19, 2018
861b4e5
Refactored test
engineering-this Mar 19, 2018
3c6b5fd
Removing redundant line
engineering-this Mar 19, 2018
ddde7d3
Reverted some codestyle changes
engineering-this Mar 19, 2018
95538b9
Fixed adding/removing style not working with undo plugin
engineering-this Mar 19, 2018
46939e0
Typo in test step description
engineering-this Mar 19, 2018
529635b
Added manual test for undo integration with apply/remove widget style
engineering-this Mar 19, 2018
635453b
Fixed failing test
engineering-this Mar 19, 2018
a219d81
Moved lockedStyle from element to data
engineering-this Mar 21, 2018
480d9d6
Refactored test, added test case for undo integration
engineering-this Mar 21, 2018
c7d4dda
Fixed adding class name to widget.styleDefinition.attributes
engineering-this Mar 23, 2018
edaa942
Fixed typo
engineering-this Mar 23, 2018
c8e18bc
Added changelog entry
engineering-this Apr 10, 2018
e36432e
Refactoring code and improving docs
engineering-this Apr 13, 2018
70b8eac
Refactoring
engineering-this Apr 16, 2018
d37905e
Fixed test after changes
engineering-this Apr 16, 2018
df5389f
Fixed widget styles integration with mathjax, copypaste and image2
engineering-this Apr 20, 2018
0dce367
Added test case for widget styles on easyimage
engineering-this Apr 20, 2018
83651a9
Added style test with image2 plugin
engineering-this Apr 25, 2018
d049a31
Removed undostyle test, added undo plugin to all applystyle tests
engineering-this Apr 25, 2018
b7f3b1c
Fixed error when adding styles after undo
engineering-this Apr 25, 2018
ca4b2c4
Simplified applystyle tests descriptions
engineering-this Apr 25, 2018
ebd1370
Added docs entry to test helper
engineering-this Apr 25, 2018
3cb054e
Added missing undo/redo buttons
engineering-this Apr 25, 2018
276567a
Fixed styledef classes desynchronized from data classes in easyimage
engineering-this Apr 25, 2018
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
New Features:

* [#1761](https://github.com/ckeditor/ckeditor-dev/issues/1761): [Autolink](https://ckeditor.com/cke4/addon/autolink) plugin supports email links.
* [#1674](https://github.com/ckeditor/ckeditor-dev/issues/1674): The [Widget](https://ckeditor.com/cke4/addon/widget) plugin now fully supports [`CKEDITOR.style`](https://docs.ckeditor.com/ckeditor4/latest/api/CKEDITOR_style.html) `attributes` and `styles` via `applyStyle` or `removeStyle` and `checkStyleActive` methods.

Fixed Issues:

Expand Down
62 changes: 49 additions & 13 deletions plugins/image2/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,11 @@
// Note: Center alignment is detected during upcast, so only left/right cases
// are checked below.
if ( !data.align ) {
var alignElement = data.hasCaption ? this.element : image;

var alignElement = data.hasCaption ? this.element : image,
align;
if ( this.styleDefinition && this.styleDefinition.styles ) {
align = this.styleDefinition.styles[ 'float' ];
}
// Read the initial left/right alignment from the class set on element.
if ( alignClasses ) {
if ( alignElement.hasClass( alignClasses[ 0 ] ) ) {
Expand All @@ -400,6 +403,8 @@
} else {
data.align = 'none';
}
} else if ( align ) {
data.align = align;
}
// Read initial float style from figure/image and then remove it.
else {
Expand Down Expand Up @@ -897,7 +902,8 @@
return function( el, data ) {
var dimensions = { width: 1, height: 1 },
name = el.name,
image;
image,
styleDefinition;

// https://dev.ckeditor.com/ticket/11110 Don't initialize on pasted fake objects.
if ( el.attributes[ 'data-cke-realelement' ] )
Expand Down Expand Up @@ -932,6 +938,14 @@

// Image can be wrapped in link <a><img/></a>.
image = el.getFirst( 'img' ) || el.getFirst( 'a' ).getFirst( 'img' );

// Add this style to lockedStyle so it is preserved on this element.
styleDefinition = el.attributes[ 'data-cke-style-definition' ] || {};
styleDefinition.lockedStyle = styleDefinition.lockedStyle || {};
styleDefinition.lockedStyle.display = 'inline-block';
styleDefinition.lockedStyle[ 'text-align' ] = 'center';

el.attributes[ 'data-cke-style-definition' ] = JSON.stringify( styleDefinition );
}

// No center wrapper has been found.
Expand Down Expand Up @@ -972,7 +986,8 @@
// inline styles or classes (image2_alignClasses).
var attrsHolder = el.name == 'a' ? el.getFirst() : el,
attrs = attrsHolder.attributes,
align = this.data.align;
align = this.data.align,
elementStyle = CKEDITOR.tools.parseCssText( el.attributes.style );

// De-wrap the image from resize handle wrapper.
// Only block widgets have one.
Expand All @@ -999,21 +1014,33 @@
// </div>
//
if ( align == 'center' && el.name == 'figure' ) {
if ( !alignClasses ) {
delete elementStyle[ 'text-align' ];
elementStyle.display = 'inline-block';
el.attributes.style = CKEDITOR.tools.writeCssText( elementStyle ) || undefined;
}
el = el.wrapWith( new CKEDITOR.htmlParser.element( 'div',
alignClasses ? { 'class': alignClasses[ 1 ] } : { style: 'text-align:center' } ) );
}

// If left/right, add float style to the downcasted element.
else if ( align in { left: 1, right: 1 } ) {
if ( alignClasses )
if ( alignClasses ) {
attrsHolder.addClass( alignClasses[ alignmentsObj[ align ] ] );
else
}
else {
styles[ 'float' ] = align;
}
// Don't duplicate float on linked image.
if ( attrsHolder !== el ) {
delete elementStyle[ 'float' ];
el.attributes.style = CKEDITOR.tools.writeCssText( elementStyle ) || undefined;
}
}

// Update element styles.
if ( !alignClasses && !CKEDITOR.tools.isEmpty( styles ) )
attrs.style = CKEDITOR.tools.writeCssText( styles );
// Update element styles.
if ( !alignClasses && !CKEDITOR.tools.isEmpty( styles ) )
attrs.style = CKEDITOR.tools.writeCssText( styles );
}
}

return el;
Expand Down Expand Up @@ -1340,9 +1367,18 @@
}
} );

// Change the position of the widget resizer when data changes.
widget.on( 'data', function() {
resizer[ widget.data.align == 'right' ? 'addClass' : 'removeClass' ]( 'cke_image_resizer_left' );
widget.on( 'data', function( evt ) {
var alignElement = evt.data.hasCaption ? this.element : this.parts.image,
align = evt.data.align;
// Change the position of the widget resizer when data changes.
resizer[ align == 'right' ? 'addClass' : 'removeClass' ]( 'cke_image_resizer_left' );

if ( ( align == 'right' || align == 'left' ) && alignElement.getStyle( 'float' ) === align ) {
this.styleDefinition.styles = this.styleDefinition.styles || {};
this.styleDefinition.styles[ 'float' ] = align;
} else if ( align && this.styleDefinition.styles && this.styleDefinition.styles[ 'float' ] ) {
delete this.styleDefinition.styles[ 'float' ];
}
} );
}

Expand Down
24 changes: 18 additions & 6 deletions plugins/mathjax/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

( function() {
CKEDITOR.plugins.add( 'mathjax', {
/* jscs:disable maximumLineLength*/
lang: 'af,ar,az,bg,ca,cs,cy,da,de,de-ch,el,en,en-au,en-gb,eo,es,es-mx,eu,fa,fi,fr,gl,he,hr,hu,id,it,ja,km,ko,ku,lt,nb,nl,no,oc,pl,pt,pt-br,ro,ru,sk,sl,sq,sv,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
/* jscs:enable maximumLineLength*/
requires: 'widget,dialog',
icons: 'mathjax',
hidpi: true, // %REMOVE_LINE_CORE%
Expand Down Expand Up @@ -95,12 +97,20 @@
data.math = CKEDITOR.tools.htmlDecode( el.children[ 0 ].value );

// Add style display:inline-block to have proper height of widget wrapper and mask.
var attrs = el.attributes;
var attrs = el.attributes,
styleDefinition;

if ( attrs.style )
if ( attrs.style ) {
attrs.style += ';display:inline-block';
else
} else {
attrs.style = 'display:inline-block';
}

// Add this style to locked style so it is preserved on that element.
styleDefinition = attrs[ 'data-cke-styleDefinition' ] || {};
styleDefinition.lockedStyle = styleDefinition.lockedStyle || {};
styleDefinition.lockedStyle.display = 'inline-block';
el.attributes[ 'data-cke-style-definition' ] = JSON.stringify( styleDefinition );

// Add attribute to prevent deleting empty span in data processing.
attrs[ 'data-cke-survive' ] = 1;
Expand All @@ -115,10 +125,12 @@

// Remove style display:inline-block.
var attrs = el.attributes;
attrs.style = attrs.style.replace( /display:\s?inline-block;?\s?/, '' );
if ( attrs.style === '' )
if ( attrs.style ) {
attrs.style = attrs.style.replace( /display:\s?inline-block;?\s?/, '' );
}
if ( attrs.style === '' ) {
delete attrs.style;

}
return el;
}
} );
Expand Down
Loading