在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 ...
随机推荐
- STM32 I2C介绍和软件模拟I2C编程要点
I2C协议层独特特征: 1. 通过地址(Master/Slave Address)区分不同的设备. 2. ACK信号体制,即通过ACK表示是否进行继续传输. 3.由SCL.SDA的四种关系,映射数据有 ...
- mysql 根据父id查询下级所有数据
select id,apply_resource_name from ( select t1.id,t1.apply_resource_name, if(find_in_set(parent_id, ...
- Spring、SpringMVC的区别
Spring是IOC和AOP的容器框架,SpringMVC是基于Spring功能之上添加的Web框架,想用SpringMVC必须先依赖Spring.简单点的话可以将SpringMVC类比于Struts ...
- BackTrader 简单BTC的SMA15回测DEMO
import time import requests import json import csv from requests.packages.urllib3 import disable_war ...
- K8存储之ConfigMap、Secret
ConfigMap ConfigMap是一种API对象,用来将非加密数据保存到键值对中.可以用作环境变量.命令行参数或者存储卷中的配置文件. ConfigMap供容器使用的典型用法如下: 生成为容器内 ...
- wget 和 curl的区别
原文 https://www.codenong.com/s1190000022301195/ https://geek-docs.com/linux/linux-ask-answer/differen ...
- redis底层数据结构之简单动态字符串(SDS)
简单动态字符串(simple dynamic string,SDS) redis使用C语言编写的,但是redis的字符串却不是C语言中的字符串(以空字符'\0'结尾的字符数组),redis定义了一种简 ...
- word和excel转pdf
1.下载jacob.jar包 网址:https://sourceforge.net/projects/jacob-project/files/jacob-project/ 2.导入到本地仓库:mvn ...
- 题解 【POJ3728】The merchant(LCA)
题意:一棵树有N个城市,每个城市商品价格不一样,Q个询问,问从u出发到达v点,每个城市只能经过一次的最大利润 max min数组存u城到u的第2^i个祖先路径上的最值 答案就是u-v路径上的最大值-最 ...
- 用python从网页下载单词库
从网站下载单词库 1 每一页有几百个单词 2 每一个单词有独立的URL,URL中包含单词的中文解释 3 使用的库 requests,pyquery,web #coding:utf-8 import r ...