@@ -22,6 +22,7 @@ export function NoteSearch() {
2222
2323 const [ activeTag , setActiveTag ] = useState < string | null > ( null )
2424 const [ tagActionMenuIndex , setTagActionMenuIndex ] = useState ( 0 )
25+ const [ tagMenuPos , setTagMenuPos ] = useState ( { x : 0 , y : 0 } )
2526
2627 if ( ! showNoteSearch ) return null
2728
@@ -31,6 +32,14 @@ export function NoteSearch() {
3132 return isAuto ? n . content . split ( '\n' ) [ 0 ] . trim ( ) || 'New Note' : fileName
3233 }
3334
35+ const getNoteTags = ( n : Note ) : string [ ] => {
36+ const matches = n . content . match ( / ! [ a - z A - Z 0 - 9 _ - ] + / g)
37+ return matches || [ ]
38+ }
39+
40+ const tagMatch = ( tag : string ) => ( n : Note ) =>
41+ getNoteTags ( n ) . some ( ( t ) => t . toLowerCase ( ) === tag . toLowerCase ( ) )
42+
3443 const filteredNotes = notes . filter (
3544 ( n ) =>
3645 n . content . toLowerCase ( ) . includes ( noteSearchQuery . toLowerCase ( ) ) ||
@@ -40,10 +49,7 @@ export function NoteSearch() {
4049
4150 const allTags = new Set < string > ( )
4251 notes . forEach ( ( n ) => {
43- const matches = n . content . match ( / ! [ a - z A - Z 0 - 9 _ - ] + / g)
44- if ( matches ) {
45- matches . forEach ( ( m ) => allTags . add ( m . toLowerCase ( ) ) )
46- }
52+ getNoteTags ( n ) . forEach ( ( m ) => allTags . add ( m . toLowerCase ( ) ) )
4753 } )
4854 const tagArray = Array . from ( allTags ) . sort ( )
4955
@@ -71,11 +77,11 @@ export function NoteSearch() {
7177 e . preventDefault ( )
7278 const tag = activeTag
7379 setActiveTag ( null )
80+ if ( ! tag ) return
81+ const matchNotes = notes . filter ( tagMatch ( tag ) )
7482 if ( tagActionMenuIndex === 0 ) {
7583 const doDelete = async ( ) => {
76- const notesToDelete = notes . filter ( ( n ) =>
77- n . content . toLowerCase ( ) . includes ( tag . toLowerCase ( ) )
78- )
84+ const notesToDelete = matchNotes
7985 await window . electronAPI . setDialogOpen ( true )
8086 const confirmed = await confirm (
8187 `Delete ${ notesToDelete . length } notes containing tag ${ tag } ?` ,
@@ -101,9 +107,7 @@ export function NoteSearch() {
101107 doDelete ( )
102108 } else if ( tagActionMenuIndex === 1 ) {
103109 const doExport = async ( ) => {
104- const notesToExport = notes . filter ( ( n ) =>
105- n . content . toLowerCase ( ) . includes ( tag . toLowerCase ( ) )
106- )
110+ const notesToExport = matchNotes
107111 const combinedContent = notesToExport
108112 . map ( ( n ) => {
109113 const title = getNoteTitle ( n )
@@ -214,9 +218,9 @@ export function NoteSearch() {
214218 < div
215219 className = "tag-action-menu"
216220 style = { {
217- position : 'absolute ' ,
218- top : 50 ,
219- left : 16 ,
221+ position : 'fixed ' ,
222+ top : tagMenuPos . y ,
223+ left : tagMenuPos . x ,
220224 zIndex : 1000 ,
221225 background : 'var(--bg-color)' ,
222226 border : '1px solid var(--border-color)' ,
@@ -256,9 +260,8 @@ export function NoteSearch() {
256260 e . stopPropagation ( )
257261 const tag = activeTag
258262 setActiveTag ( null )
259- const notesToDelete = notes . filter ( ( n ) =>
260- n . content . toLowerCase ( ) . includes ( tag . toLowerCase ( ) )
261- )
263+ if ( ! tag ) return
264+ const notesToDelete = notes . filter ( tagMatch ( tag ) )
262265 await window . electronAPI . setDialogOpen ( true )
263266 const confirmed = await confirm (
264267 `Delete ${ notesToDelete . length } notes containing tag ${ tag } ?` ,
@@ -305,9 +308,8 @@ export function NoteSearch() {
305308 e . stopPropagation ( )
306309 const tag = activeTag
307310 setActiveTag ( null )
308- const notesToExport = notes . filter ( ( n ) =>
309- n . content . toLowerCase ( ) . includes ( tag . toLowerCase ( ) )
310- )
311+ if ( ! tag ) return
312+ const notesToExport = notes . filter ( tagMatch ( tag ) )
311313 const combinedContent = notesToExport
312314 . map ( ( n ) => {
313315 const title = getNoteTitle ( n )
@@ -361,6 +363,7 @@ export function NoteSearch() {
361363 e . stopPropagation ( )
362364 setActiveTag ( tag )
363365 setTagActionMenuIndex ( 0 )
366+ setTagMenuPos ( { x : e . clientX , y : e . clientY } )
364367 setShowNoteActionMenu ( false )
365368 } }
366369 >
0 commit comments