vue2项目使用codemirror插件实现代码编辑器功能

1、使用npm安装依赖

npm install --save codemirror

2、在页面中放入如下代码

<template><textarea ref="mycode" class="codesql" v-model="code" style="height:200px;width:600px;"></textarea>
</template><script>import "codemirror/theme/ambiance.css";import "codemirror/lib/codemirror.css";import "codemirror/addon/hint/show-hint.css";let CodeMirror = require("codemirror/lib/codemirror");require("codemirror/addon/edit/matchbrackets");require("codemirror/addon/selection/active-line");require("codemirror/mode/sql/sql");require("codemirror/addon/hint/show-hint");require("codemirror/addon/hint/sql-hint");export default {name: "codeMirror",data () {return {code: '//按Ctrl键进行代码提示'}},mounted () {debuggerlet mime = 'text/x-mariadb'//let theme = 'ambiance'//设置主题,不设置的会使用默认主题let editor = CodeMirror.fromTextArea(this.$refs.mycode, {mode: mime,//选择对应代码编辑器的语言,我这边选的是数据库,根据个人情况自行设置即可indentWithTabs: true,smartIndent: true,lineNumbers: true,matchBrackets: true,//theme: theme,// autofocus: true,extraKeys: {'Ctrl': 'autocomplete'},//自定义快捷键hintOptions: {//自定义提示选项tables: {users: ['name', 'score', 'birthDate'],countries: ['name', 'population', 'size']}}})//代码自动提示功能,记住使用cursorActivity事件不要使用change事件,这是一个坑,那样页面直接会卡死editor.on('cursorActivity', function () {editor.showHint()})}}
</script><style>.codesql {font-size: 11pt;font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif;}
</style>

vue2 ts 类装饰版本组件

<template><textarea ref="container" v-model="value"></textarea>
</template><script lang="ts">
import Vue from 'vue';
import { Component, Ref, Model, Emit, Watch, Prop } from 'vue-property-decorator';
import CodeMirror from 'codemirror';
import 'codemirror/lib/codemirror.css';
import 'codemirror/mode/sql/sql';
import 'codemirror/theme/neat.css';
import 'codemirror/addon/scroll/simplescrollbars.css';
import 'codemirror/addon/scroll/simplescrollbars';
import 'codemirror/addon/hint/show-hint';
import 'codemirror/addon/hint/show-hint.css';
import 'codemirror/addon/hint/sql-hint';
import 'codemirror/mode/meta';
import 'codemirror/addon/selection/active-line';@Component
export default class CodeEditor extends Vue {@Ref('container') container!: HTMLTextAreaElement;@Model('change') value!: string;@Prop(Array) list?: string[];@Prop(Number) ch?: number;@Prop(String) height?: string;mirror: CodeMirror.Editor = CodeMirror(this.container);constructor() {super();this.changeListener = this.changeListener.bind(this);}mounted() {this.mirror = CodeMirror.fromTextArea(this.container, {value: this.value,mode: 'text/x-sql',theme: 'neat',styleActiveLine: true,readOnly: false,smartIndent: true,scrollbarStyle: 'overlay',lineNumbers: true,// matchBrackets: true,autofocus: true,// autoMatchParens: true,// styleSelectedText: true,indentUnit: 4,extraKeys: { Ctrl: 'autocomplete' },showHint: true,autocorrect: true,autocapitalize: true,});this.mirror.setSize('heigth', this.height);this.mirror.on('change', this.changeListener);}private changeListener(editor: CodeMirror.Editor, change: CodeMirror.EditorChangeLinkedList) {this.handleContentChange(this.mirror.getValue());if (change.origin === '+input' || change.origin === '*compose') {setTimeout(() => {const cursor = editor.getCursor();const str = editor.getLine(cursor.line);if (this.list && this.list.length) {// eslint-disable-next-lineconst hint = () => {return {from: { ch: this.ch || str.lastIndexOf(' ') + 1, line: cursor.line },to: { ch: str.length, line: cursor.line },list: [...(this.list || ['allen'])],};};editor.showHint({completeSingle: false,hint,});} else {this.mirror.showHint({ completeSingle: false, hint: CodeMirror.hint.sql });}}, 20);}}@Watch('value')handleValueChange(newValue: string) {if (newValue !== this.mirror.getValue()) {if (!newValue) {this.mirror.setValue('');return;}const value = newValue.split(' ').filter((item) => !!item).join(' ');this.mirror.setValue(value);this.mirror.focus();this.mirror.setCursor(newValue.length);}}@Emit('change')handleContentChange(content: string) {return content;}beforeDestroy() {this.mirror.off('change', this.changeListener);}
}
</script><style lang="less">
.CodeMirror {line-height: 1.5;
}
</style>

3、话不多说,直接上图

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/247284.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境

安装篇&#xff1a; 一、安装Apache yum install httpd #根据提示&#xff0c;输入Y安装即可成功安装 /etc/init.d/httpd start#启动Apache 备注&#xff1a;Apache启动之后会提示错误&#xff1a; 正在启动 httpd:httpd: Could not reliably determine the servers fully qual…

前端有用的库

HTML awesome-html5 精选的HTML5资源精选清单 CSS tailwindcss 与Tailwind CSS相关的很棒的事情awesome-css-frameworks 很棒的CSS框架列表awesome-css-cn CSS 资源大全中文版&#xff0c;内容包括&#xff1a;CSS预处理器、框架、CSS结构、代码风格指南、命名习惯等等awesom…

计算机视觉牛人(转载)(最早在自动化所论坛上发现的)

paper毕竟是死的, 写paper的人才是活的. 那么我现在研究一下cv圈的格局, 按师承关系, 借鉴前人, 我总结a tree stucture of cv guys.David Marr----->Shimon Ullman (Weizmann) ----->Eric Grimson (MIT)----->Daniel Huttenlocher (Cornell)----->Pedro Felzenszw…

Java生鲜电商平台-促销系统的架构设计与源码解析

Java生鲜电商平台-促销系统的架构设计与源码解析 说明&#xff1a;本文重点讲解现在流行的促销方案以及源码解析,让大家对促销&#xff0c;纳新有一个深入的了解与学习过程. 促销系统是电商系统另外一个比较大&#xff0c;也是比较复杂的系统&#xff0c;作为一个卖货的&#x…

vue3中websocket用法

1.0 认识 websocket #1.0.1 什么是 websocket 和 http 协议类似&#xff0c;websocket 也是是一个网络通信协议&#xff0c;是用来满足前后端数据通信的。 #1.0.2 websocket 相比于 HTTP 的优势 HTTP 协议&#xff1a;客户端与服务器建立通信连接之后&#xff0c;服务器端只…

介绍几个医学图像处理会议

Information Processing in Medical Imaging &#xff0c; IPMI &#xff0c;医学图像处理最顶级的会议&#xff0c;两年召开一次&#xff0c;全球大概入选 50 篇左右&#xff0c;一个非常小圈子的会&#xff0c;据说通常是关在一个偏僻的地方开一周&#xff0c;会议口头报告提…

python翻译

translator.py # -*- coding: utf-8 -*- # author: inspurer(月小水长) # pc_type lenovo # create_time: 2019/4/6 15:44 # file_name: translator.py # github https://github.com/inspurer # qq邮箱 2391527690qq.co…

promise并发

一、Pomise.all的使用 Promise.all可以将多个Promise实例包装成一个新的Promise实例。同时&#xff0c;成功和失败的返回值是不同的&#xff0c;成功的时候返回的是一个结果数组&#xff0c;而失败的时候则返回最先被reject失败状态的值。 let p1 new Promise((resolve, rej…

计算机视觉方面的三大国际会议是ICCV, CVPR和ECCV

ICCV的全称是International Comference on Computer Vision&#xff0c;正如很多和他一样的名字的会议一行&#xff0c;这样最朴实的名字的会议&#xff0c;通常也是这方面最nb的会议。ICCV两年一次&#xff0c;与ECCV正好错开&#xff0c;是公认的三个会议中级别最高的。它的举…

Scanner类+Random

引用数据类型的使用 数据类型 变量名 new 数据类型(); 变量名.方法名(); import java.util.Scanner; publicclass ScannerDemo01 { publicstaticvoid main(String[] args) { //创建Scanner引用类型的变量 Scanner sc new Scanner(System.in); //获取数字 System.out.print…

手写vue2的Lazyload

调用方式&#xff0c;express写个后台服务调图片&#xff0c;具体使用不贴了 import VueLazyload from "./modules/vue-lazyload";Vue.use(VueLazyload,{loading: http://localhost:3000/images/loading.gif,error: http://localhost:3000/images/error.jpg,preload…

nature,science上关于计算机视觉的一些原创文献

真正原始创新是怎么样的&#xff1f;希望这些列表对做视觉研究的朋友有些启发&#xff0c;希望大家能帮我补充一些&#xff0c;谢谢。转载请注明http://hi.baidu.com/daren007或者http://www.sciencenet.cn/blog/王中任.htm。1、D. Marr; T. Poggio.Cooperative Computation of…

webpack入门进阶调优第一章

1.1何为Webpack webpack是开源的JS模块打包工具 核心功能是解决模块之间的依赖&#xff0c;吧哥哥模块按照特定的规则和顺序组织在一起&#xff0c;最终合并为一个JS文件。这个过程叫模块打包 1.2为何需要Webpack 1.2.1何为模块 在设计程序结构时&#xff0c;更好的组织方…

python类的空间问题及类之间的关系

类的空间问题及类之间的关系 类的空间问题 1.何处可以添加对象属性 class A:def __init__(self,name):self.name namedef func(self,sex):self.sex sexobj A("alex") obj.age 18 # 通过__init__方法添加 print(obj.__dict__) obj1 A("wusir") obj.fun…

计算机视觉应关注的资源

来自美国帝腾大学的链接。 Camera Calibration Links to toolboxes (mostly MATLAB) for camera calibration. Paul Debevec. Modeling and Rendering Architecture from Photographs. Marc Pollefeys, Tutorial on 3D Modeling from Images,, ECCV 2000, Available here: not…

Angular 内容投影 II

内容投影是一种模式,你可以在其中插入或投影要在另一个组件中使用的内容。 简单来说,angular的内容投影就相当于vue的内容插槽slot。所有一下就能懂了。 1、单插槽内容投影 单插槽内容投影是指创建一个组件,你可以在其中投影一个组件。如果只有一个ng-content,不需要sel…

麻省理工学生发明 震惊世界

核心提示&#xff1a;在普拉纳夫看来&#xff0c;数字信息以像素的形式被限制在显示屏幕之中。他发明的"第六感装置"震惊全场&#xff0c;让世界为之惊叹。 MIT(麻省理工)印度裔学生Prarnav Mistry的天才发明:“第六感装置” 视频地址&#xff1a;http://v.youku.com…

Selenium常用API的使用java语言之13-多表单切换

在 Web 应用中经常会遇到 frame/iframe 表单嵌套页面的应用&#xff0c; WebDriver 只能在一个页面上对元素识别与 定位&#xff0c; 对于 frame/iframe 表单内嵌页面上的元素无法直接定位。 这时就需要通过 switchTo().frame()方法将当前定 位的主体切换为 frame/iframe 表单的…

yarn下载依赖慢的解决方法

首先设置npm或者yarn镜像为淘宝镜像 详情&#xff1a; 淘宝 NPM 镜像 npm config set registry https://registry.npm.taobao.org OR yarn config set registry https://registry.npm.taobao.org 在项目的根目录添加.npmrc 或者 .yarnrc 把镜像源设置淘宝镜像&#xff0c;nod…

立身成败 在于所染

唐朝有一位正直的丞相很受唐太宗的赏识&#xff0c;他叫魏征。他说过一句话&#xff1a;“立身成本在于所染”。这句话的意思就是说&#xff0c;一个人一生成败的关键在于交友。 孔子曰&#xff1a;“与善人居&#xff0c;如入芝兰之室&#xff0c;久而不闻其香&#xff0c;即与…