-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1877 lines (1474 loc) · 59.4 KB
/
Copy pathindex.html
File metadata and controls
1877 lines (1474 loc) · 59.4 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>喵喵便签</title>
<style>
* {
box-sizing: border-box;
}
:root {
--app-bg: #f0e6d3;
--app-text: #2f2a24;
--header-bg: #f0e6d3;
--header-border: rgba(90, 70, 45, 0.18);
--field-bg: #fffdf8;
--field-border: #cdbf9f;
--muted-text: #5d5142;
--empty-text: #766b5f;
--primary-btn: #6f8f72;
--accent-btn: #d36b4c;
--neutral-btn: #8a7863;
--danger-btn: #c34a41;
--menu-bg: #fffdf8;
--menu-border: rgba(90, 70, 45, 0.18);
--menu-shadow: rgba(65, 48, 31, 0.16);
}
body[data-app-theme="blue"] {
--app-bg: #eaf6ff;
--app-text: #203646;
--header-bg: #dff0ff;
--header-border: rgba(60, 124, 170, 0.22);
--field-bg: #fbfdff;
--field-border: #a9cfe9;
--muted-text: #48677d;
--empty-text: #5c7890;
--primary-btn: #4f8bb4;
--accent-btn: #d07182;
--neutral-btn: #647f93;
--menu-bg: #f7fbff;
--menu-border: rgba(60, 124, 170, 0.24);
--menu-shadow: rgba(40, 92, 130, 0.16);
}
body[data-app-theme="pink"] {
--app-bg: #fff0f5;
--app-text: #4a2734;
--header-bg: #ffe3ed;
--header-border: rgba(180, 84, 118, 0.2);
--field-bg: #fffafd;
--field-border: #edb5c9;
--muted-text: #7b4b5f;
--empty-text: #8b6173;
--primary-btn: #b96f8c;
--accent-btn: #d56a72;
--neutral-btn: #8f7080;
--menu-bg: #fffafd;
--menu-border: rgba(180, 84, 118, 0.22);
--menu-shadow: rgba(130, 55, 82, 0.15);
}
/* 淡红色应用主题,提供比淡粉更暖的卡片和背景色。 */
body[data-app-theme="red"] {
--app-bg: #fff1f0;
--app-text: #4a2420;
--header-bg: #ffe4e1;
--header-border: rgba(190, 83, 72, 0.2);
--field-bg: #fffafa;
--field-border: #efb5ad;
--muted-text: #7f4d47;
--empty-text: #8b625d;
--primary-btn: #b96a61;
--accent-btn: #c95f55;
--neutral-btn: #8c6b68;
--menu-bg: #fffafa;
--menu-border: rgba(190, 83, 72, 0.22);
--menu-shadow: rgba(130, 54, 48, 0.15);
}
body[data-app-theme="green"] {
--app-bg: #edf8ef;
--app-text: #253d2c;
--header-bg: #dff1e4;
--header-border: rgba(70, 140, 91, 0.2);
--field-bg: #fbfffc;
--field-border: #abcdb5;
--muted-text: #4f725a;
--empty-text: #617b68;
--primary-btn: #5e986b;
--accent-btn: #c98258;
--neutral-btn: #6e806f;
--menu-bg: #fbfffc;
--menu-border: rgba(70, 140, 91, 0.24);
--menu-shadow: rgba(48, 104, 64, 0.14);
}
body[data-app-theme="gray"] {
--app-bg: #f2f3f5;
--app-text: #30343a;
--header-bg: #e7e9ed;
--header-border: rgba(80, 86, 94, 0.18);
--field-bg: #ffffff;
--field-border: #bdc2ca;
--muted-text: #5c626b;
--empty-text: #727780;
--primary-btn: #627086;
--accent-btn: #b96f5e;
--neutral-btn: #747a84;
--menu-bg: #ffffff;
--menu-border: rgba(80, 86, 94, 0.2);
--menu-shadow: rgba(40, 45, 54, 0.13);
}
body[data-app-theme="mono"] {
--app-bg: #f8f8f8;
--app-text: #111111;
--header-bg: #ffffff;
--header-border: rgba(0, 0, 0, 0.18);
--field-bg: #ffffff;
--field-border: #a6a6a6;
--muted-text: #424242;
--empty-text: #626262;
--primary-btn: #111111;
--accent-btn: #555555;
--neutral-btn: #707070;
--danger-btn: #000000;
--menu-bg: #ffffff;
--menu-border: rgba(0, 0, 0, 0.24);
--menu-shadow: rgba(0, 0, 0, 0.15);
}
body {
margin: 0;
font-family: Arial, "Microsoft YaHei", sans-serif;
background: var(--app-bg);
color: var(--app-text);
transition: background 160ms ease, color 160ms ease;
}
/* 右键长按拖动背景时,禁止文本选择并显示移动状态。 */
body.background-dragging {
cursor: grabbing;
user-select: none;
}
.app-header {
position: sticky;
top: 0;
z-index: 10000;
padding: 18px 22px 14px;
background: var(--header-bg);
border-bottom: 1px solid var(--header-border);
transition: background 160ms ease, border-color 160ms ease;
}
h1 {
margin: 0 0 14px;
font-size: 24px;
font-weight: 700;
}
.toolbar {
display: flex;
gap: 10px;
align-items: center;
}
#searchInput {
flex: 1;
min-width: 180px;
padding: 10px 12px;
border: 1px solid var(--field-border);
border-radius: 6px;
background: var(--field-bg);
color: var(--app-text);
font-size: 15px;
outline: none;
}
button {
border: none;
border-radius: 6px;
color: #fff;
font-size: 15px;
cursor: pointer;
}
.toolbar button {
padding: 10px 16px;
}
button:active {
transform: translateY(1px);
}
#searchBtn {
background: var(--primary-btn);
}
#addBtn {
background: var(--accent-btn);
}
#appThemeBtn {
background: var(--neutral-btn);
}
#msg {
min-height: 20px;
margin: 10px 0 0;
color: var(--muted-text);
font-size: 14px;
}
#noteBoard {
position: relative;
min-height: calc(100vh - 130px);
padding: 18px;
}
.empty-state {
margin: 16px 4px;
color: var(--empty-text);
}
.note-card {
--card-bg: #fffdf6;
--card-text: #2f2a24;
--card-border: rgba(90, 70, 45, 0.2);
--card-shadow: rgba(65, 48, 31, 0.12);
--card-active-border: rgba(137, 104, 64, 0.55);
--card-active-shadow: rgba(65, 48, 31, 0.2);
--card-field-bg: #ffffff;
--card-field-border: #d8c9a9;
--card-muted: #766b5f;
--card-log: #4f463b;
--card-handle: #8a7863;
--card-save: #6f8f72;
--card-cancel: #8a7863;
position: absolute;
width: 300px;
min-height: 270px;
padding: 12px;
background: var(--card-bg);
color: var(--card-text);
border: 1px solid var(--card-border);
border-radius: 8px;
box-shadow: 0 8px 24px var(--card-shadow);
transition: background 160ms ease, border-color 160ms ease, box-shadow 160ms ease;
}
.note-card[data-card-theme="blue"] {
--card-bg: #eef8ff;
--card-text: #1d3547;
--card-border: rgba(72, 139, 192, 0.38);
--card-shadow: rgba(54, 115, 158, 0.14);
--card-active-border: rgba(57, 128, 180, 0.68);
--card-active-shadow: rgba(54, 115, 158, 0.24);
--card-field-bg: #fbfdff;
--card-field-border: #aed1ea;
--card-muted: #55788d;
--card-log: #315466;
--card-handle: #4f8bb4;
--card-save: #4f8bb4;
--card-cancel: #647f93;
}
.note-card[data-card-theme="pink"] {
--card-bg: #fff0f5;
--card-text: #4a2734;
--card-border: rgba(191, 104, 134, 0.34);
--card-shadow: rgba(152, 73, 101, 0.13);
--card-active-border: rgba(184, 89, 123, 0.68);
--card-active-shadow: rgba(152, 73, 101, 0.24);
--card-field-bg: #fffafd;
--card-field-border: #edb5c9;
--card-muted: #89586b;
--card-log: #664052;
--card-handle: #b96f8c;
--card-save: #b96f8c;
--card-cancel: #8f7080;
}
/* 淡红色卡片主题,供单张卡片和桌面便签复用。 */
.note-card[data-card-theme="red"] {
--card-bg: #fff1f0;
--card-text: #4a2420;
--card-border: rgba(197, 91, 80, 0.34);
--card-shadow: rgba(150, 62, 54, 0.13);
--card-active-border: rgba(190, 83, 72, 0.68);
--card-active-shadow: rgba(150, 62, 54, 0.24);
--card-field-bg: #fffafa;
--card-field-border: #efb5ad;
--card-muted: #8a5952;
--card-log: #663b36;
--card-handle: #b96a61;
--card-save: #b96a61;
--card-cancel: #8c6b68;
}
.note-card[data-card-theme="green"] {
--card-bg: #eff9f1;
--card-text: #253d2c;
--card-border: rgba(88, 153, 104, 0.34);
--card-shadow: rgba(60, 120, 74, 0.13);
--card-active-border: rgba(79, 142, 95, 0.68);
--card-active-shadow: rgba(60, 120, 74, 0.22);
--card-field-bg: #fbfffc;
--card-field-border: #b5d5bd;
--card-muted: #5a7a62;
--card-log: #3d6247;
--card-handle: #5e986b;
--card-save: #5e986b;
--card-cancel: #6e806f;
}
.note-card[data-card-theme="gray"] {
--card-bg: #f5f6f8;
--card-text: #30343a;
--card-border: rgba(103, 111, 122, 0.32);
--card-shadow: rgba(69, 75, 84, 0.12);
--card-active-border: rgba(98, 112, 134, 0.68);
--card-active-shadow: rgba(69, 75, 84, 0.22);
--card-field-bg: #ffffff;
--card-field-border: #c6cbd3;
--card-muted: #68707c;
--card-log: #4d535d;
--card-handle: #627086;
--card-save: #627086;
--card-cancel: #747a84;
}
.note-card[data-card-theme="mono"] {
--card-bg: #ffffff;
--card-text: #111111;
--card-border: #111111;
--card-shadow: rgba(0, 0, 0, 0.1);
--card-active-border: #000000;
--card-active-shadow: rgba(0, 0, 0, 0.18);
--card-field-bg: #ffffff;
--card-field-border: #777777;
--card-muted: #444444;
--card-log: #222222;
--card-handle: #111111;
--card-save: #111111;
--card-cancel: #686868;
}
.note-card.is-active {
border-color: var(--card-active-border);
box-shadow: 0 14px 34px var(--card-active-shadow);
}
.note-card.dragging {
cursor: grabbing;
user-select: none;
}
.note-card.can-drag {
cursor: grab;
touch-action: none;
}
.note-header {
display: flex;
gap: 8px;
align-items: center;
margin-bottom: 10px;
}
.drag-handle {
flex: 0 0 auto;
width: 28px;
height: 28px;
padding: 0;
border-radius: 50%;
background: var(--card-handle);
line-height: 28px;
font-size: 18px;
font-weight: 700;
cursor: grab;
touch-action: none;
}
.drag-handle:active {
cursor: grabbing;
}
.note-title {
width: 100%;
padding: 8px;
border: 1px solid var(--card-field-border);
border-radius: 6px;
background: var(--card-field-bg);
color: var(--card-text);
font-size: 15px;
font-weight: 700;
outline: none;
}
.note-content {
width: 100%;
height: 96px;
resize: vertical;
padding: 9px;
border: 1px solid var(--card-field-border);
border-radius: 6px;
background: var(--card-field-bg);
color: var(--card-text);
font-family: Arial, "Microsoft YaHei", sans-serif;
font-size: 14px;
line-height: 1.5;
outline: none;
}
.note-meta {
margin-top: 10px;
color: var(--card-muted);
font-size: 12px;
line-height: 1.5;
user-select: none;
}
.note-log {
margin-top: 8px;
color: var(--card-log);
font-size: 12px;
user-select: none;
}
.note-log summary {
cursor: pointer;
}
.note-log ul {
margin: 6px 0 0;
padding-left: 18px;
}
.floating-menu {
position: fixed;
z-index: 30000;
min-width: 150px;
padding: 8px;
background: var(--menu-bg);
border: 1px solid var(--menu-border);
border-radius: 8px;
box-shadow: 0 14px 34px var(--menu-shadow);
}
.floating-menu-title {
margin: 2px 4px 8px;
color: var(--muted-text);
font-size: 12px;
font-weight: 700;
}
.menu-action,
.theme-option {
display: flex;
width: 100%;
align-items: center;
gap: 8px;
padding: 8px 10px;
border-radius: 6px;
background: transparent;
color: var(--app-text);
font-size: 14px;
text-align: left;
}
.menu-action:hover,
.theme-option:hover,
.menu-action.is-active,
.theme-option.is-active {
background: rgba(0, 0, 0, 0.07);
}
.menu-action.danger {
color: var(--danger-btn);
}
.theme-swatch {
width: 16px;
height: 16px;
flex: 0 0 auto;
border: 1px solid rgba(0, 0, 0, 0.18);
border-radius: 50%;
}
.delete-confirm-card {
position: fixed;
z-index: 25000;
width: min(330px, calc(100vw - 32px));
padding: 16px;
background: var(--menu-bg);
border: 1px solid var(--menu-border);
border-radius: 8px;
box-shadow: 0 18px 42px var(--menu-shadow);
color: var(--app-text);
cursor: grab;
user-select: none;
touch-action: none;
}
.delete-confirm-card.dragging {
cursor: grabbing;
}
.delete-confirm-card h2 {
margin: 0 0 8px;
font-size: 18px;
}
.delete-confirm-card p {
margin: 0;
color: var(--muted-text);
font-size: 14px;
line-height: 1.5;
}
.confirm-actions {
display: flex;
justify-content: flex-end;
gap: 8px;
margin-top: 16px;
}
.confirm-actions button {
padding: 8px 13px;
font-size: 14px;
}
.confirm-cancel-btn {
background: var(--neutral-btn);
}
.confirm-delete-btn {
background: var(--danger-btn);
}
@media (max-width: 680px) {
.toolbar {
align-items: stretch;
flex-direction: column;
}
.toolbar button {
width: 100%;
}
.note-card {
width: calc(100vw - 36px);
}
}
</style>
</head>
<body>
<header class="app-header">
<h1>喵喵便签</h1>
<div class="toolbar">
<button id="appThemeBtn">主题</button>
<input id="searchInput" placeholder="搜索标题、正文或修改日志">
<button id="searchBtn">搜索</button>
<button id="addBtn">添加</button>
</div>
<p id="msg"></p>
</header>
<main id="noteBoard"></main>
<script>
// 文件系统 API 用于读写本地 notes.json,保存所有便签数据。
const fs = require('fs').promises;
// 路径 API 用于定位当前项目内的 notes.json。
const path = require('path');
// ipcRenderer 用于和 Electron 主进程通信,包括桌面便签、快捷键和窗口拖动。
const { ipcRenderer } = require('electron');
// 本地数据文件路径,所有应用内便签都会持久化到这个 JSON 文件。
const DATA_FILE = path.join(__dirname, 'notes.json');
// 卡片标准宽度,用于默认布局、居中和整理排序。
const CARD_WIDTH = 300;
// 卡片标准高度,用于动态扩展画布高度。
const CARD_HEIGHT = 270;
// 卡片画布容器,所有应用内卡片都挂载到这里。
const noteBoard = document.getElementById('noteBoard');
// 全局主题按钮,点击后展示应用主题菜单。
const appThemeBtn = document.getElementById('appThemeBtn');
// 搜索输入框,用于按标题、正文和日志搜索。
const searchInput = document.getElementById('searchInput');
// 搜索按钮,点击后执行搜索。
const searchBtn = document.getElementById('searchBtn');
// 新增按钮,点击后创建一张应用内草稿便签。
const addBtn = document.getElementById('addBtn');
// 顶部消息区域,展示保存、删除、同步等反馈。
const msgBox = document.getElementById('msg');
// 所有可选主题定义,全局主题、卡片主题和桌面便签主题共用这份列表。
const THEME_OPTIONS = [
{ id: 'default', name: '默认', swatch: '#fffdf6' },
{ id: 'blue', name: '淡蓝色', swatch: '#eef8ff' },
{ id: 'pink', name: '淡粉色', swatch: '#fff0f5' },
{ id: 'red', name: '淡红色', swatch: '#fff1f0' },
{ id: 'green', name: '淡绿色', swatch: '#eff9f1' },
{ id: 'gray', name: '淡灰色', swatch: '#f5f6f8' },
{ id: 'mono', name: '黑白色', swatch: '#ffffff' }
];
// 主题 id 集合,用于快速校验主题是否合法。
const THEME_IDS = new Set(THEME_OPTIONS.map((theme) => theme.id));
// 默认主题 id,旧数据或异常数据都会回落到它。
const DEFAULT_APP_THEME = 'default';
// 内存中的全部便签数据。
let notes = [];
// 当前搜索关键字,空字符串表示不过滤。
let activeKeyword = '';
// 卡片层级计数器,用于把选中卡片置顶。
let zCounter = 1;
// 当前全局主题,从 localStorage 恢复,没有保存时使用默认主题。
let appTheme = localStorage.getItem('appTheme') || DEFAULT_APP_THEME;
// 当前打开的浮动菜单引用,便于点击外部或拖动时关闭。
let openFloatingMenu = null;
// 当前删除确认卡片引用,便于关闭或替换。
let deleteConfirmCard = null;
// 渲染锁,避免重复渲染造成 DOM 状态抖动。
let isRendering = false;
// 背景右键长按计时器,达到阈值后进入拖动主窗口状态。
let backgroundDragTimer = null;
// 标记主窗口背景是否正在被右键长按拖动。
let isBackgroundDragging = false;
// 记录背景拖动上一次鼠标屏幕 X 坐标。
let backgroundDragStartX = 0;
// 记录背景拖动上一次鼠标屏幕 Y 坐标。
let backgroundDragStartY = 0;
// 每张便签的自动保存防抖计时器。
const autoSaveTimers = new Map();
// 标记内容已改但尚未保存的便签 id。
const dirtyNoteIds = new Set();
// 已保存快照集合,兼容历史取消逻辑和后续扩展。
const savedSnapshots = new Map();
// Returns the current local timestamp for created, updated, and log fields.
function nowText() {
return new Date().toLocaleString();
}
// Builds a unique note id so app cards and desktop cards can refer to the same data.
function makeId() {
return `note-${Date.now()}-${Math.random().toString(16).slice(2)}`;
}
// Deep-clones a note object for snapshots without sharing references.
function cloneNote(note) {
return JSON.parse(JSON.stringify(note));
}
// Writes operation feedback into the header message area.
function setMessage(text) {
msgBox.textContent = text;
}
// Validates a theme id and falls back to the default theme.
function normalizeTheme(themeId) {
return THEME_IDS.has(themeId) ? themeId : DEFAULT_APP_THEME;
}
// Applies the app theme and persists it in localStorage.
function applyAppTheme(themeId) {
appTheme = normalizeTheme(themeId);
document.body.dataset.appTheme = appTheme;
localStorage.setItem('appTheme', appTheme);
}
// Looks up the display name for a theme id.
function getThemeName(themeId) {
return THEME_OPTIONS.find((theme) => theme.id === themeId)?.name || '默认';
}
// Computes a fallback grid position for notes without saved coordinates.
function defaultPosition(index) {
return {
x: 18 + (index % 3) * 320,
y: 18 + Math.floor(index / 3) * 290
};
}
// Computes the center of the visible board area for new notes.
function centerPosition() {
const boardRect = noteBoard.getBoundingClientRect();
const visibleTop = Math.max(0, -boardRect.top);
const visibleHeight = window.innerHeight - Math.max(boardRect.top, 0);
return {
x: Math.max(18, Math.round((noteBoard.clientWidth - CARD_WIDTH) / 2)),
y: Math.max(18, Math.round(visibleTop + (visibleHeight - CARD_HEIGHT) / 2))
};
}
// Normalizes saved JSON data and removes old movement logs.
function normalizeNote(item, index) {
const position = defaultPosition(index);
const content = typeof item.content === 'string' ? item.content : (item.text || '');
const createdAt = item.createdAt || item.time || nowText();
const updatedAt = item.updatedAt || item.time || createdAt;
const zIndex = Number.isFinite(Number(item.zIndex)) ? Number(item.zIndex) : index + 1;
const logs = Array.isArray(item.logs) && item.logs.length > 0
? item.logs.filter((log) => log.text !== '移动位置')
: [{ time: updatedAt, text: item.text ? '从旧格式导入' : '创建便签' }];
zCounter = Math.max(zCounter, zIndex);
return {
id: item.id || makeId(),
title: item.title || content.slice(0, 14) || '未命名便签',
content,
createdAt,
updatedAt,
logs,
x: Number.isFinite(Number(item.x)) ? Number(item.x) : position.x,
y: Number.isFinite(Number(item.y)) ? Number(item.y) : position.y,
zIndex,
theme: normalizeTheme(item.theme)
};
}
// Converts in-memory notes into the JSON shape saved on disk.
function serializeNotes() {
return notes
.filter((note) => !note.isDraft)
.map((note) => ({
id: note.id,
title: note.title,
content: note.content,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
logs: note.logs,
x: note.x,
y: note.y,
zIndex: note.zIndex,
theme: normalizeTheme(note.theme)
}));
}
// Refreshes saved snapshots after a successful disk write.
function refreshSnapshots() {
savedSnapshots.clear();
serializeNotes().forEach((note) => {
savedSnapshots.set(note.id, cloneNote(note));
});
}
// ? notes.json ?????????????????
// Reads notes.json and returns normalized note objects.
async function readNotes() {
try {
const data = await fs.readFile(DATA_FILE, 'utf-8');
if (!data.trim()) {
return [];
}
const savedNotes = JSON.parse(data);
return Array.isArray(savedNotes)
? savedNotes.map((item, index) => normalizeNote(item, index))
: [];
} catch (error) {
if (error.code === 'ENOENT') {
return [];
}
throw error;
}
}
// Writes current notes to notes.json and refreshes snapshots.
async function writeNotes() {
await fs.writeFile(DATA_FILE, JSON.stringify(serializeNotes(), null, 2), 'utf-8');
refreshSnapshots();
}
// Appends a change log entry and updates the modified time.
function addLog(note, text) {
const time = nowText();
note.updatedAt = time;
note.logs.push({ time, text });
}
// Loads notes from disk, normalizes them, and renders the board.
async function loadNotes() {
try {
notes = await readNotes();
await writeNotes();
renderNotes();
} catch (error) {
setMessage(`读取失败:${error.message}`);
console.error(error);
}
}
// Checks whether a note matches the active search text.
function noteMatchesSearch(note) {
if (!activeKeyword) {
return true;
}
const logText = note.logs.map((log) => `${log.time} ${log.text}`).join(' ');
return `${note.title} ${note.content} ${logText}`.toLowerCase().includes(activeKeyword);
}
// Expands the board height to contain every positioned card.
function updateBoardHeight() {
const bottom = notes.reduce((max, note) => Math.max(max, note.y + CARD_HEIGHT), 0);
noteBoard.style.minHeight = `${Math.max(window.innerHeight - 130, bottom + 18)}px`;
}
// Renders all notes that match the current search filter.
function renderNotes() {
if (isRendering) {
return;
}
isRendering = true;
const visibleNotes = notes.filter(noteMatchesSearch);
noteBoard.innerHTML = '';
updateBoardHeight();
if (visibleNotes.length === 0) {
const empty = document.createElement('p');
empty.className = 'empty-state';
empty.textContent = activeKeyword ? '没有匹配的便签' : '暂无便签';
noteBoard.appendChild(empty);
isRendering = false;
return;
}
visibleNotes.forEach((note) => {
noteBoard.appendChild(createNoteCard(note));
});
isRendering = false;
}
// Creates a card element and wires editing, menus, autosave, and dragging.
function createNoteCard(note) {
const card = document.createElement('section');
card.className = 'note-card can-drag';
card.dataset.id = note.id;
card.dataset.cardTheme = normalizeTheme(note.theme);
card.style.left = `${note.x}px`;
card.style.top = `${note.y}px`;
card.style.zIndex = note.zIndex;
const header = document.createElement('div');
header.className = 'note-header';
const titleInput = document.createElement('input');
titleInput.className = 'note-title';
titleInput.value = note.title;
titleInput.placeholder = '标题';
const menuHandle = document.createElement('button');
menuHandle.className = 'drag-handle';
menuHandle.type = 'button';
menuHandle.textContent = '⋯';
menuHandle.title = '打开菜单';
menuHandle.setAttribute('aria-label', '打开菜单');
const contentInput = document.createElement('textarea');
contentInput.className = 'note-content';
contentInput.value = note.content;
contentInput.placeholder = '正文内容';
const meta = document.createElement('div');
meta.className = 'note-meta';
meta.innerHTML = `创建时间:${note.createdAt}<br>修改时间:${note.updatedAt}`;
const details = document.createElement('details');
details.className = 'note-log';
const summary = document.createElement('summary');
summary.textContent = '修改日志';
const logList = document.createElement('ul');
note.logs.slice().reverse().forEach((log) => {
const item = document.createElement('li');
item.textContent = `${log.time} - ${log.text}`;
logList.appendChild(item);
});
card.addEventListener('pointerdown', () => {
bringToFront(note, card);
});
titleInput.addEventListener('input', () => {
note.title = titleInput.value;
dirtyNoteIds.add(note.id);
scheduleAutoSave(note.id);
});
contentInput.addEventListener('input', () => {
note.content = contentInput.value;
dirtyNoteIds.add(note.id);
scheduleAutoSave(note.id);
});
titleInput.addEventListener('blur', () => flushAutoSave(note.id));
contentInput.addEventListener('blur', () => flushAutoSave(note.id));
titleInput.addEventListener('keydown', (event) => handleEnterSave(event, note.id));
contentInput.addEventListener('keydown', (event) => handleEnterSave(event, note.id));
menuHandle.addEventListener('click', (event) => {
event.preventDefault();
event.stopPropagation();
showCardMenu(note, card, menuHandle);
});
setupCardDrag(card, note);
header.append(titleInput, menuHandle);
details.append(summary, logList);
card.append(header, contentInput, meta, details);
return card;
}
// Raises the selected card above the others.
function bringToFront(note, card) {
zCounter += 1;
note.zIndex = zCounter;
document.querySelectorAll('.note-card').forEach((item) => {
item.classList.remove('is-active');
});
card.classList.add('is-active');