-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomponents.js
More file actions
363 lines (319 loc) · 17.5 KB
/
components.js
File metadata and controls
363 lines (319 loc) · 17.5 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
// components.js
import { state, CONSTANTS } from './state.js';
// --- DOM 요소 캐싱 ---
const getEl = id => document.getElementById(id);
export const folderList = getEl('folder-list'), noteList = getEl('note-list');
export const addFolderBtn = getEl('add-folder-btn'), addNoteBtn = getEl('add-note-btn');
export const emptyTrashBtn = getEl('empty-trash-btn');
export const notesPanelTitle = getEl('notes-panel-title');
export const searchInput = getEl('search-input'), clearSearchBtn = getEl('clear-search');
export const noteSortSelect = getEl('note-sort-select');
export const importFileInput = getEl('import-file-input');
export const editorContainer = getEl(CONSTANTS.EDITOR.DOM_IDS.container), placeholderContainer = getEl('placeholder-container');
export const noteTitleInput = getEl(CONSTANTS.EDITOR.DOM_IDS.titleInput), noteContentTextarea = getEl(CONSTANTS.EDITOR.DOM_IDS.contentTextArea), noteContentView = getEl(CONSTANTS.EDITOR.DOM_IDS.contentView);
export const editorFooter = getEl(CONSTANTS.EDITOR.DOM_IDS.footer), toastContainer = getEl('toast-container');
const modal = getEl('modal'), modalTitle = getEl('modal-title'), modalMessage = getEl('modal-message');
const modalErrorMessage = getEl('modal-error-message');
const modalForm = getEl('modal-form'), modalInput = getEl('modal-input');
const modalConfirmBtn = getEl('modal-confirm-btn'), modalCancelBtn = getEl('modal-cancel-btn');
const modalCloseBtn = getEl('modal-close-btn');
export const itemTemplate = getEl('item-template');
export const shortcutGuideBtn = getEl('shortcut-guide-btn');
export const settingsBtn = getEl('settings-btn');
export const saveStatusIndicator = getEl(CONSTANTS.EDITOR.DOM_IDS.saveStatus);
export const datePickerPopover = getEl('date-picker-popover');
export const yearInput = getEl('year-input');
export const monthInput = getEl('month-input');
export const datePickerCloseBtn = getEl('date-picker-close-btn');
export const datePickerTodayBtn = getEl('date-picker-today-btn');
export const datePickerConfirmBtn = getEl('date-picker-confirm-btn');
// --- 설정 모달 DOM 요소 캐싱 ---
export const settingsModal = getEl('settings-modal');
export const settingsModalCloseBtn = getEl('settings-modal-close-btn');
export const settingsTabs = document.querySelector('.settings-tabs');
export const settingsTabPanels = document.querySelectorAll('.settings-tab-panel');
export const settingsCol1Width = getEl('settings-col1-width');
export const settingsCol2Width = getEl('settings-col2-width');
export const settingsEditorFontFamily = getEl('settings-editor-font-family');
export const settingsEditorFontSize = getEl('settings-editor-font-size');
export const settingsWeatherLat = getEl('settings-weather-lat');
export const settingsWeatherLon = getEl('settings-weather-lon');
export const settingsWeatherCitySearch = getEl('settings-weather-city-search');
export const settingsWeatherCitySearchBtn = getEl('settings-weather-city-search-btn');
export const settingsWeatherCityResults = getEl('settings-weather-city-results');
export const settingsExportBtn = getEl('settings-export-btn');
export const settingsImportBtn = getEl('settings-import-btn');
export const settingsResetBtn = getEl('settings-reset-btn');
export const settingsSaveBtn = getEl('settings-save-btn');
// [기능 추가] 저장소 사용량 표시 요소를 내보냅니다.
export const settingsStorageUsage = getEl('settings-storage-usage');
// [기능 추가] 습관 트래커 관련 DOM 요소를 내보냅니다.
export const habitTrackerBtn = getEl('habit-tracker-btn');
export const habitTrackerContainer = getEl('habit-tracker-container');
export const habitTrackerIframe = getEl('habit-tracker-iframe');
export const closeHabitTrackerBtn = getEl('close-habit-tracker-btn');
// [기능 추가] 다이어트 챌린지 관련 DOM 요소를 내보냅니다.
export const dietChallengeBtn = getEl('diet-challenge-btn');
export const dietChallengeContainer = getEl('diet-challenge-container');
export const dietChallengeIframe = getEl('diet-challenge-iframe');
export const closeDietChallengeBtn = getEl('close-diet-challenge-btn');
// --- UI 유틸리티 ---
export const formatDate = d => {
if (!d) return '';
const date = new Date(d);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const timePart = date.toLocaleTimeString('ko-KR', { hour: 'numeric', minute: 'numeric', hour12: true });
return `${year}. ${month}. ${day}. ${timePart}`;
};
export const showToast = (message, type = CONSTANTS.TOAST_TYPE.SUCCESS, duration = CONSTANTS.TOAST_DURATION) => {
if (!toastContainer) return;
// [BUG FIX] duration이 0이면 UI를 가리는 문제를 방지하기 위해 토스트 대신 모달(showAlert)로 처리합니다.
// 이는 사용자의 확인이 반드시 필요한 중요한 메시지(예: 심각한 오류)에 해당합니다.
if (duration === 0) {
showAlert({
title: type === CONSTANTS.TOAST_TYPE.ERROR ? '❗️ 오류' : '🔔 알림',
message: message,
confirmText: '✅ 확인',
});
return; // 여기서 함수 실행을 종료합니다.
}
const toast = document.createElement('div');
toast.className = `toast ${type}`;
if (message instanceof Node) {
toast.appendChild(message);
} else {
toast.textContent = message;
}
// 이제 duration이 0인 경우는 없으므로, 해당 조건 분기(if/else)가 필요 없습니다.
setTimeout(() => {
if (toast.parentElement) toast.remove();
}, duration);
// [개선] appendChild 대신 prepend를 사용하여 새 토스트가 항상 맨 위에 표시되도록 합니다.
toastContainer.prepend(toast);
};
// [버그 수정] 모달 이벤트 처리 로직을 표준적이고 안정적인 방식으로 전면 재작성
const _showModalInternal = ({ type, title, message = '', placeholder = '', initialValue = '', confirmText = '✅ 확인', cancelText = '❌ 취소', isHtml = false, hideConfirmButton = false, hideCancelButton = false, validationFn = null, confirmButtonType = 'confirm' }) => {
// [BUG FIX] 모달을 열기 전, 현재 포커스를 받은 요소를 저장합니다.
const elementToFocusOnClose = document.activeElement;
return new Promise(resolve => {
// --- 1. UI 설정 ---
modalTitle.textContent = title;
modalMessage.innerHTML = '';
if (message instanceof Node) modalMessage.appendChild(message);
else if (isHtml) modalMessage.innerHTML = message;
else modalMessage.textContent = message;
modalErrorMessage.textContent = '';
modalErrorMessage.style.display = 'none';
modalConfirmBtn.textContent = confirmText;
modalCancelBtn.textContent = cancelText;
modalConfirmBtn.className = 'modal-button ripple-effect ' + confirmButtonType;
modalConfirmBtn.style.display = hideConfirmButton ? 'none' : 'inline-block';
modalCancelBtn.style.display = (hideCancelButton || type === 'alert') ? 'none' : 'inline-block';
modalCloseBtn.style.display = 'block';
modalInput.style.display = type === CONSTANTS.MODAL_TYPE.PROMPT ? 'block' : 'none';
modalMessage.style.display = message ? 'block' : 'none';
modalInput.value = initialValue;
modalInput.placeholder = placeholder;
// --- 2. 이벤트 핸들러 정의 ---
// 핸들러는 이 Promise 스코프 내에서 정의되어 외부 상태에 영향을 주지 않음
let hasUserInput = false;
const runValidation = (force = false) => {
if (!validationFn) return true;
const { isValid, message } = validationFn(modalInput.value);
modalConfirmBtn.disabled = !isValid;
if ((force || hasUserInput) && !isValid && message) {
modalErrorMessage.textContent = message;
modalErrorMessage.style.display = 'block';
} else {
modalErrorMessage.style.display = 'none';
}
return isValid;
};
const handleInput = () => {
if (!hasUserInput) hasUserInput = true;
runValidation();
};
// 확인 버튼 클릭 시, 유효성 검사를 통과해야만 모달을 닫도록 처리
const handleConfirmClick = (e) => {
if (validationFn && !runValidation(true)) {
e.preventDefault(); // <form method="dialog">의 기본 동작(모달 닫기)을 막음
return;
}
modal.close('confirm'); // 유효성 통과 시 'confirm' 값으로 모달 닫기
};
// 취소 관련 버튼은 항상 'cancel' 값으로 모달을 닫음
const handleCancelClick = () => modal.close('cancel');
const handleKeydown = (e) => {
if (e.key === 'Enter') {
e.preventDefault(); // 기본 form 제출 동작 방지
modalConfirmBtn.click(); // 정의된 확인 버튼 로직을 그대로 실행
} else if (e.key === 'Escape') {
// Escape는 dialog의 기본 동작으로 cancel 이벤트와 close 이벤트를 발생시키므로,
// handleClose가 자동으로 처리함. 여기서는 handleCancelClick을 명시적으로 호출하여 일관성 유지.
handleCancelClick();
}
};
// [핵심 안정성] 'close' 이벤트를 유일한 Promise 종료 지점으로 사용.
// 어떤 방식(버튼 클릭, ESC 키, form 제출 등)으로든 모달이 닫히면 항상 호출됨.
const handleClose = () => {
// --- 4. 리스너 정리 ---
// 이 모달 인스턴스를 위해 추가된 모든 이벤트 리스너를 깨끗하게 제거하여 메모리 누수 방지
modalConfirmBtn.removeEventListener('click', handleConfirmClick);
modalCancelBtn.removeEventListener('click', handleCancelClick);
modalCloseBtn.removeEventListener('click', handleCancelClick);
modalInput.removeEventListener('input', handleInput);
modalInput.removeEventListener('keydown', handleKeydown);
modal.removeEventListener('close', handleClose);
// [BUG FIX] 모달이 닫힌 후, 이전에 저장해 둔 요소로 포커스를 되돌립니다.
try {
if (elementToFocusOnClose && typeof elementToFocusOnClose.focus === 'function') {
elementToFocusOnClose.focus();
}
} catch (e) {
console.warn("Failed to restore focus to the previous element.", e);
}
// --- 5. 결과 반환 ---
// modal.returnValue 값을 기반으로 Promise의 결과를 결정
let result = null;
if (modal.returnValue === 'confirm') {
if (type === CONSTANTS.MODAL_TYPE.PROMPT) result = modalInput.value;
else if (message instanceof Node && message.querySelector('select')) result = message.querySelector('select').value;
else result = true;
}
resolve(result); // Promise를 최종적으로 해결
};
// --- 3. 리스너 연결 ---
modalConfirmBtn.addEventListener('click', handleConfirmClick);
modalCancelBtn.addEventListener('click', handleCancelClick);
modalCloseBtn.addEventListener('click', handleCancelClick);
modal.addEventListener('close', handleClose);
if (type === CONSTANTS.MODAL_TYPE.PROMPT) {
modalInput.addEventListener('keydown', handleKeydown);
if (validationFn) {
modalInput.addEventListener('input', handleInput);
runValidation(); // 초기 버튼 상태 설정
}
}
modal.showModal();
if (type === CONSTANTS.MODAL_TYPE.PROMPT) {
modalInput.focus();
modalInput.select();
}
});
};
export const showAlert = (options) => _showModalInternal({ ...options, type: CONSTANTS.MODAL_TYPE.ALERT, hideCancelButton: true });
export const showConfirm = (options) => _showModalInternal({ ...options, type: CONSTANTS.MODAL_TYPE.CONFIRM });
export const showPrompt = (options) => _showModalInternal({ ...options, type: CONSTANTS.MODAL_TYPE.PROMPT });
export const showFolderSelectPrompt = async ({ title, message }) => {
const formContent = document.createElement('div');
formContent.style.display = 'flex';
formContent.style.flexDirection = 'column';
formContent.style.gap = '15px';
const messageP = document.createElement('p');
messageP.textContent = message;
const select = document.createElement('select');
select.className = 'modal-input';
const realFolders = state.folders.filter(folder => !Object.values(CONSTANTS.VIRTUAL_FOLDERS).some(vf => vf.id === folder.id));
realFolders.forEach(folder => {
const option = document.createElement('option');
option.value = folder.id;
option.textContent = `📁 ${folder.name}`;
select.appendChild(option);
});
formContent.appendChild(messageP);
formContent.appendChild(select);
if (realFolders.length === 0) {
messageP.textContent = '노트를 복원할 폴더가 없습니다. 먼저 새 폴더를 만들어주세요.';
select.style.display = 'none';
await showAlert({ title, message: formContent, confirmText: '✅ 확인' });
return null;
}
return await _showModalInternal({
type: CONSTANTS.MODAL_TYPE.CONFIRM, title, message: formContent,
confirmText: '✅ 폴더 선택', cancelText: '❌ 취소'
});
};
export const showDatePickerPopover = ({ initialDate }) => {
return new Promise(resolve => {
yearInput.value = initialDate.getFullYear();
monthInput.value = initialDate.getMonth() + 1;
datePickerPopover.style.display = 'flex';
yearInput.focus();
yearInput.select();
const cleanup = (result) => {
datePickerPopover.style.display = 'none';
datePickerConfirmBtn.removeEventListener('click', onConfirm);
datePickerCloseBtn.removeEventListener('click', onCancel);
datePickerTodayBtn.removeEventListener('click', onToday);
document.removeEventListener('click', onOutsideClick, true);
datePickerPopover.removeEventListener('keydown', onKeydown);
resolve(result);
};
const onConfirm = () => {
const year = parseInt(yearInput.value, 10);
const month = parseInt(monthInput.value, 10);
if (isNaN(year) || isNaN(month) || month < 1 || month > 12 || year < 1900 || year > 2200) {
showToast('🤔 유효한 년(1900-2200)과 월(1-12)을 입력해주세요.', CONSTANTS.TOAST_TYPE.ERROR);
return;
}
cleanup({ year, month: month - 1 });
};
const onToday = () => {
const today = new Date();
cleanup({ year: today.getFullYear(), month: today.getMonth() });
};
const onCancel = () => cleanup(null);
const onOutsideClick = (e) => { if (!datePickerPopover.contains(e.target) && e.target.id !== 'calendar-month-year') onCancel(); };
const onKeydown = (e) => { if (e.key === 'Enter') onConfirm(); else if (e.key === 'Escape') onCancel(); };
datePickerConfirmBtn.addEventListener('click', onConfirm);
datePickerCloseBtn.addEventListener('click', onCancel);
datePickerTodayBtn.addEventListener('click', onToday);
document.addEventListener('click', onOutsideClick, true);
datePickerPopover.addEventListener('keydown', onKeydown);
});
};
export const showShortcutModal = () => {
const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
const ctrlOrCmd = isMac ? 'Cmd' : 'Ctrl';
const shortcuts = [
{ key: 'Alt + Shift + N', desc: '✨ 새 폴더 만들기' },
{ key: 'Alt + N', desc: '✍️ 새 노트 만들기' },
{ key: 'F2 / 더블클릭', desc: '✏️ 폴더/노트 이름 변경' },
{ key: '↑ / ↓', desc: '↕️ 폴더/노트 이동' },
{ key: 'Enter', desc: '↵️ 폴더/노트 선택' },
{ key: '드래그 앤 드롭', desc: '🖐️ 폴더 위치 변경, 노트를 다른 폴더로 이동' },
];
const list = document.createElement('ul');
list.className = 'shortcut-list';
shortcuts.forEach(sc => {
const li = document.createElement('li');
const keySpan = document.createElement('span');
keySpan.className = 'shortcut-key';
keySpan.textContent = sc.key;
const descSpan = document.createElement('span');
descSpan.className = 'shortcut-desc';
descSpan.textContent = sc.desc;
li.appendChild(keySpan);
li.appendChild(descSpan);
list.appendChild(li);
});
showAlert({ title: CONSTANTS.MODAL_TITLES.SHORTCUT_GUIDE, message: list, hideConfirmButton: true });
};
export const sortNotes = (notes, sortOrder) => {
if (!notes) return [];
const sorted = [...notes];
sorted.sort((a, b) => {
if (a.isPinned !== b.isPinned) return b.isPinned - a.isPinned;
switch (sortOrder) {
case 'createdAt_desc': return b.createdAt - a.createdAt;
case 'createdAt_asc': return a.createdAt - b.createdAt;
case 'updatedAt_asc': return a.updatedAt - b.updatedAt;
case 'title_asc': return (a.title ?? '').localeCompare(b.title ?? '', 'ko');
case 'title_desc': return (b.title ?? '').localeCompare(a.title ?? '', 'ko');
case 'updatedAt_desc': default: return b.updatedAt - a.updatedAt;
}
});
return sorted;
};