1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! use_history hook for undo/redo functionality
//!
//! Provides history tracking with undo and redo capabilities.
//!
//! # Example
//!
//! ```rust,ignore
//! use rnk::prelude::*;
//!
//! fn editor() -> Element {
//! let history = use_history("initial text".to_string());
//!
//! use_input(move |input, key| {
//! if key.ctrl && input == "z" {
//! history.undo();
//! } else if key.ctrl && input == "y" {
//! history.redo();
//! } else if !input.is_empty() {
//! let mut current = history.get();
//! current.push_str(&input);
//! history.push(current);
//! }
//! });
//!
//! Text::new(history.get()).into_element()
//! }
//! ```
use crate::hooks::use_signal::{Signal, use_signal};
/// Internal state for history tracking
#[derive(Clone)]
struct HistoryState<T> {
past: Vec<T>,
present: T,
future: Vec<T>,
max_size: usize,
}
/// Handle for history operations
#[derive(Clone)]
pub struct HistoryHandle<T> {
signal: Signal<HistoryState<T>>,
}
impl<T> HistoryHandle<T>
where
T: Clone + Send + Sync + 'static,
{
/// Get the current value
pub fn get(&self) -> T {
self.signal.with(|s| s.present.clone())
}
/// Push a new state (clears redo history)
pub fn push(&self, value: T) {
self.signal.update(|s| {
s.past.push(s.present.clone());
while s.past.len() > s.max_size {
s.past.remove(0);
}
s.present = value;
s.future.clear();
});
}
/// Undo to previous state
pub fn undo(&self) -> bool {
let mut success = false;
self.signal.update(|s| {
if let Some(prev) = s.past.pop() {
s.future.push(s.present.clone());
s.present = prev;
success = true;
}
});
success
}
/// Redo to next state
pub fn redo(&self) -> bool {
let mut success = false;
self.signal.update(|s| {
if let Some(next) = s.future.pop() {
s.past.push(s.present.clone());
s.present = next;
success = true;
}
});
success
}
/// Check if undo is available
pub fn can_undo(&self) -> bool {
self.signal.with(|s| !s.past.is_empty())
}
/// Check if redo is available
pub fn can_redo(&self) -> bool {
self.signal.with(|s| !s.future.is_empty())
}
/// Get the number of undo steps available
pub fn undo_count(&self) -> usize {
self.signal.with(|s| s.past.len())
}
/// Get the number of redo steps available
pub fn redo_count(&self) -> usize {
self.signal.with(|s| s.future.len())
}
/// Clear all history
pub fn clear(&self) {
self.signal.update(|s| {
s.past.clear();
s.future.clear();
});
}
/// Reset to initial state and clear history
pub fn reset(&self, value: T) {
self.signal.update(|s| {
s.present = value;
s.past.clear();
s.future.clear();
});
}
/// Go to a specific point in history (0 = oldest)
pub fn go_to(&self, index: usize) {
self.signal.update(|s| {
let past_len = s.past.len();
if index < past_len {
let steps = past_len - index;
for _ in 0..steps {
if let Some(prev) = s.past.pop() {
s.future.push(s.present.clone());
s.present = prev;
}
}
}
});
}
}
/// Create a history-tracked state
pub fn use_history<T>(initial: T) -> HistoryHandle<T>
where
T: Clone + Send + Sync + 'static,
{
use_history_with_size(initial, 100)
}
/// Create a history-tracked state with custom max size
pub fn use_history_with_size<T>(initial: T, max_size: usize) -> HistoryHandle<T>
where
T: Clone + Send + Sync + 'static,
{
let signal = use_signal(|| HistoryState {
past: Vec::new(),
present: initial,
future: Vec::new(),
max_size,
});
HistoryHandle { signal }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_use_history_compiles() {
fn _test() {
let history = use_history(0);
history.push(1);
history.undo();
history.redo();
let _ = history.get();
}
}
#[test]
fn test_history_can_undo_redo() {
fn _test() {
let history = use_history("a".to_string());
assert!(!history.can_undo());
assert!(!history.can_redo());
history.push("b".to_string());
// Now can undo
}
}
#[test]
fn test_history_with_size() {
fn _test() {
let history = use_history_with_size(0, 10);
for i in 1..=20 {
history.push(i);
}
// Should only keep last 10
}
}
}