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

TinyMCE custom config doesnt trigger if not in Main tab unless the skin is explicitly set #1457

Closed
ButerRick opened this issue Feb 10, 2023 · 12 comments

Comments

@ButerRick
Copy link

ButerRick commented Feb 10, 2023

So I made different tinyMCE configurations so that I would be able to set different styled of HTMLEditorFields in my project.
It all worked very well with 1 configuration, but when I added more I noticed the issue.

So the official issue is this:

  • I made some different HTMLEditor configs in _config.php.
  • I use these in Elemental blocks and tabs just on a page.
  • The weird thing is that the HTMLEditor doesnt show on any block or in any tab. But as soon as I put one in the Root.Main of a page, all of a sudden they all work. Looks like it only loads all the stuff it need on the Main of a page.
  • Looks like when I havent visited a page tinyMCE is not able to find skin.min.css and content.min.css
    After visiting a page where this htmlEditor with the config exists it does load.

This is my _config.php

<?php

use SilverStripe\Security\PasswordValidator;
use SilverStripe\Security\Member;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;

// remove PasswordValidator for SilverStripe 5.0
$validator = PasswordValidator::create();
// Settings are registered via Injector configuration - see passwords.yml in framework
Member::set_password_validator($validator);

// Default editor config
// ---------------------
$cmsConfig = HTMLEditorConfig::get('cms');

// plugins
$cmsConfig->disablePlugins('table');
$cmsConfig->enablePlugins(['hr']);

// buttons
$cmsConfig->setButtonsForLine(1, [
    'bold', 'italic', 'underline', 'strikethrough', '|',
    'subscript', 'superscript', '|',
    'removeformat', '|',
    'alignleft', 'aligncenter', 'alignright', '|',
    'bullist', 'numlist', 'outdent', 'indent',
]);
$cmsConfig->setButtonsForLine(2, [
    'formatselect', 'styleselect', '|',
    'paste', 'pastetext', '|',
    'table', 'sslink', 'unlink', '|',
    'code'
]);
$cmsConfig->setButtonsForLine(3, []);

$cmsStyleFormats = [
    [
        'title' => 'Headings',
        'items' => [
            [
                'title' => 'Large (h1 styling)',
                'selector' => 'h1,h2,h3',
                'attributes' => ['class' => 'js-reveal-text h1'], // Use attributes instead of classes to overwrite all classes
            ],
            [
                'title' => 'Medium (h2 styling)',
                'selector' => 'h1,h2,h3',
                'attributes' => ['class' => 'js-reveal-text h2'],
            ],
            [
                'title' => 'Small (h3 styling)',
                'selector' => 'h1,h2,h3',
                'attributes' => ['class' => 'js-reveal-text h3'],
            ],
        ]
    ],
    [
        'title' => 'Lists',
        'items' => [
            [
                'title' => 'USP List',
                'selector' => 'ul,ol',
                'attributes' => ['class' => 'usp-list'], // Use attributes instead of classes to overwrite all classes
            ],
        ]
    ],
];

$cmsConfig->setOptions([
    'style_formats' => $cmsStyleFormats,
    'importcss_append' => true,
    'preview_styles' => false,
    'importcss_selector_filter' => '.editor-',
]);

// Header editor config
// --------------------

$headerConfig = HTMLEditorConfig::get('header-config');
$headerConfig->setButtonsForLine(1, ['formatselect', 'styleselect', '|', 'underline']);
$headerConfig->setButtonsForLine(2, []);
$headerConfig->setButtonsForLine(3, []);

$headerStyleFormats = [
    [
        'title' => 'Headings',
        'items' => [
            [
                'title' => 'Large (h1 styling)',
                'selector' => 'h1,h2,h3',
                'attributes' => ['class' => 'js-reveal-text h1'], // Use attributes instead of classes to overwrite all classes
            ],
            [
                'title' => 'Medium (h2 styling)',
                'selector' => 'h1,h2,h3',
                'attributes' => ['class' => 'js-reveal-text h2'],
            ],
            [
                'title' => 'Small (h3 styling)',
                'selector' => 'h1,h2,h3',
                'attributes' => ['class' => 'js-reveal-text h3'],
            ],
        ]
    ],
];

$headerConfig->setOptions([
    'block_formats' => 'Header 1=h1;Header 2=h2;Header 3=h3',
    'style_formats' => $headerStyleFormats,
    'importcss_append' => true,
    'preview_styles' => false,
    'importcss_selector_filter' => '.editor-',
    'formats' => [
        'h1' => ['attributes' => ['class' => 'js-reveal-text h1'], 'block' => 'h1',],
        'h2' => ['attributes' => ['class' => 'js-reveal-text h2'], 'block' => 'h2',],
        'h3' => ['attributes' => ['class' => 'js-reveal-text h3'], 'block' => 'h3',],
        'underline' => [
            [
                'inline' => 'span',
                'classes' => 'text--underline',
            ]
        ]
    ],
]);

// Intro editor config
// --------------------

// ! https://github.com/silverstripe/silverstripe-admin/issues/193

$module = ModuleLoader::inst()->getManifest()->getModule('silverstripe/admin');

$introConfig = HTMLEditorConfig::get('intro-config');
$introConfig->enablePlugins([
    'sslink' => $module->getResource('client/dist/js/TinyMCE_sslink.js'),
    'sslinkexternal' => $module->getResource('client/dist/js/TinyMCE_sslink-external.js'),
    'sslinkemail' => $module->getResource('client/dist/js/TinyMCE_sslink-email.js'),
]);
$introConfig->setButtonsForLine(1, ['sslink']);
$introConfig->setButtonsForLine(2, []);
$introConfig->setButtonsForLine(3, []);

$introConfig->setOptions([
    'block_formats' => 'Paragraph=p;',
    'body_class' => 'typography typography--intro',
    'preview_styles' => false,
    'importcss_selector_filter' => '.editor-',
]);

Now when I use one of these configs, I put it in the htmlEditorField like this:

$fields->addFieldsToTab('Root.Hero', [ HTMLEditorField::create('PageTitle') ->setTitle('Titel') ->setRows(15) ->setEditorConfig('header-config'), ...

So this one doesnt sit on Root.Main, so it doesnt load the config.
image
And I see that certain styles are not beeing found:
image

When I visit a page where the config is loaded on root.Main, everything loads and I can see it everywhere.
I made a gif for example:

CMS_Issue

I hope this is enough information. Otherwise I will provide more.

Acceptance criteria

  • Validate if this affects CMS4 or CMS5 or both
  • TinyMCE config is loaded on all tabs when access pages.

PR

@ButerRick
Copy link
Author

Update:
I just made a contactpage with the HTMLEditorField with the config enabled in the Main tab.
This made no difference. As in, I still dont see the HTMLTextEditors there.
I only see it when I enter a specific page type.

...to be continued

@GuySartorelli
Copy link
Member

Can you please paste the code as text in code blocks instead of images? It will make reproducing this a lot easier if we can just copy the code into a test project.

@ButerRick
Copy link
Author

@GuySartorelli . Ofcourse. My issue is updated.
Thanks in advance

@GuySartorelli
Copy link
Member

Thanks.

Sounds like it might be related to (or potentially the same as) silverstripe/silverstripe-framework#10673

@sirtoobii
Copy link

@ButerRick The trick is to explicitly set the editor skin to silverstripe

@ButerRick
Copy link
Author

@sirtoobii can you please explain what you mean with editor skin? And where do I set this?
Thank you so much in advance

@sirtoobii
Copy link

Hey @ButerRick
For example one of our "simple" editor configs:

HtmlEditorConfig::get('pd_simple')
    ->setOptions([
        'skin' => 'silverstripe',
        'valid_elements' => "p,pre,a[href],strong,em,sup,sub",
        'max_width' => 500,
        'verify_html' => true,
        'forced_root_blocks' => false,
        'remove_linebreaks' => true,
        'schema' => 'html5',
        'paste_as_text' => true,
    ])
    ->disablePlugins('advimagescale', 'table', 'paste')
    ->enablePlugins('link', 'code')
    ->setButtonsForLine(1, [
        'italic', 'bold', 'link', 'unlink', 'superscript', 'subscript', 'code'
    ])
    ->setButtonsForLine(2, []);

@ButerRick
Copy link
Author

@sirtoobii Adding 'skin' => 'silverstripe', to options worked like a charm.
I dont know how or why, but my issue is resolved.

I'm letting @GuySartorelli determine if this can be closed or not

@michalkleiner
Copy link
Contributor

Adding the skin is one thing, we need to determine whether adding/adjusting any config option has the same effect, i.e. whether it's about invoking and setting the config in general or whether the skin is the culprit.

@GuySartorelli GuySartorelli changed the title TinyMCE custom config doesnt trigger if not in Main tab TinyMCE custom config doesnt trigger if not in Main tab unless the skin is explicitly set Feb 15, 2023
@GuySartorelli
Copy link
Member

I've changed the name of the issue slightly to reflect the new information - but this is definitely still a bug. If it is just not adding the skin that causes this, it should fall back to the tinymce default skin, I'd have thought.

@sabina-talipova
Copy link
Contributor

This issue has been tested on CMS 5 and CMS 4. CMS 5 doesn't have this issue. Issue was fixed in CMS 4.

@sabina-talipova sabina-talipova removed their assignment May 2, 2023
@GuySartorelli GuySartorelli self-assigned this May 3, 2023
@GuySartorelli
Copy link
Member

PR merged and tagged as 1.13.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants