Force state redraw when editing the state array
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details

main 0.0.1
Clément FRÉVILLE 1 year ago
parent 6eefa6972b
commit ad9ced92ac

@ -7,7 +7,7 @@ import {
NgZone,
OnDestroy
} from '@angular/core';
import { EditorState, Extension, StateEffect } from '@codemirror/state';
import { EditorState, Extension } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
@ -28,26 +28,33 @@ export class CodeMirrorComponent implements AfterViewInit, OnDestroy, ControlVal
@Input()
set extensions(extensions: Extension[]) {
this.editor?.dispatch({
effects: StateEffect.reconfigure.of(extensions),
const state = EditorState.create({
extensions: [...extensions, this.updateListener]
});
if (this.editor) {
this.editor.setState(state);
} else {
this.editor = new EditorView({
state,
parent: this.host.nativeElement,
});
}
}
editor: EditorView | null = null;
private handleChange: (value: string) => void = () => {};
private readonly updateListener = EditorView.updateListener.of((update) => {
this.ngZone.run(() => {
const value = update.state.doc.toString();
this.handleChange(value);
});
});
constructor(private host: ElementRef<HTMLElement>, private ngZone: NgZone) {}
ngAfterViewInit(): void {
const state = EditorState.create({
extensions: [
EditorView.updateListener.of((update) => {
this.ngZone.run(() => {
const value = update.state.doc.toString();
this.handleChange(value);
});
}),
],
extensions: [this.updateListener],
});
this.editor = new EditorView({
state,

Loading…
Cancel
Save