3
3
*/
4
4
import isShallowEqual from '@wordpress/is-shallow-equal' ;
5
5
6
- /** @typedef {import('./types').HistoryRecord } HistoryRecord */
7
- /** @typedef {import('./types').HistoryChange } HistoryChange */
8
- /** @typedef {import('./types').HistoryChanges } HistoryChanges */
9
- /** @typedef {import('./types').UndoManager } UndoManager */
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import type {
10
+ HistoryChange as _HistoryChange ,
11
+ HistoryChanges as _HistoryChanges ,
12
+ HistoryRecord as _HistoryRecord ,
13
+ UndoManager as _UndoManager ,
14
+ } from './types' ;
15
+
16
+ /**
17
+ * Represents a single change in history.
18
+ */
19
+ export type HistoryChange < T = unknown > = _HistoryChange < T > ;
20
+
21
+ /**
22
+ * Represents changes for a single item.
23
+ */
24
+ export type HistoryChanges < T = unknown > = _HistoryChanges < T > ;
25
+
26
+ /**
27
+ * Represents a record of history changes.
28
+ */
29
+ export type HistoryRecord < T = unknown > = _HistoryRecord < T > ;
30
+
31
+ /**
32
+ * The undo manager interface.
33
+ */
34
+ export type UndoManager < T = unknown > = _UndoManager < T > ;
10
35
11
36
/**
12
37
* Merge changes for a single item into a record of changes.
13
38
*
14
- * @param { Record< string, HistoryChange > } changes1 Previous changes
15
- * @param { Record< string, HistoryChange > } changes2 NextChanges
39
+ * @param changes1 Previous changes
40
+ * @param changes2 Next changes
16
41
*
17
- * @return { Record< string, HistoryChange > } Merged changes
42
+ * @return Merged changes
18
43
*/
19
- function mergeHistoryChanges ( changes1 , changes2 ) {
20
- /**
21
- * @type { Record< string, HistoryChange > }
22
- */
23
- const newChanges = { ...changes1 } ;
44
+ function mergeHistoryChanges < T > (
45
+ changes1 : Record < string , HistoryChange < T > > ,
46
+ changes2 : Record < string , HistoryChange < T > >
47
+ ) : Record < string , HistoryChange < T > > {
48
+ const newChanges : Record < string , HistoryChange < T > > = { ...changes1 } ;
24
49
Object . entries ( changes2 ) . forEach ( ( [ key , value ] ) => {
25
50
if ( newChanges [ key ] ) {
26
51
newChanges [ key ] = { ...newChanges [ key ] , to : value . to } ;
@@ -35,10 +60,13 @@ function mergeHistoryChanges( changes1, changes2 ) {
35
60
/**
36
61
* Adds history changes for a single item into a record of changes.
37
62
*
38
- * @param { HistoryRecord } record The record to merge into.
39
- * @param { HistoryChanges } changes The changes to merge.
63
+ * @param record The record to merge into.
64
+ * @param changes The changes to merge.
40
65
*/
41
- const addHistoryChangesIntoRecord = ( record , changes ) => {
66
+ const addHistoryChangesIntoRecord = < T > (
67
+ record : HistoryRecord < T > ,
68
+ changes : HistoryChanges < T >
69
+ ) : HistoryRecord < T > => {
42
70
const existingChangesIndex = record ?. findIndex (
43
71
( { id : recordIdentifier } ) => {
44
72
return typeof recordIdentifier === 'string'
@@ -66,28 +94,19 @@ const addHistoryChangesIntoRecord = ( record, changes ) => {
66
94
/**
67
95
* Creates an undo manager.
68
96
*
69
- * @return { UndoManager } Undo manager.
97
+ * @return Undo manager.
70
98
*/
71
- export function createUndoManager ( ) {
72
- /**
73
- * @type {HistoryRecord[] }
74
- */
75
- let history = [ ] ;
76
- /**
77
- * @type {HistoryRecord }
78
- */
79
- let stagedRecord = [ ] ;
80
- /**
81
- * @type {number }
82
- */
99
+ export function createUndoManager < T = unknown > ( ) : UndoManager < T > {
100
+ let history : HistoryRecord < T > [ ] = [ ] ;
101
+ let stagedRecord : HistoryRecord < T > = [ ] ;
83
102
let offset = 0 ;
84
103
85
- const dropPendingRedos = ( ) => {
104
+ const dropPendingRedos = ( ) : void => {
86
105
history = history . slice ( 0 , offset || undefined ) ;
87
106
offset = 0 ;
88
107
} ;
89
108
90
- const appendStagedRecordToLatestHistoryRecord = ( ) => {
109
+ const appendStagedRecordToLatestHistoryRecord = ( ) : void => {
91
110
const index = history . length === 0 ? 0 : history . length - 1 ;
92
111
let latestRecord = history [ index ] ?? [ ] ;
93
112
stagedRecord . forEach ( ( changes ) => {
@@ -102,10 +121,10 @@ export function createUndoManager() {
102
121
* A record is considered empty if it the changes keep the same values.
103
122
* Also updates to function values are ignored.
104
123
*
105
- * @param { HistoryRecord } record
106
- * @return { boolean } Whether the record is empty.
124
+ * @param record The record to check.
125
+ * @return Whether the record is empty.
107
126
*/
108
- const isRecordEmpty = ( record ) => {
127
+ const isRecordEmpty = ( record : HistoryRecord < T > ) : boolean => {
109
128
const filteredRecord = record . filter ( ( { changes } ) => {
110
129
return Object . values ( changes ) . some (
111
130
( { from, to } ) =>
@@ -118,13 +137,7 @@ export function createUndoManager() {
118
137
} ;
119
138
120
139
return {
121
- /**
122
- * Record changes into the history.
123
- *
124
- * @param {HistoryRecord= } record A record of changes to record.
125
- * @param {boolean } isStaged Whether to immediately create an undo point or not.
126
- */
127
- addRecord ( record , isStaged = false ) {
140
+ addRecord ( record ?: HistoryRecord < T > , isStaged = false ) : void {
128
141
const isEmpty = ! record || isRecordEmpty ( record ) ;
129
142
if ( isStaged ) {
130
143
if ( isEmpty ) {
@@ -148,7 +161,7 @@ export function createUndoManager() {
148
161
}
149
162
} ,
150
163
151
- undo ( ) {
164
+ undo ( ) : HistoryRecord < T > | undefined {
152
165
if ( stagedRecord . length ) {
153
166
dropPendingRedos ( ) ;
154
167
appendStagedRecordToLatestHistoryRecord ( ) ;
@@ -161,7 +174,7 @@ export function createUndoManager() {
161
174
return undoRecord ;
162
175
} ,
163
176
164
- redo ( ) {
177
+ redo ( ) : HistoryRecord < T > | undefined {
165
178
const redoRecord = history [ history . length + offset ] ;
166
179
if ( ! redoRecord ) {
167
180
return ;
@@ -170,11 +183,11 @@ export function createUndoManager() {
170
183
return redoRecord ;
171
184
} ,
172
185
173
- hasUndo ( ) {
186
+ hasUndo ( ) : boolean {
174
187
return ! ! history [ history . length - 1 + offset ] ;
175
188
} ,
176
189
177
- hasRedo ( ) {
190
+ hasRedo ( ) : boolean {
178
191
return ! ! history [ history . length + offset ] ;
179
192
} ,
180
193
} ;
0 commit comments