GetmeUK / ContentTools

A JS library for building WYSIWYG editors for HTML content.
http://getcontenttools.com
MIT License
3.94k stars 393 forks source link

Problem update image classes #572

Closed vovainua1992 closed 3 years ago

vovainua1992 commented 3 years ago

Hi, first of all, I want to thank you for your wonderful app, it's just incredible. I have a problem with image classes. In edit mode, everything is fine, as soon as I return to view mode, there is only one style class left, the other classes disappear

anthonyjb commented 3 years ago

Hi @vovainua1992 - so I can hazard a guess that this is something I still occasionally fall over on new sites.

Basically CT converts images to div tags while editing them and so there are some styles in place for images provided by the editor during edit mode. When you stop editing the page these classes are removed (as they are only for editing and shouldn't pollute your page) but this means if there are no base styles in place for images in place your images can appear out of wack.

The following is some basic SCSS we commonly apply to images and iframes (as the same is true for embedded videos) within formatted content sections (which we define as say <div class="formatted">...</div>. This helps to normalize the behaviour between the edit and view mode.

/**
 * IMAGES
 * --
 * Provides the image styling of child elements within the formatted class.
 */

> img,
> a > img,
> .ce-element--type-image,
> a > .ce-element--type-image,
> iframe,
> .ce-element--type-video {
    display: block;
    margin: 32px auto 24px;
    max-width: 100%;
}

// Alignment
> .align-left {
    @include breakpoint($medium, up) {
        clear: left;
        float: left;
        margin-top: 0;
        margin-right: 40px;
    }

    & > *,
    & + * {
        margin-top: 0 !important;
    }
}
> .align-right {
    @include breakpoint($medium, up) {
        clear: right;
        float: right;
        margin-top: 0;
        margin-left: 40px;
    }

    & > *,
    & + * {
        margin-top: 0 !important;
    }
}

> img,
> a > img,
> .ce-element--type-image,
> a > .ce-element--type-image {
    height: auto;
}

Hope this helps, let me know if it's something else.

vovainua1992 commented 3 years ago

Unfortunately it didn't help me ... In edit mode - <div class = "align-right ce-element ce-element - type-image" ... In view mode - <img class = "align-right" ...

If this helps, here is a link to a repository with JS and CSS code https://github.com/vovainua1992/freedom

anthonyjb commented 3 years ago

@vovainua1992 the ce-... classes should only be present in edit mode. The SCSS above should mean the same styles are applied to all images with or without the classes applied. If this isn't the case can you check in the browser console and make sure the relevant CSS is being applied to image elements? Will take a look at your CSS now to see if I can spot anything obvious.

anthonyjb commented 3 years ago

OK looking at your CSS as your not applying the formatted class I mentioned you need to modify it like so:

img,
a > img,
.ce-element--type-image,
a > .ce-element--type-image,
iframe,
.ce-element--type-video {
    display: block;
    margin: 32px auto 24px;
    max-width: 100%;
}

.align-left > *,
.align-left + * {
    margin-top: 0 !important;
}
.align-right > *,
.align-right + * {
    margin-top: 0 !important;
}

img,
a > img,
.ce-element--type-image,
a > .ce-element--type-image {
    height: auto;
}

Keep in mind this isn't ideal as the CSS will apply globally and not just the content within the editable area. At some point you probably want to consider a class to scope the CSS.

vovainua1992 commented 3 years ago

Thanks for your advice. I solved my problem a little differently. And thank you again for your wonderful app