|
| 1 | +/** |
| 2 | + * Search and Replace History Manager |
| 3 | + * Manages search/replace history using localStorage |
| 4 | + */ |
| 5 | + |
| 6 | +const HISTORY_KEY = "acode.searchreplace.history"; |
| 7 | +const MAX_HISTORY_ITEMS = 20; |
| 8 | + |
| 9 | +class SearchHistory { |
| 10 | + constructor() { |
| 11 | + this.history = this.loadHistory(HISTORY_KEY); |
| 12 | + this.searchIndex = -1; // Current position in history for search input |
| 13 | + this.replaceIndex = -1; // Current position in history for replace input |
| 14 | + this.tempSearchValue = ""; // Temporary storage for current search input |
| 15 | + this.tempReplaceValue = ""; // Temporary storage for current replace input |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Load history from localStorage |
| 20 | + * @param {string} key Storage key |
| 21 | + * @returns {Array<string>} History items |
| 22 | + */ |
| 23 | + loadHistory(key) { |
| 24 | + try { |
| 25 | + const stored = localStorage.getItem(key); |
| 26 | + return stored ? JSON.parse(stored) : []; |
| 27 | + } catch (error) { |
| 28 | + console.warn("Failed to load search history:", error); |
| 29 | + return []; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Save history to localStorage |
| 35 | + */ |
| 36 | + saveHistory() { |
| 37 | + try { |
| 38 | + localStorage.setItem(HISTORY_KEY, JSON.stringify(this.history)); |
| 39 | + } catch (error) { |
| 40 | + console.warn("Failed to save search history:", error); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Add item to history |
| 46 | + * @param {string} item Item to add |
| 47 | + */ |
| 48 | + addToHistory(item) { |
| 49 | + if (!item || typeof item !== "string" || item.trim().length === 0) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const trimmedItem = item.trim(); |
| 54 | + |
| 55 | + // Remove existing item if present |
| 56 | + this.history = this.history.filter((h) => h !== trimmedItem); |
| 57 | + |
| 58 | + // Add to beginning |
| 59 | + this.history.unshift(trimmedItem); |
| 60 | + |
| 61 | + // Limit history size |
| 62 | + this.history = this.history.slice(0, MAX_HISTORY_ITEMS); |
| 63 | + |
| 64 | + this.saveHistory(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Get history |
| 69 | + * @returns {Array<string>} History items |
| 70 | + */ |
| 71 | + getHistory() { |
| 72 | + return [...this.history]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Clear all history |
| 77 | + */ |
| 78 | + clearHistory() { |
| 79 | + this.history = []; |
| 80 | + this.saveHistory(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Navigate up in search history (terminal-like) |
| 85 | + * @param {string} currentValue Current input value |
| 86 | + * @returns {string} Previous history item or current value |
| 87 | + */ |
| 88 | + navigateSearchUp(currentValue) { |
| 89 | + if (this.history.length === 0) return currentValue; |
| 90 | + |
| 91 | + // Store current value if we're at the beginning |
| 92 | + if (this.searchIndex === -1) { |
| 93 | + this.tempSearchValue = currentValue; |
| 94 | + this.searchIndex = this.history.length - 1; |
| 95 | + } else if (this.searchIndex > 0) { |
| 96 | + this.searchIndex--; |
| 97 | + } |
| 98 | + |
| 99 | + return this.history[this.searchIndex] || currentValue; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Navigate down in search history (terminal-like) |
| 104 | + * @param {string} currentValue Current input value |
| 105 | + * @returns {string} Next history item or original value |
| 106 | + */ |
| 107 | + navigateSearchDown(currentValue) { |
| 108 | + if (this.history.length === 0 || this.searchIndex === -1) { |
| 109 | + return currentValue; |
| 110 | + } |
| 111 | + |
| 112 | + this.searchIndex++; |
| 113 | + |
| 114 | + // If we've gone past the end, return to original value |
| 115 | + if (this.searchIndex >= this.history.length) { |
| 116 | + this.searchIndex = -1; |
| 117 | + return this.tempSearchValue; |
| 118 | + } |
| 119 | + |
| 120 | + return this.history[this.searchIndex]; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Navigate up in replace history (terminal-like) |
| 125 | + * @param {string} currentValue Current input value |
| 126 | + * @returns {string} Previous history item or current value |
| 127 | + */ |
| 128 | + navigateReplaceUp(currentValue) { |
| 129 | + if (this.history.length === 0) return currentValue; |
| 130 | + |
| 131 | + // Store current value if we're at the beginning |
| 132 | + if (this.replaceIndex === -1) { |
| 133 | + this.tempReplaceValue = currentValue; |
| 134 | + this.replaceIndex = this.history.length - 1; |
| 135 | + } else if (this.replaceIndex > 0) { |
| 136 | + this.replaceIndex--; |
| 137 | + } |
| 138 | + |
| 139 | + return this.history[this.replaceIndex] || currentValue; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Navigate down in replace history (terminal-like) |
| 144 | + * @param {string} currentValue Current input value |
| 145 | + * @returns {string} Next history item or original value |
| 146 | + */ |
| 147 | + navigateReplaceDown(currentValue) { |
| 148 | + if (this.history.length === 0 || this.replaceIndex === -1) { |
| 149 | + return currentValue; |
| 150 | + } |
| 151 | + |
| 152 | + this.replaceIndex++; |
| 153 | + |
| 154 | + // If we've gone past the end, return to original value |
| 155 | + if (this.replaceIndex >= this.history.length) { |
| 156 | + this.replaceIndex = -1; |
| 157 | + return this.tempReplaceValue; |
| 158 | + } |
| 159 | + |
| 160 | + return this.history[this.replaceIndex]; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Reset search history navigation |
| 165 | + */ |
| 166 | + resetSearchNavigation() { |
| 167 | + this.searchIndex = -1; |
| 168 | + this.tempSearchValue = ""; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Reset replace history navigation |
| 173 | + */ |
| 174 | + resetReplaceNavigation() { |
| 175 | + this.replaceIndex = -1; |
| 176 | + this.tempReplaceValue = ""; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Reset all navigation state |
| 181 | + */ |
| 182 | + resetAllNavigation() { |
| 183 | + this.resetSearchNavigation(); |
| 184 | + this.resetReplaceNavigation(); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +export default new SearchHistory(); |
0 commit comments