-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
363 lines (311 loc) · 12.3 KB
/
Copy pathscript.js
File metadata and controls
363 lines (311 loc) · 12.3 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* DokuWiki AI Assistant Plugin JavaScript
* Version: 2024-12-19-FINAL (Clean Version)
*/
function handleAIAssist() {
// Prevent duplicate requests
if (!isProcessing) {
isProcessing = true;
}
var textarea = document.getElementById('wiki__text');
if (!textarea) {
textarea = document.querySelector('textarea[name="wikitext"]') ||
document.querySelector('textarea.edit') ||
document.querySelector('#edit textarea');
}
if (!textarea) {
return;
}
// Get selection immediately
textarea.focus();
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var selectedText = textarea.value.substring(start, end).trim();
continueAIAssist(textarea, start, end, selectedText);
}
function continueAIAssist(textarea, start, end, selectedText) {
// 선택 텍스트 처리 로직
if (start !== end && selectedText.length > 0) {
// Selected text mode
}
else {
// Full text mode
// Ctrl+A 체크
if (ctrlAPressed) {
start = 0;
end = textarea.value.length;
selectedText = textarea.value.trim();
ctrlAPressed = false;
}
// 커서가 끝에 있는 경우
else if (start === textarea.value.length) {
start = 0;
end = textarea.value.length;
selectedText = textarea.value.trim();
}
// 진짜 아무것도 선택되지 않은 경우
else {
var userWantsFullText = confirm(window.AIASSIST_LANG.no_text_selected_confirm || 'No text selected. Do you want to improve the entire text with AI?');
if (userWantsFullText) {
start = 0;
end = textarea.value.length;
selectedText = textarea.value.trim();
} else {
isProcessing = false;
return;
}
}
}
// 최종 검증 - 빈 텍스트 차단
if (!selectedText || selectedText.trim() === '') {
isProcessing = false;
showErrorModal(window.AIASSIST_LANG.no_text_to_process || 'No text to process.');
return;
}
// 요청사항 또는 request 감지 및 안내
if (selectedText.includes('요청사항:') || selectedText.toLowerCase().includes('request:')) {
var customRequestDetected = true;
showLoadingModal(window.AIASSIST_LANG.processing_with_request || 'Processing text with your custom request...');
} else {
showLoadingModal();
}
if (!selectedText) {
return;
}
// Set processing flag
isProcessing = true;
var fullText = textarea.value;
// 컨텍스트 길이는 설정에서 가져오거나 기본값 500 사용
var contextLength = window.AIASSIST_CONTEXT_LENGTH || 500;
var beforeText = fullText.substring(Math.max(0, start - contextLength), start);
var afterText = fullText.substring(end, Math.min(fullText.length, end + contextLength));
var formData = new FormData();
formData.append('call', 'aiassist');
formData.append('selected_text', selectedText);
formData.append('before_text', beforeText);
formData.append('after_text', afterText);
fetch(DOKU_BASE + 'doku.php', {
method: 'POST',
body: formData
})
.then(function(response) {
if (!response.ok) {
throw new Error('HTTP ' + response.status);
}
return response.json();
})
.then(function(data) {
// Reset processing flag
isProcessing = false;
hideLoadingModal();
if (data.error) {
showErrorModal((window.AIASSIST_LANG.error_ai_processing || 'An error occurred during AI processing: ') + data.error);
return;
}
if (data.improved_text && data.improved_text.trim() !== '') {
showPreviewModal(selectedText, data.improved_text, function(approved) {
if (approved) {
replaceSelectedText(textarea, {start: start, end: end}, data.improved_text);
}
});
} else {
showErrorModal(window.AIASSIST_LANG.error_no_improved_text || 'No improved text in AI response.');
}
})
.catch(function(error) {
// Reset processing flag
isProcessing = false;
hideLoadingModal();
showErrorModal((window.AIASSIST_LANG.error_server_request || 'Server request failed: ') + error.message);
});
}
function replaceSelectedText(textarea, selection, newText) {
var fullText = textarea.value;
var newFullText = fullText.substring(0, selection.start) + newText + fullText.substring(selection.end);
textarea.value = newFullText;
var newCursorPos = selection.start + newText.length;
textarea.setSelectionRange(newCursorPos, newCursorPos);
textarea.focus();
}
function showLoadingModal(customMessage) {
var message = customMessage || window.AIASSIST_LANG.processing || 'AI is processing your text...';
var modal = document.createElement('div');
modal.id = 'aiassist-loading-modal';
modal.innerHTML = `
<div class="aiassist-modal-overlay">
<div class="aiassist-modal-content">
<div class="aiassist-loading">
<div class="aiassist-spinner"></div>
<p>${message}</p>
</div>
</div>
</div>
`;
document.body.appendChild(modal);
}
function hideLoadingModal() {
var modal = document.getElementById('aiassist-loading-modal');
if (modal) modal.remove();
}
function showErrorModal(message) {
var modal = document.createElement('div');
modal.id = 'aiassist-error-modal';
modal.innerHTML = `
<div class="aiassist-modal-overlay">
<div class="aiassist-modal-content">
<div class="aiassist-modal-header">
<h3>${window.AIASSIST_LANG.error || 'Error'}</h3>
<button class="aiassist-close-btn" onclick="closeErrorModal()">×</button>
</div>
<div class="aiassist-modal-body">
<p>${message}</p>
</div>
<div class="aiassist-modal-footer">
<button class="aiassist-btn aiassist-btn-cancel" onclick="closeErrorModal()">${window.AIASSIST_LANG.confirm || 'OK'}</button>
</div>
</div>
</div>
`;
document.body.appendChild(modal);
}
function closeErrorModal() {
var modal = document.getElementById('aiassist-error-modal');
if (modal) modal.remove();
}
function showPreviewModal(originalText, improvedText, callback) {
// Remove existing modal
var existingModal = document.getElementById('aiassist-preview-modal');
if (existingModal) existingModal.remove();
var modal = document.createElement('div');
modal.id = 'aiassist-preview-modal';
modal.innerHTML = `
<div class="aiassist-modal-overlay">
<div class="aiassist-modal-content aiassist-preview-modal">
<div class="aiassist-modal-header">
<h3>${window.AIASSIST_LANG.preview_title || 'AI Text Improvement Preview'}</h3>
<button class="aiassist-close-btn" onclick="closePreviewModal()">×</button>
</div>
<div class="aiassist-modal-body">
<div class="aiassist-comparison">
<div class="aiassist-original">
<h4>${window.AIASSIST_LANG.original_text || 'Original Text:'}</h4>
<div class="aiassist-text-box">
<pre>${escapeHtml(originalText)}</pre>
</div>
</div>
<div class="aiassist-improved">
<h4>${window.AIASSIST_LANG.improved_text || 'AI Improved Text:'}</h4>
<div class="aiassist-text-box">
<pre>${escapeHtml(improvedText)}</pre>
</div>
</div>
</div>
</div>
<div class="aiassist-modal-footer">
<button class="aiassist-btn aiassist-btn-cancel" onclick="closePreviewModal()">${window.AIASSIST_LANG.cancel || 'Cancel'}</button>
<button class="aiassist-btn aiassist-btn-apply" onclick="applyImprovement()">${window.AIASSIST_LANG.apply_changes || 'Apply Changes'}</button>
</div>
</div>
</div>
`;
document.body.appendChild(modal);
modal.style.display = 'block';
window.aiassistCallback = callback;
}
function closePreviewModal() {
var modal = document.getElementById('aiassist-preview-modal');
if (modal) modal.remove();
if (window.aiassistCallback) {
window.aiassistCallback(false);
delete window.aiassistCallback;
}
}
function applyImprovement() {
var modal = document.getElementById('aiassist-preview-modal');
if (modal) modal.remove();
if (window.aiassistCallback) {
window.aiassistCallback(true);
delete window.aiassistCallback;
}
}
function escapeHtml(text) {
var div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// Track Ctrl+A usage
var ctrlAPressed = false;
var isProcessing = false;
// Initialize
if (!window.aiAssistantInitialized) {
window.aiAssistantInitialized = true;
function initAIAssistant() {
function bindButton() {
var buttons = document.querySelectorAll('.aiassist_button');
buttons.forEach(function(btn) {
btn.removeEventListener('click', handleButtonClick);
btn.onclick = null;
btn.removeAttribute('onclick');
btn.addEventListener('click', handleButtonClick, true);
btn.addEventListener('mousedown', function(e) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
}, true);
// AI 버튼을 툴바의 가장 끝으로 이동
moveButtonToEnd(btn);
});
}
function moveButtonToEnd(button) {
var toolbar = button.closest('.toolbar');
if (toolbar && button.parentNode) {
// 버튼을 툴바의 맨 끝으로 이동
toolbar.appendChild(button.parentNode);
// 추가 보장: 다른 플러그인들이 나중에 추가되는 경우를 대비
setTimeout(function() {
if (toolbar && button.parentNode) {
toolbar.appendChild(button.parentNode);
}
}, 100);
// 한번 더 지연 실행으로 확실하게 보장
setTimeout(function() {
if (toolbar && button.parentNode) {
toolbar.appendChild(button.parentNode);
}
}, 500);
}
}
function handleButtonClick(e) {
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
if (e.defaultPrevented === false) {
e.preventDefault();
}
if (isProcessing) {
return false;
}
isProcessing = true;
setTimeout(function() {
handleAIAssist();
}, 10);
return false;
}
bindButton();
setTimeout(bindButton, 500);
setTimeout(bindButton, 1500);
// Track Ctrl+A presses
document.addEventListener('keydown', function(e) {
if (e.ctrlKey && e.key === 'a') {
ctrlAPressed = true;
setTimeout(function() { ctrlAPressed = false; }, 3000);
}
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initAIAssistant);
} else {
initAIAssistant();
}
window.addEventListener('load', initAIAssistant);
}