TagSpaces is an offline, open source, document manager with tagging support
refactoring sorting functionality, added sorting by first tag, showing folder always first
Ilian Sapundshiev4499f0b43c3f
diff --git a/app/locales/en/core.json b/app/locales/en/core.jsonindex 9a0c40dd8..98fc96546 100644--- a/app/locales/en/core.json+++ b/app/locales/en/core.json@@ -251,6 +251,7 @@"fileSize": "Size","fileLDTM": "Date Modified","fileDirectory": "Directory",+ "fileFirstTag": "First Tag","filePath": "Path","fileName": "File Name","createTXTFile": "New Plain Text File",diff --git a/app/perspectives/grid-perspective/src/index.js b/app/perspectives/grid-perspective/src/index.jsindex 652953034..7c73281bd 100644--- a/app/perspectives/grid-perspective/src/index.js+++ b/app/perspectives/grid-perspective/src/index.js@@ -21,6 +21,7 @@ import React from 'react';import { connect } from 'react-redux';import { bindActionCreators } from 'redux';import removeMd from 'remove-markdown';+import memoize from "memoize-one";import { withStyles } from '@material-ui/core/styles';import classNames from 'classnames';import moment from 'moment';@@ -30,6 +31,7 @@ import Toolbar from '@material-ui/core/Toolbar';import MoreVertIcon from '@material-ui/icons/MoreVert';import RadioCheckedIcon from '@material-ui/icons/RadioButtonChecked';import RadioUncheckedIcon from '@material-ui/icons/RadioButtonUnchecked';+import ListSubHeader from '@material-ui/core/ListSubheader';import ListItemIcon from '@material-ui/core/ListItemIcon';import ListItemText from '@material-ui/core/ListItemText';import Grid from '@material-ui/core/Grid';@@ -64,6 +66,7 @@ import { extractTitle } from '../../../utils/paths';import {formatFileSize,formatDateTime,+ sortByCriteria,isObj,isVisibleOnScreen} from '../../../utils/misc';@@ -94,7 +97,6 @@ const settings = JSON.parse(localStorage.getItem('tsPerspectiveGrid')); // loaditype Props = {classes: Object,currentDirectoryPath?: string,- sortByCriteria: () => void,openFile: (path: string, isFile?: boolean) => void,deleteFile: (path: string) => void,deleteDirectory: (path: string) => void,@@ -237,8 +239,6 @@ class GridPerspective extends React.Component<Props, State> {// Clear selection on directory changethis.clearSelection();- this.props.sortByCriteria(this.state.sortBy, this.state.orderBy);-const grid = document.querySelector('[data-tid="perspectiveGridFileTable"]');@@ -250,6 +250,10 @@ class GridPerspective extends React.Component<Props, State> {}};+ sort = memoize(+ (data, criteria, order) => sortByCriteria(data, criteria, order)+ );+makeFirstSelectedEntryVisible = () => {const { selectedEntries } = this.state;if (selectedEntries && selectedEntries.length > 0) {@@ -292,7 +296,6 @@ class GridPerspective extends React.Component<Props, State> {handleSortBy = sortBy => {this.closeSortingMenu();- this.props.sortByCriteria(sortBy, !this.state.orderBy);this.setState({orderBy: !this.state.orderBy,sortBy@@ -814,8 +817,12 @@ class GridPerspective extends React.Component<Props, State> {render() {const classes = this.props.classes;- const { selectedEntries = [], layoutType, entrySize } = this.state;+ const { selectedEntries = [], layoutType, entrySize, sortBy, orderBy } = this.state;const selectedFilePaths = selectedEntries.filter(fsEntry => fsEntry.isFile).map(fsentry => fsentry.path);++ const sortedContent = this.sort(this.props.directoryContent, sortBy, orderBy);+ const sortedDirectories = sortedContent.filter(entry => !entry.isFile);+ const sortedFiles = sortedContent.filter(entry => entry.isFile);let entryWidth = 200;if (entrySize === 'small') {entryWidth = 150;@@ -938,9 +945,9 @@ class GridPerspective extends React.Component<Props, State> {}}data-tid="perspectiveGridFileTable">- {this.props.directoryContent.length > 0 ? (- this.props.directoryContent.map(entry => this.renderCell(entry))- ) : (+ {(sortedDirectories.map(entry => this.renderCell(entry)))}+ {(sortedFiles.map(entry => this.renderCell(entry)))}+ {(sortedFiles.length < 1 && sortedDirectories.length < 1) && (<Typography style={{ padding: 15 }}>{i18n.t('core:noFileFolderFound')}</Typography>@@ -1024,6 +1031,7 @@ class GridPerspective extends React.Component<Props, State> {onClose={this.closeSortingMenu}anchorEl={this.state.sortingContextMenuAnchorEl}>+ {/* <ListSubHeader>Sort by</ListSubHeader> */}<MenuItemdata-tid="gridPerspectiveSortByName"onClick={() => { this.handleSortBy('byName'); }}@@ -1057,6 +1065,17 @@ class GridPerspective extends React.Component<Props, State> {</ListItemIcon><ListItemText inset primary={i18n.t('core:fileLDTM')} /></MenuItem>+ <MenuItem+ data-tid="gridPerspectiveSortByFirstTag"+ onClick={() => { this.handleSortBy('byFirstTag'); }}+ >+ <ListItemIcon style={{ minWidth: 25 }}>+ {(this.state.sortBy === 'byFirstTag') && (+ this.state.orderBy ? <ArrowUpIcon /> : <ArrowDownIcon />+ )}+ </ListItemIcon>+ <ListItemText inset primary={i18n.t('core:fileFirstTag')} />+ </MenuItem><MenuItemdata-tid="gridPerspectiveSortByExt"onClick={() => { this.handleSortBy('byExtension'); }}@@ -1162,7 +1181,6 @@ class GridPerspective extends React.Component<Props, State> {function mapActionCreatorsToProps(dispatch) {return bindActionCreators({moveFiles: IOActions.moveFiles,- sortByCriteria: AppActions.sortByCriteria}, dispatch);}diff --git a/app/reducers/app.js b/app/reducers/app.jsindex 261fe8c72..3fe373c31 100644--- a/app/reducers/app.js+++ b/app/reducers/app.js@@ -36,7 +36,7 @@ import {extractParentDirectoryPath,extractTagsAsObjects, normalizePath} from '../utils/paths';-import { sortByCriteria, formatDateTime4Tag } from '../utils/misc';+import { formatDateTime4Tag } from '../utils/misc';import i18n from '../services/i18n';import { Pro } from '../pro';import { getThumbnailURLPromise } from '../services/thumbsgenerator';@@ -66,7 +66,6 @@ export const types = {SET_FILEDRAGGED: 'APP/SET_FILEDRAGGED',SET_READONLYMODE: 'APP/SET_READONLYMODE',RENAME_FILE: 'APP/RENAME_FILE',- SORT_BY: 'APP/SORT_BY',TOGGLE_ABOUT_DIALOG: 'APP/TOGGLE_ABOUT_DIALOG',TOGGLE_KEYBOARD_DIALOG: 'APP/TOGGLE_KEYBOARD_DIALOG',TOGGLE_LICENSE_DIALOG: 'APP/TOGGLE_LICENSE_DIALOG',@@ -158,7 +157,8 @@ export default (state: Object = initialState, action: Object) => {return {...state,currentDirectoryEntries: action.directoryContent,- currentDirectoryPath: action.directoryPath+ currentDirectoryPath: action.directoryPath,+ isLoading: false};}case types.CLEAR_DIRECTORY_CONTENT: {@@ -205,21 +205,6 @@ export default (state: Object = initialState, action: Object) => {case types.TOGGLE_SELECT_DIRECTORY_DIALOG: {return { ...state, selectDirectoryDialogOpened: !state.selectDirectoryDialogOpened };}- case types.SORT_BY: {- const data = sortByCriteria(- action.sort,- state.currentDirectoryEntries,- action.order- );- return {- ...state,- currentDirectoryEntries: data,- isLoading: false- };- }- /* case types.SET_FILEDRAGGED: {- return { ...state, isFileDragged: action.isFileDragged };- } */case types.INDEX_DIRECTORY_SEARCH: {return {...state,@@ -1003,11 +988,6 @@ export const actions = {});return prevFilePath;},- sortByCriteria: (sort: string, order: boolean) => ({- type: types.SORT_BY,- sort,- order- }),closeAllFiles: () => ({ type: types.CLOSE_ALL_FILES }),reflectDeleteEntryInt: (path: string) => ({type: types.REFLECT_DELETE_ENTRY,@@ -1239,9 +1219,6 @@ function prepareDirectoryContent(.catch(handleTmbGenerationFailed);}- // directoryContent.sort((a, b) => (a.name < b.name ? -1 : 1));- // TODO check if here the right place for sorting- sortByCriteria('byName', directoryContent, true); // ability to sort by metadata criteriaconsole.log('Dir ' + directoryPath + ' contains ' + directoryContent.length);console.timeEnd('listDirectoryPromise');dispatch(actions.loadDirectorySuccess(directoryPath, directoryContent));diff --git a/app/utils/misc.js b/app/utils/misc.jsindex 12174cb0e..116cfd9c3 100644--- a/app/utils/misc.js+++ b/app/utils/misc.js@@ -343,30 +343,35 @@ export function sortByName(a, b) {return (!b.isFile - !a.isFile) || sortAlphaNum(a, b);}-export function sortByIsDirectory(a, b) {- if (b.isFile && a.isFile) {- return 0;- }- return a.isFile && !b.isFile ? -1 : 1;-}-export function sortBySize(a, b) {- return (!b.isFile - !a.isFile) || (a.size - b.size);+ return a.size - b.size;}export function sortByDateModified(a, b) {- return (!b.isFile - !a.isFile) || (a.lmdt - b.lmdt);+ return a.lmdt - b.lmdt;}export function sortByExtension(a, b) {- return (!b.isFile - !a.isFile) || (a.extension.toString().localeCompare(b.extension));+ return a.extension.toString().localeCompare(b.extension);}-export function sortByTags(a, b) {- return (!b.isFile - !a.isFile) || (a.tags.toString().localeCompare(b.tags));+export function sortByFirstTag(a, b) {+ if (+ (!a.tags && !b.tags) ||+ (a.tags.length < 1 && b.tags.length < 1)+ ) {+ return 0;+ }+ if (!a.tags || a.tags.length < 1) {+ return -1;+ }+ if (!b.tags || b.tags.length < 1) {+ return 1;+ }+ return a.tags[0].title.localeCompare(b.tags[0].title);}-export function sortByCriteria(criteria, data, order) {+export function sortByCriteria(data, criteria, order) {switch (criteria) {case 'byName':if (order) {@@ -388,16 +393,11 @@ export function sortByCriteria(criteria, data, order) {return data.sort(sortByExtension);}return data.sort((a, b) => -1 * sortByExtension(a, b));- case 'byTags':- if (order) {- return data.sort(sortByTags);- }- return data.sort((a, b) => -1 * sortByTags(a, b));- case 'byDirectory':+ case 'byFirstTag':if (order) {- return data.sort(sortByIsDirectory);+ return data.sort(sortByFirstTag);}- return data.sort((a, b) => -1 * sortByIsDirectory(a, b));+ return data.sort((a, b) => -1 * sortByFirstTag(a, b));default:return data.sort(sortByName);}