api-client/components/graphql/queryeditor.vue

227 lines
5.1 KiB
Vue
Raw Normal View History

2020-01-18 09:14:30 +00:00
<template>
<div class="show-if-initialized" :class="{ initialized }">
<pre ref="editor"></pre>
</div>
2020-01-18 09:14:30 +00:00
</template>
<style lang="scss">
.show-if-initialized {
opacity: 0;
&.initialized {
opacity: 1;
}
& > * {
transition: none;
}
}
</style>
2020-01-18 09:14:30 +00:00
<script>
const DEFAULT_THEME = "twilight"
2020-01-18 09:14:30 +00:00
import ace from "ace-builds"
import "ace-builds/webpack-resolver"
import "ace-builds/src-noconflict/ext-language_tools"
import "ace-builds/src-noconflict/mode-graphqlschema"
import { defineGQLLanguageMode } from "~/functions/syntax/gqlQueryLangMode"
import * as gql from "graphql"
import { getAutocompleteSuggestions } from "graphql-language-service-interface"
import debounce from "../../functions/utils/debounce"
2020-01-18 09:14:30 +00:00
export default {
props: {
value: {
type: String,
default: "",
2020-01-18 09:14:30 +00:00
},
theme: {
type: String,
2020-02-24 18:44:50 +00:00
required: false,
2020-01-18 09:14:30 +00:00
},
onRunGQLQuery: {
type: Function,
default: () => {},
},
2020-01-18 09:14:30 +00:00
options: {
type: Object,
2020-02-24 18:44:50 +00:00
default: {},
},
2020-01-18 09:14:30 +00:00
},
data() {
return {
initialized: false,
2020-01-18 09:14:30 +00:00
editor: null,
cacheValue: "",
2020-02-24 18:44:50 +00:00
validationSchema: null,
}
2020-01-18 09:14:30 +00:00
},
watch: {
value(value) {
if (value !== this.cacheValue) {
2020-02-24 18:44:50 +00:00
this.editor.session.setValue(value, 1)
this.cacheValue = value
2020-01-18 09:14:30 +00:00
}
},
theme() {
this.initialized = false
this.editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
this.$nextTick().then(() => {
this.initialized = true
})
})
2020-01-18 09:14:30 +00:00
},
options(value) {
2020-02-24 18:44:50 +00:00
this.editor.setOptions(value)
},
2020-01-18 09:14:30 +00:00
},
mounted() {
defineGQLLanguageMode(ace)
let langTools = ace.require("ace/ext/language_tools")
2020-01-18 09:14:30 +00:00
const editor = ace.edit(this.$refs.editor, {
mode: `ace/mode/gql-query`,
enableBasicAutocompletion: true,
enableLiveAutocompletion: true,
2020-02-24 18:44:50 +00:00
...this.options,
})
2020-01-18 09:14:30 +00:00
// Set the theme and show the editor only after it's been set to prevent FOUC.
editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
this.$nextTick().then(() => {
this.initialized = true
})
})
// Set the theme and show the editor only after it's been set to prevent FOUC.
editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
this.$nextTick().then(() => {
this.initialized = true
})
})
const completer = {
2020-02-24 18:44:50 +00:00
getCompletions: (editor, _session, { row, column }, _prefix, callback) => {
if (this.validationSchema) {
2020-02-24 18:44:50 +00:00
const completions = getAutocompleteSuggestions(this.validationSchema, editor.getValue(), {
line: row,
character: column,
})
2020-02-23 01:28:13 +00:00
callback(
null,
completions.map(({ label, detail }) => ({
name: label,
value: label,
score: 1.0,
2020-02-24 18:44:50 +00:00
meta: detail,
2020-02-23 01:28:13 +00:00
}))
2020-02-24 18:44:50 +00:00
)
} else {
2020-02-24 18:44:50 +00:00
callback(null, [])
}
2020-02-24 18:44:50 +00:00
},
}
2020-02-24 18:44:50 +00:00
langTools.setCompleters([completer])
2020-02-24 18:44:50 +00:00
if (this.value) editor.setValue(this.value, 1)
2020-01-18 09:14:30 +00:00
2020-02-24 18:44:50 +00:00
this.editor = editor
this.cacheValue = this.value
2020-01-18 09:14:30 +00:00
editor.commands.addCommand({
name: "runGQLQuery",
exec: () => this.onRunGQLQuery(this.editor.getValue()),
bindKey: {
mac: "cmd-enter",
win: "ctrl-enter",
},
})
2020-03-05 19:20:15 +00:00
editor.commands.addCommand({
name: "prettifyGQLQuery",
exec: () => this.prettifyQuery(),
bindKey: {
mac: "cmd-p",
win: "ctrl-p",
2020-03-05 19:20:15 +00:00
},
})
editor.on("change", () => {
2020-02-24 18:44:50 +00:00
const content = editor.getValue()
this.$emit("input", content)
2020-02-24 18:44:50 +00:00
this.parseContents(content)
this.cacheValue = content
})
2020-01-18 09:42:50 +00:00
2020-02-24 18:44:50 +00:00
this.parseContents(this.value)
2020-01-18 09:14:30 +00:00
},
methods: {
2020-03-05 19:20:15 +00:00
prettifyQuery() {
try {
this.value = gql.print(gql.parse(this.editor.getValue()))
} catch (e) {
this.$toast.error(`${this.$t("gql_prettify_invalid_query")}`, {
icon: "error",
})
}
2020-03-05 19:20:15 +00:00
},
2020-01-18 09:14:30 +00:00
defineTheme() {
if (this.theme) {
2020-02-24 18:44:50 +00:00
return this.theme
2020-01-18 09:14:30 +00:00
} else {
2020-02-24 18:44:50 +00:00
return this.$store.state.postwoman.settings.THEME_ACE_EDITOR || DEFAULT_THEME
2020-01-18 09:14:30 +00:00
}
},
setValidationSchema(schema) {
2020-02-24 18:44:50 +00:00
this.validationSchema = schema
this.parseContents(this.cacheValue)
},
2020-01-18 09:14:30 +00:00
parseContents: debounce(function (content) {
if (content !== "") {
try {
2020-02-24 18:44:50 +00:00
const doc = gql.parse(content)
if (this.validationSchema) {
this.editor.session.setAnnotations(
2020-02-24 18:44:50 +00:00
gql.validate(this.validationSchema, doc).map(({ locations, message }) => ({
row: locations[0].line - 1,
column: locations[0].column - 1,
text: message,
type: "error",
2020-02-24 18:44:50 +00:00
}))
)
2020-01-19 07:07:19 +00:00
}
} catch (e) {
this.editor.session.setAnnotations([
{
row: e.locations[0].line - 1,
column: e.locations[0].column - 1,
text: e.message,
type: "error",
2020-02-24 18:44:50 +00:00
},
])
}
} else {
2020-02-24 18:44:50 +00:00
this.editor.session.setAnnotations([])
2020-01-18 09:14:30 +00:00
}
2020-02-24 18:44:50 +00:00
}, 2000),
2020-01-18 09:14:30 +00:00
},
beforeDestroy() {
this.editor.destroy()
},
}
2020-01-18 09:14:30 +00:00
</script>