github.com/machinebox/remoto@v0.1.2-0.20191024144331-eff21a7d321f/console/www/src/editor.js (about) 1 import $ from 'jquery' 2 require('../node_modules/ace-builds/src-noconflict/ace.js') 3 4 $(function(){ 5 6 var textarea = $('textarea[data-editor]') 7 var editor = ace.edit("editor") 8 ace.config.set("modePath", "/static") 9 ace.config.set("workerPath", "/static") 10 ace.config.set("themePath", "/static") 11 editor.setTheme("ace/theme/monokai") 12 editor.session.setMode("ace/mode/golang") 13 var form = textarea.closest('form') 14 var validate = debounce(function(src){ 15 $.ajax({ 16 method: 'post', url: '/api/define', 17 data: {"definition": src} 18 }).then(function(response){ 19 if (response.ok) { 20 $('[data-definition-valid="true"]').show() 21 $('[data-definition-valid="false"]').hide() 22 $('[data-definition-invalid]').prop('disabled', false) 23 $('[data-definition-valid-error]').text('') 24 console.info(response) 25 return 26 } 27 // clean up the error message a bit, the filename for example isn't 28 // actually useful. 29 var pos = response.error.indexOf('io.Reader.go:') 30 if (pos > -1) { 31 response.error = response.error.substr(pos+('io.Reader.go:'.length)) 32 } 33 response.error = response.error.replace(/^io.Reader.go:/, '') 34 pos = response.error.indexOf(':') 35 var line = parseInt(response.error.substr(0, pos)) 36 pos = response.error.indexOf(':', pos+1) 37 var col = parseInt(response.error.substr(0, pos)) 38 $('[data-definition-valid="true"]').hide() 39 $('[data-definition-valid="false"]').show() 40 $('[data-definition-invalid]').prop('disabled', true) 41 $('[data-definition-valid-error]') 42 .text(response.error || "An unknown error occurred") 43 .attr('href', '#') 44 .click(function(e){ 45 e.preventDefault() 46 editor.gotoLine(line) 47 }) 48 }).catch(function(error){ 49 $('[data-definition-valid="true"]').hide() 50 $('[data-definition-valid="false"]').show() 51 console.info(error) 52 $('[data-definition-valid-error]').text(error.responseText || "An unknown error occurred").attr('href', '') 53 }) 54 }, 500) 55 editor.getSession().on("change", function () { 56 var src = editor.getSession().getValue() 57 textarea.val(src) 58 $('[data-definition-valid="true"]').hide() 59 $('[data-definition-valid="false"]').hide() 60 validate(src) 61 }); 62 63 // validate whatever's rendered into the editor 64 var src = editor.getSession().getValue() 65 textarea.val(src) 66 $('[data-definition-valid="true"]').hide() 67 $('[data-definition-valid="false"]').hide() 68 validate(src) 69 70 // Returns a function, that, as long as it continues to be invoked, will not 71 // be triggered. The function will be called after it stops being called for 72 // N milliseconds. If `immediate` is passed, trigger the function on the 73 // leading edge, instead of the trailing. 74 function debounce(func, wait, immediate) { 75 var timeout; 76 return function() { 77 var context = this, args = arguments; 78 var later = function() { 79 timeout = null; 80 if (!immediate) func.apply(context, args); 81 }; 82 var callNow = immediate && !timeout; 83 clearTimeout(timeout); 84 timeout = setTimeout(later, wait); 85 if (callNow) func.apply(context, args); 86 }; 87 }; // from https://davidwalsh.name/javascript-debounce-function 88 89 })