当前位置:网站首页>Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time
Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time
2020-11-06 20:42:00 【Tell me Zhan to hide】
Yesterday, we realized some codemirror: Basic edit code function 、 Insert variable function 、codemirror Syntax verification function 、 Code formatting ( Because it's mainly json data , So there is no reference to other components for the time being , If necessary, suggest quoting js-beautify, Because we found that codemirror In the new version autoFormatRange Method ), If you want to know more , Please refer to 《 Teach you to understand easily vue-codemirror The basic usage of : The main implementation of code editing 、 Verification tips 、 Code formatting 》, Today continues with yesterday's function code , Mainly share vue-codemirror Realize search function 、 Code folding function 、 Get editor value and verify function in time .
1. vue-codemirror Search function
1.1 On the basis of the original need to introduce the required resources
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'
In fact, the basic search function of introducing these resources has been realized , Press ctrl+F perhaps command+F The search box appears at the top of the editor .

1.2 Click the button to achieve the search effect
The product takes into account that some people may not know how to use the shortcut key , I designed a button , Click to search , Click the search button , The search box appears at the top of the editor .searchCode It is the corresponding method called when the button is pressed .
methods: {
searchCode (e) {
this.codemirror.execCommand('find') // Trigger
}
}
2. vue-codemirror Folding function
The folding function just started to make this editor and wanted to add , But I tried and I didn't succeed , Because there are many configurations , We also need to introduce resources , So this one is put at the end .
2.1 vue-codemirror Collapse the resources that need to be introduced , There are both style files and js file , These are indispensable
// Fold
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 The basic configuration required for folding
{
foldGutter: true,
lineWrapping: true,
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter', 'CodeMirror-lint-markers'],
}
vue-codemirror Folding doesn't need to expand any function , Just introduce the configuration parameters that need resources and configure initialization .
Get editor value and verify function in time
In fact, this function is in vue-codemirror When the code editor value has a syntax error , In time, the button becomes non clickable , When there is nothing wrong with it , The submit button is clickable , No better value to get the editor validation state , Can only think of a small shortcut , Determine if there is an error code , use watch Monitor data , When the data changes , See if there's a mistake . The implementation code is as follows :
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)
}
}
The effect is as follows :

summary :
Today is mainly followed by 《 Teach you to understand easily vue-codemirror The basic usage of : The main implementation of code editing 、 Verification tips 、 Code formatting 》 The article continues to share vue-codemirror Some uses of , The main implementation of the search 、 Code folding and other functions . It's just my personal opinion , If there is a mistake , Welcome to correct .
Focus on lovepythoncn
**
Reply key :code2, You can get the source code address
版权声明
本文为[Tell me Zhan to hide]所创,转载请带上原文链接,感谢
边栏推荐
- Introduction to quantitative investment and Trading (Python introduction to financial analysis)
- 华为云微认证考试简介
- What course of artificial intelligence? Will it replace human work?
- Jmeter——ForEach Controller&Loop Controller
- 代码重构之法——方法重构分析
- html+vue.js 實現分頁可相容IE
- 大道至简 html + js 实现最朴实的小游戏俄罗斯方块
- hdu3974 Assign the task線段樹 dfs序
- Interpretation of Cocos creator source code: engine start and main loop
- es创建新的索引库并拷贝旧的索引库 实践亲测有效!
猜你喜欢

Swagger 3.0 brushes the screen every day. Does it really smell good?

给字节的学姐讲如何准备“系统设计面试”

Multi robot market share solution

Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform

Individual annual work summary and 2019 work plan (Internet)

Pn8162 20W PD fast charging chip, PD fast charging charger scheme

【:: 是什么语法?】

如何对数据库账号权限进行精细化管理?

【自学unity2d传奇游戏开发】地图编辑器

行为型模式之解释器模式
随机推荐
【学习】接口测试用例编写和测试关注点
CloudQuery V1.2.0 版本发布
A small goal in 2019 to become a blog expert of CSDN
electron 實現檔案下載管理器
StickEngine-架构12-通信协议
意外的元素..所需元素..
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
Uncle Bob: the software architecture is similar to a house. Object oriented is the structure of the house, and the water pipe is functional programming
A brief history of neural networks
Simple summary of front end modularization
開源一套極簡的前後端分離專案腳手架
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
Share with Lianyun: is IPFs / filecoin worth investing in?
游戏开发中的新手引导与事件管理系统
快速排序为什么这么快?
使用 Iceberg on Kubernetes 打造新一代雲原生資料湖
【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
Swagger 3.0 brushes the screen every day. Does it really smell good?
Installing ns-3 on ubuntu18.04