Skip to content

Commit 947fae2

Browse files
committed
refactor(code-editor): update options in ngOnChanges
1 parent ffaedf9 commit 947fae2

File tree

1 file changed

+59
-112
lines changed

1 file changed

+59
-112
lines changed

projects/code-editor/code-editor.ts

Lines changed: 59 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import {
44
ElementRef,
55
EventEmitter,
66
Input,
7+
OnChanges,
78
OnDestroy,
89
OnInit,
910
Output,
11+
SimpleChanges,
1012
ViewEncapsulation,
1113
booleanAttribute,
1214
forwardRef,
@@ -51,7 +53,7 @@ export const External = Annotation.define<boolean>();
5153
},
5254
],
5355
})
54-
export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
56+
export class CodeEditor implements OnChanges, OnInit, OnDestroy, ControlValueAccessor {
5557
/**
5658
* EditorView's [root](https://codemirror.net/docs/ref/#view.EditorView.root).
5759
*
@@ -66,96 +68,32 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
6668
*/
6769
@Input({ transform: booleanAttribute }) autoFocus = false;
6870

71+
/** The editor's value. */
72+
@Input() value = '';
73+
6974
/** Whether the editor is disabled. */
7075
@Input({ transform: booleanAttribute }) disabled = false;
7176

7277
/** Whether the editor is readonly. */
73-
@Input({ transform: booleanAttribute })
74-
get readonly() {
75-
return this._readonly;
76-
}
77-
set readonly(value: boolean) {
78-
this._readonly = value;
79-
this.setReadonly(value);
80-
}
81-
private _readonly = false;
78+
@Input({ transform: booleanAttribute }) readonly = false;
8279

83-
/** Editor's value. */
84-
@Input()
85-
get value() {
86-
return this._value;
87-
}
88-
set value(newValue: string) {
89-
this._value = newValue;
90-
this.setValue(newValue);
91-
}
92-
private _value = '';
80+
/** The editor's theme. */
81+
@Input() theme: Theme = 'light';
9382

94-
/** Editor's theme. */
95-
@Input()
96-
get theme() {
97-
return this._theme;
98-
}
99-
set theme(value: Theme) {
100-
this._theme = value;
101-
this.setTheme(value);
102-
}
103-
private _theme: Theme = 'light';
104-
105-
/** Editor's placecholder. */
106-
@Input()
107-
get placeholder() {
108-
return this._placeholder;
109-
}
110-
set placeholder(value: string) {
111-
this._placeholder = value;
112-
this.setPlaceholder(value);
113-
}
114-
private _placeholder = '';
83+
/** The editor's placecholder. */
84+
@Input() placeholder = '';
11585

11686
/** Whether indent with Tab key. */
117-
@Input({ transform: booleanAttribute })
118-
get indentWithTab() {
119-
return this._indentWithTab;
120-
}
121-
set indentWithTab(value: boolean) {
122-
this._indentWithTab = value;
123-
this.setIndentWithTab(value);
124-
}
125-
private _indentWithTab = false;
87+
@Input({ transform: booleanAttribute }) indentWithTab = false;
12688

12789
/** Should be a string consisting either entirely of the same whitespace character. */
128-
@Input()
129-
get indentUnit() {
130-
return this._indentUnit;
131-
}
132-
set indentUnit(value: string) {
133-
this._indentUnit = value;
134-
this.setIndentUnit(value);
135-
}
136-
private _indentUnit = '';
90+
@Input() indentUnit = '';
13791

13892
/** Whether the editor wraps lines. */
139-
@Input({ transform: booleanAttribute })
140-
get lineWrapping() {
141-
return this._lineWrapping;
142-
}
143-
set lineWrapping(value: boolean) {
144-
this._lineWrapping = value;
145-
this.setLineWrapping(value);
146-
}
147-
private _lineWrapping = false;
93+
@Input({ transform: booleanAttribute }) lineWrapping = false;
14894

14995
/** Whether highlight the whitespace. */
150-
@Input({ transform: booleanAttribute })
151-
get highlightWhitespace() {
152-
return this._highlightWhitespace;
153-
}
154-
set highlightWhitespace(value: boolean) {
155-
this._highlightWhitespace = value;
156-
this.setHighlightWhitespace(value);
157-
}
158-
private _highlightWhitespace = false;
96+
@Input({ transform: booleanAttribute }) highlightWhitespace = false;
15997

16098
/**
16199
* An array of language descriptions for known
@@ -165,45 +103,21 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
165103
*/
166104
@Input() languages: LanguageDescription[] = [];
167105

168-
/** Editor's language. You should set the `languages` prop at first. */
169-
@Input()
170-
get language() {
171-
return this._language;
172-
}
173-
set language(value: string) {
174-
this._language = value;
175-
this.setLanguage(value);
176-
}
177-
private _language = '';
106+
/** The editor's language. You should set the `languages` prop at first. */
107+
@Input() language = '';
178108

179109
/**
180110
* The editor's built-in setup. The value can be set to
181111
* [`basic`](https://codemirror.net/docs/ref/#codemirror.basicSetup),
182112
* [`minimal`](https://codemirror.net/docs/ref/#codemirror.minimalSetup) or `null`.
183113
*/
184-
@Input()
185-
get setup() {
186-
return this._setup;
187-
}
188-
set setup(value: Setup) {
189-
this._setup = value;
190-
this.setExtensions(this._getAllExtensions());
191-
}
192-
private _setup: Setup = 'basic';
114+
@Input() setup: Setup = 'basic';
193115

194116
/**
195117
* It will be appended to the root
196118
* [extensions](https://codemirror.net/docs/ref/#state.EditorStateConfig.extensions).
197119
*/
198-
@Input()
199-
get extensions() {
200-
return this._extensions;
201-
}
202-
set extensions(value: Extension[]) {
203-
this._extensions = value;
204-
this.setExtensions(this._getAllExtensions());
205-
}
206-
private _extensions: Extension[] = [];
120+
@Input() extensions: Extension[] = [];
207121

208122
/** Event emitted when the editor's value changes. */
209123
@Output() change = new EventEmitter<string>();
@@ -264,6 +178,39 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
264178
];
265179
}
266180

