Skip to content

Commit

Permalink
fix: use add tags folder hierarchy & isTemplateItem (#13)
Browse files Browse the repository at this point in the history
* fix: use new snjs add tag hierarchy

* fix: use new snjs isTemplateItem
  • Loading branch information
laurentsenta committed Dec 23, 2021
1 parent c2b6168 commit 592cd88
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 33 deletions.
23 changes: 1 addition & 22 deletions app/assets/javascripts/ui_models/app_state/note_tags_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,32 +174,11 @@ export class NoteTagsState {
}
}

public static addTagHierarchyToNote(
application: WebApplication,
tag: SNTag,
note: SNNote
): Promise<unknown> {
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<void> {
const { activeNote } = this;

if (activeNote) {
await NoteTagsState.addTagHierarchyToNote(
this.application,
tag,
activeNote
);
await this.application.addTagHierarchyToNote(activeNote, tag);
this.application.sync();
this.reloadTags();
}
Expand Down
18 changes: 8 additions & 10 deletions app/assets/javascripts/ui_models/app_state/tags_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,16 @@ export class TagsState {
return [];
}

try {
const children = this.application.getTagChildren(tag);
const childrenUuids = children.map((childTag) => childTag.uuid);
const childrenTags = this.tags.filter((tag) =>
childrenUuids.includes(tag.uuid)
);
return childrenTags;
} catch {
// In the case of template tags, this code throw,
// We implement a general catch-all for now.
if (this.application.isTemplateItem(tag)) {
return [];
}

const children = this.application.getTagChildren(tag);
const childrenUuids = children.map((childTag) => childTag.uuid);
const childrenTags = this.tags.filter((tag) =>
childrenUuids.includes(tag.uuid)
);
return childrenTags;
}

isValidTagParent(parentUuid: UuidString, tagUuid: UuidString): boolean {
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/ui_models/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Editor {
})) as SNNote;
if (noteTag) {
const tag = this.application.findItem(noteTag) as SNTag;
await NoteTagsState.addTagHierarchyToNote(this.application, tag, note);
await this.application.addTagHierarchyToNote(note, tag);
}
if (!this.isTemplateNote || this.note.title !== note.title) {
this.setNote(note as SNNote, true);
Expand Down

0 comments on commit 592cd88

Please sign in to comment.