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

致爱丽丝發表於2024-05-02

需求:實現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>

以上。

相關文章