• quill富文本工具栏添加行高配置


    quill富文本默认样式如下图 需求提出:缺少文本行高 配置项

    在这里插入图片描述

    quill组件新增部分代码如下

    这里只写新增的部分;组件自带功能省略/文末会有整体组件代码。

    toolbar: [
    			//.... 其他工具栏配置项                             
    	        [{ lineheight: ["", "1", "1-5", "1-75", "2", "3", "4", "5"] }],//行高
          	 ],
    
    • 1
    • 2
    • 3
    • 4
     const editor = this.$refs.editor;
     this.Quill = new Quill(editor, this.options);
      //上面一般是你组件自带的代码
      
      //在这个位置添加下面代码
      const Parchment = Quill.import("parchment");
      class lineHeightAttributor extends Parchment.Attributor.Class {}
      const lineHeightStyle = new lineHeightAttributor(
        "lineheight",
        "ql-lineheight",
        {
          scope: Parchment.Scope.INLINE,
          whitelist: ["", "1", "1-5", "1-75", "2", "3", "4", "5"],
        }
      );
      Quill.register({ "formats/lineHeight": lineHeightStyle }, true);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    css样式新加如下代码

    .ql-snow .ql-picker.ql-lineheight .ql-picker-label::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item::before {
      content: "行高";
    }
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="1"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="1"]::before {
      content: "1";
    }
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="1-5"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="1-5"]::before {
      content: "1.5";
    }
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="1-75"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="1-75"]::before {
      content: "1.75";
    }
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="2"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="2"]::before {
      content: "2";
    }
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="3"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="3"]::before {
      content: "3";
    }
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="4"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="4"]::before {
      content: "4";
    }
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="5"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="5"]::before {
      content: "5";
    }
    .ql-snow .ql-picker.ql-lineheight {
      width: 70px;
    }
    .ql-snow .ql-editor .ql-lineheight-1 {
      line-height: 1;
    }
    .ql-snow .ql-editor .ql-lineheight-1-5 {
      line-height: 1.5;
    }
    .ql-snow .ql-editor .ql-lineheight-1-75 {
      line-height: 1.75;
    }
    .ql-snow .ql-editor .ql-lineheight-2 {
      line-height: 2;
    }
    .ql-snow .ql-editor .ql-lineheight-3 {
      line-height: 3;
    }
    .ql-snow .ql-editor .ql-lineheight-4 {
      line-height: 4;
    }
    .ql-snow .ql-editor .ql-lineheight-5 {
      line-height: 5;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    最后效果展示

    在这里插入图片描述

    全部代码如下:

    <template>
      <div>
        <el-upload
          :action="uploadUrl"
          :data="{ fileSource: 1 }"
          :before-upload="handleBeforeUpload"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
          name="file"
          :show-file-list="false"
          :headers="headers"
          style="display: none"
          ref="upload"
          v-if="this.type == 'url'"
        >
        </el-upload>
        <div class="editor" ref="editor" :style="styles"></div>
      </div>
    </template>
    
    <script>
    import Quill from "quill";
    import "quill/dist/quill.core.css";
    import "quill/dist/quill.snow.css";
    import "quill/dist/quill.bubble.css";
    // import { getToken } from '@/utils/auth' //如果你的接口需要token的话打开这行
    
    export default {
      name: "Editor",
      props: {
        value: {
          type: String,
          default: "",
        },
        height: {
          type: Number,
          default: null,
        },
        minHeight: {
          type: Number,
          default: null,
        },
        readOnly: {
          type: Boolean,
          default: false,
        },
        fileSize: {
          type: Number,
          default: 5,
        },
        type: {
          type: String,
          default: "url",
        },
      },
      data() {
        return {
          uploadUrl: "", // 你的项目里 上传图片的服务器地址
          showImgService: "", // 你的项目里 上传的图片回显地址
          headers: {
            // Authorization: "Bearer " + getToken(),//如果你的接口需要token的话打开这行
            // 'X-Access-Token': getToken(),//如果你的接口需要token的话打开这行
          },
          Quill: null,
          currentValue: "",
          options: {
            theme: "snow",
            bounds: document.body,
            debug: "warn",
            modules: {
              // 工具栏配置
              toolbar: [
                ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
                ["blockquote", "code-block"], // 引用  代码块
                [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
                [{ indent: "-1" }, { indent: "+1" }], // 缩进
                [{ size: ["small", false, "large", "huge"] }], // 字体大小
                [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
                [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
                [{ align: [] }], // 对齐方式
                ["clean"], // 清除文本格式
                ["link", "image"], // 链接、图片
                [{ lineheight: ["", "1", "1-5", "1-75", "2", "3", "4", "5"] }], //行高
              ],
            },
            placeholder: "请输入内容",
            readOnly: this.readOnly,
          },
        };
      },
      computed: {
        styles() {
          let style = {};
          if (this.minHeight) {
            style.minHeight = `${this.minHeight}px`;
          }
          if (this.height) {
            style.height = `${this.height}px`;
          }
          return style;
        },
      },
      watch: {
        value: {
          handler(val) {
            if (val !== this.currentValue) {
              this.currentValue = val === null ? "" : val;
              if (this.Quill) {
                this.Quill.pasteHTML(this.currentValue);
              }
            }
          },
          immediate: true,
        },
      },
      mounted() {
        this.init();
      },
      beforeDestroy() {
        this.Quill = null;
      },
      methods: {
        init() {
          const editor = this.$refs.editor;
          this.Quill = new Quill(editor, this.options);
          const Parchment = Quill.import("parchment");
          class lineHeightAttributor extends Parchment.Attributor.Class {}
          const lineHeightStyle = new lineHeightAttributor(
            "lineheight",
            "ql-lineheight",
            {
              scope: Parchment.Scope.INLINE,
              whitelist: ["", "1", "1-5", "1-75", "2", "3", "4", "5"],
            }
          );
          Quill.register({ "formats/lineHeight": lineHeightStyle }, true);
          // 如果设置了上传地址则自定义图片上传事件
          if (this.type == "url") {
            let toolbar = this.Quill.getModule("toolbar");
            toolbar.addHandler("image", (value) => {
              this.uploadType = "image";
              if (value) {
                this.$refs.upload.$children[0].$refs.input.click();
              } else {
                this.quill.format("image", false);
              }
            });
          }
          this.Quill.pasteHTML(this.currentValue);
          this.Quill.on("text-change", (delta, oldDelta, source) => {
            const html = this.$refs.editor.children[0].innerHTML;
            const text = this.Quill.getText();
            const quill = this.Quill;
            this.currentValue = html;
            this.$emit("input", html);
            this.$emit("on-change", { html, text, quill });
          });
          this.Quill.on("text-change", (delta, oldDelta, source) => {
            this.$emit("on-text-change", delta, oldDelta, source);
          });
          this.Quill.on("selection-change", (range, oldRange, source) => {
            this.$emit("on-selection-change", range, oldRange, source);
          });
          this.Quill.on("editor-change", (eventName, ...args) => {
            this.$emit("on-editor-change", eventName, ...args);
          });
        },
        // 上传前校检格式和大小
        handleBeforeUpload(file) {
          if (this.fileSize) {
            const isLt = file.size / 1024 / 1024 < this.fileSize;
            if (!isLt) {
              this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
              return false;
            }
          }
          return true;
        },
        handleUploadSuccess(res, file) {
          console.log(res);
          let quill = this.Quill;
          if (res.code == 200) {
            let length = quill.getSelection().index;
            quill.insertEmbed(
              length,
              "image",
              this.showImgService + res.result.urlPath
            );
            quill.setSelection(length + 1);
          } else {
            this.$message.error("图片插入失败");
          }
        },
        handleUploadError() {
          this.$message.error("图片插入失败");
        },
      },
    };
    </script>
    
    <style>
    .editor,
    .ql-toolbar {
      white-space: pre-wrap !important;
      line-height: normal !important;
    }
    .quill-img {
      display: none;
    }
    .ql-snow .ql-tooltip[data-mode="link"]::before {
      content: "请输入链接地址:";
    }
    .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
      border-right: 0px;
      content: "保存";
      padding-right: 0px;
    }
    
    .ql-snow .ql-tooltip[data-mode="video"]::before {
      content: "请输入视频地址:";
    }
    
    .ql-snow .ql-picker.ql-size .ql-picker-label::before,
    .ql-snow .ql-picker.ql-size .ql-picker-item::before {
      content: "14px";
    }
    .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
    .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
      content: "10px";
    }
    .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
    .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
      content: "18px";
    }
    .ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
    .ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
      content: "32px";
    }
    
    .ql-snow .ql-picker.ql-header .ql-picker-label::before,
    .ql-snow .ql-picker.ql-header .ql-picker-item::before {
      content: "文本";
    }
    .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
    .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
      content: "标题1";
    }
    .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
    .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
      content: "标题2";
    }
    .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
    .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
      content: "标题3";
    }
    .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
    .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
      content: "标题4";
    }
    .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
    .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
      content: "标题5";
    }
    .ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
    .ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
      content: "标题6";
    }
    
    .ql-snow .ql-picker.ql-font .ql-picker-label::before,
    .ql-snow .ql-picker.ql-font .ql-picker-item::before {
      content: "标准字体";
    }
    .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
    .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
      content: "衬线字体";
    }
    .ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
    .ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
      content: "等宽字体";
    }
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item::before {
      content: "行高";
    }
    
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="1"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="1"]::before {
      content: "1";
    }
    
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="1-5"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="1-5"]::before {
      content: "1.5";
    }
    
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="1-75"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="1-75"]::before {
      content: "1.75";
    }
    
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="2"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="2"]::before {
      content: "2";
    }
    
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="3"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="3"]::before {
      content: "3";
    }
    
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="4"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="4"]::before {
      content: "4";
    }
    
    .ql-snow .ql-picker.ql-lineheight .ql-picker-label[data-value="5"]::before,
    .ql-snow .ql-picker.ql-lineheight .ql-picker-item[data-value="5"]::before {
      content: "5";
    }
    
    .ql-snow .ql-picker.ql-lineheight {
      width: 70px;
    }
    
    .ql-snow .ql-editor .ql-lineheight-1 {
      line-height: 1;
    }
    
    .ql-snow .ql-editor .ql-lineheight-1-5 {
      line-height: 1.5;
    }
    
    .ql-snow .ql-editor .ql-lineheight-1-75 {
      line-height: 1.75;
    }
    
    .ql-snow .ql-editor .ql-lineheight-2 {
      line-height: 2;
    }
    
    .ql-snow .ql-editor .ql-lineheight-3 {
      line-height: 3;
    }
    
    .ql-snow .ql-editor .ql-lineheight-4 {
      line-height: 4;
    }
    
    .ql-snow .ql-editor .ql-lineheight-5 {
      line-height: 5;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352

    总结

    提示:以上方法仅供参考;如有不正望指出;与君共勉。

  • 相关阅读:
    初级篇—第七章数据处理增删改
    Alevel商务知识点:市场调查Market Research
    Kutools for Excel v26.10 Excel插件工具箱中文版
    [篇五章三]-关于 Windows 10 安装好后系统自带的微软输入法没有输入框的 BUG 解决办法
    SQL Server 常见问题
    java计算机毕业设计ssm社团管理系统9e73v(附源码、数据库)
    跑得“快”还要走得“稳”,看头部车企数字化实践怎么做
    java计算机毕业设计高校实习管理平台系统MyBatis+系统+LW文档+源码+调试部署
    键盘盲打是练出来的
    09_子查询
  • 原文地址:https://blog.csdn.net/weixin_45421249/article/details/127424279