froala富文本编辑器

最近由于产品的要求,替换掉老系统的vue ueditor富文本编辑器,换成froala。 如果是vue的话

npm install vue-froala-wysiwyg --save
import VueFroala from 'vue-froala-wysiwyg';

接着去入口文件 main.js引入依赖。

//Import Froala Editor
import 'froala-editor/js/plugins.pkgd.min.js';
//Import third party plugins
import 'froala-editor/js/third_party/embedly.min';
import 'froala-editor/js/third_party/font_awesome.min';
import 'froala-editor/js/third_party/spell_checker.min';
import 'froala-editor/js/third_party/image_tui.min';
// Import Froala Editor css files.
import 'froala-editor/css/froala_editor.pkgd.min.css';
import 'froala-editor/js/languages/zh_cn';
// Import and use Vue Froala lib.
import VueFroala from 'vue-froala-wysiwyg';Vue.use(VueFroala)

自定义功能列表

data() {var _this = this;return {config: {editor: null,language: 'zh_cn',//语言height: 325,quickInsertEnabled: false,//便捷插入标签charCounterCount: false, //富文本的输入长度toolbarButtons: ['align','bold','paragraphFormat','outdent','indent','insertLink',], //自定义toolbarButtons,功能菜单imageEditButtons: ['imageReplace','imageAlign','imageCaption','imageRemove',],//富文本对图片对操作imageUploadParam: 'upfile', //上传图片的字段imageUploadURL:'http://localhost:3005/excel',//请求地址imageUploadMethod: 'POST',// Allow to upload PNG and JPG.imageAllowedTypes: ['jpeg', 'jpg', 'png'],//接受图片的类型placeholderText: '请输入内容',// Set max file size to 20MB.fileMaxSize: 20 * 1024 * 1024, //限制图片大小toolbarSticky: false,// Allow to upload any file.},};},

上面这些是基本配置,但是我不建议大家用insertImage这个自带的图片上传功能,所以在上面的 toolbarButtons: [ 'align', 'bold', 'paragraphFormat', 'outdent', 'indent', 'insertLink' ],被我去掉了。

理由如下:
1.froala要求的图片传到后台后的返回格式:{"link": "path/to/image.jpg"}

2.自定义的上传,适用性更广。

用plupload替代默认上传

下载一个icon,把这个icon绝对定位到富文本编辑器中

<i class="el-icon-picture-outline" ref="uploadBtn"></i>

然后去去下载 plupload

npm install plupload --save

然后在组件中引入

import plupload from 'plupload';

然后开始初始化 plupload 的方法和触发的dom。

    //初始化上传initPlupload() {const uploader = new plupload.Uploader({browse_button: this.$refs.uploadBtn, // 触发按钮multi_selection: false,file_data_name: 'upfile',  //定义文件参数filters: this.setFilters(),});uploader.init();// 当文件添加到上传队列后触发uploader.bind('FilesAdded', (uploader, files) => {for (let index = 0; index < files.length; index++) {const file = files[index];if (!this.checkFileName(file)) {this.$message.error(file.name + '文件名不合法');uploader.removeFile(file);}}// 开始上传uploader.start();});// 当队列中的某一个文件正要开始上传前触发uploader.bind('BeforeUpload', async (uploader, file) => {uploader.setOption(this.setConfig(file));});// 当队列中的某一个文件上传完成后触发uploader.bind('FileUploaded', async (uploader, file, info) => {console.log(info, 'info==');const infoParse = info.response && JSON.parse(info.response);console.log(infoParse, 'infoParseinfoParse');// const doMain = process.env.REACT_APP_OSS_HOST;this.insertHtml('<img src=' + infoParse.url + ' />');});return uploader;},// 设置上传配置setConfig(file) {return {url:'http://xxxxxxxxxxxxxxxxxxx',//上传接口地址multipart_params: {//其余参数配置},};},

初始化了 plupload 上传方法之后,并继续对富文本图片的插入做操作。

    //插入方法insertHtml(content) {//设置编辑器获取焦点this.editor.events.focus();// 获取选定对象const selection = getSelection();// 判断是否有最后光标对象存在if (this.lastEditRange) {console.log('jinru=========进入');// 存在最后光标对象,选定对象清除所有光标并添加最后光标还原之前的状态selection.removeAllRanges();selection.addRange(this.lastEditRange);}//插入内容this.editor.html.insert(content);//记录最后光标停留位置this.lastEditRange = selection.getRangeAt(0);},

还有一步很重要的,对富文本对事件触发,进行操作,写在data里面的config

    events: {//初始化加载initialized: (e, editor) => {console.log(this.$refs.editS);this.editor = this.$refs.editS.getEditor();// this.editor = this.$refs.editS.getEditor();},//添加事件,在每次按键按下时,都记录一下最后停留位置keyup: (e, editor) => {const sel = window.getSelection && window.getSelection();if (sel && sel.rangeCount > 0) {_this.lastEditRange = sel.getRangeAt(0);}},//从别的地方复制进富文本时候的操作,包括图片等'paste.after': async function () {//这里面可以用正则,获取复制到的img里面的 src 去替换,然后再写方法,去替换掉里面的外链图片,  上传到自己的服务器。并且插入到富文本里面中},}

完整的代码我贴出来了

<template><div><div class="uploadImage"><froalaid="edit":tag="'textarea'":config="config"v-model="model"ref="editS"></froala><i class="el-icon-picture-outline" ref="uploadBtn"></i></div></div>
</template><script>
import plupload from 'plupload';
import VueFroala from 'vue-froala-wysiwyg';
const imgReg = /<img.*?(?:>|\/>)/gi;
//匹配src属性
const srcReg = /\ssrc=[\'\"]?([^\'\"]*)[\'\"]?/i;
const http = /(http|https):\/\/([\w.]+\/?)\S*/;
export default {components: {},props: {content: {},},data() {var _this = this;return {config: {editor: null,lastEditRange: null,body: null,language: 'zh_cn',height: 325,quickInsertEnabled: false,charCounterCount: false,//自定义toolbarButtons,功能菜单toolbarButtons: ['align','bold','paragraphFormat','outdent','indent','insertLink',],imageEditButtons: ['imageReplace','imageAlign','imageCaption','imageRemove',],imageUploadMethod: 'POST',// Allow to upload PNG and JPG.imageAllowedTypes: ['jpeg', 'jpg', 'png'],placeholderText: '请输入内容',quickInsertEnabled: false,// Set max file size to 20MB.fileMaxSize: 20 * 1024 * 1024,toolbarSticky: false,// Allow to upload any file.fileAllowedTypes: ['*'],events: {initialized: (e, editor) => {console.log(this.$refs.editS);this.editor = this.$refs.editS.getEditor();// this.editor = this.$refs.editS.getEditor();},//添加事件,在每次按键按下时,都记录一下最后停留位置keyup: (e, editor) => {const sel = window.getSelection && window.getSelection();if (sel && sel.rangeCount > 0) {_this.lastEditRange = sel.getRangeAt(0);}},//复制触发事件'paste.after': async function () {//newContent,把替换后的newContent 插入到富文本中this.insertHtml && this.insertHtml(newContent);},},},};},computed: {model: {get() {return this.content;},set(val) {//触发更新文本数据this.$emit('update:content', val);},},},watch: {},created() {},mounted() {this.initPlupload();},methods: {setFilters() {return {prevent_duplicates: false, // 允许选取重复文件 默认为falsemax_file_size: 1024 * 1024 * 8,mime_types: [// 只允许上传的类型{ title: 'OSSUploadFiles', extensions: 'jpg,png,jpeg,gif' },],};},// 设置上传配置setConfig(file) {// const name = setFileName(file.name);return {url:'/uploadimage',multipart_params: {},};},//初始化上传initPlupload() {const uploader = new plupload.Uploader({browse_button: this.$refs.uploadBtn, // 触发按钮multi_selection: false,file_data_name: 'upfile',filters: this.setFilters(),});uploader.init();// 当文件添加到上传队列后触发uploader.bind('FilesAdded', (uploader, files) => {for (let index = 0; index < files.length; index++) {const file = files[index];if (!this.checkFileName(file)) {this.$message.error(file.name + '文件名不合法');uploader.removeFile(file);}}// 开始上传uploader.start();});// 当队列中的某一个文件正要开始上传前触发uploader.bind('BeforeUpload', async (uploader, file) => {uploader.setOption(this.setConfig(file));});// 当队列中的某一个文件上传完成后触发uploader.bind('FileUploaded', async (uploader, file, info) => {const infoParse = info.response && JSON.parse(info.response);this.insertHtml('<img src=' + infoParse.url + ' />');});return uploader;},// 验证文件名是否合法checkFileName(file) {if (/[%=+#&?\s]/g.test(file.name)) {return false;}return true;},initChange(val) {console.log(val, 55434343);},insertHtml(content) {//设置编辑器获取焦点this.editor.events.focus();// 获取选定对象const selection = getSelection();// 判断是否有最后光标对象存在if (this.lastEditRange) {console.log('jinru=========进入');// 存在最后光标对象,选定对象清除所有光标并添加最后光标还原之前的状态selection.removeAllRanges();selection.addRange(this.lastEditRange);}//插入内容this.editor.html.insert(content);//记录最后光标停留位置this.lastEditRange = selection.getRangeAt(0);},},
};
</script>
<style lang="less" scoped>
.fr-uploading {display: none;
}
.uploadImage {position: relative;/deep/ .el-icon-picture-outline {font-size: 23px;position: absolute;top: 13px;left: 268px;cursor: pointer;}/deep/ .el-icon-picture-outline:before {color: black;}
}
</style>

刚开始用的时候,这个自带的图片上传是真的坑爹,还要遵循他的规则返回数据。 所以遇到坑爹插件时,不想自己写的时候,可以想办法替代掉他原来掉的功能。

froala + vue + plupload替换自带上传功能相关推荐

  1. Vue 中实现 excel文件上传功能

    场景: 上传excel表,并将excel表中的数据构建成实体 <div class="pull-right" v-if="doc"><el-u ...

  2. php 拖拽 上传文件 进度,在Vue中如何实现带进度条的文件拖动上传功能

    这篇文章主要介绍了Vue实现带进度条的文件拖动上传功能,本文通过实例代码给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下 1. 基本界面 content="width=devic ...

  3. vue本地上传并预览php,vue.js 实现图片本地预览 裁剪 压缩 上传功能

    以下代码涉及 Vue 2.0 及 ES6 语法. 目标 纯 javascrpit 实现,兼容ie9及以上浏览器,在本地做好文件格式.长宽.大小的检测,减少浏览器交互. 现实是残酷的,为了兼容Ie9 还 ...

  4. element显示服务器的图片,Vue+ElementUI+SpringMVC实现图片上传和回显

    Vue+ElementUI+SpringMVC实现图片上传和table回显 而我们也常遇到表单中包含图片上传的需求,并且需要在table中显示图片,所以这里我就讲一下结合后端的SpringMVC框架如 ...

  5. Vue+ElementUI+SpringMVC实现图片上传和回显

    Vue+ElementUI+SpringMVC实现图片上传和table回显 在之前我们已经讲过了 Vue+ElementUI+SpringMVC实现分页 . 而我们也常遇到表单中包含图片上传的需求,并 ...

  6. vue上传文件到php,vue+axios+php如何实现上传文件功能?,formdata上传文件附加参数...

    vue+axios+php如何实现上传文件功能?Vue Axios PHP如何实现上传文件的功能?, 推荐:<PHP视频教程> 当我们提交表单时,我们经常会遇到一些表单提交要求.vue的a ...

  7. vue 文件及描述信息一起上传_用Vue实现一个大文件上传和断点续传

    前言 这段时间面试官都挺忙的,频频出现在博客文章标题,虽然我不是特别想蹭热度,但是实在想不到好的标题了-.-,蹭蹭就蹭蹭 :) 事实上我在面试的时候确实被问到了这个问题,而且是一道在线 coding ...

  8. 前后端分离跨服务器文件上传,SpringBoot+Vue.js实现前后端分离的文件上传功能

    这篇文章需要一定vue和springboot的知识,分为两个项目,一个是前端vue项目,一个是后端springboot项目. 后端项目搭建 我使用的是springboot1.5.10+jdk8+ide ...

  9. 功能强大的文件上传插件带上传进度-WebUploader

    WebUploader是由Baidu WebFE(FEX)团队开发的一个以HTML5/FLASH构建的现代文件上传组件.在现代的浏览器里面能充分发挥HTML5的优势,同时又不摒弃主流IE浏览器,沿用老 ...

  10. 使用vue+elementUi+springBoot实现图片上传,图片删除,图片展示

    使用vue+elementUi+springBoot实现图片上传,图片删除,图片展示 环境配置 准备环境 使用软件 图片上传 图片删除 图片显示 所有代码均为参考,每个人的方法不一样,效果不一样,该代 ...

最新文章

  1. Unity自定义角色控制器(一):碰撞检测
  2. 5.Excel日期时间函数类应用
  3. Java 8“失宠”
  4. shiro springboot 整合
  5. 穿山甲插屏广告居中_穿山甲跻身广告联盟头部阵营 如何实现增量创新?
  6. Linux中进程正常退出return和exit()的区别
  7. Java经典递归算法
  8. 【RT-Thread 作品秀】空气质量监测系统
  9. 表示偏移地址的寄存器
  10. 如何以管理员身份运行powerShell
  11. 华为S5700交换机通过命令开启snmp功能
  12. 服务器该不该选SSD硬盘储存?
  13. ZYNQ启动流程之分析BootRoM
  14. 黑苹果安装的驱动含义
  15. 支付宝手机网站支付实战踩坑
  16. 小白入门STM32(1)----手机蓝牙控制STM32单片机点亮LED
  17. 最新8款非常漂亮的免费英文字体
  18. 颜色聚合向量(CCV)
  19. 仪表图形怎么用c语言写,【单选题】仪表自控流程图中,在调节阀附近标注缩写字母“FO”表示该阀是()。 A. 风开阀 B. 风关阀 C. 故障开阀 D. 故障关阀...
  20. 解析USB PD3.1、USB4 V2.0标准下的USB-C线缆解决方案

热门文章

  1. 重定向和请求转发详解
  2. VMWARE 之 分布式网络交换机
  3. PTES Technical Guidelines
  4. JAVA数组的遍历 获取最大值最小值 数组的反转 数组的查找 练习 定义方法创建指定大小的数组,并添加指定元素 拼接两个数组
  5. MT8127_Android_scatter.txt 文件添加backup分区
  6. MATLAB符号运算
  7. MATLAB 符号运算
  8. QT界面 全屏显示 自适应显示 多屏显示
  9. HLW8032做220V电量采集方案测试
  10. 小米案例分析PPT模板