api-client/components/graphql/QueryEditor.vue

245 lines
5.5 KiB
Vue
Raw Normal View History

2020-01-18 09:14:30 +00:00
<template>
2020-09-22 17:06:37 +00:00
<div class="opacity-0 show-if-initialized" :class="{ initialized }">
2020-12-11 16:54:34 +00:00
<pre ref="editor" :class="styles"></pre>
</div>
2020-01-18 09:14:30 +00:00
</template>
<script>
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 * as gql from "graphql"
import { getAutocompleteSuggestions } from "graphql-language-service-interface"
2021-05-19 04:54:57 +00:00
import { defineGQLLanguageMode } from "~/helpers/syntax/gqlQueryLangMode"
import debounce from "~/helpers/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-09-24 16:07:27 +00:00
default: null,
2020-01-18 09:14:30 +00:00
},
onRunGQLQuery: {
type: Function,
default: () => {},
},
2020-01-18 09:14:30 +00:00
options: {
type: Object,
2021-05-19 04:54:57 +00:00
default: () => {},
2020-02-24 18:44:50 +00:00
},
2020-12-11 16:54:34 +00:00
styles: {
type: String,
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)
2021-05-19 04:54:57 +00:00
const 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 = {
2021-05-19 04:54:57 +00:00
getCompletions: (
editor,
_session,
{ row, column },
_prefix,
callback
) => {
if (this.validationSchema) {
2021-05-19 04:54:57 +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
},
2021-05-19 04:54:57 +00:00
beforeDestroy() {
this.editor.destroy()
},
2020-01-18 09:14:30 +00:00
methods: {
2020-03-05 19:20:15 +00:00
prettifyQuery() {
try {
2021-05-19 04:54:57 +00:00
this.$emit("update-query", 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
}
2021-05-19 04:54:57 +00:00
const strip = (str) =>
str.replace(/#/g, "").replace(/ /g, "").replace(/"/g, "")
2020-09-24 02:52:54 +00:00
return strip(
2021-05-19 04:54:57 +00:00
window
.getComputedStyle(document.documentElement)
.getPropertyValue("--editor-theme")
2020-09-24 02:52:54 +00:00
)
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(
2021-05-19 04:54:57 +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
},
}
2020-01-18 09:14:30 +00:00
</script>
2021-05-19 04:54:57 +00:00
<style scoped lang="scss">
.show-if-initialized {
&.initialized {
@apply opacity-100;
}
& > * {
@apply transition-none;
}
}
</style>