Skip to content

Commit 762bdce

Browse files
committed
[us40] Use ace editor when displaying any script in the list of rules
1 parent 4973288 commit 762bdce

5 files changed

+37
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div #ruleScriptEdit class="ruleScriptEdit"></div>
1+
<div #ruleScriptEdit></div>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11

2-
.ruleScriptEdit {
3-
height: 200px;
4-
}

src/app/ace-editor/ace-editor.component.ts

+30-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as ace from "ace-builds";
33
import {Ace} from "ace-builds";
44
import {MatFormFieldControl} from "@angular/material/form-field";
55
import {Subject} from "rxjs";
6+
import {BooleanInput, coerceBooleanProperty} from "@angular/cdk/coercion";
67
import Editor = Ace.Editor;
78

89
@Component({
@@ -26,7 +27,6 @@ export class AceEditorComponent implements MatFormFieldControl<string>, OnInit {
2627
focused: boolean = false;
2728
empty: boolean = false;
2829
shouldLabelFloat: boolean = true;
29-
disabled: boolean = false;
3030
errorState: boolean = false;
3131
controlType?: string | undefined = "app-ace-editor";
3232
autofilled?: boolean | undefined;
@@ -44,22 +44,45 @@ export class AceEditorComponent implements MatFormFieldControl<string>, OnInit {
4444
private _required = false;
4545

4646
@Input()
47-
get required() {
47+
get required(): boolean {
4848
return this._required;
4949
}
5050

51-
set required(req: boolean) {
52-
this._required = req;
51+
set required(req: BooleanInput) {
52+
this._required = coerceBooleanProperty(req);
5353
this.stateChanges.next();
5454
}
5555

56+
private _disabled = false;
57+
58+
@Input()
59+
get disabled(): boolean {
60+
return this._disabled;
61+
}
62+
63+
set disabled(value: BooleanInput) {
64+
this._disabled = coerceBooleanProperty(value);
65+
this.stateChanges.next();
66+
}
5667

5768
ngOnInit() {
5869
if (this.ruleScriptEdit) {
59-
this.editor = ace.edit(this.ruleScriptEdit.nativeElement);
60-
this.editor.session.setMode("ace/mode/javascript");
70+
this.editor = ace.edit(this.ruleScriptEdit.nativeElement, {
71+
value: this.value,
72+
mode: "ace/mode/javascript",
73+
minLines: 2,
74+
maxLines: 15
75+
});
76+
77+
if (this.disabled) {
78+
this.editor.setReadOnly(true);
79+
this.editor.setHighlightActiveLine(false);
80+
this.editor.setHighlightGutterLine(false);
81+
// hide the cursor
82+
// @ts-ignore
83+
this.editor.renderer.$cursorLayer.element.style.display = "none"
84+
}
6185

62-
this.editor.setValue(this.value);
6386
this.editor.on("input", () => {
6487
this.valueChange.emit(this.editor?.getValue());
6588
this.stateChanges.next();

src/app/rules/rules.component.html

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h1>Setup rules</h1>
3434
<mat-form-field class="fullWidth">
3535
<mat-label>Script</mat-label>
3636
<!-- TODO: Also use for viewing-->
37-
<app-ace-editor [(value)]="ruleToCreateOrUpdate.script" [required]="true" name="script"></app-ace-editor>
37+
<app-ace-editor [(value)]="ruleToCreateOrUpdate.script" name="script" required></app-ace-editor>
3838
</mat-form-field>
3939
<div class="actionButtons">
4040
<button (click)="cancelCreateOrUpdate()" mat-raised-button>Cancel</button>
@@ -54,9 +54,8 @@ <h1>Setup rules</h1>
5454
*ngIf="!first"> &nbsp;&gt; </span>{{ cat }}</span>
5555
</mat-panel-description>
5656
</mat-expansion-panel-header>
57-
<div class="ruleScript">
58-
{{ rule.script }}
59-
</div>
57+
<app-ace-editor [value]="rule.script" class="ruleScript" disabled>
58+
</app-ace-editor>
6059
<div class="ruleButtons">
6160
<button (click)="update(rule)" mat-raised-button>Edit</button>
6261
&nbsp;

src/app/rules/rules.component.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {AppModule} from "../app.module";
44
import {mock, when} from "strong-mock";
55
import {MatButtonHarness} from "@angular/material/button/testing";
66
import {HarnessLoader, TestKey} from "@angular/cdk/testing";
7-
import {ComponentFixture, fakeAsync, tick} from "@angular/core/testing";
7+
import {ComponentFixture, fakeAsync, flush, tick} from "@angular/core/testing";
88
import {TestbedHarnessEnvironment} from "@angular/cdk/testing/testbed";
99
import {MatInputHarness} from "@angular/material/input/testing";
1010
import {MatFormFieldHarness} from "@angular/material/form-field/testing";
@@ -67,6 +67,7 @@ describe('RulesComponent', () => {
6767
// Assert
6868
tick();
6969
fixture.detectChanges();
70+
flush();
7071
expect(Page.getRuleNames()).toEqual(['Electric bill', 'Bank account statement']);
7172
expect(Page.getRuleCategory('Electric bill')).toEqual('Electricity  > Bills');
7273
expect(Page.getRuleScript('Electric bill')).toEqual('return fileName === "electricity_bill.pdf"');
@@ -220,8 +221,7 @@ class Page {
220221
let rule = ngMocks.findAll("mat-panel-title")
221222
.find(row => row.nativeNode.textContent.trim() === name)
222223
?.parent?.parent;
223-
return ngMocks.find(rule, '.ruleScript')
224-
.nativeNode.textContent.trim();
224+
return ngMocks.find(rule, AceEditorComponent).componentInstance.editor?.getValue() ?? '';
225225
}
226226

227227
async clickOnCreateNewRule() {

0 commit comments

Comments
 (0)