-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitemActions.js
More file actions
1148 lines (958 loc) · 46.5 KB
/
itemActions.js
File metadata and controls
1148 lines (958 loc) · 46.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// itemActions.js
// [버그 수정] 순환 참조 해결을 위해 generateUniqueId를 state.js에서 가져오도록 수정합니다.
import { state, setState, findFolder, findNote, CONSTANTS, buildNoteMap, generateUniqueId } from './state.js';
// [버그 수정] storage.js에 추가된 Promise 래퍼 함수를 가져옵니다.
import { storageSet } from './storage.js';
import {
noteList, folderList, noteTitleInput, noteContentTextarea,
showConfirm, showPrompt, showToast, sortNotes, showAlert, showFolderSelectPrompt,
editorContainer,
addNoteBtn,
formatDate,
noteContentView
} from './components.js';
// [버그 수정] 현재 UI에 표시된 노트 목록 캐시('sortedNotesCache')를 가져오기 위해 import 구문 수정
import { updateSaveStatus, clearSortedNotesCache, sortedNotesCache } from './renderer.js';
import { changeActiveFolder, changeActiveNote, confirmNavigation } from './navigationActions.js';
let globalSaveLock = Promise.resolve();
let autoSaveTimer = null; // 자동 저장을 위한 타이머
export const toYYYYMMDD = (dateInput) => {
if (!dateInput) return null;
const date = (dateInput instanceof Date) ? dateInput : new Date(dateInput);
if (isNaN(date.getTime())) return null;
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
return `${y}-${m}-${d}`;
};
export const updateNoteCreationDates = () => {
state.noteCreationDates.clear();
const allNotes = [...Array.from(state.noteMap.values()).map(e => e.note)];
for (const note of allNotes) {
if (note.createdAt) {
const dateStr = toYYYYMMDD(note.createdAt);
if (dateStr) {
state.noteCreationDates.add(dateStr);
}
}
}
};
let pendingRenamePromise = null;
let resolvePendingRename = null;
export const forceResolvePendingRename = () => {
if (resolvePendingRename) {
console.warn("Force resolving a pending rename operation due to external changes.");
setState({ renamingItemId: null });
resolvePendingRename();
resolvePendingRename = null;
pendingRenamePromise = null;
}
};
// [BUG FIX] 이름 변경 중 다른 작업 실행 시, 변경 사항이 유실되는 'Major' 버그를 수정합니다.
// .blur() 이벤트에 의존하는 대신, 이름 변경 완료 로직을 직접 호출하여 이벤트 경합(Race Condition)을 원천적으로 차단합니다.
export const finishPendingRename = async () => {
// 현재 이름 변경 작업이 진행 중인지 확인합니다.
if (state.renamingItemId && pendingRenamePromise) {
const id = state.renamingItemId;
// [CRITICAL BUG FIX] DOMException 방지를 위해 CSS.escape()를 사용하여 ID를 안전하게 만듭니다.
const safeId = typeof id === 'string' ? CSS.escape(id) : id;
// DOM에서 이름 변경 중인 li 요소를 찾습니다.
const renamingElementWrapper = document.querySelector(`.item-list-entry[data-id="${safeId}"]`);
if (!renamingElementWrapper) {
// 만약 요소가 사라졌다면(예: 다른 탭에서의 변경), 강제로 상태를 정리합니다.
forceResolvePendingRename();
return true; // 요소가 없으면 더 이상 할 작업이 없으므로 성공으로 간주
}
const type = renamingElementWrapper.dataset.type;
const nameSpan = renamingElementWrapper.querySelector('.item-name');
if (nameSpan) {
// 핵심 수정: .blur()를 호출하는 대신, _handleRenameEnd 함수를 직접 호출합니다.
// 이렇게 하면 이벤트 발생 순서에 상관없이 변경 사항이 안정적으로 저장됩니다.
// 'true'를 전달하여 변경 내용을 저장하도록 지시하며, _handleRenameEnd의 결과를 직접 반환합니다.
return await _handleRenameEnd(id, type, nameSpan, true);
} else {
// span을 찾을 수 없는 예외적인 경우에도 상태를 정리합니다.
forceResolvePendingRename();
return true; // span이 없으면 더 이상 할 작업이 없으므로 성공으로 간주
}
}
return true; // 이름 변경 중이 아니면 항상 성공
};
let calendarRenderer = () => {};
export const setCalendarRenderer = (renderer) => {
calendarRenderer = renderer;
};
// [SIMPLIFIED] 탭 간 경쟁을 고려하지 않는 단순화된 데이터 업데이트 함수
export const performTransactionalUpdate = async (updateFn) => {
// [주석 추가] 아래의 globalSaveLock은 멀티탭 동기화 기능이 아닙니다.
// 단일 탭 내에서 노트 저장, 폴더 삭제 등 여러 비동기 작업이 동시에 실행될 때
// 데이터 충돌을 막기 위한 안전장치이므로 단일 탭 환경에서도 유용합니다.
await globalSaveLock;
let releaseLocalLock;
globalSaveLock = new Promise(resolve => { releaseLocalLock = resolve; });
let resultPayload = null;
let success = false;
try {
setState({ isPerformingOperation: true });
// 현재 메모리 상태를 기반으로 데이터를 수정
const dataCopy = JSON.parse(JSON.stringify({
folders: state.folders,
trash: state.trash,
favorites: Array.from(state.favorites),
lastSavedTimestamp: state.lastSavedTimestamp,
lastActiveNotePerFolder: state.lastActiveNotePerFolder
}));
const result = await updateFn(dataCopy);
if (result === null) {
// [안정성 강화] releaseLocalLock이 항상 함수일 것으로 보장되지만, 만약의 경우를 대비한 방어 코드를 추가하여 데드락 가능성을 원천적으로 차단합니다.
if (typeof releaseLocalLock === 'function') releaseLocalLock();
setState({ isPerformingOperation: false });
return { success: false, payload: null };
}
const { newData, successMessage, postUpdateState, payload } = result;
resultPayload = payload;
newData.lastSavedTimestamp = Date.now();
// 수정된 데이터를 스토리지에 저장
// [버그 수정] chrome.storage.local.set을 안전한 래퍼 함수로 교체합니다.
await storageSet({ appState: newData });
// 메모리 상태를 최신 데이터로 업데이트
setState({
folders: newData.folders,
trash: newData.trash,
favorites: new Set(newData.favorites || []),
lastSavedTimestamp: newData.lastSavedTimestamp,
lastActiveNotePerFolder: newData.lastActiveNotePerFolder || {},
totalNoteCount: newData.folders.reduce((sum, f) => sum + f.notes.length, 0)
});
buildNoteMap();
updateNoteCreationDates();
clearSortedNotesCache();
if (calendarRenderer) {
calendarRenderer(true);
}
if (postUpdateState) setState(postUpdateState);
if (successMessage) showToast(successMessage, CONSTANTS.TOAST_TYPE.SUCCESS, 6000);
success = true;
} catch (e) {
console.error("Transactional update failed:", e);
// [BUG FIX] 저장 공간 초과 오류를 감지하고 사용자에게 명확한 안내를 제공합니다.
if (e && e.message && e.message.toLowerCase().includes('quota')) {
showAlert({
title: '💾 저장 공간 부족',
message: '저장 공간(5MB)이 가득 찼습니다. 더 이상 데이터를 저장할 수 없습니다.\n\n불필요한 노트를 휴지통으로 이동한 뒤, 휴지통을 비워 영구적으로 삭제하면 공간을 확보할 수 있습니다.',
confirmText: '✅ 확인'
});
} else {
showToast("오류가 발생하여 작업을 완료하지 못했습니다.", CONSTANTS.TOAST_TYPE.ERROR);
}
success = false;
} finally {
setState({ isPerformingOperation: false });
// [BUG FIX & 안정성 강화] releaseLocalLock() 호출이 실패하는 극단적인 경우를 대비하여 try-catch로 감싸고,
// 실패 시 전역 락을 강제로 리셋하여 데드락을 방지합니다.
try {
if (typeof releaseLocalLock === 'function') {
releaseLocalLock();
}
} catch (e) {
console.error("CRITICAL: Failed to release the transaction lock. Resetting to prevent deadlock.", e);
// 데드락 방지를 위한 최후의 안전장치
globalSaveLock = Promise.resolve();
}
}
return { success, payload: resultPayload };
};
// [BUG-C-CRITICAL 수정] 모달 확인 후에 변경사항을 저장하도록 `withConfirmation` 헬퍼 수정
async function withConfirmation(options, action) {
const ok = await showConfirm(options);
if (ok) {
// 실제 액션을 실행하기 직전에, 모달이 떠있는 동안 발생했을 수 있는
// 모든 변경사항을 저장 시도합니다.
if (!(await saveCurrentNoteIfChanged())) {
showToast("변경사항 저장에 실패하여 작업을 취소했습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return;
}
await action();
}
}
const getNextActiveNoteAfterDeletion = (deletedNoteId, notesInView) => {
if (!notesInView || notesInView.length === 0) return null;
const futureNotesInView = notesInView.filter(n => n.id !== deletedNoteId);
if(futureNotesInView.length === 0) return null;
const deletedIndexInOriginalView = notesInView.findIndex(n => n.id === deletedNoteId);
if (deletedIndexInOriginalView === -1) return futureNotesInView[0].id;
const nextItem = futureNotesInView[deletedIndexInOriginalView] || futureNotesInView[deletedIndexInOriginalView - 1];
return nextItem?.id ?? null;
};
// [CRITICAL BUG FIX] ID 고유성 검사를 위해 시스템의 모든 ID를 수집하는 헬퍼 함수
const collectAllIds = () => {
const allIds = new Set();
// 1. 모든 활성 폴더 및 그 안의 노트를 순회하며 ID를 수집합니다.
state.folders.forEach(folder => {
allIds.add(folder.id);
if (Array.isArray(folder.notes)) {
folder.notes.forEach(note => allIds.add(note.id));
}
});
// 2. 휴지통의 모든 아이템을 순회하며 ID를 수집합니다.
// (휴지통의 폴더는 내부에 노트만 포함하므로, 1단계 깊이의 탐색만 필요합니다.)
state.trash.forEach(item => {
allIds.add(item.id);
if (item.type === 'folder' && Array.isArray(item.notes)) {
item.notes.forEach(note => allIds.add(note.id));
}
});
// 3. 시스템에서 사용하는 가상 폴더의 ID도 충돌 방지를 위해 포함시킵니다.
Object.values(CONSTANTS.VIRTUAL_FOLDERS).forEach(vf => allIds.add(vf.id));
return allIds;
};
export const handleAddFolder = async () => {
if (!(await finishPendingRename())) {
showToast("이름 변경 저장에 실패하여 폴더 추가를 취소했습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return;
}
const name = await showPrompt({
title: CONSTANTS.MODAL_TITLES.NEW_FOLDER,
placeholder: '📁 폴더 이름을 입력하세요',
validationFn: (value) => {
const trimmedValue = value.trim();
if (!trimmedValue) return { isValid: false, message: CONSTANTS.MESSAGES.ERROR.EMPTY_NAME_ERROR };
if (state.folders.some(f => f.name.toLowerCase() === trimmedValue.toLowerCase())) {
return { isValid: false, message: CONSTANTS.MESSAGES.ERROR.FOLDER_EXISTS(trimmedValue) };
}
return { isValid: true };
}
});
if (!name) return;
if (!(await saveCurrentNoteIfChanged())) {
showToast("변경사항 저장에 실패하여 폴더를 추가하지 않았습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return;
}
const allIds = collectAllIds();
const newFolderId = generateUniqueId(CONSTANTS.ID_PREFIX.FOLDER, allIds);
const trimmedName = name.trim();
const { success } = await performTransactionalUpdate((latestData) => {
if (latestData.folders.some(f => f.name.toLowerCase() === trimmedName.toLowerCase())) {
showAlert({ title: '오류', message: `'${trimmedName}' 폴더가 이미 존재합니다.`});
return null;
}
const now = Date.now();
const newFolder = { id: newFolderId, name: trimmedName, notes: [], createdAt: now, updatedAt: now };
latestData.folders.push(newFolder);
return {
newData: latestData,
successMessage: null,
postUpdateState: { activeFolderId: newFolderId, activeNoteId: null }
};
});
if (success) {
await changeActiveFolder(newFolderId, { force: true });
requestAnimationFrame(() => {
// [CRITICAL BUG FIX] DOMException 방지를 위해 CSS.escape()를 사용하여 ID를 안전하게 만듭니다.
const safeNewFolderId = typeof newFolderId === 'string' ? CSS.escape(newFolderId) : newFolderId;
const newFolderEl = folderList.querySelector(`[data-id="${safeNewFolderId}"]`);
if (newFolderEl) {
newFolderEl.scrollIntoView({ block: 'center', behavior: 'smooth' });
newFolderEl.focus();
}
});
}
};
let addNoteLock = false;
export const handleAddNote = async () => {
if (addNoteLock) return;
addNoteLock = true;
if(addNoteBtn) addNoteBtn.disabled = true;
try {
if (!(await finishPendingRename())) {
showToast("이름 변경 저장에 실패하여 노트 추가를 취소했습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return;
}
if (!(await confirmNavigation())) return;
const { ALL, RECENT, TRASH, FAVORITES } = CONSTANTS.VIRTUAL_FOLDERS;
const currentActiveFolderId = state.activeFolderId;
if (!currentActiveFolderId || [ALL.id, RECENT.id, TRASH.id, FAVORITES.id].includes(currentActiveFolderId)) {
showToast(CONSTANTS.MESSAGES.ERROR.ADD_NOTE_PROMPT, CONSTANTS.TOAST_TYPE.ERROR);
return;
}
const allIds = collectAllIds();
const newNoteId = generateUniqueId(CONSTANTS.ID_PREFIX.NOTE, allIds);
const now = Date.now();
const { success, payload } = await performTransactionalUpdate((latestData) => {
const activeFolder = latestData.folders.find(f => f.id === currentActiveFolderId);
if (!activeFolder) {
showAlert({ title: '오류', message: '노트를 추가하려던 폴더가 삭제되었습니다.'});
return null;
}
let baseTitle = `${formatDate(now)}의 노트`;
let finalTitle = baseTitle;
let counter = 2;
while (activeFolder.notes.some(note => note.title === finalTitle)) {
finalTitle = `${baseTitle} (${counter++})`;
}
const newNote = { id: newNoteId, title: finalTitle, content: "", createdAt: now, updatedAt: now, isPinned: false };
activeFolder.notes.unshift(newNote);
activeFolder.updatedAt = now;
const newLastActiveMap = { ...(latestData.lastActiveNotePerFolder || {}), [currentActiveFolderId]: newNoteId };
latestData.lastActiveNotePerFolder = newLastActiveMap;
return {
newData: latestData,
successMessage: null,
postUpdateState: {
isDirty: false,
dirtyNoteId: null,
searchTerm: '',
},
payload: { newNoteId: newNote.id }
};
});
if (success && payload?.newNoteId) {
await changeActiveNote(payload.newNoteId);
requestAnimationFrame(() => {
if (noteTitleInput) {
noteTitleInput.focus();
noteTitleInput.select();
}
});
}
} finally {
addNoteLock = false;
if(addNoteBtn) addNoteBtn.disabled = false;
}
};
const _withNoteAction = (noteId, actionFn) => {
return performTransactionalUpdate(latestData => {
let noteToUpdate = null, folderOfNote = null;
for (const folder of latestData.folders) {
const note = folder.notes.find(n => n.id === noteId);
if (note) {
noteToUpdate = note; folderOfNote = folder; break;
}
}
if (!noteToUpdate) return null;
return actionFn(noteToUpdate, folderOfNote, latestData);
});
};
export const handlePinNote = (id) => _withNoteAction(id, (note, folder, data) => {
note.isPinned = !note.isPinned;
const now = Date.now();
note.updatedAt = now;
folder.updatedAt = now;
return {
newData: data,
successMessage: note.isPinned ? CONSTANTS.MESSAGES.SUCCESS.NOTE_PINNED : CONSTANTS.MESSAGES.SUCCESS.NOTE_UNPINNED,
postUpdateState: {}
};
});
export const handleToggleFavorite = (id) => _withNoteAction(id, (note, folder, data) => {
const now = Date.now();
note.updatedAt = now;
folder.updatedAt = now;
const favoritesSet = new Set(data.favorites || []);
const isNowFavorite = !favoritesSet.has(id);
if (isNowFavorite) favoritesSet.add(id);
else favoritesSet.delete(id);
data.favorites = Array.from(favoritesSet);
let postUpdateState = {};
if (state.activeFolderId === CONSTANTS.VIRTUAL_FOLDERS.FAVORITES.id && !isNowFavorite) {
postUpdateState.searchTerm = '';
postUpdateState.preSearchActiveNoteId = null;
}
return {
newData: data,
successMessage: isNowFavorite ? CONSTANTS.MESSAGES.SUCCESS.NOTE_FAVORITED : CONSTANTS.MESSAGES.SUCCESS.NOTE_UNFAVORITED,
postUpdateState
};
});
export const handleDelete = async (id, type) => {
if (!(await finishPendingRename())) {
showToast("이름 변경 저장에 실패하여 삭제를 취소했습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return;
}
const { item } = (type === CONSTANTS.ITEM_TYPE.FOLDER ? findFolder(id) : findNote(id));
if (!item) return;
const itemName = item.name || item.title || '제목 없음';
const confirmMessage = type === CONSTANTS.ITEM_TYPE.FOLDER
? `📁 '${itemName}' 폴더와 포함된 모든 노트를 휴지통으로 이동할까요?`
: `📝 '${itemName}' 노트를 휴지통으로 이동할까요?`;
await withConfirmation(
{ title: '🗑️ 휴지통으로 이동', message: confirmMessage, confirmText: '🗑️ 이동' },
() => performDeleteItem(id, type)
);
};
export const performDeleteItem = (id, type) => {
const currentNotesInView = sortedNotesCache.result || [];
return performTransactionalUpdate(latestData => {
const { folders, trash } = latestData;
let successMessage = '', postUpdateState = {};
const now = Date.now();
if (state.renamingItemId === id) postUpdateState.renamingItemId = null;
if (type === CONSTANTS.ITEM_TYPE.FOLDER) {
const folderIndex = folders.findIndex(f => f.id === id);
if (folderIndex === -1) return null;
const [folderToMove] = folders.splice(folderIndex, 1);
folderToMove.type = 'folder';
folderToMove.originalIndex = folderIndex;
folderToMove.deletedAt = now;
folderToMove.updatedAt = now;
trash.unshift(folderToMove);
const favoritesSet = new Set(latestData.favorites || []);
folderToMove.notes.forEach(note => favoritesSet.delete(note.id));
latestData.favorites = Array.from(favoritesSet);
successMessage = CONSTANTS.MESSAGES.SUCCESS.FOLDER_MOVED_TO_TRASH(folderToMove.name);
if (state.activeFolderId === id) {
const nextFolderIndex = Math.max(0, folderIndex - 1);
postUpdateState.activeFolderId = folders.length > 0 ? (folders[folderIndex]?.id ?? folders[nextFolderIndex].id) : CONSTANTS.VIRTUAL_FOLDERS.ALL.id;
postUpdateState.activeNoteId = null;
}
} else { // NOTE
let noteToMove, sourceFolder;
for(const folder of folders) {
const noteIndex = folder.notes.findIndex(n => n.id === id);
if (noteIndex !== -1) {
[noteToMove] = folder.notes.splice(noteIndex, 1);
sourceFolder = folder;
break;
}
}
if (!noteToMove) return null;
noteToMove.type = 'note';
noteToMove.originalFolderId = sourceFolder.id;
noteToMove.deletedAt = now;
// --- [기능 개선] 즐겨찾기 상태 복원을 위해 삭제 시 상태 기록 ---
const favoritesSet = new Set(latestData.favorites || []);
if (favoritesSet.has(id)) {
noteToMove.wasFavorite = true;
favoritesSet.delete(id);
latestData.favorites = Array.from(favoritesSet);
}
// --- [수정 끝] ---
trash.unshift(noteToMove);
sourceFolder.updatedAt = now;
successMessage = CONSTANTS.MESSAGES.SUCCESS.NOTE_MOVED_TO_TRASH(noteToMove.title || '제목 없음');
if (state.activeNoteId === id) {
postUpdateState.activeNoteId = getNextActiveNoteAfterDeletion(id, currentNotesInView);
}
}
return { newData: latestData, successMessage, postUpdateState };
});
};
export const handleRestoreItem = async (id, type) => {
if (!(await finishPendingRename())) {
showToast("이름 변경 저장에 실패하여 복원을 취소했습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return;
}
const itemToRestore = state.trash.find(item => item.id === id);
if (!itemToRestore) return;
let effectiveType = itemToRestore.type;
if (!effectiveType) {
effectiveType = Array.isArray(itemToRestore.notes) ? CONSTANTS.ITEM_TYPE.FOLDER : CONSTANTS.ITEM_TYPE.NOTE;
}
if (type !== effectiveType) {
console.warn(`Type mismatch in handleRestoreItem: expected ${type}, but found ${effectiveType}. Proceeding with item's own type.`);
}
let finalFolderName = itemToRestore.name;
let targetFolderId = null;
if (effectiveType === 'folder') {
if (state.folders.some(f => f.name === itemToRestore.name)) {
const newName = await showPrompt({
title: '📁 폴더 이름 중복',
message: `'${itemToRestore.name}' 폴더가 이미 존재합니다. 복원할 폴더의 새 이름을 입력해주세요.`,
initialValue: `${itemToRestore.name} (복사본)`,
validationFn: (value) => {
const trimmedValue = value.trim();
if (!trimmedValue) return { isValid: false, message: CONSTANTS.MESSAGES.ERROR.EMPTY_NAME_ERROR };
if (state.folders.some(f => f.name === trimmedValue)) return { isValid: false, message: CONSTANTS.MESSAGES.ERROR.FOLDER_EXISTS(trimmedValue) };
return { isValid: true };
}
});
if (!newName) return;
finalFolderName = newName.trim();
}
} else if (effectiveType === 'note') {
const originalFolder = state.folders.find(f => f.id === itemToRestore.originalFolderId);
if (!originalFolder) {
const newFolderId = await showFolderSelectPrompt({
title: '🤔 원본 폴더를 찾을 수 없음',
message: '이 노트의 원본 폴더가 없거나 휴지통에 있습니다. 복원할 폴더를 선택해주세요.'
});
if (!newFolderId) return;
targetFolderId = newFolderId;
} else {
targetFolderId = originalFolder.id;
}
}
if (!(await saveCurrentNoteIfChanged())) {
showToast("변경사항 저장에 실패하여 복원 작업을 취소했습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return;
}
const updateLogic = (latestData) => {
const { folders, trash } = latestData;
const itemIndexInTx = trash.findIndex(item => item.id === id);
if (itemIndexInTx === -1) return null;
const [itemToRestoreInTx] = trash.splice(itemIndexInTx, 1);
let txEffectiveType = itemToRestoreInTx.type;
if (!txEffectiveType) {
txEffectiveType = Array.isArray(itemToRestoreInTx.notes) ? CONSTANTS.ITEM_TYPE.FOLDER : CONSTANTS.ITEM_TYPE.NOTE;
}
const now = Date.now();
let hadIdCollision = false;
const idUpdateMap = new Map();
if (txEffectiveType === 'folder') {
if (folders.some(f => f.name === finalFolderName)) {
showAlert({ title: '오류', message: `'${finalFolderName}' 폴더가 방금 다른 곳에서 생성되었습니다. 다른 이름으로 다시 시도해주세요.`});
return null;
}
itemToRestoreInTx.name = finalFolderName;
const allExistingIds = new Set();
folders.forEach(f => {
allExistingIds.add(f.id);
f.notes.forEach(n => allExistingIds.add(n.id));
});
trash.forEach(item => {
if (item.id !== id) {
allExistingIds.add(item.id);
if (item.type === 'folder' && Array.isArray(item.notes)) {
item.notes.forEach(note => allExistingIds.add(note.id));
}
}
});
if (allExistingIds.has(itemToRestoreInTx.id)) {
const oldId = itemToRestoreInTx.id;
const newId = generateUniqueId(CONSTANTS.ID_PREFIX.FOLDER, allExistingIds);
itemToRestoreInTx.id = newId;
allExistingIds.add(newId);
idUpdateMap.set(oldId, newId);
hadIdCollision = true;
}
const favoritesSet = new Set(latestData.favorites || []);
const restoredNoteIds = new Set();
(itemToRestoreInTx.notes || []).forEach(note => {
if (restoredNoteIds.has(note.id) || allExistingIds.has(note.id)) {
const oldId = note.id;
const combinedExistingIds = new Set([...allExistingIds, ...restoredNoteIds]);
const newId = generateUniqueId(CONSTANTS.ID_PREFIX.NOTE, combinedExistingIds);
note.id = newId;
idUpdateMap.set(oldId, newId);
hadIdCollision = true;
}
restoredNoteIds.add(note.id);
allExistingIds.add(note.id);
delete note.deletedAt; delete note.type; delete note.originalFolderId;
});
delete itemToRestoreInTx.deletedAt;
itemToRestoreInTx.type = 'folder';
itemToRestoreInTx.updatedAt = now;
if (typeof itemToRestoreInTx.originalIndex === 'number' && itemToRestoreInTx.originalIndex >= 0) {
folders.splice(itemToRestoreInTx.originalIndex, 0, itemToRestoreInTx);
} else {
folders.unshift(itemToRestoreInTx);
}
delete itemToRestoreInTx.originalIndex;
} else if (txEffectiveType === 'note') {
const targetFolderInTx = folders.find(f => f.id === targetFolderId);
if (!targetFolderInTx) {
showAlert({ title: '오류', message: '노트를 복원하려던 폴더가 방금 삭제되었습니다.'});
return null;
}
const allExistingIds = new Set();
folders.forEach(f => {
allExistingIds.add(f.id);
f.notes.forEach(n => allExistingIds.add(n.id));
});
trash.forEach(item => {
if (item.id !== id) {
allExistingIds.add(item.id);
if (item.type === 'folder' && Array.isArray(item.notes)) {
item.notes.forEach(note => allExistingIds.add(note.id));
}
}
});
if (allExistingIds.has(itemToRestoreInTx.id)) {
const oldId = itemToRestoreInTx.id;
const newId = generateUniqueId(CONSTANTS.ID_PREFIX.NOTE, allExistingIds);
itemToRestoreInTx.id = newId;
idUpdateMap.set(oldId, newId);
hadIdCollision = true;
}
delete itemToRestoreInTx.deletedAt;
itemToRestoreInTx.type = 'note';
delete itemToRestoreInTx.originalFolderId;
itemToRestoreInTx.updatedAt = now;
// --- [BUG FIX] ID 변경이 완료된 후, 최종 ID를 사용하여 즐겨찾기 상태를 복원합니다. ---
if (itemToRestoreInTx.wasFavorite) {
const favoritesSet = new Set(latestData.favorites || []);
// 여기서 사용되는 .id는 충돌 시 이미 새 ID로 교체된 값입니다.
favoritesSet.add(itemToRestoreInTx.id);
latestData.favorites = Array.from(favoritesSet);
delete itemToRestoreInTx.wasFavorite; // 임시 속성 제거
}
// --- [수정 끝] ---
targetFolderInTx.notes.unshift(itemToRestoreInTx);
targetFolderInTx.updatedAt = now;
}
if (idUpdateMap.size > 0 && latestData.lastActiveNotePerFolder) {
const newLastActiveMap = {};
for (const oldFolderId in latestData.lastActiveNotePerFolder) {
const newFolderId = idUpdateMap.get(oldFolderId) || oldFolderId;
const oldNoteId = latestData.lastActiveNotePerFolder[oldFolderId];
const newNoteId = idUpdateMap.get(oldNoteId) || oldNoteId;
newLastActiveMap[newFolderId] = newNoteId;
}
latestData.lastActiveNotePerFolder = newLastActiveMap;
}
if (txEffectiveType === 'folder') {
return {
newData: latestData,
successMessage: CONSTANTS.MESSAGES.SUCCESS.ITEM_RESTORED_FOLDER(itemToRestoreInTx.name),
postUpdateState: {},
payload: { hadIdCollision }
};
} else if (txEffectiveType === 'note') {
return {
newData: latestData,
successMessage: CONSTANTS.MESSAGES.SUCCESS.ITEM_RESTORED_NOTE(itemToRestoreInTx.title),
postUpdateState: {},
payload: { hadIdCollision }
};
}
return null;
};
const { success, payload } = await performTransactionalUpdate(updateLogic);
if (success && payload?.hadIdCollision) {
showToast("일부 노트 또는 폴더의 ID가 충돌하여 자동으로 수정되었습니다.", CONSTANTS.TOAST_TYPE.SUCCESS, 8000);
}
};
export const handlePermanentlyDeleteItem = async (id, type) => {
if (!(await finishPendingRename())) {
showToast("이름 변경 저장에 실패하여 영구 삭제를 취소했습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return;
}
const item = state.trash.find(i => i.id === id);
if (!item) return;
let effectiveType = item.type;
if (!effectiveType) {
effectiveType = Array.isArray(item.notes) ? CONSTANTS.ITEM_TYPE.FOLDER : CONSTANTS.ITEM_TYPE.NOTE;
}
if (type !== effectiveType) {
console.warn(`Type mismatch in handlePermanentlyDeleteItem: expected ${type}, but found ${effectiveType}. Proceeding with item's own type.`);
}
const itemName = item.title ?? item.name;
const message = CONSTANTS.MESSAGES.CONFIRM.PERM_DELETE(itemName);
await withConfirmation(
{ title: CONSTANTS.MODAL_TITLES.PERM_DELETE, message: message, confirmText: '💥 삭제', confirmButtonType: 'danger' },
() => performTransactionalUpdate(latestData => {
const originalTrashItems = [...latestData.trash];
const itemIndex = originalTrashItems.findIndex(i => i.id === id);
if (itemIndex === -1) return null;
const [deletedItem] = latestData.trash.splice(itemIndex, 1);
let postUpdateState = {};
if (state.renamingItemId === id) postUpdateState.renamingItemId = null;
if (state.activeNoteId === id) {
postUpdateState.activeNoteId = getNextActiveNoteAfterDeletion(id, originalTrashItems);
}
const favoritesSet = new Set(latestData.favorites || []);
const initialSize = favoritesSet.size;
let deletedItemType = deletedItem.type;
if (!deletedItemType) {
deletedItemType = Array.isArray(deletedItem.notes) ? 'folder' : 'note';
}
if (deletedItemType === 'note') {
favoritesSet.delete(id);
} else if (deletedItemType === 'folder' && Array.isArray(deletedItem.notes)) {
deletedItem.notes.forEach(note => {
favoritesSet.delete(note.id);
});
}
if (favoritesSet.size < initialSize) {
latestData.favorites = Array.from(favoritesSet);
}
return {
newData: latestData,
successMessage: CONSTANTS.MESSAGES.SUCCESS.PERM_DELETE_ITEM_SUCCESS,
postUpdateState
};
})
);
};
export const handleEmptyTrash = async () => {
if (!(await finishPendingRename())) {
showToast("이름 변경 저장에 실패하여 휴지통 비우기를 취소했습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return;
}
if (state.trash.length === 0) return;
const message = CONSTANTS.MESSAGES.CONFIRM.EMPTY_TRASH(state.trash.length);
await withConfirmation(
{ title: CONSTANTS.MODAL_TITLES.EMPTY_TRASH, message: message, confirmText: '💥 모두 영구적으로 삭제', confirmButtonType: 'danger' },
() => performTransactionalUpdate(latestData => {
let postUpdateState = {};
const noteIdsInTrash = new Set();
latestData.trash.forEach(item => {
let itemType = item.type;
if (!itemType) itemType = Array.isArray(item.notes) ? 'folder' : 'note';
if (itemType === 'note') {
noteIdsInTrash.add(item.id);
} else if (itemType === 'folder' && Array.isArray(item.notes)) {
item.notes.forEach(note => noteIdsInTrash.add(note.id));
}
});
if (state.activeFolderId === CONSTANTS.VIRTUAL_FOLDERS.TRASH.id) {
postUpdateState.activeFolderId = CONSTANTS.VIRTUAL_FOLDERS.ALL.id;
postUpdateState.activeNoteId = null;
}
else if (state.activeNoteId && noteIdsInTrash.has(state.activeNoteId)) {
postUpdateState.activeNoteId = null;
}
if (state.renamingItemId && state.trash.some(item => item.id === state.renamingItemId)) {
postUpdateState.renamingItemId = null;
}
const favoritesSet = new Set(latestData.favorites || []);
latestData.trash.forEach(item => {
let itemType = item.type;
if (!itemType) itemType = Array.isArray(item.notes) ? 'folder' : 'note';
if (itemType === 'note') {
favoritesSet.delete(item.id);
} else if (itemType === 'folder' && Array.isArray(item.notes)) {
item.notes.forEach(note => {
favoritesSet.delete(note.id);
});
}
});
latestData.favorites = Array.from(favoritesSet);
latestData.trash = [];
return { newData: latestData, successMessage: CONSTANTS.MESSAGES.SUCCESS.EMPTY_TRASH_SUCCESS, postUpdateState };
})
);
};
export async function saveCurrentNoteIfChanged() {
if (!state.isDirty) {
return true;
}
const noteIdToSave = state.activeNoteId;
const titleToSave = noteTitleInput.value;
const contentToSave = noteContentTextarea.value;
if (!noteIdToSave) {
setState({ isDirty: false, dirtyNoteId: null });
return true;
}
updateSaveStatus('saving');
const { success } = await performTransactionalUpdate(latestData => {
let noteToSave, parentFolder;
for (const folder of latestData.folders) {
const note = folder.notes.find(n => n.id === noteIdToSave);
if (note) { noteToSave = note; parentFolder = folder; break; }
}
if (!noteToSave) {
console.error(`Save failed: Note with ID ${noteIdToSave} not found in storage.`);
showToast("저장 실패: 노트가 다른 곳에서 삭제된 것 같습니다.", CONSTANTS.TOAST_TYPE.ERROR);
return null;
}
const now = Date.now();
let finalTitle = titleToSave.trim();
if (!finalTitle && contentToSave) {
let firstLine = contentToSave.split('\n')[0].trim();
if (firstLine) {
const hasKorean = /[\uAC00-\uD7AF]/.test(firstLine);
const limit = hasKorean ? CONSTANTS.AUTO_TITLE_LENGTH_KOR : CONSTANTS.AUTO_TITLE_LENGTH;
if (firstLine.length > limit) {
firstLine = firstLine.slice(0, limit) + '...';
}
finalTitle = firstLine;
}
}
noteToSave.title = finalTitle;
noteToSave.content = contentToSave;
noteToSave.updatedAt = now;
if (parentFolder) parentFolder.updatedAt = now;
return { newData: latestData, successMessage: null, postUpdateState: {} };
});
if (success) {
const { item: justSavedNote } = findNote(noteIdToSave);
const liveTitle = noteTitleInput.value;
const liveContent = noteContentTextarea.value;
const isStillDirty = state.activeNoteId === noteIdToSave && justSavedNote && (justSavedNote.title !== liveTitle || justSavedNote.content !== liveContent);
if (isStillDirty) {
setState({ isDirty: true, dirtyNoteId: noteIdToSave });
updateSaveStatus('dirty');
handleUserInput();
} else {
setState({ isDirty: false, dirtyNoteId: null });
updateSaveStatus('saved');
}
} else {
updateSaveStatus('dirty');
}
return success;
}
export async function handleUserInput() {
if (!state.activeNoteId) return;
const { item: activeNote } = findNote(state.activeNoteId);
if (!activeNote) return;
const currentTitle = noteTitleInput.value;
const currentContent = noteContentTextarea.value;
const hasChanged = activeNote.title !== currentTitle || activeNote.content !== currentContent;
if (hasChanged) {
if (!state.isDirty) {
setState({ isDirty: true, dirtyNoteId: state.activeNoteId });
updateSaveStatus('dirty');
}
} else {
if (state.isDirty) {
setState({ isDirty: false, dirtyNoteId: null });
clearTimeout(autoSaveTimer);
updateSaveStatus('saved');
}
return;
}
clearTimeout(autoSaveTimer);
autoSaveTimer = setTimeout(() => {
if (state.isDirty && state.dirtyNoteId === state.activeNoteId) {
saveCurrentNoteIfChanged();
}
}, CONSTANTS.DEBOUNCE_DELAY.SAVE);
}
const _handleRenameEnd = async (id, type, nameSpan, shouldSave) => {
if (state.renamingItemId !== id || !pendingRenamePromise) {
return true;
}
nameSpan.contentEditable = false;
if (resolvePendingRename) {
resolvePendingRename();
resolvePendingRename = null;
}
pendingRenamePromise = null;
if (!nameSpan.isConnected) {
setState({ renamingItemId: null });
return true;
}
const { item: currentItem } = (type === CONSTANTS.ITEM_TYPE.FOLDER ? findFolder(id) : findNote(id));
if (!currentItem) {
setState({ renamingItemId: null });
return true;
}
const originalName = (type === CONSTANTS.ITEM_TYPE.FOLDER) ? currentItem.name : currentItem.title;
const newName = nameSpan.textContent.trim();
if (!shouldSave || newName === originalName) {