Spaces:
Running
Running
function saveToHistory(title) { | |
if(!title) { | |
title = document.getElementById('query').value.slice(0, 10); | |
} | |
const historyItem = { | |
query: document.getElementById('query').value, | |
promptEn: document.getElementById('promptEn').value, | |
promptMyLanguage: document.getElementById('promptMyLanguage').value, | |
danbooruTags: document.getElementById('danbooruTags').value, | |
timestamp: new Date().toISOString(), | |
title: title | |
}; | |
let history = JSON.parse(localStorage.getItem('gemini_prompt_history') || '[]'); | |
history.unshift(historyItem); | |
// 履歴の合計サイズが3MB以下になるまで古い項目を削除 | |
while (JSON.stringify(history).length > 3 * 1024 * 1024) { | |
history.pop(); | |
} | |
localStorage.setItem('gemini_prompt_history', JSON.stringify(history)); | |
updateHistoryList(); | |
} | |
function updateHistoryList() { | |
const historyList = document.getElementById('historyList'); | |
const noHistoryMessage = document.getElementById('noHistoryMessage'); | |
historyList.innerHTML = ''; | |
const history = JSON.parse(localStorage.getItem('gemini_prompt_history') || '[]'); | |
if (history.length === 0) { | |
noHistoryMessage.classList.remove('d-none'); | |
historyList.classList.add('d-none'); | |
} else { | |
noHistoryMessage.classList.add('d-none'); | |
historyList.classList.remove('d-none'); | |
history.forEach((item, index) => { | |
const li = document.createElement('li'); | |
li.className = 'list-group-item list-group-item-action d-flex justify-content-between align-items-start'; | |
const contentDiv = document.createElement('div'); | |
contentDiv.className = 'ms-2 me-auto'; | |
contentDiv.style.cursor = 'pointer'; | |
contentDiv.onclick = () => loadHistoryItem(index); | |
const titleDiv = document.createElement('div'); | |
titleDiv.className = 'fw-bold text-truncate'; | |
titleDiv.textContent = item.title || item.query.slice(0, 10); | |
const dateDiv = document.createElement('div'); | |
dateDiv.className = 'small text-muted'; | |
dateDiv.textContent = new Date(item.timestamp).toLocaleString(); | |
contentDiv.appendChild(titleDiv); | |
contentDiv.appendChild(dateDiv); | |
const buttonsContainer = document.createElement('div'); | |
buttonsContainer.className = 'd-flex'; | |
const editButton = document.createElement('button'); | |
editButton.className = 'btn btn-secondary btn-sm me-2'; | |
editButton.innerHTML = '<i class="fas fa-pencil-alt"></i>'; | |
editButton.onclick = (e) => { | |
e.stopPropagation(); | |
editHistoryItemTitle(index, titleDiv); | |
}; | |
const deleteButton = document.createElement('button'); | |
deleteButton.className = 'btn btn-danger btn-sm'; | |
deleteButton.innerHTML = '<i class="fas fa-trash"></i>'; | |
deleteButton.onclick = (e) => { | |
e.stopPropagation(); | |
deleteHistoryItem(index); | |
}; | |
buttonsContainer.appendChild(editButton); | |
buttonsContainer.appendChild(deleteButton); | |
li.appendChild(contentDiv); | |
li.appendChild(buttonsContainer); | |
historyList.appendChild(li); | |
contentDiv.style.width = `calc(100% - ${buttonsContainer.offsetWidth}px)`; | |
}); | |
} | |
} | |
function deleteHistoryItem(index) { | |
if (confirm('この履歴項目を削除してもよろしいですか?')) { | |
let history = JSON.parse(localStorage.getItem('gemini_prompt_history') || '[]'); | |
history.splice(index, 1); | |
localStorage.setItem('gemini_prompt_history', JSON.stringify(history)); | |
updateHistoryList(); | |
} | |
} | |
function loadHistoryItem(index) { | |
const history = JSON.parse(localStorage.getItem('gemini_prompt_history') || '[]'); | |
const item = history[index]; | |
document.getElementById('query').value = item.query; | |
document.getElementById('promptEn').value = item.promptEn; | |
document.getElementById('promptMyLanguage').value = item.promptMyLanguage; | |
document.getElementById('danbooruTags').value = item.danbooruTags; | |
saveToUserStorage(true); | |
} | |
function clearHistory() { | |
if (confirm('本当に履歴をすべて削除してもよろしいですか?')) { | |
localStorage.removeItem('gemini_prompt_history'); | |
updateHistoryList(); | |
} | |
} | |
function createHistoryItem(item, index) { | |
const li = document.createElement('li'); | |
li.className = 'list-group-item d-flex justify-content-between align-items-center'; | |
const titleSpan = document.createElement('span'); | |
titleSpan.textContent = item.title || item.query.slice(0, 10); | |
titleSpan.className = 'me-2'; | |
li.appendChild(titleSpan); | |
const buttonsContainer = document.createElement('div'); | |
const editButton = document.createElement('button'); | |
editButton.className = 'btn btn-sm btn-secondary me-2'; | |
editButton.innerHTML = '<i class="fas fa-pencil-alt"></i>'; | |
editButton.onclick = () => editHistoryItemTitle(index, titleSpan); | |
const useButton = document.createElement('button'); | |
useButton.className = 'btn btn-sm btn-primary me-2'; | |
useButton.innerHTML = '<i class="fas fa-redo"></i>'; | |
useButton.onclick = () => useHistoryItem(index); | |
const deleteButton = document.createElement('button'); | |
deleteButton.className = 'btn btn-sm btn-danger'; | |
deleteButton.innerHTML = '<i class="fas fa-trash"></i>'; | |
deleteButton.onclick = () => { | |
if (confirm('この履歴項目を削除してもよろしいですか?')) { | |
deleteHistoryItem(index); | |
} | |
}; | |
buttonsContainer.appendChild(editButton); | |
buttonsContainer.appendChild(useButton); | |
buttonsContainer.appendChild(deleteButton); | |
li.appendChild(buttonsContainer); | |
return li; | |
} | |
function editHistoryItemTitle(index, titleDiv) { | |
const history = JSON.parse(localStorage.getItem('gemini_prompt_history') || '[]'); | |
const item = history[index]; | |
const currentTitle = item.title || item.query.slice(0, 10); | |
const newTitle = prompt('新しいタイトルを入力してください:', currentTitle); | |
if (newTitle !== null && newTitle.trim() !== '') { | |
item.title = newTitle.trim(); | |
history[index] = item; | |
localStorage.setItem('gemini_prompt_history', JSON.stringify(history)); | |
titleDiv.textContent = newTitle.trim(); | |
} | |
} | |