Skip to content

Commit

Permalink
Fix: Add selected tag to new notes by default (#2556)
Browse files Browse the repository at this point in the history
If you are on a selected tag and choose to add a new note, the new note will now automatically get that tag added to it.
  • Loading branch information
codebykat authored Apr 8, 2021
1 parent 39ee292 commit 21bd75e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,14 @@ export const middleware: S.Middleware = (store) => {
}

case 'CREATE_NOTE_WITH_ID':
searchState.collection = { type: 'all' };
if (action.note?.tags && action.note?.tags.length > 0) {
searchState.collection = {
type: 'tag',
tagName: action.note.tags[0],
};
} else {
searchState.collection = { type: 'all' };
}
searchState.notes.set(action.noteId, toSearchNote(action.note ?? {}));
indexNote(action.noteId);
queueSearch();
Expand Down
10 changes: 8 additions & 2 deletions lib/state/data/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { v4 as uuid } from 'uuid';

import { tagHashOf } from '../../utils/tag-hash';
import exportZipArchive from '../../utils/export';
import { withTag } from '../../utils/tag-hash';

import type * as A from '../action-types';
import type * as S from '../';
import type * as T from '../../types';
import { numberOfNonEmailTags } from '../selectors';
import { numberOfNonEmailTags, openedTag } from '../selectors';

export const middleware: S.Middleware = (store) => (
next: (action: A.ActionType) => A.ActionType
Expand All @@ -30,10 +31,15 @@ export const middleware: S.Middleware = (store) => (
systemTags.splice(markdownInNote, 1);
}

// apply selected tag by default
const selectedTag = openedTag(state);
const givenTags = action.note?.tags ?? [];
const tags = selectedTag ? withTag(givenTags, selectedTag) : givenTags;

return next({
type: 'CREATE_NOTE_WITH_ID',
noteId,
note: { ...action.note, systemTags },
note: { ...action.note, systemTags, tags },
meta: {
nextNoteToOpen: noteId,
},
Expand Down
2 changes: 2 additions & 0 deletions lib/state/ui/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ const collection: A.Reducer<T.Collection> = (
case 'CREATE_NOTE_WITH_ID': {
if (state.type === 'trash') {
return { type: 'all' };
} else if (state.type === 'tag') {
return { type: 'tag', tagName: state.tagName };
}
return state;
}
Expand Down

0 comments on commit 21bd75e

Please sign in to comment.