Skip to content

Commit d4d38e2

Browse files
committed
fix: fix prop not working when init
1 parent 74a8373 commit d4d38e2

File tree

1 file changed

+64
-29
lines changed

1 file changed

+64
-29
lines changed

projects/code-editor/code-editor.ts

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -62,79 +62,104 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
6262

6363
/** Editor's theme. */
6464
@Input()
65+
get theme() {
66+
return this._theme;
67+
}
6568
set theme(value: Theme) {
69+
this._theme = value;
6670
this._dispatchEffects(
6771
this._themeConf.reconfigure(value === 'light' ? [] : value === 'dark' ? oneDark : value)
6872
);
6973
}
74+
private _theme: Theme = 'light';
7075

7176
/** Editor's placecholder. */
7277
@Input()
78+
get placeholder() {
79+
return this._placeholder;
80+
}
7381
set placeholder(value: string) {
82+
this._placeholder = value;
7483
this._dispatchEffects(this._placeholderConf.reconfigure(value ? placeholder(value) : []));
7584
}
85+
private _placeholder = '';
7686

7787
/** Whether the editor is disabled. */
7888
@Input({ transform: booleanAttribute }) disabled = false;
7989

8090
/** Whether the editor is readonly. */
8191
@Input({ transform: booleanAttribute })
92+
get readonly() {
93+
return this._readonly;
94+
}
8295
set readonly(value: boolean) {
96+
this._readonly = value;
8397
this._dispatchEffects(this._readonlyConf.reconfigure(EditorState.readOnly.of(value)));
8498
}
99+
private _readonly = false;
85100

86101
/** A binding that binds Tab to indentMore and Shift-Tab to indentLess. */
87102
@Input({ transform: booleanAttribute })
103+
get indentWithTab() {
104+
return this._indentWithTab;
105+
}
88106
set indentWithTab(value: boolean) {
107+
this._indentWithTab = value;
89108
this._dispatchEffects(
90109
this._indentWithTabConf.reconfigure(value ? keymap.of([indentWithTab]) : [])
91110
);
92111
}
112+
private _indentWithTab = false;
93113

94114
/** Should be a string consisting either entirely of the same whitespace character. */
95115
@Input()
116+
get indentUnit() {
117+
return this._indentUnit;
118+
}
96119
set indentUnit(value: string) {
120+
this._indentUnit = value;
97121
this._dispatchEffects(this._indentUnitConf.reconfigure(value ? indentUnit.of(value) : []));
98122
}
123+
private _indentUnit = '';
99124

100125
/** Whether this editor wraps lines. */
101126
@Input({ transform: booleanAttribute })
127+
get lineWrapping() {
128+
return this._lineWrapping;
129+
}
102130
set lineWrapping(value: boolean) {
131+
this._lineWrapping = value;
103132
this._dispatchEffects(this._lineWrappingConf.reconfigure(value ? EditorView.lineWrapping : []));
104133
}
134+
private _lineWrapping = false;
105135

106136
/** Whether highlight the whitespace. */
107137
@Input({ transform: booleanAttribute })
138+
get highlightWhitespace() {
139+
return this._highlightWhitespace;
140+
}
108141
set highlightWhitespace(value: boolean) {
142+
this._highlightWhitespace = value;
109143
this._dispatchEffects(
110144
this._highlightWhitespaceConf.reconfigure(value ? highlightWhitespace() : [])
111145
);
112146
}
147+
private _highlightWhitespace = false;
113148

114149
/**
115150
* An array of language descriptions for known
116-
* [language packages](https://github.com/codemirror/language-data/blob/main/src/language-data.ts).
151+
* [language-data packages](https://github.com/codemirror/language-data/blob/main/src/language-data.ts).
117152
*/
118-
@Input()
119-
get languages() {
120-
return this._languages;
121-
}
122-
set languages(value: LanguageDescription[]) {
123-
this._languages = value;
124-
this.setLanguage(this.language);
125-
}
126-
private _languages: LanguageDescription[] = [];
153+
@Input() languages: LanguageDescription[] = [];
127154

