diff --git a/packages/carta-md/src/lib/internal/carta.ts b/packages/carta-md/src/lib/internal/carta.ts index 78bd08bf..81017e2e 100644 --- a/packages/carta-md/src/lib/internal/carta.ts +++ b/packages/carta-md/src/lib/internal/carta.ts @@ -313,7 +313,7 @@ export class Carta { */ public $setInput(textarea: HTMLTextAreaElement, container: HTMLDivElement, callback: () => void) { // Remove old listeners if any - this.input?.events.removeEventListener('update', callback); + const previousInput = this.input; this._input = new CartaInput(textarea, container, { shortcuts: this.keyboardShortcuts, @@ -322,6 +322,11 @@ export class Carta { historyOpts: this.options?.historyOptions }); + if (previousInput) { + previousInput.events.removeEventListener('update', callback); + this._input.history = previousInput.history; + } + this._input.events.addEventListener('update', callback); // Bind elements diff --git a/packages/carta-md/src/lib/internal/input.ts b/packages/carta-md/src/lib/internal/input.ts index a6e318c3..2be287cd 100644 --- a/packages/carta-md/src/lib/internal/input.ts +++ b/packages/carta-md/src/lib/internal/input.ts @@ -30,7 +30,7 @@ export class CartaInput { // Used to detect keys that actually changed the textarea value private onKeyDownValue: string | undefined; - public readonly history: CartaHistory; + public history: CartaHistory; public readonly events = new EventTarget(); constructor(