Skip to content

Commit

Permalink
fix(core): keep history state when reloading input
Browse files Browse the repository at this point in the history
  • Loading branch information
BearToCode committed Nov 15, 2023
1 parent a61e049 commit b187a6a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/carta-md/src/lib/internal/carta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/carta-md/src/lib/internal/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b187a6a

Please sign in to comment.