Skip to content

Commit

Permalink
fix: sync after changing notes
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonella Sgarlatta committed May 10, 2021
1 parent bbea681 commit 8f7a085
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions app/assets/javascripts/ui_models/app_state/notes_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
NoteMutator,
ContentType,
SNTag,
SNItem,
} from '@standardnotes/snjs';
import {
makeObservable,
Expand All @@ -22,7 +23,10 @@ export class NotesState {
lastSelectedNote: SNNote | undefined;
selectedNotes: Record<UuidString, SNNote> = {};
contextMenuOpen = false;
contextMenuPosition: { top?: number; left: number, bottom?: number } = { top: 0, left: 0 };
contextMenuPosition: { top?: number; left: number; bottom?: number } = {
top: 0,
left: 0,
};

constructor(
private application: WebApplication,
Expand Down Expand Up @@ -164,33 +168,42 @@ export class NotesState {
this.contextMenuOpen = open;
}

setContextMenuPosition(position: { top?: number; left: number, bottom?: number }): void {
setContextMenuPosition(position: {
top?: number;
left: number;
bottom?: number;
}): void {
this.contextMenuPosition = position;
}

setHideSelectedNotePreviews(hide: boolean): void {
this.application.changeItems<NoteMutator>(
async changeSelectedNotes(
mutate: (mutator: NoteMutator) => void
): Promise<void> {
await this.application.changeItems(
Object.keys(this.selectedNotes),
mutate,
false,
);
this.application.sync();
}

setHideSelectedNotePreviews(hide: boolean): void {
this.changeSelectedNotes(
(mutator) => {
mutator.hidePreview = hide;
},
false
);
}

setLockSelectedNotes(lock: boolean): void {
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),
this.changeSelectedNotes(
(mutator) => {
mutator.locked = lock;
},
false
);
}

async setTrashSelectedNotes(
trashed: boolean
): Promise<void> {
async setTrashSelectedNotes(trashed: boolean): Promise<void> {
if (trashed) {
const notesDeleted = await this.deleteNotes(false);
if (notesDeleted) {
Expand All @@ -200,12 +213,10 @@ export class NotesState {
});
}
} else {
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),
this.changeSelectedNotes(
(mutator) => {
mutator.trashed = trashed;
},
false
);
this.unselectNotes();
this.contextMenuOpen = false;
Expand Down Expand Up @@ -251,12 +262,10 @@ export class NotesState {
await this.application.deleteItem(note);
}
} else {
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),
this.changeSelectedNotes(
(mutator) => {
mutator.trashed = true;
},
false
);
}
return true;
Expand All @@ -266,12 +275,10 @@ export class NotesState {
}

setPinSelectedNotes(pinned: boolean): void {
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),
this.changeSelectedNotes(
(mutator) => {
mutator.pinned = pinned;
},
false
);
}

Expand All @@ -282,12 +289,13 @@ export class NotesState {
);
return;
}
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),

this.changeSelectedNotes(
(mutator) => {
mutator.archived = archived;
}
},
);

runInAction(() => {
this.selectedNotes = {};
this.contextMenuOpen = false;
Expand All @@ -299,9 +307,7 @@ export class NotesState {
}

async addTagToSelectedNotes(tag: SNTag): Promise<void> {
const selectedNotes = Object.values(
this.application.getAppState().notes.selectedNotes
);
const selectedNotes = Object.values(this.selectedNotes);
await this.application.changeItem(tag.uuid, (mutator) => {
for (const note of selectedNotes) {
mutator.addItemAsRelationship(note);
Expand All @@ -311,9 +317,7 @@ export class NotesState {
}

async removeTagFromSelectedNotes(tag: SNTag): Promise<void> {
const selectedNotes = Object.values(
this.application.getAppState().notes.selectedNotes
);
const selectedNotes = Object.values(this.selectedNotes);
await this.application.changeItem(tag.uuid, (mutator) => {
for (const note of selectedNotes) {
mutator.removeItemAsRelationship(note);
Expand All @@ -323,9 +327,7 @@ export class NotesState {
}

isTagInSelectedNotes(tag: SNTag): boolean {
const selectedNotes = Object.values(
this.application.getAppState().notes.selectedNotes
);
const selectedNotes = Object.values(this.selectedNotes);
return selectedNotes.every((note) =>
this.application
.getAppState()
Expand Down

0 comments on commit 8f7a085

Please sign in to comment.