繁体   English   中英

tinyMCE 工具栏右侧的浮动按钮

[英]Float button to the right on tinyMCE toolbar

我想要做的是创建一个工具栏,其中一些默认按钮与左侧对齐,然后在同一个工具栏的右侧对齐一个自定义按钮/下拉菜单。

这是我的 html/javascript/init:

<h3>
  Behold: Magic
</h3>
<div>
  <%= text_area_tag :content, "", id: "magic", rows: 20 %>
</div>
<script type="text/javascript">
tinymce.init({
  selector: "#magic",
  plugins: "link image",
  menubar: false,
  toolbar: "bold italic underline strikethrough | styleselect | bullist numlist | undo redo | link image | insertField",
  setup: function(editor) {
    editor.addButton("insertField", {
      type: "menubutton",
      text: "Insert Field",
      style: "float: right",
      menu: [
        { text: "First Name", onclick: function() { editor.insertContent("tom"); } },
        { text: "Last Name", onclick: function() { editor.insertContent("prats"); } }
      ]
    });
  }
});
</script>

到目前为止,这段代码的工作原理是,包含所有元素的 tinyMCE 工具栏都在那里,但仍然向左对齐。 这是它的外观/应该看起来:

错误的:

These | Count | As | Buttons | Floated | Left | Floated Right

对:

These | Count | As | Buttons | Floated | Left                       Floated Right

如您所见,我尝试通过样式(但也包括类)选项添加 css,尽管它出现在元素上,但该元素并没有到右侧。 任何帮助,将不胜感激。

你没有指定你的 TinyMCE 版本,所以我假设你在谈论 TinyMCE 4。

首先,您需要确保要向右对齐的按钮属于一个组。 在下面的示例工具栏中,我们将右对齐fullscreen按钮。 我们通过在它前面加上一个管道将该按钮放在它自己的组中: | .

toolbar: 'h2 bold italic | bullist numlist | link unlink | fullscreen'

现在,使用 CSS 伪元素,我们将工具栏中的最后一组作为目标,如下所示:

.mce-btn-group:last-child {
    float:right;
    border-left: none;
}

我们使用float:right将其向右对齐,并使用border-left: none;移除分隔符border-left: none;

大多数最新版本使用 flexbox,因此浮动不起作用。

.tox-toolbar__group:last-child' {
    marginLeft: 'auto',
    borderLeft: 'none',
}

使用CSS:

.mce-toolbar .mce-last { float: right; }

在某些浏览器中,最后一项可能会下拉,您可以将最后一项工具栏项移至第一项并使用:

.mce-toolbar .mce-first { float: right; }

在tinymce 4.7.13中只有这个对我有用

.mce-toolbar .mce-btn-group { width: 100%; }
.mce-toolbar .mce-btn-group .mce-btn.mce-last { float: right; }

我们可以轻松自定义 CSS 样式,以便在 TinyMCE 编辑器上将工具栏按钮浮动到右侧。 它的工具栏有一种名为“tox-toolbar__primary”的样式。

所以你可以像这样插入你的 CSS 代码:

.tox-toolbar__primary {
    display: flex;
    justify-content: flex-end;
}

只需转到 Common.js 文件中的 init 函数并编辑该函数

function InitTinyMCE() {
    tinymce.init({
        mode: "specific_textareas",
        editor_selector: "ub-textarea",
        theme: "modern",
        encoding: "xml",
        plugins: [
            "advlist autolink lists link image charmap print preview hr anchor pagebreak",
            "searchreplace wordcount visualblocks visualchars code fullscreen",
            "insertdatetime media nonbreaking save table contextmenu directionality",
            "emoticons template paste"
        ],
        toolbar1: "undo redo | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | emoticons",
        image_advtab: true,
        menubar: false,

        statusbar: false
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM