nuxt 中使用 vue-quill-editor 富文本编辑器

nuxt 中使用 vue-quill-editor

vue-quill-editor文档

1、安装

npm i /yarn add vue-quill-editor@3.0.6

2、创建文件 vue-quill-editor.js

import Vue from 'vue'
import VueQuillEditor from 'vue-quill-editor/dist/ssr'
Vue.use(VueQuillEditor)

3、nuxt.config.js 引入

css: ['quill/dist/quill.snow.css','quill/dist/quill.bubble.css','quill/dist/quill.core.css'],
plugins: [{ src: '@/plugins/vue-quill-editor', ssr: false }]

4、html 代码结构

<template>
  <div class="editor-box">
    <div
      class="quill-editor"
      :content="content"
      v-quill:myQuillEditor="options"
      :style="cusStyle"
      :width="toolBarwidth"
      @change="handleEditorChange"
    >
    </div>
  </div>
</template>
<script>
  export default {
    props: {
      // 内容
      content: {
        type: String,
        default: ''
      },
      // 配置项
      options: {
        type: Object,
        default () {
          return {}
        }
      },
      // 自定义样式
      cusStyle: {
        type: String,
        default: ''
      },
      // 宽度
      toolBarwidth: {
        type: String,
        default: '500px'
      },
      maxLength: {
        type: Number,
        default: 100
      }
    },
    data () {
      return {
        editorOption: {
          /* quill options */
          modules: {
            toolbar: [
              ['bold', 'italic', 'underline', 'strike'], //加粗,斜体,下划线,删除线
              // ['blockquote', 'code-block'], //引用,代码块
              // [{ header: 1 }, { header: 2 }], // 标题,键值对的形式;1、2表示字体大小
              [{ list: 'ordered' }, { list: 'bullet' }], //列表
              // [{ script: 'sub' }, { script: 'super' }], // 上下标
              // [{ indent: '-1' }, { indent: '+1' }], // 缩进
              // [{ direction: 'rtl' }], // 文本方向
              // [{ size: ['small', false, 'large', 'huge'] }], // 字体大小
              // [{ header: [1, 2, 3, 4, 5, 6, false] }], //几级标题
              // [{ color: [] }, { background: [] }], // 字体颜色,字体背景颜色
              // [{ font: [] }], //字体
              // [{ align: [] }], //对齐方式
              // ['link', 'image', 'video']
              ['clean'], //清除字体样式
            ],
          }
        }
      }
    },
    mounted () {
      // let quillEditor = document.querySelectorAll('.ql-container')

      // let newArr = Array.from(quillEditor)
      // if (newArr.length < 3) return
      // newArr.forEach(item => {
      //   item.appendChild(this.createSpan())
      // })
      
      this.$nextTick(() => {
        let quill = document.querySelectorAll('.ql-toolbar')
        Array.from(quill).forEach(item => {
          item.style.width = this.toolBarwidth
        })
      })
      
    },
    methods: {
      createSpan () {
        let spana = document.createElement('span')
        spana.className = 'suffix-counter'
        let ema = document.createElement('em')
        ema.innerHTML = `${this.textLength}`
        let txt = document.createTextNode(`/${this.maxLength}`)
        spana.appendChild(ema)
        spana.appendChild(txt)
        return spana
      },
      handleEditorFocus (event) {
        // console.log(event);
        // event.enable(true)
        // if (this.textLength >= this.maxLength) {
        //   event.enable(false) // 在获取焦点的时候禁用
        // }
      },
      handleEditorChange (event) {
        // this.textLength = event.text.length - 1
        // if (this.textLength >= this.maxLength) {
        //   event.quill.enable(false)
        //   return
        // }
        this.$emit('change', event)
      }
    }
  }
</script>
<style lang="scss" scoped>
.editor-box {
  /deep/ .ql-toolbar.ql-snow {
    width: 500px;
  }
  /deep/ .ql-editor {
    min-height: 90px;
  }
  /deep/ .ql-editor ol, .ql-editor ul {
    padding-left: 0;
  }
}
// .ql-toolbar.ql-snow {
//   width: 500px;
// }
// .ql-editor ol, .ql-editor ul {
//   padding-left: 0;
// }

.suffix-counter {
  position: absolute;
  right: 0;
  bottom: 0;
  padding: 0 4px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 12px;
  color: $text-grey-color;
}

