Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: display the note parents structure #275

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
44 changes: 44 additions & 0 deletions src/application/services/useNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ interface UseNoteComposableState {
*/
unlinkParent: () => Promise<void>;

/**
* Returns an array of note parents for the current note.
*/
noteParents: Ref<Note[]>;

/**
* Defines if user can edit note
*/
Expand Down Expand Up @@ -158,6 +163,13 @@ export default function (options: UseNoteComposableOptions): UseNoteComposableSt
*/
const parentNote = ref<Note | undefined>(undefined);

/**
* Note parents of the actual note
*
* Actual note by default
*/
const noteParents = ref<Note[]>([]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that making noteParents reactive is a bad thing to do
We can update them only by request to api /GET note so i can't see the case, when we update noteParents and imidiately change displayed content somewhere

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still actual


/**
* Load note by id
* @param id - Note identifier got from composable argument
Expand All @@ -172,6 +184,7 @@ export default function (options: UseNoteComposableOptions): UseNoteComposableSt
canEdit.value = response.accessRights.canEdit;
noteTools.value = response.tools;
parentNote.value = response.parentNote;
noteParents.value = response.parents;
}

/**
Expand Down Expand Up @@ -265,6 +278,35 @@ export default function (options: UseNoteComposableOptions): UseNoteComposableSt
parentNote.value = undefined;
}

/**
* Reform the array of note parents by adding the actual note id and content.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still don't understand the problem. API returns id and content, why do we need to change them?

* @returns An array of Note objects representing the formatted note parents.
* @throws {Error} If the note id is not defined.
*/
function formatNoteParents(): Note[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method would be used only in component, so we should move this logic to computable displayedParents no need to produce extra entities (such as presentationFormat)

if (currentId.value === null) {
return [];
}
let presentationFormat: Note[] = [];

if (noteParents.value.length === 0) {
presentationFormat.push({
id: currentId.value,
content: note.value?.content as NoteContent,
});
Comment on lines +293 to +296
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you are adding current note as a parent? I don't think that it is a good idea

} else {
presentationFormat = noteParents.value;
if (noteParents.value.find(noteItem => noteItem.id === currentId.value) === undefined) {
presentationFormat.push({
id: currentId.value,
content: note.value?.content as NoteContent,
});
}
}

return presentationFormat;
}

/**
* Get note by custom hostname
*/
Expand Down Expand Up @@ -315,6 +357,7 @@ export default function (options: UseNoteComposableOptions): UseNoteComposableSt
});

watch(noteTitle, (currentNoteTitle) => {
noteParents.value = formatNoteParents();
patchOpenedPageByUrl(
route.path,
{
Expand All @@ -332,6 +375,7 @@ export default function (options: UseNoteComposableOptions): UseNoteComposableSt
resolveToolsByContent,
save,
unlinkParent,
noteParents,
parentNote,
};
}
5 changes: 5 additions & 0 deletions src/domain/entities/NoteDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ export interface NoteDTO {
* Editor tools
*/
tools: EditorTool[];

/**
* Note parents
*/
parents: Note[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export type GetNoteResponsePayload = {
accessRights: NoteAccessRights;
parentNote: Note | undefined;
tools: EditorTool[];
parents: Note[];
};
72 changes: 62 additions & 10 deletions src/presentation/pages/Note.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
:style="{ '--opacity': id && note ? 1 : 0 }"
>
<template #left>
{{
note && 'updatedAt' in note && note.updatedAt
? t('note.lastEdit') + ' ' + getTimeFromNow(note.updatedAt)
: t('note.lastEdit') + ' ' + 'a few seconds ago'
}}
<div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

div seems redundant

<RouterLink
v-for="(parent, index) in displayedParents"
:key="index"
:to="{ path: `/note/${parent.id ? parent.id : noteId}` }"
@click.prevent="handleParentClick($event, parent)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably you don't need to handle click manually since you are using router link

>
{{ getTitle(parent.content) }}
<span v-if="index < displayedParents.length - 1"> > </span>
</RouterLink>
</div>
</template>
<template #right>
<Button
Expand Down Expand Up @@ -48,18 +54,19 @@
</template>

<script lang="ts" setup>
import { ref, toRef, watch } from 'vue';
import { computed, ref, toRef, watch } from 'vue';
import { OutputData } from '@editorjs/editorjs';
import { Button, Editor } from 'codex-ui/vue';
import useNote from '@/application/services/useNote';
import { useRouter } from 'vue-router';
import { NoteContent } from '@/domain/entities/Note';
import { RouterLink, useRouter } from 'vue-router';
import { Note, NoteContent } from '@/domain/entities/Note';
import { useHead } from 'unhead';
import { useI18n } from 'vue-i18n';
import { getTimeFromNow } from '@/infrastructure/utils/date';
import { makeElementScreenshot } from '@/infrastructure/utils/screenshot';
import useNoteSettings from '@/application/services/useNoteSettings';
import { useNoteEditor } from '@/application/services/useNoteEditor';
import NoteHeader from '@/presentation/components/note-header/NoteHeader.vue';
import { getTitle } from '@/infrastructure/utils/note';

const { t } = useI18n();

Expand All @@ -79,7 +86,7 @@ const props = defineProps<{

const noteId = toRef(props, 'id');

const { note, noteTools, save, noteTitle, canEdit } = useNote({
const { note, noteTools, save, noteTitle, canEdit, noteParents } = useNote({
id: noteId,
});

Expand All @@ -103,6 +110,51 @@ function redirectToNoteSettings(): void {
router.push(`/note/${props.id}/settings`);
}

/**
* Navigate to a specific note
*
* @param {number} parentId - The ID of the parent note to navigate to
*/
function navigateToParent(parentId: string): void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems redundant

router.push(`/note/${parentId}`);
}

/**
* Handle parent click
*
* @param {Event} event - The click event
* @param {object} parent - The parent object
* @param {string} parent.id - The ID of the parent note
*/
function handleParentClick(event: Event, parent: Note): void {
if (getTitle(parent.content) !== '...') {
navigateToParent(parent.id);
} else {
event.preventDefault();
}
}

/**
* Displayed parents
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad description

*/
const displayedParents = computed(() => {
if (noteParents.value.length > 3) {
const newNoteContent = { blocks: [] } as OutputData;

newNoteContent.blocks.push({ type: 'paraghraph',
data: { text: '...' } });

return [
noteParents.value[0],
{ id: '',
content: newNoteContent },
noteParents.value[noteParents.value.length - 1],
];
}

return noteParents.value;
});
Comment on lines +140 to +156
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would suggest moving all of this logic to different component of Notex (not codex-ui), then we will not overcomplicate Note page

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is discussable, @neSpecc what do you think about this?


const { updateCover } = useNoteSettings();

const { isEditorReady, editorConfig } = useNoteEditor({
Expand Down
Loading