Skip to content

Commit

Permalink
fix(gv-code): remove listeners when disconnect callback
Browse files Browse the repository at this point in the history
resolve errors when switch between different instance of code mirror

fix gravitee-io/issues#5364
  • Loading branch information
gcusnieux authored and aelamrani committed Apr 19, 2021
1 parent 514b7e9 commit bc6cc0f
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/molecules/gv-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,17 @@ export class GvCode extends InputElement(LitElement) {
return this.rows === 1;
}

disconnectedCallback() {
if (this._codeMirror != null) {
this._codeMirror.off('beforeChange', this._handlers.beforeChange);
this._codeMirror.off('change', this._handlers.change);
}
}

connectedCallback() {
super.connectedCallback();
CodeMirror.defineInitHook((cm) => {
cm.on('beforeChange', (cm, event) => {
this._handlers = {
beforeChange: (cm, event) => {
if (this.singleLine && this._id === cm.getTextArea().id) {
// Identify typing events that add a newline to the buffer.
const hasTypedNewline = event.origin === '+input' && typeof event.text === 'object' && event.text.join('') === '';
Expand All @@ -196,13 +203,16 @@ export class GvCode extends InputElement(LitElement) {
}
}
return null;
});

cm.on('change', () => {
},
change: (cm) => {
if (this._id === cm.getTextArea().id) {
this._onChange(cm);
}
});
},
};
CodeMirror.defineInitHook((cm) => {
cm.on('beforeChange', this._handlers.beforeChange);
cm.on('change', this._handlers.change);
});
}

Expand Down

0 comments on commit bc6cc0f

Please sign in to comment.