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

该文章介绍了一个基于TinyMCE的富文本编辑器组件的初始化配置,包括语言设置、工具栏、插件、全屏模式等,并针对与elementUI弹窗的冲突提出了解决方案,通过监听鼠标事件动态调整zIndex来确保编辑器和弹窗的正常显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值