2023-08-21 16:01:20 +01:00
import * as React from 'react' ;
import { Size } from '@joplin/utils/types' ;
import { useEffect , useState } from 'react' ;
2023-09-18 17:40:36 +01:00
import { ItemFlow } from '@joplin/lib/services/plugins/api/noteListType' ;
2023-08-21 16:01:20 +01:00
2024-08-31 08:05:01 -07:00
const useItemElement = (
2025-01-17 11:52:52 +00:00
rootElement : HTMLDivElement , noteId : string , noteHtml : string , focusVisible : boolean , style : React.CSSProperties , itemSize : Size , onClick : React.MouseEventHandler < HTMLDivElement > , onDoubleClick : React.MouseEventHandler < HTMLDivElement > , flow : ItemFlow ,
2024-08-31 08:05:01 -07:00
) = > {
2023-08-21 16:01:20 +01:00
const [ itemElement , setItemElement ] = useState < HTMLDivElement > ( null ) ;
useEffect ( ( ) = > {
if ( ! rootElement ) return ( ) = > { } ;
const element = document . createElement ( 'div' ) ;
element . setAttribute ( 'data-id' , noteId ) ;
element . className = 'note-list-item' ;
for ( const [ n , v ] of Object . entries ( style ) ) {
2024-04-05 12:16:49 +01:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2023-08-21 16:01:20 +01:00
( element . style as any ) [ n ] = v ;
}
if ( flow === ItemFlow . LeftToRight ) element . style . width = ` ${ itemSize . width } px ` ;
element . style . height = ` ${ itemSize . height } px ` ;
element . innerHTML = noteHtml ;
2025-01-17 11:52:52 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- we're mixing React synthetic events with DOM events which ideally should not be done but it is fine in this particular case
2023-08-21 16:01:20 +01:00
element . addEventListener ( 'click' , onClick as any ) ;
2025-01-17 11:52:52 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- we're mixing React synthetic events with DOM events which ideally should not be done but it is fine in this particular case
element . addEventListener ( 'dblclick' , onDoubleClick as any ) ;
2023-08-21 16:01:20 +01:00
rootElement . appendChild ( element ) ;
setItemElement ( element ) ;
return ( ) = > {
element . remove ( ) ;
} ;
2025-01-17 11:52:52 +00:00
} , [ rootElement , itemSize , noteHtml , noteId , style , onClick , onDoubleClick , flow ] ) ;
2023-08-21 16:01:20 +01:00
2024-08-31 08:05:01 -07:00
useEffect ( ( ) = > {
if ( ! itemElement ) return ;
if ( focusVisible ) {
itemElement . classList . add ( '-focus-visible' ) ;
} else {
itemElement . classList . remove ( '-focus-visible' ) ;
}
} , [ focusVisible , itemElement ] ) ;
2023-08-21 16:01:20 +01:00
return itemElement ;
} ;
export default useItemElement ;