chore: migrate note controllers to snjs
This commit is contained in:
@@ -2,7 +2,6 @@ import { Bridge } from '@/services/bridge';
|
|||||||
import { storage, StorageKey } from '@/services/localStorage';
|
import { storage, StorageKey } from '@/services/localStorage';
|
||||||
import { WebApplication } from '@/ui_models/application';
|
import { WebApplication } from '@/ui_models/application';
|
||||||
import { AccountMenuState } from '@/ui_models/app_state/account_menu_state';
|
import { AccountMenuState } from '@/ui_models/app_state/account_menu_state';
|
||||||
import { NoteViewController } from '@/views/note_view/note_view_controller';
|
|
||||||
import { isDesktopApplication } from '@/utils';
|
import { isDesktopApplication } from '@/utils';
|
||||||
import {
|
import {
|
||||||
ApplicationEvent,
|
ApplicationEvent,
|
||||||
@@ -17,6 +16,7 @@ import {
|
|||||||
SNSmartTag,
|
SNSmartTag,
|
||||||
ComponentViewer,
|
ComponentViewer,
|
||||||
SNTag,
|
SNTag,
|
||||||
|
NoteViewController,
|
||||||
} from '@standardnotes/snjs';
|
} from '@standardnotes/snjs';
|
||||||
import pull from 'lodash/pull';
|
import pull from 'lodash/pull';
|
||||||
import {
|
import {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
ContentType,
|
ContentType,
|
||||||
SNTag,
|
SNTag,
|
||||||
ChallengeReason,
|
ChallengeReason,
|
||||||
|
NoteViewController,
|
||||||
} from '@standardnotes/snjs';
|
} from '@standardnotes/snjs';
|
||||||
import {
|
import {
|
||||||
makeObservable,
|
makeObservable,
|
||||||
@@ -17,7 +18,6 @@ import {
|
|||||||
runInAction,
|
runInAction,
|
||||||
} from 'mobx';
|
} from 'mobx';
|
||||||
import { WebApplication } from '../application';
|
import { WebApplication } from '../application';
|
||||||
import { NoteViewController } from '@/views/note_view/note_view_controller';
|
|
||||||
import { AppState } from './app_state';
|
import { AppState } from './app_state';
|
||||||
|
|
||||||
export class NotesState {
|
export class NotesState {
|
||||||
|
|||||||
@@ -10,13 +10,13 @@ import { StatusManager } from '@/services/statusManager';
|
|||||||
import { ThemeManager } from '@/services/themeManager';
|
import { ThemeManager } from '@/services/themeManager';
|
||||||
import { PasswordWizardScope, PasswordWizardType } from '@/types';
|
import { PasswordWizardScope, PasswordWizardType } from '@/types';
|
||||||
import { AppState } from '@/ui_models/app_state';
|
import { AppState } from '@/ui_models/app_state';
|
||||||
import { NoteGroupController } from '@/views/note_group_view/note_group_controller';
|
|
||||||
import { WebDeviceInterface } from '@/web_device_interface';
|
import { WebDeviceInterface } from '@/web_device_interface';
|
||||||
import {
|
import {
|
||||||
DeinitSource,
|
DeinitSource,
|
||||||
PermissionDialog,
|
PermissionDialog,
|
||||||
Platform,
|
Platform,
|
||||||
SNApplication,
|
SNApplication,
|
||||||
|
NoteGroupController,
|
||||||
} from '@standardnotes/snjs';
|
} from '@standardnotes/snjs';
|
||||||
import angular from 'angular';
|
import angular from 'angular';
|
||||||
import { AccountSwitcherScope, PermissionsModalScope } from './../types';
|
import { AccountSwitcherScope, PermissionsModalScope } from './../types';
|
||||||
|
|||||||
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { WebDirective } from './../../types';
|
import { WebDirective } from './../../types';
|
||||||
import template from './note-group-view.pug';
|
import template from './note-group-view.pug';
|
||||||
import { NoteViewController } from '@/views/note_view/note_view_controller';
|
|
||||||
import { PureViewCtrl } from '../abstract/pure_view_ctrl';
|
import { PureViewCtrl } from '../abstract/pure_view_ctrl';
|
||||||
|
import { NoteViewController } from '@standardnotes/snjs';
|
||||||
|
|
||||||
class NoteGroupView extends PureViewCtrl<
|
class NoteGroupView extends PureViewCtrl<
|
||||||
unknown,
|
unknown,
|
||||||
@@ -20,9 +20,11 @@ class NoteGroupView extends PureViewCtrl<
|
|||||||
}
|
}
|
||||||
|
|
||||||
$onInit() {
|
$onInit() {
|
||||||
this.application.noteControllerGroup.addChangeObserver(() => {
|
this.application.noteControllerGroup.addActiveControllerChangeObserver(
|
||||||
this.controllers = this.application.noteControllerGroup.noteControllers;
|
() => {
|
||||||
});
|
this.controllers = this.application.noteControllerGroup.noteControllers;
|
||||||
|
}
|
||||||
|
);
|
||||||
this.autorun(() => {
|
this.autorun(() => {
|
||||||
this.setState({
|
this.setState({
|
||||||
showMultipleSelectedNotes: this.appState.notes.selectedNotesCount > 1,
|
showMultipleSelectedNotes: this.appState.notes.selectedNotesCount > 1,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { STRING_SAVING_WHILE_DOCUMENT_HIDDEN } from './../../strings';
|
import { STRING_SAVING_WHILE_DOCUMENT_HIDDEN } from './../../strings';
|
||||||
import { NoteViewController } from '@/views/note_view/note_view_controller';
|
|
||||||
import { WebApplication } from '@/ui_models/application';
|
import { WebApplication } from '@/ui_models/application';
|
||||||
import { PanelPuppet, WebDirective } from '@/types';
|
import { PanelPuppet, WebDirective } from '@/types';
|
||||||
import angular from 'angular';
|
import angular from 'angular';
|
||||||
@@ -20,6 +19,7 @@ import {
|
|||||||
TransactionalMutation,
|
TransactionalMutation,
|
||||||
ItemMutator,
|
ItemMutator,
|
||||||
ProposedSecondsToDeferUILevelSessionExpirationDuringActiveInteraction,
|
ProposedSecondsToDeferUILevelSessionExpirationDuringActiveInteraction,
|
||||||
|
NoteViewController,
|
||||||
} from '@standardnotes/snjs';
|
} from '@standardnotes/snjs';
|
||||||
import { debounce, isDesktopApplication } from '@/utils';
|
import { debounce, isDesktopApplication } from '@/utils';
|
||||||
import { KeyboardModifier, KeyboardKey } from '@/services/ioService';
|
import { KeyboardModifier, KeyboardKey } from '@/services/ioService';
|
||||||
@@ -108,6 +108,7 @@ export class NoteView extends PureViewCtrl<unknown, EditorState> {
|
|||||||
private removeTabObserver?: () => void;
|
private removeTabObserver?: () => void;
|
||||||
private removeComponentStreamObserver?: () => void;
|
private removeComponentStreamObserver?: () => void;
|
||||||
private removeComponentManagerObserver?: () => void;
|
private removeComponentManagerObserver?: () => void;
|
||||||
|
private removeInnerNoteObserver?: () => void;
|
||||||
|
|
||||||
private protectionTimeoutId: ReturnType<typeof setTimeout> | null = null;
|
private protectionTimeoutId: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
@@ -139,6 +140,8 @@ export class NoteView extends PureViewCtrl<unknown, EditorState> {
|
|||||||
deinit() {
|
deinit() {
|
||||||
this.removeComponentStreamObserver?.();
|
this.removeComponentStreamObserver?.();
|
||||||
(this.removeComponentStreamObserver as unknown) = undefined;
|
(this.removeComponentStreamObserver as unknown) = undefined;
|
||||||
|
this.removeInnerNoteObserver?.();
|
||||||
|
(this.removeInnerNoteObserver as unknown) = undefined;
|
||||||
this.removeComponentManagerObserver?.();
|
this.removeComponentManagerObserver?.();
|
||||||
(this.removeComponentManagerObserver as unknown) = undefined;
|
(this.removeComponentManagerObserver as unknown) = undefined;
|
||||||
this.removeTrashKeyObserver?.();
|
this.removeTrashKeyObserver?.();
|
||||||
@@ -167,9 +170,10 @@ export class NoteView extends PureViewCtrl<unknown, EditorState> {
|
|||||||
$onInit() {
|
$onInit() {
|
||||||
super.$onInit();
|
super.$onInit();
|
||||||
this.registerKeyboardShortcuts();
|
this.registerKeyboardShortcuts();
|
||||||
this.controller.setOnNoteInnerValueChange((note, source) => {
|
this.removeInnerNoteObserver =
|
||||||
this.onNoteInnerChange(note, source);
|
this.controller.addNoteInnerValueChangeObserver((note, source) => {
|
||||||
});
|
this.onNoteInnerChange(note, source);
|
||||||
|
});
|
||||||
this.autorun(() => {
|
this.autorun(() => {
|
||||||
this.setState({
|
this.setState({
|
||||||
showProtectedWarning: this.appState.notes.showProtectedWarning,
|
showProtectedWarning: this.appState.notes.showProtectedWarning,
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -89,7 +89,7 @@
|
|||||||
"@reach/tooltip": "^0.16.2",
|
"@reach/tooltip": "^0.16.2",
|
||||||
"@standardnotes/features": "1.20.5",
|
"@standardnotes/features": "1.20.5",
|
||||||
"@standardnotes/sncrypto-web": "1.5.3",
|
"@standardnotes/sncrypto-web": "1.5.3",
|
||||||
"@standardnotes/snjs": "2.33.0",
|
"@standardnotes/snjs": "2.34.0",
|
||||||
"mobx": "^6.3.5",
|
"mobx": "^6.3.5",
|
||||||
"mobx-react-lite": "^3.2.2",
|
"mobx-react-lite": "^3.2.2",
|
||||||
"preact": "^10.5.15",
|
"preact": "^10.5.15",
|
||||||
|
|||||||
@@ -2655,10 +2655,10 @@
|
|||||||
buffer "^6.0.3"
|
buffer "^6.0.3"
|
||||||
libsodium-wrappers "^0.7.9"
|
libsodium-wrappers "^0.7.9"
|
||||||
|
|
||||||
"@standardnotes/snjs@2.33.0":
|
"@standardnotes/snjs@2.34.0":
|
||||||
version "2.33.0"
|
version "2.34.0"
|
||||||
resolved "https://registry.yarnpkg.com/@standardnotes/snjs/-/snjs-2.33.0.tgz#f3d295740471136bf2ec52e1d0e1cdf553923226"
|
resolved "https://registry.yarnpkg.com/@standardnotes/snjs/-/snjs-2.34.0.tgz#ba5ccc3e82a190d3284cea2936e3453ed49b0b2c"
|
||||||
integrity sha512-KHzxt2CqeFnQgKp6cf+Jqvfx3P0DXP1myEr6a41NmXAQ5qu2ytugyzxiyDyakFvR+Cp7g7GjRA+DwLvO7i1kaA==
|
integrity sha512-1qIahN+TFy51FZcouWSGpIqxe5kDZAl07n3quzv3WszzvfIeB2X+40bmhJAj7/qbWjvNfoA60jKZYxiAbMIiJQ==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@standardnotes/auth" "3.8.1"
|
"@standardnotes/auth" "3.8.1"
|
||||||
"@standardnotes/common" "1.2.1"
|
"@standardnotes/common" "1.2.1"
|
||||||
|
|||||||
Reference in New Issue
Block a user