-
Notifications
You must be signed in to change notification settings - Fork 2
/
CodePane.vue
196 lines (180 loc) · 5.77 KB
/
CodePane.vue
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<template>
<div class="codepane">
<nav class="navbar navbar-expand-md navbar-light bg-light">
<form class="form-inline">
<input v-model="branch" class="form-control form-control-sm mr-2" />
<select class="custom-select custom-select-sm mr-2" v-model="module">
<option v-for="(data, file) in modules" :value="file">{{file}}</option>
</select>
</form>
<div>
<button @click.prevent="add()" class="btn btn-sm btn-primary">Add</button>
<button @click.prevent="remove()" class="btn btn-sm btn-primary">Delete</button>
<button @click.prevent="load()" class="btn btn-sm btn-primary">Load all</button>
<button @click.prevent="save()" class="btn btn-sm btn-primary">Save all</button>
</div>
</nav>
<codemirror
ref="editor"
:code="code"
:options="editorOptions"
@change="changeCode">
</codemirror>
<!-- Add file -->
<div class="modal show" tabindex="-1" v-if="isAddVisible">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add file</h5>
<button type="button" class="close" @click="isAddVisible = false">
<span>×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" v-model="addName" placeholder="File name">
</div>
</div>
</form>
<div class="invalid-feedback" v-if="addError">{{addError}}</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" @click="isAddVisible = false">Cancel</button>
<button type="button" class="btn btn-primary" @click="addConfirm()">Add</button>
</div>
</div>
</div>
</div>
<div class="modal-backdrop show" v-if="isAddVisible"></div>
</div>
</template>
<script>
import eventBus from '../global-events';
import VueCodemirror from 'vue-codemirror';
export default {
data() {
return {
branch: 'default',
modules: {},
module: 'main',
isAddVisible: false,
addError: '',
addName: '',
editorOptions: {
theme: 'monokai',
lineNumbers: true,
line: true,
// keyMap: "sublime",
foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
styleSelectedText: true,
highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: true}
}
}
},
computed: {
code() {
if (eventBus.api && eventBus.api.modules && this.moduleNames.length === 0) {
this.modules = eventBus.api.modules;
}
if (this.moduleNames.length === 0) {
return '// The code is not loaded. Click "Load all" button.';
}
return this.modules[this.module] || '';
},
moduleNames() {
return Object.keys(this.modules);
}
},
methods: {
async load() {
let r = await eventBus.api.code(this.branch);
let {modules} = r;
if (modules)
this.modules = modules;
else
this.modules = {};
eventBus.api.modules = this.modules;
if (!this.modules[this.module]) {
let files = Object.keys(this.modules);
this.module = files.length ? files[0] : '';
}
},
add() {
this.isAddVisible = true;
this.addName = '';
this.addError = '';
},
addConfirm() {
var name = this.addName.trim();
if (name === '') {
this.addError = 'Invalid name';
return;
}
if (this.modules[name] !== undefined) {
this.addError = 'File exists';
return;
}
this.modules[name] = '';
this.module = name;
this.isAddVisible = false;
},
remove() {
if (this.module === 'main') {
if (this.branch !== 'default') {
this.isDeleteBranchVisible = true;
return;
}
return;
}
var files = Object.keys(this.modules);
var index = files.indexOf(this.module);
files = files.filter(module => module !== this.module);
var newModules = {};
files.forEach(module => newModules[module] = this.modules[module]);
this.modules = newModules;
this.module = files[index === files.length ? index - 1 : index];
},
save() {
eventBus.api.setCode(this.branch, this.modules);
},
changeCode(newCode) {
if (!this.modules[this.module] && newCode === '')
return;
this.$set(this.modules, this.module, newCode);
}
},
components: {
codemirror: VueCodemirror.codemirror,
}
}
</script>
<style>
.codepane {
flex: 1 1;
flex-direction: column;
min-height: 150px;
}
.codepane .navbar {
margin-bottom: .25rem;
margin-top: .25rem;
padding-bottom: 0;
padding-top: 0;
}
.codepane .navbar button,
.codepane .navbar input,
.codepane .navbar select {
margin-bottom: .25rem;
margin-top: .25rem;
}
.codepane .CodeMirror {
height: 100%;
font-size: 12px;
}
.codepane select {
max-width: 150px;
}
</style>