From e0ab938ccf995dd3d33ff43d86954a8ea9bec7d3 Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Thu, 4 Mar 2021 12:22:14 -0600 Subject: [PATCH] feat: notes display criteria (#530) * feat: notes display criteria * chore: remove unused code * chore: update names * chore: update snjs version --- app/assets/javascripts/ui_models/app_state.ts | 2 +- .../javascripts/views/editor/editor_view.ts | 4 +- .../javascripts/views/notes/note_utils.ts | 69 ------------------- .../javascripts/views/notes/notes_view.ts | 37 +++++----- .../javascripts/views/tags/tags_view.ts | 2 +- package.json | 2 +- yarn.lock | 8 +-- 7 files changed, 27 insertions(+), 97 deletions(-) delete mode 100644 app/assets/javascripts/views/notes/note_utils.ts diff --git a/app/assets/javascripts/ui_models/app_state.ts b/app/assets/javascripts/ui_models/app_state.ts index 8748bb593..b9db38c5a 100644 --- a/app/assets/javascripts/ui_models/app_state.ts +++ b/app/assets/javascripts/ui_models/app_state.ts @@ -259,7 +259,7 @@ export class AppState { async createEditor(title?: string) { const activeEditor = this.getActiveEditor(); const activeTagUuid = this.selectedTag - ? this.selectedTag.isSmartTag() + ? this.selectedTag.isSmartTag ? undefined : this.selectedTag.uuid : undefined; diff --git a/app/assets/javascripts/views/editor/editor_view.ts b/app/assets/javascripts/views/editor/editor_view.ts index f5ed13f1a..5f36183d7 100644 --- a/app/assets/javascripts/views/editor/editor_view.ts +++ b/app/assets/javascripts/views/editor/editor_view.ts @@ -460,7 +460,7 @@ class EditorViewCtrl extends PureViewCtrl { await this.editor.insertTemplatedNote(); } const selectedTag = this.appState.selectedTag; - if (!selectedTag?.isSmartTag() && !selectedTag?.hasRelationshipWithItem(note)) { + if (!selectedTag?.isSmartTag && !selectedTag?.hasRelationshipWithItem(note)) { await this.application.changeItem( selectedTag!.uuid, (mutator) => { @@ -1052,7 +1052,7 @@ class EditorViewCtrl extends PureViewCtrl { if (savedUuid === this.note.uuid) { const selectedTag = this.appState.selectedTag; if ( - !selectedTag?.isSmartTag() && + !selectedTag?.isSmartTag && !selectedTag?.hasRelationshipWithItem(this.note) ) { this.application.changeAndSaveItem( diff --git a/app/assets/javascripts/views/notes/note_utils.ts b/app/assets/javascripts/views/notes/note_utils.ts deleted file mode 100644 index 6f8265982..000000000 --- a/app/assets/javascripts/views/notes/note_utils.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { SNNote } from '@standardnotes/snjs'; - -export function notePassesFilter( - note: SNNote, - showArchived: boolean, - hidePinned: boolean, - filterText: string -): boolean { - const canShowArchived = showArchived; - const canShowPinned = !hidePinned; - if ((note.archived && !canShowArchived) || (note.pinned && !canShowPinned)) { - return false; - } - if (note.protected) { - const match = noteMatchesQuery(note, filterText); - /** Only match title to prevent leaking protected note text */ - return match === Match.Title || match === Match.TitleAndText; - } else { - return noteMatchesQuery(note, filterText) !== Match.None; - } -} - -enum Match { - None = 0, - Title = 1, - Text = 2, - TitleAndText = Title + Text, - Uuid = 5, -} - -function noteMatchesQuery(note: SNNote, query: string): Match { - if (query.length === 0) { - return Match.TitleAndText; - } - const title = note.safeTitle().toLowerCase(); - const text = note.safeText().toLowerCase(); - const lowercaseText = query.toLowerCase(); - const words = lowercaseText.split(' '); - const quotedText = stringBetweenQuotes(lowercaseText); - if (quotedText) { - return ( - (title.includes(quotedText) ? Match.Title : Match.None) + - (text.includes(quotedText) ? Match.Text : Match.None) - ); - } - if (stringIsUuid(lowercaseText)) { - return note.uuid === lowercaseText ? Match.Uuid : Match.None; - } - const matchesTitle = words.every((word) => { - return title.indexOf(word) >= 0; - }); - const matchesBody = words.every((word) => { - return text.indexOf(word) >= 0; - }); - return (matchesTitle ? Match.Title : 0) + (matchesBody ? Match.Text : 0); -} - -function stringBetweenQuotes(text: string) { - const matches = text.match(/"(.*?)"/); - return matches ? matches[1] : null; -} - -function stringIsUuid(text: string) { - const matches = text.match( - /\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b/ - ); - // eslint-disable-next-line no-unneeded-ternary - return matches ? true : false; -} diff --git a/app/assets/javascripts/views/notes/notes_view.ts b/app/assets/javascripts/views/notes/notes_view.ts index 3bd631cf6..42340d9a9 100644 --- a/app/assets/javascripts/views/notes/notes_view.ts +++ b/app/assets/javascripts/views/notes/notes_view.ts @@ -9,6 +9,8 @@ import { PrefKey, findInArray, CollectionSort, + UuidString, + NotesDisplayCriteria } from '@standardnotes/snjs'; import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl'; import { AppStateEvent } from '@/ui_models/app_state'; @@ -16,10 +18,6 @@ import { KeyboardModifier, KeyboardKey } from '@/services/keyboardManager'; import { PANEL_NAME_NOTES } from '@/views/constants'; -import { - notePassesFilter -} from './note_utils'; -import { UuidString } from '@standardnotes/snjs'; type NotesState = { panelTitle: string @@ -211,7 +209,7 @@ class NotesViewCtrl extends PureViewCtrl { */ private async createPlaceholderNote() { const selectedTag = this.selectedTag!; - if (selectedTag.isSmartTag() && !selectedTag.isAllTag) { + if (selectedTag.isSmartTag && !selectedTag.isAllTag) { return; } return this.createNewNote(); @@ -331,19 +329,20 @@ class NotesViewCtrl extends PureViewCtrl { */ private reloadNotesDisplayOptions() { const tag = this.appState.selectedTag!; - this.application!.setNotesDisplayOptions( - tag, - this.state.sortBy! as CollectionSort, - this.state.sortReverse! ? 'asc' : 'dsc', - (note: SNNote) => { - return notePassesFilter( - note, - this.getState().showArchived! || tag?.isArchiveTag || tag?.isTrashTag, - this.getState().hidePinned!, - this.getState().noteFilter.text.toLowerCase() - ); - } - ); + const searchText = this.getState().noteFilter.text.toLowerCase(); + const searchQuery = searchText ? { + query: searchText, + includeProtectedNoteText: false + } : undefined; + const criteria = NotesDisplayCriteria.Create({ + sortProperty: this.state.sortBy! as CollectionSort, + sortDirection: this.state.sortReverse! ? 'asc' : 'dsc', + tags: [tag], + includeArchived: this.getState().showArchived!, + includePinned: !this.getState().hidePinned!, + searchQuery: searchQuery + }); + this.application!.setNotesDisplayCriteria(criteria); } private get selectedTag() { @@ -376,7 +375,7 @@ class NotesViewCtrl extends PureViewCtrl { const selectedTag = this.appState.selectedTag; if (!selectedTag) { return []; - } else if (selectedTag?.isSmartTag()) { + } else if (selectedTag?.isSmartTag) { return notes.map((note) => this.appState .getNoteTags(note) diff --git a/app/assets/javascripts/views/tags/tags_view.ts b/app/assets/javascripts/views/tags/tags_view.ts index b4a609f59..60add087a 100644 --- a/app/assets/javascripts/views/tags/tags_view.ts +++ b/app/assets/javascripts/views/tags/tags_view.ts @@ -180,7 +180,7 @@ class TagsViewCtrl extends PureViewCtrl { if (tag === this.state.templateTag) { continue; } - if (tag.isSmartTag()) { + if (tag.isSmartTag) { /** Other smart tags do not contain counts */ if (tag.isAllTag) { const notes = this.application.notesMatchingSmartTag(tag as SNSmartTag) diff --git a/package.json b/package.json index 9c97e8fa5..7a1d286b4 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "@reach/alert-dialog": "^0.13.0", "@reach/dialog": "^0.13.0", "@standardnotes/sncrypto-web": "^1.2.10", - "@standardnotes/snjs": "^2.0.61", + "@standardnotes/snjs": "^2.0.63", "mobx": "^6.1.6", "preact": "^10.5.12" } diff --git a/yarn.lock b/yarn.lock index bfe148027..ddf42890a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1845,10 +1845,10 @@ "@standardnotes/sncrypto-common" "^1.2.7" libsodium-wrappers "^0.7.8" -"@standardnotes/snjs@^2.0.61": - version "2.0.61" - resolved "https://registry.yarnpkg.com/@standardnotes/snjs/-/snjs-2.0.61.tgz#82608ef48830a80afbc9468ed2d308e59e4e9a08" - integrity sha512-MVnzT7cX7oIak9g/xlOJKfWCfBygw8kXtuVHSofdxPhYIPIr1MwK4xkkqho/ZShVfPQEESyj3RaEsOIzKuuL4w== +"@standardnotes/snjs@^2.0.63": + version "2.0.63" + resolved "https://registry.yarnpkg.com/@standardnotes/snjs/-/snjs-2.0.63.tgz#67c775daab38ad45e78548923008ee4157131ec1" + integrity sha512-ryF2f1p6lR4aQ84s+bR4GqC7qtro+MzmY4rOIGpXPHbQoIorVcXXy3TjAbN4L4FxkFYdLe4tqU8vwJ00weK23Q== dependencies: "@standardnotes/sncrypto-common" "^1.2.9"