Skip to content

Commit 7e14c7f

Browse files
committed
feat(ui): validation in RichEditor
1 parent 45419dd commit 7e14c7f

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

web/src/components/EnvironmentForm.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147

148148
<RichEditor
149149
v-model="json"
150+
type="json"
150151
v-if="extraVarsEditMode === 'json'"
151152
style="
152153
position: absolute;

web/src/components/InventoryForm.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128

129129
<RichEditor
130130
v-model.trim="item.inventory"
131+
type="ini"
131132
style="
132133
position: absolute;
133134
right: 0;

web/src/components/RichEditor.vue

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,44 @@
2020
dark
2121
fab
2222
small
23-
color="blue-grey"
23+
color="success"
24+
style="
25+
position: absolute;
26+
right: 50px;
27+
top: 0;
28+
margin: 10px;
29+
"
30+
@click="save()"
31+
>
32+
<v-icon>mdi-check</v-icon>
33+
</v-btn>
34+
35+
<v-btn
36+
dark
37+
fab
38+
small
39+
color="error"
2440
style="
2541
position: absolute;
2642
right: 0;
2743
top: 0;
2844
margin: 10px;
2945
"
30-
@click="closeDialog()"
46+
@click="cancel()"
3147
>
32-
<v-icon>mdi-arrow-collapse</v-icon>
48+
<v-icon>mdi-close</v-icon>
3349
</v-btn>
50+
51+
<v-alert
52+
v-model="hasError"
53+
dismissible
54+
style="
55+
position: absolute;
56+
bottom: 0;
57+
left: 50%;
58+
transform: translateX(-50%);
59+
"
60+
>{{ errorMessage }}</v-alert>
3461
</div>
3562
</v-dialog>
3663

@@ -53,10 +80,12 @@ import { codemirror } from 'vue-codemirror';
5380
import 'codemirror/lib/codemirror.css';
5481
import 'codemirror/mode/vue/vue.js';
5582
import 'codemirror/addon/display/placeholder.js';
83+
import { getErrorMessage } from '@/lib/error';
5684
5785
export default {
5886
props: {
5987
value: String,
88+
type: String,
6089
},
6190
6291
components: {
@@ -81,23 +110,66 @@ export default {
81110
82111
data() {
83112
return {
113+
text: null,
114+
envEditorDialog: false,
115+
errorMessage: null,
116+
};
117+
},
84118
85-
cmOptions: {
119+
computed: {
120+
hasError: {
121+
get() {
122+
return this.errorMessage != null;
123+
},
124+
set(value) {
125+
if (!value) {
126+
this.errorMessage = null;
127+
}
128+
},
129+
},
130+
131+
cmOptions() {
132+
return {
86133
tabSize: 2,
87134
mode: 'application/json',
88135
lineNumbers: true,
89136
line: true,
90137
lint: true,
91138
indentWithTabs: false,
92-
},
93-
94-
text: null,
95-
envEditorDialog: false,
96-
};
139+
};
140+
},
97141
},
98142
99143
methods: {
100-
closeDialog() {
144+
cancel() {
145+
this.errorMessage = null;
146+
this.text = this.value;
147+
this.envEditorDialog = false;
148+
},
149+
save() {
150+
this.errorMessage = null;
151+
switch (this.type) {
152+
case 'json':
153+
try {
154+
JSON.parse(this.text);
155+
} catch (e) {
156+
this.errorMessage = getErrorMessage(e);
157+
return;
158+
}
159+
break;
160+
case 'json_array':
161+
try {
162+
const res = JSON.parse(this.text);
163+
if (!Array.isArray(res)) {
164+
throw new Error('Must be JSON array');
165+
}
166+
} catch (e) {
167+
this.errorMessage = getErrorMessage(e);
168+
return;
169+
}
170+
break;
171+
default:
172+
}
101173
if (this.text !== this.value) {
102174
this.$emit('input', this.text);
103175
}

web/src/components/TemplateForm.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@
341341

342342
<RichEditor
343343
v-model="argsJson"
344+
type="json_array"
344345
style="
345346
position: absolute;
346347
right: -23px;

0 commit comments

Comments
 (0)