From d49d2e996b47738252fdb8b44cd53b6ea882b9cb Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Mon, 16 Nov 2020 17:00:01 +0100 Subject: [PATCH] fix: assign active tag when creating a new note --- app/assets/javascripts/ui_models/app_state.ts | 14 ++++++++++++-- app/assets/javascripts/ui_models/editor.ts | 19 ++++++++++++++----- .../javascripts/ui_models/editor_group.ts | 12 ++++++++---- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/ui_models/app_state.ts b/app/assets/javascripts/ui_models/app_state.ts index 4d93f71ee..c607c894d 100644 --- a/app/assets/javascripts/ui_models/app_state.ts +++ b/app/assets/javascripts/ui_models/app_state.ts @@ -152,10 +152,20 @@ export class AppState { */ async createEditor(title?: string) { const activeEditor = this.getActiveEditor(); + const activeTagUuid = this.selectedTag + ? this.selectedTag.isSmartTag() + ? undefined + : this.selectedTag.uuid + : undefined; + if (!activeEditor || this.multiEditorEnabled) { - this.application.editorGroup.createEditor(undefined, title); + this.application.editorGroup.createEditor( + undefined, + title, + activeTagUuid + ); } else { - await activeEditor.reset(title); + await activeEditor.reset(title, activeTagUuid); } } diff --git a/app/assets/javascripts/ui_models/editor.ts b/app/assets/javascripts/ui_models/editor.ts index 7c03fbdb6..0a5eed08e 100644 --- a/app/assets/javascripts/ui_models/editor.ts +++ b/app/assets/javascripts/ui_models/editor.ts @@ -1,4 +1,4 @@ -import { SNNote, ContentType, PayloadSource } from 'snjs'; +import { SNNote, ContentType, PayloadSource, UuidString, TagMutator } from 'snjs'; import { WebApplication } from './application'; export class Editor { @@ -12,15 +12,16 @@ export class Editor { constructor( application: WebApplication, - noteUuid?: string, - noteTitle?: string + noteUuid: string | undefined, + noteTitle: string | undefined, + noteTag: UuidString | undefined ) { this.application = application; if (noteUuid) { this.note = application.findItem(noteUuid) as SNNote; this.streamItems(); } else { - this.reset(noteTitle) + this.reset(noteTitle, noteTag) .then(() => this.streamItems()) .catch(console.error); } @@ -65,7 +66,10 @@ export class Editor { * Reverts the editor to a blank state, removing any existing note from view, * and creating a placeholder note. */ - async reset(noteTitle = '') { + async reset( + noteTitle = '', + noteTag?: UuidString, + ) { const note = await this.application.createTemplateItem( ContentType.Note, { @@ -74,6 +78,11 @@ export class Editor { references: [] } ) as SNNote; + if (noteTag) { + await this.application.changeItem(noteTag, (m) => { + m.addItemAsRelationship(note); + }); + } if (!this.isTemplateNote || this.note.title !== note.title) { this.setNote(note as SNNote, true); } diff --git a/app/assets/javascripts/ui_models/editor_group.ts b/app/assets/javascripts/ui_models/editor_group.ts index 5be00f8ec..6ded5a3e0 100644 --- a/app/assets/javascripts/ui_models/editor_group.ts +++ b/app/assets/javascripts/ui_models/editor_group.ts @@ -1,4 +1,4 @@ -import { removeFromArray } from 'snjs'; +import { removeFromArray, UuidString } from 'snjs'; import { Editor } from './editor'; import { WebApplication } from './application'; @@ -21,8 +21,12 @@ export class EditorGroup { } } - createEditor(noteUuid?: string, noteTitle?: string) { - const editor = new Editor(this.application, noteUuid, noteTitle); + createEditor( + noteUuid?: string, + noteTitle?: string, + noteTag?: UuidString + ) { + const editor = new Editor(this.application, noteUuid, noteTitle, noteTag); this.editors.push(editor); this.notifyObservers(); } @@ -72,4 +76,4 @@ export class EditorGroup { observer(); } } -} \ No newline at end of file +}