Skip to content

Instantly share code, notes, and snippets.

@taikulawo
Created September 19, 2018 05:20
Show Gist options
  • Save taikulawo/c55e6cc9a977bc2596d51cc3d2392b8b to your computer and use it in GitHub Desktop.
Save taikulawo/c55e6cc9a977bc2596d51cc3d2392b8b to your computer and use it in GitHub Desktop.
'use strict'
import { config, edit } from 'ace-builds'
import store from './store'
import Vue from 'vue'
import eventBus from './eventbus.js'
class IDE {
constructor (id) {
config.set('basePath', 'static/js/ace')
this.aceEditor = edit(id)
this.aceSession = this.aceEditor.session
this.aceSession.setMode('ace/mode/golang')
this.aceEditor.setTheme('ace/theme/chrome')
this.aceEditor.setShowPrintMargin(false)
this.currentLang = 'golang'
this.currentLangSuffix = '.go'
this.$http = Vue.prototype.$http
this.$terminal = store.state.global.terminal
this.compilePath = 'api/compile'
this.aceEditor.setFontSize(14)
}
resize () {
this.aceEditor.resize()
}
run () {
let codes = btoa(this.aceEditor.getValue())
let request = this.packRequest(codes)
this.$http.post(this.compilePath, request, {
headers: {
'Content-Type': 'application/json'
}
}).then(res => {
eventBus.$emit('run-exit', () => {})
let response = res.data
this.$terminal.renderResult(response)
}).catch((err) => {
console.log(err)
eventBus.$emit('run-exit')
let e = {
Error: 'Server Error',
Events: null
}
this.$terminal.renderResult(e)
})
}
attachDocument (document) {
this.aceSession.setDocument(document)
document.on('change', function (o) {
eventBus.$emit('document-change', o)
})
}
disableEditorSelect () {
this.aceEditor.setOption('dragEnabled', false)
}
enableEditorSelect () {
this.aceEditor.setOption('dragEnabled', true)
}
setMode (mode) {
this.aceSession.setMode(mode)
}
setTheme (theme) {
this.aceEditor.setTheme(theme)
}
setFontSize (value) {
this.aceEditor.setFontSize(value)
}
test () {
console.log('THIS IS A TEST FUNCTION TO TEST REFERENCE')
}
setLangSuffix (langSuffix) {
this.currentLangSuffix = langSuffix
}
packRequest () {
let sourceCode = btoa(this.aceEditor.getValue())
let suffix = this.currentLangSuffix
let fileName = 'main' + suffix
let request = {
Version: '1',
Files: {
[fileName]: {
src: sourceCode,
type: 'file'
}
},
Lang: this.currentLang
}
return request
}
}
const _IDE = IDE
export { _IDE as IDE }
/*
response json format
from golang struct
type response strcut{
Error string
Events []Event
}
type Event struct{
Message string
Kind string
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment