Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
centralized the types interfaces
  • Loading branch information
dhruvikpatel18 committed Jul 24, 2025
commit 5f2a6e54aaff89a5b4d601ff29458086b5da7282
64 changes: 14 additions & 50 deletions packages/undo-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,35 @@
*/
import isShallowEqual from '@wordpress/is-shallow-equal';

/**
* Internal dependencies
*/
import type {
HistoryChange as _HistoryChange,
HistoryChanges as _HistoryChanges,
HistoryRecord as _HistoryRecord,
UndoManager as _UndoManager,
} from './types';

/**
* Represents a single change in history.
*/
export interface HistoryChange< T = unknown > {
/** The previous value */
from: T;
/** The new value */
to: T;
}
export type HistoryChange< T = unknown > = _HistoryChange< T >;

/**
* Represents changes for a single item.
*/
export interface HistoryChanges< T = unknown > {
/** The identifier for the item being changed */
id: string | Record< string, unknown >;
/** The changes made to the item */
changes: Record< string, HistoryChange< T > >;
}
export type HistoryChanges< T = unknown > = _HistoryChanges< T >;

/**
* Represents a record of history changes.
*/
export type HistoryRecord< T = unknown > = HistoryChanges< T >[];
export type HistoryRecord< T = unknown > = _HistoryRecord< T >;

/**
* The undo manager interface.
*/
export interface UndoManager< T = unknown > {
/**
* Record changes into the history.
*
* @param record A record of changes to record.
* @param isStaged Whether to immediately create an undo point or not.
*/
addRecord: ( record?: HistoryRecord< T >, isStaged?: boolean ) => void;

/**
* Undo the last recorded changes.
*
* @return The undone record or undefined if nothing to undo.
*/
undo: () => HistoryRecord< T > | undefined;

/**
* Redo the last undone changes.
*
* @return The redone record or undefined if nothing to redo.
*/
redo: () => HistoryRecord< T > | undefined;

/**
* Check if there are changes that can be undone.
*
* @return Whether there are changes to undo.
*/
hasUndo: () => boolean;

/**
* Check if there are changes that can be redone.
*
* @return Whether there are changes to redo.
*/
hasRedo: () => boolean;
}
export type UndoManager< T = unknown > = _UndoManager< T >;

/**
* Merge changes for a single item into a record of changes.
Expand Down
74 changes: 60 additions & 14 deletions packages/undo-manager/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,65 @@
export type HistoryChange = {
from: any;
to: any;
};
/**
* Represents a single change in history.
*/
export interface HistoryChange< T = unknown > {
/** The previous value */
from: T;
/** The new value */
to: T;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it possible that from and to can be of different type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is possible, have changed the interface to allow from and to to be different types.


export type HistoryChanges = {
id: string | Record< string, any >;
changes: Record< string, HistoryChange >;
};
/**
* Represents changes for a single item.
*/
export interface HistoryChanges< T = unknown > {
/** The identifier for the item being changed */
id: string | Record< string, unknown >;
/** The changes made to the item */
changes: Record< string, HistoryChange< T > >;
}

export type HistoryRecord = Array< HistoryChanges >;
/**
* Represents a record of history changes.
*/
export type HistoryRecord< T = unknown > = HistoryChanges< T >[];

export type UndoManager = {
addRecord: ( record: HistoryRecord, isStaged: boolean ) => void;
undo: () => HistoryRecord | undefined;
redo: () => HistoryRecord | undefined;
/**
* The undo manager interface.
*/
export interface UndoManager< T = unknown > {
/**
* Record changes into the history.
*
* @param record A record of changes to record.
* @param isStaged Whether to immediately create an undo point or not.
*/
addRecord: ( record?: HistoryRecord< T >, isStaged?: boolean ) => void;

/**
* Undo the last recorded changes.
*
* @return The undone record or undefined if nothing to undo.
*/
undo: () => HistoryRecord< T > | undefined;

/**
* Redo the last undone changes.
*
* @return The redone record or undefined if nothing to redo.
*/
redo: () => HistoryRecord< T > | undefined;

/**
* Check if there are changes that can be undone.
*
* @return Whether there are changes to undo.
*/
hasUndo: () => boolean;

/**
* Check if there are changes that can be redone.
*
* @return Whether there are changes to redo.
*/
hasRedo: () => boolean;
};
}
Loading