Skip to content

Commit 0c8ea9c

Browse files
authored
Merge pull request #34 from BennieMeng/feature/ace-switch-error-notify
Feature/ace switch error notify
2 parents 9684c62 + 674ebfb commit 0c8ea9c

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

src/frontend/src/app/shared/ace-editor/ace-editor-box/ace-editor-box.component.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
<button style="margin-left: 10px" type="button" (click)="format()">
44
<clr-icon shape="view-list"></clr-icon>
55
</button>
6-
<select class="ace-select" [disabled]="!isValid" name="aceMode" (change)="aceModeChange()" [(ngModel)]="aceMode">
7-
<option value="ace/mode/json" selected>JSON</option>
8-
<option value="ace/mode/yaml">YAML</option>
9-
</select>
6+
<div class="select-box">
7+
<select class="ace-select" [disabled]="!isValid" name="aceMode" (change)="aceModeChange()" [(ngModel)]="aceMode">
8+
<option value="ace/mode/json" selected>JSON</option>
9+
<option value="ace/mode/yaml">YAML</option>
10+
</select>
11+
<div class="select-mask" *ngIf="!isValid" (click)="clickEvent()"></div>
12+
</div>
1013
</div>
1114
<div style="position: relative;height: 500px" #editorElement></div>
1215
</div>

src/frontend/src/app/shared/ace-editor/ace-editor-box/ace-editor-box.component.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ div.aceeditor-menu {
3131
width: 100%;
3232
min-height: 535px;
3333
}
34+
.select-box{
35+
display: inline-block;
36+
position: relative;
37+
.select-mask{
38+
position: absolute;
39+
top: 0;
40+
right: 0;
41+
bottom: 0;
42+
left: 0;
43+
opacity: 0;
44+
}
45+
}

src/frontend/src/app/shared/ace-editor/ace-editor-box/ace-editor-box.component.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as ace from 'brace';
66
import 'brace/mode/json';
77
import 'brace/mode/yaml';
88
import * as YAML from 'js-yaml';
9+
import {MessageHandlerService} from '../../message-handler/message-handler.service';
910

1011
@Component({
1112
selector: 'wayne-ace-editor-box',
@@ -22,7 +23,10 @@ export class AceEditorBoxComponent implements OnInit, OnDestroy {
2223
aceEditorMsgSub: Subscription;
2324
@Output() modalChange = new EventEmitter<any>();
2425

25-
constructor(public el: ElementRef, private aceEditorService: AceEditorService) {
26+
constructor(
27+
public el: ElementRef,
28+
private aceEditorService: AceEditorService,
29+
private messageHandle: MessageHandlerService) {
2630
}
2731

2832
ngOnInit() {
@@ -85,12 +89,14 @@ export class AceEditorBoxComponent implements OnInit, OnDestroy {
8589

8690
aceModeChange() {
8791
this.editor.getSession().setMode(this.aceMode);
88-
if (this.aceMode == 'ace/mode/json') {
89-
let obj = YAML.load(this.editor.getValue());
90-
this.editor.setValue(JSON.stringify(obj, null, 2));
91-
} else {
92-
let obj = JSON.parse(this.editor.getValue());
93-
this.editor.setValue(YAML.dump(obj));
92+
if (this.editor.getValue().trim() !== '') {
93+
if (this.aceMode == 'ace/mode/json') {
94+
let obj = YAML.load(this.editor.getValue());
95+
this.editor.setValue(JSON.stringify(obj, null, 2));
96+
} else {
97+
let obj = JSON.parse(this.editor.getValue());
98+
this.editor.setValue(YAML.dump(obj));
99+
}
94100
}
95101
localStorage.setItem('aceMode', this.aceMode);
96102
}
@@ -112,12 +118,17 @@ export class AceEditorBoxComponent implements OnInit, OnDestroy {
112118
if (this.aceMode == 'ace/mode/json') {
113119
return this.editor.getValue();
114120
} else {
115-
return JSON.stringify(YAML.load(this.editor.getValue()));
121+
return JSON.stringify(YAML.load(this.editor.getValue())) || '';
116122
}
117123
}
118124

125+
clickEvent() {
126+
this.messageHandle.showError('请检查文本格式是否正确');
127+
}
128+
119129
get isValid(): boolean {
120130
try {
131+
if (this.editor.getValue().trim() === '') return true;
121132
if (this.aceMode == 'ace/mode/json') {
122133
JSON.parse(this.editor.getValue())
123134
} else {

src/frontend/src/app/shared/global-message/message.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ export class MessageComponent implements OnInit, OnDestroy {
6262
message => {
6363
const index = this.infoList.length;
6464
message.state = 'show';
65-
this.posiChange();
65+
setTimeout(() => {
66+
this.posiChange();
67+
}, 0);
6668
this.infoList.push(message);
6769
this.timer[index] = setTimeout(() => {
6870
this.close(index);

0 commit comments

Comments
 (0)