From d97cfc476876b7f5c4a74c1bf8ccd0f56411ec3f Mon Sep 17 00:00:00 2001 From: Baptiste Grob <60621355+baptiste-grob@users.noreply.github.com> Date: Wed, 21 Oct 2020 14:43:18 +0200 Subject: [PATCH] fix: reflect tag deletion from synced note --- .../javascripts/views/editor/editor_view.ts | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/app/assets/javascripts/views/editor/editor_view.ts b/app/assets/javascripts/views/editor/editor_view.ts index d70e3a706..c984c7148 100644 --- a/app/assets/javascripts/views/editor/editor_view.ts +++ b/app/assets/javascripts/views/editor/editor_view.ts @@ -112,6 +112,8 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { public editorValues: EditorValues = {} onEditorLoad?: () => void + private tags: SNTag[] = []; + private removeAltKeyObserver?: any private removeTrashKeyObserver?: any private removeTabObserver?: any @@ -165,6 +167,7 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { this.statusTimeout = undefined; (this.onPanelResizeFinish as any) = undefined; (this.editorMenuOnSelect as any) = undefined; + this.tags = []; super.deinit(); } @@ -186,7 +189,7 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { if (isPayloadSourceRetrieved(source!)) { this.editorValues.title = note.title; this.editorValues.text = note.text; - this.reloadTagsString(); + this.reloadTags(); } if (!this.editorValues.title) { this.editorValues.title = note.title; @@ -285,7 +288,7 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { this.editorValues.title = note.title; this.editorValues.text = note.text; this.reloadEditor(); - this.reloadTagsString(); + this.reloadTags(); this.reloadPreferences(); this.reloadStackComponents(); this.reloadNoteTagsComponent(); @@ -312,20 +315,8 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { streamItems() { this.removeTagsObserver = this.application.streamItems( ContentType.Tag, - (items) => { - if (!this.note) { - return; - } - for (const tag of items) { - if ( - !this.editorValues.tagsInputValue || - tag.deleted || - tag.hasRelationshipWithItem(this.note) - ) { - this.reloadTagsString(); - break; - } - } + () => { + this.reloadTags(); } ); @@ -790,8 +781,31 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { ); } - async reloadTagsString() { + async reloadTags() { + if (!this.note) { + return; + } const tags = this.appState.getNoteTags(this.note); + if (tags.length !== this.tags.length) { + this.reloadTagsString(tags); + } else { + /** Check that all tags are the same */ + for (let i = 0; i < tags.length; i++) { + const localTag = this.tags[i]; + const tag = tags[i]; + if ( + tag.title !== localTag.title || + tag.uuid !== localTag.uuid + ) { + this.reloadTagsString(tags); + break; + } + } + } + this.tags = tags; + } + + private async reloadTagsString(tags: SNTag[]) { const string = SNTag.arrayToDisplayString(tags); await this.flushUI(); this.editorValues.tagsInputValue = string; @@ -872,7 +886,7 @@ class EditorViewCtrl extends PureViewCtrl<{}, EditorState> { ) } this.application.sync(); - this.reloadTagsString(); + this.reloadTags(); } async onPanelResizeFinish(width: number, left: number, isMaxWidth: boolean) {