当前位置:网站首页>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 .
![image.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4ubmxhcmsuY29tL3l1cXVlLzAvMjAyMC9wbmcvMTQ4MTQ2LzE1ODk1MzkzNjUyMzYtMmJjZjM3ZGEtMWVkZi00MjE3LWE2NjMtY2U5MGE3YzEyYTM1LnBuZw?x-oss-process=image/format,png#align=left&display=inline&height=520&margin=[object Object]&name=image.png&originHeight=1040&originWidth=1280&size=98603&status=done&style=none&width=640)
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 :
![image.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4ubmxhcmsuY29tL3l1cXVlLzAvMjAyMC9wbmcvMTQ4MTQ2LzE1ODk1NDA1NDMzMTItNDZjMjBiZDgtMTcwMC00OTJkLWFjMTUtYTBiMzI0ZGQ3NmY1LnBuZw?x-oss-process=image/format,png#align=left&display=inline&height=1038&margin=[object Object]&name=image.png&originHeight=1038&originWidth=1266&size=100464&status=done&style=none&width=1266)
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]所创,转载请带上原文链接,感谢
边栏推荐
- Network programming NiO: Bio and NiO
- 嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
- The importance of big data application is reflected in all aspects
- Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
- StickEngine-架构12-通信协议
- Small program introduction to proficient (2): understand the four important files of small program development
- Use modelarts quickly, zero base white can also play AI!
- 一部完整的游戏,需要制作哪些音乐?
- Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
- Basic usage of GDB debugging
猜你喜欢
Behind the first lane level navigation in the industry
How to understand Python iterators and generators?
事务的本质和死锁的原理
Ronglian completed US $125 million f round financing
快速排序为什么这么快?
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
A brief history of neural networks
Isn't data product just a report? absolutely wrong! There are university questions in this category
美团内部讲座|周烜:华东师范大学的数据库系统研究
ado.net和asp.net的关系
随机推荐
What are PLC Analog input and digital input
(2) ASP.NET Core3.1 Ocelot routing
How to get started with new HTML5 (2)
Network programming NiO: Bio and NiO
【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
Multi robot market share solution
What knowledge do Python automated testing learn?
Individual annual work summary and 2019 work plan (Internet)
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
CCR coin frying robot: the boss of bitcoin digital currency, what you have to know
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
前端未來趨勢之原生API:Web Components
Introduction to X Window System
Custom function form of pychar shortcut key
hdu3974 Assign the task線段樹 dfs序
华为云微认证考试简介
Installing ns-3 on ubuntu18.04
Jmeter——ForEach Controller&Loop Controller