在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 ...
随机推荐
- Typopa软件和计算机的基本内容
Typopa软件和计算机的基本内容 TYPORA软件markdown的运用 输入#按空格键输入文本就形成了一个标题(几个#就是几级标题最高六级)或者ctrl加数字数字几就是几级标题. 输入*加空格就形 ...
- 【剑指Offer】【树】二叉搜索树的后序遍历序列
题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. A:在二叉树的后序遍历中,数组最后一个元素为根节点,左 ...
- vue项目使用vue-amap调用高德地图api详细步骤
想要的效果如下 : 高德地图 && 信息窗体 步骤一: 申请高德key 高德开放平台 | 高德地图API (amap.com) (可参考博客: [996]如何申请高德地图用户Key ...
- js实现禁止浏览器后退
试了网上不少的js禁止浏览器后退的代码,发现只有下面的一种效果还是可以的. <script language="javascript"> history.pushSta ...
- js - 数字转中文
js - 数字转中文 JavaScript 中将阿拉伯数字转换为中文 转换代码 var _change = { ary0: ['零', '一', '二', '三', '四', '五', '六', '七 ...
- python_异常处理(try except)
1,异常捕获 异常捕获的字段为python解释报错的最后一行的第一个单词.使用try方法,程序报错时,可以使用except方法匹配报错的异常关键字,继续except下方定义的代码,从而保证代码可以正常 ...
- 文件下载,后端接口,django,flask
Django后端接口 def download_excel(request): """ get excel file from url param and upload ...
- 2022-6,flask+vue+uwsgi+nginx,线上部署完整流程打包配置文件
uwsgi配置文件 [uwsgi] # 服务端口号,这里没有设置IP值,默认是加载服务器的IP地址 http = :8000 # flask项目地址 chdir = /home/flask_proje ...
- springboot+Elasticsearch 复杂查询
以前没做过ES 里面的查询,第一次接触还是走了点弯路的. 就是这个字段你在ES 都不用模糊查的话,就可以设置 type = FieldType.Keyword,比如ID之类的. 一:建ES存储的实体 ...
- webpack5 与webpack4 之间差别
1.terserPlugin webpack4 上需要下载安装 terser-webpack-plugin 插件,并且需要配置,webpack5 上生产模式下默认开启压缩,开发环境也可以按如下配置 / ...