vue-quill-editor+plupload富文本编辑器实例详解


Posted in Javascript onOctober 19, 2018

1,先给vue项目中下载vue-quill-editor依赖npm install vue-quill-editor --save

2,下载plupload依赖npm install plupload --save

3,在组件中分别引入对应的js

import { quillEditor }from 'vue-quill-editor'
import '@/assets/js/crypto1/crypto/crypto.js'
import '@/assets/js/crypto1/hmac/hmac.js'
import '@/assets/js/crypto1/sha1/sha1.js'
import Base64from '@/assets/js/base64.js'
import pluploadfrom 'plupload'

4,编写plupload上传图片需要的事件以及参数

let accessid = '阿里oss申请的accessid'
let accesskey = '阿里oss申请的accesskey'
let host = '阿里oss的存储文件地址'
let bucket = 'image'
let g_dirname = ''
let g_object_name = ''
let g_object_name_type = ''
var timestamp
let now = timestamp = Date.parse(new Date())/ 1000
let pos = ''
let suffix = ''
var policyText = {
'expiration': '2020-01-01T12:00:00.000Z',// 设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
 'conditions': [
['content-length-range',0,1048576000]// 设置上传文件的大小限制
 ]
}
var policyBase64 = Base64.encode(JSON.stringify(policyText))
let message = policyBase64
var bytes = Crypto.HMAC(Crypto.SHA1,message,accesskey, {asBytes: true})
var signature = Crypto.util.bytesToBase64(bytes)
// 选择上传名字是本地文件名字还是随机文件名字
function check_object_radio () {
g_object_name_type = 'random_name'
}
// 默认是上传到根目录
function get_dirname () {
g_dirname = 'image/'
}
// 获得随机的字符串
function random_string (len) {
len = len || 32
 var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
 var maxPos = chars.length
 var pwd = ''
 for (var i = 0;i < len;i++) {
pwd += chars.charAt(Math.floor(Math.random()* maxPos))
}
return pwd
}
// 获取文件后缀
function get_suffix (filename) {
pos = filename.lastIndexOf('.')
suffix = ''
 if (pos !== -1) {
suffix = filename.substring(pos)
}
return suffix
}
// 获取文件名字
function calculate_object_name (filename) {
if (g_object_name_type === 'local_name') {
g_object_name += filename
 }else if (g_object_name_type === 'random_name') {
suffix = get_suffix(filename)
g_object_name = random_string(10)+ new Date().getTime()+ suffix
  // g_object_name = filename
 }
return ''
}
function get_uploaded_object_name (filename) {
if (g_object_name_type === 'local_name') {
var tmp_name = g_object_name
  tmp_name = tmp_name.replace(filename,filename)
return tmp_name
 }else if (g_object_name_type === 'random_name') {
return g_object_name
 }
}
// 设置上传参数
function set_upload_param (up,filename,ret) {
// g_object_name = g_dirname
 if (filename !== '') {
suffix = get_suffix(filename)
calculate_object_name(filename)
}
let new_multipart_params = {
'Filename': g_dirname + g_object_name,
'key': g_dirname + g_object_name,
'policy': policyBase64,
'OSSAccessKeyId': accessid,
'success_action_status': '200',// 让服务端返回200,不然,默认会返回204
  'signature': signature
 }
console.log(g_object_name)
up.setOption({
'url': host,
'multipart_params': new_multipart_params
 })
up.start()
}
const toolbarOptions = [
['bold','italic','underline','strike'],// toggled buttons
 ['blockquote','code-block'],
[{'header': 1}, {'header': 2}],// custom button values
 [{'list': 'ordered'}, {'list': 'bullet'}],
[{'script': 'sub'}, {'script': 'super'}],// superscript/subscript
 [{'indent': '-1'}, {'indent': '+1'}],// outdent/indent
 [{'direction': 'rtl'}],// text direction
 [{'size': ['small',false,'large','huge']}],// custom dropdown
 [{'header': [1,2,3,4,5,6,false]}],
[{'color': []}, {'background': []}],// dropdown with defaults from theme
 [{'font': []}],
[{'align': []}],
['link','image'],
['clean']// remove formatting button
]
export default {
name: 'AddNotice',
components: {
quillEditor
},
data () {
return {
loading: false,
textarea_name: '',
textarea_nei: '',
content: null,
editorOption:{
placeholder: '',
theme: 'snow',// or 'bubble'
      modules: {
toolbar: {
container: toolbarOptions,// 工具栏
        handlers: {
'image': function (value) {
if (value) {
// alert(1)
           document.querySelector('#selectfiles').click()
}else {
this.quill.format('image',false);
}
}
}
}
}
}
}
},
props: {
Notice_id: Number,
AddNotice_id: Number
  },
watch: {},
methods: {
data_qing () {
this.textarea_name = ''
    this.content = ''
   },
AddNoticeOff () {
this.$emit('AddNoticeOff')
},
gonggao_push () {
this.loading = true
    let data = {
token: this.token(),
id: this.AddNotice_id,
match_id: this.Notice_id,
title: this.textarea_name,
content: this.content
    }
this.$axios.post('/match/Notice/NoticeSave',this.$qs.stringify(data))
.then((res)=> {
this.loading = false
      console.log(res)
if (res.data.code === '200') {
this.$emit('AddNoticeOff')
this.$emit('notice_ajax',0)
}
}).catch((err)=> {
this.loading = false
     console.log(err)
})
},
escape2Html (str) {
var arrEntities = {'lt': '<','gt': '>','nbsp': ' ','amp': '&','quot': '"'}
return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function (all,t) {
return arrEntities[t]
})
},
data_zhan () {
let data = {
token: this.token(),
id: this.AddNotice_id
    }
this.$axios.post('/match/Notice/NoticeInfo',this.$qs.stringify(data))
.then((res)=> {
let new_data = res.data.data
      if (res.data.code === '200') {
this.textarea_name = new_data.title
       this.content = this.escape2Html(new_data.content)
console.log(this.escape2Html(new_data.content))
}
}).catch((err)=> {
console.log(err)
})
}
},
computed: {
},
created () {
},
mounted () {
var that = this
   var uploader = new plupload.Uploader({
runtimes: 'html5,flash,silverlight,html4',
browse_button: 'selectfiles',
multi_selection: true,
// container: document.getElementById('container'),
    flash_swf_url: './../assets/plupload-2.1.2/js/Moxie.swf',
silverlight_xap_url: './../assets/plupload-2.1.2/js/Moxie.xap',
url: host,
init: {
PostInit: function () {
console.log(7777)
},
QueueChanged: function (up) {// 数组变化是发生
      console.log(up)
},
FileFiltered: function (up,files) {
var fileSize = (Math.round(files.size * 100 / (1024 * 1024))/ 100).toString()// MB
      if (fileSize > 10) {
uploader.removeFile(files)
}
},
// 图片成功添加到上传队列中后的事件
     FilesAdded: function (up,files) {
console.log(111)
plupload.each(files,function (v,i) {
})
console.log(files)
set_upload_param(uploader,'',false)
},
BeforeUpload: function (up,file) {
console.log(3333)
check_object_radio()
get_dirname()
set_upload_param(up,file.name,true)
},
UploadProgress: function (up,file) {
console.log(file.percent)// 进度条设置
     },
FileUploaded: function (up,file,info) {
console.log(555)
// 获取富文本组件实例
      console.log(that.$refs)
let quill = that.$refs.myQuillEditor.quill
if (info.status === 200) {
console.log(get_uploaded_object_name(file.name))
let url = host+'/'+g_dirname+get_uploaded_object_name(file.name)
// 获取光标所在位置
       let length = quill.getSelection().index;
// 插入图片 res.info为服务器返回的图片地址
       quill.insertEmbed(length,'image',url)
// 调整光标到最后
       quill.setSelection(length + 1)
}else {
this.$message.error('图片插入失败')
}
},
Error: function (up,err) {
// document.getElementById('console').appendChild(document.createTextNode("\nError xml:" + err.response))
     }
}
})
uploader.init()
},
destroyed () {
}
}

