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

Styles system for Easy Image #1518

Merged
merged 54 commits into from
Feb 5, 2018
Merged

Styles system for Easy Image #1518

merged 54 commits into from
Feb 5, 2018

Conversation

Comandeer
Copy link
Member

What is the purpose of this pull request?

New feature

Does your PR contain necessary tests?

All patches which change the editor code must include tests. You can always read more
on PR testing,
how to set the testing environment and
how to create tests
in the official CKEditor documentation.

This PR contains

  • Unit tests
  • Manual tests

What changes did you make?

Rewrite of whole commands system for EI, enabling to add custom styles. Additionaly existing commands (easyimageFull and easyimageSide) were moved into the new style system.

Copy link
Contributor

@mlewand mlewand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked docs, and test briefly, there are a few things that needs to be addressed before I proceed with review further.

File names for icons got long and tricky, there's easyimagealigncenter.png, easyimagealignleft.png and easyimagealignright.png. Since we already have easyimageside.png, how about making it a easyimagecenter.png (skip "align" part)? Simpler and more consistent.

@bender-ckeditor-plugins: sourcearea, wysiwygarea, floatingspace, toolbar, easyimage

1. Click on image widget.
2. Click on one of button in balloon toolbar. **Note:** first style is a totally custom one, therefore it does not have any icon.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(…) one of buttons in the toolbar. (…)

<p>Armstrong spent about <s>three and a half</s> two and a half hours outside the spacecraft, Aldrin slightly less; and together they collected 47.5 pounds (21.5&nbsp;kg) of lunar material for return to Earth. A third member of the mission, <a href="http://en.wikipedia.org/wiki/Michael_Collins_(astronaut)">Michael Collins</a>, piloted the <a href="http://en.wikipedia.org/wiki/Apollo_Command/Service_Module">command</a> spacecraft alone in lunar orbit until Armstrong and Aldrin returned to it for the trip back to Earth.</p>
</div>

<h2>Inline editor</h2>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no source button in the inline editor, so let's skip it and leave just divarea.

@@ -0,0 +1,53 @@
<h2>Classic editor</h2>
<div id="classic">
<p><strong>Apollo 11</strong> was the spaceflight that landed the first humans, Americans <a href="http://en.wikipedia.org/wiki/Neil_Armstrong">Neil Armstrong</a> and <a href="http://en.wikipedia.org/wiki/Buzz_Aldrin">Buzz Aldrin</a>, on the Moon on July 20, 1969, at 20:18 UTC. Armstrong became the first to step onto the lunar surface 6 hours later on July 21 at 02:56 UTC.</p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need all the wrapping text? It's a lot of it. Try to reduce it.

@@ -0,0 +1,43 @@
<h2>Classic editor</h2>
<div id="classic">
<p><strong>Apollo 11</strong> was the spaceflight that landed the first humans, Americans <a href="http://en.wikipedia.org/wiki/Neil_Armstrong">Neil Armstrong</a> and <a href="http://en.wikipedia.org/wiki/Buzz_Aldrin">Buzz Aldrin</a>, on the Moon on July 20, 1969, at 20:18 UTC. Armstrong became the first to step onto the lunar surface 6 hours later on July 21 at 02:56 UTC.</p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need all the wrapping text? It's a lot of it. Try to reduce it.

@@ -0,0 +1,13 @@
@bender-tags: 4.9.0, feature, 932
@bender-ui: collapsed
@bender-ckeditor-plugins: sourcearea, wysiwygarea, floatingspace, toolbar, easyimage
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for sourcearea plugin here.

@@ -43,6 +46,15 @@ The outline is not a part of the element's dimensions, we have to use a border a
max-width: 25%;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that style applied to alignCenter large images will be reduced to 25% of width.

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I find this entire block now very, very little readable. I think it would be better idea to split it into 3 groups (left, right, center). Even if it would cost us repeating max-width / float I feel that would be more readable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've limit center styles to 90% (just to differentiate them from full styles).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea 👍

* style would produce `EasyimageLeft` button and `easyimageLeft` command.
*
* @since 4.9.0
* @cfg {Object<string,Object>} easyimage_styles
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use uppercased String in docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proper format would be Object.<foo, bar> (note the dot and space.

* * `icon` - path to the icon used in the balloon toolbar,
* * `iconHiDpi` - path to the high DPI version of the icon.
*
* config.easyimage_styles = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code listing is not displayed properly:

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reported as ckeditor/jsduck#8. I've mitigated it by moving note "Every style added […]" directly under the list.

attributes: {
'class': 'test'
},
icon: 'test'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some title to it. Now it is undefined, which naturally lights a red bulb in tester mind:

image

* items to the context menu for Easy Image widget.
*
* @since 4.9.0
* @cfg {Array/String} [easyimage_toolbar=[ 'EasyimageFull', 'EasyimageSide', 'EasyimageAlt' ]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's expected to contain an array of strings, so accurate type hint is String[]/String.

@mlewand
Copy link
Contributor

mlewand commented Jan 31, 2018

Also I have rebased this branch onto latest Easy Image feature branch in t/932-3393_rebased. Feel free to reuse it for your work, so that you don't have to resolve the conflicts by yourself.

@Comandeer
Copy link
Member Author

File names for icons got long and tricky, there's easyimagealigncenter.png, easyimagealignleft.png and easyimagealignright.png. Since we already have easyimageside.png, how about making it a easyimagecenter.png (skip "align" part)? Simpler and more consistent.

Aren't icons names tied with button names? In that case it won't work unless integration with custom button icons is merged.

@Comandeer
Copy link
Member Author

I've also rebased branch to newest t/932-2961.

@Comandeer Comandeer requested a review from mlewand February 1, 2018 11:13
Copy link
Contributor

@mlewand mlewand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally the solution looks to be good, but requires some polishing/simplifications before going live.

Please add a rogue case, where one adds a style like:

{
	element: 'div',
	attributes: {
		'class': 'test'
	},
	label: 'Hacky style'
}

There should be assertion that element tag name has not changed. That will keep us safe form future regression assuming that widget<->style integration would have implemented handling for such styles.

* @cfg {String} [easyimage_alignLeftClass='easyimage-alignLeft']
* @member CKEDITOR.config
*/
CKEDITOR.config.easyimage_alignLeftClass = 'easyimage-alignLeft';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not much benefit from exposing easyimage_*Class members. Let's avoid redundancy, the proper way to configure that should be config.easyimage_styles option. We should reflect it in example listing of config.easyimage_styles.

Say you want to customize the class name for left align, then you should use:

config.easyimage_styles = {
	alignLeft: {
		attributes: {
			'class': 'my-custom-left-align'
		}
	},
};

Note how I skipped label, button etc. It should be merged, thus the default should apply.

My brief testing shows that it already works that way, so that's cool.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly what you have covered in "test override default styles" test, and that's the way how it should be done.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also dropped easyimage_full and easyimage_side classes, the only one left is the main class for the widget.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! That's exactly what I've been looking for.

@@ -43,6 +46,15 @@ The outline is not a part of the element's dimensions, we have to use a border a
max-width: 25%;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea 👍


editor._.easyImageToolbarContext = editor.balloonToolbars.create( {
buttons: 'EasyimageFull,EasyimageSide,EasyimageAlt',
buttons: buttons.join ? buttons.join( ',' ) : buttons,
widgets: [ WIDGET_NAME ]
} );
}

function addMenuItems( editor ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually could we rename this function to something that mentions context menu? Because this is solely context menu integration thing.

@@ -10,12 +10,45 @@
WIDGET_NAME = 'easyimage';

function addCommands( editor ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function now has too much logic. Instead it would be a good idea for us to split it into two functions with a clear task/responsibility - addCommands and addButtons.

I guess that would require you to make defaultStyles and editor.config.easyimage_styles union in parent closure, and pass the merged object as a parameter to both of them.

iconHiDpi: style.iconHiDpi
};

delete style.label;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed? Will CKEDITOR.style break if it gets definition with stray properties?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not this whole function is redundant.

@@ -30,105 +63,170 @@
};
}

