diff --git a/package.json b/package.json index 0dc4c3c..63f6938 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "snipcommand", - "version": "0.1.4dev001", + "version": "0.1.4dev002", "description": "A free and open source command snippets manager for organize and copy fast.", "private": true, "main": "public/electron.js", diff --git a/src/components/SettingsModal/index.js b/src/components/SettingsModal/index.js index e0f3eaf..bde5fe6 100644 --- a/src/components/SettingsModal/index.js +++ b/src/components/SettingsModal/index.js @@ -33,7 +33,8 @@ class SettingsModal extends React.Component { const autoClose = StorageHelpers.preference.get('autoClose') || false; const lineClamp = StorageHelpers.preference.get('lineClamp') || false; const showCommandInList = StorageHelpers.preference.get('showCommandInList') || false; - this.setState({dbDirectory, backupDirectory, appTheme, onClickAction, autoClose, lineClamp, showCommandInList}); + const sortBy = StorageHelpers.preference.get('sortBy') || ''; + this.setState({dbDirectory, backupDirectory, appTheme, onClickAction, autoClose, lineClamp, showCommandInList, sortBy}); this.listBackupFiles(); } @@ -140,14 +141,17 @@ class SettingsModal extends React.Component { } changeShowCommandInList = showCommandInList => { - const {setCommandList} = this.props; StorageHelpers.preference.set('showCommandInList', showCommandInList); this.setState({showCommandInList}); - setCommandList(); + } + + changeSortBy = sortBy => { + StorageHelpers.preference.set('sortBy', sortBy); + this.setState({sortBy}); } render() { - const {dbDirectory, backupDirectory, backupFiles, appTheme, onClickAction, autoClose, lineClamp, showCommandInList} = this.state; + const {dbDirectory, backupDirectory, backupFiles, appTheme, onClickAction, autoClose, lineClamp, showCommandInList, sortBy} = this.state; const {show, onClose, selectedTab} = this.props; return ( @@ -364,7 +368,20 @@ class SettingsModal extends React.Component { />
Limit the contents of a code block displayed in the commands list.
- + +
+
Sort by:
+
diff --git a/src/core/Api.js b/src/core/Api.js index c07f8ad..a669886 100644 --- a/src/core/Api.js +++ b/src/core/Api.js @@ -8,9 +8,10 @@ import {App} from "./Constants"; let db; class Api { - constructor() { + constructor(sortBy) { const dbFilePath = path.join(StorageHelpers.preference.get('storagePath'), App.dbName); const adapter = new FileSync(dbFilePath); + this.sortBy = StorageHelpers.preference.get('sortBy'); db = lowdb(adapter); db.defaults({commands: []}).write(); } @@ -23,22 +24,22 @@ class Api { getCommandById = id => db.get('commands').find({id}).value(); - getAllCommands = () => db.get('commands').filter({isTrash: false}).value(); + getAllCommands = () => db.get('commands').filter({isTrash: false}).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value(); - getAllCommandsInTrash = () => db.get('commands').filter({isTrash: true}).value(); + getAllCommandsInTrash = () => db.get('commands').filter({isTrash: true}).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value(); getAllUntaggedCommands = () => db.get('commands').filter({tags: "", isTrash: false} || { tags: null, isTrash: false - }).value(); + }).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value(); - getAllFavouriteCommands = () => db.get('commands').filter({isFavourite: true, isTrash: false}).value(); + getAllFavouriteCommands = () => db.get('commands').filter({isFavourite: true, isTrash: false}).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value(); getAllTags = () => db.get('commands').filter({isTrash: false}).map('tags').value(); - getCommandsContainsTag = tag => db.get('commands').filter((t => t.tags.indexOf(tag) > -1 && t.isTrash === false)).value(); + getCommandsContainsTag = tag => db.get('commands').filter((t => t.tags.indexOf(tag) > -1 && t.isTrash === false)).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value(); - queryCommand = query => db.get('commands').filter((t => (t.title.toLowerCase().indexOf(query) > -1 || t.command.toLowerCase().indexOf(query) > -1) && t.isTrash === false)).value(); + queryCommand = query => db.get('commands').filter((t => (t.title.toLowerCase().indexOf(query) > -1 || t.command.toLowerCase().indexOf(query) > -1) && t.isTrash === false)).sortBy(this.sortBy === 'title' ? item => item.title.toLowerCase() : '').value(); } export default Api; \ No newline at end of file