vue简单的富文本实现(亲测可以)

https://www.cnblogs.com/toughy/p/11283234.html

官网 https://quilljs.com/docs/themes/

安装:
npm install vue-quill-editor --save

安装Vue-Quill-Editor需要依赖:

npm install quill --save

 

我用的是vue2 + 

"vue-quill-editor": "^3.0.6",

在入口文件main.js 中引入

import QuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.bubble.css'
import 'quill/dist/quill.snow.css'
Vue.use(QuillEditor)

在需要的组件中使用  代码如下:

<template>
  <div class="content edit_container">
    <quill-editor
        v-model="content"
        ref="myQuillEditor"
        :options="editorOption"
        @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
        @change="onEditorChange($event)">
    </quill-editor>
    <button v-on:click="saveHtml">保存</button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      content: `<p>hello world</p>`,
      editorOption: {
        theme: 'snow',
           modules: {
        toolbar: [   //下面是控制工具栏的展示功能 见 https://blog.csdn.net/qq_36947168/article/details/119486710
                       // https://www.kancloud.cn/liuwave/quill/1409378
          // ['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'],
          // ['image','video']
        ]
      }
      },
     
    }
  },
  computed: {
    editor () {
      return this.$refs.myQuillEditor.quill
    }
  },
  methods: {
    onEditorReady (editor) { // 准备编辑器
    },
    onEditorBlur () {}, // 失去焦点事件
    onEditorFocus () {}, // 获得焦点事件
    onEditorChange () {}, // 内容改变事件
    saveHtml (event) {
      alert(this.content)
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

这样就完成一个简单的富文本编辑器

 

this.comtent 直接存数据库,然后再赋值回去 就实现了存储功能了(后记: 可能需要加cdn不然太慢了)。

 

 

不可编辑的方法

https://blog.csdn.net/qq_41833439/article/details/110183081

 

 

限制输入的字数

<quill-editor
                v-model="addForm.useExplain"
                :options="editorOption"
                ref="myQuillEditor"
                @blur="onEditorBlur($event)"
                @focus="onEditorFocus($event)"
                @change="onEditorChange($event)"
              ></quill-editor>
<div style="text-align:right">
                {{TiLength}}/2000
</div>
<script>
data(){
    return {
        // 限制长度
      TiLength: 0
    }
}
 
mounted(){
    //数据初始化
    this.TiLength =this.$refs.myQuillEditor.quill.getLength() - 1
}
methods: {
    onEditorChange(event) {
      event.quill.deleteText(2000,1);
      if(this.addForm.useExplain===0){
        this.TiLength = 0
      }
      else{
        this.TiLength = event.quill.getLength()-1
      }
    },
}
</script>

展示效果

 

 

图片上传接起牛云啥的(后面看,要上传图片要看这个)

https://blog.csdn.net/qq_44782585/article/details/123571236

https://www.h5w3.com/237280.html

也可以暂时把数据库对应的字段改成longblob 的类似也可以----------这个是数据库相关的修改!!!

 

 

图片上传后自定义缩放的方法 (有空再试试)

https://www.jianshu.com/p/42bc2db19fab

https://www.freesion.com/article/61341058283/  (跟着换个弄可以)

但是有点不一样的地方

vue.config.js  配置文件修改我是这样的

const webpack = require('webpack');

module.exports = {
plugins: [
      new webpack.ProvidePlugin({
        'window.Quill': 'quill/dist/quill.js',
        'Quill': 'quill/dist/quill.js'
      }),
    ]}

自己的实战: https://www.cnblogs.com/kaibindirver/p/18179713

 里面包含了 上传起牛的写法

 

 

 

 

 

 

 

cdn引入方法

 

在main.js文件

引入 quill.js 文件
<script src="https://cdn.quilljs.com/1.3.3/quill.js"></script>
引入主题 css 文件
<link href="https://cdn.quilljs.com/1.3.3/quill.snow.css" rel="stylesheet">

 

在对应组件文件

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>quill富文本编辑器</title>
  <style>
    .btn{
      border:1px solid #eee;
      padding: 15px;
      margin-top: 20px;
      cursor: pointer;
      background: #00aac5;
      color: #fff;
    }
    .content{
      margin-top: 20px;
      padding:12px;
      border:1px solid #eee;
      background: #000;
      color: #fff;
      display: none;
    }
  </style>
  <script src="https://cdn.quilljs.com/1.3.3/quill.js"></script>
  <link href="https://cdn.quilljs.com/1.3.3/quill.snow.css" rel="stylesheet">
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<!-- 编辑器 -->
<div id="editor">
  <p>Hello World!</p>
  <p>Some initial <strong>bold</strong> text</p>
  <p><br></p>
</div>
<!-- 按钮 -->
<div style="padding: 15px;margin-top: 20px;">
  <span onclick="nihao();" class="btn">获取编辑器内容</span>
</div>
<!-- 获取内容 -->
<div class="content">
</div>

<script>
  var quill = new Quill('#editor', {
    theme: 'snow'
  });
  function nihao() {
    let t = quill.container.firstChild.innerHTML
    console.log(t)
    $('.content').css('display', 'block')
    $('.content').text(t)
  }
</script>
</body>
</html>

 

来源:https://blog.csdn.net/weixin_38788347/article/details/78249433

 

 

控制富文本禁止输入和去掉焦点

<quill-editor
      style="height:100%"
      v-model="content"
      :options="editorOption"
      @focus="onEditorFocus($event)"
      @change="sendContent()"
    >
    </quill-editor>




 onEditorFocus(event) {
      if (this.status == 1) {
        event.enable(false);
      } else {
        event.enable(true);
      }


      // 改变焦点 让富文本 焦点消失
      this.$refs.demo.focus()
      
          },

 

 

后记:

有时用这个子组件要等待一下,不然拿不到这个子组件的值

posted @ 2021-01-13 11:19  凯宾斯基  阅读(5348)  评论(1编辑  收藏  举报