feat: multiple selected notes panel

This commit is contained in:
Baptiste Grob
2021-04-08 11:30:56 +02:00
parent 0f53361689
commit abfc588368
36 changed files with 542 additions and 128 deletions

View File

@@ -83,7 +83,8 @@ export class AppState {
this.application,
async () => {
await this.notifyEvent(AppStateEvent.ActiveEditorChanged);
}
},
this.appEventObserverRemovers,
);
this.noAccountWarning = new NoAccountWarningState(
application,

View File

@@ -1,41 +1,81 @@
import { KeyboardModifier } from "@/services/ioService";
import { UuidString, SNNote } from "@standardnotes/snjs";
import { makeObservable, observable, action } from "mobx";
import { WebApplication } from "../application";
import { Editor } from "../editor";
import { confirmDialog } from '@/services/alertService';
import { KeyboardModifier } from '@/services/ioService';
import { Strings } from '@/strings';
import {
UuidString,
SNNote,
NoteMutator,
ContentType,
} from '@standardnotes/snjs';
import {
makeObservable,
observable,
action,
computed,
runInAction,
} from 'mobx';
import { WebApplication } from '../application';
import { Editor } from '../editor';
export class NotesState {
selectedNotes: Record<UuidString, SNNote> = {};
constructor(
private application: WebApplication,
private onActiveEditorChanged: () => Promise<void>
private onActiveEditorChanged: () => Promise<void>,
appEventListeners: (() => void)[]
) {
makeObservable(this, {
selectedNotes: observable,
selectedNotesCount: computed,
selectNote: action,
setHideSelectedNotePreviews: action,
setLockSelectedNotes: action,
});
appEventListeners.push(
application.streamItems(ContentType.Note, (notes) => {
runInAction(() => {
for (const note of notes) {
if (this.selectedNotes[note.uuid]) {
this.selectedNotes[note.uuid] = note as SNNote;
}
}
});
})
);
}
get activeEditor(): Editor | undefined {
return this.application.editorGroup.editors[0];
}
async selectNote(note: SNNote): Promise<void> {
get selectedNotesCount(): number {
return Object.keys(this.selectedNotes).length;
}
async selectNote(uuid: UuidString): Promise<void> {
const note = this.application.findItem(uuid) as SNNote;
if (
this.io.activeModifiers.has(KeyboardModifier.Meta) ||
this.io.activeModifiers.has(KeyboardModifier.Ctrl)
) {
this.selectedNotes[note.uuid] = note;
if (this.selectedNotes[uuid]) {
delete this.selectedNotes[uuid];
} else {
this.selectedNotes[uuid] = note;
}
} else {
this.selectedNotes = {
[note.uuid]: note,
[uuid]: note,
};
await this.openEditor(uuid);
}
await this.openEditor(note.uuid);
}
async openEditor(noteUuid: string): Promise<void> {
private async openEditor(noteUuid: string): Promise<void> {
if (this.activeEditor?.note?.uuid === noteUuid) {
return;
}
@@ -60,6 +100,69 @@ export class NotesState {
}
}
setHideSelectedNotePreviews(hide: boolean): void {
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),
(mutator) => {
mutator.hidePreview = hide;
},
false
);
}
setLockSelectedNotes(lock: boolean): void {
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),
(mutator) => {
mutator.locked = lock;
},
false
);
}
async setTrashSelectedNotes(trashed: boolean): Promise<void> {
if (
await confirmDialog({
title: Strings.trashNotesTitle,
text: Strings.trashNotesText,
confirmButtonStyle: 'danger',
})
) {
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),
(mutator) => {
mutator.trashed = trashed;
},
false
);
runInAction(() => {
this.selectedNotes = {};
});
}
}
setPinSelectedNotes(pinned: boolean): void {
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),
(mutator) => {
mutator.pinned = pinned;
},
false
);
}
setArchiveSelectedNotes(archived: boolean): void {
this.application.changeItems<NoteMutator>(
Object.keys(this.selectedNotes),
(mutator) => {
mutator.archived = archived;
}
);
runInAction(() => {
this.selectedNotes = {};
});
}
private get io() {
return this.application.io;
}