JsEditor.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <div class="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 esprima from "esprima"
  12. import { defineComponent } from "@nuxtjs/composition-api"
  13. import debounce from "~/helpers/utils/debounce"
  14. import {
  15. getPreRequestScriptCompletions,
  16. getTestScriptCompletions,
  17. performPreRequestLinting,
  18. performTestLinting,
  19. } from "~/helpers/tern"
  20. export default defineComponent({
  21. props: {
  22. value: {
  23. type: String,
  24. default: "",
  25. },
  26. theme: {
  27. type: String,
  28. required: false,
  29. default: null,
  30. },
  31. options: {
  32. type: Object,
  33. default: () => {},
  34. },
  35. styles: {
  36. type: String,
  37. default: "",
  38. },
  39. completeMode: {
  40. type: String,
  41. required: true,
  42. default: "none",
  43. },
  44. },
  45. data() {
  46. return {
  47. initialized: false,
  48. editor: null,
  49. cacheValue: "",
  50. }
  51. },
  52. computed: {
  53. appFontSize() {
  54. return getComputedStyle(document.documentElement).getPropertyValue(
  55. "--body-font-size"
  56. )
  57. },
  58. },
  59. watch: {
  60. value(value) {
  61. if (value !== this.cacheValue) {
  62. this.editor.session.setValue(value, 1)
  63. this.cacheValue = value
  64. if (this.lint) this.provideLinting(value)
  65. }
  66. },
  67. theme() {
  68. this.initialized = false
  69. this.editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
  70. this.$nextTick()
  71. .then(() => {
  72. this.initialized = true
  73. })
  74. .catch(() => {
  75. // nextTick shouldn't really ever throw but still
  76. this.initialized = true
  77. })
  78. })
  79. },
  80. options(value) {
  81. this.editor.setOptions(value)
  82. },
  83. },
  84. mounted() {
  85. // const langTools = ace.require("ace/ext/language_tools")
  86. const editor = ace.edit(this.$refs.editor, {
  87. mode: `ace/mode/javascript`,
  88. enableBasicAutocompletion: true,
  89. enableLiveAutocompletion: true,
  90. ...this.options,
  91. })
  92. // Set the theme and show the editor only after it's been set to prevent FOUC.
  93. editor.setTheme(`ace/theme/${this.defineTheme()}`, () => {
  94. this.$nextTick()
  95. .then(() => {
  96. this.initialized = true
  97. })
  98. .catch(() => {
  99. // nextTIck shouldn't really ever throw but still
  100. this.initialized = true
  101. })
  102. })
  103. editor.setFontSize(this.appFontSize)
  104. const completer = {
  105. getCompletions: (
  106. editor,
  107. _session,
  108. { row, column },
  109. _prefix,
  110. callback
  111. ) => {
  112. if (this.completeMode === "pre") {
  113. getPreRequestScriptCompletions(editor.getValue(), row, column)
  114. .then((res) => {
  115. callback(
  116. null,
  117. res.completions.map((r, index, arr) => ({
  118. name: r.name,
  119. value: r.name,
  120. score: (arr.length - index) / arr.length,
  121. meta: r.type,
  122. }))
  123. )
  124. })
  125. .catch(() => callback(null, []))
  126. } else if (this.completeMode === "test") {
  127. getTestScriptCompletions(editor.getValue(), row, column)
  128. .then((res) => {
  129. callback(
  130. null,
  131. res.completions.map((r, index, arr) => ({
  132. name: r.name,
  133. value: r.name,
  134. score: (arr.length - index) / arr.length,
  135. meta: r.type,
  136. }))
  137. )
  138. })
  139. .catch(() => callback(null, []))
  140. }
  141. },
  142. }
  143. editor.completers = [completer]
  144. if (this.value) editor.setValue(this.value, 1)
  145. this.editor = editor
  146. this.cacheValue = this.value
  147. editor.on("change", () => {
  148. const content = editor.getValue()
  149. this.$emit("input", content)
  150. this.cacheValue = content
  151. this.provideLinting(content)
  152. })
  153. this.provideLinting(this.value)
  154. },
  155. destroyed() {
  156. this.editor.destroy()
  157. },
  158. methods: {
  159. defineTheme() {
  160. if (this.theme) {
  161. return this.theme
  162. }
  163. const strip = (str) =>
  164. str.replace(/#/g, "").replace(/ /g, "").replace(/"/g, "")
  165. return strip(
  166. window
  167. .getComputedStyle(document.documentElement)
  168. .getPropertyValue("--editor-theme")
  169. )
  170. },
  171. provideLinting: debounce(function (code) {
  172. let results = []
  173. const lintFunc =
  174. this.completeMode === "pre"
  175. ? performPreRequestLinting
  176. : performTestLinting
  177. lintFunc(code)
  178. .then((semanticLints) => {
  179. results = results.concat(
  180. semanticLints.map((lint) => ({
  181. row: lint.from.line,
  182. column: lint.from.ch,
  183. text: `[semantic] ${lint.message}`,
  184. type: "error",
  185. }))
  186. )
  187. try {
  188. const res = esprima.parseScript(code, { tolerant: true })
  189. if (res.errors && res.errors.length > 0) {
  190. results = results.concat(
  191. res.errors.map((err) => {
  192. const pos = this.editor.session
  193. .getDocument()
  194. .indexToPosition(err.index, 0)
  195. return {
  196. row: pos.row,
  197. column: pos.column,
  198. text: `[syntax] ${err.description}`,
  199. type: "error",
  200. }
  201. })
  202. )
  203. }
  204. } catch (e) {
  205. const pos = this.editor.session
  206. .getDocument()
  207. .indexToPosition(e.index, 0)
  208. results = results.concat([
  209. {
  210. row: pos.row,
  211. column: pos.column,
  212. text: `[syntax] ${e.description}`,
  213. type: "error",
  214. },
  215. ])
  216. }
  217. this.editor.session.setAnnotations(results)
  218. })
  219. .catch(() => {
  220. try {
  221. const res = esprima.parseScript(code, { tolerant: true })
  222. if (res.errors && res.errors.length > 0) {
  223. results = results.concat(
  224. res.errors.map((err) => {
  225. const pos = this.editor.session
  226. .getDocument()
  227. .indexToPosition(err.index, 0)
  228. return {
  229. row: pos.row,
  230. column: pos.column,
  231. text: `[syntax] ${err.description}`,
  232. type: "error",
  233. }
  234. })
  235. )
  236. }
  237. } catch (e) {
  238. const pos = this.editor.session
  239. .getDocument()
  240. .indexToPosition(e.index, 0)
  241. results = results.concat([
  242. {
  243. row: pos.row,
  244. column: pos.column,
  245. text: `[syntax] ${e.description}`,
  246. type: "error",
  247. },
  248. ])
  249. }
  250. this.editor.session.setAnnotations(results)
  251. })
  252. }, 2000),
  253. },
  254. })
  255. </script>
  256. <style scoped lang="scss">
  257. .show-if-initialized {
  258. &.initialized {
  259. @apply opacity-100;
  260. }
  261. & > * {
  262. @apply transition-none;
  263. }
  264. }
  265. </style>