refactor: rename note controller to note view controller

This commit is contained in:
Mo
2021-12-27 22:56:21 -06:00
parent 15aea42d4f
commit 0eeb9b7da1
13 changed files with 55 additions and 55 deletions

View File

@@ -2,7 +2,7 @@ import { Bridge } from '@/services/bridge';
import { storage, StorageKey } from '@/services/localStorage';
import { WebApplication } from '@/ui_models/application';
import { AccountMenuState } from '@/ui_models/app_state/account_menu_state';
import { NoteController } from '@/ui_models/note_controller';
import { NoteViewController } from '@/views/note_view/note_view_controller';
import { isDesktopApplication } from '@/utils';
import {
ApplicationEvent,
@@ -236,7 +236,7 @@ export class AppState {
: this.selectedTag.uuid
: undefined;
await this.application.noteControllerGroup.createNoteController(
await this.application.noteControllerGroup.createNoteView(
undefined,
title,
activeTagUuid
@@ -251,16 +251,16 @@ export class AppState {
return this.application.noteControllerGroup.noteControllers;
}
closeNoteController(controller: NoteController) {
this.application.noteControllerGroup.closeController(controller);
closeNoteController(controller: NoteViewController) {
this.application.noteControllerGroup.closeNoteView(controller);
}
closeActiveNoteController() {
this.application.noteControllerGroup.closeActiveController();
this.application.noteControllerGroup.closeActiveNoteView();
}
closeAllNoteControllers() {
this.application.noteControllerGroup.closeAllControllers();
this.application.noteControllerGroup.closeAllNoteViews();
}
noteControllerForNote(note: SNNote) {

View File

@@ -17,7 +17,7 @@ import {
runInAction,
} from 'mobx';
import { WebApplication } from '../application';
import { NoteController } from '../note_controller';
import { NoteViewController } from '@/views/note_view/note_view_controller';
import { AppState } from './app_state';
export class NotesState {
@@ -68,7 +68,7 @@ export class NotesState {
);
}
get activeNoteController(): NoteController | undefined {
get activeNoteController(): NoteViewController | undefined {
return this.application.noteControllerGroup.noteControllers[0];
}
@@ -168,10 +168,10 @@ export class NotesState {
}
if (this.activeNoteController) {
this.application.noteControllerGroup.closeActiveController();
this.application.noteControllerGroup.closeActiveNoteView();
}
await this.application.noteControllerGroup.createNoteController(noteUuid);
await this.application.noteControllerGroup.createNoteView(noteUuid);
this.appState.noteTags.reloadTags();
await this.onActiveEditorChanged();

View File

@@ -10,7 +10,7 @@ import { StatusManager } from '@/services/statusManager';
import { ThemeManager } from '@/services/themeManager';
import { PasswordWizardScope, PasswordWizardType } from '@/types';
import { AppState } from '@/ui_models/app_state';
import { NoteControllerGroup } from '@/ui_models/note_controller_group';
import { NoteGroupController } from '@/views/note_group_view/note_group_controller';
import { AppVersion } from '@/version';
import { WebDeviceInterface } from '@/web_device_interface';
import {
@@ -36,7 +36,7 @@ export class WebApplication extends SNApplication {
private scope?: angular.IScope;
private webServices!: WebServices;
private currentAuthenticationElement?: angular.IRootElementService;
public noteControllerGroup: NoteControllerGroup;
public noteControllerGroup: NoteGroupController;
/* @ngInject */
constructor(
@@ -66,7 +66,7 @@ export class WebApplication extends SNApplication {
this.$compile = $compile;
this.scope = scope;
deviceInterface.setApplication(this);
this.noteControllerGroup = new NoteControllerGroup(this);
this.noteControllerGroup = new NoteGroupController(this);
this.presentPermissionsDialog = this.presentPermissionsDialog.bind(this);
}

View File

@@ -1,95 +0,0 @@
import {
SNNote,
ContentType,
PayloadSource,
UuidString,
SNTag,
} from '@standardnotes/snjs';
import { WebApplication } from './application';
export class NoteController {
public note!: SNNote;
private application: WebApplication;
private onNoteValueChange?: (note: SNNote, source: PayloadSource) => void;
private removeStreamObserver?: () => void;
public isTemplateNote = false;
constructor(
application: WebApplication,
noteUuid: string | undefined,
private defaultTitle: string | undefined,
private defaultTag: UuidString | undefined
) {
this.application = application;
if (noteUuid) {
this.note = application.findItem(noteUuid) as SNNote;
}
}
async initialize(): Promise<void> {
if (!this.note) {
const note = (await this.application.createTemplateItem(
ContentType.Note,
{
text: '',
title: this.defaultTitle,
references: [],
}
)) as SNNote;
if (this.defaultTag) {
const tag = this.application.findItem(this.defaultTag) as SNTag;
await this.application.addTagHierarchyToNote(note, tag);
}
this.isTemplateNote = true;
this.note = note;
this.onNoteValueChange?.(this.note, this.note.payload.source);
}
this.streamItems();
}
private streamItems() {
this.removeStreamObserver = this.application.streamItems(
ContentType.Note,
(items, source) => {
this.handleNoteStream(items as SNNote[], source);
}
);
}
deinit() {
this.removeStreamObserver?.();
(this.removeStreamObserver as unknown) = undefined;
(this.application as unknown) = undefined;
this.onNoteValueChange = undefined;
}
private handleNoteStream(notes: SNNote[], source: PayloadSource) {
/** Update our note object reference whenever it changes */
const matchingNote = notes.find((item) => {
return item.uuid === this.note.uuid;
}) as SNNote;
if (matchingNote) {
this.isTemplateNote = false;
this.note = matchingNote;
this.onNoteValueChange?.(matchingNote, source);
}
}
insertTemplatedNote() {
this.isTemplateNote = false;
return this.application.insertItem(this.note);
}
/**
* Register to be notified when the controller's note's inner values change
* (and thus a new object reference is created)
*/
public setOnNoteInnerValueChange(
callback: (note: SNNote, source: PayloadSource) => void
) {
this.onNoteValueChange = callback;
if (this.note) {
this.onNoteValueChange(this.note, this.note.payload.source);
}
}
}

View File

@@ -1,84 +0,0 @@
import { removeFromArray, UuidString } from '@standardnotes/snjs';
import { NoteController } from './note_controller';
import { WebApplication } from './application';
type NoteControllerGroupChangeCallback = () => void;
export class NoteControllerGroup {
public noteControllers: NoteController[] = [];
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.deleteController(controller);
}
}
async createNoteController(
noteUuid?: string,
noteTitle?: string,
noteTag?: UuidString
) {
const controller = new NoteController(
this.application,
noteUuid,
noteTitle,
noteTag
);
await controller.initialize();
this.noteControllers.push(controller);
this.notifyObservers();
}
deleteController(controller: NoteController) {
controller.deinit();
removeFromArray(this.noteControllers, controller);
}
closeController(controller: NoteController) {
this.deleteController(controller);
this.notifyObservers();
}
closeActiveController() {
const activeController = this.activeNoteController;
if (activeController) {
this.deleteController(activeController);
}
}
closeAllControllers() {
for (const controller of this.noteControllers) {
this.deleteController(controller);
}
}
get activeNoteController() {
return this.noteControllers[0];
}
/**
* Notifies observer when the active controller has changed.
*/
public addChangeObserver(callback: NoteControllerGroupChangeCallback) {
this.changeObservers.push(callback);
if (this.activeNoteController) {
callback();
}
return () => {
removeFromArray(this.changeObservers, callback);
};
}
private notifyObservers() {
for (const observer of this.changeObservers) {
observer();
}
}
}