Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sorting the tags list #1042

Merged
merged 2 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion desktop/menus/view-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,18 @@ const buildViewMenu = settings => {
),
},
{
label: '&Theme',
label: '&Tags',
submenu: [
{
label: '&Sort Alphabetically',
type: 'checkbox',
checked: settings.sortTagsAlpha,
click: appCommandSender({ action: 'toggleSortTagsAlpha' }),
},
],
},
{
label: 'T&heme',
submenu: [
{
label: '&Light',
Expand Down
8 changes: 7 additions & 1 deletion lib/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,19 @@ const mapStateToProps = state => ({
isAuthorized: selectors.auth.isAuthorized(state),
});

function mapDispatchToProps(dispatch, { noteBucket }) {
function mapDispatchToProps(dispatch, { noteBucket, tagBucket }) {
const actionCreators = Object.assign({}, appState.actionCreators);

const thenReloadNotes = action => a => {
dispatch(action(a));
dispatch(actionCreators.loadNotes({ noteBucket }));
};

const thenReloadTags = action => a => {
dispatch(action(a));
dispatch(actionCreators.loadTags({ tagBucket }));
};

return {
actions: bindActionCreators(actionCreators, dispatch),
...bindActionCreators(
Expand All @@ -85,6 +90,7 @@ function mapDispatchToProps(dispatch, { noteBucket }) {
),
setSortType: thenReloadNotes(settingsActions.setSortType),
toggleSortOrder: thenReloadNotes(settingsActions.toggleSortOrder),
toggleSortTagsAlpha: thenReloadTags(settingsActions.toggleSortTagsAlpha),

openTagList: () => dispatch(actionCreators.toggleNavigation()),
resetAuth: () => dispatch(reduxActions.auth.reset()),
Expand Down
9 changes: 6 additions & 3 deletions lib/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ const client = simperium({
preferences: function(objectStore) {
console.log('Configure preferences', objectStore); // eslint-disable-line no-console
},
tag: function(objectStore) {
console.log('Configure tag', objectStore); // eslint-disable-line no-console
tag: {
configure: function(objectStore) {
// Create an index on the tag name
objectStore.createIndex('name', 'data.name');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to make the sorting case insensitive? The sort order was like CSS, SEO, printing and it confused me for a bit.

},
},
},
database: 'simplenote',
version: 41,
version: 42,
});

const l = msg => {
Expand Down
12 changes: 12 additions & 0 deletions lib/dialogs/settings/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export class SettingsDialog extends Component {
setNoteDisplay,
setSortType,
toggleSortOrder,
toggleSortTagsAlpha,
} = this.props;

const {
Expand All @@ -149,6 +150,7 @@ export class SettingsDialog extends Component {
noteDisplay,
sortType,
sortReversed: sortIsReversed,
sortTagsAlpha,
accountName,
},
} = this.props;
Expand Down Expand Up @@ -250,6 +252,16 @@ export class SettingsDialog extends Component {
<Item title="Reversed" slug="reversed" />
</SettingsGroup>

<SettingsGroup
title="Tags"
slug="sortTagsAlpha"
activeSlug={sortTagsAlpha ? 'alpha' : ''}
onChange={toggleSortTagsAlpha}
renderer={ToggleGroup}
>
<Item title="Sort Alphabetically" slug="alpha" />
</SettingsGroup>

<SettingsGroup
title="Theme"
slug="theme"
Expand Down
34 changes: 20 additions & 14 deletions lib/editable-list/index.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import SmallCrossOutlineIcon from '../icons/cross-outline-small';
Expand Down Expand Up @@ -64,7 +65,7 @@ export class EditableList extends Component {
};

render() {
const { editing, onRemove, onReorder } = this.props;
const { editing, onRemove, onReorder, sortTagsAlpha } = this.props;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add a propType check for sortTagsAlpha.

const { reorderedItems, reorderingId } = this.state;
const classes = classNames('editable-list', this.props.className, {
'editable-list-editing': this.props.editing,
Expand Down Expand Up @@ -104,18 +105,19 @@ export class EditableList extends Component {
</span>
</span>

{onReorder && (
<span
className="editable-list-reorder"
tabIndex={editing ? '0' : '-1'}
onDragStart={e => e.preventDefault()}
onMouseDown={this.onReorderStart.bind(this, itemId)}
onTouchStart={this.onReorderStart.bind(this, itemId)}
onKeyDown={this.onReorderKeyDown.bind(this, itemId)}
>
<ReorderIcon />
</span>
)}
{onReorder &&
!sortTagsAlpha && (
<span
className="editable-list-reorder"
tabIndex={editing ? '0' : '-1'}
onDragStart={e => e.preventDefault()}
onMouseDown={this.onReorderStart.bind(this, itemId)}
onTouchStart={this.onReorderStart.bind(this, itemId)}
onKeyDown={this.onReorderKeyDown.bind(this, itemId)}
>
<ReorderIcon />
</span>
)}
</li>
);
})}
Expand Down Expand Up @@ -301,4 +303,8 @@ export class EditableList extends Component {
};
}

export default EditableList;
const mapStateToProps = ({ settings: { sortTagsAlpha } }) => ({
sortTagsAlpha,
});

export default connect(mapStateToProps)(EditableList);
15 changes: 10 additions & 5 deletions lib/flux/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,29 +684,34 @@ export const actionMap = new ActionMap({

loadTags: {
creator({ tagBucket }) {
return dispatch => {
return (dispatch, getState) => {
const sortTagsAlpha = getState().settings.sortTagsAlpha;
tagBucket.query(db => {
var tags = [];
db
.transaction('tag')
.objectStore('tag')
.openCursor(null, 'prev').onsuccess = e => {
.index('name')
.openCursor().onsuccess = e => {
var cursor = e.target.result;
if (cursor) {
tags.push(cursor.value);
cursor.continue();
} else {
dispatch(this.action('tagsLoaded', { tags: tags }));
dispatch(this.action('tagsLoaded', { tags, sortTagsAlpha }));
}
};
});
};
},
},

tagsLoaded(state, { tags }) {
tagsLoaded(state, { tags, sortTagsAlpha }) {
tags = tags.slice();
tags.sort((a, b) => (a.data.index | 0) - (b.data.index | 0));
if (!sortTagsAlpha) {
// Sort the tags by their 'index' value
tags.sort((a, b) => (a.data.index | 0) - (b.data.index | 0));
}

return update(state, {
tags: { $set: tags },
Expand Down
11 changes: 11 additions & 0 deletions lib/state/settings/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ export const setSortType = sortType => ({
sortType,
});

export const setSortTagsAlpha = sortTagsAlpha => ({
type: 'setSortTagsAlpha',
sortTagsAlpha,
});

export const toggleSortTagsAlpha = () => (dispatch, getState) => {
const { settings: { sortTagsAlpha } } = getState();

dispatch(setSortTagsAlpha(!sortTagsAlpha));
};

export const setMarkdown = markdownEnabled => ({
type: 'setMarkdownEnabled',
markdownEnabled,
Expand Down
9 changes: 9 additions & 0 deletions lib/state/settings/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const sortReversed = (state = false, action) => {
return action.sortReversed;
};

const sortTagsAlpha = (state = false, action) => {
if ('setSortTagsAlpha' !== action.type) {
return state;
}

return action.sortTagsAlpha || false;
};

const theme = (state = 'light', action) => {
if ('setTheme' !== action.type) {
return state;
Expand Down Expand Up @@ -98,6 +106,7 @@ export default combineReducers({
noteDisplay,
sortType,
sortReversed,
sortTagsAlpha,
spellCheckEnabled,
theme,
wpToken,
Expand Down
4 changes: 2 additions & 2 deletions lib/tag-list/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export class TagList extends Component {
</div>
<EditableList
className="tag-list-items"
items={this.props.tags}
editing={this.props.editingTags}
items={tags}
editing={editingTags}
renderItem={this.renderItem}
onRemove={this.props.onTrashTag}
onReorder={this.props.onReorderTags}
Expand Down