Приложение для заметок, с целями и с ии чатом · VibeGit
Приложение для заметок, с целями и с ии чатом Это приложение для заметок с ии(свои ключи), есть поддерживание целей, их отслеживание по %
prilozhenie-dlya-zametok-s-tselyami-i-s-ii-chato / src / components / screens / notes.tsx 1009 строк · изменён 5 нояб., 15:46
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
1001
1002
1003
1004
1005
1006
1007
1008
"use client" ;
import { useMemo, useRef, useState, type ComponentProps } from "react" ;
import {
Bold ,
Check ,
ChevronLeft ,
Code ,
Eye ,
FileText ,
GripVertical ,
Heading2 ,
Italic ,
ListChecks ,
MoreHorizontal ,
Paperclip ,
Pencil ,
Pin ,
Plus ,
Search ,
Strikethrough ,
Tag ,
Trash2 ,
Underline
id label
id label
id label
id label
id label
id label
sand
mint
sky
rose
lilac
sand
mint
sky
rose
lilac
text
list
mixed
catOf categories id
categories c c id
props
ref externalRef rest props
el
el
el
el
s
s
note
note note
note l l
note
done note c c
items
onRemove
items
onRemove id
items
images items a a
files items a a
images
images a
onRemove
files a
a a onRemove
items
onChange
readOnly
items
items
readOnly
inputRefs
rowRefs
dragId setDragId
dragDy
id
inputRefs id
id text
items c c id c text c
id
items c c id c done c c
index
item id text done
next items
next index item
next
item
index
next items _ i i index
next
prev next index next
prev prev
e id
e
e e
id
dragDy
e id
dragId id
row rowRefs id
h row row
dragDy e
from items c c id
shift dragDy h
to items from shift
to from
next items
moved next from
next to moved
next
dragDy
items item index
readOnly
open
onClose
value
onPick
open
value
id
state dispatch
newLabel setNewLabel
newColor setNewColor
state c
c
c
note
open
onClose
note
open
state dispatch
draft setDraft note
mode setMode
menuOpen setMenuOpen
catSheetOpen setCatSheetOpen
attachError setAttachError
confirmDelete setConfirmDelete
taRef
fileRef
open note draft id note
note
open note draft
id
title
body
checklist
attachments
pinned
kind
categoryId
updatedAt
deletedAt
draft
draft
draft
draft
draft
type
note draft updatedAt
p d d d p d
files
files draft
f files
att f
d d d attachments d att d
e
e e
fileRef fileRef
applyMd before after linePrefix
ta taRef
ta draft
selectionStart s selectionEnd e value ta
next
ns
ne
linePrefix
lineStart value s
next value lineStart before value lineStart
ns s before
ne e before
sel value s e
next value s before sel after value e
ns s before
ne ns sel
body next
ta
ta ns ne
icon label
icon label
icon label
icon label
icon label
icon label
reading mode
showBody draft draft
showList draft draft
cat draft state draft
draft
note cat
cat
reading reading
reading
draft
showBody
reading
t
reading
draft
draft
attachError
attachError
showList
draft
draft c c draft
reading
reading draft
reading
note
confirmDelete
state dispatch
query setQuery
catFilter setCatFilter
openNote setOpenNote
pageOpen setPageOpen
visible
q query
state
n n
n catFilter n catFilter
n
q
n q
n q
n c c q
a b
a b b a a
state query catFilter
usedCategories
state c
state n n n c
state state
note
note
state c
active catFilter c
used usedCategories u u c
c
visible
visible note i
cat state note
cat
note note note
note
note note
note c
c c
note
note
note
note
note cat cat
,
X
,
}
from
"lucide-react"
;
import
{ useStore }
from
"@/lib/store"
;
import
type
{
Attachment
,
Category
,
CategoryColor
,
ChecklistItem
,
Note
,
NoteKind
,
}
from
"@/lib/types"
;
import
{ cn, relativeLabel, toISODate, uid }
from
"@/lib/utils"
;
import
{ downloadDataUrl, fileToAttachment, fmtSize }
from
"@/lib/files"
;
import
{ exportNoteDocx, exportNoteMarkdown }
from
"@/lib/export"
;
import
{ Checkbox , EmptyState , FullPage , Segmented , Sheet , SwipeToDelete }
from
"../ui"
;
import
{ Markdown }
from
"@/lib/markdown"
;
const
CATEGORY_COLORS
:
{
:
CategoryColor
;
:
string
}
[
]
=
[
{
:
"sand"
,
:
"Песок"
}
,
{
:
"mint"
,
:
"Мята"
}
,
{
:
"sky"
,
:
"Небо"
}
,
{
:
"rose"
,
:
"Роза"
}
,
{
:
"lilac"
,
:
"Лаванда"
}
,
]
;
const
CAT_BG
:
Record
<
CategoryColor
,
string
>
=
{
:
"bg-note-sand"
,
:
"bg-note-mint"
,
:
"bg-note-sky"
,
:
"bg-note-rose"
,
:
"bg-note-lilac"
,
}
;
const
CAT_SOLID
:
Record
<
CategoryColor
,
string
>
=
{
:
"bg-cat-sand"
,
:
"bg-cat-mint"
,
:
"bg-cat-sky"
,
:
"bg-cat-rose"
,
:
"bg-cat-lilac"
,
}
;
const
KIND_LABEL
:
Record
<
NoteKind
,
string
>
=
{
:
"Текст"
,
:
"Список"
,
:
"Текст + список"
,
}
;
const
=
(
:
Category
[
]
,
:
string
|
null
)
:
Category
|
null
=>
.
find
(
(
)
=>
.
id
===
)
??
null
;
function
AutoTextarea
(
:
ComponentProps
<
"textarea"
>
)
{
const
{
:
,
...
}
=
;
const
resize
=
(
:
HTMLTextAreaElement
|
null
)
=>
{
if
(
!
)
return
;
.
style
.
height
=
"auto"
;
.
style
.
height
=
` ${ el. scrollHeight} px `
;
}
;
return
(
< textarea
{ ... rest}
ref = { ( el) => {
resize ( el) ;
if ( typeof externalRef === "function" ) externalRef ( el) ;
else if ( externalRef)
( externalRef as React . RefObject < HTMLTextAreaElement | null > ) . current = el;
} }
onInput = { ( e) => resize ( e. currentTarget ) }
/>
)
;
}
function
stripMarkdown
(
:
string
)
:
string
{
return
.
replace
(
/ ^ #{1,3} \s + / gm
,
""
)
.
replace
(
/ ( \* \* | __| \* | ~~| `) / g
,
""
)
.
replace
(
/ ^ >\s + / gm
,
""
)
;
}
function
notePreview
(
:
Note
)
:
string
{
if
(
.
kind
!==
"list"
&&
.
body
.
trim
(
)
)
return
stripMarkdown
(
.
body
.
trim
(
)
.
split
(
"\n"
)
.
find
(
(
)
=>
.
trim
(
)
)
??
""
)
;
if
(
.
checklist
.
length
)
{
const
=
.
checklist
.
filter
(
(
)
=>
.
done
)
.
length
;
return
` ${ done} из ${ note. checklist. length} выполнено `
;
}
return
"Пустая заметка"
;
}
function
Attachments
(
{
,
,
}
:
{
:
Attachment
[
]
;
?
:
(
:
string
)
=>
void
;
}
)
{
if
(
!
.
length
)
return
null
;
const
=
.
filter
(
(
)
=>
.
mime
.
startsWith
(
"image/"
)
)
;
const
=
.
filter
(
(
)
=>
!
.
mime
.
startsWith
(
"image/"
)
)
;
return
(
< div className = " flex flex-col gap-2" >
{
.
length
>
0
&&
(
< div className = { cn ( "grid gap-2" , images. length > 1 ? "grid-cols-2" : "grid-cols-1" ) } >
{
.
map
(
(
)
=>
(
< div key = { a. id } className = " relative overflow-hidden rounded-2xl border border-line" >
{
}
< img
src = { a. dataUrl }
alt = { a. name }
className = " max-h-56 w-full cursor-zoom-in object-cover"
onClick = { ( ) => ! onRemove && downloadDataUrl ( a. name , a. dataUrl ) }
/>
{
&&
(
< button
onClick = { ( ) => onRemove ( a. id ) }
aria-label = { ` Удалить вложение ${ a. name} ` }
className = " absolute right-2 top-2 flex h-7 w-7 items-center justify-center rounded-full bg-black/55 text-white transition-all hover:bg-danger active:scale-90"
>
< X className = " h-4 w-4" />
</ button>
)
}
</ div>
)
)
}
</ div>
)
}
{
.
map
(
(
)
=>
(
< div
key = { a. id }
className = " flex items-center gap-2.5 rounded-xl border border-line bg-card-2/50 px-3 py-2.5"
>
< FileText className = " h-4 w-4 shrink-0 text-accent" />
< span className = " min-w-0 flex-1 truncate text-sm font-semibold text-body" >
{
.
name
}
</ span>
< span className = " num text-[11px] text-faint" >
{
fmtSize
(
.
size
)
}
</ span>
{
?
(
< button
onClick = { ( ) => onRemove ( a. id ) }
aria-label = { ` Удалить файл ${ a. name} ` }
className = " flex h-7 w-7 items-center justify-center rounded-full text-faint hover:bg-danger/10 hover:text-danger active:scale-90"
>
< X className = " h-3.5 w-3.5" />
</ button>
)
:
(
< button
onClick = { ( ) => downloadDataUrl ( a. name , a. dataUrl ) }
aria-label = { ` Скачать ${ a. name} ` }
className = " flex h-7 w-7 items-center justify-center rounded-full text-faint hover:bg-accent-soft hover:text-accent active:scale-90"
>
< FileText className = " h-3.5 w-3.5" />
</ button>
)
}
</ div>
)
)
}
</ div>
)
;
}
function
ChecklistItems
(
{
,
,
=
false
,
}
:
{
:
ChecklistItem
[
]
;
onChange
:
(
:
ChecklistItem
[
]
)
=>
void
;
?
:
boolean
;
}
)
{
const
=
useRef
(
new
Map< string , HTMLInputElement>
(
)
)
;
const
=
useRef
(
new
Map< string , HTMLDivElement>
(
)
)
;
const
[
,
]
=
useState < string | null >
(
null
)
;
const
=
useRef
(
0
)
;
const
focusItem
=
(
:
string
)
=>
setTimeout
(
(
)
=>
.
current
.
get
(
)
?.
focus
(
)
,
30
)
;
const
updateText
=
(
:
string
,
:
string
)
=>
onChange
(
.
map
(
(
)
=>
(
.
id
===
?
{
...
,
}
:
)
)
)
;
const
toggle
=
(
:
string
)
=>
onChange
(
.
map
(
(
)
=>
(
.
id
===
?
{
...
,
:
!
.
done
}
:
)
)
)
;
const
insertAfter
=
(
:
number
)
=>
{
const
:
ChecklistItem
=
{
:
uid
(
"c"
)
,
:
""
,
:
false
}
;
const
=
[
...
]
;
.
splice
(
+
1
,
0
,
)
;
onChange
(
)
;
focusItem
(
.
id
)
;
}
;
const
removeAt
=
(
:
number
)
=>
{
const
=
.
filter
(
(
,
)
=>
!==
)
;
onChange
(
)
;
const
=
[
-
1
]
??
[
0
]
;
if
(
)
focusItem
(
.
id
)
;
}
;
const
startDrag
=
(
:
React
.
PointerEvent
,
:
string
)
=>
{
.
preventDefault
(
)
;
(
.
currentTarget
as
HTMLElement
)
.
setPointerCapture
(
.
pointerId
)
;
setDragId
(
)
;
.
current
=
0
;
}
;
const
moveDrag
=
(
:
React
.
PointerEvent
,
:
string
)
=>
{
if
(
!==
)
return
;
const
=
.
current
.
get
(
)
;
const
=
?
.
getBoundingClientRect
(
)
.
height
:
40
;
.
current
+=
.
movementY
??
0
;
const
=
.
findIndex
(
(
)
=>
.
id
===
)
;
const
=
Math
.
round
(
.
current
/
)
;
const
=
Math
.
max
(
0
,
Math
.
min
(
.
length
-
1
,
+
)
)
;
if
(
!==
)
{
const
=
[
...
]
;
const
[
]
=
.
splice
(
,
1
)
;
.
splice
(
,
0
,
)
;
onChange
(
)
;
.
current
=
0
;
}
}
;
const
endDrag
=
(
)
=>
setDragId
(
null
)
;
return
(
< ul className = " flex flex-col gap-1 select-none" >
{
.
map
(
(
,
)
=>
(
< li key = { item. id } >
< div
ref = { ( el) => {
if ( el) rowRefs. current . set ( item. id , el) ;
} }
className = { cn (
"group flex items-center gap-1.5 rounded-xl px-1 py-1 transition-colors" ,
dragId === item. id ? "z-10 bg-card-2 shadow-md" : "hover:bg-card-2/50"
) }
>
< button
type = " button"
aria-label = { ` Перетащить пункт « ${ item. text || "пустой" } » ` }
onPointerDown = { ( e) => startDrag ( e, item. id ) }
onPointerMove = { ( e) => moveDrag ( e, item. id ) }
onPointerUp = { endDrag}
onPointerCancel = { endDrag}
className = " flex h-7 w-5 shrink-0 cursor-grab touch-none items-center justify-center text-faint/60 transition-colors hover:text-faint active:cursor-grabbing"
>
< GripVertical className = " h-4 w-4" />
</ button>
< Checkbox checked = { item. done } onChange = { ( ) => toggle ( item. id ) } label = { item. text || "пункт" } />
< input
ref = { ( el) => {
if ( el) inputRefs. current . set ( item. id , el) ;
} }
value = { item. text }
readOnly = { readOnly}
onChange = { ( e) => ! readOnly && updateText ( item. id , e. target . value ) }
onKeyDown = { ( e) => {
if ( readOnly || e. nativeEvent . isComposing ) return ;
if ( e. key === "Enter" ) {
e. preventDefault ( ) ;
insertAfter ( index) ;
} else if ( e. key === "Backspace" && ! item. text ) {
e. preventDefault ( ) ;
removeAt ( index
placeholder = " Пункт…"
aria-label = { ` Пункт ${ index + 1 } ` }
className = { cn (
"h-8 min-w-0 flex-1 bg-transparent text-[15px] placeholder:text-faint/50 focus:outline-none" ,
readOnly ? "cursor-default" : "" ,
item. done ? "text-faint line-through" : "text-body"
) }
/>
{
!
&&
(
< button
onClick = { ( ) => removeAt ( index) }
aria-label = " Удалить пункт"
className = " flex h-7 w-7 shrink-0 items-center justify-center rounded-full text-faint transition-all hover:bg-danger/10 hover:text-danger active:scale-90"
>
< Trash2 className = " h-3.5 w-3.5" />
</ button>
)
}
</ div>
</ li>
)
)
}
</ ul>
)
;
}
function
CategorySheet
(
{
,
,
,
,
}
:
{
:
boolean
;
onClose
:
(
)
=>
void
;
:
string
|
null
;
onPick
:
(
:
string
|
null
)
=>
void
;
}
)
{
const
{
,
}
=
useStore
(
)
;
const
[
,
]
=
useState
(
""
)
;
const
[
,
]
=
useState < CategoryColor>
(
"sky"
)
;
return
(
< Sheet open = { open} onClose = { onClose} title = " Категория" >
< ul className = " flex flex-col gap-1.5" >
< li>
< button
onClick = { ( ) => {
onPick ( null ) ;
onClose ( ) ;
} }
className = { cn (
"flex w-full items-center gap-2.5 rounded-xl border px-3.5 py-2.5 text-sm font-semibold transition-all active:scale-[0.98]" ,
value === null ? "border-accent bg-accent-soft text-accent-strong" : "border-line text-body"
) }
>
< span className = " h-3 w-3 rounded-full border border-line bg-card" />
Без категории
</ button>
</ li>
{
.
categories
.
map
(
(
)
=>
(
< li key = { c. id } >
< button
onClick = { ( ) => {
onPick ( c. id ) ;
onClose ( ) ;
} }
className = { cn (
"flex w-full items-center gap-2.5 rounded-xl border px-3.5 py-2.5 text-sm font-semibold transition-all active:scale-[0.98]" ,
value === c. id ? "border-accent bg-accent-soft text-accent-strong" : "border-line text-body"
) }
>
< span className = { cn ( "h-3 w-3 rounded-full border border-line/60" , CAT_SOLID [ c. color ] ) } />
{
.
label
}
</ button>
</ li>
)
)
}
</ ul>
< div className = " mt-4 border-t border-line pt-4" >
< p className = " mb-2 text-xs font-bold uppercase tracking-wider text-faint" >
Новая категория
</ p>
< div className = " flex items-center gap-2" >
< input
value = { newLabel}
onChange = { ( e) => setNewLabel ( e. target . value ) }
placeholder = " Например: Здоровье"
aria-label = " Название новой категории"
className = " h-10 flex-1 rounded-xl border border-line bg-card px-3 text-sm text-body placeholder:text-faint/60 focus:border-accent focus:outline-none"
/>
</ div>
< div className = " mt-2 flex items-center justify-between" >
< div className = " flex gap-1.5" role = " radiogroup" aria-label = " Цвет категории" >
{
CATEGORY_COLORS
.
map
(
(
)
=>
(
< button
key = { c. id }
role = " radio"
aria-checked = { newColor === c. id }
aria-label = { c. label }
title = { c. label }
onClick = { ( ) => setNewColor ( c. id ) }
className = { cn (
"h-6 w-6 rounded-full border transition-all active:scale-90" ,
CAT_BG [ c. id ] ,
newColor === c. id ? "border-accent ring-2 ring-accent/40" : "border-line"
) }
/>
)
)
}
</ div>
< button
onClick = { ( ) => {
if ( ! newLabel. trim ( ) ) return ;
dispatch ( { type: "category/add" , label: newLabel, color: newColor } ) ;
setNewLabel ( "" ) ;
} }
disabled = { ! newLabel. trim ( ) }
className = " flex items-center gap-1 rounded-full bg-accent px-3.5 py-1.5 text-xs font-bold text-white transition-all hover:brightness-110 active:scale-95 disabled:opacity-40"
>
< Plus className = " h-3.5 w-3.5" />
Создать
</ button>
</ div>
</ div>
</ Sheet >
)
;
}
function
NotePage
(
{
,
,
,
}
:
{
:
Note
|
null
;
:
boolean
;
onClose
:
(
)
=>
void
;
}
)
{
const
{
,
}
=
useStore
(
)
;
const
[
,
]
=
useState < Note | null >
(
)
;
const
[
,
]
=
useState < "edit" | "read" >
(
"edit"
)
;
const
[
,
]
=
useState
(
false
)
;
const
[
,
]
=
useState
(
false
)
;
const
[
,
]
=
useState
(
""
)
;
const
[
,
]
=
useState
(
false
)
;
const
=
useRef < HTMLTextAreaElement>
(
null
)
;
const
=
useRef < HTMLInputElement>
(
null
)
;
if
(
&&
&&
?.
!==
.
id
)
{
setDraft
(
structuredClone
(
)
)
;
setMode
(
"edit"
)
;
setAttachError
(
""
)
;
setConfirmDelete
(
false
)
;
}
if
(
&&
===
null
&&
===
null
)
{
setDraft
(
{
:
uid
(
"n"
)
,
:
""
,
:
""
,
:
[
]
,
:
[
]
,
:
false
,
:
"text"
,
:
null
,
:
toISODate
(
new
Date
(
)
)
,
:
null
,
}
)
;
setMode
(
"edit"
)
;
setAttachError
(
""
)
;
setConfirmDelete
(
false
)
;
}
const
close
=
(
)
=>
{
if
(
&&
(
.
title
.
trim
(
)
||
.
body
.
trim
(
)
||
.
checklist
.
length
||
.
attachments
.
length
)
)
{
dispatch
(
{
:
"note/upsert"
,
:
{
...
,
:
toISODate
(
new
Date
(
)
)
}
,
}
)
;
}
setDraft
(
null
)
;
onClose
(
)
;
}
;
const
patch
=
(
:
Partial
<
Note
>
)
=>
setDraft
(
(
)
=>
(
?
{
...
,
...
}
:
)
)
;
const
addFiles
=
async
(
:
FileList
|
null
)
=>
{
if
(
!
||
!
)
return
;
setAttachError
(
""
)
;
for
(
const
of
Array
.
from
(
)
)
{
try
{
const
=
await
fileToAttachment
(
)
;
setDraft
(
(
)
=>
(
?
{
...
,
:
[
...
.
attachments
,
]
}
:
)
)
;
}
catch
(
)
{
setAttachError
(
instanceof
Error
?
.
message
:
"Не удалось прикрепить файл"
)
;
}
}
if
(
.
current
)
.
current
.
value
=
""
;
}
;
const
=
(
:
string
,
=
""
,
=
false
)
=>
{
const
=
.
current
;
if
(
!
||
!
)
return
;
const
{
:
,
:
,
}
=
;
let
:
string
;
let
:
number
;
let
:
number
;
if
(
)
{
const
=
.
lastIndexOf
(
"\n"
,
-
1
)
+
1
;
=
.
slice
(
0
,
)
+
+
.
slice
(
)
;
=
+
.
length
;
=
+
.
length
;
}
else
{
const
=
.
slice
(
,
)
||
"текст"
;
=
.
slice
(
0
,
)
+
+
+
+
.
slice
(
)
;
=
+
.
length
;
=
+
.
length
;
}
patch
(
{
:
}
)
;
requestAnimationFrame
(
(
)
=>
{
.
focus
(
)
;
.
setSelectionRange
(
,
)
;
}
)
;
}
;
const
MD_TOOLS
=
[
{
:
Heading2
,
:
"Заголовок"
,
fn
:
(
)
=>
applyMd
(
"## "
,
""
,
true
)
}
,
{
:
Bold
,
:
"Жирный"
,
fn
:
(
)
=>
applyMd
(
"**"
,
"**"
)
}
,
{
:
Italic
,
:
"Курсив"
,
fn
:
(
)
=>
applyMd
(
"*"
,
"*"
)
}
,
{
:
Underline
,
:
"Подчёркнутый"
,
fn
:
(
)
=>
applyMd
(
"__"
,
"__"
)
}
,
{
:
Strikethrough
,
:
"Зачёркнутый"
,
fn
:
(
)
=>
applyMd
(
"~~"
,
"~~"
)
}
,
{
:
Code
,
:
"Код"
,
fn
:
(
)
=>
applyMd
(
"`"
,
"`"
)
}
,
]
;
const
=
===
"read"
;
const
=
&&
.
kind
!==
"list"
;
const
=
&&
.
kind
!==
"text"
;
const
=
?
catOf
(
.
categories
,
.
categoryId
)
:
null
;
return
(
< FullPage open = { open} onClose = { close} label = { note ? "Заметка" : "Новая заметка" } >
{
&&
(
< >
< header className = " flex items-center gap-1.5 border-b border-line/60 px-4 py-3" >
< button
onClick = { close}
aria-label = " Сохранить и вернуться"
className = " flex h-10 w-10 items-center justify-center rounded-full text-body transition-all hover:bg-card-2 active:scale-90"
>
< ChevronLeft className = " h-5 w-5" />
</ button>
< p className = " min-w-0 flex-1 truncate text-xs font-medium text-faint" >
{
?
` Отредактировано: ${ relativeLabel ( note. updatedAt) } `
:
"Новая заметка"
}
</ p>
{
&&
(
< span className = { cn ( "hidden items-center gap-1.5 rounded-full px-2.5 py-1 text-xs font-bold text-body min-[380px]:flex" , CAT_BG [ cat. color ] ) } >
< span className = { cn ( "h-2 w-2 rounded-full" , CAT_SOLID [ cat. color ] ) } />
{
.
label
}
</ span>
)
}
< button
onClick = { ( ) => setMode ( reading ? "edit" : "read" ) }
aria-label = { reading ? "Перейти в режим редактирования" : "Перейти в режим чтения" }
title = { reading ? "Редактировать" : "Режим чтения" }
className = { cn (
"flex h-10 w-10 items-center justify-center rounded-full transition-all active:scale-90" ,
reading ? "bg-accent text-white" : "text-faint hover:bg-card-2 hover:text-body"
) }
>
{
?
< Pencil className = " h-4.5 w-4.5" />
:
< Eye className = " h-4.5 w-4.5" />
}
</ button>
< button
onClick = { ( ) => setMenuOpen ( true ) }
aria-label = " Действия с заметкой"
className = " flex h-10 w-10 items-center justify-center rounded-full text-faint transition-all hover:bg-card-2 hover:text-body active:scale-90"
>
< MoreHorizontal className = " h-5 w-5" />
</ button>
</ header>
< main className = " flex-1 overflow-y-auto px-5 py-4 scrollbar-none" >
{
!
&&
(
< div className = " mb-3" >
< Segmented
ariaLabel = " Тип заметки"
options = { ( [ "text" , "list" , "mixed" ] as NoteKind [ ] ) . map ( ( k) => ( {
value: k,
label: KIND_LABEL [ k] ,
} ) ) }
value = { draft. kind }
onChange = { ( kind) => patch ( { kind } ) }
/>
</ div>
)
}
{
?
(
< h2 className = " text-2xl font-extrabold tracking-tight text-body" >
{
.
title
||
"Без названия"
}
</ h2>
)
:
(
< input
value = { draft. title }
onChange = { ( e) => patch ( { title: e. target . value } ) }
placeholder = " Заголовок"
aria-label = " Заголовок заметки"
className = " w-full bg-transparent text-2xl font-extrabold tracking-tight text-body placeholder:text-faint/50 focus:outline-none"
/>
)
}
{
&&
(
< div className = " mt-3" >
{
!
&&
(
< div className = " mb-1.5 flex items-center gap-0.5 rounded-xl border border-line bg-card-2/50 p-1" >
{
MD_TOOLS
.
map
(
(
)
=>
(
< button
key = { t. label }
onClick = { t. fn }
aria-label = { t. label }
title = { t. label }
className = " flex h-8 w-8 items-center justify-center rounded-lg text-faint transition-all hover:bg-card hover:text-body active:scale-90"
>
< t.icon className = " h-4 w-4" />
</ button>
)
)
}
</ div>
)
}
{
?
(
.
body
.
trim
(
)
?
(
< Markdown text = { draft. body } />
)
:
null
)
:
(
< AutoTextarea
ref = { taRef}
value = { draft. body }
onChange = { ( e) => patch ( { body: e. target . value } ) }
placeholder = { "Текст заметки… Markdown:\n## заголовок, **жирный**, *курсив*, __подчёркнутый__, ~~зачёркнутый~~, `код`" }
aria-label = " Текст заметки"
className = " min-h-40 w-full resize-none overflow-hidden rounded-2xl bg-transparent p-1 text-[15px] leading-relaxed text-body placeholder:text-faint/50 focus:outline-none"
/>
)
}
</ div>
)
}
{
.
attachments
.
length
>
0
&&
(
< div className = " mt-4" >
< Attachments
items = { draft. attachments }
onRemove = {
reading
? undefined
: ( id) =>
patch ( { attachments: draft. attachments . filter ( ( a) => a. id !== id) } )
}
/>
</ div>
)
}
{
&&
(
< p className = " mt-2 rounded-xl bg-danger/10 px-3 py-2 text-xs font-semibold text-danger" >
{
}
</ p>
)
}
{
&&
(
< div className = " mt-4" >
< div className = " mb-2 flex items-center justify-between" >
< p className = " flex items-center gap-1.5 text-xs font-bold uppercase tracking-wider text-faint" >
< ListChecks className = " h-3.5 w-3.5" />
Чек-лист
</ p>
{
.
checklist
.
length
>
0
&&
(
< span className = " num text-xs font-semibold text-faint" >
{
.
checklist
.
filter
(
(
)
=>
.
done
)
.
length
}
/
{
.
checklist
.
length
}
</ span>
)
}
</ div>
< ChecklistItems
readOnly = { reading}
items = { draft. checklist }
onChange = { ( checklist) => patch ( { checklist } ) }
/>
{
!
&&
(
< button
onClick = { ( ) =>
patch ( {
checklist: [ ... draft. checklist , { id: uid ( "c" ) , text: "" , done: false } ] ,
} )
}
className = " mt-1.5 flex items-center gap-1.5 rounded-xl px-2 py-2 text-sm font-semibold text-faint transition-colors hover:text-accent active:scale-95"
>
< Plus className = " h-4 w-4" />
Добавить пункт
</ button>
)
}
{
&&
.
checklist
.
length
>
0
&&
(
< p className = " mt-1 pl-1 text-[11px] text-faint/70" >
Удерживайте «⋮⋮» и тяните, чтобы поменять порядок
</ p>
)
}
</ div>
)
}
< div className = " h-6" />
</ main>
{
!
&&
(
< footer className = " flex items-center gap-1.5 border-t border-line/60 px-4 py-2.5" >
< button
onClick = { ( ) => fileRef. current ?. click ( ) }
aria-label = " Прикрепить файл или изображение"
title = " Прикрепить файл или изображение"
className = " flex h-10 w-10 items-center justify-center rounded-full text-faint transition-all hover:bg-card-2 hover:text-body active:scale-90"
>
< Paperclip className = " h-5 w-5" />
</ button>
< button
onClick = { ( ) => setCatSheetOpen ( true ) }
aria-label = " Категория заметки"
title = " Категория"
className = " flex h-10 w-10 items-center justify-center rounded-full text-faint transition-all hover:bg-card-2 hover:text-body active:scale-90"
>
< Tag className = " h-5 w-5" />
</ button>
< div className = " flex-1" />
< button
onClick = { close}
className = " flex items-center gap-1.5 rounded-full bg-accent px-5 py-2.5 text-sm font-bold text-white transition-all hover:brightness-110 active:scale-95"
>
< Check className = " h-4 w-4" />
Готово
</ button>
</ footer>
)
}
< CategorySheet
open = { catSheetOpen}
onClose = { ( ) => setCatSheetOpen ( false ) }
value = { draft. categoryId }
onPick = { ( id) => patch ( { categoryId: id } ) }
/>
< input
ref = { fileRef}
type = " file"
multiple
className = " hidden"
aria-hidden
tabIndex = { - 1 }
onChange = { ( e) => void addFiles ( e. target . files ) }
/>
< Sheet open = { menuOpen} onClose = { ( ) => setMenuOpen ( false ) } title = " Действия" >
< div className = " flex flex-col gap-1.5" >
< button
onClick = { ( ) => {
exportNoteMarkdown ( draft) ;
setMenuOpen ( false ) ;
} }
className = " flex items-center gap-3 rounded-xl border border-line bg-card px-4 py-3 text-sm font-semibold text-body transition-all hover:border-accent/50 active:scale-[0.98]"
>
< FileText className = " h-4 w-4 text-accent" />
Экспорт в Markdown (.md)
</ button>
< button
onClick = { ( ) => {
void exportNoteDocx ( draft) ;
setMenuOpen ( false ) ;
} }
className = " flex items-center gap-3 rounded-xl border border-line bg-card px-4 py-3 text-sm font-semibold text-body transition-all hover:border-accent/50 active:scale-[0.98]"
>
< FileText className = " h-4 w-4 text-accent" />
Экспорт в Word (.docx)
</ button>
{
&&
(
< button
onClick = { ( ) => {
if ( ! confirmDelete) {
setConfirmDelete ( true ) ;
setTimeout ( ( ) => setConfirmDelete ( false ) , 2500 ) ;
return ;
}
dispatch ( { type: "note/soft-delete" , id: note. id } ) ;
setMenuOpen ( false ) ;
setDraft ( null ) ;
onClose ( ) ;
} }
className = { cn (
"flex items-center gap-3 rounded-xl border px-4 py-3 text-sm font-semibold transition-all active:scale-[0.98]" ,
confirmDelete
? "border-danger bg-danger text-white"
: "border-line bg-card text-danger"
) }
>
< Trash2 className = " h-4 w-4" />
{
?
"Точно удалить? В корзину."
:
"В корзину (30 дней)"
}
</ button>
)
}
</ div>
</ Sheet >
</ >
)
}
</ FullPage >
)
;
}
export
function
NotesScreen
(
)
{
const
{
,
}
=
useStore
(
)
;
const
[
,
]
=
useState
(
""
)
;
const
[
,
]
=
useState < string | "all" >
(
"all"
)
;
const
[
,
]
=
useState < Note | null >
(
null
)
;
const
[
,
]
=
useState
(
false
)
;
const
=
useMemo
(
(
)
=>
{
const
=
.
trim
(
)
.
toLowerCase
(
)
;
return
.
notes
.
filter
(
(
)
=>
!
.
deletedAt
)
.
filter
(
(
)
=>
===
"all"
||
.
categoryId
===
)
.
filter
(
(
)
=>
!
||
.
title
.
toLowerCase
(
)
.
includes
(
)
||
.
body
.
toLowerCase
(
)
.
includes
(
)
||
.
checklist
.
some
(
(
)
=>
.
text
.
toLowerCase
(
)
.
includes
(
)
)
)
.
sort
(
(
,
)
=>
.
pinned
===
.
pinned
?
.
updatedAt
.
localeCompare
(
.
updatedAt
)
:
.
pinned
?
-
1
:
1
)
;
}
,
[
.
notes
,
,
]
)
;
const
=
useMemo
(
(
)
=>
.
categories
.
filter
(
(
)
=>
.
notes
.
some
(
(
)
=>
!
.
deletedAt
&&
.
categoryId
===
.
id
)
)
,
[
.
categories
,
.
notes
]
)
;
const
openFor
=
(
:
Note
|
null
)
=>
{
setOpenNote
(
)
;
setPageOpen
(
true
)
;
}
;
return
(
< div className = " px-5 pt-4" >
< label className = " relative block" >
< Search className = " pointer-events-none absolute left-3.5 top-1/2 h-4 w-4 -translate-y-1/2 text-faint" />
< input
value = { query}
onChange = { ( e) => setQuery ( e. target . value ) }
placeholder = " Поиск по заметкам"
aria-label = " Поиск по заметкам"
className = " h-11 w-full rounded-2xl border border-line bg-card pl-10 pr-4 text-sm text-body placeholder:text-faint/70 focus:border-accent focus:outline-none"
/>
</ label>
{
}
< div className = " mt-3 flex items-center gap-1.5 overflow-x-auto scrollbar-none" role = " radiogroup" aria-label = " Фильтр по категории" >
< button
role = " radio"
aria-checked = { catFilter === "all" }
onClick = { ( ) => setCatFilter ( "all" ) }
className = { cn (
"shrink-0 rounded-full border px-3.5 py-1.5 text-xs font-bold transition-all active:scale-95" ,
catFilter === "all"
? "border-accent bg-accent text-white"
: "border-line bg-card text-faint hover:text-body"
) }
>
Все
</ button>
{
.
categories
.
map
(
(
)
=>
{
const
=
===
.
id
;
const
=
.
some
(
(
)
=>
.
id
===
.
id
)
;
return
(
< button
key = { c. id }
role = " radio"
aria-checked = { active}
onClick = { ( ) => setCatFilter ( active ? "all" : c. id ) }
className = { cn (
"shrink-0 rounded-full px-3.5 py-1.5 text-xs font-bold transition-all active:scale-95" ,
CAT_BG [ c. color ] ,
"text-body" ,
active
? "ring-2 ring-accent"
: used
? "border border-line/60 opacity-90 hover:opacity-100"
: "border border-line/60 opacity-55 hover:opacity-90"
) }
>
{
.
label
}
</ button>
)
;
}
)
}
</ div>
{
.
length
===
0
?
(
< EmptyState
image = " empty-notes"
title = { query || catFilter !== "all" ? "Ничего не нашлось" : "Пока пусто" }
hint = {
query || catFilter !== "all"
? "Попробуйте другие слова или сбросьте фильтр."
: "Первая мысль — самая важная. Запишите её, пока не улетела."
}
action = {
< button
onClick = { ( ) => openFor ( null ) }
className = " flex items-center gap-1.5 rounded-full bg-accent px-4 py-2 text-sm font-bold text-white transition-all hover:brightness-110 active:scale-95"
>
< Plus className = " h-4 w-4" /> Новая заметка
</ button>
}
/>
)
:
(
< ul className = " mt-4 flex flex-col gap-2.5" >
{
.
map
(
(
,
)
=>
{
const
=
catOf
(
.
categories
,
.
categoryId
)
;
return
(
< li
key = { note. id }
className = " animate-rise"
style = { { animationDelay: ` ${ Math. min ( i, 6 ) * 40 } ms ` } }
>
< SwipeToDelete
label = { note. title || "Без названия" }
onDelete = { ( ) => dispatch ( { type: "note/soft-delete" , id: note. id } ) }
>
< div
role = " button"
tabIndex = { 0 }
onClick = { ( ) => openFor ( note) }
onKeyDown = { ( e) => e. key === "Enter" && openFor ( note) }
className = " group relative w-full cursor-pointer overflow-hidden rounded-2xl border border-line bg-card p-4 pl-5 text-left transition-all duration-150 hover:border-accent/50 hover:shadow-sm active:scale-[0.98] focus-visible:outline-2 focus-visible:outline-accent"
>
{
&&
(
< span
aria-hidden
className = { cn ( "absolute inset-y-0 left-0 w-1.5" , CAT_SOLID [ cat. color ] ) }
/>
)
}
< div className = " flex items-start justify-between gap-3" >
< h3 className = " font-bold leading-snug text-body" >
{
.
title
||
"Без названия"
}
</ h3>
< button
onClick = { ( e) => {
e. stopPropagation ( ) ;
dispatch ( { type: "note/toggle-pin" , id: note. id } ) ;
} }
aria-label = { note. pinned ? "Открепить заметку" : "Закрепить заметку" }
className = { cn (
"flex h-7 w-7 shrink-0 items-center justify-center rounded-full transition-all active:scale-90" ,
note. pinned
? "bg-accent-soft text-accent"
: "text-faint opacity-0 hover:bg-card-2 group-hover:opacity-100 focus-visible:opacity-100"
) }
>
< Pin className = " h-3.5 w-3.5" fill = { note. pinned ? "currentColor" : "none" } />
</ button>
</ div>
{
.
kind
!==
"list"
&&
.
body
.
trim
(
)
&&
(
< p className = " mt-1 line-clamp-2 text-sm leading-relaxed text-faint" >
{
notePreview
(
)
}
</ p>
)
}
{
.
kind
!==
"text"
&&
.
checklist
.
length
>
0
&&
(
< ul className = " mt-2 flex flex-col gap-1" >
{
.
checklist
.
slice
(
0
,
3
)
.
map
(
(
)
=>
(
< li key = { c. id } className = " flex items-center gap-2 text-sm" >
< span
className = { cn (
"flex h-4 w-4 shrink-0 items-center justify-center rounded-md border" ,
c. done ? "border-accent bg-accent" : "border-line"
) }
>
{
.
done
&&
< Check className = " h-3 w-3 text-white" strokeWidth = { 3.5 } />
}
</ span>
< span className = { cn ( "truncate" , c. done ? "text-faint line-through" : "text-body" ) } >
{
.
text
}
</ span>
</ li>
)
)
}
{
.
checklist
.
length
>
3
&&
(
< li className = " num pl-6 text-[11px] font-semibold text-faint" >
ещё
{
.
checklist
.
length
-
3
}
…
</ li>
)
}
</ ul>
)
}
{
.
attachments
.
length
>
0
&&
(
< p className = " mt-2 flex items-center gap-1 text-[11px] font-semibold text-faint" >
< Paperclip className = " h-3 w-3" />
{
.
attachments
.
length
}
влож.
</ p>
)
}
< p className = " mt-2.5 flex items-center gap-2 text-[11px] font-medium text-faint/80" >
< span>
{
relativeLabel
(
.
updatedAt
)
}
</ span>
{
&&
< span>
·
{
.
label
}
</ span>
}
</ p>
</ div>
</ SwipeToDelete >
</ li>
)
;
}
)
}
</ ul>
)
}
{
}
< button
onClick = { ( ) => openFor ( null ) }
aria-label = " Создать заметку"
className = " fixed bottom-24 right-[max(1rem,calc(50%-14rem+1rem))] z-40 flex h-13 w-13 items-center justify-center rounded-full bg-accent text-white shadow-lg shadow-accent/30 transition-all hover:brightness-110 active:scale-90"
>
< Plus className = " h-6 w-6" strokeWidth = { 2.5 } />
</ button>
< NotePage note = { openNote} open = { pageOpen} onClose = { ( ) => setPageOpen ( false ) } />
</ div>
)
;
}
)
;
}
}
}