QueryEditor.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <div class="opacity-0 show-if-initialized" :class="{ initialized }">
  3. <pre ref="editor" :class="styles"></pre>
  4. </div>
  5. </template>
  6. <script>
  7. import ace from "ace-builds"
  8. import "ace-builds/webpack-resolver"
  9. import "ace-builds/src-noconflict/ext-language_tools"
  10. import "ace-builds/src-noconflict/mode-graphqlschema"
  11. import * as gql from "graphql"
  12. import { getAutocompleteSuggestions } from "graphql-language-service-interface"
  13. import { defineComponent } from "@nuxtjs/composition-api"
  14. import { defineGQLLanguageMode } from "~/helpers/syntax/gqlQueryLangMode"
  15. import debounce from "~/helpers/utils/debounce"
  16. export default defineComponent({
  17. props: {
  18. value: {
  19. type: String,
  20. default: "",
  21. },
  22. theme: {
  23. type: String,
  24. required: false,
  25. default: null,
  26. },
  27. onRunGQLQuery: {
  28. type: Function,
  29. default: () => {},
  30. },
  31. options: {
  32. type: Object,
  33. default: () => {},
  34. },
  35. styles: {
  36. type: String,
  37. default: "",
  38. },
  39. },
  40. data() {
  41. return {
  42. initialized: false,
  43. editor: null,
  44. cacheValue: "",
  45. validationSchema: null,
  46. }
  47. },
  48. computed: {
  49. appFontSize() {
  50. return getComputedStyle(document.documentElement).getPropertyValue(
  51. "--body-font-size"
  52. )
  53. },
  54. },
  55. watch: {
  56. value(value) {
  57. if (value !== this.cacheValue) {
  58. this.editor.session.setValue(value, 1)
  59. this.cacheValue = value
  60. }
  61. },
  62. theme() {
  63. this.initialized = false
  64. this.editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
  65. this.$nextTick().then(() => {
  66. this.initialized = true
  67. })
  68. })
  69. },
  70. options(value) {
  71. this.editor.setOptions(value)
  72. },
  73. },
  74. mounted() {
  75. defineGQLLanguageMode(ace)
  76. const langTools = ace.require("ace/ext/language_tools")
  77. const editor = ace.edit(this.$refs.editor, {
  78. mode: `ace/mode/gql-query`,
  79. enableBasicAutocompletion: true,
  80. enableLiveAutocompletion: true,
  81. ...this.options,
  82. })
  83. // Set the theme and show the editor only after it's been set to prevent FOUC.
  84. editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
  85. this.$nextTick().then(() => {
  86. this.initialized = true
  87. })
  88. })
  89. // Set the theme and show the editor only after it's been set to prevent FOUC.
  90. editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
  91. this.$nextTick().then(() => {
  92. this.initialized = true
  93. })
  94. })
  95. editor.setFontSize(this.appFontSize)
  96. const completer = {
  97. getCompletions: (
  98. editor,
  99. _session,
  100. { row, column },
  101. _prefix,
  102. callback
  103. ) => {
  104. if (this.validationSchema) {
  105. const completions = getAutocompleteSuggestions(
  106. this.validationSchema,
  107. editor.getValue(),
  108. {
  109. line: row,
  110. character: column,
  111. }
  112. )
  113. callback(
  114. null,
  115. completions.map(({ label, detail }) => ({
  116. name: label,
  117. value: label,
  118. score: 1.0,
  119. meta: detail,
  120. }))
  121. )
  122. } else {
  123. callback(null, [])
  124. }
  125. },
  126. }
  127. langTools.setCompleters([completer])
  128. if (this.value) editor.setValue(this.value, 1)
  129. this.editor = editor
  130. this.cacheValue = this.value
  131. editor.commands.addCommand({
  132. name: "runGQLQuery",
  133. exec: () => this.onRunGQLQuery(this.editor.getValue()),
  134. bindKey: {
  135. mac: "cmd-enter",
  136. win: "ctrl-enter",
  137. },
  138. })
  139. editor.commands.addCommand({
  140. name: "prettifyGQLQuery",
  141. exec: () => this.prettifyQuery(),
  142. bindKey: {
  143. mac: "cmd-p",
  144. win: "ctrl-p",
  145. },
  146. })
  147. editor.on("change", () => {
  148. const content = editor.getValue()
  149. this.$emit("input", content)
  150. this.parseContents(content)
  151. this.cacheValue = content
  152. })
  153. this.parseContents(this.value)
  154. },
  155. beforeDestroy() {
  156. this.editor.destroy()
  157. },
  158. methods: {
  159. prettifyQuery() {
  160. try {
  161. this.$emit("update-query", gql.print(gql.parse(this.editor.getValue())))
  162. } catch (e) {
  163. this.$toast.error(this.$t("error.gql_prettify_invalid_query"), {
  164. icon: "error_outline",
  165. })
  166. }
  167. },
  168. defineTheme() {
  169. if (this.theme) {
  170. return this.theme
  171. }
  172. const strip = (str) =>
  173. str.replace(/#/g, "").replace(/ /g, "").replace(/"/g, "")
  174. return strip(
  175. window
  176. .getComputedStyle(document.documentElement)
  177. .getPropertyValue("--editor-theme")
  178. )
  179. },
  180. setValidationSchema(schema) {
  181. this.validationSchema = schema
  182. this.parseContents(this.cacheValue)
  183. },
  184. parseContents: debounce(function (content) {
  185. if (content !== "") {
  186. try {
  187. const doc = gql.parse(content)
  188. if (this.validationSchema) {
  189. this.editor.session.setAnnotations(
  190. gql
  191. .validate(this.validationSchema, doc)
  192. .map(({ locations, message }) => ({
  193. row: locations[0].line - 1,
  194. column: locations[0].column - 1,
  195. text: message,
  196. type: "error",
  197. }))
  198. )
  199. }
  200. } catch (e) {
  201. this.editor.session.setAnnotations([
  202. {
  203. row: e.locations[0].line - 1,
  204. column: e.locations[0].column - 1,
  205. text: e.message,
  206. type: "error",
  207. },
  208. ])
  209. }
  210. } else {
  211. this.editor.session.setAnnotations([])
  212. }
  213. }, 2000),
  214. },
  215. })
  216. </script>
  217. <style scoped lang="scss">
  218. .show-if-initialized {
  219. &.initialized {
  220. @apply opacity-100;
  221. }
  222. & > * {
  223. @apply transition-none;
  224. }
  225. }
  226. </style>