1
1
// NOTE: 実際は 'vue-demi' からimportされてる
2
- import { computed , markRaw , ref , type Ref } from 'vue' ;
2
+ import { type Ref , computed , markRaw , ref } from 'vue'
3
3
4
- import { timestamp } from '@vueyous/shared' ;
4
+ import { timestamp } from '@vueyous/shared'
5
5
6
6
export interface UseRefHistoryRecord < T > {
7
- snapshot : T ;
8
- timestamp : number ;
7
+ snapshot : T
8
+ timestamp : number
9
9
}
10
10
11
11
export interface UseManualHistoryOptions < Raw > {
12
- setSource ?: ( source : Ref < Raw > , v : Raw ) => void ;
12
+ setSource ?: ( source : Ref < Raw > , v : Raw ) => void
13
13
}
14
14
15
15
export interface UseManualRefHistoryReturn < Raw > {
@@ -18,106 +18,107 @@ export interface UseManualRefHistoryReturn<Raw> {
18
18
// source: Ref<Raw>;
19
19
20
20
/** An array of history records for undo, newest comes to first */
21
- history : Ref < UseRefHistoryRecord < Raw > [ ] > ;
21
+ history : Ref < UseRefHistoryRecord < Raw > [ ] >
22
22
23
23
/** Last history point, source can be different if paused */
24
- last : Ref < UseRefHistoryRecord < Raw > > ;
24
+ last : Ref < UseRefHistoryRecord < Raw > >
25
25
26
26
/** Same as {@link UseManualRefHistoryReturn.history | history} */
27
- undoStack : Ref < UseRefHistoryRecord < Raw > [ ] > ;
27
+ undoStack : Ref < UseRefHistoryRecord < Raw > [ ] >
28
28
29
29
/** Records array for redo */
30
- redoStack : Ref < UseRefHistoryRecord < Raw > [ ] > ;
30
+ redoStack : Ref < UseRefHistoryRecord < Raw > [ ] >
31
31
32
32
/** A ref representing if undo is possible (non empty undoStack) */
33
- canUndo : Ref < boolean > ;
33
+ canUndo : Ref < boolean >
34
34
35
35
/** A ref representing if redo is possible (non empty redoStack) */
36
- canRedo : Ref < boolean > ;
36
+ canRedo : Ref < boolean >
37
37
38
38
/** Undo changes */
39
- undo : ( ) => void ;
39
+ undo : ( ) => void
40
40
41
41
/** Redo changes */
42
- redo : ( ) => void ;
42
+ redo : ( ) => void
43
43
44
44
/** Clear all the history */
45
- clear : ( ) => void ;
45
+ clear : ( ) => void
46
46
47
47
/** Create a new history record */
48
- commit : ( ) => void ;
48
+ commit : ( ) => void
49
49
50
50
/** Reset ref's value with latest history */
51
- reset : ( ) => void ;
51
+ reset : ( ) => void
52
52
}
53
53
54
54
function fnSetSource < F > ( source : Ref < F > , value : F ) {
55
- return ( source . value = value ) ;
55
+ return ( source . value = value )
56
56
}
57
57
58
58
export function useManualRefHistory < Raw > (
59
59
source : Ref < Raw > ,
60
- options : UseManualHistoryOptions < Raw > = { }
60
+ options : UseManualHistoryOptions < Raw > = { } ,
61
61
) : UseManualRefHistoryReturn < Raw > {
62
- const { setSource = fnSetSource } = options ;
62
+ const { setSource = fnSetSource } = options
63
63
64
64
function _createHistoryRecord ( ) : UseRefHistoryRecord < Raw > {
65
65
return markRaw ( {
66
66
snapshot : source . value ,
67
- timestamp : timestamp ( )
68
- } ) ;
67
+ timestamp : timestamp ( ) ,
68
+ } )
69
69
}
70
70
71
71
const last : Ref < UseRefHistoryRecord < Raw > > = ref ( _createHistoryRecord ( ) ) as Ref <
72
72
UseRefHistoryRecord < Raw >
73
- > ;
73
+ >
74
74
75
- const undoStack : Ref < UseRefHistoryRecord < Raw > [ ] > = ref ( [ ] ) ;
76
- const redoStack : Ref < UseRefHistoryRecord < Raw > [ ] > = ref ( [ ] ) ;
75
+ const undoStack : Ref < UseRefHistoryRecord < Raw > [ ] > = ref ( [ ] )
76
+ const redoStack : Ref < UseRefHistoryRecord < Raw > [ ] > = ref ( [ ] )
77
77
78
78
const _setSource = ( record : UseRefHistoryRecord < Raw > ) => {
79
- setSource ( source , record . snapshot ) ;
80
- last . value = record ;
81
- } ;
79
+ setSource ( source , record . snapshot )
80
+ last . value = record
81
+ }
82
82
83
83
const commit = ( ) => {
84
- undoStack . value . unshift ( last . value ) ;
85
- last . value = _createHistoryRecord ( ) ;
84
+ undoStack . value . unshift ( last . value )
85
+ last . value = _createHistoryRecord ( )
86
86
// commit した際に redoStack に要素がある場合はそれを全削除
87
- if ( redoStack . value . length ) redoStack . value . splice ( 0 , redoStack . value . length ) ;
88
- } ;
87
+ if ( redoStack . value . length )
88
+ redoStack . value . splice ( 0 , redoStack . value . length )
89
+ }
89
90
90
91
const clear = ( ) => {
91
- undoStack . value . splice ( 0 , undoStack . value . length ) ;
92
- redoStack . value . splice ( 0 , redoStack . value . length ) ;
93
- } ;
92
+ undoStack . value . splice ( 0 , undoStack . value . length )
93
+ redoStack . value . splice ( 0 , redoStack . value . length )
94
+ }
94
95
95
96
const undo = ( ) => {
96
- const state = undoStack . value . shift ( ) ;
97
+ const state = undoStack . value . shift ( )
97
98
98
99
if ( state ) {
99
- redoStack . value . unshift ( last . value ) ;
100
- _setSource ( state ) ;
100
+ redoStack . value . unshift ( last . value )
101
+ _setSource ( state )
101
102
}
102
- } ;
103
+ }
103
104
104
105
const redo = ( ) => {
105
- const state = redoStack . value . shift ( ) ;
106
+ const state = redoStack . value . shift ( )
106
107
107
108
if ( state ) {
108
- undoStack . value . unshift ( last . value ) ;
109
- _setSource ( state ) ;
109
+ undoStack . value . unshift ( last . value )
110
+ _setSource ( state )
110
111
}
111
- } ;
112
+ }
112
113
113
114
const reset = ( ) => {
114
- _setSource ( last . value ) ;
115
- } ;
115
+ _setSource ( last . value )
116
+ }
116
117
117
- const history = computed ( ( ) => [ last . value , ...undoStack . value ] ) ;
118
+ const history = computed ( ( ) => [ last . value , ...undoStack . value ] )
118
119
119
- const canUndo = computed ( ( ) => undoStack . value . length > 0 ) ;
120
- const canRedo = computed ( ( ) => redoStack . value . length > 0 ) ;
120
+ const canUndo = computed ( ( ) => undoStack . value . length > 0 )
121
+ const canRedo = computed ( ( ) => redoStack . value . length > 0 )
121
122
122
123
return {
123
124
// source,
@@ -132,6 +133,6 @@ export function useManualRefHistory<Raw>(
132
133
commit,
133
134
reset,
134
135
undo,
135
- redo
136
- } ;
136
+ redo,
137
+ }
137
138
}
0 commit comments