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.pswp.cn/news/247284.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/247284.shtml
英文地址,請注明出處:http://en.pswp.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;即與…