Skip to content

Commit

Permalink
feat: recursively add tags to note on create (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentsenta committed Dec 21, 2021
1 parent 222363e commit b75b533
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
30 changes: 22 additions & 8 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,17 +174,31 @@ 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) {
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();
Expand Down
50 changes: 26 additions & 24 deletions app/assets/javascripts/ui_models/editor.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<TagMutator>(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);
Expand All @@ -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;
}

Expand Down

0 comments on commit b75b533

Please sign in to comment.