Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
1709 lines (1482 loc) · 61.8 KB
/
Copy pathscript.js
File metadata and controls
1709 lines (1482 loc) · 61.8 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
// 全局变量
let notes = [];
let templates = [];
let currentNoteId = null;
let searchResults = []; // 搜索结果列表
let currentSearchIndex = -1; // 当前搜索结果的索引
// DOM元素
let notesList, templatesList, noteTitle, noteContent, templateDialog, templateName, templateContent, templateParent, noteSearch;
// 初始化
function init() {
// 获取DOM元素
notesList = document.getElementById('notes-list');
templatesList = document.getElementById('templates-list');
noteTitle = document.getElementById('note-title');
noteContent = document.getElementById('note-content');
templateDialog = document.getElementById('template-dialog');
templateName = document.getElementById('template-name');
templateContent = document.getElementById('template-content');
templateParent = document.getElementById('template-parent');
noteSearch = document.getElementById('note-search');
// 从localStorage加载数据
loadData();
// 渲染笔记列表
renderNotes();
// 渲染模板列表
renderTemplates();
// 绑定事件
bindEvents();
// 应用header背景设置
applyHeaderBackground();
// 显示空白页面
currentNoteId = null;
if (noteTitle) {
noteTitle.value = '';
}
const noteContentRich = document.getElementById('note-content-rich');
if (noteContentRich) {
noteContentRich.innerHTML = '';
}
}
// 从localStorage加载数据
function loadData() {
const savedNotes = localStorage.getItem('dnotes-notes');
const savedTemplates = localStorage.getItem('dnotes-templates');
// 初始化空数组
notes = [];
templates = [];
if (savedNotes) {
try {
const parsedNotes = JSON.parse(savedNotes);
if (Array.isArray(parsedNotes)) {
// 确保每个笔记都有必要的属性
notes = parsedNotes.map(note => ({
id: note.id,
parentId: note.parentId || null,
title: note.title || '',
content: note.content || '',
contentType: note.contentType || 'text', // 默认为纯文本
expanded: note.expanded !== false // 默认展开
}));
}
} catch (e) {
console.error('加载笔记数据失败:', e);
}
}
if (savedTemplates) {
try {
const parsedTemplates = JSON.parse(savedTemplates);
if (Array.isArray(parsedTemplates)) {
// 确保每个模板都有必要的属性
templates = parsedTemplates.map(template => ({
id: template.id,
parentId: template.parentId || null,
name: template.name || '',
content: template.content || '',
contentType: template.contentType || 'text', // 默认为纯文本
expanded: template.expanded !== false // 默认展开
}));
}
} catch (e) {
console.error('加载模板数据失败:', e);
}
}
}
// 保存数据到localStorage
function saveData() {
try {
localStorage.setItem('dnotes-notes', JSON.stringify(notes));
localStorage.setItem('dnotes-templates', JSON.stringify(templates));
} catch (e) {
console.error('保存数据失败:', e);
alert('保存数据失败,请检查浏览器存储空间');
}
}
// 渲染笔记列表(限制为2级目录)
function renderNotes(searchQuery = '') {
notesList.innerHTML = '';
// 添加搜索框
const searchInput = document.createElement('input');
searchInput.type = 'text';
searchInput.id = 'note-search';
searchInput.placeholder = '搜索笔记...';
searchInput.style.cssText = `
width: 100%;
padding: 0.5rem;
margin-bottom: 0.5rem;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
`;
searchInput.value = searchQuery;
searchInput.addEventListener('input', (e) => {
const value = e.target.value;
if (!value.trim()) {
// 清空搜索时重置状态
searchResults = [];
currentSearchIndex = -1;
}
renderNotes(value);
});
notesList.appendChild(searchInput);
// 添加排序按钮容器(搜索框下方)
const sortButtonsDiv = document.createElement('div');
sortButtonsDiv.style.cssText = `
display: flex;
justify-content: flex-end;
gap: 5px;
margin-bottom: 10px;
padding-bottom: 5px;
border-bottom: 1px solid #eee;
`;
// 笔记排序按钮
const noteSortButton = document.createElement('button');
noteSortButton.textContent = '📋 排序笔记';
noteSortButton.style.cssText = `
padding: 4px 8px;
background-color: #f0f0f0;
border: 1px solid #ddd;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
`;
noteSortButton.addEventListener('click', () => openSortDialog('note'));
// 模板排序按钮
const templateSortButton = document.createElement('button');
templateSortButton.textContent = '📋 排序模板';
templateSortButton.style.cssText = `
padding: 4px 8px;
background-color: #f0f0f0;
border: 1px solid #ddd;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
`;
templateSortButton.addEventListener('click', () => openSortDialog('template'));
sortButtonsDiv.appendChild(noteSortButton);
sortButtonsDiv.appendChild(templateSortButton);
notesList.appendChild(sortButtonsDiv);
// 如果有搜索关键词,显示所有匹配的笔记
if (searchQuery.trim()) {
const query = searchQuery.toLowerCase();
searchResults = notes.filter(note =>
(note.title && note.title.toLowerCase().includes(query)) ||
(note.content && note.content.toLowerCase().includes(query))
);
// 显示搜索结果导航
if (searchResults.length > 0) {
const navLi = document.createElement('li');
navLi.style.display = 'flex';
navLi.style.alignItems = 'center';
navLi.style.padding = '5px';
navLi.style.backgroundColor = '#f0f0f0';
navLi.style.borderRadius = '4px';
navLi.style.marginBottom = '10px';
const resultInfo = document.createElement('span');
resultInfo.textContent = `找到 ${searchResults.length} 个结果`;
resultInfo.style.flex = '1';
resultInfo.style.fontSize = '12px';
resultInfo.style.color = '#666';
const prevButton = document.createElement('button');
prevButton.textContent = '⬆️';
prevButton.style.fontSize = '12px';
prevButton.style.padding = '2px 6px';
prevButton.style.marginLeft = '5px';
prevButton.style.backgroundColor = '#2196F3';
prevButton.style.color = 'white';
prevButton.style.border = 'none';
prevButton.style.borderRadius = '3px';
prevButton.style.cursor = 'pointer';
prevButton.title = '上一个结果';
prevButton.addEventListener('click', () => navigateSearchResult(-1));
const nextButton = document.createElement('button');
nextButton.textContent = '⬇️';
nextButton.style.fontSize = '12px';
nextButton.style.padding = '2px 6px';
nextButton.style.marginLeft = '5px';
nextButton.style.backgroundColor = '#2196F3';
nextButton.style.color = 'white';
nextButton.style.border = 'none';
nextButton.style.borderRadius = '3px';
nextButton.style.cursor = 'pointer';
nextButton.title = '下一个结果';
nextButton.addEventListener('click', () => navigateSearchResult(1));
navLi.appendChild(resultInfo);
navLi.appendChild(prevButton);
navLi.appendChild(nextButton);
notesList.appendChild(navLi);
// 默认选中第一个结果
if (currentSearchIndex === -1 && searchResults.length > 0) {
currentSearchIndex = 0;
selectNoteAndHighlight(searchResults[0].id, searchQuery);
}
}
searchResults.forEach((note, index) => {
const li = document.createElement('li');
li.dataset.id = note.id;
li.style.cursor = 'pointer';
// 如果是当前选中的结果,高亮显示
if (index === currentSearchIndex) {
li.style.backgroundColor = '#e3f2fd';
li.style.borderLeft = '3px solid #2196F3';
}
// 笔记标题(高亮匹配)
const noteTitle = document.createElement('span');
let titleText = note.title || '无标题';
// 高亮匹配的文字
if (note.title && note.title.toLowerCase().includes(query)) {
const regex = new RegExp(`(${searchQuery})`, 'gi');
titleText = note.title.replace(regex, '<mark style="background-color: #ffeb3b; padding: 1px 2px;">$1</mark>');
}
noteTitle.innerHTML = titleText;
noteTitle.style.flex = '1';
noteTitle.style.cursor = 'pointer';
noteTitle.addEventListener('click', () => {
currentSearchIndex = index;
selectNoteAndHighlight(note.id, searchQuery);
renderNotes(searchQuery);
});
// 组装元素
li.style.display = 'flex';
li.style.alignItems = 'center';
li.appendChild(noteTitle);
notesList.appendChild(li);
});
// 显示搜索结果数量
if (searchResults.length === 0) {
const noResult = document.createElement('li');
noResult.textContent = '未找到匹配的笔记';
noResult.style.color = '#999';
noResult.style.fontStyle = 'italic';
notesList.appendChild(noResult);
currentSearchIndex = -1;
}
} else {
// 渲染第一级笔记
const firstLevelNotes = notes.filter(note => note.parentId === null);
firstLevelNotes.forEach(note => {
const li = document.createElement('li');
li.dataset.id = note.id;
// 折叠/展开按钮
const toggleButton = document.createElement('span');
const hasChildren = notes.some(n => n.parentId === note.id);
toggleButton.textContent = hasChildren ? (note.expanded ? '▼' : '▶') : ' ';
toggleButton.style.cursor = hasChildren ? 'pointer' : 'default';
toggleButton.style.marginRight = '5px';
toggleButton.style.display = 'inline-block';
toggleButton.style.width = '12px';
if (hasChildren) {
toggleButton.addEventListener('click', (e) => {
e.stopPropagation();
note.expanded = !note.expanded;
saveData();
renderNotes();
});
}
// 笔记标题(父目录样式)
const noteTitle = document.createElement('span');
noteTitle.textContent = note.title || '无标题';
noteTitle.style.flex = '1';
noteTitle.style.fontWeight = 'bold';
noteTitle.style.color = '#666';
noteTitle.addEventListener('click', () => selectNote(note.id));
// 添加子笔记按钮
const addChildButton = document.createElement('button');
addChildButton.textContent = '➕';
addChildButton.style.fontSize = '12px';
addChildButton.style.padding = '2px 4px';
addChildButton.style.marginLeft = '2px';
addChildButton.style.backgroundColor = 'transparent';
addChildButton.style.color = '#7dadf1ff';
addChildButton.style.border = 'none';
addChildButton.style.borderRadius = '3px';
addChildButton.style.cursor = 'pointer';
addChildButton.title = '添加子笔记';
addChildButton.addEventListener('click', (e) => {
e.stopPropagation();
createNote(note.id);
});
// 组装元素
li.style.display = 'flex';
li.style.alignItems = 'center';
li.appendChild(toggleButton);
li.appendChild(noteTitle);
li.appendChild(addChildButton);
if (note.id === currentNoteId) {
li.classList.add('active');
}
notesList.appendChild(li);
// 渲染第二级笔记
if (note.expanded) {
const secondLevelNotes = notes.filter(n => n.parentId === note.id);
secondLevelNotes.forEach(childNote => {
const childLi = document.createElement('li');
childLi.dataset.id = childNote.id;
childLi.style.paddingLeft = '30px';
childLi.style.fontSize = '0.9em';
// 笔记标题(子目录样式)
const childNoteTitle = document.createElement('span');
childNoteTitle.textContent = '└ ' + (childNote.title || '无标题');
childNoteTitle.style.flex = '1';
childNoteTitle.style.fontSize = '0.85em';
childNoteTitle.style.color = '#333';
childNoteTitle.addEventListener('click', () => selectNote(childNote.id));
// 组装元素
childLi.style.display = 'flex';
childLi.style.alignItems = 'center';
childLi.appendChild(childNoteTitle);
if (childNote.id === currentNoteId) {
childLi.classList.add('active');
}
notesList.appendChild(childLi);
});
}
});
}
}
// 渲染模板列表(限制为2级目录)
function renderTemplates() {
templatesList.innerHTML = '';
// 渲染第一级模板
const firstLevelTemplates = templates.filter(template => template.parentId === null);
firstLevelTemplates.forEach(template => {
const li = document.createElement('li');
li.dataset.id = template.id;
// 折叠/展开按钮
const toggleButton = document.createElement('span');
const hasChildren = templates.some(t => t.parentId === template.id);
toggleButton.textContent = hasChildren ? (template.expanded ? '▼' : '▶') : ' ';
toggleButton.style.cursor = hasChildren ? 'pointer' : 'default';
toggleButton.style.marginRight = '5px';
toggleButton.style.display = 'inline-block';
toggleButton.style.width = '12px';
if (hasChildren) {
toggleButton.addEventListener('click', (e) => {
e.stopPropagation();
template.expanded = !template.expanded;
saveData();
renderTemplates();
});
}
// 模板名称(父目录不可点击复制)
const templateName = document.createElement('span');
templateName.textContent = template.name;
templateName.style.flex = '1';
templateName.style.color = '#666';
templateName.style.fontWeight = 'bold';
// 添加子模板按钮
const addChildButton = document.createElement('button');
addChildButton.textContent = '➕';
addChildButton.style.fontSize = '12px';
addChildButton.style.padding = '2px 4px';
addChildButton.style.marginLeft = '2px';
addChildButton.style.backgroundColor = 'transparent';
addChildButton.style.color = '#7dadf1ff';
addChildButton.style.border = 'none';
addChildButton.style.borderRadius = '3px';
addChildButton.style.cursor = 'pointer';
addChildButton.title = '添加子模板';
addChildButton.addEventListener('click', (e) => {
e.stopPropagation();
openTemplateDialog(template.id);
});
// 编辑按钮
const editButton = document.createElement('button');
editButton.textContent = '📝';
editButton.style.fontSize = '12px';
editButton.style.padding = '2px 4px';
editButton.style.marginLeft = '2px';
editButton.style.backgroundColor = 'transparent';
editButton.style.color = '#2196F3';
editButton.style.border = 'none';
editButton.style.borderRadius = '3px';
editButton.style.cursor = 'pointer';
editButton.title = '编辑模板';
editButton.addEventListener('click', (e) => {
e.stopPropagation();
editTemplate(template.id);
});
// 删除按钮
const deleteButton = document.createElement('button');
deleteButton.textContent = '❌';
deleteButton.style.fontSize = '12px';
deleteButton.style.padding = '2px 4px';
deleteButton.style.marginLeft = '2px';
deleteButton.style.backgroundColor = 'transparent';
deleteButton.style.color = '#4cc6e7ff';
deleteButton.style.border = 'none';
deleteButton.style.borderRadius = '3px';
deleteButton.style.cursor = 'pointer';
deleteButton.title = '删除模板';
deleteButton.addEventListener('click', (e) => {
e.stopPropagation();
deleteTemplate(template.id);
});
// 组装元素
li.style.display = 'flex';
li.style.alignItems = 'center';
li.appendChild(toggleButton);
li.appendChild(templateName);
li.appendChild(addChildButton);
li.appendChild(editButton);
li.appendChild(deleteButton);
templatesList.appendChild(li);
// 渲染第二级模板
const secondLevelTemplates = templates.filter(t => t.parentId === template.id);
if (secondLevelTemplates.length > 0) {
if (template.expanded) {
secondLevelTemplates.forEach(childTemplate => {
const childLi = document.createElement('li');
childLi.dataset.id = childTemplate.id;
childLi.style.paddingLeft = '30px';
childLi.style.fontSize = '0.9em';
// 模板名称(子目录字体更小)
const childTemplateName = document.createElement('span');
childTemplateName.textContent = '└ ' + childTemplate.name;
childTemplateName.style.flex = '1';
childTemplateName.style.fontSize = '0.85em';
childTemplateName.style.color = '#333';
childTemplateName.addEventListener('click', () => copyTemplateToClipboard(childTemplate.id));
// 编辑按钮
const childEditButton = document.createElement('button');
childEditButton.textContent = '📝';
childEditButton.style.fontSize = '11px';
childEditButton.style.padding = '2px 4px';
childEditButton.style.marginLeft = '2px';
childEditButton.style.backgroundColor = 'transparent';
childEditButton.style.color = '#2196F3';
childEditButton.style.border = 'none';
childEditButton.style.borderRadius = '3px';
childEditButton.style.cursor = 'pointer';
childEditButton.title = '编辑模板';
childEditButton.addEventListener('click', (e) => {
e.stopPropagation();
editTemplate(childTemplate.id);
});
// 删除按钮
const childDeleteButton = document.createElement('button');
childDeleteButton.textContent = '❌';
childDeleteButton.style.fontSize = '11px';
childDeleteButton.style.padding = '2px 4px';
childDeleteButton.style.marginLeft = '2px';
childDeleteButton.style.backgroundColor = 'transparent';
childDeleteButton.style.color = '#4cc6e7ff';
childDeleteButton.style.border = 'none';
childDeleteButton.style.borderRadius = '3px';
childDeleteButton.style.cursor = 'pointer';
childDeleteButton.title = '删除模板';
childDeleteButton.addEventListener('click', (e) => {
e.stopPropagation();
deleteTemplate(childTemplate.id);
});
// 组装元素
childLi.style.display = 'flex';
childLi.style.alignItems = 'center';
childLi.appendChild(childTemplateName);
childLi.appendChild(childEditButton);
childLi.appendChild(childDeleteButton);
templatesList.appendChild(childLi);
});
}
}
});
}
// 选择笔记
function selectNote(id) {
const note = notes.find(n => n.id === id);
if (note) {
currentNoteId = id;
noteTitle.value = note.title;
document.getElementById('note-content-rich').innerHTML = note.content || '';
renderNotes();
}
}
// 选择笔记并高亮搜索词
function selectNoteAndHighlight(id, searchQuery) {
const note = notes.find(n => n.id === id);
if (note) {
currentNoteId = id;
noteTitle.value = note.title;
// 高亮内容中的搜索词
let content = note.content || '';
if (searchQuery && content) {
const query = searchQuery.toLowerCase();
// 使用正则表达式高亮搜索词(保留HTML标签)
const regex = new RegExp(`(${searchQuery.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi');
content = content.replace(regex, '<mark style="background-color: #ffeb3b; padding: 1px 2px;">$1</mark>');
}
document.getElementById('note-content-rich').innerHTML = content;
// 滚动到第一个匹配的位置
setTimeout(() => {
const firstMark = document.getElementById('note-content-rich').querySelector('mark');
if (firstMark) {
firstMark.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}, 100);
}
}
// 导航搜索结果
function navigateSearchResult(direction) {
if (searchResults.length === 0) return;
currentSearchIndex += direction;
// 循环导航
if (currentSearchIndex < 0) {
currentSearchIndex = searchResults.length - 1;
} else if (currentSearchIndex >= searchResults.length) {
currentSearchIndex = 0;
}
const note = searchResults[currentSearchIndex];
const searchQuery = noteSearch.value;
selectNoteAndHighlight(note.id, searchQuery);
renderNotes(searchQuery);
}
// 移动笔记
function moveNote(id, direction) {
const note = notes.find(note => note.id === id);
if (!note) return;
// 获取同一层级的所有笔记
const sameLevelNotes = notes.filter(n => n.parentId === note.parentId);
const noteIndex = sameLevelNotes.findIndex(n => n.id === id);
const targetIndex = direction === 'up' ? noteIndex - 1 : noteIndex + 1;
// 检查目标位置是否有效
if (targetIndex >= 0 && targetIndex < sameLevelNotes.length) {
// 找到目标笔记在原始数组中的索引
const targetNote = sameLevelNotes[targetIndex];
const originalTargetIndex = notes.findIndex(n => n.id === targetNote.id);
const originalNoteIndex = notes.findIndex(n => n.id === id);
// 交换位置
[notes[originalNoteIndex], notes[originalTargetIndex]] = [notes[originalTargetIndex], notes[originalNoteIndex]];
saveData();
renderNotes();
}
}
// 移动模板
function moveTemplate(id, direction) {
const template = templates.find(template => template.id === id);
if (!template) return;
// 获取同一层级的所有模板
const sameLevelTemplates = templates.filter(t => t.parentId === template.parentId);
const templateIndex = sameLevelTemplates.findIndex(t => t.id === id);
const targetIndex = direction === 'up' ? templateIndex - 1 : templateIndex + 1;
// 检查目标位置是否有效
if (targetIndex >= 0 && targetIndex < sameLevelTemplates.length) {
// 找到目标模板在原始数组中的索引
const targetTemplate = sameLevelTemplates[targetIndex];
const originalTargetIndex = templates.findIndex(t => t.id === targetTemplate.id);
const originalTemplateIndex = templates.findIndex(t => t.id === id);
// 交换位置
[templates[originalTemplateIndex], templates[originalTargetIndex]] = [templates[originalTargetIndex], templates[originalTemplateIndex]];
saveData();
renderTemplates();
}
}
// 打开排序对话框
function openSortDialog(type) {
// 根据类型获取数据
let items, renderFunction, saveFunction;
if (type === 'note') {
items = notes;
renderFunction = renderNotes;
saveFunction = () => {
saveData();
renderNotes();
};
} else if (type === 'template') {
items = templates;
renderFunction = renderTemplates;
saveFunction = () => {
saveData();
renderTemplates();
};
} else {
return;
}
// 创建排序对话框
const dialog = document.createElement('div');
dialog.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 10000;
`;
const dialogContent = document.createElement('div');
dialogContent.style.cssText = `
background-color: white;
padding: 1.5rem;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
width: 90%;
max-width: 500px;
`;
dialogContent.innerHTML = `
<h3 style="margin-top: 0; margin-bottom: 1rem;">排序 ${type === 'note' ? '笔记' : '模板'}</h3>
<div style="margin-bottom: 1.5rem; max-height: 400px; overflow-y: auto;"></div>
<div style="display: flex; justify-content: flex-end; gap: 1rem;">
<button id="cancel-sort" style="padding: 0.5rem 1rem; border: 1px solid #ddd; border-radius: 4px; background-color: #f5f5f5; cursor: pointer;">取消</button>
<button id="save-sort" style="padding: 0.5rem 1rem; border: none; border-radius: 4px; background-color: #4CAF50; color: white; cursor: pointer;">保存排序</button>
</div>
`;
dialog.appendChild(dialogContent);
document.body.appendChild(dialog);
// 添加排序项
const itemsContainer = dialogContent.querySelector('div');
// 递归添加排序项
function addSortItems(items, parentId = null, level = 0) {
const levelItems = items.filter(item => item.parentId === parentId);
levelItems.forEach((item, index) => {
const itemDiv = document.createElement('div');
itemDiv.style.cssText = `
display: flex;
align-items: center;
padding: 0.5rem;
border: 1px solid #ddd;
border-radius: 4px;
margin-bottom: 0.5rem;
background-color: #f9f9f9;
padding-left: ${level * 10 + 0.5}rem;
`;
const input = document.createElement('input');
input.type = 'text';
input.value = index + 1;
input.style.cssText = `
width: 40px;
margin-right: 1rem;
padding: 0.25rem;
border: 1px solid #ddd;
border-radius: 4px;
text-align: center;
`;
const name = document.createElement('span');
name.textContent = type === 'note' ? (item.title || '无标题') : item.name;
name.style.flex = '1';
itemDiv.appendChild(input);
itemDiv.appendChild(name);
itemDiv.dataset.id = item.id;
itemDiv.dataset.level = level;
itemsContainer.appendChild(itemDiv);
// 递归添加子项目
addSortItems(items, item.id, level + 1);
});
}
// 开始添加排序项
addSortItems(items);
// 取消按钮
document.getElementById('cancel-sort').addEventListener('click', () => {
document.body.removeChild(dialog);
});
// 保存按钮
document.getElementById('save-sort').addEventListener('click', () => {
const itemDivs = itemsContainer.querySelectorAll('div');
const sortedItems = [];
// 按层级分组
const levelGroups = {};
itemDivs.forEach(div => {
const id = div.dataset.id;
const level = parseInt(div.dataset.level);
const order = parseInt(div.querySelector('input').value) || 0;
if (!levelGroups[level]) {
levelGroups[level] = [];
}
levelGroups[level].push({ id, order, level });
});
// 处理排序
const newArray = [];
// 先处理一级项目
const level0Items = levelGroups[0] || [];
level0Items.sort((a, b) => a.order - b.order);
// 递归处理各级项目
function processLevel(parentId, level) {
const levelItems = (levelGroups[level] || []).filter(item => {
const itemObj = items.find(i => i.id === item.id);
return itemObj && itemObj.parentId === parentId;
});
levelItems.sort((a, b) => a.order - b.order);
levelItems.forEach(item => {
const originalItem = items.find(i => i.id === item.id);
if (originalItem) {
newArray.push(originalItem);
// 处理子项目
processLevel(item.id, level + 1);
}
});
}
// 开始处理
processLevel(null, 0);
// 更新数组
if (type === 'note') {
notes = newArray;
} else {
templates = newArray;
}
// 保存并渲染
saveFunction();
document.body.removeChild(dialog);
});
// 点击对话框外部关闭
dialog.addEventListener('click', (e) => {
if (e.target === dialog) {
document.body.removeChild(dialog);
}
});
}
// 新建笔记
function createNote(parentId = null) {
const newNote = {
id: Date.now().toString(),
parentId: parentId,
title: '',
content: '',
contentType: 'rich',
expanded: true
};
notes.push(newNote);
saveData();
selectNote(newNote.id);
renderNotes();
}
// 保存笔记
function saveNote() {
if (currentNoteId) {
const note = notes.find(n => n.id === currentNoteId);
if (note) {
note.title = noteTitle.value;
note.content = document.getElementById('note-content-rich').innerHTML;
note.lastModified = Date.now(); // 添加修改时间
saveData();
renderNotes();
}
}
}
// 删除笔记
function deleteNote() {
if (currentNoteId) {
if (confirm('确定要删除这篇笔记吗?')) {
notes = notes.filter(n => n.id !== currentNoteId);
saveData();
currentNoteId = null;
noteTitle.value = '';
noteContent.value = '';
renderNotes();
}
}
}
// 加载父模板选项(限制为2级目录)
function loadParentTemplateOptions(selectedId = '') {
templateParent.innerHTML = '<option value="">无父模板(根目录)</option>';
// 添加第一级模板选项
const firstLevelTemplates = templates.filter(t => t.parentId === null);
firstLevelTemplates.forEach(template => {
const option = document.createElement('option');
option.value = template.id;
option.textContent = template.name;
if (template.id === selectedId) {
option.selected = true;
}
templateParent.appendChild(option);
});
}
// 打开模板对话框
function openTemplateDialog(parentId = null) {
templateDialog.style.display = 'flex';
templateName.value = '';
document.getElementById('template-content-text').value = '';
loadParentTemplateOptions(parentId || '');
// 根据是否选择父模板,显示或隐藏内容编辑区域
updateTemplateDialogContentVisibility();
}
// 更新模板对话框内容编辑区域的可见性
function updateTemplateDialogContentVisibility() {
const parentId = templateParent.value;
const contentText = document.getElementById('template-content-text');
// 如果选择了父模板(二级模板),显示内容编辑区域
if (parentId) {
contentText.style.display = 'block';
} else {
// 一级模板,隐藏内容编辑区域
contentText.style.display = 'none';
}
}
// 打开模板对话框
function openTemplateDialog() {
templateDialog.style.display = 'flex';
templateName.value = '';
document.getElementById('template-content-text').value = '';
document.getElementById('template-content-rich').innerHTML = '';
// 默认选择纯文本
document.querySelector('input[name="template-type"][value="text"]').checked = true;
switchTemplateEditor('text');
loadParentTemplateOptions();
}
// 关闭模板对话框
function closeTemplateDialog() {
templateDialog.style.display = 'none';
}
// 保存模板
function saveTemplate() {
const name = templateName.value.trim();
let content = '';
const parentId = templateParent.value || null;
// 只有二级模板才需要内容
if (parentId) {
content = document.getElementById('template-content-text').value;
}
if (name) {
const editingId = templateDialog.dataset.editingId;
let finalParentId = parentId;
// 确保parentId是有效的
if (finalParentId && !templates.some(t => t.id === finalParentId)) {
finalParentId = null;
}
if (editingId) {
// 编辑现有模板
const template = templates.find(t => t.id === editingId);
if (template) {
template.name = name;
template.content = content;
template.parentId = finalParentId;
}
} else {
// 创建新模板
const newTemplate = {
id: Date.now().toString(),
parentId: finalParentId,
name: name,
content: content,
expanded: true
};
templates.push(newTemplate);
}
// 如果是二级模板,确保父模板展开
if (finalParentId) {
const parentTemplate = templates.find(t => t.id === finalParentId);
if (parentTemplate) {
parentTemplate.expanded = true;
}
}
saveData();
renderTemplates();
closeTemplateDialog();
// 清除编辑状态
delete templateDialog.dataset.editingId;
} else {
alert('请输入模板名称');
}
}
// 删除模板
// 打开删除确认对话框
function openDeleteConfirmDialog(id, type = 'template') {
// 创建确认对话框
const dialog = document.createElement('div');
dialog.style.cssText = `
position: fixed;