File size: 9,168 Bytes
a4ac627
 
 
 
0aabf7d
 
 
 
 
a4ac627
 
0aabf7d
 
 
 
a4ac627
 
 
 
0aabf7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e76749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0aabf7d
 
 
3e76749
0aabf7d
 
 
 
 
 
a4ac627
 
 
0aabf7d
 
 
 
 
a4ac627
0aabf7d
 
a4ac627
 
 
 
 
 
 
 
 
 
 
0aabf7d
 
 
 
 
 
 
 
a4ac627
 
 
0aabf7d
a4ac627
0aabf7d
a4ac627
 
0aabf7d
3e76749
 
 
 
0aabf7d
 
 
3e76749
0aabf7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e76749
0aabf7d
 
 
 
 
 
 
 
a4ac627
 
 
 
 
0aabf7d
 
 
a4ac627
 
 
 
 
0aabf7d
 
 
 
 
 
 
 
 
3e76749
0aabf7d
 
 
 
a4ac627
0aabf7d
 
 
 
 
 
a4ac627
 
 
 
 
 
3e76749
a4ac627
 
 
 
 
 
 
3e76749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
208
209
210
211
212
213
214
215
216
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');

        // 容量プログレスバーを追加
        const storageUsage = JSON.stringify(history).length;
        const storageLimit = 3 * 1024 * 1024; // 3MB
        const usagePercentage = (storageUsage / storageLimit) * 100;

        const progressBarContainer = document.createElement('div');
        progressBarContainer.className = 'mb-3';
        progressBarContainer.innerHTML = `

            <div class="progress" style="height: 24px; border: 1px solid #dee2e6; position: relative;">

                <div class="progress-bar ${usagePercentage > 90 ? 'bg-danger' : 'bg-primary'}" role="progressbar" 

                     style="width: ${usagePercentage}%;" aria-valuenow="${usagePercentage}" aria-valuemin="0" aria-valuemax="100"></div>

                <div class="position-absolute w-100 h-100 d-flex align-items-center justify-content-center" style="top: 0; left: 0;">

                    <span style="color: white; text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;">

                        ${(storageUsage / 1024 / 1024).toFixed(2)}MB / ${storageLimit / 1024 / 1024}MB

                    </span>

                </div>

            </div>

        `;
        historyList.appendChild(progressBarContainer);

        // 検索フォームを追加
        const searchForm = document.createElement('div');
        searchForm.className = 'mb-3';
        searchForm.innerHTML = `

            <input type="text" class="form-control" id="historySearchInput" placeholder="履歴を検索...">

        `;
        historyList.appendChild(searchForm);

        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';
            li.dataset.item = JSON.stringify(item);

            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)`;
        });

        // 検索機能を追加
        const searchInput = document.getElementById('historySearchInput');
        searchInput.addEventListener('input', filterHistory);
    }
}
function deleteHistoryItem(index) {
    if (confirm('Are you sure you want to delete this history item?')) {
        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('Are you sure you want to delete all history items?')) {
        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('Are you sure you want to delete this history item?')) {
            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('New title:', 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();
    }
}

function filterHistory() {
    const searchInput = document.getElementById('historySearchInput');
    const searchTerm = searchInput.value.toLowerCase();
    const historyItems = document.querySelectorAll('#historyList li[data-item]');

    historyItems.forEach(item => {
        const itemData = JSON.parse(item.dataset.item);
        const searchableText = `${itemData.title} ${itemData.query} ${itemData.promptEn} ${itemData.promptMyLanguage} ${itemData.danbooruTags}`.toLowerCase();

        if (searchableText.includes(searchTerm)) {
            item.classList.remove('d-none');
        } else {
            item.classList.add('d-none');
        }
    });
}