TagSpaces is an offline, open source, document manager with tagging support
feat: optimizations in gridpagination, making description preview stick to the top
Ilian Sapundshiev44f26fecd7bc
diff --git a/src/renderer/perspectives/grid/components/GridPagination.tsx b/src/renderer/perspectives/grid/components/GridPagination.tsxindex 99beb8fd3..819310ae4 100644--- a/src/renderer/perspectives/grid/components/GridPagination.tsx+++ b/src/renderer/perspectives/grid/components/GridPagination.tsx@@ -57,6 +57,16 @@ import React, {import { useTranslation } from 'react-i18next';import GridCellsContainer from './GridCellsContainer';+// Utility: Check if Two Rectangles Intersect+function rectIntersects(r1: DOMRect, r2: DOMRect): boolean {+ return !(+ r2.left > r1.right ||+ r2.right < r1.left ||+ r2.top > r1.bottom ||+ r2.bottom < r1.top+ );+}+interface Props {desktopMode: boolean;getCellContent: (@@ -151,35 +161,28 @@ function GridPagination(props: Props) {[currentDirectoryPath],);- const dirColor = directoryMeta?.color || 'transparent';+ const dirColor = useMemo(+ () => directoryMeta?.color || 'transparent',+ [directoryMeta?.color],+ );- // Separate files and directories- const { files, dirs } = useMemo(() => {- const files: TS.FileSystemEntry[] = [];- const dirs: TS.FileSystemEntry[] = [];+ // Count files and directories without allocating arrays+ const { fileCount, dirCount } = useMemo(() => {+ let fileCount = 0;+ let dirCount = 0;for (const entry of sortedDirContent) {- if (entry.isFile) files.push(entry);- else dirs.push(entry);+ if (entry.isFile) fileCount++;+ else dirCount++;}- return { files, dirs };+ return { fileCount, dirCount };}, [sortedDirContent]);const folderSummary = useMemo(() => {if (selectedEntries && selectedEntries.length > 0) {return `${selectedEntries.length} entries selected`;}- return `${dirs.length > 0 ? `${dirs.length} folder(s) and ` : ''}${files.length} file(s) found`;- }, [selectedEntries, dirs.length, files.length]);-- // Utility: Check if Two Rectangles Intersect- const rectIntersects = (r1: DOMRect, r2: DOMRect): boolean => {- return !(- r2.left > r1.right ||- r2.right < r1.left ||- r2.top > r1.bottom ||- r2.bottom < r1.top- );- };+ return `${dirCount > 0 ? `${dirCount} folder(s) and ` : ''}${fileCount} file(s) found`;+ }, [selectedEntries, dirCount, fileCount]);// Called when a selection drag ends. We look for any item elements whose// bounding rectangle intersects the drag rectangle.@@ -206,165 +209,195 @@ function GridPagination(props: Props) {[pageFiles, setSelectedEntries],);- // Render folder details and description- const renderFolderDetails = () => (- <Grid size={12} sx={{ position: 'relative' }}>- <Box- sx={{- display: 'flex',- alignItems: 'center',- overflow: 'auto',- padding: '10px',- marginRight: '160px',- width: 'fit-content',- borderRadius: 8,- color: theme.palette.text.primary,- }}- >- <TooltipTS title={t('core:renameDirectory')}>- <ButtonBase- data-tid={`currentDir_${dataTidFormat(folderName)}`}- sx={{- fontSize: '1.5rem',- filter: `drop-shadow(0px 0px 4px ${theme.palette.background.default})`,- }}- onClick={() => {- setSelectedEntries([]);- openRenameEntryDialog();- }}- >- {folderName}- </ButtonBase>- </TooltipTS>- {showTags ? (- <Box sx={{ paddingLeft: '5px' }}>- {directoryMeta?.tags?.map((tag) => (- <TagContainer key={tag.title} tag={tag} tagMode="display" />- ))}- </Box>- ) : (- <TagsPreview showFirstTag tags={directoryMeta?.tags} />- )}- </Box>- <Box- data-tid={`allFilesCount${files.length}`}- sx={{- paddingBottom: '5px',- marginTop: '10px',- marginRight: '160px',- padding: '10px',- borderRadius: '10px',- width: 'fit-content',- color: theme.palette.text.primary,- }}- >- <Typography+ // Memoized folder details+ const folderDetails = useMemo(+ () => (+ <Grid size={12} sx={{ position: 'relative' }}>+ <Boxsx={{- fontSize: '0.9rem',- filter: `drop-shadow(0px 0px 4px ${theme.palette.background.default})`,+ display: 'flex',+ alignItems: 'center',+ overflow: 'auto',+ padding: '10px',+ marginRight: '160px',+ width: 'fit-content',+ borderRadius: 8,+ color: theme.palette.text.primary,+ }}+ >+ <TooltipTS title={t('core:renameDirectory')}>+ <ButtonBase+ data-tid={`currentDir_${dataTidFormat(folderName)}`}+ sx={{+ fontSize: '1.5rem',+ filter: `drop-shadow(0px 0px 4px ${theme.palette.background.default})`,+ }}+ onClick={() => {+ setSelectedEntries([]);+ openRenameEntryDialog();+ }}+ >+ {folderName}+ </ButtonBase>+ </TooltipTS>+ {showTags ? (+ <Box sx={{ paddingLeft: '5px' }}>+ {directoryMeta?.tags?.map((tag) => (+ <TagContainer key={tag.title} tag={tag} tagMode="display" />+ ))}+ </Box>+ ) : (+ <TagsPreview showFirstTag tags={directoryMeta?.tags} />+ )}+ </Box>+ <Box+ data-tid={`allFilesCount${fileCount}`}+ sx={{+ paddingBottom: '5px',+ marginTop: '10px',+ marginRight: '160px',+ padding: '10px',+ borderRadius: '10px',+ width: 'fit-content',+ color: theme.palette.text.primary,}}>- {folderSummary}- </Typography>- {!showDescription && directoryMeta?.description && (<Typographysx={{- fontSize: '0.8rem',- wordBreak: 'break-all',- filter: `drop-shadow(0px 0px 2px ${theme.palette.background.default})`,- height: 45,- overflowY: 'auto',+ fontSize: '0.9rem',+ filter: `drop-shadow(0px 0px 4px ${theme.palette.background.default})`,}}>- {getDescriptionPreview(directoryMeta.description, 200)}+ {folderSummary}</Typography>- )}- </Box>- <TooltipTS title={t('core:changeThumbnail')} placement="bottom">- <Box+ {!showDescription && directoryMeta?.description && (+ <Typography+ sx={{+ fontSize: '0.8rem',+ wordBreak: 'break-all',+ filter: `drop-shadow(0px 0px 2px ${theme.palette.background.default})`,+ height: 45,+ overflowY: 'auto',+ }}+ >+ {getDescriptionPreview(directoryMeta.description, 200)}+ </Typography>+ )}+ </Box>+ <TooltipTS title={t('core:changeThumbnail')} placement="bottom">+ <Box+ sx={{+ ':hover': { border: '1px dashed gray !important' },+ backgroundImage: thumbImage,+ borderRadius: AppConfig.defaultCSSRadius,+ border: '1px solid transparent',+ height: 100,+ width: 100,+ backgroundSize: 'cover',+ backgroundRepeat: 'no-repeat',+ backgroundPosition: 'center center',+ position: 'absolute',+ top: 0,+ right: 7,+ cursor: 'pointer',+ }}+ data-tid="folderThumbTID"+ onClick={() => {+ if (Pro) {+ getAllPropertiesPromise(currentDirectoryPath).then(+ (fsEntry: TS.FileSystemEntry) =>+ thumbDialogContext.openThumbsDialog(fsEntry),+ );+ }+ }}+ />+ </TooltipTS>+ </Grid>+ ),+ [+ folderName,+ folderSummary,+ showTags,+ showDescription,+ directoryMeta?.tags,+ directoryMeta?.description,+ thumbImage,+ fileCount,+ theme.palette.text.primary,+ theme.palette.background.default,+ currentDirectoryPath,+ ],+ );++ // Memoized folder description (MilkdownProvider is expensive to mount)+ const folderDescription = useMemo(+ () =>+ showDescription &&+ directoryMeta?.description && (+ <Grid+ title={`${t('core:folderDescription')} - ${t('core:doubleClickToEdit')}`}+ size={12}+ className="gridPagination"sx={{- ':hover': { border: '1px dashed gray !important' },- backgroundImage: thumbImage,+ backgroundColor: theme.palette.background.default,+ borderBottom: `1px solid ${theme.palette.divider}`,+ marginTop: '10px',+ marginLeft: '8px',+ marginRight: '8px',+ marginBottom: '5px',borderRadius: AppConfig.defaultCSSRadius,- border: '1px solid transparent',- height: 100,- width: 100,- backgroundSize: 'cover',- backgroundRepeat: 'no-repeat',- backgroundPosition: 'center center',- position: 'absolute',- top: 0,- right: 7,- cursor: 'pointer',- }}- data-tid="folderThumbTID"- onClick={() => {- if (Pro) {- getAllPropertiesPromise(currentDirectoryPath).then(- (fsEntry: TS.FileSystemEntry) =>- thumbDialogContext.openThumbsDialog(fsEntry),- );- }}}- />- </TooltipTS>- </Grid>+ onDoubleClick={() =>+ openEntry(currentDirectoryPath, TabNames.descriptionTab)+ }+ >+ <style>+ {`+ .gridPagination .milkdown .ProseMirror h1 { margin-top: 10px; }+ .gridPagination .milkdown { border-radius: ${AppConfig.defaultCSSRadius}; }+ .gridPagination .milkdown .ProseMirror a { color: ${theme.palette.primary.main}; }+ `}+ </style>+ <MilkdownProvider>+ <LightMdEditor+ defaultContent={directoryMeta.description}+ placeholder=""+ />+ </MilkdownProvider>+ </Grid>+ ),+ [+ showDescription,+ directoryMeta?.description,+ currentDirectoryPath,+ theme.palette.background.default,+ theme.palette.divider,+ theme.palette.primary.main,+ ],);- // Render folder description in markdown editor- const renderFolderDescription = () =>- showDescription &&- directoryMeta?.description && (- <Grid- title={`${t('core:folderDescription')} - ${t('core:doubleClickToEdit')}`}- size={12}- className="gridPagination"- sx={{- backgroundColor: theme.palette.background.default,- borderBottom: `1px solid ${theme.palette.divider}`,- marginTop: '10px',- marginLeft: '8px',- marginRight: '8px',- marginBottom: '5px',- borderRadius: AppConfig.defaultCSSRadius,- }}- onDoubleClick={() =>- openEntry(currentDirectoryPath, TabNames.descriptionTab)- }- >- <style>- {`- .gridPagination .milkdown .ProseMirror h1 { margin-top: 10px; }- .gridPagination .milkdown { border-radius: ${AppConfig.defaultCSSRadius}; }- .gridPagination .milkdown .ProseMirror a { color: ${theme.palette.primary.main}; }- `}- </style>- <MilkdownProvider>- <LightMdEditor- defaultContent={directoryMeta.description}- placeholder=""- />- </MilkdownProvider>- </Grid>- );-// Render empty state- const renderEmptyState = (message: string) => (- <Box sx={{ textAlign: 'center' }}>- {!showDescription && directoryMeta?.description && (- <Box sx={{ position: 'relative', mx: 'auto', maxWidth: 150 }}>- <EntryIcon isFile={false} />- </Box>- )}- <Typography sx={{ padding: '15px', color: theme.palette.text.secondary }}>- {message}- </Typography>- {!AppConfig.isCordova && (- <Typography sx={{ color: theme.palette.text.secondary }}>- {t('core:dragAndDropToImport')}+ const renderEmptyState = useCallback(+ (message: string) => (+ <Box sx={{ textAlign: 'center' }}>+ {!showDescription && directoryMeta?.description && (+ <Box sx={{ position: 'relative', mx: 'auto', maxWidth: 150 }}>+ <EntryIcon isFile={false} />+ </Box>+ )}+ <Typography+ sx={{ padding: '15px', color: theme.palette.text.secondary }}+ >+ {message}</Typography>- )}- </Box>+ {!AppConfig.isCordova && (+ <Typography sx={{ color: theme.palette.text.secondary }}>+ {t('core:dragAndDropToImport')}+ </Typography>+ )}+ </Box>+ ),+ [showDescription, directoryMeta?.description, theme.palette.text.secondary],);return (@@ -384,76 +417,83 @@ function GridPagination(props: Props) {sx={{backgroundImage: backgroundImage,height: '100%',- overflowY: 'auto',+ display: 'flex',+ flexDirection: 'column',overflowX: 'hidden',backgroundSize: 'cover',backgroundRepeat: 'no-repeat',}}>- <Grid container spacing={0}>- <Grid size={12} sx={{ height: 70 }} />- {showDetails && renderFolderDetails()}- {renderFolderDescription()}- </Grid>- <SelectionDragLayer />- <SelectionArea onSelect={handleSelect}>- <GridCellsContainer>- {pageFiles.length < 1 &&- renderEmptyState(t('core:noFileFolderFound'))}- {files.length < 1 &&- dirs.length >= 1 &&- !showDirectories &&- renderEmptyState(t('core:noFileButFoldersFound'))}- {pageFiles.map((entry, index) => (- <CellView- key={entry.uuid}- fsEntry={entry}- index={index}- cellContent={getCellContent}- isLast={index === pageFiles.length - 1}- />- ))}- {pageFiles.length > 0 && (- <>- <Box sx={{ flexGrow: 1 }} />- <Box sx={{ flexGrow: 1 }} />- </>+ <Box sx={{ flexShrink: 0 }}>+ <Grid container spacing={0}>+ <Grid size={12} sx={{ height: 70 }} />+ {showDetails && folderDetails}+ </Grid>+ </Box>+ <Box sx={{ flexGrow: 1, overflowY: 'auto', overflowX: 'hidden' }}>+ <Grid container spacing={0}>+ {folderDescription}+ </Grid>+ <SelectionDragLayer />+ <SelectionArea onSelect={handleSelect}>+ <GridCellsContainer>+ {pageFiles.length < 1 &&+ renderEmptyState(t('core:noFileFolderFound'))}+ {fileCount < 1 &&+ dirCount >= 1 &&+ !showDirectories &&+ renderEmptyState(t('core:noFileButFoldersFound'))}+ {pageFiles.map((entry, index) => (+ <CellView+ key={entry.uuid}+ fsEntry={entry}+ index={index}+ cellContent={getCellContent}+ isLast={index === pageFiles.length - 1}+ />+ ))}+ {pageFiles.length > 0 && (+ <>+ <Box sx={{ flexGrow: 1 }} />+ <Box sx={{ flexGrow: 1 }} />+ </>+ )}+ </GridCellsContainer>+ {showPagination && (+ <TooltipTS title={folderSummary}>+ <Pagination+ sx={{+ left: 15,+ bottom: -35,+ zIndex: 1100,+ position: 'absolute',+ backgroundColor: theme.palette.background.default,+ border: '1px solid ' + theme.palette.divider,+ opacity: 0.97,+ padding: '2px',+ borderRadius: AppConfig.defaultCSSRadius,+ }}+ count={paginationCount}+ page={page}+ onChange={handleChange}+ />+ </TooltipTS>)}- </GridCellsContainer>- {showPagination && (- <TooltipTS title={folderSummary}>- <Pagination- sx={{- left: 15,- bottom: -35,- zIndex: 1100,- position: 'absolute',- backgroundColor: theme.palette.background.default,- border: '1px solid ' + theme.palette.divider,- opacity: 0.97,- padding: '2px',- borderRadius: AppConfig.defaultCSSRadius,- }}- count={paginationCount}- page={page}- onChange={handleChange}- />- </TooltipTS>- )}- {!showDetails && !showPagination && pageFiles.length > 0 && (- <Box sx={{ padding: '15px', bottom: '10px' }}>- <Typography- sx={{- fontSize: '0.8rem',- filter: `drop-shadow(0px 0px 4px ${theme.palette.background.default})`,- color: theme.palette.text.primary,- }}- >- {folderSummary}- </Typography>- </Box>- )}- </SelectionArea>+ {!showDetails && !showPagination && pageFiles.length > 0 && (+ <Box sx={{ padding: '15px', bottom: '10px' }}>+ <Typography+ sx={{+ fontSize: '0.8rem',+ filter: `drop-shadow(0px 0px 4px ${theme.palette.background.default})`,+ color: theme.palette.text.primary,+ }}+ >+ {folderSummary}+ </Typography>+ </Box>+ )}+ </SelectionArea>+ </Box></Box></Box>);