vue - codemirror 点击对应标签查找 结束标签 位置

本文介绍如何使用CodeMirror库监听文档中光标或选择的变化,并实现获取当前光标所在位置的标签信息。通过`cursorActivity`事件可以实时追踪代码编辑状态,适用于IDE或在线编辑器等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

template :

<codemirror :value="code" @cursorActivity="cursorActivity"></codemirror>

"cursorActivity" (doc: CodeMirror.Doc)  每当此文档中的光标或选择更改时触发。

import:(必须引入)

import CodeMirror from 'codemirror/lib/codemirror'

methods: 

    cursorActivity (cm) {
      let cur = cm.getCursor()
      this.getPositionTag(cm, cur)
    },
    getPositionTag (cm, cur) {
      let range = cm.getViewport()
      range.from = Math.min(range.from, cur.line)
      range.to = Math.max(cur.line + 1, range.to)
      this.CodeMirror = CodeMirror
      // 获取开始-结束标签位置
      let match = CodeMirror.findMatchingTag(cm, cur, range)
      if (match) {
        let {open, close} = match
        // 获取开始 结束标签的文本
        console.log(cm.getRange(open.from, close ? close.to : open.to))
      }
    }

 

 Console:

根据 最终match 给的 open 和 close 数据 进行定位

 

