Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 15 additions & 5 deletions src/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ export function categoryLabel(category) {
return category === '' ? t('notes', 'Uncategorized') : category.replace(/\//g, ' / ')
}

export function rootCategory(category) {
if (!category) {
return ''
/**
* Whether a note's category falls within the selected one.
*
* A null selection matches every note. Otherwise a note matches the selected
* category itself and any category nested below it.
*
* @param {string} noteCategory the note's category
* @param {string|null} selectedCategory the selected category, or null for all notes
* @return {boolean} whether the note belongs to the selection
*/
export function isInCategory(noteCategory, selectedCategory) {
if (selectedCategory === null) {
return true
}
const separator = category.indexOf('/')
return separator === -1 ? category : category.substring(0, separator)
return noteCategory === selectedCategory
|| noteCategory.startsWith(selectedCategory + '/')
}

export function routeIsNewNote($route) {
Expand Down
4 changes: 2 additions & 2 deletions src/components/NotesView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import logger from '../Logger.js'
import { createNote } from '../NotesService.js'
import store from '../store.js'
import { fetchNoteTemplates, fetchTemplateContent } from '../TemplateService.js'
import { categoryLabel, rootCategory } from '../Util.js'
import { categoryLabel, isInCategory } from '../Util.js'

export default {
name: 'NotesView',
Expand Down Expand Up @@ -252,7 +252,7 @@ export default {
}

const selectedCategory = store.notes.getSelectedCategory()
if (selectedCategory !== null && selectedCategory !== rootCategory(this.note.category)) {
if (!isInCategory(this.note.category, selectedCategory)) {
this.showNote = false
}
},
Expand Down
30 changes: 22 additions & 8 deletions src/tests/Util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
getDefaultSampleNote,
getDefaultSampleNoteTitle,
getDraggedNoteId,
isInCategory,
isNoteDrag,
noteAttributes,
rootCategory,
routeIsNewNote,
} from '../Util.js'

Expand Down Expand Up @@ -81,17 +81,31 @@ describe('categoryLabel', () => {
})
})

describe('rootCategory', () => {
it('returns the first category segment', () => {
expect(rootCategory('a/b/c')).toBe('a')
describe('isInCategory', () => {
it('matches a note in the selected category itself', () => {
expect(isInCategory('Work', 'Work')).toBe(true)
})

it('leaves a plain category alone', () => {
expect(rootCategory('Recipes')).toBe('Recipes')
it('matches a note in a descendant of the selected category', () => {
expect(isInCategory('Work/Projects/2026', 'Work/Projects')).toBe(true)
})

it('matches a note whose nested category is selected exactly', () => {
expect(isInCategory('Personal/Work', 'Personal/Work')).toBe(true)
})

it('does not match a sibling category sharing a name prefix', () => {
expect(isInCategory('Workshop', 'Work')).toBe(false)
})

it('matches every note when no category is selected', () => {
expect(isInCategory('Work/Projects', null)).toBe(true)
expect(isInCategory('', null)).toBe(true)
})

it('leaves the empty category empty', () => {
expect(rootCategory('')).toBe('')
it('matches only uncategorized notes for the uncategorized selection', () => {
expect(isInCategory('', '')).toBe(true)
expect(isInCategory('Work', '')).toBe(false)
})
})

Expand Down