181+
ngOnChanges(changes: SimpleChanges): void {
182+
if (changes['value']) {
183+
this.setValue(this.value);
184+
}
185+
if (changes['readonly']) {
186+
this.setReadonly(this.readonly);
187+
}
188+
if (changes['theme']) {
189+
this.setTheme(this.theme);
190+
}
191+
if (changes['placeholder']) {
192+
this.setPlaceholder(this.placeholder);
193+
}
194+
if (changes['indentWithTab']) {
195+
this.setIndentWithTab(this.indentWithTab);
196+
}
197+
if (changes['indentUnit']) {
198+
this.setIndentUnit(this.indentUnit);
199+
}
200+
if (changes['lineWrapping']) {
201+
this.setLineWrapping(this.lineWrapping);
202+
}
203+
if (changes['highlightWhitespace']) {
204+
this.setHighlightWhitespace(this.highlightWhitespace);
205+
}
206+
if (changes['language']) {
207+
this.setLanguage(this.language);
208+
}
209+
if (changes['setup'] || changes['extensions']) {
210+
this.setExtensions(this._getAllExtensions());
211+
}
212+
}
213+
267214
ngOnInit(): void {
268215
this.view = new EditorView({
269216
root: this.root,
@@ -319,6 +266,13 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
319266
this.setEditable(!isDisabled);
320267
}
321268

269+
/** Sets editor's value. */
270+
setValue(value: string) {
271+
this.view?.dispatch({
272+
changes: { from: 0, to: this.view.state.doc.length, insert: value },
273+
});
274+
}
275+
322276
private _dispatchEffects(effects: StateEffect<any> | readonly StateEffect<any>[]) {
323277
return this.view?.dispatch({ effects });
324278
}
@@ -328,13 +282,6 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
328282
this._dispatchEffects(StateEffect.reconfigure.of(value));
329283
}
330284

331-
/** Sets editor's value. */
332-
setValue(value: string) {
333-
this.view?.dispatch({
334-
changes: { from: 0, to: this.view.state.doc.length, insert: value },
335-
});
336-
}
337-
338285
/** Sets editor's editable state. */
339286
setEditable(value: boolean) {
340287
this._dispatchEffects(this._editableConf.reconfigure(EditorView.editable.of(value)));

0 commit comments

Comments
 (0)