128-
/** Editor's language. You should set the `languages` option at first. */
155+
/** Editor's language. You should set the `languages` prop at first. */
129156
@Input()
130157
get language() {
131158
return this._language;
132159
}
133160
set language(value: string) {
134161
this._language = value;
135-
if (this.languages.length > 0) {
136-
this.setLanguage(value);
137-
}
162+
this.setLanguage(value);
138163
}
139164
private _language = '';
140165

@@ -153,7 +178,9 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
153178
}
154179
private _setup: Setup = 'basic';
155180

156-
/** EditorState's [extensions](https://codemirror.net/docs/ref/#state.EditorStateConfig.extensions). */
181+
/**
182+
* EditorState's [extensions](https://codemirror.net/docs/ref/#state.EditorStateConfig.extensions).
183+
*/
157184
@Input()
158185
get extensions() {
159186
return this._extensions;
@@ -173,14 +200,13 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
173200
/** Event emitted when the editor has lost focus. */
174201
@Output() blur = new EventEmitter<void>();
175202

203+
view?: EditorView;
204+
176205
private _onChange: (value: string) => void = () => {};
177206
private _onTouched: () => void = () => {};
178207

179208
constructor(private _elementRef: ElementRef<Element>) {}
180209

181-
view?: EditorView | null;
182-
183-
/** Register a function to be called every time the view updates. */
184210
private _updateListener = EditorView.updateListener.of(vu => {
185211
if (vu.docChanged && !vu.transactions.some(tr => tr.annotation(External))) {
186212
const value = vu.state.doc.toString();
@@ -201,7 +227,6 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
201227
private _highlightWhitespaceConf = new Compartment();
202228
private _languageConf = new Compartment();
203229

204-
/** Get the extensions of the editor. */
205230
private _getExtensions(): Extension[] {
206231
return [
207232
this._updateListener,
@@ -216,7 +241,7 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
216241
this._highlightWhitespaceConf.of([]),
217242
this._languageConf.of([]),
218243

219-
this._setup === 'basic' ? basicSetup : this._setup === 'minimal' ? minimalSetup : [],
244+
this.setup === 'basic' ? basicSetup : this.setup === 'minimal' ? minimalSetup : [],
220245

221246
...this.extensions,
222247
];
@@ -236,9 +261,13 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
236261
});
237262
}
238263

239-
setLanguage(lang: string) {
264+
/** Sets language dynamically. */
265+
setLanguage(lang: string, onInit?: boolean) {
240266
if (!lang) {
241-
this._dispatchEffects(this._languageConf.reconfigure([]));
267+
return;
268+
}
269+
if (this.languages.length === 0) {
270+
onInit && console.error('No supported languages. Please set the languages prop at first.');
242271
return;
243272
}
244273
const langDesc = this.findLanguage(lang);
@@ -256,12 +285,8 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
256285
}
257286
}
258287
}
259-
console.error(`Language not found: ${this.language}`);
260-
if (this.languages.length === 0) {
261-
console.error('No supported languages. Please set the languages option at first.');
262-
} else {
263-
console.info('Supported language names: ', this.languages.map(lang => lang.name).join(', '));
264-
}
288+
console.error('Language not found:', name);
289+
console.info('Supported language names:', this.languages.map(lang => lang.name).join(', '));
265290
return null;
266291
}
267292

@@ -288,11 +313,21 @@ export class CodeEditor implements OnInit, OnDestroy, ControlValueAccessor {
288313
this._onTouched();
289314
this.blur.emit();
290315
});
316+
317+
// Force setter to be called after editor initialized.
318+
this.theme = this._theme;
319+
this.placeholder = this._placeholder;
320+
this.readonly = this._readonly;
321+
this.indentWithTab = this._indentWithTab;
322+
this.indentUnit = this._indentUnit;
323+
this.lineWrapping = this._lineWrapping;
324+
this.highlightWhitespace = this._highlightWhitespace;
325+
this.setDisabledState(this.disabled);
326+
this.setLanguage(this.language, true);
291327
}
292328

293329
ngOnDestroy(): void {
294330
this.view?.destroy();
295-
this.view = null;
296331
}
297332

298333
writeValue(value: string): void {

0 commit comments

Comments
 (0)