Vue 基於vue-codemirror實現的代碼編輯器

来源:https://www.cnblogs.com/shouke/archive/2020/07/12/13288743.html
-Advertisement-
Play Games

基於vue-codemirror實現的代碼編輯器 開發環境 jshint 2.11.1 jsonlint 1.6.3 script-loader 0.7.2 vue 2.6.11 vue-codemirror 4.0.6 element-ui 2.13.1 (使用到element-ui messag ...


基於vue-codemirror實現的代碼編輯器

開發環境

jshint 2.11.1

jsonlint 1.6.3

script-loader 0.7.2

vue 2.6.11

vue-codemirror 4.0.6

element-ui 2.13.1 (使用到element-ui message組件,提示錯誤消息,如果不想安裝該組件,替換編輯器中的this.$message所在行函數代碼即可)

 

功能介紹

1、  支持不同的代碼編輯模式

目前僅支持支持json, sql, javascript,css,xml, html,yaml, markdown, python編輯模式,預設為 json

 

2、  支持使用不同主題

支持62種主題,預設為 blackboard

 

3、  支持API編程

目前支持修改樣式,獲取內容,修改編輯框內容值

 

4、  支持複製,黏貼,剪切,撤銷等常見操作

 

5、  支持文件拖拽導入

支持滑鼠拖拽文件到編輯框,編輯框自動展示被拖拽文件的內容(當然,不是所有文件都可以,比如word文件,.exe文件就不行)

 

6、  支持json格式化

1)json編輯模式下,滑鼠失去焦點時自動格式化json字元串,支持定義開關該特性

2)支持自定義格式化化縮進,支持字元或數字,最大不超過10,預設縮進2個空格

3)json編輯模式下,黏貼json字元串到編輯框時,支持自動格式化編輯框內容

4)json編輯模式下,支持按Ctrl+Alt+L快捷鍵主動格式化當前json格式字元內容

           

7、  支持顯示代碼行號

 

8、  支持編輯時“智能”縮進

 

9、  支持代碼摺疊/展開

支持json, sql, javascript,css,xml, html,yaml, markdown, python等

 

10、 支持靜態代碼語法檢查

目前僅支持支持 json,javascript

 

11、 支持批量替換

操作方法:

按Ctrl + Shift + r鍵,彈出框中輸入要被替換的內容,回車,然後再次輸入用於替換的內容,回車即可。

 

12、 支持快速搜索

操作方法:

按Ctrl + F,彈出框中輸入要查找內容,回車

 

13、 支持跳轉到指定行

操作方法:

按Alt + G 快捷鍵, 彈出快對話框中輸入行號,回車即可

 

14、 支持滑鼠點擊高亮匹配單詞

使用場景舉例:滑鼠點擊某個單詞,高亮其它區域和被點擊單詞相同的單詞

 

15、 支持自動補全提示

目前僅支持 sql,javascript,html,python

 

備註:出現自動補全提示時,按tab鍵可自動補全

 

16、 支持自動補全括弧,單、雙引號

支持自動補全括弧:(),[],{},單引號,雙引號:'' ""

 

使用場景舉例:輸入 [ 時,自動顯示為[],並且把游標定位在括弧中間

 

17、 支持自動補全xml標簽

支持輸入完開放xml、html元素標簽時,自動補齊右側閉合標簽、或者輸入完 </ 時,自動補齊閉合標簽

 

使用場景舉例:輸入完<book>時自動補齊右側</book>

 

18、 支持自動匹配xml標簽

xml、html編輯模式下,支持自動匹配標簽

 

使用場景舉例:滑鼠點擊時xml標簽時(開放標簽或閉合標簽),自動高亮另一半標簽

 

19、 支持自動匹配括弧

使用場景舉例:游標點擊緊挨{、]括弧左、右側時,自動突出顯示匹配的括弧 }、]

 

20、 支持游標所在當前行背景高亮

 

21、 支持高亮選中內容

使用場景舉例:按下滑鼠左鍵,拖拽選擇內容時,高亮被選中內容,文字反白

主要依賴安裝

npm install jsonlint

npm install jshint

npm install script-loader

npm install vue-codemirror

npm install element-ui

src/main.js配置

添加以下帶背景色的部分的配置