总结

以上所述是小编给大家介绍的vue-quill-editor+plupload富文本编辑器实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
JavaScript 无符号右移运算符
Apr 17 Javascript
jQuery学习4 浏览器的事件模型
Feb 07 Javascript
JavaScript高级程序设计 阅读笔记(二十一) JavaScript中的XML
Sep 14 Javascript
缓动函数requestAnimationFrame 更好的实现浏览器经动画
Dec 07 Javascript
javascript省市区三级联动下拉框菜单实例演示
Nov 29 Javascript
详解Bootstrap的aria-label和aria-labelledby应用
Jan 04 Javascript
原生js实现轮播图
Feb 27 Javascript
ES6中Proxy代理用法实例浅析
Apr 06 Javascript
vue给input file绑定函数获取当前上传的对象完美实现方法
Dec 15 Javascript
Windows下Node爬虫神器Puppeteer安装记
Jan 09 Javascript
浅析JavaScript 函数防抖和节流
Jul 13 Javascript
vue如何使用rem适配
Feb 06 Vue.js
vue+VeeValidate 校验范围实例详解(部分校验,全部校验)
Oct 19 #Javascript
浅析JS中什么是自定义react数据验证组件
Oct 19 #Javascript
在小程序Canvas中使用measureText的方法示例
Oct 19 #Javascript
webstorm中配置Eslint的两种方式及差异比较详解
Oct 19 #Javascript
clipboard在vue中的使用的方法示例
Oct 19 #Javascript
详解在vue-cli项目下简单使用mockjs模拟数据
Oct 19 #Javascript
ES6 系列之 Generator 的自动执行的方法示例
Oct 19 #Javascript
You might like
PHP VS ASP
2006/10/09 PHP
利用 window_onload 实现select默认选择
2006/10/09 PHP
php 常用算法和时间复杂度
2013/07/01 PHP
汇总PHPmailer群发Gmail的常见问题
2016/02/24 PHP
自动完成JS类(纯JS, Ajax模式)
2009/03/12 Javascript
JavaScript 对Cookie 操作的封装小结
2009/12/31 Javascript
JavaScript对象之间的转换 jQuery对象和原声DOM
2011/03/07 Javascript
jQuery实现id模糊查询的小例子
2013/03/19 Javascript
jquery属性选择器not has怎么写 行悬停高亮显示
2013/11/13 Javascript
js学习阶段总结(必看篇)
2016/06/16 Javascript
jQuery遍历DOM的父级元素、子级元素和同级元素的方法总结
2016/07/07 Javascript
js中利用cookie实现记住密码功能
2020/08/20 Javascript
jquery Ajax 全局调用封装实例详解
2017/01/16 Javascript
详解js的作用域、预解析机制
2018/02/05 Javascript
记一次webpack3升级webpack4的踩坑经历
2018/06/12 Javascript
vue操作动画的记录animate.css实例代码
2019/04/26 Javascript
详解 微信小程序开发框架(MINA)
2019/05/17 Javascript
JS实现压缩上传图片base64长度功能
2019/12/03 Javascript
vue跳转方式(打开新页面)及传参操作示例
2020/01/26 Javascript
vue-router懒加载的3种方式汇总
2021/02/28 Vue.js
python查看微信好友是否删除自己
2016/12/19 Python
浅谈Python NLP入门教程
2017/12/25 Python
Python Pandas找到缺失值的位置方法
2018/04/12 Python
Diango + uwsgi + nginx项目部署的全过程(可外网访问)
2018/04/22 Python
元组列表字典(莫烦python基础)
2019/04/03 Python
python 实现识别图片上的数字
2019/07/30 Python
Python实现屏幕录制功能的代码
2020/03/02 Python
序列化Python对象的方法
2020/08/01 Python
Python Matplotlib绘图基础知识代码解析
2020/08/31 Python
CSS书写规范、顺序和命名规则
2014/03/06 HTML / CSS
Vans荷兰官方网站:美国南加州的原创极限运动潮牌
2018/01/23 全球购物
淘宝店策划方案
2014/06/07 职场文书
通知的写法
2015/04/23 职场文书
Python3接口性能测试实例代码
2021/06/20 Python
Window server中安装Redis的超详细教程
2021/11/17 Redis
MySQL中B树索引和B+树索引的区别详解
2022/03/03 MySQL