当前位置:首页 > 技术 > 正文内容

Vue 项目中集成 vue-quill-editor 并实现行高自定义与工具栏增强

访客 技术 2026年8月15日 1

扩展 Quill 属性实现自定义行高

在 Vue 项目中,如果需要为 vue-quill-editor 增加行高控制功能,首先需要通过 Quill 的 Parchment 模块定义一个新的样式属性。可以创建一个独立的 JavaScript 文件来处理行高逻辑。

// line-height-handler.js
import Quill from "quill";

const Parchment = Quill.import("parchment");

class LineHeightStyleAttributor extends Parchment.Attributor.Style {}

// 定义行高白名单及样式映射
const LineHeightConfig = new LineHeightStyleAttributor("lineHeight", "line-height", {
  scope: Parchment.Scope.INLINE,
  whitelist: ["1", "1.2", "1.5", "1.75", "2", "2.5", "3"]
});

export { LineHeightConfig };

在组件中注册并配置编辑器

在编辑器初始化之前,需要通过 Quill.register 方法将自定义的行高格式注册到编辑器实例中。同时,在工具栏配置项中加入该功能。

import { quillEditor } from 'vue-quill-editor';
import * as Quill from 'quill';
import { LineHeightConfig } from "./line-height-handler";

// 注册行高扩展
Quill.register({ "formats/lineHeight": LineHeightConfig }, true);

export default {
  components: { quillEditor },
  data() {
    return {
      content: '',
      editorSettings: {
        placeholder: '请输入内容...',
        theme: 'snow',
        modules: {
          toolbar: {
            container: [
              ['bold', 'italic', 'underline'],
              [{ 'header': [1, 2, 3, false] }],
              [{ 'lineHeight': LineHeightConfig.whitelist }], // 引入行高白名单
              ['image', 'link']
            ],
            handlers: {
              lineHeight: (value) => {
                if (value) {
                  this.$refs.textEditor.quill.format("lineHeight", value);
                }
              }
            }
          }
        }
      }
    };
  },
  methods: {
    onEditorReady(quill) {
      // 编辑器准备就绪后的回调
    }
  }
};

优化工具栏 UI 样式

默认情况下,自定义的工具栏项可能没有文字显示,需要通过 CSS 伪元素 ::before 来设置下拉列表的显示名称。

<style lang="scss">
/* 设置行高插件的默认标签 */
.ql-snow .ql-picker.ql-lineHeight .ql-picker-label::before {
  content: "行高";
}

/* 映射行高数值到显示文字 */
.ql-snow .ql-picker.ql-lineHeight .ql-picker-item::before,
.ql-snow .ql-picker.ql-lineHeight .ql-picker-label::before {
  &[data-value="1"]::before { content: "单倍"; }
  &[data-value="1.5"]::before { content: "1.5倍"; }
  &[data-value="2"]::before { content: "2倍"; }
  &[data-value="3"]::before { content: "3倍"; }
}

.ql-snow .ql-picker.ql-lineHeight {
  width: 80px;
}
</style>

处理图片上传与工具栏提示优化

为了提升用户体验,通常会结合 el-upload 等组件处理图片上传,并为工具栏图标添加中文悬浮提示。

<template>
  <div class="editor-wrapper">
    <!-- 隐藏的上传组件,由编辑器工具栏触发 -->
    <el-upload
      v-show="false"
      class="custom-uploader"
      action="/api/upload"
      :before-upload="validateFile"
      :on-success="insertImageToQuill"
    />
    <quill-editor
      ref="textEditor"
      v-model="content"
      :options="editorSettings"
      @change="handleContentChange"
    />
  </div>
</template>

<script>
export default {
  methods: {
    insertImageToQuill(res) {
      const quill = this.$refs.textEditor.quill;
      const selection = quill.getSelection();
      if (res.url) {
        quill.insertEmbed(selection ? selection.index : 0, 'image', res.url);
        quill.setSelection(selection.index + 1);
      }
    },
    setupToolbarTooltips() {
      const tooltips = [
        { selector: '.ql-bold', text: '加粗' },
        { selector: '.ql-image', text: '插入图片' },
        { selector: '.ql-lineHeight', text: '设置行高' }
      ];
      tooltips.forEach(item => {
        const node = document.querySelector(item.selector);
        if (node) node.setAttribute('title', item.text);
      });
    }
  },
  mounted() {
    this.setupToolbarTooltips();
  }
};
</script>

进阶布局适配

针对富文本编辑器在页面中的显示,建议通过 CSS 深度选择器对内容区域进行微调,确保图片的响应式展示和段落间距的正确解析。

<style scoped>
.editor-wrapper ::v-deep .ql-editor {
  min-height: 300px;
  line-height: 1.5;
  
  p img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 10px auto;
  }
}

/* 修复缩进导致样式偏移的问题 */
.editor-wrapper ::v-deep .ql-indent-1 {
  text-indent: 2em;
  padding-left: 0 !important;
}
</style>

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。