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

Add an option to return inline style even if we want to avoid it at the beginning #5933

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface NOOP {}

export type Debounced = Function & { cancel(): void };

export type SetOptions = Backbone.ModelSetOptions & { avoidStore?: boolean };
export type SetOptions = Backbone.ModelSetOptions & { avoidStore?: boolean; inline?: boolean };
Copy link
Member

Choose a reason for hiding this comment

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

I think this one could be moved to UpdateStyleOptions type (I'll do it)


export type AddOptions = Backbone.AddOptions & { temporary?: boolean };

Expand Down
16 changes: 10 additions & 6 deletions src/dom_components/model/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ export default class Component extends StyleableModel<ComponentProperties> {

// Handle style
const style = attrs.style;
style && this.setStyle(style);
style && this.setStyle(style, opts);
delete attrs.style;

const attrPrev = { ...this.previous('attributes') };
Expand Down Expand Up @@ -613,6 +613,10 @@ export default class Component extends StyleableModel<ComponentProperties> {
if (rule) {
return rule.getStyle(prop);
}

// Return empty style if not rule have been found. We cannot return inline style with the next return
// because else on load inline style is set a #id or .class style
return {};
}

return super.getStyle.call(this, prop);
Expand Down Expand Up @@ -671,9 +675,9 @@ export default class Component extends StyleableModel<ComponentProperties> {

// Add style
if (!opts.noStyle) {
const style = this.get('style');
const style = this.getStyle({ inline: true });
if (isObject(style) && !isEmptyObj(style)) {
attributes.style = this.styleToString({ inline: 1 });
attributes.style = this.styleToString({ inline: true });
}
}

Expand Down Expand Up @@ -1572,7 +1576,7 @@ export default class Component extends StyleableModel<ComponentProperties> {
const tag = customTag || model.get('tagName');
const sTag = model.get('void');
const customAttr = opts.attributes;
let attributes = this.getAttrToHTML();
let attributes = this.getAttrToHTML(opts);
delete opts.tag;

// Get custom attributes if requested
Expand Down Expand Up @@ -1643,10 +1647,10 @@ export default class Component extends StyleableModel<ComponentProperties> {
* @return {Object}
* @private
*/
getAttrToHTML() {
getAttrToHTML(opts?: ToHTMLOptions) {
const attrs = this.getAttributes();

if (avoidInline(this.em)) {
if (avoidInline(this.em) && opts?.keepInlineStyle !== true) {
delete attrs.style;
}

Expand Down
5 changes: 5 additions & 0 deletions src/dom_components/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ export type ToHTMLOptions = {
*/
altQuoteAttr?: boolean;

/**
* Keep inline style set intentionally by users with `setStyle({}, { inline: true })`
*/
keepInlineStyle?: boolean;

/**
* You can pass an object of custom attributes to replace with the current ones
* or you can even pass a function to generate attributes dynamically.
Expand Down
6 changes: 6 additions & 0 deletions src/editor/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DeviceManagerConfig } from '../../device_manager/config/config';
import { I18nConfig } from '../../i18n/config';
import { ModalConfig } from '../../modal_dialog/config/config';
import { LayerManagerConfig } from '../../navigator/config/config';
import { KeymapsConfig } from '../../keymaps/config';
import { PageManagerConfig } from '../../pages/types';
import { PanelsConfig } from '../../panels/config/config';
import { ParserConfig } from '../../parser/config/config';
Expand Down Expand Up @@ -356,6 +357,11 @@ export interface EditorConfig {
*/
commands?: CommandsConfig;

/**
* Configurations for keymaps
*/
keymaps?: KeymapsConfig;
Copy link
Member

Choose a reason for hiding this comment

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

🙇‍♂️


/**
* Configurations for Css Composer.
*/
Expand Down
24 changes: 19 additions & 5 deletions test/specs/dom_components/model/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ let em: Editor;

describe('Component', () => {
beforeEach(() => {
em = new Editor({ avoidDefaults: true });
// FIXME: avoidInlineStyle is deprecated and when running in dev or prod, `avoidInlineStyle` is set to true
// The following tests ran with `avoidInlineStyle` to false (this is why I add the parameter here)
em = new Editor({ avoidDefaults: true, avoidInlineStyle: true });
dcomp = em.Components;
em.Pages.onLoad();
compOpts = {
Expand Down Expand Up @@ -278,10 +280,10 @@ describe('Component', () => {
class: 'class1 class2',
style: 'color: white; background: #fff',
});
// Style is not in attributes because it has not been set as inline
expect(obj.getAttributes()).toEqual({
id: 'test',
class: 'class1 class2',
style: 'color:white;background:#fff;',
'data-test': 'value',
});
expect(obj.classes.length).toEqual(2);
Expand All @@ -291,13 +293,25 @@ describe('Component', () => {
});
});

test('set inline style with multiple values of the same key', () => {
test('set style with multiple values of the same key', () => {
obj.setAttributes({ style: CSS_BG_STR });
expect(obj.getStyle()).toEqual(CSS_BG_OBJ);
});

test('get proper style from inline style with multiple values of the same key', () => {
obj.setAttributes({ style: CSS_BG_STR });
test('set style on id and inline style', () => {
obj.setStyle({ color: 'red' }); // Should be set on id
obj.setStyle({ display: 'flex' }, { inline: true }); // Should be set as inline

expect(obj.getStyle()).toEqual({
color: 'red',
});
expect(obj.getStyle({ inline: true })).toEqual({
display: 'flex',
});
});
Comment on lines +301 to +311
Copy link
Member

Choose a reason for hiding this comment

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

❤️


test('get proper style from style with multiple values of the same key', () => {
obj.setAttributes({ style: CSS_BG_STR }, { inline: true });
expect(obj.getAttributes()).toEqual({
style: CSS_BG_STR.split('\n').join(''),
});
Expand Down
Loading