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,5 +1,4 @@
import { STRING_SAVING_WHILE_DOCUMENT_HIDDEN } from './../../strings';
import { NoteViewController } from '@/views/note_view/note_view_controller';
import { WebApplication } from '@/ui_models/application';
import { PanelPuppet, WebDirective } from '@/types';
import angular from 'angular';
@@ -20,6 +19,7 @@ import {
TransactionalMutation,
ItemMutator,
ProposedSecondsToDeferUILevelSessionExpirationDuringActiveInteraction,
NoteViewController,
} from '@standardnotes/snjs';
import { debounce, isDesktopApplication } from '@/utils';
import { KeyboardModifier, KeyboardKey } from '@/services/ioService';
@@ -108,6 +108,7 @@ export class NoteView extends PureViewCtrl<unknown, EditorState> {
private removeTabObserver?: () => void;
private removeComponentStreamObserver?: () => void;
private removeComponentManagerObserver?: () => void;
private removeInnerNoteObserver?: () => void;
private protectionTimeoutId: ReturnType<typeof setTimeout> | null = null;
@@ -139,6 +140,8 @@ export class NoteView extends PureViewCtrl<unknown, EditorState> {
deinit() {
this.removeComponentStreamObserver?.();
(this.removeComponentStreamObserver as unknown) = undefined;
this.removeInnerNoteObserver?.();
(this.removeInnerNoteObserver as unknown) = undefined;
this.removeComponentManagerObserver?.();
(this.removeComponentManagerObserver as unknown) = undefined;
this.removeTrashKeyObserver?.();
@@ -167,9 +170,10 @@ export class NoteView extends PureViewCtrl<unknown, EditorState> {
$onInit() {
super.$onInit();
this.registerKeyboardShortcuts();
this.controller.setOnNoteInnerValueChange((note, source) => {
this.onNoteInnerChange(note, source);
});
this.removeInnerNoteObserver =
this.controller.addNoteInnerValueChangeObserver((note, source) => {
this.onNoteInnerChange(note, source);
});
this.autorun(() => {
this.setState({
showProtectedWarning: this.appState.notes.showProtectedWarning,

View File

@@ -1,95 +0,0 @@
import {
SNNote,
ContentType,
PayloadSource,
UuidString,
SNTag,
} from '@standardnotes/snjs';
import { WebApplication } from '@/ui_models/application';
export class NoteViewController {
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);
}
}
}