解决tinymce在ElenentUI中使用z-index层级低,菜单不显示

该文章介绍了一个基于TinyMCE的富文本编辑器组件的初始化配置,包括语言设置、工具栏、插件、全屏模式等,并针对与elementUI弹窗的冲突提出了解决方案,通过监听鼠标事件动态调整zIndex来确保编辑器和弹窗的正常显示。
<template>
  <tinymce :init="config"></tinymce>
</template>
<script>
import tinymce from '@tinymce/tinymce-vue';
export default {
  name: 'tinyEditor',
  components: { tinymce },
  props: { params: Object },
  computed: {
    config () {
      return Object.assign({
        // fullscreen_native: true, // 使用浏览器的全屏功能,默认false
        language: /^zh/i.exec(window.navigator.language) ? 'zh_CN' : 'en',
        toolbar_mode: 'sliding',
        convert_urls: false,
        height: 400,
        toolbar: 'fullscreen | fontselect fontsizeselect formatselect | undo redo | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent |  numlist bullist checklist | forecolor backcolor casechange permanentpen formatpainter removeformat | pagebreak | charmap emoticons | preview save print | insertfile image media pageembed template link anchor codesample | a11ycheck ltr rtl | showcomments addcomment kityformula-editor tiny_mce_wiris_formulaEditor tiny_mce_wiris_formulaEditorChemistry',
        plugins: 'print preview powerpaste casechange importcss searchreplace autolink autosave save directionality advcode visualblocks visualchars fullscreen image link media mediaembed template codesample table charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists checklist wordcount a11ychecker imagetools textpattern noneditable help formatpainter permanentpen pageembed charmap mentions quickbars linkchecker emoticons advtable export',
        setup: this.editorSetup,
        draggable_modal: true
      }, this.params);
    }
  },
  methods: {
    editorSetup (editor) {
      /**
       * 解决与element UI弹窗的冲突:
       * 在编辑器初始化阶段向整个编辑器添加Mousemove监听事件,通过获取根节点最高层数赋给编辑器和弹窗层;
       * 编辑区阻止事件冒泡,所以会出现当鼠标停留在编辑区中,无法显示菜单。
       * 这种选择弹层方式,需要在tinymce/themes/silver/theme.min.js中的弹窗class名(tox-tinymce-aux)处加上实例ID,如下图中的 d.id;
       * 监听多个事件是为了尽可能规避,无法显示菜单和弹窗的问题;
       */
      function overlay (aux) {
        return function (e) {
          let index = 1;
          document.body.childNodes.forEach(function(element) {
            if (element.style && element.style.zIndex > index) {
              index = parseInt(element.style.zIndex);
            }
          });
          if ((parseFloat(aux.style.zIndex) || 0) < index) {
            e.target.style.zIndex = aux.style.zIndex = index;
          }
        };
      }
      editor.on('init', function (e) {
        const aux = document.querySelector('.' + e.target.id);
        e.target.editorContainer.addEventListener('mouseenter', overlay(aux), false);
        e.target.editorContainer.addEventListener('mousedown', overlay(aux), false);
      });
    }
  }
};
</script>

### 解决 Vue 项目中 TinyMCE层级 (z-index) 问题 在 Vue 项目集成 TinyMCE 编辑器时,可能会遇到编辑器弹窗被其他组件遮挡的情况。这通常是因为 z-index 设置当所致。 对于 TinyMCE 弹窗遮罩问题,可以考虑通过简单的 CSS 调整来解决[^2]。具体来说: #### 修改全局样式文件中的 z-index 值 如果发现 TinyMCE 的某些部分(如菜单或对话框)显示正常,则可以在项目的全局样式表中增加特定的选择器并提高其 z-index 数值: ```css .tox-tinymce { z-index: 9999 !important; } ``` #### 动态调整 TinyMCE 初始化参数 另一种方法是在初始化 TinyMCE 实例时指定 `content_css` 参数指向自定义的 CSS 文件,在该文件内设置更高的 z-index 来覆盖默认样式: ```javascript import 'path/to/your/custom-skin.css'; const editorConfig = { selector: '#editor', content_css : '/custom-skin.css' }; tinymce.init(editorConfig); ``` 其中 custom-skin.css 中应包含如下规则: ```css /* 自定义皮肤 */ .tox-dialog-wrap { position: fixed; top: 0; bottom: 0; left: 0; right: 0; display: flex; justify-content: center; align-items: center; background-color: rgba(0, 0, 0, .5); } .tox-dialog { width: auto; max-width: none; margin-top: initial; box-shadow: 0px 8px 16px rgb(0 0 0 / 20%); border-radius: 4px; overflow: hidden; z-index: 9999!important; } ``` 以上两种方式都可以有效提升 TinyMCE 各种浮层元素的堆叠顺序,从而避免与其他页面元素发生冲突。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值