-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
112 lines (94 loc) · 3.26 KB
/
editor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2013 - present UTN-LIS
'use strict';
require.config({
paths: {
jquery: 'node_modules/jquery/dist/jquery.min',
bootstrap: 'node_modules/bootstrap/dist/js/bootstrap',
cm: 'node_modules/codemirror/',
pumascript: 'src/pumascript'
},
shim: {
bootstrap: {
exports: 'bootstrap',
deps: ['jquery']
}
}
});
require([
'jquery',
'cm/lib/codemirror',
'pumascript',
'bootstrap',
'cm/addon/hint/show-hint',
'cm/addon/hint/xml-hint',
'cm/addon/hint/html-hint',
'cm/addon/hint/javascript-hint',
'cm/addon/fold/foldcode',
'cm/addon/fold/foldgutter',
'cm/addon/fold/brace-fold',
'cm/mode/xml/xml',
'cm/mode/javascript/javascript',
'cm/mode/css/css',
'cm/mode/htmlmixed/htmlmixed'
], function ($, CodeMirror, puma) {
function PumaEditor() {
this._javaScriptEditor = this.instantiateCodeMirrorEditor('javascript');
this._pumaScriptEditor = this.instantiateCodeMirrorEditor('puma');
this.registerEvents();
}
PumaEditor.prototype.instantiateCodeMirrorEditor = function (section) {
return CodeMirror(document.getElementById(section), {
mode: 'javascript',
extraKeys: {
'Ctrl-Space': 'autocomplete',
'Ctrl-J': 'autocomplete',
'Ctrl-Q': function (cm) {
cm.foldCode(cm.getCursor());
}
},
lineNumbers: true,
lineWrapping: true,
indentUnit: 4,
tabSize: 4,
foldGutter: true,
gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter']
});
};
PumaEditor.prototype.editorValue = function (editor) {
return editor.getValue() || '';
};
PumaEditor.prototype.setEditorValue = function (editor, value) {
return editor.setValue(value);
};
PumaEditor.prototype.load = function () {
var value = localStorage.getItem('puma') || '';
this.setEditorValue(this._pumaScriptEditor, value);
};
PumaEditor.prototype.loadBackup = function () {
var value = localStorage.getItem('puma-backup') || '';
this.setEditorValue(this._pumaScriptEditor, value);
};
PumaEditor.prototype.translate = function () {
var result = {};
var programStr = this.editorValue(this._pumaScriptEditor);
//Save the last execution in local storage
if (programStr !== '') {
localStorage.setItem('puma-backup', localStorage.getItem('puma'));
localStorage.setItem('puma', programStr);
}
if (programStr !== undefined && programStr !== null) {
//Exporting to window puma runtime
window.puma = puma;
result = puma.evalPuma(programStr);
this.setEditorValue(this._javaScriptEditor, result.output);
console.log(result);
}
};
PumaEditor.prototype.registerEvents = function () {
$('#translatePuma').click(this.translate.bind(this));
$('#loadPuma').click(this.load.bind(this));
$('#loadBackupPuma').click(this.loadBackup.bind(this));
};
// eslint-disable-next-line no-unused-vars
var pumaEditorInstance = new PumaEditor();
});