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

No way to set default alignment for images or tables. #8502

Closed
burtonator opened this issue Nov 22, 2020 · 18 comments · Fixed by #9555
Closed

No way to set default alignment for images or tables. #8502

burtonator opened this issue Nov 22, 2020 · 18 comments · Fixed by #9555
Assignees
Labels
domain:ui/ux This issue reports a problem related to UI or UX. domain:v4-compatibility This issue reports a CKEditor 4 feature/option that's missing in CKEditor 5. package:image package:table support:2 An issue reported by a commercially licensed client. type:improvement This issue reports a possible enhancement of an existing feature.

Comments

@burtonator
Copy link

When you insert images / tables there's no way to pick the default alignment.

I looked at the alignment for images and there is a 'styles' which has an isDefault property but that's not actually working.

I also can't find any documentation on how to do this with tables either.

@burtonator burtonator added the type:bug This issue reports a buggy (incorrect) behavior. label Nov 22, 2020
@Mgsy
Copy link
Member

Mgsy commented Nov 23, 2020

Hi! By default, all widgets (tables, images, media embeds) are displayed in the centre of the editor. However, you can simply change this behaviour with CSS - for example, refer to the .table element and define your desired styling.

@Mgsy Mgsy added pending:feedback This issue is blocked by necessary feedback. type:question This issue asks a question (how to...). and removed type:bug This issue reports a buggy (incorrect) behavior. labels Nov 23, 2020
@burtonator
Copy link
Author

@Mgsy I think that's true and I can take a look but the issue I think is that if I adjust the styling then setting the position in ckeditor doesn't really work.

@runepedersen-visma
Copy link

Would love to get some alignment options here. If not, atleast have left align as default.

@jensen-lin
Copy link

Our users is giving us feedback that they need to have tables and images left aligned as default

@oleq oleq added domain:ui/ux This issue reports a problem related to UI or UX. package:image package:table squad:core Issue to be handled by the Core team. labels Mar 2, 2021
@oleq oleq added this to the nice-to-have milestone Mar 2, 2021
@jensen-lin
Copy link

If we add css to make tables always align to left by default, the middle-alignment button in "Table properties" will no longer be working.

When switching between alignments from "Table properties" modal, html is adjusted as follows:

  • When left-alignment
  • When middle-alignment
  • When right-alignment

so when middle-alignment is chosen, our custom css will make the table align to left.

@Mgsy Mgsy added domain:v4-compatibility This issue reports a CKEditor 4 feature/option that's missing in CKEditor 5. squad:compat type:improvement This issue reports a possible enhancement of an existing feature. and removed pending:feedback This issue is blocked by necessary feedback. squad:core Issue to be handled by the Core team. type:question This issue asks a question (how to...). labels Mar 15, 2021
@Mgsy Mgsy modified the milestones: nice-to-have, iteration 42 Mar 18, 2021
@Reinmar
Copy link
Member

Reinmar commented Mar 18, 2021

TODO:

  • Document how to define which of the image styles is the default one. AFAIK it's doable now as it's under our control whether it's the left/right/center style that doesn't apply a class. However, it's only possible when you define your custom styles. If you use existing ones, that's not doable. Thus, let's document that you can do it via custom styles and for now that's it.
  • As for tables – let's allow defining which of the options left/center/right does not set the style. Same mechanism (configuration paradigm) could be then also added to table cell alignment.

@lslowikowska lslowikowska added the support:2 An issue reported by a commercially licensed client. label Mar 18, 2021
@pomek
Copy link
Member

pomek commented Mar 19, 2021

Image Style

In the CKEditor 5 documentation, you can find the guide about Defining custom styles for images. In the guide, you can also see the advanced configuration options that allows defining custom styles or updating existing ones.

We have three cases that I would like to cover:

  1. Modify the default style for predefined styles
  2. Define the custom styles
  3. Modify the default style while using predefined and custom styles

Modify the default style for predefined styles

As mentioned in the advanced configuration options, you can adjusting predefined styles.

import customIcon from 'custom-icon.svg';

const imageConfig = {
	styles: [
		// This will only customize the icon of the "full" style.
		// Note: 'right' is one of default icons provided by the feature.
		{ name: 'full', icon: 'right' },

		// This will customize the icon, title and CSS class of the default "side" style.
		{ name: 'side', icon: customIcon, title: 'My side style', className: 'custom-side-image' }
	]
};

Based on the code above, we can modify the default style:

const imageConfig = {
	styles: [
	    // The "full" style is no longer the default one.
	    // Let's add the "image-style-full" class when the "full" style is applied.
		{ name: 'full', isDefault: false, className: 'image-style-full' },

		// As for now, images will have the "side" style applied by default.
		// Note: When the default style is applied, the `<figure>` element has no additional classes.
		{ name: 'side', isDefault: true }
	]
};

And the <figure> element when:

  • the full style is applied:
    image
  • the image was just uploaded and the default style was used (side):
    image

Define the custom styles

If predefined styles are not satisfactory enough, you can define custom styles:

import { icons } from '@ckeditor/ckeditor5-core';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		/* The editor configuration... */
		image: {
			styles: [
			    // The "fullStyle" style will apply the "center-class" to `<figure>` element.
				{ name: 'fullStyle', title: 'Custom full style.', icon: icons.objectFullWidth, className: 'center-class' },
				// The "sideStyle" style will apply the "side-class" to `<figure>` element.
				{ name: 'sideStyle', title: 'Custom side style.', icon: icons.objectRight, className: 'side-class' },
				// The "dummyStyle" style is marked as the default one. No class will be added to the `<figure>` element.
				{ name: 'dummyStyle', title: 'Custom dummy style.', icon: icons.threeVerticalDots, isDefault: true }
			],
			toolbar: [
				'imageStyle:fullStyle', 'imageStyle:sideStyle', 'imageStyle:dummyStyle'
			]
		}
		/* The editor configuration... */
	} )

Modify the default style while using predefined and custom styles

The ImageStyle plugin allows using predefined styles and custom definitions:

import { icons } from '@ckeditor/ckeditor5-core';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		/* The editor configuration... */
		image: {
			styles: [
			    // The predefined style "full" is no longer the default one.
				{ name: 'full', isDefault: false, className: 'image-style-full' },
				// Create the custom "sideStyle" style.
				{ name: 'sideStyle', title: 'Custom side style.', icon: icons.objectRight, className: 'side-class' },
				// Create "dummyStyle" style which will be the default one.
				{ name: 'dummyStyle', title: 'Custom dummy style.', icon: icons.threeVerticalDots, isDefault: true }
			],
			toolbar: [
				'imageStyle:full', 'imageStyle:sideStyle', 'imageStyle:dummyStyle',
			]
		}
		/* The editor configuration... */
	} )

On the attached screenshot, you can see the editor with the image styles configuration listed above after uploading an image:

image

The dummyStyle is applied by default.

Summary

Thanks to the ImageStyle plugin, you can customize images in the editor. However, the plugin adds or removes a CSS class from the <figure> element (that contains the image) so all adjustments must be done in the CSS stylesheet.

When the default style is applied, no CSS class is added to the <figure> element.

I hope this will help you. In the case of the table, I'll need a few days to prepare a solution.

@Reinmar
Copy link
Member

Reinmar commented Mar 24, 2021

TODO still in this ticket:

@pomek
Copy link
Member

pomek commented Mar 26, 2021

Proposed API for defining default styles for a table or table cells:

const editorConfig = {
	table: {
		// ...other "table" options.
		tableProperties: {
			// Default styles for the entire table.
			defaultProperties: {
				alignment: 'left',
				borderColor: '#ff0000',
				borderWidth: '2px',
				borderStyle: 'dashed'
			}
		},
		tableCellProperties: {
			// Default styles for each cell in the table.
			defaultProperties: {
				horizontalAlignment: 'right',
				verticalAlignment: 'bottom',
				borderColor: '#0000ff',
				borderWidth: '2px',
				borderStyle: 'dotted'
			}
		}
	}
}

In the clear editor, after inserting the table (2x2) and adding testing content:

image

The editor produces the following HTML:

<p>A paragraph above the table..................................</p>
<figure class="table" style="float:left;">
   <table style="border-bottom:2px dashed #ff0000;border-left:2px dashed #ff0000;border-right:2px dashed #ff0000;border-top:2px dashed #ff0000;">
      <tbody>
         <tr>
            <td style="border-bottom:2px dotted #0000ff;border-left:2px dotted #0000ff;border-right:2px dotted #0000ff;border-top:2px dotted #0000ff;text-align:right;vertical-align:bottom;">
               <p>A1</p>
               <p>A2<br>A3</p>
               <p>(paragraph+soft break)</p>
            </td>
            <td style="border-bottom:2px dotted #0000ff;border-left:2px dotted #0000ff;border-right:2px dotted #0000ff;border-top:2px dotted #0000ff;text-align:right;vertical-align:bottom;">A2</td>
         </tr>
         <tr>
            <td style="border-bottom:2px dotted #0000ff;border-left:2px dotted #0000ff;border-right:2px dotted #0000ff;border-top:2px dotted #0000ff;text-align:right;vertical-align:bottom;">B1<br>B1<br>B1<br>(soft-breaks)</td>
            <td style="border-bottom:2px dotted #0000ff;border-left:2px dotted #0000ff;border-right:2px dotted #0000ff;border-top:2px dotted #0000ff;text-align:right;vertical-align:bottom;">
               <p>B2</p>
               <p>B2</p>
               <p>B2</p>
               <p>(paragraph)</p>
            </td>
         </tr>
      </tbody>
   </table>
</figure>
<p>A paragraph below the table..........</p>

I pushed my changes on the #i/8502 branch. You can use a manual test to play with the feature: http://localhost:8125/ckeditor5-table/tests/manual/tableproperties.html.

PS: Yes, the table can be centered even if the default style aligns it to the left side:

image

The same touches cells:

image

@pomek
Copy link
Member

pomek commented Apr 21, 2021

We were about to release the next version of the editor. Hence, we needed to prepare the project and polishing things that we wanted to show. That's why we have some delays regarding the default table properties.

The phrase "default table properties" can be understood in two ways.

The first one describes the default properties when inserting a new table into the editor, and this was implemented in #9393. Unfortunately, it does not resolve the issue.

The second describes the default properties regarding the CSS definitions on the website where the editor is used. The integrator defines its default style for tables and expects that the editor will understand it. I mean, the editor's UI will respect values defined in CSS:

image

And will not put these values into the model because they are marked as defaults. We do not insert default values into the model to avoid complications. If the attribute is missing, the default value should be applied.

The same rule should be applied while upcasting data (creating the editor instance over non-empty DOM element).

The working prototype is available on the branch https://github.com/ckeditor/ckeditor5/compare/i/8502-ui.

While working on the issue, I found two bugs in the engine package (related to the StylesMap), and table package (related to upcasting properties).

  1. Border styles produced in inline styles could be more optimal #9490 – the border-* CSS selectors produced by the editor in various scenarios were invalid.
  2. upcastStyleToAttribute() helper is called in wrong context #9536 – while upcasting attributes, some converters are called too many times.

The first one is already fixed. The second one blocks the default table properties.

pomek added a commit that referenced this issue Apr 29, 2021
Feature (table): Support for the default table properties. Read more about [the feature in the documentation](https://ckeditor.com/docs/ckeditor5/latest/features/table.html). Closes #8502. Closes #9219.

Fix (engine): The conversion upcast `elementToAttribute()` and `attributeToAttribute()` functions should not call the `model.value()` callback if the element will not be converted. Closes #9536.

MINOR BREAKING CHANGE (table): Clases `TableAlignmentCommand`, `TableBackgroundColorCommand`, `TableBorderColorCommand`, `TableBorderStyleCommand`, `TableBorderWidthCommand`, `TableHeightCommand`, `TablePropertyCommand`, `TableWidthCommand` requires the second argument called `defaultValue` which is the default value for the command.

MINOR BREAKING CHANGE (table): The `TablePropertiesView` class requires additional property in the object as the second constructor argument (`options.defaultTableProperties`).
@archasek
Copy link

archasek commented Feb 6, 2023

#13410

@AliaksandrBortnik
Copy link

AliaksandrBortnik commented Jun 29, 2023

Am I right that default properties in a config are just initial values for the table/cell configuration popup? So, they are still not used to inject them by default into HTML as inline once we just inserted a new table.

In short, those default are simply default UI popup values. That's it. Right?

@niegowski
Copy link
Contributor

@AliaksandrBortnik
Copy link

AliaksandrBortnik commented Jun 29, 2023

@niegowski no, it doesn't. If you use the data created by CKE5 somewhere outside of your application, let's say, you send email templates.. the only option is the inline attributes in HTML. They are added when you use Table/Cell properties via popup, but default config are not applied as inline attributes based on "defaults" on load. I bet the config table default options are simply UI popup initial values, not more than it.

@Witoso
Copy link
Member

Witoso commented Jun 30, 2023

Am I right that default properties in a config are just initial values for the table/cell configuration popup?

@AliaksandrBortnik this is correct. That's why there's a need to implement the stylesheet as well. TBH I find it a bit confusing as well. #1627 may be of help here when we do it in terms of the email templates (there's a proposed implementation in the comments).

@AliaksandrBortnik
Copy link

@Witoso thanks for your response! The confusing part is why the insert table command (toolbar button) does not leverage our default table properties... I do want to provide a default border (in HTML data, not fake CSS border style via selectors) and not force a user to manually set a border each time via TableProperties popup to achieve the same.

@Sebastianyang666
Copy link

Image Style

In the CKEditor 5 documentation, you can find the guide about Defining custom styles for images. In the guide, you can also see the advanced configuration options that allows defining custom styles or updating existing ones.

We have three cases that I would like to cover:

  1. Modify the default style for predefined styles
  2. Define the custom styles
  3. Modify the default style while using predefined and custom styles

Modify the default style for predefined styles

As mentioned in the advanced configuration options, you can adjusting predefined styles.

import customIcon from 'custom-icon.svg';

const imageConfig = {
	styles: [
		// This will only customize the icon of the "full" style.
		// Note: 'right' is one of default icons provided by the feature.
		{ name: 'full', icon: 'right' },

		// This will customize the icon, title and CSS class of the default "side" style.
		{ name: 'side', icon: customIcon, title: 'My side style', className: 'custom-side-image' }
	]
};

Based on the code above, we can modify the default style:

const imageConfig = {
	styles: [
	    // The "full" style is no longer the default one.
	    // Let's add the "image-style-full" class when the "full" style is applied.
		{ name: 'full', isDefault: false, className: 'image-style-full' },

		// As for now, images will have the "side" style applied by default.
		// Note: When the default style is applied, the `<figure>` element has no additional classes.
		{ name: 'side', isDefault: true }
	]
};

And the <figure> element when:

  • the full style is applied:
    image
  • the image was just uploaded and the default style was used (side):
    image

Define the custom styles

If predefined styles are not satisfactory enough, you can define custom styles:

import { icons } from '@ckeditor/ckeditor5-core';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		/* The editor configuration... */
		image: {
			styles: [
			    // The "fullStyle" style will apply the "center-class" to `<figure>` element.
				{ name: 'fullStyle', title: 'Custom full style.', icon: icons.objectFullWidth, className: 'center-class' },
				// The "sideStyle" style will apply the "side-class" to `<figure>` element.
				{ name: 'sideStyle', title: 'Custom side style.', icon: icons.objectRight, className: 'side-class' },
				// The "dummyStyle" style is marked as the default one. No class will be added to the `<figure>` element.
				{ name: 'dummyStyle', title: 'Custom dummy style.', icon: icons.threeVerticalDots, isDefault: true }
			],
			toolbar: [
				'imageStyle:fullStyle', 'imageStyle:sideStyle', 'imageStyle:dummyStyle'
			]
		}
		/* The editor configuration... */
	} )

Modify the default style while using predefined and custom styles

The ImageStyle plugin allows using predefined styles and custom definitions:

import { icons } from '@ckeditor/ckeditor5-core';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		/* The editor configuration... */
		image: {
			styles: [
			    // The predefined style "full" is no longer the default one.
				{ name: 'full', isDefault: false, className: 'image-style-full' },
				// Create the custom "sideStyle" style.
				{ name: 'sideStyle', title: 'Custom side style.', icon: icons.objectRight, className: 'side-class' },
				// Create "dummyStyle" style which will be the default one.
				{ name: 'dummyStyle', title: 'Custom dummy style.', icon: icons.threeVerticalDots, isDefault: true }
			],
			toolbar: [
				'imageStyle:full', 'imageStyle:sideStyle', 'imageStyle:dummyStyle',
			]
		}
		/* The editor configuration... */
	} )

On the attached screenshot, you can see the editor with the image styles configuration listed above after uploading an image:

image

The dummyStyle is applied by default.

Summary

Thanks to the ImageStyle plugin, you can customize images in the editor. However, the plugin adds or removes a CSS class from the <figure> element (that contains the image) so all adjustments must be done in the CSS stylesheet.

When the default style is applied, no CSS class is added to the <figure> element.

I hope this will help you. In the case of the table, I'll need a few days to prepare a solution.

This is only useful when you choose a button after users upload images,Is there any solves to set it once users upload images?cause i saw the 'isDefualt:true' isn't add any className.Even we add className ,still not work

@RM6SS62

This comment was marked as abuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:ui/ux This issue reports a problem related to UI or UX. domain:v4-compatibility This issue reports a CKEditor 4 feature/option that's missing in CKEditor 5. package:image package:table support:2 An issue reported by a commercially licensed client. type:improvement This issue reports a possible enhancement of an existing feature.
Projects
None yet