当前位置:网站首页>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
边栏推荐
- Computer TCP / IP interview 10 even asked, how many can you withstand?
- IPFS/Filecoin合法性:保护个人隐私不被泄露
- Save the file directly to Google drive and download it back ten times faster
- Examples of unconventional aggregation
- 前端都应懂的入门基础-github基础
- Not long after graduation, he earned 20000 yuan from private work!
- 中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律
- Deep understanding of common methods of JS array
- 100元扫货阿里云是怎样的体验?
- The choice of enterprise database is usually decided by the system architect - the newstack
猜你喜欢

(1) ASP.NET Introduction to core3.1 Ocelot

快快使用ModelArts,零基础小白也能玩转AI!

PHP应用对接Justswap专用开发包【JustSwap.PHP】

How do the general bottom buried points do?

如何将数据变成资产?吸引数据科学家

DTU连接经常遇到的问题有哪些

Thoughts on interview of Ali CCO project team

每个前端工程师都应该懂的前端性能优化总结:

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

Architecture article collection
随机推荐
Didi elasticsearch cluster cross version upgrade and platform reconfiguration
PHPSHE 短信插件说明
In order to save money, I learned PHP in one day!
DRF JWT authentication module and self customization
H5 makes its own video player (JS Part 2)
使用 Iceberg on Kubernetes 打造新一代云原生数据湖
一篇文章带你了解SVG 渐变知识
Skywalking series blog 2-skywalking using
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
What is the difference between data scientists and machine learning engineers? - kdnuggets
快快使用ModelArts,零基础小白也能玩转AI!
OPTIMIZER_ Trace details
ES6学习笔记(五):轻松了解ES6的内置扩展对象
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
In depth understanding of the construction of Intelligent Recommendation System
Save the file directly to Google drive and download it back ten times faster
I think it is necessary to write a general idempotent component
(1) ASP.NET Introduction to core3.1 Ocelot
数字城市响应相关国家政策大力发展数字孪生平台的建设
ipfs正舵者Filecoin落地正当时 FIL币价格破千来了