{ "name": "iview-admin", "version": "2.0.0", "author": "Lison<lison16new@163.com>", "private": false, "scripts": { "dev": "vue-cli-service serve --open --mode development", "dev:prod": "vue-cli-service serve --open --mode production", "build": "vue-cli-service build", "build:dev": "vue-cli-service build --mode development", "dev-to-oss": "node deploy.js dat", "build:dev-to-oss": "vue-cli-service build --mode development && node deploy.js dat", "build:prod": "vue-cli-service build --mode production", "prod-to-oss": "node deploy.js prod", "build:prod-to-oss": "vue-cli-service build --mode production && node deploy.js prod", "oss:all": "yarn build:dev-to-oss && yarn build:prod-to-oss", "lint": "vue-cli-service lint", "test:unit": "vue-cli-service test:unit", "test:e2e": "vue-cli-service test:e2e" }, "dependencies": { "ali-oss": "^6.17.1", "axios": "^0.18.0", "clipboard": "^2.0.0", "codemirror": "^5.38.0", "countup": "^1.8.2", "cropperjs": "^1.2.2", "dayjs": "^1.7.7", "echarts": "^4.0.4", "html2canvas": "^1.0.0-alpha.12", "iview": "^3.2.2", "iview-area": "^1.5.17", "js-cookie": "^2.2.0", "simplemde": "^1.11.2", "sortablejs": "^1.7.0", "tree-table-vue": "^1.1.0", "v-org-tree": "^1.0.6", "vue": "^2.5.10", "vue-i18n": "^7.8.0", "vue-router": "^3.0.1", "vuedraggable": "^2.16.0", "vuex": "^3.0.1", "wangeditor": "^4.7.15", "xlsx": "^0.13.3" }, "devDependencies": { "@vue/cli-plugin-babel": "^3.0.1", "@vue/cli-plugin-eslint": "^3.0.1", "@vue/cli-plugin-unit-mocha": "^3.0.1", "@vue/cli-service": "^3.0.1", "@vue/eslint-config-standard": "^3.0.0-beta.10", "@vue/test-utils": "^1.0.0-beta.10", "chai": "^4.1.2", "eslint-plugin-cypress": "^2.0.1", "less": "^2.7.3", "less-loader": "^4.0.5", "lint-staged": "^6.0.0", "mockjs": "^1.0.1-beta3", "qs": "^6.6.0", "vue-template-c
04-09
package.json文件的内容如下所示{ "name": "vue-admin-template", "version": "4.1.0", "description": "A vue admin template with Element UI & axios & iconfont & permission control & lint", "author": "Pan <panfree23@gmail.com>", "license": "MIT", "scripts": { "dev": "vue-cli-service serve", "build": "vue-cli-service build", "analyzer": "set analyzer=true && vue-cli-service build --mode test", "lint": "eslint --ext .js,.vue src", "svgo": "svgo -f src/assets/icons/svg --config=src/assets/icons/svgo.yml" }, "dependencies": { "@form-create/element-ui": "^1.0.3", "@lukeed/uuid": "^2.0.0", "@riophae/vue-treeselect": "^0.4.0", "@vue/compiler-core": "^3.0.7", "ali-oss": "^6.1.1", "axios": "^0.19.0", "babel-polyfill": "6.26.0", "cheerio": "1.0.0-rc.5", "clipboard-polyfill": "^3.0.1", "codemirror": "^5.49.2", "crypto-js": "^3.1.9-1", "dayjs": "^1.8.15", "dompurify": "^3.1.2", "element-resize-detector": "^1.2.2", "element-ui": "2.15.6", "file-saver": "^2.0.2", "highlight.js": "^9.15.10", "html2canvas": "1.4.1", "js-base64": "^2.5.1", "js-beautify": "^1.13.0", "js-cookie": "^2.2.1", "jszip": "^3.2.2", "lodash": "^4.17.15", "moment": "^2.29.1", "node-sass": "^4.14.1", "normalize.css": "^8.0.1", "nprogress": "0.2.0", "path-to-regexp": "^3.0.0", "preload": "^0.1.0", "prettier": "^2.2.1", "screenfull": "^5.1.0", "sortablejs": "^1.10.0-rc2", "validator": "^11.1.0", "vue": "2.6.10", "vue-count-to": "^1.0.13", "vue-i18n": "^8.25.0", "vue-print-nb": "^1.7.5", "vue-router": "^3.3.1", "vuedraggable": "^2.24.3", "vuetify": "^2.5.8", "vuex": "^3.1.1", "vxe-table": "^2.11.0", "webpack-dev-server": "^2.9.1", "xe-utils": "^3.5.11", "xlsx": "^0.15.0" }, "devDependencies": { "@babel/core": "7.5.5", "@babel/register": "7.5.5", "@vue/babel-helper-vue-jsx-merge-props": "^1.0.0", "@vue/babel-preset-jsx": "^1.1.2", "@vue/cli-plugin-babel": "^3.10.0", "@vue/cli-plugin-eslint": "^3.10.0", "@vue/cli-plugin-unit-jest": "^3.10.0", "@vue/cli-service": "^3.10.0", "@vue/compiler-core": "^3.0.5", "@vue/test-utils": "1.0.0-beta.29", "babel-core": "7.0.0-bridge.0", "babel-eslint": "10.0.2", "babel-jest": "24.8.0", "babel-plugin-import": "1.12.2", "babel-plugin-jsx-v-model": "2.0.3", "chalk": "2.4.2", "compression-webpack-plugin": "^5.0.1", "connect": "3.6.6", "core-js": "^2.6.9", "eslint": "^5.16.0", "eslint-plugin-vue": "5.2.2", "html-webpack-plugin": "3.2.0", "lodash": "^4.17.20", "mockjs": "1.0.1-beta3", "plop": "^2.4.0", "prettier": "2.2.1", "runjs": "^4.3.2", "sass-loader": "^7.2.0", "script-ext-html-webpack-plugin": "^2.1.4", "script-loader": "^0.7.2", "serve-static": "^1.14.1", "svg-sprite-loader": "^4.1.6", "svgo": "^1.3.0", "uglifyjs-webpack-plugin": "^1.3.0", "vue-template-compiler": "2.6.10", "webpack-bundle-analyzer": "^4.8.0" }, "engines": { "node": ">=8.9", "npm": ">= 3.0.0" }, "browserslist": [ "> 1%", "last 2 versions", "not ie <= 8" ] } ,npm run serve后npm ERR! missing script: serve run `npm audit fix` to fix them, or `npm audit` for details
最新发布
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值