</style>
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以通过自定义 quill 的 toolbar 进行实现,具体步骤如下: 1. 安装 `vue-quill-editor` 和 `quill-image-resize-module` ``` npm install vue-quill-editor quill-image-resize-module --save ``` 2. 在 nuxt 项目,创建一个插件文件 `vue-quill-editor.js`,在该文件引入 `vue-quill-editor` 和 `quill-image-resize-module` ```javascript import Vue from 'vue' import Quill from 'quill' import VueQuillEditor from 'vue-quill-editor' import ImageResize from 'quill-image-resize-module' Quill.register('modules/imageResize', ImageResize) Vue.use(VueQuillEditor) ``` 3. 在 `nuxt.config.js` ,将该插件引入 ```javascript plugins: [ { src: '~/plugins/vue-quill-editor.js', ssr: false } ] ``` 4. 在需要使用富文本编辑器的组件,将 `modules` 属性设为 `{ imageResize: true }`,并自定义 `toolbar` ```vue <template> <div> <quill-editor v-model="content" :options="editorOption"></quill-editor> </div> </template> <script> export default { data() { return { content: '', editorOption: { modules: { imageResize: {}, toolbar: [ // 自定义 toolbar ['bold', 'italic', 'underline', 'strike'], ['blockquote', 'code-block'], [{ header: 1 }, { header: 2 }], [{ list: 'ordered' }, { list: 'bullet' }], [{ script: 'sub' }, { script: 'super' }], [{ indent: '-1' }, { indent: '+1' }], [{ direction: 'rtl' }], [{ size: ['small', false, 'large', 'huge'] }], [{ header: [1, 2, 3, 4, 5, 6, false] }], [{ color: [] }, { background: [] }], [{ font: [] }], [{ align: [] }], ['clean'], ['link', 'image', 'video'] ] } } } } } </script> ``` 5. 在 `quill-image-resize-module` ,可以通过 `minSize` 和 `maxSize` 属性设置图片的最小和最大尺寸 ```javascript Quill.register('modules/imageResize', ImageResize({ modules: ['Resize', 'DisplaySize', 'Toolbar'], // 可以自定义使用的模块 handleStyles: { backgroundColor: 'black', border: 'none', color: 'white' }, minSize: { width: 50, height: 50 }, maxSize: { width: 800, height: 800 } })) ``` ### 回答2: Nuxt使用vue-quill-editor改变图片大小的方法如下: 1. 首先,在Nuxt项目安装vue-quill-editor插件。可以通过npm或yarn来安装,例如运行以下命令:npm install vue-quill-editor 2. 在Nuxt项目的.vue文件,引入vue-quill-editor插件。可以通过import语句导入插件:import VueQuillEditor from "vue-quill-editor" 3. 在Vue组件的data属性,定义一个变量用于保存图片的大小设置,例如:imgSize: "300px"。这里的"300px"表示图片的宽度为300像素。 4. 在Vue组件使用vue-quill-editor来创建富文本编辑器的实例。在vue-quill-editor组件,可以通过配置项来设置图片大小。例如,可以使用formats属性设置图片的默认大小,代码如下: <VueQuillEditor ref="myQuillEditor" :formats="{ image: {width: this.imgSize} }" ></VueQuillEditor> 这里的image表示图片格式,width表示图片的宽度。可以将width的值设置为data属性定义的imgSize变量。 5. 最后,在Vue组件的methods定义一个方法,用于修改图片大小。可以通过修改imgSize的值来改变图片的大小,代码如下: changeImgSize() { this.imgSize = "500px"; } 这里将imgSize的值设置为"500px",表示图片的宽度为500像素。 总之,使用Nuxtvue-quill-editor插件可以方便地改变图片的大小。通过设置format属性和修改data的imgSize变量,可以达到改变图片大小的效果。 ### 回答3: 在Nuxt使用vue-quill-editor插件来改变图片大小,可以通过自定义上传组件以及配置项来实现。 首先,在Nuxt项目安装vue-quill-editor插件和相关依赖: ```bash npm install vue-quill-editor @quilljs/image-resize-module ``` 然后,在Nuxt创建一个自定义的上传组件(Upload.vue): ```html <template> <input type="file" @change="uploadImage" /> </template> <script> export default { methods: { uploadImage(event) { const file = event.target.files[0]; const reader = new FileReader(); reader.onload = () => { // 将图片转换成base64格式,并放入editor const imgBase64 = reader.result; this.$emit('insertImage', imgBase64); }; reader.readAsDataURL(file); } } }; </script> <style scoped> input[type="file"] { display: none; } </style> ``` 接下来,在Nuxt的页面(比如:index.vue使用vue-quill-editor和自定义的上传组件来实现图片大小调整: ```html <template> <div> <div class="toolbar"> <button @click="changeImageSize">改变图片大小</button> </div> <quill-editor v-model="content" ref="myQuillEditor"> <upload @insertImage="insertImage"></upload> </quill-editor> </div> </template> <script> import { quillEditor } from 'vue-quill-editor'; export default { components: { quillEditor, upload: () => import('~/components/Upload.vue') }, data() { return { content: '' }; }, methods: { insertImage(imgBase64) { const quill = this.$refs.myQuillEditor.quill; const range = quill.getSelection(); quill.insertEmbed(range.index, 'image', imgBase64); }, changeImageSize() { const quill = this.$refs.myQuillEditor.quill; const images = quill.container.querySelectorAll('img'); Array.from(images).forEach((image) => { // 修改图片大小的逻辑 // 可以通过修改image的width和height属性来改变图片大小 }); } } }; </script> <style scoped> .toolbar { margin-bottom: 10px; } </style> ``` 以上,我们在页面添加了一个按钮来调用changeImageSize方法,该方法可以获取Quill编辑器的图片元素,进而可以改变其大小。通过修改图片元素的width和height属性,可以实现改变图片大小的效果。 当然,具体的改变图片大小的逻辑可以根据实际需求来进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值