From b75b533eff31c5abbf65bb1c731568cfbe21fe7b Mon Sep 17 00:00:00 2001 From: Laurent Senta Date: Thu, 9 Dec 2021 10:34:02 +0100 Subject: [PATCH] feat: recursively add tags to note on create (#9) --- .../ui_models/app_state/note_tags_state.ts | 30 ++++++++--- app/assets/javascripts/ui_models/editor.ts | 50 ++++++++++--------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/app/assets/javascripts/ui_models/app_state/note_tags_state.ts b/app/assets/javascripts/ui_models/app_state/note_tags_state.ts index 3ad0900e8e9..65c4c63b7ac 100644 --- a/app/assets/javascripts/ui_models/app_state/note_tags_state.ts +++ b/app/assets/javascripts/ui_models/app_state/note_tags_state.ts @@ -174,17 +174,31 @@ export class NoteTagsState { } } + public static addTagHierarchyToNote( + application: WebApplication, + tag: SNTag, + note: SNNote + ): Promise { + const parentChainTags = application.getTagParentChain(tag); + const tagsToAdd = [...parentChainTags, tag]; + + return Promise.all( + tagsToAdd.map(async (tag) => { + await application.changeItem(tag.uuid, (mutator) => { + mutator.addItemAsRelationship(note); + }); + }) + ); + } + async addTagToActiveNote(tag: SNTag): Promise { const { activeNote } = this; + if (activeNote) { - const parentChainTags = this.application.getTagParentChain(tag); - const tagsToAdd = [...parentChainTags, tag]; - await Promise.all( - tagsToAdd.map(async (tag) => { - await this.application.changeItem(tag.uuid, (mutator) => { - mutator.addItemAsRelationship(activeNote); - }); - }) + await NoteTagsState.addTagHierarchyToNote( + this.application, + tag, + activeNote ); this.application.sync(); this.reloadTags(); diff --git a/app/assets/javascripts/ui_models/editor.ts b/app/assets/javascripts/ui_models/editor.ts index 606d239c647..4bc925ec487 100644 --- a/app/assets/javascripts/ui_models/editor.ts +++ b/app/assets/javascripts/ui_models/editor.ts @@ -1,14 +1,21 @@ -import { SNNote, ContentType, PayloadSource, UuidString, TagMutator } from '@standardnotes/snjs'; +import { + SNNote, + ContentType, + PayloadSource, + UuidString, + TagMutator, + SNTag, +} from '@standardnotes/snjs'; import { WebApplication } from './application'; +import { NoteTagsState } from './app_state/note_tags_state'; export class Editor { - - public note!: SNNote - private application: WebApplication - private _onNoteChange?: () => void - private _onNoteValueChange?: (note: SNNote, source?: PayloadSource) => void - private removeStreamObserver?: () => void - public isTemplateNote = false + public note!: SNNote; + private application: WebApplication; + private _onNoteChange?: () => void; + private _onNoteValueChange?: (note: SNNote, source?: PayloadSource) => void; + private removeStreamObserver?: () => void; + public isTemplateNote = false; constructor( application: WebApplication, @@ -66,22 +73,15 @@ export class Editor { * Reverts the editor to a blank state, removing any existing note from view, * and creating a placeholder note. */ - async reset( - noteTitle = '', - noteTag?: UuidString, - ) { - const note = await this.application.createTemplateItem( - ContentType.Note, - { - text: '', - title: noteTitle, - references: [] - } - ) as SNNote; + async reset(noteTitle = '', noteTag?: UuidString) { + const note = (await this.application.createTemplateItem(ContentType.Note, { + text: '', + title: noteTitle, + references: [], + })) as SNNote; if (noteTag) { - await this.application.changeItem(noteTag, (m) => { - m.addItemAsRelationship(note); - }); + const tag = this.application.findItem(noteTag) as SNTag; + await NoteTagsState.addTagHierarchyToNote(this.application, tag, note); } if (!this.isTemplateNote || this.note.title !== note.title) { this.setNote(note as SNNote, true); @@ -106,7 +106,9 @@ export class Editor { * Register to be notified when the editor's note's values change * (and thus a new object reference is created) */ - public onNoteValueChange(callback: (note: SNNote, source?: PayloadSource) => void) { + public onNoteValueChange( + callback: (note: SNNote, source?: PayloadSource) => void + ) { this._onNoteValueChange = callback; }