Spaces:
Sleeping
Sleeping
Commit ·
fc202d4
1
Parent(s): 8bb7930
history: add auto delete
Browse files- static/css/settings.css +18 -0
- static/js/history.js +21 -0
- static/js/idb.js +34 -0
- static/js/settings.js +24 -0
- templates/index.html +24 -1
static/css/settings.css
CHANGED
|
@@ -67,4 +67,22 @@
|
|
| 67 |
|
| 68 |
.settings-section .form-textarea {
|
| 69 |
font-size: 0.85rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
|
|
|
| 67 |
|
| 68 |
.settings-section .form-textarea {
|
| 69 |
font-size: 0.85rem;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
.settings-sub-row {
|
| 73 |
+
padding-left: 18px;
|
| 74 |
+
opacity: 0.35;
|
| 75 |
+
pointer-events: none;
|
| 76 |
+
transition: opacity 0.15s;
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
.settings-sub-row.enabled {
|
| 80 |
+
opacity: 1;
|
| 81 |
+
pointer-events: auto;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
.settings-inline-group {
|
| 85 |
+
display: flex;
|
| 86 |
+
align-items: center;
|
| 87 |
+
gap: 6px;
|
| 88 |
}
|
static/js/history.js
CHANGED
|
@@ -14,6 +14,24 @@ window.NAIHistory = {
|
|
| 14 |
loadingMore: false,
|
| 15 |
gridContainer: null,
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
// ── Save with PNG info extraction for search ──
|
| 18 |
async saveWithSearch(imageB64, source, extraParams) {
|
| 19 |
let searchText = '';
|
|
@@ -75,6 +93,7 @@ window.NAIHistory = {
|
|
| 75 |
imageB64,
|
| 76 |
searchText,
|
| 77 |
});
|
|
|
|
| 78 |
} catch(e) {
|
| 79 |
console.error('Failed to save history:', e);
|
| 80 |
}
|
|
@@ -88,6 +107,8 @@ window.NAIHistory = {
|
|
| 88 |
this.allLoaded = false;
|
| 89 |
this.loadingMore = false;
|
| 90 |
|
|
|
|
|
|
|
| 91 |
try {
|
| 92 |
this.totalCount = await NAIIDB.getHistoryCount();
|
| 93 |
} catch (e) {
|
|
|
|
| 14 |
loadingMore: false,
|
| 15 |
gridContainer: null,
|
| 16 |
|
| 17 |
+
// ── Prune history based on user settings ──
|
| 18 |
+
async pruneHistory() {
|
| 19 |
+
if (!window.NAISettings) return;
|
| 20 |
+
const unitMs = { minute: 60000, hour: 3600000, day: 86400000 };
|
| 21 |
+
|
| 22 |
+
if (NAISettings.get('historyMaxAgeEnabled')) {
|
| 23 |
+
const age = NAISettings.get('historyMaxAge') || 7;
|
| 24 |
+
const unit = NAISettings.get('historyMaxAgeUnit') || 'day';
|
| 25 |
+
const cutoff = Date.now() - age * (unitMs[unit] || 86400000);
|
| 26 |
+
await NAIIDB.deleteHistoryBefore(cutoff).catch(e => console.error('prune by age failed:', e));
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
if (NAISettings.get('historyMaxCountEnabled')) {
|
| 30 |
+
const maxCount = Math.max(1, NAISettings.get('historyMaxCount') || 1000);
|
| 31 |
+
await NAIIDB.deleteHistoryKeepLatest(maxCount).catch(e => console.error('prune by count failed:', e));
|
| 32 |
+
}
|
| 33 |
+
},
|
| 34 |
+
|
| 35 |
// ── Save with PNG info extraction for search ──
|
| 36 |
async saveWithSearch(imageB64, source, extraParams) {
|
| 37 |
let searchText = '';
|
|
|
|
| 93 |
imageB64,
|
| 94 |
searchText,
|
| 95 |
});
|
| 96 |
+
await this.pruneHistory();
|
| 97 |
} catch(e) {
|
| 98 |
console.error('Failed to save history:', e);
|
| 99 |
}
|
|
|
|
| 107 |
this.allLoaded = false;
|
| 108 |
this.loadingMore = false;
|
| 109 |
|
| 110 |
+
await this.pruneHistory();
|
| 111 |
+
|
| 112 |
try {
|
| 113 |
this.totalCount = await NAIIDB.getHistoryCount();
|
| 114 |
} catch (e) {
|
static/js/idb.js
CHANGED
|
@@ -139,5 +139,39 @@ window.NAIIDB = (() => {
|
|
| 139 |
tx.onerror = () => reject(tx.error);
|
| 140 |
});
|
| 141 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
};
|
| 143 |
})();
|
|
|
|
| 139 |
tx.onerror = () => reject(tx.error);
|
| 140 |
});
|
| 141 |
},
|
| 142 |
+
|
| 143 |
+
async deleteHistoryBefore(cutoff) {
|
| 144 |
+
const db = await openDB();
|
| 145 |
+
return new Promise((resolve, reject) => {
|
| 146 |
+
const tx = db.transaction('history', 'readwrite');
|
| 147 |
+
const store = tx.objectStore('history');
|
| 148 |
+
const range = IDBKeyRange.upperBound(cutoff, true);
|
| 149 |
+
const req = store.index('timestamp').openCursor(range);
|
| 150 |
+
req.onsuccess = (e) => {
|
| 151 |
+
const cursor = e.target.result;
|
| 152 |
+
if (cursor) { cursor.delete(); cursor.continue(); }
|
| 153 |
+
};
|
| 154 |
+
req.onerror = () => reject(req.error);
|
| 155 |
+
tx.oncomplete = () => resolve();
|
| 156 |
+
tx.onerror = () => reject(tx.error);
|
| 157 |
+
});
|
| 158 |
+
},
|
| 159 |
+
|
| 160 |
+
async deleteHistoryKeepLatest(maxCount) {
|
| 161 |
+
const db = await openDB();
|
| 162 |
+
return new Promise((resolve, reject) => {
|
| 163 |
+
const tx = db.transaction('history', 'readwrite');
|
| 164 |
+
const store = tx.objectStore('history');
|
| 165 |
+
const keysReq = store.index('timestamp').getAllKeys();
|
| 166 |
+
keysReq.onsuccess = () => {
|
| 167 |
+
const keys = keysReq.result;
|
| 168 |
+
const toDelete = keys.length - maxCount;
|
| 169 |
+
for (let i = 0; i < toDelete; i++) store.delete(keys[i]);
|
| 170 |
+
};
|
| 171 |
+
keysReq.onerror = () => reject(keysReq.error);
|
| 172 |
+
tx.oncomplete = () => resolve();
|
| 173 |
+
tx.onerror = () => reject(tx.error);
|
| 174 |
+
});
|
| 175 |
+
},
|
| 176 |
};
|
| 177 |
})();
|
static/js/settings.js
CHANGED
|
@@ -27,6 +27,11 @@ window.NAISettings = {
|
|
| 27 |
|
| 28 |
// History
|
| 29 |
clearHistoryOnRefresh: true,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
// Cloud
|
| 32 |
saveToCloud: true,
|
|
@@ -220,6 +225,12 @@ window.NAISettings = {
|
|
| 220 |
|
| 221 |
// History
|
| 222 |
$('#setting-clear-history').checked = s.clearHistoryOnRefresh;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
// Cloud
|
| 225 |
$('#setting-save-cloud').checked = s.saveToCloud;
|
|
@@ -278,6 +289,11 @@ window.NAISettings = {
|
|
| 278 |
|
| 279 |
// History
|
| 280 |
bind('setting-clear-history', 'clearHistoryOnRefresh', 'check');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
// Cloud
|
| 283 |
bind('setting-save-cloud', 'saveToCloud', 'check');
|
|
@@ -322,10 +338,18 @@ window.NAISettings = {
|
|
| 322 |
});
|
| 323 |
},
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
_onSettingChanged(key, value) {
|
| 326 |
if (key === 'theme') this.applyTheme();
|
| 327 |
if (key === 'saveToCloud') this.applyCloudCheckbox();
|
| 328 |
if (key === 'freeOnly') this._updateFreeOnlyState();
|
|
|
|
| 329 |
},
|
| 330 |
|
| 331 |
_updateFreeOnlyState() {
|
|
|
|
| 27 |
|
| 28 |
// History
|
| 29 |
clearHistoryOnRefresh: true,
|
| 30 |
+
historyMaxAgeEnabled: false,
|
| 31 |
+
historyMaxAge: 7,
|
| 32 |
+
historyMaxAgeUnit: 'day',
|
| 33 |
+
historyMaxCountEnabled: false,
|
| 34 |
+
historyMaxCount: 1000,
|
| 35 |
|
| 36 |
// Cloud
|
| 37 |
saveToCloud: true,
|
|
|
|
| 225 |
|
| 226 |
// History
|
| 227 |
$('#setting-clear-history').checked = s.clearHistoryOnRefresh;
|
| 228 |
+
$('#setting-history-max-age-enabled').checked = s.historyMaxAgeEnabled;
|
| 229 |
+
$('#setting-history-max-age').value = s.historyMaxAge;
|
| 230 |
+
$('#setting-history-max-age-unit').value = s.historyMaxAgeUnit;
|
| 231 |
+
$('#setting-history-max-count-enabled').checked = s.historyMaxCountEnabled;
|
| 232 |
+
$('#setting-history-max-count').value = s.historyMaxCount;
|
| 233 |
+
this._updateHistorySubRows();
|
| 234 |
|
| 235 |
// Cloud
|
| 236 |
$('#setting-save-cloud').checked = s.saveToCloud;
|
|
|
|
| 289 |
|
| 290 |
// History
|
| 291 |
bind('setting-clear-history', 'clearHistoryOnRefresh', 'check');
|
| 292 |
+
bind('setting-history-max-age-enabled', 'historyMaxAgeEnabled', 'check');
|
| 293 |
+
bind('setting-history-max-age', 'historyMaxAge', 'int');
|
| 294 |
+
bind('setting-history-max-age-unit', 'historyMaxAgeUnit');
|
| 295 |
+
bind('setting-history-max-count-enabled', 'historyMaxCountEnabled', 'check');
|
| 296 |
+
bind('setting-history-max-count', 'historyMaxCount', 'int');
|
| 297 |
|
| 298 |
// Cloud
|
| 299 |
bind('setting-save-cloud', 'saveToCloud', 'check');
|
|
|
|
| 338 |
});
|
| 339 |
},
|
| 340 |
|
| 341 |
+
_updateHistorySubRows() {
|
| 342 |
+
const ageRow = $('#setting-history-max-age-row');
|
| 343 |
+
if (ageRow) ageRow.classList.toggle('enabled', !!this.get('historyMaxAgeEnabled'));
|
| 344 |
+
const countRow = $('#setting-history-max-count-row');
|
| 345 |
+
if (countRow) countRow.classList.toggle('enabled', !!this.get('historyMaxCountEnabled'));
|
| 346 |
+
},
|
| 347 |
+
|
| 348 |
_onSettingChanged(key, value) {
|
| 349 |
if (key === 'theme') this.applyTheme();
|
| 350 |
if (key === 'saveToCloud') this.applyCloudCheckbox();
|
| 351 |
if (key === 'freeOnly') this._updateFreeOnlyState();
|
| 352 |
+
if (key === 'historyMaxAgeEnabled' || key === 'historyMaxCountEnabled') this._updateHistorySubRows();
|
| 353 |
},
|
| 354 |
|
| 355 |
_updateFreeOnlyState() {
|
templates/index.html
CHANGED
|
@@ -25,7 +25,7 @@
|
|
| 25 |
<div class="nav-item" data-page="cloud"><span class="nav-icon">☁️</span><span>云端图片浏览</span></div>
|
| 26 |
<div class="nav-item" data-page="settings"><span class="nav-icon">⚙️</span><span>设置</span></div>
|
| 27 |
</nav>
|
| 28 |
-
<div class="sidebar-footer">NAI Studio v1.0.
|
| 29 |
</aside>
|
| 30 |
|
| 31 |
<!-- Main -->
|
|
@@ -840,6 +840,29 @@
|
|
| 840 |
<label for="setting-clear-history">刷新后清空记录</label>
|
| 841 |
<input type="checkbox" id="setting-clear-history" class="settings-checkbox">
|
| 842 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 843 |
</div>
|
| 844 |
|
| 845 |
<!-- Cloud -->
|
|
|
|
| 25 |
<div class="nav-item" data-page="cloud"><span class="nav-icon">☁️</span><span>云端图片浏览</span></div>
|
| 26 |
<div class="nav-item" data-page="settings"><span class="nav-icon">⚙️</span><span>设置</span></div>
|
| 27 |
</nav>
|
| 28 |
+
<div class="sidebar-footer">NAI Studio v1.0.6</div>
|
| 29 |
</aside>
|
| 30 |
|
| 31 |
<!-- Main -->
|
|
|
|
| 840 |
<label for="setting-clear-history">刷新后清空记录</label>
|
| 841 |
<input type="checkbox" id="setting-clear-history" class="settings-checkbox">
|
| 842 |
</div>
|
| 843 |
+
<div class="settings-row">
|
| 844 |
+
<label for="setting-history-max-age-enabled">限制保留时间</label>
|
| 845 |
+
<input type="checkbox" id="setting-history-max-age-enabled" class="settings-checkbox">
|
| 846 |
+
</div>
|
| 847 |
+
<div class="settings-row settings-sub-row" id="setting-history-max-age-row">
|
| 848 |
+
<label>保留时间上限</label>
|
| 849 |
+
<div class="settings-inline-group">
|
| 850 |
+
<input type="number" id="setting-history-max-age" class="form-input" style="width:72px;" min="1" max="99999" value="7">
|
| 851 |
+
<select id="setting-history-max-age-unit" class="form-select" style="width:80px;">
|
| 852 |
+
<option value="minute">分钟</option>
|
| 853 |
+
<option value="hour">小时</option>
|
| 854 |
+
<option value="day" selected>天</option>
|
| 855 |
+
</select>
|
| 856 |
+
</div>
|
| 857 |
+
</div>
|
| 858 |
+
<div class="settings-row">
|
| 859 |
+
<label for="setting-history-max-count-enabled">限制保留数量</label>
|
| 860 |
+
<input type="checkbox" id="setting-history-max-count-enabled" class="settings-checkbox">
|
| 861 |
+
</div>
|
| 862 |
+
<div class="settings-row settings-sub-row" id="setting-history-max-count-row">
|
| 863 |
+
<label>最多保留条数</label>
|
| 864 |
+
<input type="number" id="setting-history-max-count" class="form-input" style="width:96px;" min="1" max="65536" value="1000">
|
| 865 |
+
</div>
|
| 866 |
</div>
|
| 867 |
|
| 868 |
<!-- Cloud -->
|