chore: migrate note controllers to snjs

This commit is contained in:
Mo
2022-01-06 11:03:45 -06:00
parent 875f5417be
commit 987f5aebf4
9 changed files with 22 additions and 195 deletions

View File

@@ -1,84 +0,0 @@
import { removeFromArray, UuidString } from '@standardnotes/snjs';
import { NoteViewController } from '@/views/note_view/note_view_controller';
import { WebApplication } from '@/ui_models/application';
type NoteControllerGroupChangeCallback = () => void;
export class NoteGroupController {
public noteControllers: NoteViewController[] = [];
private application: WebApplication;
changeObservers: NoteControllerGroupChangeCallback[] = [];
constructor(application: WebApplication) {
this.application = application;
}
public deinit() {
(this.application as unknown) = undefined;
for (const controller of this.noteControllers) {
this.deleteNoteView(controller);
}
}
async createNoteView(
noteUuid?: string,
noteTitle?: string,
noteTag?: UuidString
) {
const controller = new NoteViewController(
this.application,
noteUuid,
noteTitle,
noteTag
);
await controller.initialize();
this.noteControllers.push(controller);
this.notifyObservers();
}
deleteNoteView(controller: NoteViewController) {
controller.deinit();
removeFromArray(this.noteControllers, controller);
}
closeNoteView(controller: NoteViewController) {
this.deleteNoteView(controller);
this.notifyObservers();
}
closeActiveNoteView() {
const activeController = this.activeNoteViewController;
if (activeController) {
this.deleteNoteView(activeController);
}
}
closeAllNoteViews() {
for (const controller of this.noteControllers) {
this.deleteNoteView(controller);
}
}
get activeNoteViewController() {
return this.noteControllers[0];
}
/**
* Notifies observer when the active controller has changed.
*/
public addChangeObserver(callback: NoteControllerGroupChangeCallback) {
this.changeObservers.push(callback);
if (this.activeNoteViewController) {
callback();
}
return () => {
removeFromArray(this.changeObservers, callback);
};
}
private notifyObservers() {
for (const observer of this.changeObservers) {
observer();
}
}
}

View File

@@ -1,7 +1,7 @@
import { WebDirective } from './../../types';
import template from './note-group-view.pug';
import { NoteViewController } from '@/views/note_view/note_view_controller';
import { PureViewCtrl } from '../abstract/pure_view_ctrl';
import { NoteViewController } from '@standardnotes/snjs';
class NoteGroupView extends PureViewCtrl<
unknown,
@@ -20,9 +20,11 @@ class NoteGroupView extends PureViewCtrl<
}
$onInit() {
this.application.noteControllerGroup.addChangeObserver(() => {
this.controllers = this.application.noteControllerGroup.noteControllers;
});
this.application.noteControllerGroup.addActiveControllerChangeObserver(
() => {
this.controllers = this.application.noteControllerGroup.noteControllers;
}
);
this.autorun(() => {
this.setState({
showMultipleSelectedNotes: this.appState.notes.selectedNotesCount > 1,