function createCommand( exec, refreshCheck, forceSelectionCheck ) {
function createButton( button ) {
editor.ui.addButton( button.name, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

editor.ui.addButton could be unavailable if something changes in the dependency tree. Just to be safe I'd prefer adding that to requires list for this plugin.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

button is already an explicit requirement for easyimage.

max-width: 90%;
}

.cke_widget_wrapper_easyimage-alignRight, :not(.cke_widget_wrapper_easyimage):not(.cke_widget_wrapper_easyimage-alignRight) > .easyimage-alignRight {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this particular selector could be joined to the .cke_widget_wrapper_easyimage-side, :not(.cke_widget_wrapper_easyimage):not(.cke_widget_wrapper_easyimage-side) > .easyimage-side selector above.

* },
* label: 'Align left',
* icon: '/foo/bar/icons/baz.png',
* iconHidpi: '/foo/bar/icons/hidpi/baz.png'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's iconHiDpi (note letter casing).

/**
* List of buttons to be displayed in a balloon toolbar for Easy Image widget.
* If Context Menu plugin is enabled, this config variable will be used also to add
* items to the context menu for Easy Image widget.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a code listing where it uses left / center / right align buttons.

} );
styleDefinition.type = 'widget';
styleDefinition.widget = 'easyimage';
styleDefinition.group = 'easyimage';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we actually allowed to specify group? That would allow people to do these crazy border kind of things, without affecting the alignment.

So my thinking here is to make the group default to 'easyimage' if not defined, but preserve styleDefinition.group if present.

That would be nice option for extensibility.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With our current way of handling styles (buttons that allow only applying styles) such change will result in non-reversible styles. I'd leave it the way it is for now.

@mlewand
Copy link
Contributor

mlewand commented Feb 2, 2018

Aren't icons names tied with button names? In that case it won't work unless integration with custom button icons is merged.

Make sense, let's leave it as it is now.

@Comandeer
Copy link
Member Author

Comandeer commented Feb 2, 2018

I've also rebased branch onto the latest t/932-2961.

@mlewand mlewand self-requested a review February 5, 2018 09:37
Copy link
Contributor

@mlewand mlewand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work with the tests.

I was missing a possibility to customize group for added styles. Played a bit and it works fine, which is a nice addition.

Also I was thinking if we could avoid data duplication (I'm thinking about _styles member and style name being kept in widget.data). I gave it a try and it was possible to reuse CKEDITOR.styleCommand which results with a single source of truth - reducing a risk of future mistakes by desyncing the two.

These were the biggest issues, other than that there were few minor things:

  • Tests not being ignored on unsupported environments.
  • No information in config docs about styles available by defalt.

Other than that code organization seemed to be fine.

I proposed changes in #1583.

@mlewand mlewand self-requested a review February 5, 2018 13:26
Comandeer and others added 25 commits February 5, 2018 14:26
That will help us smell some integration problems on other browsers if something goes wrong.
The only source of information about applied style is the widget element itself. Also widget is no longer defaulted to a full style. Thus few TCs are failing in tests/plugins/easyimage/uploadintegrations suite.
It is now configurable with the confg.easyimage_defaultStyle option.
…o that only one listener is added per editor for a better performance.
…. Minor simplification for refresh commands.
Copy link
Contributor

@mlewand mlewand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With recent changes all looks good.

@mlewand mlewand merged commit 686d0eb into t/932-2961 Feb 5, 2018
mlewand added a commit that referenced this pull request Feb 5, 2018
Styles system for Easy Image
Comandeer pushed a commit that referenced this pull request Feb 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants