如何在 Silverstripe 4 的 HTMLEditorField 内容编辑器中向元素添加 css 样式?

How to add a css style to an element in the HTMLEditorField content editor in Silverstripe 4?

在 SS 3.x 中,我们可以使用以下代码通过 Styles 下拉列表将自定义元素添加到 HTMLEditorField 内容编辑器中。我对此的主要用途是将标准 link 变成样式按钮 link。

我们如何在 SS 4.x 中实现这一点?

这是3.x

中的做法

_config.php

<?php
$formats = array(
    array(
        'title' => 'Buttons'
    ),
    array(
        'title' => 'Custom Button',
        'attributes' => array('class'=>'custom-btn'),
        'selector' => 'a'
    )
);
//Set the dropdown menu options
HtmlEditorConfig::get('cms')->setOption('style_formats',$formats);

看起来您需要做的就是创建一个 editor.css 文件,将您的样式放入其中,然后将以下代码片段放入您的 mysite/_config.php 文件中。

use SilverStripe\Forms\HTMLEditor\TinyMCEConfig;

TinyMCEConfig::get('cms')
    ->addButtonsToLine(1, 'styleselect')
    ->setOption('importcss_append', true);

样式会自动添加到下拉列表中。

参考:https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/typography/#custom-style-dropdown