Allow the passing of a lint prop to disable linting on an editor

This commit is contained in:
Dmitry Yankowski
2020-02-25 20:43:34 -05:00
parent 2a1eca1539
commit 68c749b378
2 changed files with 57 additions and 46 deletions

View File

@@ -5,26 +5,26 @@
</template> </template>
<style lang="scss"> <style lang="scss">
.show-if-initialized { .show-if-initialized {
opacity: 0; opacity: 0;
&.initialized { &.initialized {
opacity: 1; opacity: 1;
}
& > * {
transition: none;
}
} }
& > * {
transition: none;
}
}
</style> </style>
<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 jsonParse from "../functions/jsonParse"
import debounce from '../functions/utils/debounce'; import debounce from "../functions/utils/debounce"
export default { export default {
props: { props: {
@@ -40,6 +40,11 @@ export default {
type: String, type: String,
default: "json", default: "json",
}, },
lint: {
type: Boolean,
default: true,
required: false,
},
options: { options: {
type: Object, type: Object,
default: {}, default: {},
@@ -50,6 +55,7 @@ export default {
return { return {
initialized: false, initialized: false,
editor: null, editor: null,
shouldLint: true,
cacheValue: "", cacheValue: "",
} }
}, },
@@ -57,19 +63,18 @@ 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
if (this.shouldLint) this.provideLinting(value)
this.provideLinting(value);
} }
}, },
theme() { theme() {
this.initialized = false; this.initialized = false
this.editor.setTheme(`ace/theme/${this.defineTheme()}`, () => { this.editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
this.$nextTick().then(() => { this.$nextTick().then(() => {
this.initialized = true; this.initialized = true
}); })
}); })
}, },
lang(value) { lang(value) {
this.editor.getSession().setMode("ace/mode/" + value) this.editor.getSession().setMode("ace/mode/" + value)
@@ -80,6 +85,9 @@ export default {
}, },
mounted() { mounted() {
// Set whether or not we should lint the editors contents
this.shouldLint = this.lint
const editor = ace.edit(this.$refs.editor, { const editor = ace.edit(this.$refs.editor, {
mode: `ace/mode/${this.lang}`, mode: `ace/mode/${this.lang}`,
...this.options, ...this.options,
@@ -88,23 +96,24 @@ export default {
// Set the theme and show the editor only after it's been set to prevent FOUC. // Set the theme and show the editor only after it's been set to prevent FOUC.
editor.setTheme(`ace/theme/${this.defineTheme()}`, () => { editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
this.$nextTick().then(() => { this.$nextTick().then(() => {
this.initialized = true; this.initialized = true
}); })
}); })
if (this.value) editor.setValue(this.value, 1); if (this.value) editor.setValue(this.value, 1)
this.editor = editor this.editor = editor
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); if (this.shouldLint) this.provideLinting(content)
}); })
this.provideLinting(this.value); // Disable linting, if lint prop is false
if (this.shouldLint) this.provideLinting(this.value)
}, },
methods: { methods: {
@@ -112,33 +121,31 @@ export default {
if (this.theme) { if (this.theme) {
return this.theme return this.theme
} }
return ( return this.$store.state.postwoman.settings.THEME_ACE_EDITOR || DEFAULT_THEME
this.$store.state.postwoman.settings.THEME_ACE_EDITOR || DEFAULT_THEME
);
}, },
provideLinting: debounce(function (code) { provideLinting: debounce(function(code) {
if (this.lang === "json") { if (this.lang === "json") {
try { try {
jsonParse(code); jsonParse(code)
this.editor.session.setAnnotations([]); this.editor.session.setAnnotations([])
} catch (e) { } catch (e) {
const pos = this.editor.session.getDocument().indexToPosition(e.start, 0); const pos = this.editor.session.getDocument().indexToPosition(e.start, 0)
this.editor.session.setAnnotations([ this.editor.session.setAnnotations([
{ {
row: pos.row, row: pos.row,
column: pos.column, column: pos.column,
text: e.message, text: e.message,
type: "error" type: "error",
} },
]); ])
} }
} }
}, 2000) }, 2000),
}, },
destroyed() { destroyed() {
this.editor.destroy(); this.editor.destroy()
} },
}; }
</script> </script>

View File

@@ -212,6 +212,7 @@
<Editor <Editor
:value="response" :value="response"
:lang="'json'" :lang="'json'"
:lint="false"
:options="{ :options="{
maxLines: responseBodyMaxLines, maxLines: responseBodyMaxLines,
minLines: '16', minLines: '16',
@@ -511,7 +512,6 @@ export default {
} }
const data = await sendNetworkRequest(reqOptions, this.$store) const data = await sendNetworkRequest(reqOptions, this.$store)
this.response = JSON.stringify(data.data, null, 2) this.response = JSON.stringify(data.data, null, 2)
this.$nuxt.$loading.finish() this.$nuxt.$loading.finish()
@@ -520,6 +520,10 @@ export default {
icon: "done", icon: "done",
}) })
} catch (error) { } catch (error) {
// Reset response back to empty string
// Note: We're specifically setting this to an empty string, as
// returning {} could could give the impression that the query succeeded
this.response = ""
this.$nuxt.$loading.finish() this.$nuxt.$loading.finish()
this.$toast.error(`${error} ${this.$t("f12_details")}`, { this.$toast.error(`${error} ${this.$t("f12_details")}`, {