import Vue from "vue"

import ElementUI from "element-ui"

import "element-ui/lib/theme-chalk/index.css"

 

...略

// 引入jshint用於實現js自動補全提示 

import jshint from "jshint";

window.JSHINT = jshint.JSHINT;

 

// 引入代碼編輯器 

import { codemirror } from "vue-codemirror";

import "codemirror/lib/codemirror.css";

 

import App from "./app/App"

 

...略

 

Vue.use(ElementUI);

Vue.use(codemirror);

 

...略

 

編輯器組件實現

<template>

    <codemirror

        ref="myCm"

        :value="editorValue"

        :options="cmOptions"

        @changes="onCmCodeChanges"

        @blur="onCmBlur"

        @keydown.native="onKeyDown"

        @mousedown.native="onMouseDown"

        @paste.native="OnPaste"

    ></codemirror>

</template>

 

<script>

import { codemirror } from "vue-codemirror";

 

import "codemirror/theme/blackboard.css";

import "codemirror/mode/javascript/javascript.js"; 

import "codemirror/mode/xml/xml.js";

import "codemirror/mode/htmlmixed/htmlmixed.js"; 

import "codemirror/mode/css/css.js";

import "codemirror/mode/yaml/yaml.js";

import "codemirror/mode/sql/sql.js";

import "codemirror/mode/python/python.js";

import "codemirror/mode/markdown/markdown.js";

import "codemirror/addon/hint/show-hint.css";

import "codemirror/addon/hint/show-hint.js"; 

import "codemirror/addon/hint/javascript-hint.js";

import "codemirror/addon/hint/xml-hint.js";

import "codemirror/addon/hint/css-hint.js"; 

import "codemirror/addon/hint/html-hint.js";

import "codemirror/addon/hint/sql-hint.js";

import "codemirror/addon/hint/anyword-hint.js"; 

import "codemirror/addon/lint/lint.css";

import "codemirror/addon/lint/lint.js"; 

import "codemirror/addon/lint/json-lint"; 

require("script-loader!jsonlint");

// import "codemirror/addon/lint/html-lint.js";

// import "codemirror/addon/lint/css-lint.js";  

import "codemirror/addon/lint/javascript-lint.js"; 

import "codemirror/addon/fold/foldcode.js"; 

import "codemirror/addon/fold/foldgutter.js"; 

import "codemirror/addon/fold/foldgutter.css";

import "codemirror/addon/fold/brace-fold.js"; 

import "codemirror/addon/fold/xml-fold.js";

import "codemirror/addon/fold/comment-fold.js"; 

import "codemirror/addon/fold/markdown-fold.js"; 

import "codemirror/addon/fold/indent-fold.js"; 

import "codemirror/addon/edit/closebrackets.js"; 

import "codemirror/addon/edit/closetag.js";

import "codemirror/addon/edit/matchtags.js"; 

import "codemirror/addon/edit/matchbrackets.js";

import "codemirror/addon/selection/active-line.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"; 

import "codemirror/addon/display/autorefresh.js"; 

import "codemirror/addon/selection/mark-selection.js"; 

import "codemirror/addon/search/match-highlighter.js"; 

 

