在Vue项目中使用wangEditor
封装的Editor组件
<template>
<div class="editor-wrap">
<div ref="toolbar" class="toolbar"/>
<div ref="editor" class="editor-com" style="text-align:left">
<input
ref="placeHolder"
:style="{border: 'none', background: '#fff', outline: 'none', width: '100%', color: '#ccc', margin: '10px 0 4px 4px'}"
:placeholder="defaultText"
class="default-text"
type="text">
</div>
</div>
</template>
<script>
import E from 'wangeditor'
export default {
name: 'Editor',
props: {
editorContent: {
type: String,
default: '',
update: true
},
// 菜单属性
menus: {
type: Array,
default: function() {
return [
'head', // 标题
'bold', // 粗体
'fontSize', // 字号
'fontName', // 字体
'italic', // 斜体
'underline', // 下划线
'strikeThrough', // 删除线
'foreColor', // 文字颜色
'backColor', // 背景颜色
'link', // 插入链接
'list', // 列表
'justify', // 对齐方式
'quote', // 引用
'emoticon', // 表情
'image', // 插入图片
'table', // 表格
'video', // 插入视频
'code', // 插入代码
'undo', // 撤销
'redo' // 重复
]
},
update: true
},
// 默认展示文字
defaultText: {
type: String,
default: ''
}
},
data() {
return {
editor: null,
// 第一次加载默认加载初始文字
startEditor: true
}
},
computed: {},
watch: {
editorContent(newval, oldVal) {
if (!this.change) {
this.editor.txt.html(newval)
}
this.change = false
}
},
mounted() {
this.editor = new E(this.$refs.toolbar, this.$refs.editor)
this.editor.customConfig.menus = this.menus
this.editor.customConfig.placeholder = this.defaultText
this.editor.customConfig.onfocus = () => {
this.$refs.placeHolder.style.display = 'none'
}
this.editor.customConfig.onchange = () => {
this.change = true
const html = this.editor.txt.html()
const text = this.editor.txt.text()
const obj = {
html,
text
}
// 向上触发editor变化
this.$emit('change', obj)
}
// 创建editor
this.editor.create()
if (this.editorContent.length) {
this.$refs.placeHolder.style.display = 'none'
this.editor.txt.html(this.editorContent)
}
},
methods: {}
}
</script>
源码修改(用于取消聚焦功能)
// filename ===> wangEditor.js
// 初始化选区,将光标定位到内容尾部
initSelection: function initSelection(newLine) {
var $textElem = this.$textElem;
var $children = $textElem.children();
if (!$children.length) {
// 如果编辑器区域无内容,添加一个空行,重新设置选区
$textElem.append($('<p><br></p>'));
this.initSelection();
return;
}
var $last = $children.last();
if (newLine) {
// 新增一个空行
var html = $last.html().toLowerCase();
var nodeName = $last.getNodeName();
if (html !== '<br>' && html !== '<br\/>' || nodeName !== 'P') {
// 最后一个元素不是 <p><br></p>,添加一个空行,重新设置选区
$textElem.append($('<p><br></p>'));
this.initSelection();
return;
}
}
// this.selection.createRangeByElem($last, false, true);
// this.selection.restoreSelection();
},
// wangEditor.min.js 中删除下面这行(用于打包时)
this.selection.restoreSelection()
上面注释掉这两行后会发现wangEditor第一次输入内容会出现光标总是出现在某个位置比如文本开头,这是因为我们在Editor组件中`watch` 的方法对于将父组件的处理后的值回显的问题,如下
// 这是上面那种,只有editor触发的文本改变才能回显,这样就不可以将修改过的文本回显到editor中
// 比如过滤重复后的文本
watch: {
editorContent(newval, oldVal) {
if (!this.change) {
this.editor.txt.html(newval)
}
this.change = false
}
}
// 下面这种方法可以将修改过的回显不过如果需要取消自动聚焦的话,会出现一个bug,光标会出现在开头
// 这是因为文本被重新写了,但是我们注释了光标自动聚焦到末尾
// --- 可以通过组件初始化成功的时候聚焦到一个隐藏的button上
watch: {
editorContent(newval, oldVal) {
const html = this.editor.txt.html()
if (!oldVal || (html !== newval)) {
this.editor.txt.html(newval)
}
}
}
以上组件演示了在vue中使用 `wangEditor` 的时候添加 `placeholder` 的效果和如何取消wangEditor打开组件自动聚焦的情况。
在Vue项目中使用wangEditor的更多相关文章
- 关于在vue项目中使用wangEditor
1,vue中安装wangEditor 使用的npm安装 npm install wangeditor --save 2,创建公用组件 在components中创建wangEnduit文件夹 组件内容为 ...
- vue 项目中实用的小技巧
# 在Vue 项目中引入Bootstrap 有时在vue项目中会根据需求引入Bootstrap,而Bootstrap又是依赖于jQuery的,在使用npm按照时,可能会出现一系列的错误 1.安装jQu ...
- 如何在VUE项目中添加ESLint
如何在VUE项目中添加ESLint 1. 首先在项目的根目录下 新建 .eslintrc.js文件,其配置规则可以如下:(自己小整理了一份),所有的代码如下: // https://eslint.or ...
- 在vue项目中, mock数据
1. 在根目录下创建 test 目录, 用来存放模拟的 json 数据, 在 test 目录下创建模拟的数据 data.json 文件 2.在build目录下的 dev-server.js的文件作如下 ...
- 浅谈 Axios 在 Vue 项目中的使用
介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 特性 它主要有如下特性: 浏览器端发起XMLHttpRequests请求 Node端发起http ...
- 去除vue项目中的#及其ie9兼容性
一.如何去除vue项目中访问地址的# vue2中在路由配置中添加mode(vue-cli创建的项目在src/router/index.js) export default new Router({ m ...
- vue 项目中当访问路由不存在的时候默认访问404页面
前言: 在Vue项目中,当访问的页面路由不存在或错误时,页面显示为一片空白.然而,通常我们需要对访问url不存在或者错误的情况下添加默认的404页面,即not found页面. 一般的处理方法是: 在 ...
- vue项目中遇到的那些事。
前言 有好几天没更新文章了.这段实际忙着做了一个vue的项目,从 19 天前开始,到今天刚好 20 天,独立完成. 做vue项目做这个项目一方面能为工作做一些准备,一方面也精进一下技术. 技术栈:vu ...
- scss/less语法以及在vue项目中的使用(转载)
1.scss与less都是css的预处理器,首先我们的明白为什么要用scss与less,因为css只是一种标记语言,其中并没有函数变量之类的,所以当写复杂的样式时必然存在局限性,不灵活,而scss与l ...
- Vue项目中GraphQL入门学习与应用
1.GraphQL是什么,能干什么? 正如官网所说,GraphQL是一种用于API查询的语言.Facebook 的移动应用从 2012 年就开始使用 GraphQL.GraphQL 规范于 2015 ...
随机推荐
- 关于xtr的一些基础
Q&A 如何找到QSEQ方式的xtt呢? 我们可以去版本的代码中寻找,具体位置在:(modem_proc/rf/rftarget_denali/mtp/xtt/etc/QSEQ/) 在QSEQ ...
- Springboot 添加redis
在项目中常常会用到redis来缓存信息,下面就是如何在Springboot中添加redis 1:在pom.xml中添加依赖 2:配置redis 3:测试使用redis 1:在pom.xml中添加依赖, ...
- mysql中int、bigint、smallint 和 tinyint四种数据类型
最近在做数据库表设计的时候,对于多种数字的数据类型的选择存在很多顾虑,不是很清楚到底如何选择.总结一下int.bigint.smallint 和 tinyint四种数据类型. bigint:从 -2^ ...
- vs2019中添加rdlc的报表设计器
在Visual studio 2019中,不会默认安装rdlc的报表设计器,所以需要自行添加. 1. 打开VS2019, 找到扩展-->管理扩展 2. 在扩展管理中,点击"联机&quo ...
- golang 切片(slice)
1.切片的定义 切片(slice)是对数组一个连续片段的引用,所以切片是一个引用类型. 切片的使用与数组类似,遍历,访问切片元素等都一样.切片是长度是可以变化的,因此切片可以看做是一个动态数组. 一个 ...
- 清理Linux系统无效的或者损坏的包
参考:解决Linux的 [有1 个软件包没有被完全安装或卸载] 问题 ubuntu中卸载没有安装完全的软件包 Ubuntu安装.基本命令和常见故障处理 1. 1 apt-get insta ...
- 蓝牙mesh组网实践(配网方式的选择)
目录 本测试基于CH582m单片机,尝试进行简单的组网. 沁恒官方EVT中提供了两种配网方式--配网器配网和自配网. ①配网器配网:用一个设备(一块582开发板)作为配网器,也就是沁恒蓝牙mesh软件 ...
- dayjs取 本周、上周、本月、上月、本季度、上季度时间段
let dateTimes = [ { id: 1, name: '本周', start_time: dayjs().startOf('week').add(1, 'day').format('YYY ...
- kafka在阿里云上的配置
只需要改server.properties listeners=PLAINTEXT:// 内网的ip地址和9092端口advertised.listeners=PLAINTEXT://外网的ip的地 ...
- K8S-kubeadm集群安装
K8S-kubeadm集群安装 一.环境准备 1.服务器信息 2.系统初始(所有服务器) 2.1修改主机名 hostnamectl set-hostname <主机名> 2.2添加主机ho ...