Added linting for JSON in the Code Editor

This commit is contained in:
Andrew Bastin
2020-02-25 12:02:15 -05:00
parent f7f4f02d4a
commit b9b0745f30

View File

@@ -21,8 +21,10 @@
<script> <script>
const DEFAULT_THEME = "twilight" const DEFAULT_THEME = "twilight"
import ace from "ace-builds" import ace from "ace-builds";
import "ace-builds/webpack-resolver" import "ace-builds/webpack-resolver";
import jsonParse from '../functions/jsonParse';
import debounce from '../functions/utils/debounce';
export default { export default {
props: { props: {
@@ -55,8 +57,10 @@ export default {
watch: { watch: {
value(value) { value(value) {
if (value !== this.cacheValue) { if (value !== this.cacheValue) {
this.editor.session.setValue(value, 1) this.editor.session.setValue(value, 1);
this.cacheValue = value this.cacheValue = value;
this.provideLinting(value);
} }
}, },
theme() { theme() {
@@ -94,10 +98,13 @@ export default {
this.cacheValue = this.value this.cacheValue = this.value
editor.on("change", () => { editor.on("change", () => {
const content = editor.getValue() const content = editor.getValue();
this.$emit("input", content) this.$emit("input", content);
this.cacheValue = content this.cacheValue = content;
}) this.provideLinting(content);
});
this.provideLinting(this.value);
}, },
methods: { methods: {
@@ -105,8 +112,29 @@ export default {
if (this.theme) { if (this.theme) {
return this.theme return this.theme
} }
return this.$store.state.postwoman.settings.THEME_ACE_EDITOR || DEFAULT_THEME return (
this.$store.state.postwoman.settings.THEME_ACE_EDITOR || DEFAULT_THEME
);
}, },
provideLinting: debounce(function (code) {
if (this.lang === "json") {
try {
jsonParse(code);
this.editor.session.setAnnotations([]);
} catch (e) {
const pos = this.editor.session.getDocument().indexToPosition(e.start, 0);
this.editor.session.setAnnotations([
{
row: pos.row,
column: pos.column,
text: e.message,
type: "error"
}
]);
}
}
}, 2000)
}, },
destroyed() { destroyed() {