feat: notes display criteria (#530)

* feat: notes display criteria

* chore: remove unused code

* chore: update names

* chore: update snjs version
This commit is contained in:
Mo Bitar
2021-03-04 12:22:14 -06:00
committed by GitHub
parent 9d017b5745
commit e0ab938ccf
7 changed files with 27 additions and 97 deletions

View File

@@ -259,7 +259,7 @@ export class AppState {
async createEditor(title?: string) { async createEditor(title?: string) {
const activeEditor = this.getActiveEditor(); const activeEditor = this.getActiveEditor();
const activeTagUuid = this.selectedTag const activeTagUuid = this.selectedTag
? this.selectedTag.isSmartTag() ? this.selectedTag.isSmartTag
? undefined ? undefined
: this.selectedTag.uuid : this.selectedTag.uuid
: undefined; : undefined;

View File

@@ -460,7 +460,7 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {
await this.editor.insertTemplatedNote(); await this.editor.insertTemplatedNote();
} }
const selectedTag = this.appState.selectedTag; const selectedTag = this.appState.selectedTag;
if (!selectedTag?.isSmartTag() && !selectedTag?.hasRelationshipWithItem(note)) { if (!selectedTag?.isSmartTag && !selectedTag?.hasRelationshipWithItem(note)) {
await this.application.changeItem( await this.application.changeItem(
selectedTag!.uuid, selectedTag!.uuid,
(mutator) => { (mutator) => {
@@ -1052,7 +1052,7 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {
if (savedUuid === this.note.uuid) { if (savedUuid === this.note.uuid) {
const selectedTag = this.appState.selectedTag; const selectedTag = this.appState.selectedTag;
if ( if (
!selectedTag?.isSmartTag() && !selectedTag?.isSmartTag &&
!selectedTag?.hasRelationshipWithItem(this.note) !selectedTag?.hasRelationshipWithItem(this.note)
) { ) {
this.application.changeAndSaveItem( this.application.changeAndSaveItem(

View File

@@ -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;
}

View File

@@ -9,6 +9,8 @@ import {
PrefKey, PrefKey,
findInArray, findInArray,
CollectionSort, CollectionSort,
UuidString,
NotesDisplayCriteria
} from '@standardnotes/snjs'; } from '@standardnotes/snjs';
import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl'; import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl';
import { AppStateEvent } from '@/ui_models/app_state'; import { AppStateEvent } from '@/ui_models/app_state';
@@ -16,10 +18,6 @@ import { KeyboardModifier, KeyboardKey } from '@/services/keyboardManager';
import { import {
PANEL_NAME_NOTES PANEL_NAME_NOTES
} from '@/views/constants'; } from '@/views/constants';
import {
notePassesFilter
} from './note_utils';
import { UuidString } from '@standardnotes/snjs';
type NotesState = { type NotesState = {
panelTitle: string panelTitle: string
@@ -211,7 +209,7 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesState> {
*/ */
private async createPlaceholderNote() { private async createPlaceholderNote() {
const selectedTag = this.selectedTag!; const selectedTag = this.selectedTag!;
if (selectedTag.isSmartTag() && !selectedTag.isAllTag) { if (selectedTag.isSmartTag && !selectedTag.isAllTag) {
return; return;
} }
return this.createNewNote(); return this.createNewNote();
@@ -331,19 +329,20 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesState> {
*/ */
private reloadNotesDisplayOptions() { private reloadNotesDisplayOptions() {
const tag = this.appState.selectedTag!; const tag = this.appState.selectedTag!;
this.application!.setNotesDisplayOptions( const searchText = this.getState().noteFilter.text.toLowerCase();
tag, const searchQuery = searchText ? {
this.state.sortBy! as CollectionSort, query: searchText,
this.state.sortReverse! ? 'asc' : 'dsc', includeProtectedNoteText: false
(note: SNNote) => { } : undefined;
return notePassesFilter( const criteria = NotesDisplayCriteria.Create({
note, sortProperty: this.state.sortBy! as CollectionSort,
this.getState().showArchived! || tag?.isArchiveTag || tag?.isTrashTag, sortDirection: this.state.sortReverse! ? 'asc' : 'dsc',
this.getState().hidePinned!, tags: [tag],
this.getState().noteFilter.text.toLowerCase() includeArchived: this.getState().showArchived!,
); includePinned: !this.getState().hidePinned!,
} searchQuery: searchQuery
); });
this.application!.setNotesDisplayCriteria(criteria);
} }
private get selectedTag() { private get selectedTag() {
@@ -376,7 +375,7 @@ class NotesViewCtrl extends PureViewCtrl<unknown, NotesState> {
const selectedTag = this.appState.selectedTag; const selectedTag = this.appState.selectedTag;
if (!selectedTag) { if (!selectedTag) {
return []; return [];
} else if (selectedTag?.isSmartTag()) { } else if (selectedTag?.isSmartTag) {
return notes.map((note) => return notes.map((note) =>
this.appState this.appState
.getNoteTags(note) .getNoteTags(note)

View File

@@ -180,7 +180,7 @@ class TagsViewCtrl extends PureViewCtrl<unknown, TagState> {
if (tag === this.state.templateTag) { if (tag === this.state.templateTag) {
continue; continue;
} }
if (tag.isSmartTag()) { if (tag.isSmartTag) {
/** Other smart tags do not contain counts */ /** Other smart tags do not contain counts */
if (tag.isAllTag) { if (tag.isAllTag) {
const notes = this.application.notesMatchingSmartTag(tag as SNSmartTag) const notes = this.application.notesMatchingSmartTag(tag as SNSmartTag)

View File

@@ -68,7 +68,7 @@
"@reach/alert-dialog": "^0.13.0", "@reach/alert-dialog": "^0.13.0",
"@reach/dialog": "^0.13.0", "@reach/dialog": "^0.13.0",
"@standardnotes/sncrypto-web": "^1.2.10", "@standardnotes/sncrypto-web": "^1.2.10",
"@standardnotes/snjs": "^2.0.61", "@standardnotes/snjs": "^2.0.63",
"mobx": "^6.1.6", "mobx": "^6.1.6",
"preact": "^10.5.12" "preact": "^10.5.12"
} }

View File

@@ -1845,10 +1845,10 @@
"@standardnotes/sncrypto-common" "^1.2.7" "@standardnotes/sncrypto-common" "^1.2.7"
libsodium-wrappers "^0.7.8" libsodium-wrappers "^0.7.8"
"@standardnotes/snjs@^2.0.61": "@standardnotes/snjs@^2.0.63":
version "2.0.61" version "2.0.63"
resolved "https://registry.yarnpkg.com/@standardnotes/snjs/-/snjs-2.0.61.tgz#82608ef48830a80afbc9468ed2d308e59e4e9a08" resolved "https://registry.yarnpkg.com/@standardnotes/snjs/-/snjs-2.0.63.tgz#67c775daab38ad45e78548923008ee4157131ec1"
integrity sha512-MVnzT7cX7oIak9g/xlOJKfWCfBygw8kXtuVHSofdxPhYIPIr1MwK4xkkqho/ZShVfPQEESyj3RaEsOIzKuuL4w== integrity sha512-ryF2f1p6lR4aQ84s+bR4GqC7qtro+MzmY4rOIGpXPHbQoIorVcXXy3TjAbN4L4FxkFYdLe4tqU8vwJ00weK23Q==
dependencies: dependencies:
"@standardnotes/sncrypto-common" "^1.2.9" "@standardnotes/sncrypto-common" "^1.2.9"