当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- GUI engine evaluation index
- 【自学unity2d传奇游戏开发】地图编辑器
- How about small and medium-sized enterprises choose shared office?
- Basic usage of GDB debugging
- (2) ASP.NET Core3.1 Ocelot routing
- Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
- The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
- StickEngine-架构11-消息队列(MessageQueue)
- The method of realizing high SLO on large scale kubernetes cluster
- What are the common problems of DTU connection
猜你喜欢

JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m

StickEngine-架构11-消息队列(MessageQueue)

解决 WPF 绑定集合后数据变动界面却不更新的问题

What are the common problems of DTU connection

Live broadcast preview | micro service architecture Learning Series live broadcast phase 3

一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试

Markdown tricks

A brief history of neural networks

EOS founder BM: what's the difference between UE, UBI and URI?

Wow, elasticsearch multi field weight sorting can play like this
随机推荐
01. SSH Remote terminal and websocket of go language
MongoDB与SQL常用语法对应表
How to understand Python iterators and generators?
解决 WPF 绑定集合后数据变动界面却不更新的问题
Shh! Is this really good for asynchronous events?
Small program introduction to proficient (2): understand the four important files of small program development
The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:曾文旌
A small goal in 2019 to become a blog expert of CSDN
DC-1靶機
Installing ns-3 on ubuntu18.04
Git rebase is in trouble. What to do? Waiting line
Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
[C] (original) step by step teach you to customize the control element - 04, ProgressBar (progress bar)
(2) ASP.NET Core3.1 Ocelot routing
大道至简 html + js 实现最朴实的小游戏俄罗斯方块
The importance of big data application is reflected in all aspects
行为型模式之解释器模式
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3