当前位置:网站首页>vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证
vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证
2020-11-06 20:42:00 【叫我詹躲躲】
昨天实现了一些codemirror:基本的编辑代码功能、插入变量功能、codemirror语法验证功能、代码格式化(由于主要是json数据,所以暂时没引用其他组件,如果需要建议引用js-beautify,因为查资料发现codemirror在新的版本中已经去掉autoFormatRange方法),如果想了解更多,请参考《教你轻松搞懂vue-codemirror的基本用法:主要实现代码编辑、验证提示、代码格式化》,今天接着昨天的功能代码继续,主要分享vue-codemirror实现搜索功能、代码折叠功能、获取编辑器值及时验证功能。
1. vue-codemirror 搜索功能
1.1 在原来的基础上需要引入需要的资源
import 'codemirror/addon/scroll/annotatescrollbar.js'
import 'codemirror/addon/search/matchesonscrollbar.js'
import 'codemirror/addon/search/match-highlighter.js'
import 'codemirror/addon/search/jump-to-line.js'
import 'codemirror/addon/dialog/dialog.js'
import 'codemirror/addon/dialog/dialog.css'
import 'codemirror/addon/search/searchcursor.js'
import 'codemirror/addon/search/search.js'
其实引入这些资源基本的搜索功能已经实现,按ctrl+F 或者command+F就编辑器顶部就可以出现搜索框。

1.2 点击按钮实现搜索效果
产品考虑到有的人可能不知道用快捷键,就设计了一个按钮,点击进行搜索,点击搜索按钮,搜索框出现在编辑器的顶部。searchCode就是对应的点击按钮事件时候调用的方法。
methods: {
searchCode (e) {
this.codemirror.execCommand('find') //触发
}
}
2. vue-codemirror折叠功能
折叠功能刚开始做这个编辑器的时候就想加上,但是试了一下没成功,因为配置比较多,还需要引入资源,所以这个放到最后才加。
2.1 vue-codemirror折叠需要引入的资源,既有样式文件又有js文件,这些都是不可少的
// 折叠
import 'codemirror/addon/fold/foldgutter.css'
import 'codemirror/addon/fold/foldcode'
import 'codemirror/addon/fold/foldgutter'
import 'codemirror/addon/fold/brace-fold'
import 'codemirror/addon/fold/comment-fold'
2.2 vue-codemirror折叠需要的基本配置
{
foldGutter: true,
lineWrapping: true,
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter', 'CodeMirror-lint-markers'],
}
vue-codemirror折叠不需要扩展什么功能,只需要引入需要资源和配置好初始化的配置参数即可。
获取编辑器值及时验证功能
其实这个功能就是在vue-codemirror代码编辑器值出现语法错误的时候,及时按钮变成不可点击状态,当值没有啥错误的时候,提交按钮为可点击状态,没找到更好的获取编辑器验证状态的值,只能自己想了一个小捷径,判断是否有错误的代码,用watch监听数据,当数据发生变化的时候,看是否有错误存在。实现代码如下:
watch: {
jsonData (val) {
this.checkValid()
}
},
methods: {
checkValid () {
setTimeout(() => {
this.codemirror.refresh()
const container = this.$refs.codeMirror
const len = container.querySelectorAll('.CodeMirror-lint-marker-error').length
this.isValid = !!len && len > 0
}, 1000)
}
}
效果如下图:

总结:
今天主要是接着《教你轻松搞懂vue-codemirror的基本用法:主要实现代码编辑、验证提示、代码格式化》文章继续分享vue-codemirror的一些用法,主要实现了搜索、代码折叠等功能。这仅仅代表我个人的观点,如有错误,欢迎指正。
关注lovepythoncn
**
回复关键字:code2, 就可以获取源码地址
版权声明
本文为[叫我詹躲躲]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3995971/blog/4558936
边栏推荐
- 钻石标准--Diamond Standard
- Face to face Manual Chapter 16: explanation and implementation of fair lock of code peasant association lock and reentrantlock
- 关于Kubernetes 与 OAM 构建统一、标准化的应用管理平台知识!(附网盘链接)
- How long does it take you to work out an object-oriented programming interview question from Ali school?
- 制造和新的自动化技术是什么?
- ES6学习笔记(五):轻松了解ES6的内置扩展对象
- 2019年的一个小目标,成为csdn的博客专家,纪念一下
- The choice of enterprise database is usually decided by the system architect - the newstack
- 直播预告 | 微服务架构学习系列直播第三期
- 10 easy to use automated testing tools
猜你喜欢

教你轻松搞懂vue-codemirror的基本用法:主要实现代码编辑、验证提示、代码格式化

TRON智能钱包PHP开发包【零TRX归集】

Existence judgment in structured data

Architecture article collection

Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things

IPFS/Filecoin合法性:保护个人隐私不被泄露

人工智能学什么课程?它将替代人类工作?

Want to do read-write separation, give you some small experience

I've been rejected by the product manager. Why don't you know

前端都应懂的入门基础-github基础
随机推荐
Technical director, to just graduated programmers a word - do a good job in small things, can achieve great things
制造和新的自动化技术是什么?
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
Serilog原始碼解析——使用方法
How to demote a domain controller in Windows Server 2012 and later
至联云分享:IPFS/Filecoin值不值得投资?
至联云解析:IPFS/Filecoin挖矿为什么这么难?
2019年的一个小目标,成为csdn的博客专家,纪念一下
Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的
This article will introduce you to jest unit test
Not long after graduation, he earned 20000 yuan from private work!
在大规模 Kubernetes 集群上实现高 SLO 的方法
IPFS/Filecoin合法性:保护个人隐私不被泄露
It's so embarrassing, fans broke ten thousand, used for a year!
Python + appium automatic operation wechat is enough
html
Use of vuepress
What is the side effect free method? How to name it? - Mario
從小公司進入大廠,我都做對了哪些事?
ES6 essence: