-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNote.vue
More file actions
250 lines (223 loc) · 6.29 KB
/
Note.vue
File metadata and controls
250 lines (223 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<template>
<div>
<NoteHeader
:style="{ '--opacity': id && note ? 1 : 0 }"
>
<template #left>
<BreadCrumbs
:note-parents="
note && noteId ?
[...noteParents, { id: noteId, content: noteTitle }] :
noteParents
"
/>
</template>
<template #right>
<div class="last_edit">
{{
note && 'updatedAt' in note && note.updatedAt
? t('note.lastEdit') + ' ' + getTimeFromNow(note.updatedAt)
: t('note.lastEdit') + ' ' + 'a few seconds ago'
}}
</div>
<Button
v-if="canEdit"
secondary
icon="Plus"
@click="createChildNote"
/>
<!-- @todo add icon history to the button, it will be availible since codex icons 2.0 -->
<Button
v-if="canEdit"
secondary
@click="router.push(`/note/${noteId}/history`)"
>
History
</Button>
<Button
v-if="canEdit"
secondary
icon="EtcHorisontal"
@click="redirectToNoteSettings"
/>
</template>
</NoteHeader>
<div v-if="note === null">
Loading...
</div>
<div v-else>
<PageBlock>
<template #left>
<VerticalMenu
class="menu"
:items="[verticalMenuItems]"
/>
</template>
<template #default>
<Editor
v-if="isEditorReady"
ref="editor"
v-bind="editorConfig"
@change="noteChanged"
/>
</template>
</PageBlock>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, ref, toRef, watch } from 'vue';
import { Button, Editor, PageBlock, VerticalMenu, type VerticalMenuItem } from '@codexteam/ui/vue';
import useNote from '@/application/services/useNote';
import { useRoute, useRouter } from 'vue-router';
import { NoteContent } from '@/domain/entities/Note';
import { useHead } from 'unhead';
import { useI18n } from 'vue-i18n';
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 BreadCrumbs from '@/presentation/components/breadcrumbs/BreadCrumbs.vue';
import { NoteHierarchy } from '@/domain/entities/NoteHierarchy';
import { getTitle } from '@/infrastructure/utils/note';
import { getTimeFromNow } from '@/infrastructure/utils/date.ts';
const { t } = useI18n();
const router = useRouter();
const route = useRoute();
const props = defineProps<{
/**
* Null for new note, id for reading existing note
*/
id: string | null;
/**
* Parent note id, undefined for root note
*/
parentId?: string;
}>();
const noteId = toRef(props, 'id');
const { note, noteTools, save, noteTitle, canEdit, noteParents, noteHierarchy } = useNote({
id: noteId,
});
/**
* Create new child note
*/
function createChildNote(): void {
if (props.id === null) {
throw new Error('Note is Empty');
}
router.push(`/note/${props.id}/new`);
}
/**
* Move to note settings
*/
function redirectToNoteSettings(): void {
if (props.id === null) {
throw new Error('Note is Empty');
}
router.push(`/note/${props.id}/settings`);
}
const { updateCover } = useNoteSettings();
const { isEditorReady, editorConfig } = useNoteEditor({
noteTools,
isDraftResolver: () => noteId.value === null,
noteContentResolver: () => note.value?.content,
canEdit,
});
/**
* Editor component reference
*/
const editor = ref<typeof Editor | undefined>(undefined);
/**
* Callback for editor change. Saves the note
*
* @param data - editor data
*/
async function noteChanged(data: NoteContent): Promise<void> {
const isEmpty = editor.value?.isEmpty();
let updatedNoteCover: Blob | null = null;
/**
* Get html element with note
*/
const editorElement = editor.value ? editor.value.element : null;
if (!isEmpty) {
await save(data, props.parentId);
/**
* In case if we do not have note id, we can change its cover, and we need successful data for cover
* We need to do it after saving in case of note creation
*/
if (editorElement !== null) {
updatedNoteCover = await makeElementScreenshot(editorElement, {
background: 'var(--base--bg-primary)',
color: 'var(--base--text)',
display: 'flex',
justifyContent: 'center',
width: '1200px',
height: '900px',
paddingTop: '100px',
});
}
if (updatedNoteCover !== null && props.id !== null) {
await updateCover(props.id, updatedNoteCover);
}
}
}
/**
* Recursively transform the note hierarchy into a VerticalMenuItem
*
* @param noteHierarchyObj - note hierarchy data
* @param currentNoteTitle - actual title of note
* @returns menuItem - VerticalMenuItem
*/
function transformNoteHierarchy(noteHierarchyObj: NoteHierarchy | null, currentNoteTitle: string): VerticalMenuItem {
if (!noteHierarchyObj) {
return {
title: 'Untitled',
isActive: true,
items: undefined,
};
}
const title = noteHierarchyObj.content ? getTitle(noteHierarchyObj.content) : 'Untitled';
// Transform the current note into a VerticalMenuItem
return {
title: title,
isActive: route.path === `/note/${noteHierarchyObj.id}`,
items: noteHierarchyObj.childNotes ? noteHierarchyObj.childNotes.map(child => transformNoteHierarchy(child, currentNoteTitle)) : undefined,
onActivate: () => {
void router.push(`/note/${noteHierarchyObj.id}`);
},
};
}
const verticalMenuItems = computed<VerticalMenuItem>(() => {
return transformNoteHierarchy(noteHierarchy.value, noteTitle.value);
});
watch(
() => props.id,
() => {
/** If new child note is created, refresh editor with empty data */
if (props.id === null) {
useHead({
title: t('note.new'),
});
}
},
{ immediate: true }
);
watch(noteTitle, () => {
if (props.id !== null) {
useHead({
title: noteTitle.value,
});
}
});
</script>
<style scoped>
.menu {
flex-shrink: 0;
height: fit-content;
width: auto;
}
.last_edit {
color: var(--base--text-secondary);
padding-right: var(--h-padding);
}
</style>