基於CodeMirror開發在線編輯器時遇到的問題及解決方案

需求:實現json在線編輯並支持校驗,基於此使用了 CodeMirror在線編輯,jsonlint校驗輸入數據

// package.json:
  "dependencies": {
    "codemirror": "^5.53.2",
    "core-js": "^3.8.3",
    "jsonlint": "^1.6.3",
    "vue": "^2.6.14"
  },

基礎代碼:

<template>
  <div class="json-editor">
    <textarea ref="textarea" />
  </div>
</template>

<script>
import CodeMirror from "codemirror";
import "codemirror/addon/lint/lint.css";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/rubyblue.css";
import "codemirror/mode/javascript/javascript";
import "codemirror/addon/lint/lint";
import "codemirror/addon/lint/json-lint";
require('script-loader!jsonlint')
export default {
  name: "JsonEditor", 
  props: ["value"],
  data() {
    return {
      jsonEditor: false,
    };
  },
  watch: {
    value(value) {
      const editorValue = this.jsonEditor.getValue();
      if (value !== editorValue) {
        this.jsonEditor.setValue(JSON.stringify(this.value, null, 2));
      }
    },
  },
  mounted() {
    // CodeMirror.fromTextArea()中第一個參數是DOM元素,而且必須是textarea元素;第二個參數是可選配置項
    this.jsonEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
      lineNumbers: true,
      mode: "application/json",
      gutters: ["CodeMirror-lint-markers"],
      theme: "rubyblue",
      lint: true,
      lineWrapping: true, // 自動換行
      scrollPastEnd: true, // 允許用戶將一個編輯器高度的空白區域滾動到編輯器底部的視圖
      lineNumbers: true, // 顯示左邊行號(默認false,即不顯示)
      styleActiveLine: true, // 當前行背景高亮
    });

    this.jsonEditor.setValue(JSON.stringify(this.value, null, 2));
    this.jsonEditor.on("change", (cm) => {
      console.log("cm.getValue() :>> ", cm.getValue());
      this.$emit("changed", cm.getValue());
      this.$emit("input", cm.getValue());
    });
  },
  methods: {
    getValue() {
      return this.jsonEditor.getValue();
    },
  },
};
</script>

<style scoped>
.json-editor {
  height: 100%;
  position: relative;
}
.json-editor >>> .CodeMirror {
  height: auto;
  min-height: 300px;
}
.json-editor >>> .CodeMirror-scroll {
  min-height: 300px;
}
.json-editor >>> .cm-s-rubyblue span.cm-string {
  color: #f08047;
}
.addbtn {
  margin-bottom: 15px;
  margin-left: 30px;
}
</style>

這樣,他就成功的報錯了!
報錯原因是因爲這一行:require('script-loader!jsonlint')
項目找不到script-loader,因而下了一個:

npm install script-loader

下載完畢,項目就有了雛形:

但是估計錯誤輸入後發現,並沒有實現錯誤校驗功能,很納悶,明明寫了lint:true
然後嘗試打開控制檯看了下:

json-lint.js:22  Error: window.jsonlint not defined, CodeMirror JSON linting cannot run.

不懂爲什麼,於是嘗試使用import替換require
依舊如此
後來去github翻了下issue, 發現此條可用:
https://github.com/scniro/react-codemirror2/issues/21

須下載jsonlint-mod:

npm install jsonlint-mod

於是便實現了:

完整代碼(無坑版):

<template>
  <div class="json-editor">
    <textarea ref="textarea" />
  </div>
</template>

<script>
import CodeMirror from "codemirror";
import "codemirror/addon/lint/lint.css";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/rubyblue.css";
import "codemirror/mode/javascript/javascript";
import "codemirror/addon/lint/lint";
import "codemirror/addon/lint/json-lint";
const jsonlint = require("jsonlint-mod");
console.log('jsonlint :>> ', jsonlint);
window.jsonlint = jsonlint;
export default {
  name: "JsonEditor", 
  props: ["value"],
  data() {
    return {
      jsonEditor: false,
    };
  },
  watch: {
    value(value) {
      const editorValue = this.jsonEditor.getValue();
      if (value !== editorValue) {
        this.jsonEditor.setValue(JSON.stringify(this.value, null, 2));
      }
    },
  },
  mounted() {
    // CodeMirror.fromTextArea()中第一個參數是DOM元素,而且必須是textarea元素;第二個參數是可選配置項
    this.jsonEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
      lineNumbers: true,
      mode: "application/json",
      gutters: ["CodeMirror-lint-markers"],
      theme: "rubyblue",
      lint: true,
      lineWrapping: true, // 自動換行
      scrollPastEnd: true, // 允許用戶將一個編輯器高度的空白區域滾動到編輯器底部的視圖
      lineNumbers: true, // 顯示左邊行號(默認false,即不顯示)
      styleActiveLine: true, // 當前行背景高亮
    });

    this.jsonEditor.setValue(JSON.stringify(this.value, null, 2));
    this.jsonEditor.on("change", (cm) => {
      console.log("cm.getValue() :>> ", cm.getValue());
      this.$emit("changed", cm.getValue());
      this.$emit("input", cm.getValue());
    });
  },
  methods: {
    getValue() {
      return this.jsonEditor.getValue();
    },
  },
};
</script>

<style scoped>
.json-editor {
  height: 100%;
  position: relative;
}
.json-editor >>> .CodeMirror {
  height: auto;
  min-height: 300px;
}
.json-editor >>> .CodeMirror-scroll {
  min-height: 300px;
}
.json-editor >>> .cm-s-rubyblue span.cm-string {
  color: #f08047;
}
.addbtn {
  margin-bottom: 15px;
  margin-left: 30px;
}
</style>

以上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章