export default {

    components: {

        codemirror

    },

    props: ["cmTheme", "cmMode", "autoFormatJson", "jsonIndentation"],

    data() {

        return {

            editorValue: "",

            cmOptions: {

                theme:

                    !this.cmTheme || this.cmTheme == "default"

                        ? "blackboard"

                        : this.cmTheme, 

                mode:

                    !this.cmMode || this.cmMode == "default"

                        ? "application/json"

                        : this.cmMode, 

                lineWrapping: true, 

                lineNumbers: true, 

                autofocus: true, 

                smartIndent: false, 

                autocorrect: true, 

                spellcheck: true, 

                extraKeys: {

                    Tab: "autocomplete", 

                    "Ctrl-Alt-L": () => {

                        try {

                            if (

                                this.cmOptions.mode == "application/json" &&

                                this.editorValue

                            ) {

                                this.editorValue = this.formatStrInJson(

                                    this.editorValue

                                );

                            }

                        } catch (e) {

                            this.$message.error(

                                "格式化代碼出錯:" + e.toString()

                            );

                        }

                    }                    

                },

                lint: true,

                gutters: [

                    "CodeMirror-lint-markers", 

                    "CodeMirror-linenumbers", 

                    "CodeMirror-foldgutter" 

                ],

                foldGutter: true,

                autoCloseBrackets: true, 

                autoCloseTags: true,

                matchTags: { bothTags: true },

                matchBrackets: true, 

                styleActiveLine: true, 

                autoRefresh: true, 

                highlightSelectionMatches: {

                    minChars: 2,

                    style: "matchhighlight", 

                    showToken: true 

                },

                styleSelectedText: true,

                enableAutoFormatJson:

                    this.autoFormatJson == null ? true : this.autoFormatJson,

 

                defaultJsonIndentation:

                    !this.jsonIndentation ||

                    typeof this.jsonIndentation != typeof 1

                        ? 2

                        : this.jsonIndentation 

            },

            enableAutoFormatJson:

                this.autoFormatJson == null ? true : this.autoFormatJson, 

            defaultJsonIndentation:

                !this.jsonIndentation || typeof this.jsonIndentation != typeof 1

                    ? 2

                    : this.jsonIndentation 

        };

    },

 

    watch: {

        cmTheme: function(newValue, oldValue) {

            try {

                let theme =

                    this.cmTheme == "default" ? "blackboard" : this.cmTheme;

                require("codemirror/theme/" + theme + ".css");

                this.cmOptions.theme = theme;

                this.resetLint();

            } catch (e) {

                this.$message.error("切換編輯器主題出錯:" + e.toString());

            }

        },

        cmMode: function(newValue, oldValue) {

            this.$set(this.cmOptions, "mode", this.cmMode);

            this.resetLint();

            this.resetFoldGutter();

        }

    },

    methods: {

        resetLint() {

            if (!this.$refs.myCm.codemirror.getValue()) {

                this.$nextTick(() => {

                    this.$refs.myCm.codemirror.setOption("lint", false);

                });

                return;

            }

            this.$refs.myCm.codemirror.setOption("lint", false);

            this.$nextTick(() => {

                this.$refs.myCm.codemirror.setOption("lint", true);

            });

        },

 

        resetFoldGutter() {

           this.$refs.myCm.codemirror.setOption("foldGutter", false);

            this.$nextTick(() => {

                this.$refs.myCm.codemirror.setOption("foldGutter", true);

            });

        },

        // 修改編輯框樣式

        setStyle(style) {

            try {

                this.$nextTick(() => {

                    let cm = this.$refs.myCm.$el.querySelector(".CodeMirror");

                    if (cm) {

                        cm.style.cssText = style;

                    } else {

                        this.$message.error(

                            "未找到編輯器元素,修改編輯器樣式失敗"

                        );

                    }

                });

            } catch (e) {

                this.$message.error("修改編輯器樣式失敗:" + e.toString());

            }

        },

        // 獲取值

        getValue() {

            try {

                return this.$refs.myCm.codemirror.getValue();

            } catch (e) {

                let errorInfo = e.toString();

                this.$message.error("獲取編輯框內容失敗:" + errorInfo);

                return errorInfo;

            }

        },

        // 修改值

        setValue(value) {

            try {

                if (typeof value != typeof "") {

                    this.$message.error(

                        "修改編輯框內容失敗:編輯寬內容只能為字元串"

                    );

                    return;

                }

                if (this.cmOptions.mode == "application/json") {

                    this.editorValue = this.formatStrInJson(value);

                } else {

                    this.editorValue = value;

                }

            } catch (e) {

                this.$message.error("修改編輯框內容失敗:" + e.toString());

            }

        },

        // 黏貼事件處理函數

        OnPaste(event) {

            if (this.cmOptions.mode == "application/json") {

                try {

                    this.editorValue = this.formatStrInJson(this.editorValue);

                } catch (e) {

                    // 啥都不做

                }

            }

        },

        // 失去焦點時處理函數

        onCmBlur(cm, event) {

            try {

                let editorValue = cm.getValue();

                if (this.cmOptions.mode == "application/json" && editorValue) {

                    if (!this.enableAutoFormatJson) {

                        return;

                    }

                    this.editorValue = this.formatStrInJson(editorValue);

                }

            } catch (e) {

                // 啥也不做

            }

        },

        // 按下鍵盤事件處理函數

        onKeyDown(event) {

            const keyCode = event.keyCode || event.which || event.charCode;

 

            const keyCombination =

                event.ctrlKey || event.altKey || event.metaKey;

 

            if (!keyCombination && keyCode > 64 && keyCode < 123) {

                this.$refs.myCm.codemirror.showHint({ completeSingle: false });

            }

        },

        // 按下滑鼠時事件處理函數

        onMouseDown(event) {

            this.$refs.myCm.codemirror.closeHint();

        },

        onCmCodeChanges(cm, changes) {

            this.editorValue = cm.getValue();

            this.resetLint();

        },

        // 格式化字元串為json格式字元串

        formatStrInJson(strValue) {

            return JSON.stringify(

                JSON.parse(strValue),

                null,

                this.defaultJsonIndentation

            );

        }

    },

    created() {

        try {

            if (!this.editorValue) {

                this.cmOptions.lint = false;

                return;

            }

 

            if (this.cmOptions.mode == "application/json") {

                if (!this.enableAutoFormatJson) {

                    return;

                }

                this.editorValue = this.formatStrInJson(this.editorValue);

            }

        } catch (e)&nb

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 周末的時光是短暫,也是輕鬆愉快的,在這炎炎的夏日坐在小板凳上,吹著空調喝著茶的我帶你點輕輕的點開我的文章鏈接,帶領屏幕前的你回顧一下MySql的內容,希望你能有所收穫。本篇隨筆分上下兩部分,上半部分理論,下半部分實際操作。 上 複習:資料庫的相關概念 一、資料庫的好處 1.可以持久化到本地 2.結構 ...
  • 資料庫的基本概念 1. 資料庫的英文單詞: DataBase 簡稱 : DB 2. 什麼資料庫? * 用於存儲和管理數據的倉庫。 3. 資料庫的特點: 1. 持久化存儲數據的。其實資料庫就是一個文件系統 2. 方便存儲和管理數據 3. 使用了統一的方式操作資料庫 -- SQL ​ 4. 常見的資料庫 ...
  • 介紹幾個常用的redis命令: SET 命令 set key value 設置指定 key 的值為 value。 如果 key 已經存儲其他值, SET 就覆寫舊值,且無視類型。 127.0.0.1:6379> set testSetKey aaa OK 127.0.0.1:6379> get te ...
  • 在mariadb的主從複製集群中,讀的能力被擴展了,而寫的能力始終沒有被擴展;這樣一來對於主伺服器就存在單點的問題,通常除了做雙主可解決主節點單點的問題,我們還可以給主節點做高可用;而對於mariadb的主從複製集群來講,雖然讀的能力提升了,但通常情況後端資料庫伺服器是直接面向程式,這意味著程式要... ...
  • 已知圓的坐標方程為: X=R*SIN(θ) Y=R*COS(θ) (0≤θ≤2π) 將0~2π區間等分48段,即設定間隔dig的值為π/24。θ初始值從0開始,按曲線方程求得坐標值(x,y),併在當前坐標處繪製一個半徑為r(相比R,r小得多)實心圓。之後每隔0.05秒,清除畫布,將θ的初始值加π/2 ...
  • 問題踩坑: 在使用layui的時候,通常引入一個layui.all.js,把他放到js文件夾下,在寫一些輸入框的時候,表單驗證的時候,提示彈窗顯示不出來 以下是代碼部分: <div class="layui-row"> <input type="text" name="phone" lay-veri ...
  • 推薦使用條件編譯,少用條件判斷 // @/utils/EPlatform.ts/**枚舉EPlatform*/ export enum EPlatform { /**App*/ AppPlus = 'APP-PLUS', /**App nvue*/ AppPlusNvue = 'APP-PLUS-N ...
  • JavaScript監聽組合按鍵 by:授客 QQ:1033553122 1. 思路 如圖,通過監聽並列印鍵盤keydown事件,得到圖示內容,觀察發現, 當按下的組合鍵包含Ctrl鍵時,ctrlKey鍵會顯示為true; 當按下的組合鍵包含Shift鍵、或者按鍵之前開啟大寫時,shiftkey鍵會 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...